Original Post
Hello,
I'm studying CS and taking second year linear algebra, so i though a cool thing to do would be to learn computer graphics. I wrote my own 4x4 row-major matrix class and a 3D vector class. I understand how rotation, scaling and translation work.
Here's my problem:
[source]
// Calculate the orthogonal basis for the camera.
Math::Vector3Df forward = look - pos;
forward.normalise();
Math::Vector3Df right = forward.crossProduct(upVector);
right.normalise();
Math::Vector3Df up = right.crossProduct(forward);
up.normalise();
Math::Matrix4Df viewMatrix;
viewMatrix.m[0] = right.x;
viewMatrix.m[1] = right.y;
viewMatrix.m[2] = right.z;
viewMatrix.m[4] = up.x;
viewMatrix.m[5] = up.y;
viewMatrix.m[6] = up.z;
viewMatrix.m[8] = -forward.x;
viewMatrix.m[9] = -forward.y;
viewMatrix.m[10] = -forward.z;
viewMatrix.m[12] = 0.0f;
viewMatrix.m[13] = 0.0f;
viewMatrix.m[14] = 0.0f;
viewMatrix.m[3] = -right.dotProduct(pos);
viewMatrix.m[7] = -up.dotProduct(pos);
viewMatrix.m[11] = forward.dotProduct(pos);
viewMatrix.m[15] = 1.0f;
viewMatrix = translate(-pos.x, -pos.y, -pos.z) * viewMatrix;
[/source]
I understand what the first bit of the code does. Namely computing an orthogonal basis for the camera. I don't understand the second part.
So my question is:
Why are the entries of the matrix assigned the values they are assigned?
Edit: I was just reading up about orthogonal bases and discovered that an orthogonal basis made up of normalized vectors is called an orthonormal basis. So the first part of the algorithm computes an orthonormal basis for the camera! Cool
I'm studying CS and taking second year linear algebra, so i though a cool thing to do would be to learn computer graphics. I wrote my own 4x4 row-major matrix class and a 3D vector class. I understand how rotation, scaling and translation work.
Here's my problem:
[source]
// Calculate the orthogonal basis for the camera.
Math::Vector3Df forward = look - pos;
forward.normalise();
Math::Vector3Df right = forward.crossProduct(upVector);
right.normalise();
Math::Vector3Df up = right.crossProduct(forward);
up.normalise();
Math::Matrix4Df viewMatrix;
viewMatrix.m[0] = right.x;
viewMatrix.m[1] = right.y;
viewMatrix.m[2] = right.z;
viewMatrix.m[4] = up.x;
viewMatrix.m[5] = up.y;
viewMatrix.m[6] = up.z;
viewMatrix.m[8] = -forward.x;
viewMatrix.m[9] = -forward.y;
viewMatrix.m[10] = -forward.z;
viewMatrix.m[12] = 0.0f;
viewMatrix.m[13] = 0.0f;
viewMatrix.m[14] = 0.0f;
viewMatrix.m[3] = -right.dotProduct(pos);
viewMatrix.m[7] = -up.dotProduct(pos);
viewMatrix.m[11] = forward.dotProduct(pos);
viewMatrix.m[15] = 1.0f;
viewMatrix = translate(-pos.x, -pos.y, -pos.z) * viewMatrix;
[/source]
I understand what the first bit of the code does. Namely computing an orthogonal basis for the camera. I don't understand the second part.
So my question is:
Why are the entries of the matrix assigned the values they are assigned?
Edit: I was just reading up about orthogonal bases and discovered that an orthogonal basis made up of normalized vectors is called an orthonormal basis. So the first part of the algorithm computes an orthonormal basis for the camera! Cool
