Convert math/matrix.c to math/matrix_4x4.c

This commit is contained in:
twinaphex 2014-10-26 02:32:10 +01:00
parent 288c6941de
commit d24691e810
13 changed files with 103 additions and 101 deletions

View File

@ -365,7 +365,7 @@ ifeq ($(HAVE_OPENGL), 1)
gfx/context/gfx_null_ctx.o \ gfx/context/gfx_null_ctx.o \
gfx/fonts/gl_font.o \ gfx/fonts/gl_font.o \
gfx/fonts/gl_raster_font.o \ gfx/fonts/gl_raster_font.o \
libretro-sdk/gfx/math/matrix.o \ libretro-sdk/gfx/math/matrix_4x4.o \
gfx/state_tracker.o \ gfx/state_tracker.o \
libretro-sdk/glsym/rglgen.o libretro-sdk/glsym/rglgen.o

View File

@ -203,7 +203,7 @@ static void lakka_draw_icon(lakka_handle_t *lakka,
float scale_factor) float scale_factor)
{ {
struct gl_coords coords; struct gl_coords coords;
math_matrix mymat, mrot; math_matrix_4x4 mymat, mrot;
if (!lakka) if (!lakka)
return; return;
@ -242,12 +242,12 @@ static void lakka_draw_icon(lakka_handle_t *lakka,
coords.color = color; coords.color = color;
glBindTexture(GL_TEXTURE_2D, texture); glBindTexture(GL_TEXTURE_2D, texture);
matrix_rotate_z(&mrot, rotation); matrix_4x4_rotate_z(&mrot, rotation);
matrix_multiply(&mymat, &mrot, &gl->mvp_no_rot); matrix_4x4_multiply(&mymat, &mrot, &gl->mvp_no_rot);
math_matrix mscal; math_matrix_4x4 mscal;
matrix_scale(&mscal, scale_factor, scale_factor, 1); matrix_4x4_scale(&mscal, scale_factor, scale_factor, 1);
matrix_multiply(&mymat, &mscal, &mymat); matrix_4x4_multiply(&mymat, &mscal, &mymat);
gl->shader->set_coords(&coords); gl->shader->set_coords(&coords);
gl->shader->set_mvp(gl, &mymat); gl->shader->set_mvp(gl, &mymat);
@ -527,15 +527,15 @@ static void lakka_draw_fbo(lakka_handle_t *lakka)
coords.color = gl->white_color_ptr; coords.color = gl->white_color_ptr;
glBindTexture(GL_TEXTURE_2D, lakka->fbo_color); glBindTexture(GL_TEXTURE_2D, lakka->fbo_color);
math_matrix mymat; math_matrix_4x4 mymat;
math_matrix_4x4 mrot;
math_matrix mrot;
matrix_rotate_z(&mrot, 0);
matrix_multiply(&mymat, &mrot, &gl->mvp_no_rot);
math_matrix mscal; math_matrix mscal;
matrix_scale(&mscal, lakka->global_scale, lakka->global_scale, 1);
matrix_multiply(&mymat, &mscal, &mymat); matrix_4x4_rotate_z(&mrot, 0);
matrix_4x4_multiply(&mymat, &mrot, &gl->mvp_no_rot);
matrix_4x4_scale(&mscal, lakka->global_scale, lakka->global_scale, 1);
matrix_4x4_multiply(&mymat, &mscal, &mymat);
gl->shader->set_coords(&coords); gl->shader->set_coords(&coords);
gl->shader->set_mvp(gl, &mymat); gl->shader->set_mvp(gl, &mymat);

View File

@ -133,7 +133,7 @@ static void xmb_draw_icon(GLuint texture, float x, float y,
float alpha, float rotation, float scale_factor) float alpha, float rotation, float scale_factor)
{ {
struct gl_coords coords; struct gl_coords coords;
math_matrix mymat, mrot, mscal; math_matrix_4x4 mymat, mrot, mscal;
xmb_handle_t *xmb = (xmb_handle_t*)driver.menu->userdata; xmb_handle_t *xmb = (xmb_handle_t*)driver.menu->userdata;
if (!xmb) if (!xmb)
@ -173,11 +173,11 @@ static void xmb_draw_icon(GLuint texture, float x, float y,
coords.color = color; coords.color = color;
glBindTexture(GL_TEXTURE_2D, texture); glBindTexture(GL_TEXTURE_2D, texture);
matrix_rotate_z(&mrot, rotation); matrix_4x4_rotate_z(&mrot, rotation);
matrix_multiply(&mymat, &mrot, &gl->mvp_no_rot); matrix_4x4_multiply(&mymat, &mrot, &gl->mvp_no_rot);
matrix_scale(&mscal, scale_factor, scale_factor, 1); matrix_4x4_scale(&mscal, scale_factor, scale_factor, 1);
matrix_multiply(&mymat, &mscal, &mymat); matrix_4x4_multiply(&mymat, &mscal, &mymat);
gl->shader->set_coords(&coords); gl->shader->set_coords(&coords);
gl->shader->set_mvp(gl, &mymat); gl->shader->set_mvp(gl, &mymat);

View File

@ -759,10 +759,10 @@ static bool gl_init_hw_render(gl_t *gl, unsigned width, unsigned height)
static void gl_set_projection(gl_t *gl, struct gl_ortho *ortho, bool allow_rotate) static void gl_set_projection(gl_t *gl, struct gl_ortho *ortho, bool allow_rotate)
{ {
math_matrix rot; math_matrix_4x4 rot;
/* Calculate projection. */ /* Calculate projection. */
matrix_ortho(&gl->mvp_no_rot, ortho->left, ortho->right, matrix_4x4_ortho(&gl->mvp_no_rot, ortho->left, ortho->right,
ortho->bottom, ortho->top, ortho->znear, ortho->zfar); ortho->bottom, ortho->top, ortho->znear, ortho->zfar);
if (!allow_rotate) if (!allow_rotate)
@ -771,8 +771,8 @@ static void gl_set_projection(gl_t *gl, struct gl_ortho *ortho, bool allow_rotat
return; return;
} }
matrix_rotate_z(&rot, M_PI * gl->rotation / 180.0f); matrix_4x4_rotate_z(&rot, M_PI * gl->rotation / 180.0f);
matrix_multiply(&gl->mvp, &rot, &gl->mvp_no_rot); matrix_4x4_multiply(&gl->mvp, &rot, &gl->mvp_no_rot);
} }
void gl_set_viewport(gl_t *gl, unsigned width, void gl_set_viewport(gl_t *gl, unsigned width,

View File

@ -18,7 +18,7 @@
#include "../general.h" #include "../general.h"
#include "fonts/fonts.h" #include "fonts/fonts.h"
#include <gfx/math/matrix.h> #include <gfx/math/matrix_4x4.h>
#include "gfx_context.h" #include "gfx_context.h"
#include <gfx/scaler/scaler.h> #include <gfx/scaler/scaler.h>
#include "fonts/gl_font.h" #include "fonts/gl_font.h"
@ -222,7 +222,7 @@ typedef struct gl
unsigned last_width[MAX_TEXTURES]; unsigned last_width[MAX_TEXTURES];
unsigned last_height[MAX_TEXTURES]; unsigned last_height[MAX_TEXTURES];
unsigned tex_w, tex_h; unsigned tex_w, tex_h;
math_matrix mvp, mvp_no_rot; math_matrix_4x4 mvp, mvp_no_rot;
struct gl_coords coords; struct gl_coords coords;
const GLfloat *vertex_ptr; const GLfloat *vertex_ptr;

View File

@ -24,7 +24,7 @@
#endif #endif
#include "../gfx_context.h" #include "../gfx_context.h"
#include <gfx/math/matrix.h> #include <gfx/math/matrix_4x4.h>
typedef struct shader_backend typedef struct shader_backend
{ {
@ -44,7 +44,7 @@ typedef struct shader_backend
enum gfx_wrap_type (*wrap_type)(unsigned index); enum gfx_wrap_type (*wrap_type)(unsigned index);
void (*shader_scale)(unsigned index, struct gfx_fbo_scale *scale); void (*shader_scale)(unsigned index, struct gfx_fbo_scale *scale);
bool (*set_coords)(const void *data); bool (*set_coords)(const void *data);
bool (*set_mvp)(void *data, const math_matrix *mat); bool (*set_mvp)(void *data, const math_matrix_4x4 *mat);
unsigned (*get_prev_textures)(void); unsigned (*get_prev_textures)(void);
bool (*mipmap_input)(unsigned index); bool (*mipmap_input)(unsigned index);

View File

@ -166,7 +166,7 @@ static void gl_cg_reset_attrib(void)
cg_attrib_idx = 0; cg_attrib_idx = 0;
} }
static bool gl_cg_set_mvp(void *data, const math_matrix *mat) static bool gl_cg_set_mvp(void *data, const math_matrix_4x4 *mat)
{ {
(void)data; (void)data;
if (cg_active && prg[active_idx].mvp) if (cg_active && prg[active_idx].mvp)

View File

@ -1056,7 +1056,7 @@ static void gl_glsl_set_params(void *data, unsigned width, unsigned height,
} }
} }
static bool gl_glsl_set_mvp(void *data, const math_matrix *mat) static bool gl_glsl_set_mvp(void *data, const math_matrix_4x4 *mat)
{ {
(void)data; (void)data;
if (!glsl_enable || !glsl_shader->modern) if (!glsl_enable || !glsl_shader->modern)

View File

@ -412,7 +412,7 @@ static void hlsl_shader_scale(unsigned idx, struct gfx_fbo_scale *scale)
scale->valid = false; scale->valid = false;
} }
static bool hlsl_set_mvp(void *data, const math_matrix *mat) static bool hlsl_set_mvp(void *data, const math_matrix_4x4 *mat)
{ {
d3d_video_t *d3d = (d3d_video_t*)data; d3d_video_t *d3d = (d3d_video_t*)data;
LPDIRECT3DDEVICE d3d_device_ptr = (LPDIRECT3DDEVICE)d3d->dev; LPDIRECT3DDEVICE d3d_device_ptr = (LPDIRECT3DDEVICE)d3d->dev;

View File

@ -21,7 +21,7 @@
#include <compat/posix_string.h> #include <compat/posix_string.h>
#include "../state_tracker.h" #include "../state_tracker.h"
#include "../../dynamic.h" #include "../../dynamic.h"
#include <gfx/math/matrix.h> #include <gfx/math/matrix_4x4.h>
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
#include "../../config.h" #include "../../config.h"
@ -48,7 +48,7 @@ static void shader_null_set_params(void *data, unsigned width, unsigned height,
{ {
} }
static bool shader_null_set_mvp(void *data, const math_matrix *mat) static bool shader_null_set_mvp(void *data, const math_matrix_4x4 *mat)
{ {
#ifdef HAVE_OPENGL #ifdef HAVE_OPENGL
#ifndef NO_GL_FF_MATRIX #ifndef NO_GL_FF_MATRIX

View File

@ -173,7 +173,7 @@ VIDEO DRIVER
============================================================ */ ============================================================ */
#if defined(HAVE_OPENGL) #if defined(HAVE_OPENGL)
#include "../libretro-sdk/gfx/math/matrix.c" #include "../libretro-sdk/gfx/math/matrix_4x4.c"
#elif defined(GEKKO) #elif defined(GEKKO)
#ifdef HW_RVL #ifdef HW_RVL
#include "../wii/vi_encoder.c" #include "../wii/vi_encoder.c"

View File

@ -20,122 +20,125 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <gfx/math/matrix.h> #include <gfx/math/matrix_4x4.h>
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
void matrix_identity(math_matrix *mat) void matrix_4x4_identity(math_matrix_4x4 *mat)
{ {
unsigned i; unsigned i;
memset(mat, 0, sizeof(*mat)); memset(mat, 0, sizeof(*mat));
for (i = 0; i < 4; i++) for (i = 0; i < 4; i++)
MAT_ELEM(*mat, i, i) = 1.0f; MAT_ELEM_4X4(*mat, i, i) = 1.0f;
} }
void matrix_transpose(math_matrix *out, const math_matrix *in) void matrix_4x4_transpose(math_matrix_4x4 *out, const math_matrix_4x4 *in)
{ {
unsigned i, j; unsigned i, j;
math_matrix mat; math_matrix_4x4 mat;
for (i = 0; i < 4; i++) for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++) for (j = 0; j < 4; j++)
MAT_ELEM(mat, j, i) = MAT_ELEM(*in, i, j); MAT_ELEM_4X4(mat, j, i) = MAT_ELEM_4X4(*in, i, j);
*out = mat; *out = mat;
} }
void matrix_rotate_x(math_matrix *mat, float rad) void matrix_4x4_rotate_x(math_matrix_4x4 *mat, float rad)
{ {
float cosine = cosf(rad); float cosine = cosf(rad);
float sine = sinf(rad); float sine = sinf(rad);
matrix_identity(mat); matrix_4x4_identity(mat);
MAT_ELEM(*mat, 1, 1) = cosine; MAT_ELEM_4X4(*mat, 1, 1) = cosine;
MAT_ELEM(*mat, 2, 2) = cosine; MAT_ELEM_4X4(*mat, 2, 2) = cosine;
MAT_ELEM(*mat, 1, 2) = -sine; MAT_ELEM_4X4(*mat, 1, 2) = -sine;
MAT_ELEM(*mat, 2, 1) = sine; MAT_ELEM_4X4(*mat, 2, 1) = sine;
} }
void matrix_rotate_y(math_matrix *mat, float rad) void matrix_4x4_rotate_y(math_matrix_4x4 *mat, float rad)
{ {
float cosine = cosf(rad); float cosine = cosf(rad);
float sine = sinf(rad); float sine = sinf(rad);
matrix_identity(mat); matrix_4x4_identity(mat);
MAT_ELEM(*mat, 0, 0) = cosine; MAT_ELEM_4X4(*mat, 0, 0) = cosine;
MAT_ELEM(*mat, 2, 2) = cosine; MAT_ELEM_4X4(*mat, 2, 2) = cosine;
MAT_ELEM(*mat, 0, 2) = -sine; MAT_ELEM_4X4(*mat, 0, 2) = -sine;
MAT_ELEM(*mat, 2, 0) = sine; MAT_ELEM_4X4(*mat, 2, 0) = sine;
} }
void matrix_rotate_z(math_matrix *mat, float rad) void matrix_4x4_rotate_z(math_matrix_4x4 *mat, float rad)
{ {
float cosine = cosf(rad); float cosine = cosf(rad);
float sine = sinf(rad); float sine = sinf(rad);
matrix_identity(mat); matrix_4x4_identity(mat);
MAT_ELEM(*mat, 0, 0) = cosine; MAT_ELEM_4X4(*mat, 0, 0) = cosine;
MAT_ELEM(*mat, 1, 1) = cosine; MAT_ELEM_4X4(*mat, 1, 1) = cosine;
MAT_ELEM(*mat, 0, 1) = -sine; MAT_ELEM_4X4(*mat, 0, 1) = -sine;
MAT_ELEM(*mat, 1, 0) = sine; MAT_ELEM_4X4(*mat, 1, 0) = sine;
} }
void matrix_ortho(math_matrix *mat, void matrix_4x4_ortho(math_matrix_4x4 *mat,
float left, float right, float left, float right,
float bottom, float top, float bottom, float top,
float znear, float zfar) float znear, float zfar)
{ {
matrix_identity(mat); matrix_4x4_identity(mat);
float tx = -(right + left) / (right - left); float tx = -(right + left) / (right - left);
float ty = -(top + bottom) / (top - bottom); float ty = -(top + bottom) / (top - bottom);
float tz = -(zfar + znear) / (zfar - znear); float tz = -(zfar + znear) / (zfar - znear);
MAT_ELEM(*mat, 0, 0) = 2.0f / (right - left); MAT_ELEM_4X4(*mat, 0, 0) = 2.0f / (right - left);
MAT_ELEM(*mat, 1, 1) = 2.0f / (top - bottom); MAT_ELEM_4X4(*mat, 1, 1) = 2.0f / (top - bottom);
MAT_ELEM(*mat, 2, 2) = -2.0f / (zfar - znear); MAT_ELEM_4X4(*mat, 2, 2) = -2.0f / (zfar - znear);
MAT_ELEM(*mat, 0, 3) = tx; MAT_ELEM_4X4(*mat, 0, 3) = tx;
MAT_ELEM(*mat, 1, 3) = ty; MAT_ELEM_4X4(*mat, 1, 3) = ty;
MAT_ELEM(*mat, 2, 3) = tz; MAT_ELEM_4X4(*mat, 2, 3) = tz;
} }
void matrix_scale(math_matrix *out, float x, float y, void matrix_4x4_scale(math_matrix_4x4 *out, float x, float y,
float z) float z)
{ {
memset(out, 0, sizeof(*out)); memset(out, 0, sizeof(*out));
MAT_ELEM(*out, 0, 0) = x; MAT_ELEM_4X4(*out, 0, 0) = x;
MAT_ELEM(*out, 1, 1) = y; MAT_ELEM_4X4(*out, 1, 1) = y;
MAT_ELEM(*out, 2, 2) = z; MAT_ELEM_4X4(*out, 2, 2) = z;
MAT_ELEM(*out, 3, 3) = 1.0f; MAT_ELEM_4X4(*out, 3, 3) = 1.0f;
} }
void matrix_translate(math_matrix *out, float x, void matrix_4x4_translate(math_matrix_4x4 *out, float x,
float y, float z) float y, float z)
{ {
matrix_identity(out); matrix_4x4_identity(out);
MAT_ELEM(*out, 0, 3) = x; MAT_ELEM_4X4(*out, 0, 3) = x;
MAT_ELEM(*out, 1, 3) = y; MAT_ELEM_4X4(*out, 1, 3) = y;
MAT_ELEM(*out, 2, 3) = z; MAT_ELEM_4X4(*out, 2, 3) = z;
} }
void matrix_projection(math_matrix *out, float znear, void matrix_4x4_projection(math_matrix_4x4 *out, float znear,
float zfar) float zfar)
{ {
memset(out, 0, sizeof(*out)); memset(out, 0, sizeof(*out));
MAT_ELEM(*out, 0, 0) = znear; MAT_ELEM_4X4(*out, 0, 0) = znear;
MAT_ELEM(*out, 1, 1) = zfar; MAT_ELEM_4X4(*out, 1, 1) = zfar;
MAT_ELEM(*out, 2, 2) = (zfar + znear) / (zfar - znear); MAT_ELEM_4X4(*out, 2, 2) = (zfar + znear) / (zfar - znear);
MAT_ELEM(*out, 2, 3) = -2.0f * zfar * znear / (zfar - znear); MAT_ELEM_4X4(*out, 2, 3) = -2.0f * zfar * znear / (zfar - znear);
MAT_ELEM(*out, 3, 2) = -1.0f; MAT_ELEM_4X4(*out, 3, 2) = -1.0f;
} }
void matrix_multiply(math_matrix *out, void matrix_4x4_multiply(
const math_matrix *a, const math_matrix *b) math_matrix_4x4 *out,
const math_matrix_4x4 *a,
const math_matrix_4x4 *b)
{ {
unsigned r, c, k; unsigned r, c, k;
math_matrix mat; math_matrix_4x4 mat;
for (r = 0; r < 4; r++) for (r = 0; r < 4; r++)
{ {
@ -143,11 +146,10 @@ void matrix_multiply(math_matrix *out,
{ {
float dot = 0.0f; float dot = 0.0f;
for (k = 0; k < 4; k++) for (k = 0; k < 4; k++)
dot += MAT_ELEM(*a, r, k) * MAT_ELEM(*b, k, c); dot += MAT_ELEM_4X4(*a, r, k) * MAT_ELEM_4X4(*b, k, c);
MAT_ELEM(mat, r, c) = dot; MAT_ELEM_4X4(mat, r, c) = dot;
} }
} }
*out = mat; *out = mat;
} }

View File

@ -28,30 +28,30 @@
* to work on GLES 2.0 and modern GL variants. * to work on GLES 2.0 and modern GL variants.
*/ */
typedef struct math_matrix typedef struct math_matrix_4x4
{ {
float data[16]; float data[16];
} math_matrix; } math_matrix_4x4;
#define MAT_ELEM(mat, r, c) ((mat).data[4 * (c) + (r)]) #define MAT_ELEM_4X4(mat, r, c) ((mat).data[4 * (c) + (r)])
void matrix_identity(math_matrix *mat); void matrix_4x4_identity(math_matrix_4x4 *mat);
void matrix_transpose(math_matrix *out, const math_matrix *in); void matrix_4x4_transpose(math_matrix_4x4 *out, const math_matrix_4x4 *in);
void matrix_rotate_x(math_matrix *mat, float rad); void matrix_4x4_rotate_x(math_matrix_4x4 *mat, float rad);
void matrix_rotate_y(math_matrix *mat, float rad); void matrix_4x4_rotate_y(math_matrix_4x4 *mat, float rad);
void matrix_rotate_z(math_matrix *mat, float rad); void matrix_4x4_rotate_z(math_matrix_4x4 *mat, float rad);
void matrix_ortho(math_matrix *mat, void matrix_4x4_ortho(math_matrix_4x4 *mat,
float left, float right, float left, float right,
float bottom, float top, float bottom, float top,
float znear, float zfar); float znear, float zfar);
void matrix_multiply(math_matrix *out, const math_matrix *a, const math_matrix *b); void matrix_4x4_multiply(math_matrix_4x4 *out, const math_matrix_4x4 *a, const math_matrix_4x4 *b);
void matrix_scale(math_matrix *out, float x, float y, float z); void matrix_4x4_scale(math_matrix_4x4 *out, float x, float y, float z);
void matrix_translate(math_matrix *out, float x, float y, float z); void matrix_4x4_translate(math_matrix_4x4 *out, float x, float y, float z);
void matrix_projection(math_matrix *out, float znear, float zfar); void matrix_4x4_projection(math_matrix_4x4 *out, float znear, float zfar);
#endif #endif