From d5429ad5af32b8eb420cd4809f0fdd59827d50c2 Mon Sep 17 00:00:00 2001 From: 0xdeadbeer <64986162+0xdeadbeer@users.noreply.github.com> Date: Sat, 14 Oct 2023 18:24:51 +0200 Subject: [PATCH] Main: add simple perspective rendering --- .gitignore | 2 ++ CMakeLists.txt | 6 +++-- main.c | 71 ++++++++++++++++++++++++++++++++++++-------------- shader.frag | 4 +-- shader.vert | 6 ++++- 5 files changed, 65 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index 990936c..24dd928 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .idea/ cmake-build-debug/ +build/ +.ccls-cache/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 9ed9e10..db509af 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,11 +13,13 @@ add_executable(${PROJECT_NAME} ${HEADER_FILES} ${SOURCE_FILES} ${SHADER_FILES}) # We need a CMAKE_DIR with some code to find external dependencies set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/") +find_package(PkgConfig REQUIRED) find_package(OpenGL REQUIRED) find_package(GLUT REQUIRED) find_package(GLEW REQUIRED) find_package(assimp REQUIRED) +find_package(cglm REQUIRED) -include_directories(${PROJECT_NAME} ${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS} ${GLEW_INCLUDE_DIRS} ${ASSIMP_INCLUDE_DIRS}) +include_directories(${PROJECT_NAME} ${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS} ${GLEW_INCLUDE_DIRS} ${ASSIMP_INCLUDE_DIRS} ${CGLM_INCLUDE_DIRS}) -target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES} ${GLUT_LIBRARIES} ${GLEW_LIBRARIES} ${ASSIMP_LIBRARIES}) \ No newline at end of file +target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES} ${GLUT_LIBRARIES} ${GLEW_LIBRARIES} ${ASSIMP_LIBRARIES} ${CGLM_LIBRARIES} m) \ No newline at end of file diff --git a/main.c b/main.c index 1ca0630..1a0653f 100644 --- a/main.c +++ b/main.c @@ -1,9 +1,8 @@ #include #include #include -#include -#include #include +#include unsigned int vao; unsigned int vbo; @@ -73,18 +72,6 @@ int load_shader(const char *path, unsigned int shader) { return 0; } -void display() { - glClearColor(0.0f, 0.0f, 0.0f, 0.0f); - glClear(GL_COLOR_BUFFER_BIT); - - glUseProgram(shader_program); - glBindVertexArray(vao); - - glDrawArrays(GL_TRIANGLES, 0, 3); - - glutSwapBuffers(); -} - int load_shaders() { glDeleteProgram(shader_program); shader_program = glCreateProgram(); @@ -122,11 +109,57 @@ int load_shaders() { glDeleteShader(vertex_shader); glDeleteShader(fragment_shader); - display(); - return 0; } +float degs = 0; + +void display() { + mat4 model; + vec3 model_axis = {0.5f, 0.5f, 0.5f}; + mat4 view; + vec3 view_translate = {0.0f, 0.0f, -3.0f}; + mat4 projection; + GLint viewport[4]; // viewport: x, y, width, height + + GLint model_uniform; + GLint view_uniform; + GLint projection_uniform; + + glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + glClear(GL_COLOR_BUFFER_BIT); + + glGetIntegerv(GL_VIEWPORT, viewport); + + glm_mat4_identity(model); + + glm_rotate(model, glm_rad((float) degs ), model_axis); + degs += 0.01f; + + model_uniform = glGetUniformLocation(shader_program, "model"); + glUniformMatrix4fv(model_uniform, 1, GL_FALSE, (float *) model); + + glm_mat4_identity(view); + glm_translate(view, view_translate); + + view_uniform = glGetUniformLocation(shader_program, "view"); + glUniformMatrix4fv(view_uniform, 1, GL_FALSE, (float *) view); + + glm_mat4_identity(projection); + glm_perspective(glm_rad(45.0f), (float)viewport[2]/(float)viewport[3], 0.01f, 100.0f, projection); + + projection_uniform = glGetUniformLocation(shader_program, "projection"); + glUniformMatrix4fv(projection_uniform, 1, GL_FALSE, (float *) projection); + + glUseProgram(shader_program); + glBindVertexArray(vao); + + glDrawArrays(GL_TRIANGLES, 0, 3); + + glutSwapBuffers(); + glutPostRedisplay(); +} + void keyboard(unsigned char key, int x, int y) { switch (key) { case '\x1B': @@ -137,11 +170,11 @@ void keyboard(unsigned char key, int x, int y) { case 'r': case 'R': if (load_shaders() != 0) { - fprintf(stderr, "Error: reloading shaders"); - break; + fprintf(stderr, "Error: reloading shaders\n"); + exit(EXIT_FAILURE); } - fprintf(stdout, "Status: successfully reloaded shaders"); + fprintf(stdout, "Status: successfully reloaded shaders\n"); break; default: diff --git a/shader.frag b/shader.frag index 38e8927..1faf9a6 100644 --- a/shader.frag +++ b/shader.frag @@ -3,6 +3,6 @@ in vec3 color; out vec4 output; void main() { -// output = vec4(color.xyz, 1.0f); - output = vec4(1.0, 1.0, 1.0, 1.0); + output = vec4(color.xyz, 1.0f); +// output = vec4(1.0, 1.0, 1.0, 1.0); } \ No newline at end of file diff --git a/shader.vert b/shader.vert index 7c49743..78d38d6 100644 --- a/shader.vert +++ b/shader.vert @@ -2,9 +2,13 @@ layout (location = 0) in vec3 pos; layout (location = 1) in vec3 col; +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + out vec3 color; void main() { - gl_Position = vec4(pos.x, pos.y, pos.z, 1.0f); + gl_Position = projection * view * model * vec4(pos.xyz, 1.0); color = col; } \ No newline at end of file