In: Computer Science
The main tasks of this C++ programming assignment include implementing a transformation matrix and a view/projection matrix. Giving three 3D points v0(2.0, 0.0, −2.0), v1(0.0, 2.0, −2.0), v2(−2.0, 0.0, −2.0),you are required to transform these points to the camera/view/monitor coordinates system, and draw a lined triangle based on them
get_projection_matrix(float eye_fov, float aspect_ratio, float zNear, float zFar): using the giving parameter, build a projection matrix, and return it.
Here is what I have so far.
Eigen::Matrix4f get_projection_matrix(float eye_fov, float
aspect_ratio,
float zNear, float zFar)
{
// Students will implement this function
Eigen::Matrix4f projection = Eigen::Matrix4f::Identity();
// TODO: Implement this function
// Create the projection matrix for the given
parameters.
// Then return it.
return projection;
}
Transformation matrices
Introduction to matrices
Simply put, a matrix is an array of numbers with a predefined
number of rows and columns. For example, a 2x3 matrix might look
like this:-
3D graphics we will mainly use 4x4 matrices. They will allow us to transform our (x, y, z, w) vertices. This is done by multiplying the vertex with a matrix:
Matrix x Vertex (in this order !!) = TransformedVertex
It's not as scary as it sounds. Place your left finger on a and your right finger on x. This is an ax. Move your left finger to the next number (b) and your right finger to the next number (y). You have passed. Once again: cz. Once again: dw. ax + by + cz + dw. You have a new x! Do the same for each line and you will get a new vector (x, y, z, w).
Now this is pretty boring to compute, and we will be doing this often, so let's ask the computer to do this instead.
In C ++ with GLM:
glm :: mat4 myMatrix;
glm :: vec4 myVector;
// fill in myMatrix and myVector somehow
glm :: vec4 transformedVector = myMatrix * myVector; // Again in
this order! it is important.
In GLSL:
mat4 myMatrix;
vec4 myVector;
// fill in myMatrix and myVector somehow
vec4 transformedVector = myMatrix * myVector; // Yes, This Is
Almost The Same As GLM
(did you cut, didn't paste this into your code? Go ahead, try
it)
The View
Matrix
To quote Futurama again:
The engines do not move the ship at all. The ship remains in place, and the engines move the universe around it.
When you think about it, the same goes for cameras. If you want to see the mountain from a different angle, you can move the camera ... or move the mountain. Although impractical in real life, in computer graphics it is really simple and convenient.
So, initially your camera is located at the beginning of the World Space. To move the world, you simply enter another matrix. Let's say you want to move the camera 3 units to the right (+ X). This is equivalent to moving your entire world (including meshes) 3 units LEFT! (-X). While the brain is melting, let's do this:
// Use #include <glm / gtc / matrix_transform.hpp> and
#include <glm / gtx / transform.hpp>
glm :: mat4 ViewMatrix = glm :: translate (glm :: mat4 (), glm ::
vec3 (-3.0f, 0.0f, 0.0f));
Again, the image below illustrates this: we've gone from World
Space (all vertices are relative to the center of the world, as we
did in the previous section) to Camera Space (all vertices are
relative to the camera).
Here is the required diagram: