Setup projection matrix and position the quad properly

This commit is contained in:
Abdelrahman Said 2024-09-22 16:24:29 +01:00
parent 8738f57c38
commit 1f593b96ac
2 changed files with 31 additions and 11 deletions

View File

@ -4,10 +4,11 @@ layout(location=0) in vec3 position;
layout(location=1) in vec3 vert_colour; layout(location=1) in vec3 vert_colour;
uniform mat4 u_model; uniform mat4 u_model;
uniform mat4 u_projection;
out vec3 vert_colours; out vec3 vert_colours;
void main() { void main() {
vert_colours = vert_colour; vert_colours = vert_colour;
gl_Position = u_model * vec4(position, 1.0f); gl_Position = u_projection * u_model * vec4(position, 1.0f);
}; };

View File

@ -1,5 +1,6 @@
#include "glad/glad.h" #include "glad/glad.h"
#include "glm/gtc/type_ptr.hpp" #include "glm/gtc/type_ptr.hpp"
#include "glm/trigonometric.hpp"
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_error.h> #include <SDL2/SDL_error.h>
#include <SDL2/SDL_video.h> #include <SDL2/SDL_video.h>
@ -52,6 +53,10 @@ struct App {
glm::mat4 model_mat; glm::mat4 model_mat;
GLint u_model_idx; 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; bool running;
}; };
@ -123,8 +128,13 @@ int init(App &app) {
glGetString(GL_SHADING_LANGUAGE_VERSION) glGetString(GL_SHADING_LANGUAGE_VERSION)
); );
app.speed = 0.02f; app.speed = 0.02f;
app.model_mat = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f)); 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; return EXIT_CODE_SUCCESS;
} }
@ -132,17 +142,17 @@ int init(App &app) {
void create_vertex_spec(App &app) { void create_vertex_spec(App &app) {
const std::vector<GLfloat> vertices = { const std::vector<GLfloat> vertices = {
// vert0 // vert0
-0.5f, -0.5f, 0.0f, // position -0.5f, -0.5f, -1.5f, // position
1.0f, 0.0f, 0.0f, // colour 1.0f, 0.0f, 0.0f, // colour
// vert1 // vert1
0.5f, -0.5f, 0.0f, // position 0.5f, -0.5f, -1.5f, // position
0.0f, 1.0f, 0.0f, // colour 0.0f, 1.0f, 0.0f, // colour
// vert2 // vert2
-0.5f, 0.5f, 0.0f, // position -0.5f, 0.5f, -1.5f, // position
0.0f, 0.0f, 1.0f, // colour 0.0f, 0.0f, 1.0f, // colour
// vert3 // vert3
0.5f, 0.5f, 0.0f, // position 0.5f, 0.5f, -1.5f, // position
1.0f, 1.0f, 0.0f, // colour 1.0f, 1.0f, 0.0f, // colour
}; };
const std::vector<GLuint> indices = { const std::vector<GLuint> indices = {
@ -210,6 +220,12 @@ void create_graphics_pipeline(App &app) {
if (app.u_model_idx < 0) { if (app.u_model_idx < 0) {
printf("Failed to find uniform %s\n", u_model); 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) { void run_main_loop(App &app) {
@ -242,6 +258,9 @@ void run_main_loop(App &app) {
if (app.u_model_idx >= 0) { if (app.u_model_idx >= 0) {
glUniformMatrix4fv(app.u_model_idx, 1, GL_FALSE, glm::value_ptr(app.model_mat)); 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 // End pre draw setup
// Draw call // Draw call