RetroArch/source/matrices.c
Francisco José García García 51922ea5be Squashed 'deps/vitaGL/' changes from c816fec50f..2934af8af0
2934af8af0 Added Patreon sponsor link.
c8f18b6f0f Getting current program only when required for vglDrawObjects.
4c5d136b0d Added directive to enable vitaShaRK usage from cmd.
4a10df3be5 Minor adjustments and bugfixes.
14a0124acf Added GL_TEXTURE_LOD_BIAS support.
40c8c6205e Added GL_NONE def and fixed glUniform4f impl.
868079c51e Added glUniform4f implementation.
0a682cbad2 Typo fix.
be3ce61ae7 Added GL_DEPTH_BITS and GL_STENCIL_BITS support.
21e6d1d330 Added runtime shader compiler support.
696e40bc62 Beautify error handler code.
537b37b110 Added glUniform3fv implementation.
7dd1403015 Fixed GLenum size and added missing types defines.
0c75f27ff1 Moved to NEON optimized memcpy usage.
98951895de Added gluPerspective implementation.
23e0b0b309 Fix for vglInitExtended not working on sys app mode.
4989c33ef5 Run clang-format.
429f1c1d8a Added system mode support.
9231680d02 Initializing sceGxm before free mem checking on vglInitExtended.
091e5e7882 Added vglInitWithCustomSizes.
f4c646ea78 Added vglSetParamBufferSize.
1b9a063c41 Beautify some code.
089e81efc5 Fix for duplicated symbols
789dcbf812 Typo fix in readRGBA4444.
1514a4b2cb Disabling lto due to it being broken on vitasdk with gcc 9.1.
fca18d9ab7 Added support for RGBA4444 texture format.
d449f12808 Added support for RGB565 texture format.

git-subtree-dir: deps/vitaGL
git-subtree-split: 2934af8af083a9acf598ab75233c518a251c6f0d
2020-07-05 11:43:47 +02:00

229 lines
6.3 KiB
C

/*
* This file is part of vitaGL
* Copyright 2017, 2018, 2019, 2020 Rinnegatamante
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* matrices.c:
* Implementation for matrices related functions
*/
#include "shared.h"
matrix4x4 *matrix = NULL; // Current in-use matrix mode
static matrix4x4 modelview_matrix_stack[MODELVIEW_STACK_DEPTH]; // Modelview matrices stack
static uint8_t modelview_stack_counter = 0; // Modelview matrices stack counter
static matrix4x4 projection_matrix_stack[GENERIC_STACK_DEPTH]; // Projection matrices stack
static uint8_t projection_stack_counter = 0; // Projection matrices stack counter
GLboolean mvp_modified = GL_TRUE; // Check if ModelViewProjection matrix needs to be recreated
/*
* ------------------------------
* - IMPLEMENTATION STARTS HERE -
* ------------------------------
*/
void glMatrixMode(GLenum mode) {
// Changing current in use matrix
switch (mode) {
case GL_MODELVIEW: // Modelview matrix
matrix = &modelview_matrix;
break;
case GL_PROJECTION: // Projection matrix
matrix = &projection_matrix;
break;
default:
SET_GL_ERROR(GL_INVALID_ENUM)
break;
}
}
void glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble nearVal, GLdouble farVal) {
#ifndef SKIP_ERROR_HANDLING
// Error handling
if (phase == MODEL_CREATION) {
SET_GL_ERROR(GL_INVALID_OPERATION)
} else if ((left == right) || (bottom == top) || (nearVal == farVal)) {
SET_GL_ERROR(GL_INVALID_VALUE)
}
#endif
// Initializing ortho matrix with requested parameters
matrix4x4_init_orthographic(*matrix, left, right, bottom, top, nearVal, farVal);
mvp_modified = GL_TRUE;
}
void glFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble nearVal, GLdouble farVal) {
#ifndef SKIP_ERROR_HANDLING
// Error handling
if (phase == MODEL_CREATION) {
SET_GL_ERROR(GL_INVALID_OPERATION)
} else if ((left == right) || (bottom == top) || (nearVal < 0) || (farVal < 0)) {
SET_GL_ERROR(GL_INVALID_VALUE)
}
#endif
// Initializing frustum matrix with requested parameters
matrix4x4_init_frustum(*matrix, left, right, bottom, top, nearVal, farVal);
mvp_modified = GL_TRUE;
}
void glLoadIdentity(void) {
// Set current in use matrix to identity one
matrix4x4_identity(*matrix);
mvp_modified = GL_TRUE;
}
void glMultMatrixf(const GLfloat *m) {
matrix4x4 res;
// Properly ordering matrix to perform multiplication
matrix4x4 tmp;
int i, j;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
tmp[i][j] = m[j * 4 + i];
}
}
// Multiplicating passed matrix with in use one
matrix4x4_multiply(res, *matrix, tmp);
// Copying result to in use matrix
matrix4x4_copy(*matrix, res);
mvp_modified = GL_TRUE;
}
void glLoadMatrixf(const GLfloat *m) {
// Properly ordering matrix
int i, j;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
(*matrix)[i][j] = m[j * 4 + i];
}
}
mvp_modified = GL_TRUE;
}
void glTranslatef(GLfloat x, GLfloat y, GLfloat z) {
// Translating in use matrix
matrix4x4_translate(*matrix, x, y, z);
mvp_modified = GL_TRUE;
}
void glScalef(GLfloat x, GLfloat y, GLfloat z) {
// Scaling in use matrix
matrix4x4_scale(*matrix, x, y, z);
mvp_modified = GL_TRUE;
}
void glRotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z) {
#ifndef SKIP_ERROR_HANDLING
// Error handling
if (phase == MODEL_CREATION) {
SET_GL_ERROR(GL_INVALID_OPERATION)
}
#endif
// Performing rotation on in use matrix depending on user call
float rad = DEG_TO_RAD(angle);
if (x == 1.0f) {
matrix4x4_rotate_x(*matrix, rad);
}
if (y == 1.0f) {
matrix4x4_rotate_y(*matrix, rad);
}
if (z == 1.0f) {
matrix4x4_rotate_z(*matrix, rad);
}
mvp_modified = GL_TRUE;
}
void glPushMatrix(void) {
#ifndef SKIP_ERROR_HANDLING
// Error handling
if (phase == MODEL_CREATION) {
SET_GL_ERROR(GL_INVALID_OPERATION)
}
#endif
if (matrix == &modelview_matrix) {
#ifndef SKIP_ERROR_HANDLING
// Error handling
if (modelview_stack_counter >= MODELVIEW_STACK_DEPTH) {
SET_GL_ERROR(GL_STACK_OVERFLOW)
} else
#endif
// Copying current matrix into the matrix stack and increasing stack counter
matrix4x4_copy(modelview_matrix_stack[modelview_stack_counter++], *matrix);
} else if (matrix == &projection_matrix) {
#ifndef SKIP_ERROR_HANDLING
// Error handling
if (projection_stack_counter >= GENERIC_STACK_DEPTH) {
SET_GL_ERROR(GL_STACK_OVERFLOW)
} else
#endif
// Copying current matrix into the matrix stack and increasing stack counter
matrix4x4_copy(projection_matrix_stack[projection_stack_counter++], *matrix);
}
}
void glPopMatrix(void) {
#ifndef SKIP_ERROR_HANDLING
// Error handling
if (phase == MODEL_CREATION) {
SET_GL_ERROR(GL_INVALID_OPERATION)
}
#endif
if (matrix == &modelview_matrix) {
#ifndef SKIP_ERROR_HANDLING
// Error handling
if (modelview_stack_counter == 0) {
SET_GL_ERROR(GL_STACK_UNDERFLOW)
} else
#endif
// Copying last matrix on stack into current matrix and decreasing stack counter
matrix4x4_copy(*matrix, modelview_matrix_stack[--modelview_stack_counter]);
} else if (matrix == &projection_matrix) {
#ifndef SKIP_ERROR_HANDLING
// Error handling
if (projection_stack_counter == 0) {
SET_GL_ERROR(GL_STACK_UNDERFLOW)
} else
#endif
// Copying last matrix on stack into current matrix and decreasing stack counter
matrix4x4_copy(*matrix, projection_matrix_stack[--projection_stack_counter]);
}
mvp_modified = GL_TRUE;
}
void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar) {
#ifndef SKIP_ERROR_HANDLING
// Error handling
if (phase == MODEL_CREATION) {
SET_GL_ERROR(GL_INVALID_OPERATION)
}
#endif
// Initializing frustum matrix with requested parameters
matrix4x4_init_perspective(*matrix, fovy, aspect, zNear, zFar);
mvp_modified = GL_TRUE;
}