summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJelle Raaijmakers <jelle@gmta.nl>2021-12-01 13:27:24 +0100
committerAndreas Kling <kling@serenityos.org>2021-12-12 21:51:08 +0100
commit7ac8cd057e8acd25c2e6405371f1af891d19c809 (patch)
treeb5d29af7ad4ab37aa2558c96147dc54e5918d7f8
parent40724a426f76a3f560b12d55e8f4e4bfcd53aa02 (diff)
downloadserenity-7ac8cd057e8acd25c2e6405371f1af891d19c809.zip
LibGL: Implement `glMultMatrixf`
-rw-r--r--Userland/Libraries/LibGL/GL/gl.h1
-rw-r--r--Userland/Libraries/LibGL/GLContext.h1
-rw-r--r--Userland/Libraries/LibGL/GLMat.cpp24
-rw-r--r--Userland/Libraries/LibGL/SoftwareGLContext.cpp33
-rw-r--r--Userland/Libraries/LibGL/SoftwareGLContext.h2
5 files changed, 48 insertions, 13 deletions
diff --git a/Userland/Libraries/LibGL/GL/gl.h b/Userland/Libraries/LibGL/GL/gl.h
index 0a2cf84ad6..36646ae92a 100644
--- a/Userland/Libraries/LibGL/GL/gl.h
+++ b/Userland/Libraries/LibGL/GL/gl.h
@@ -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);
diff --git a/Userland/Libraries/LibGL/GLContext.h b/Userland/Libraries/LibGL/GLContext.h
index ddd7d9e296..a2ffa0989f 100644
--- a/Userland/Libraries/LibGL/GLContext.h
+++ b/Userland/Libraries/LibGL/GLContext.h
@@ -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;
diff --git a/Userland/Libraries/LibGL/GLMat.cpp b/Userland/Libraries/LibGL/GLMat.cpp
index 8c3fa0c548..c459f71e07 100644
--- a/Userland/Libraries/LibGL/GLMat.cpp
+++ b/Userland/Libraries/LibGL/GLMat.cpp
@@ -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()
diff --git a/Userland/Libraries/LibGL/SoftwareGLContext.cpp b/Userland/Libraries/LibGL/SoftwareGLContext.cpp
index 495e29401c..2af62ab289 100644
--- a/Userland/Libraries/LibGL/SoftwareGLContext.cpp
+++ b/Userland/Libraries/LibGL/SoftwareGLContext.cpp
@@ -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);
diff --git a/Userland/Libraries/LibGL/SoftwareGLContext.h b/Userland/Libraries/LibGL/SoftwareGLContext.h
index c95a125a1b..611e0298fd 100644
--- a/Userland/Libraries/LibGL/SoftwareGLContext.h
+++ b/Userland/Libraries/LibGL/SoftwareGLContext.h
@@ -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),