diff --git a/client/src/gfx/shape_renderer.cpp b/client/src/gfx/shape_renderer.cpp index a61f0f4..3dbeb09 100644 --- a/client/src/gfx/shape_renderer.cpp +++ b/client/src/gfx/shape_renderer.cpp @@ -1,36 +1,10 @@ #include #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", diff --git a/client/src/gfx/txt_renderer.cpp b/client/src/gfx/txt_renderer.cpp index 191540e..f0012d8 100644 --- a/client/src/gfx/txt_renderer.cpp +++ b/client/src/gfx/txt_renderer.cpp @@ -5,35 +5,11 @@ #include #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", diff --git a/common/src/math/math.h b/common/src/math/math.h new file mode 100644 index 0000000..5a694c2 --- /dev/null +++ b/common/src/math/math.h @@ -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 */