From 3bb99b5a26ed1e0bedf11b6bd5f9982477840d34 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Thu, 11 Jan 2024 23:31:54 +0100 Subject: [PATCH] src/graphics: corrected aspect ratio and optimized matrices --- include/graphics.h | 62 +---------------------------------------- src/graphics/draw.c | 32 ++++++++++----------- src/graphics/graphics.c | 12 ++++---- 3 files changed, 23 insertions(+), 83 deletions(-) diff --git a/include/graphics.h b/include/graphics.h index e95ab44..9c64c60 100644 --- a/include/graphics.h +++ b/include/graphics.h @@ -30,6 +30,7 @@ #include #include #include +#include #define VERTEX_SHADER_FILE "src/graphics/shaders/shader.vert" #define FRAG_SHADER_FILE "src/graphics/shaders/shader.frag" @@ -163,67 +164,6 @@ void graphics_draw_line (const int stack_id, GLuint a, GLuint b); */ void graphics_draw_plan (const int stack_id, GLuint a, GLuint b, GLuint c); -/* - * Initializes an identity matrix - * - * @param res, target ptr - * - * @return void - */ -static inline void compute_i(float *res) -{ - /* initialize to the identity matrix */ - res[0] = 1.f; res[4] = 0.f; res[8] = 0.f; res[12] = 0.f; - res[1] = 0.f; res[5] = 1.f; res[9] = 0.f; res[13] = 0.f; - res[2] = 0.f; res[6] = 0.f; res[10] = 1.f; res[14] = 0.f; - res[3] = 0.f; res[7] = 0.f; res[11] = 0.f; res[15] = 1.f; -} - -/* - * Computes the projection matrix - * - * @param res, target ptr - * phi, XXX - * theta, XXX - * psi, XXX - * - * @return void - */ -static inline void compute_mvp(float *res, float phi, float theta, float psi) -{ - float x = phi *(G_PI / 180.f); - float y = theta *(G_PI / 180.f); - float z = psi *(G_PI / 180.f); - float c1 = cosf(x), s1 = sinf(x); - float c2 = cosf(y), s2 = sinf(y); - float c3 = cosf(z), s3 = sinf(z); - float c3c2 = c3 * c2; - float s3c1 = s3 * c1; - float c3s2s1 = c3 * s2 * s1; - float s3s1 = s3 * s1; - float c3s2c1 = c3 * s2 * c1; - float s3c2 = s3 * c2; - float c3c1 = c3 * c1; - float s3s2s1 = s3 * s2 * s1; - float c3s1 = c3 * s1; - float s3s2c1 = s3 * s2 * c1; - float c2s1 = c2 * s1; - float c2c1 = c2 * c1; - - compute_i(res); - - /* apply all three rotations using the three matrices: - * - * ⎡ c3 s3 0 ⎤ ⎡ c2 0 -s2 ⎤ ⎡ 1 0 0 ⎤ - * ⎢ -s3 c3 0 ⎥ ⎢ 0 1 0 ⎥ ⎢ 0 c1 s1 ⎥ - * ⎣ 0 0 1 ⎦ ⎣ s2 0 c2 ⎦ ⎣ 0 -s1 c1 ⎦ - */ - res[0] = c3c2; res[4] = s3c1 + c3s2s1; res[8] = s3s1 - c3s2c1; res[12] = 0.f; - res[1] = -s3c2; res[5] = c3c1 - s3s2s1; res[9] = c3s1 + s3s2c1; res[13] = 0.f; - res[2] = s2; res[6] = -c2s1; res[10] = c2c1; res[14] = 0.f; - res[3] = 0.f; res[7] = 0.f; res[11] = 0.f; res[15] = 1.f; -} - /* * Created and compile a shader * diff --git a/src/graphics/draw.c b/src/graphics/draw.c index 34437bc..7b9732c 100644 --- a/src/graphics/draw.c +++ b/src/graphics/draw.c @@ -32,6 +32,7 @@ #include #include +#include #include "../../include/base.h" #include "../../include/ui.h" #include "../../include/graphics.h" @@ -154,21 +155,20 @@ void graphics_draw(const int stack_id) //print_stack(stack_id); - float m[16]; - float v[16]; - float p[16]; + GLint cur_viewport[4]; + glGetIntegerv(GL_VIEWPORT, cur_viewport); - // XXX TODO get stack from stack_id + mat4 m = GLM_MAT4_IDENTITY_INIT; + glm_rotate_x(m, glm_rad(stack->rotation_angles[X_AXIS]), m); + glm_rotate_y(m, glm_rad(stack->rotation_angles[Y_AXIS]), m); + glm_rotate_z(m, glm_rad(stack->rotation_angles[Z_AXIS]), m); - /* Compute the model view projection matrix using the - * rotation angles specified through the GtkRange widgets - */ - compute_mvp(p, - stack->rotation_angles[X_AXIS], - stack->rotation_angles[Y_AXIS], - stack->rotation_angles[Z_AXIS]); - compute_i(m); - compute_i(v); + mat4 v = GLM_MAT4_IDENTITY_INIT; // XXX define zoom and translations here ? + + mat4 p = GLM_MAT4_IDENTITY_INIT; + //glm_ortho(-1.0f, +1.0f, -1.0f, +1.0f, -1.0f, +1.0f, p); + glm_ortho_default((float)cur_viewport[2] / (float)cur_viewport[3], p); + //glm_perspective_default((float)cur_viewport[2] / (float)cur_viewport[3], p); /* Use our shaders */ glUseProgram(stack->program); @@ -177,9 +177,9 @@ void graphics_draw(const int stack_id) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* Update the "mvp" matrix we use in the shader */ - glUniformMatrix4fv(stack->m, 1, GL_FALSE, &m[0]); - glUniformMatrix4fv(stack->v, 1, GL_FALSE, &v[0]); - glUniformMatrix4fv(stack->p, 1, GL_FALSE, &p[0]); + glUniformMatrix4fv(stack->m, 1, GL_FALSE, (float *)m); + glUniformMatrix4fv(stack->v, 1, GL_FALSE, (float *)v); + glUniformMatrix4fv(stack->p, 1, GL_FALSE, (float *)p); /* Use the vertices in our buffer */ glEnableVertexAttribArray(0); diff --git a/src/graphics/graphics.c b/src/graphics/graphics.c index e28a72e..142c285 100644 --- a/src/graphics/graphics.c +++ b/src/graphics/graphics.c @@ -275,12 +275,12 @@ void graphics_model_setup (const int stack_id) /*------------------------------------------------------------------------*/ - draw_space_ridges_vertex (stack_id, stack->buffer_vertex_size, - space_X, space_Y, space_Z); - draw_space_ridges_lines (stack_id); - draw_grids_on_space_faces_vertex (stack_id, space_X, space_Y, space_Z); - draw_grids_on_space_faces_lines (stack_id, stack->buffer_lines_size, - space_X, space_Y, space_Z); + /* draw_space_ridges_vertex (stack_id, stack->buffer_vertex_size, */ + /* space_X, space_Y, space_Z); */ + /* draw_space_ridges_lines (stack_id); */ + /* draw_grids_on_space_faces_vertex (stack_id, space_X, space_Y, space_Z); */ + /* draw_grids_on_space_faces_lines (stack_id, stack->buffer_lines_size, */ + /* space_X, space_Y, space_Z); */ stack->buffer_vertex_0_arrow = stack->buffer_vertex_size;