From a278d1843fe808f41423ada3d0a40b925efbf82e Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 10 May 2011 23:38:26 -0400 Subject: [PATCH] store matrices in column-major mode --- glslUtil/glslUtil.c | 54 ++++++++++++++++++++++----------------------- lighting/test.cc | 4 ++-- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/glslUtil/glslUtil.c b/glslUtil/glslUtil.c index af316f8..b29133c 100644 --- a/glslUtil/glslUtil.c +++ b/glslUtil/glslUtil.c @@ -52,9 +52,9 @@ void guMatrixMult(guMatrix4x4 *m, guMatrix4x4 *a, guMatrix4x4 *b) GLfloat v = 0.0; for (p = 0; p < 4; p++) { - v += (*a)[i][p] * (*b)[p][j]; + v += (*a)[p][i] * (*b)[j][p]; } - (*m)[i][j] = v; + (*m)[j][i] = v; } } } @@ -64,7 +64,7 @@ void guMatrixTranslate(guMatrix4x4 *m, GLfloat x, GLfloat y, GLfloat z) int i; for (i = 0; i < 4; i++) { - (*m)[i][3] += (*m)[i][0] * x + (*m)[i][1] * y + (*m)[i][2] * z; + (*m)[3][i] += (*m)[0][i] * x + (*m)[1][i] * y + (*m)[2][i] * z; } } @@ -73,10 +73,9 @@ void guMatrixScale(guMatrix4x4 *m, GLfloat x, GLfloat y, GLfloat z) int i; for (i = 0; i < 4; i++) { - GLfloat *base = &(*m)[i][0]; - base[0] *= x; - base[1] *= y; - base[2] *= z; + (*m)[0][i] *= x; + (*m)[1][i] *= y; + (*m)[2][i] *= z; } } @@ -97,20 +96,20 @@ void guMatrixRotate(guMatrix4x4 *m, GLfloat angle, guMatrix4x4 mult; GLfloat oc = 1 - c; mult[0][0] = lx * lx * oc + c; - mult[0][1] = lx * ly * oc - lz * s; - mult[0][2] = lx * lz * oc + ly * s; - mult[0][3] = 0.0; - mult[1][0] = ly * lx * oc + lz * s; - mult[1][1] = ly * ly * oc + c; - mult[1][2] = ly * lz * oc - lx * s; - mult[1][3] = 0.0; - mult[2][0] = lx * lz * oc - ly * s; - mult[2][1] = ly * lz * oc + lx * s; - mult[2][2] = lz * lz * oc + c; - mult[2][3] = 0.0; + mult[1][0] = lx * ly * oc - lz * s; + mult[2][0] = lx * lz * oc + ly * s; mult[3][0] = 0.0; + mult[0][1] = ly * lx * oc + lz * s; + mult[1][1] = ly * ly * oc + c; + mult[2][1] = ly * lz * oc - lx * s; mult[3][1] = 0.0; + mult[0][2] = lx * lz * oc - ly * s; + mult[1][2] = ly * lz * oc + lx * s; + mult[2][2] = lz * lz * oc + c; mult[3][2] = 0.0; + mult[0][3] = 0.0; + mult[1][3] = 0.0; + mult[2][3] = 0.0; mult[3][3] = 1.0; guMatrixMult(m, m, &mult); } @@ -133,12 +132,12 @@ void guMatrixFrustum(guMatrix4x4 *m, GLfloat tb = top - bottom; GLfloat fn = far - near; mult[0][0] = 2 * near / rl; - mult[0][2] = (right + left) / rl; + mult[2][0] = (right + left) / rl; mult[1][1] = 2 * near / tb; - mult[1][2] = (top + bottom) / tb; + mult[2][1] = (top + bottom) / tb; mult[2][2] = - (far + near) / fn; - mult[2][3] = - 2 * far * near / fn; - mult[3][2] = -1.0; + mult[3][2] = - 2 * far * near / fn; + mult[2][3] = -1.0; guMatrixMult(m, m, &mult); } @@ -159,8 +158,8 @@ void guPerspective(guMatrix4x4 *m, GLfloat fovy, GLfloat aspect, mult[0][0] = f / aspect; mult[1][1] = f; mult[2][2] = (far + near) / nf; - mult[2][3] = (2 * far * near) / nf; - mult[3][2] = -1.0; + mult[3][2] = (2 * far * near) / nf; + mult[2][3] = -1.0; guMatrixMult(m, m, &mult); } @@ -182,12 +181,13 @@ void guOrtho(guMatrix4x4 *m, GLfloat tb = top - bottom; GLfloat fn = far - near; mult[0][0] = 2 / rl; - mult[0][3] = - (right + left) / rl; + mult[3][0] = - (right + left) / rl; mult[1][1] = 2 / tb; - mult[1][3] = - (top + bottom) / tb; + mult[3][1] = - (top + bottom) / tb; mult[2][2] = -2 / fn; - mult[2][3] = - (far + near) / fn; + mult[3][2] = - (far + near) / fn; mult[3][3] = 1.0; + guMatrixMult(m, m, &mult); } char *guGetShaderLog(GLuint id) diff --git a/lighting/test.cc b/lighting/test.cc index b4c7519..5a993fd 100644 --- a/lighting/test.cc +++ b/lighting/test.cc @@ -188,8 +188,8 @@ bool init(int width, int height) glUniform4f(diffuse_loc, 1.0, 0.6, 0.0, 1.0); glUniform4f(specular_loc, 1.0, 1.0, 1.0, 1.0); glUniform1f(shininess_loc, 85.0); - glUniformMatrix4fv(projection_loc, 1, GL_TRUE, &projection[0][0]); - glUniformMatrix4fv(modelview_loc, 1, GL_TRUE, &modelview[0][0]); + glUniformMatrix4fv(projection_loc, 1, GL_FALSE, &projection[0][0]); + glUniformMatrix4fv(modelview_loc, 1, GL_FALSE, &modelview[0][0]); data_vbo = makeBuffer(GL_ARRAY_BUFFER, data, sizeof(data)); index_vbo = makeBuffer(GL_ELEMENT_ARRAY_BUFFER, indices, sizeof(indices));