For our first programming exercise you are going to be using program 4.3 from the textbook as your starting point. You will modify this program to add camera motion. By pressing keys on the keyboard the user will be able to move the camera around in the scene and tilt and turn the camera.
Start by reading the GLFW input guide. This guide will show you how to add key event handling to a GLFW application.
In your key event handling code add support for the following keys.
Key | What it does |
---|---|
a | Move camera position left |
d | Move camera position right |
w | Move camera position forward |
s | Move camera position backward |
up arrow | Tilt camera up |
down arrow | Tilt camera down |
left arrow | Turn camera left |
right arrow | Turn camera right |
The original version of program 4.3 assumed that the camera axes would always stay aligned with the world x, y, and z axes. As soon as we add the capability to tilt and turn the camera this will no longer be the case. To facilitate changing the camera orientation you should introduce these new global variables in the program:
glm::vec4 cameraVX,cameraVY,cameraVZ;
and intialize them as follows in the init()
function:
cameraVX = glm::vec4(1.0,0.0,0.0,0.0); cameraVY = glm::vec4(0.0,1.0,0.0,0.0); cameraVZ = glm::vec4(0.0,0.0,1.0,0.0);
Next, replace the line
vMat = glm::translate(glm::mat4(1.0f), glm::vec3(-cameraX, -cameraY, -cameraZ));
in the display()
function with
vMat = glm::inverse(glm::mat4(cameraVX,cameraVY,cameraVZ,glm::vec4(cameraX, cameraY, cameraZ,1.0)));
This will make a view matrix that properly incorporates both the camera location and the camera orientation vectors.
When the user presses the keys to move the camara location or orientation, your code will need to update the values of the cameraX, cameraY, cameraZ, cameraVX, cameraVY, and cameraVZ variables accordingly.
For this assignment you will only need to modify the code in the main.cpp file. When you are done, just send me the updated main.cpp as an attachment to an email message.