Browse Source

Lesson 23.8

Godzil 3 years ago
parent
commit
a40ece9250
3 changed files with 42 additions and 16 deletions
  1. 2 0
      source/camera.c
  2. 4 1
      source/include/camera.h
  3. 36 15
      source/main.c

+ 2 - 0
source/camera.c

@@ -16,4 +16,6 @@ camera_t camera =
 {
     .position = { 0, 0, 0 },
     .direction = { 0, 0, 1 },
+    .forwardVelocity = { 0, 0, 0},
+    .yawAngle = 0,
 };

+ 4 - 1
source/include/camera.h

@@ -19,10 +19,13 @@ typedef struct camera_t
 {
     vec3_t position;
     vec3_t direction;
+
+    vec3_t forwardVelocity;
+    double yawAngle;
 } camera_t;
 
 /***********************************************************************************************************************
- * Global variable
+ * Global variables
  **********************************************************************************************************************/
 extern camera_t camera;
 

+ 36 - 15
source/main.c

@@ -50,7 +50,6 @@ double updateTime, renderTime, waitTime;
 int trianglesCount, trianglesTotal;
 triangle_t *projectedTriangles = NULL;
 const vec3_t origin = {0, 0, 0 };
-const vec3_t up = {0, 1, 0 };
 
 void setup()
 {
@@ -145,30 +144,55 @@ void processInput()
             currentDisplayMode = DISPLAY_MODE_TEXTURED_WIRE;
             break;
 
-        case SDLK_c:
+        case SDLK_u:
             doNotCull = false;
             break;
 
-        case SDLK_d:
+        case SDLK_j:
             doNotCull = true;
             break;
 
-        case SDLK_f:
+        case SDLK_i:
             doPerpectiveCorrection = false;
             break;
 
-        case SDLK_v:
+        case SDLK_k:
             doPerpectiveCorrection = true;
             break;
 
-        case SDLK_b:
+        case SDLK_o:
             doZBuffer = true;
             break;
 
-        case SDLK_g:
+        case SDLK_p:
             doZBuffer = false;
             break;
 
+        case SDLK_w:
+            camera.forwardVelocity = vec3ScalarMult(camera.direction, 5 * deltaTime);
+            camera.position = vec3AddVectors(camera.position, camera.forwardVelocity);
+            break;
+
+        case SDLK_d:
+            camera.yawAngle += 1.0 * deltaTime;
+            break;
+
+        case SDLK_s:
+            camera.forwardVelocity = vec3ScalarMult(camera.direction, 5 * deltaTime);
+            camera.position = vec3SubVectors(camera.position, camera.forwardVelocity);
+            break;
+
+        case SDLK_a:
+            camera.yawAngle -= 1.0 * deltaTime;
+            break;
+
+        case SDLK_UP:
+            camera.position.y += 3.0 * deltaTime;
+            break;
+
+        case SDLK_DOWN:
+            camera.position.y -= 3.0 * deltaTime;
+            break;
         }
         break;
 
@@ -192,7 +216,6 @@ void update()
     uint32_t timeToWait = FRAME_TARGET_TIME - (SDL_GetTicks() - previousFrameTime);
     matrix4_t worldMatrix;
     uint32_t ticks = getMicroSecTime();
-    vec3_t target = { 0, 0, 4 };
 
     if (timeToWait <= FRAME_TARGET_TIME)
     {
@@ -204,18 +227,16 @@ void update()
     waitTime = (waitTime + (getMicroSecTime() - ticks) / 1000.) / 2.0;
     ticks = getMicroSecTime();
 
-    mesh.rotation.x += 0.6 * deltaTime;
-    mesh.rotation.y += 0.6 * deltaTime;
-    mesh.rotation.z += 0.6 * deltaTime;
     mesh.translation.z = 5;
 
-    camera.position.x += 0.0 * deltaTime;
-    camera.position.y += 0.0 * deltaTime;
+    vec3_t upDirection = { 0, 1, 0 };
+    vec3_t lookAtTarget = { 0, 0, 1 };
+    camera.direction = vec3FromVec4(mat4ProdVec4(mat4RotationY(camera.yawAngle), vec4FromVec3(lookAtTarget)));
+    lookAtTarget = vec3AddVectors(camera.position, camera.direction);
 
-    viewMatrix = mat4LookAt(camera.position, target, up);
+    viewMatrix = mat4LookAt(camera.position, lookAtTarget, upDirection);
 
     worldMatrix = mat4Identity;
-
     worldMatrix = mat4ProdMat4(mat4Scale(mesh.scale.x, mesh.scale.y, mesh.scale.z), worldMatrix);
     worldMatrix = mat4ProdMat4(mat4RotationZ(mesh.rotation.z), worldMatrix);
     worldMatrix = mat4ProdMat4(mat4RotationY(mesh.rotation.y), worldMatrix);