Solution

Files changed (1) hide show
  1. lab3-camera/lab3_main.cpp +11 -8
lab3-camera/lab3_main.cpp CHANGED
@@ -140,13 +140,13 @@ void display()
140
140
  mat4 cityModelMatrix(1.0f);
141
141
 
142
142
  // Set up the view matrix
143
- // The view matrix defines where the viewer is looking
144
- // Initially fixed, but will be replaced in the tutorial.
145
- mat4 constantViewMatrix = mat4(0.707106769f, -0.408248276f, 1.00000000f, 0.000000000f, //
146
- 0.000000000f, 0.816496551f, 1.00000000f, 0.000000000f, //
147
- -0.707106769f, -0.408248276f, 1.00000000f, 0.000000000f, //
148
- 0.000000000f, 0.000000000f, -30.0000000f, 1.00000000f); //
149
- mat4 viewMatrix = constantViewMatrix;
143
+ // use camera direction as -z axis and compute the x (cameraRight) and y (cameraUp) base vectors
144
+ vec3 cameraRight = normalize(cross(cameraDirection, worldUp));
145
+ vec3 cameraUp = normalize(cross(cameraRight, cameraDirection));
146
+
147
+ mat3 cameraBaseVectorsWorldSpace(cameraRight, cameraUp, -cameraDirection);
148
+ mat4 cameraRotation = mat4(transpose(cameraBaseVectorsWorldSpace));
149
+ mat4 viewMatrix = cameraRotation * translate(-cameraPosition);
150
150
 
151
151
  // Setup the projection matrix
152
152
  if(w != old_w || h != old_h)
@@ -245,7 +245,10 @@ bool handleEvents(void)
245
245
  int delta_y = event.motion.y - g_prevMouseCoords.y;
246
246
  if(event.button.button == SDL_BUTTON_LEFT)
247
247
  {
248
- printf("Mouse motion while left button down (%i, %i)\n", event.motion.x, event.motion.y);
248
+ float rotationSpeed = 0.005f;
249
+ mat4 yaw = rotate(rotationSpeed * -delta_x, worldUp);
250
+ mat4 pitch = rotate(rotationSpeed * -delta_y, normalize(cross(cameraDirection, worldUp)));
251
+ cameraDirection = vec3(pitch * yaw * vec4(cameraDirection, 0.0f));
249
252
  }
250
253
  g_prevMouseCoords.x = event.motion.x;
251
254
  g_prevMouseCoords.y = event.motion.y;