1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:37:35 +00:00

LibGL: Implement glMultMatrixf

This commit is contained in:
Jelle Raaijmakers 2021-12-01 13:27:24 +01:00 committed by Andreas Kling
parent 40724a426f
commit 7ac8cd057e
5 changed files with 48 additions and 13 deletions

View file

@ -351,6 +351,7 @@ GLAPI void glMatrixMode(GLenum mode);
GLAPI void glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble nearVal, GLdouble farVal);
GLAPI void glPushMatrix();
GLAPI void glPopMatrix();
GLAPI void glMultMatrixf(GLfloat const* matrix);
GLAPI void glRotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z);
GLAPI void glScalef(GLfloat x, GLfloat y, GLfloat z);
GLAPI void glTranslatef(GLfloat x, GLfloat y, GLfloat z);

View file

@ -35,6 +35,7 @@ public:
virtual void gl_ortho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val) = 0;
virtual void gl_push_matrix() = 0;
virtual void gl_pop_matrix() = 0;
virtual void gl_mult_matrix(FloatMatrix4x4 const& matrix) = 0;
virtual void gl_rotate(GLdouble angle, GLdouble x, GLdouble y, GLdouble z) = 0;
virtual void gl_scale(GLdouble x, GLdouble y, GLdouble z) = 0;
virtual void gl_translate(GLdouble x, GLdouble y, GLdouble z) = 0;

View file

@ -35,18 +35,28 @@ void glPopMatrix()
g_gl_context->gl_pop_matrix();
}
void glLoadMatrixf(const GLfloat* matrix)
/*
* Transposes input matrices (column-major) to our Matrix (row-major).
*/
template<typename T>
static constexpr Matrix4x4<T> transpose_input_matrix(T const* matrix)
{
// Transpose the matrix here because glLoadMatrix expects elements
// in column major order but out Matrix class stores elements in
// row major order.
FloatMatrix4x4 mat(
return {
matrix[0], matrix[4], matrix[8], matrix[12],
matrix[1], matrix[5], matrix[9], matrix[13],
matrix[2], matrix[6], matrix[10], matrix[14],
matrix[3], matrix[7], matrix[11], matrix[15]);
matrix[3], matrix[7], matrix[11], matrix[15],
};
}
g_gl_context->gl_load_matrix(mat);
void glMultMatrixf(GLfloat const* matrix)
{
g_gl_context->gl_mult_matrix(transpose_input_matrix<float>(matrix));
}
void glLoadMatrixf(const GLfloat* matrix)
{
g_gl_context->gl_load_matrix(transpose_input_matrix<float>(matrix));
}
void glLoadIdentity()

View file

@ -33,6 +33,14 @@ static constexpr size_t MATRIX_STACK_LIMIT = 1024;
return; \
}
#define APPEND_TO_CALL_LIST_WITH_ARG_AND_RETURN_IF_NEEDED(name, arg) \
if (should_append_to_listing()) { \
auto ptr = store_in_listing(arg); \
append_to_listing<&SoftwareGLContext::name>(*ptr); \
if (!should_execute_after_appending_to_listing()) \
return; \
}
#define RETURN_WITH_ERROR_IF(condition, error) \
if (condition) { \
if (m_error == GL_NO_ERROR) \
@ -368,12 +376,7 @@ void SoftwareGLContext::gl_load_identity()
void SoftwareGLContext::gl_load_matrix(const FloatMatrix4x4& matrix)
{
if (should_append_to_listing()) {
auto ptr = store_in_listing(matrix);
append_to_listing<&SoftwareGLContext::gl_load_matrix>(*ptr);
if (!should_execute_after_appending_to_listing())
return;
}
APPEND_TO_CALL_LIST_WITH_ARG_AND_RETURN_IF_NEEDED(gl_load_matrix, matrix);
RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
@ -442,6 +445,24 @@ void SoftwareGLContext::gl_pop_matrix()
}
}
void SoftwareGLContext::gl_mult_matrix(FloatMatrix4x4 const& matrix)
{
APPEND_TO_CALL_LIST_WITH_ARG_AND_RETURN_IF_NEEDED(gl_mult_matrix, matrix);
RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
switch (m_current_matrix_mode) {
case GL_PROJECTION:
m_projection_matrix = m_projection_matrix * matrix;
break;
case GL_MODELVIEW:
m_model_view_matrix = m_model_view_matrix * matrix;
break;
default:
dbgln_if(GL_DEBUG, "glMultMatrix(): Attempt to mult matrix with unsupported matrix mode {}", m_current_matrix_mode);
}
}
void SoftwareGLContext::gl_rotate(GLdouble angle, GLdouble x, GLdouble y, GLdouble z)
{
APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_rotate, angle, x, y, z);

View file

@ -46,6 +46,7 @@ public:
virtual void gl_ortho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val) override;
virtual void gl_push_matrix() override;
virtual void gl_pop_matrix() override;
virtual void gl_mult_matrix(FloatMatrix4x4 const& matrix) override;
virtual void gl_rotate(GLdouble angle, GLdouble x, GLdouble y, GLdouble z) override;
virtual void gl_scale(GLdouble x, GLdouble y, GLdouble z) override;
virtual void gl_translate(GLdouble x, GLdouble y, GLdouble z) override;
@ -216,6 +217,7 @@ private:
decltype(&SoftwareGLContext::gl_ortho),
decltype(&SoftwareGLContext::gl_push_matrix),
decltype(&SoftwareGLContext::gl_pop_matrix),
decltype(&SoftwareGLContext::gl_mult_matrix),
decltype(&SoftwareGLContext::gl_rotate),
decltype(&SoftwareGLContext::gl_scale),
decltype(&SoftwareGLContext::gl_translate),