src/graphics: corrected aspect ratio and optimized matrices

This commit is contained in:
Adrien Bourmault 2024-01-11 23:31:54 +01:00
parent 0da55ac88b
commit 3bb99b5a26
Signed by: neox
GPG Key ID: 2974E1D5F25DFCC8
3 changed files with 23 additions and 83 deletions

View File

@ -30,6 +30,7 @@
#include <epoxy/gl.h>
#include <GL/glu.h>
#include <GL/glext.h>
#include <cglm/cglm.h>
#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
*

View File

@ -32,6 +32,7 @@
#include <stdlib.h>
#include <time.h>
#include <cglm/cglm.h>
#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);

View File

@ -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;