Skip to content

Commit

Permalink
fix matrix order once and for all (hopefully)
Browse files Browse the repository at this point in the history
  • Loading branch information
fgsfdsfgs committed Dec 9, 2020
1 parent 59bb764 commit 4138166
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 32 deletions.
38 changes: 15 additions & 23 deletions src/matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ GL_API void glLoadTransposeMatrixd(const GLdouble *m) {
}

GL_API void glMultMatrixf(const GLfloat *m) {
mat4_mul(&pbgl.mtx[pbgl.mtx_current].mtx, (const mat4f *)m, &pbgl.mtx[pbgl.mtx_current].mtx);
mat4_mul(&pbgl.mtx[pbgl.mtx_current].mtx, &pbgl.mtx[pbgl.mtx_current].mtx, (const mat4f *)m);
pbgl.mtx[pbgl.mtx_current].identity = GL_FALSE;
pbgl.state_dirty = pbgl.mtx_any_dirty = pbgl.mtx[pbgl.mtx_current].dirty = GL_TRUE;
}
Expand All @@ -106,7 +106,7 @@ GL_API void glMultMatrixd(const GLdouble *m) {
mat4f tmp;
for (int i = 0; i < 16; ++i)
tmp.v[i] = (float)m[i];
mat4_mul(&pbgl.mtx[pbgl.mtx_current].mtx, &tmp, &pbgl.mtx[pbgl.mtx_current].mtx);
mat4_mul(&pbgl.mtx[pbgl.mtx_current].mtx, &pbgl.mtx[pbgl.mtx_current].mtx, &tmp);
pbgl.mtx[pbgl.mtx_current].identity = GL_FALSE;
pbgl.state_dirty = pbgl.mtx_any_dirty = pbgl.mtx[pbgl.mtx_current].dirty = GL_TRUE;
}
Expand All @@ -116,7 +116,7 @@ GL_API void glMultTransposeMatrixf(const GLfloat *m) {
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
tmp.m[i][j] = m[j * 4 + i];
mat4_mul(&pbgl.mtx[pbgl.mtx_current].mtx, &tmp, &pbgl.mtx[pbgl.mtx_current].mtx);
mat4_mul(&pbgl.mtx[pbgl.mtx_current].mtx, &pbgl.mtx[pbgl.mtx_current].mtx, &tmp);
pbgl.mtx[pbgl.mtx_current].identity = GL_FALSE;
pbgl.state_dirty = pbgl.mtx_any_dirty = pbgl.mtx[pbgl.mtx_current].dirty = GL_TRUE;
}
Expand All @@ -126,7 +126,7 @@ GL_API void glMultTransposeMatrixd(const GLdouble *m) {
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
tmp.m[i][j] = (float)m[j * 4 + i];
mat4_mul(&pbgl.mtx[pbgl.mtx_current].mtx, &tmp, &pbgl.mtx[pbgl.mtx_current].mtx);
mat4_mul(&pbgl.mtx[pbgl.mtx_current].mtx, &pbgl.mtx[pbgl.mtx_current].mtx, &tmp);
pbgl.mtx[pbgl.mtx_current].identity = GL_FALSE;
pbgl.state_dirty = pbgl.mtx_any_dirty = pbgl.mtx[pbgl.mtx_current].dirty = GL_TRUE;
}
Expand All @@ -151,9 +151,9 @@ GL_API void glPopMatrix(void) {

GL_API void glTranslatef(GLfloat x, GLfloat y, GLfloat z) {
mat4f tmp = mat4_identity;
tmp.m[0][3] = x;
tmp.m[1][3] = y;
tmp.m[2][3] = z;
tmp.m[3][0] = x;
tmp.m[3][1] = y;
tmp.m[3][2] = z;
glMultMatrixf(tmp.v);
}

Expand All @@ -180,9 +180,7 @@ GL_API void glRotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z) {
if ((x == 0.f && y == 0.f && z == 0.f) || (angle == 0.f))
return;

// normalize axis vector
const GLfloat len = 1.f / sqrtf(x * x + y * y + z * z);
x *= len; y *= len; z *= len;
// HACK: assume vector is normalized

// angle is passed in in degrees
angle *= M_PI / 180.f;
Expand Down Expand Up @@ -217,14 +215,11 @@ GL_API void glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top
mat4f tmp = {{ 0.f }};

tmp.m[0][0] = 2.0 / (right - left);
tmp.m[0][3] = -(left + right) / (right - left);

tmp.m[1][1] = 2.0 / (top - bottom);
tmp.m[1][3] = -(top + bottom) / (top - bottom);

tmp.m[2][2] = -2.0 / (zfar - znear);
tmp.m[2][3] = -(zfar + znear) / (zfar - znear);

tmp.m[3][0] = - (right + left) / (right - left);
tmp.m[3][1] = - (top + bottom) / (top - bottom);
tmp.m[3][2] = - (zfar + znear) / (zfar - znear);
tmp.m[3][3] = 1.f;

glMultMatrixf(tmp.v);
Expand All @@ -234,15 +229,12 @@ GL_API void glFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble t
mat4f tmp = {{ 0.f }};

tmp.m[0][0] = 2.0 * znear / (right - left);
tmp.m[2][0] = (left + right) / (right - left);

tmp.m[1][1] = 2.0 * znear / (top - bottom);
tmp.m[2][0] = (right + left) / (right - left);
tmp.m[2][1] = (top + bottom) / (top - bottom);

tmp.m[2][2] = -(zfar + znear) / (zfar - znear);
tmp.m[3][2] = -1.f;

tmp.m[2][3] = -2.0 * znear * zfar / (zfar - znear);
tmp.m[2][2] = - (zfar + znear) / (zfar - znear);
tmp.m[2][3] = -1.f;
tmp.m[3][2] = - (2.0 * zfar * znear) / (zfar - znear);

glMultMatrixf(tmp.v);
}
Expand Down
12 changes: 6 additions & 6 deletions src/state.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,21 +285,21 @@ GLboolean pbgl_state_flush(void) {
p = pb_begin();
if (pbgl.mtx[MTX_PROJECTION].dirty || pbgl.mtx[MTX_MODELVIEW].dirty) {
mat4f tmp;
mat4_mul(&tmp, &pbgl.mtx[MTX_PROJECTION].mtx, &pbgl.view.mtx);
mat4_mul(&tmp, &pbgl.view.mtx, &pbgl.mtx[MTX_PROJECTION].mtx);
if (pbgl.mtx[MTX_PROJECTION].dirty)
p = push_command_matrix4x4(p, NV097_SET_PROJECTION_MATRIX, tmp.v);
p = push_command_matrix4x4_transposed(p, NV097_SET_PROJECTION_MATRIX, tmp.v);
if (pbgl.mtx[MTX_MODELVIEW].dirty) {
// inverse modelview matrix only required for lighting, only calculate it when required
// lighting toggle will set the dirty flag if needed
if (pbgl.flags.lighting) {
mat4f invmv;
mat4_invert(&invmv, &pbgl.mtx[MTX_MODELVIEW].mtx);
p = push_command_matrix4x4_transposed(p, NV097_SET_INVERSE_MODEL_VIEW_MATRIX, invmv.v);
p = push_command_matrix4x4(p, NV097_SET_INVERSE_MODEL_VIEW_MATRIX, invmv.v);
}
p = push_command_matrix4x4(p, NV097_SET_MODEL_VIEW_MATRIX, pbgl.mtx[MTX_MODELVIEW].mtx.v);
p = push_command_matrix4x4_transposed(p, NV097_SET_MODEL_VIEW_MATRIX, pbgl.mtx[MTX_MODELVIEW].mtx.v);
}
mat4_mul(&tmp, &pbgl.mtx[MTX_MODELVIEW].mtx, &tmp);
p = push_command_matrix4x4(p, NV097_SET_COMPOSITE_MATRIX, tmp.v);
mat4_mul(&tmp, &tmp, &pbgl.mtx[MTX_MODELVIEW].mtx);
p = push_command_matrix4x4_transposed(p, NV097_SET_COMPOSITE_MATRIX, tmp.v);
pbgl.mtx[MTX_PROJECTION].dirty = pbgl.mtx[MTX_MODELVIEW].dirty = GL_FALSE;
}
pb_end(p);
Expand Down
1 change: 1 addition & 0 deletions src/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ typedef union {
GLuint texgen_r : 1; // 15
GLuint texgen_s : 1; // 16
GLuint texgen_t : 1; // 17
GLuint normalize : 1; // 18
};
GLuint word;
} server_flags_t;
Expand Down
6 changes: 3 additions & 3 deletions src/types.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ mat4f *mat4_invert(mat4f *out, const mat4f *m) {
void mat4_viewport(mat4f *m, float x, float y, float w, float h, float znear, float zfar) {
*m = mat4_zero;
m->m[0][0] = w / 2.0f;
m->m[0][3] = x + w / 2.0f;
m->m[1][1] = h / -2.0f;
m->m[1][3] = y + h / 2.0f;
m->m[2][2] = (zfar - znear) / 2.f;
m->m[2][3] = (zfar + znear) / 2.f;
m->m[3][0] = x + w / 2.0f;
m->m[3][1] = y + h / 2.0f;
m->m[3][2] = (zfar + znear) / 2.f;
m->m[3][3] = 1.0f;
}

0 comments on commit 4138166

Please sign in to comment.