[Refactor] Consolidate duplicated matrix code into math util

This commit is contained in:
Ritchie Cunningham 2025-09-27 23:13:11 +01:00
parent cf48324516
commit e8ca1630c6
3 changed files with 33 additions and 54 deletions

View File

@ -1,36 +1,10 @@
#include <GL/glew.h>
#include "shape_renderer.h"
/* Yep, definately need a math lib, for now, this is just copy form txt_renderer
* TODO: Refactor to math lib.
*/
void ortho_proj(float* mat, float left, float right, float bottom, float top,
float near, float far) {
mat[0] = 2.0f / (right - left);
mat[4] = 0.0f;
mat[8] = 0.0f;
mat[12] = -(right + left) / (right - left);
mat[1] = 0.0f;
mat[5] = 2.0f / (top - bottom);
mat[9] = 0.0f;
mat[13] = -(top + bottom) / (top - bottom);
mat[2] = 0.0f;
mat[6] = 0.0f;
mat[10] = -2.0f / (far - near);
mat[14] = -(far + near) / (far - near);
mat[3] = 0.0f;
mat[7] = 0.0f;
mat[11] = 0.0f;
mat[15] = 1.0f;
}
#include "math/math.h"
ShapeRenderer::ShapeRenderer(unsigned int screen_width, unsigned int screen_height) {
ortho_proj(_projection, 0.0f, (float)screen_width, 0.0f, (float)screen_height, -1.0f, 1.0f);
math::ortho_proj(_projection, 0.0f, (float)screen_width, 0.0f, (float)screen_height, -1.0f, 1.0f);
/* Load shader. */
_shape_shader = new Shader("assets/shaders/shape.vert",

View File

@ -5,35 +5,11 @@
#include <freetype/freetype.h>
#include "txt_renderer.h"
/* Not sure we'll need a whole math lib? Basic ortho for now. */
void ortho(float* mat, float left, float right, float bottom, float top,
float near, float far) {
mat[0] = 2.0f / (right - left);
mat[4] = 0.0f;
mat[8] = 0.0f;
mat[12] = -(right + left) / (right - left);
mat[1] = 0.0f;
mat[5] = 2.0f / (top - bottom);
mat[9] = 0.0f;
mat[13] = -(top + bottom) / (top - bottom);
mat[2] = 0.0f;
mat[6] = 0.0f;
mat[10] = -2.0f / (far - near);
mat[14] = -(far + near) / (far - near);
mat[3] = 0.0f;
mat[7] = 0.0f;
mat[11] = 0.0f;
mat[15] = 1.0f;
}
#include "math/math.h"
TextRenderer::TextRenderer(unsigned int screen_width, unsigned int screen_height) {
/* Create projection matrix. */
ortho(_projecton, 0.0f, (float)screen_width, 0.0f, (float)screen_height, -1.0f, 1.0f);
math::ortho_proj(_projecton, 0.0f, (float)screen_width, 0.0f, (float)screen_height, -1.0f, 1.0f);
/* Load shader. */
_txt_shader = new Shader("assets/shaders/text.vert",

29
common/src/math/math.h Normal file
View File

@ -0,0 +1,29 @@
#pragma once
namespace math {
inline void ortho_proj(float* mat, float left, float right, float bottom, float top,
float near, float far) {
mat[0] = 2.0f / (right - left);
mat[4] = 0.0f;
mat[8] = 0.0f;
mat[12] = -(right + left) / (right - left);
mat[1] = 0.0f;
mat[5] = 2.0f / (top - bottom);
mat[9] = 0.0f;
mat[13] = -(top + bottom) / (top - bottom);
mat[2] = 0.0f;
mat[6] = 0.0f;
mat[10] = -2.0f / (far - near);
mat[14] = -(far + near) / (far - near);
mat[3] = 0.0f;
mat[7] = 0.0f;
mat[11] = 0.0f;
mat[15] = 1.0f;
}
} /* namespace math */