diff --git a/glslUtil/glslUtil.c b/glslUtil/glslUtil.c index 6c3757a..9341264 100644 --- a/glslUtil/glslUtil.c +++ b/glslUtil/glslUtil.c @@ -108,12 +108,15 @@ void guMatrixFrustum(guMatrix4x4 *m, mult[i][j] = 0.0; } } - mult[0][0] = 2 * near / (right - left); - mult[0][2] = (right + left) / (right - left); - mult[1][1] = 2 * near / (top - bottom); - mult[1][2] = (top + bottom) / (top - bottom); - mult[2][2] = - (far + near) / (far - near); - mult[2][3] = - 2 * far * near / (far - near); + GLfloat rl = right - left; + GLfloat tb = top - bottom; + GLfloat fn = far - near; + mult[0][0] = 2 * near / rl; + mult[0][2] = (right + left) / rl; + mult[1][1] = 2 * near / tb; + mult[1][2] = (top + bottom) / tb; + mult[2][2] = - (far + near) / fn; + mult[2][3] = - 2 * far * near / fn; mult[3][2] = -1.0; guMatrixMult(m, m, &mult); } @@ -131,10 +134,11 @@ void guPerspective(guMatrix4x4 *m, GLfloat fovy, GLfloat aspect, } } GLfloat f = 1.0 / tan(fovy / 2.0); + GLfloat nf = near - far; mult[0][0] = f / aspect; mult[1][1] = f; - mult[2][2] = (far + near) / (near - far); - mult[2][3] = (2 * far * near) / (near - far); + mult[2][2] = (far + near) / nf; + mult[2][3] = (2 * far * near) / nf; mult[3][2] = -1.0; guMatrixMult(m, m, &mult); }