The initial state is an Identity Matrix set with the code:
void Matrix::LoadIdentity(Matrix44f m)
{
static Matrix44f identity = { 1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f };
memcpy(m, identity, sizeof(Matrix44f));
}
This is the code that sets the perspective matrix:
myMatrix.SetPerspective(35.0f, float(1024)/float(768), 1.0f, 1000.0f);
void Matrix::SetPerspective(float fFov, float fAspect, float fNear, float fFar)
{
float xmin, xmax, ymin, ymax; // Dimensions of near clipping plane
// Do the Math for the near clipping plane
ymax = fNear * float(tan( fFov * M_PI / 360.0 ));
ymin = -ymax;
xmin = ymin * fAspect;
xmax = -xmin;
// Construct the projection matrix
LoadIdentity(projMatrix);
projMatrix[0] = (2.0f * fNear)/(xmax - xmin);
projMatrix[5] = (2.0f * fNear)/(ymax - ymin);
projMatrix[8] = (xmax + xmin) / (xmax - xmin);
projMatrix[9] = (ymax + ymin) / (ymax - ymin);
projMatrix[10] = -((fFar + fNear)/(fFar - fNear));
projMatrix[11] = -1.0f;
projMatrix[14] = -((2.0f * fFar * fNear)/(fFar - fNear));
projMatrix[15] = 0.0f;
}
The perspective matrix is applied to the final ModelViewMatrix with a matrix Muliplication:
myMatrix.Multiply( *mModelViewProjection,
myMatrix.GetProjectionMatrix(),
myMatrix.mModelView);
void Matrix::Multiply(Matrix44f product, const Matrix44f a, const Matrix44f b )
{
for (int i = 0; i < 4; i++) {
float ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3);
P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0) + ai3 * B(3,0);
P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1) + ai3 * B(3,1);
P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2) + ai3 * B(3,2);
P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3 * B(3,3);
}
}
Here are the results of outputting the different parts of the matrix.
The mModelView matrix contains the translations and rotations. When in orthographic mode:
1.000000 | 0.000000 | 0.000000 | 0.000000
0.000000 | 1.000000 | 0.000000 | 0.000000
0.000000 | 0.000000 | 1.000000 | 0.000000
0.000000 | 0.000000 | 0.000000 | 1.000000
The mModelView matrix when in perspective mode:
2.378696 | 0.000000 | 0.000000 | 0.000000
0.000000 | 3.171595 | 0.000000 | 0.000000
0.000000 | 0.000000 | -1.002002 | 1.002002
0.000000 | 0.000000 | -2.002002 | 2.002002
The mModelViewProjection matrix, this is the matrix that stores the final view matrix regardless which mode we are in.
When in orthographic mode:
1.000000 | 0.000000 | 1.000000 | 0.000000
-1.804688 | -1.809082 | 0.000000 | 0.000000
0.000000 | 0.000000 | -0.200000 | 0.350000
0.000000 | 0.000000 | 0.000000 | 0.000000
When in perspective mode:
2.378696 | 0.000000 | 1.000000 | 0.000000
-1.046876 | -1.051239 | 0.000000 | 0.000000
0.000000 | 0.000000 | -0.200000 | 0.350000
0.000000 | 0.000000 | 0.000000 | 0.000000