diff --git a/shaders/vert.glsl b/shaders/vert.glsl index 2c9821a..8ce7bc9 100644 --- a/shaders/vert.glsl +++ b/shaders/vert.glsl @@ -4,10 +4,11 @@ layout(location=0) in vec3 position; layout(location=1) in vec3 vert_colour; uniform mat4 u_model; +uniform mat4 u_projection; out vec3 vert_colours; void main() { vert_colours = vert_colour; - gl_Position = u_model * vec4(position, 1.0f); + gl_Position = u_projection * u_model * vec4(position, 1.0f); }; diff --git a/src/main.cc b/src/main.cc index b7d6a3b..aa14b70 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,5 +1,6 @@ #include "glad/glad.h" #include "glm/gtc/type_ptr.hpp" +#include "glm/trigonometric.hpp" #include #include #include @@ -52,6 +53,10 @@ struct App { glm::mat4 model_mat; GLint u_model_idx; + // Projection matrix to be sent as uniform to the vertex shader + glm::mat4 projection_mat; + GLint u_projection_idx; + bool running; }; @@ -123,8 +128,13 @@ int init(App &app) { glGetString(GL_SHADING_LANGUAGE_VERSION) ); - app.speed = 0.02f; - app.model_mat = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f)); + app.speed = 0.02f; + app.model_mat = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f)); + app.projection_mat = glm::perspective(glm::radians(60.0f), + (float)WINDOW_WIDTH / (float)WINDOW_HEIGHT, + 0.1f, + 20.0f + ); return EXIT_CODE_SUCCESS; } @@ -132,17 +142,17 @@ int init(App &app) { void create_vertex_spec(App &app) { const std::vector vertices = { // vert0 - -0.5f, -0.5f, 0.0f, // position - 1.0f, 0.0f, 0.0f, // colour + -0.5f, -0.5f, -1.5f, // position + 1.0f, 0.0f, 0.0f, // colour // vert1 - 0.5f, -0.5f, 0.0f, // position - 0.0f, 1.0f, 0.0f, // colour + 0.5f, -0.5f, -1.5f, // position + 0.0f, 1.0f, 0.0f, // colour // vert2 - -0.5f, 0.5f, 0.0f, // position - 0.0f, 0.0f, 1.0f, // colour + -0.5f, 0.5f, -1.5f, // position + 0.0f, 0.0f, 1.0f, // colour // vert3 - 0.5f, 0.5f, 0.0f, // position - 1.0f, 1.0f, 0.0f, // colour + 0.5f, 0.5f, -1.5f, // position + 1.0f, 1.0f, 0.0f, // colour }; const std::vector indices = { @@ -210,6 +220,12 @@ void create_graphics_pipeline(App &app) { if (app.u_model_idx < 0) { printf("Failed to find uniform %s\n", u_model); } + + const char *u_projection = "u_projection"; + app.u_projection_idx = glGetUniformLocation(app.shader_program, u_projection); + if (app.u_projection_idx < 0) { + printf("Failed to find uniform %s\n", u_projection); + } } void run_main_loop(App &app) { @@ -242,6 +258,9 @@ void run_main_loop(App &app) { if (app.u_model_idx >= 0) { glUniformMatrix4fv(app.u_model_idx, 1, GL_FALSE, glm::value_ptr(app.model_mat)); } + if (app.u_projection_idx >= 0) { + glUniformMatrix4fv(app.u_projection_idx, 1, GL_FALSE, glm::value_ptr(app.projection_mat)); + } // End pre draw setup // Draw call