OpenGL rotation, translation, projection matrices

I’ve finally written my next progression program and generated a cube.

If you simply drew this cube, you would only see the front part of the cube and nothing else. To get some 3D effect, I had to rotate the cube, then translate it out into the world and projected it into our screen. I used Jamie King’s 3D Computer Graphics Using OpenGL tutorial to create this program.

I’ve used following glm math functions to generate the full transformation Matrix:

  • glm::perspective: The first parameter describes the field of view angle. It describes the angle the viewer can see. The second parameter is the aspect ratio and is the ratio between the screen width and screen height. The third and forth parameter describe the near and far clipping plane. Those two values need to be positive and near < far. These two values describe the area the camera can see. The resulting matrix is the projection matrix.
  • glm::translate: The first parameter is the projection matrix generated before. The second on requires a vector which contains the coordinates the object has to move to. E.g., if you take following vector glm::vec3(2.0f, 1.0f, -2.0f), you tell the function to move the object 2 points along the positive x axis, 1 point along the positive y axis, and 2 points along the negative z axis. The point to remember here is that we look into the negative z axis. For some reason, this changes as soon as we are in the projection matrix (we look into the positive z axis). The resulting matrix is the projection translation matrix.
  • glm::rotate: The first parameter is the projection translation matrix that has been generated before. The second one requires the degree by which the object needs to be rotated. The rotation uses the right hand rule. The third parameter requires a vector. If you want the rotation to be along the x axis, the vector would be glm::vec3(1.0f, .0f, .0f). Along the y axis, you need the vector to be glm::vec3(0f, 1.0f, .0f) and along the z axis glm::vec3(.0f, .0f, 1.0f). This function is fairly straight forward and easy to use.

Additionally, this program also uses a uniform matrix shader variable which can be written to by using the two OpenGL functions glGetUniformLocation and glUniformMatrix4fv. That’s the first program, that’s actually a bit exciting and already gives a lot of room for playing and experimenting. The code can as always be found in github.

Last but not least, the code generates two programs. One uses MFC (as before) as window creation/input wrapper and the other one uses freeglut. You can compile both by using NMAKE.