diff options
author | talha <talha@talhaamir.xyz> | 2024-03-12 04:47:19 +0500 |
---|---|---|
committer | talha <talha@talhaamir.xyz> | 2024-03-12 04:47:19 +0500 |
commit | b8b64ac409f78f3b7a1798aa645a44554f99868f (patch) | |
tree | 085aee04220a2b7d7e192bd10784502c1edefa5f | |
parent | 7cc462063081f613c50bfd872188a50b8300d151 (diff) |
Updated project, fixed implicit type conversion issues
-rw-r--r-- | build.bat | 8 | ||||
-rw-r--r-- | main.c | 126 | ||||
-rw-r--r-- | main.cpp | 15 |
3 files changed, 10 insertions, 139 deletions
@@ -10,9 +10,9 @@ REM -Od => (for learning) No Optimisation and leave everything as is REM -MT => Use the static library, package all the c REM runtime library into the executable REM ******************************************** -IF NOT EXIST w:\programming\sdl_base\build mkdir w:\programming\sdl_base\build -pushd w:\programming\sdl_base\build -set ProjectRoot=w:\programming\sdl_base +IF NOT EXIST w:\programming\learn_opengl_simple\build mkdir w:\programming\learn_opengl_simple\build +pushd w:\programming\learn_opengl_simple\build +set ProjectRoot=w:\programming\learn_opengl_simple set IncludePath=%ProjectRoot%\include set LibPath=%ProjectRoot%\libs set CommonIncludeFlags=/I"%IncludePath%" @@ -20,5 +20,5 @@ set CommonFileFlags=%IncludePath%\glad\glad.c set CommonLinkerFlags=-incremental:no %ProjectRoot%\libs\SDL2\SDL2.lib %ProjectRoot%\libs\SDL2\SDL2main.lib opengl32.lib shell32.lib set CommonCompilerFlags=-MT -nologo -Gm- -GR- -EHa- -Od -Oi -WX -W2 -wd4201 -wd4100 -wd4189 -FC -Z7 -cl %CommonCompilerFlags% %CommonIncludeFlags% %ProjectRoot%\main.c %CommonFileFlags% /link /LIBPATH:LibPath -subsystem:windows %CommonLinkerFlags% +cl %CommonCompilerFlags% %CommonIncludeFlags% %ProjectRoot%\main.cpp %CommonFileFlags% /link /LIBPATH:LibPath -subsystem:windows %CommonLinkerFlags% popd @@ -1,126 +0,0 @@ -#include <math.h> -#include <SDL2/SDL.h> -#include <glad/glad.h> -#include <stdio.h> - -int main(int argc, char *argv[]) -{ - int width = 1920; - int height = 1080; - - if (SDL_Init(SDL_INIT_VIDEO) != 0) - { - printf("Error initialising SDL2: %s\n", SDL_GetError()); - return 0; - }; - - // initialise window with opengl flag - SDL_Window *window = SDL_CreateWindow("SDL Test", - SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, - width, - height, - SDL_WINDOW_OPENGL); - - // create an opengl context - SDL_GLContext context = SDL_GL_CreateContext(window); - if (!context) - { - printf("OpenGL context creation failed: %s\n", SDL_GetError()); - return -1; - } - - // load glad - if (!gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress)) { - printf("Failed to initialize Glad\n"); - return 1; - } - - // opengl rendering and shader stuff here - // @todo: - // have to learn how to setup shaders and other things with modern opengl (4.5+) - const char* vertexSource = - "#version 330 core\n" - "layout(location = 0) in vec3 position;" - "void main() {" - " gl_Position = vec4(position, 1.0);" - "}"; - - const char* fragmentSource = - "#version 330 core\n" - "out vec4 fragColor;" - "void main() {" - " fragColor = vec4(1.0, 0.0, 0.0, 1.0);" - "}"; - - GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); - glShaderSource(vertexShader, 1, &vertexSource, NULL); - glCompileShader(vertexShader); - - GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); - glShaderSource(fragmentShader, 1, &fragmentSource, NULL); - glCompileShader(fragmentShader); - - GLuint shaderProgram = glCreateProgram(); - glAttachShader(shaderProgram, vertexShader); - glAttachShader(shaderProgram, fragmentShader); - glLinkProgram(shaderProgram); - glUseProgram(shaderProgram); - - glDeleteShader(vertexShader); - glDeleteShader(fragmentShader); - - GLfloat vertices[] = { - -0.5f, -0.5f, 0.0f, - 0.5f, -0.5f, 0.0f, - 0.0f, 0.5f, 0.0f - }; - - GLuint VBO, VAO; - glGenVertexArrays(1, &VAO); - glGenBuffers(1, &VBO); - - glBindVertexArray(VAO); - - glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0); - glEnableVertexAttribArray(0); - - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindVertexArray(0); - for(;;) - { - SDL_Event ev; - while(SDL_PollEvent(&ev)) - { - if (ev.type == SDL_QUIT) - { - return 0; - } - } - - // opengl rendering code here - // @note: this part is more or less the same - // as with my learn opengl experiments - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); - - glUseProgram(shaderProgram); - glBindVertexArray(VAO); - glDrawArrays(GL_TRIANGLES, 0, 3); - glBindVertexArray(0); - - SDL_GL_SwapWindow(window); - } - // opengl free calls - glDeleteVertexArrays(1, &VAO); - glDeleteBuffers(1, &VBO); - glDeleteProgram(shaderProgram); - // sdl free calls - SDL_GL_DeleteContext(context); - SDL_DestroyWindow(window); - SDL_Quit(); - return 0; -} @@ -5,9 +5,6 @@ #define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" -#pragma comment(lib, "SDL2.lib") -#pragma comment(lib, "SDL2main.lib") - /* @lookup: * - The normal matrix calculation in the fragment shader for the object affected by light has been mainly copied. * I have tried to understand the formula, and whilst it made some sense, it is not fully clear to me, and I cannot picture it yet. @@ -88,7 +85,7 @@ unsigned int create_shader_program(unsigned int vertex_shader, unsigned int frag } // =========================================================== MATH ================================================== -#define PI 3.14159265358979323846264338327950288 +#define PI 3.14159265358979323846264338327950288f #define Square(x) ((x)*(x)) #define To_Radian(x) ((x) * PI / 180.0f) #define To_Degree(x) ((x) * 180.0f / PI) @@ -864,8 +861,8 @@ int main(int argc, char* argv[]) Vec3 preset_up_dir = Vec3{ 0.0, 1.0, 0.0 }; float angle_yaw, angle_pitch, angle_roll; - angle_pitch = To_Radian(0.0); - angle_yaw = -To_Radian(90.0); + angle_pitch = (float)To_Radian(0.0f); + angle_yaw = (float)-To_Radian(90.0f); Vec3 camera_look = camera_look_around(angle_pitch, angle_yaw); @@ -878,7 +875,7 @@ int main(int argc, char* argv[]) uint32_t view_loc = glGetUniformLocation(shader_program, "View"); glUniformMatrix4fv(view_loc, 1, GL_TRUE, view.buffer); - Mat4 proj = perspective4m(To_Radian(90.0), (float)width / (float)height, 0.1, 100.0); + Mat4 proj = perspective4m((float)To_Radian(90.0), (float)width / (float)height, 0.1f, 100.0f); uint32_t proj_loc = glGetUniformLocation(shader_program, "Projection"); glUniformMatrix4fv(proj_loc, 1, GL_TRUE, proj.buffer); @@ -980,8 +977,8 @@ int main(int argc, char* argv[]) case (SDL_MOUSEMOTION): { SDL_MouseMotionEvent mouse_event = ev.motion; - float x_motion = mouse_event.xrel; - float y_motion = mouse_event.yrel; + float x_motion = (float)mouse_event.xrel; + float y_motion = (float)mouse_event.yrel; if (x_motion != 0.0 || y_motion != 0.0) { angle_yaw = angle_yaw + To_Radian(x_motion * 0.1f); |