Browse Source

Lesson 23.5

Godzil 3 years ago
parent
commit
0c7c619434
5 changed files with 96 additions and 14 deletions
  1. 19 0
      source/camera.c
  2. 33 0
      source/include/camera.h
  3. 2 0
      source/include/matrix.h
  4. 20 14
      source/main.c
  5. 22 0
      source/matrix.c

+ 19 - 0
source/camera.c

@@ -0,0 +1,19 @@
+/*
+ * 3D Engine 
+ * camera.c: 
+ * Based on pikuma.com 3D software renderer in C
+ * Copyright (c) 2021 986-Studio. All rights reserved.
+ *
+ * Created by Manoël Trapier on 11/03/2021.
+ */
+
+#include <camera.h>
+#include <vector.h>
+
+
+
+camera_t camera =
+{
+    .position = { 0, 0, 0 },
+    .direction = { 0, 0, 1 },
+};

+ 33 - 0
source/include/camera.h

@@ -0,0 +1,33 @@
+/*
+ * 3D Engine 
+ * camera.h: 
+ * Based on pikuma.com 3D software renderer in C
+ * Copyright (c) 2021 986-Studio. All rights reserved.
+ *
+ * Created by Manoël Trapier on 11/03/2021.
+ */
+
+#ifndef THREEDENGINE_SOURCE_INCLUDE_CAMERA_H
+#define THREEDENGINE_SOURCE_INCLUDE_CAMERA_H
+
+#include <vector.h>
+
+/***********************************************************************************************************************
+ * Data types
+ **********************************************************************************************************************/
+typedef struct camera_t
+{
+    vec3_t position;
+    vec3_t direction;
+} camera_t;
+
+/***********************************************************************************************************************
+ * Global variable
+ **********************************************************************************************************************/
+extern camera_t camera;
+
+/***********************************************************************************************************************
+ * Prototypes
+ **********************************************************************************************************************/
+
+#endif /* THREEDENGINE_SOURCE_INCLUDE_CAMERA_H */

+ 2 - 0
source/include/matrix.h

@@ -53,4 +53,6 @@ matrix4_t mat4RotationZ(double angle);
 
 matrix4_t mat4Perspective(double aspectRation, double FOV, double zNear, double zFar);
 
+matrix4_t mat4LookAt(vec3_t eye, vec3_t target, vec3_t up);
+
 #endif /* THREEDENGINE_SOURCE_INCLUDE_MATRICE_H */

+ 20 - 14
source/main.c

@@ -23,6 +23,7 @@
 #include <vector.h>
 #include <mesh.h>
 #include <light.h>
+#include <camera.h>
 
 bool isRunning = true;
 int previousFrameTime = 0;
@@ -41,18 +42,14 @@ bool doNotCull = false;
 bool doZBuffer = true;
 enum displayMode_t currentDisplayMode = DISPLAY_MODE_SOLID;
 matrix4_t projectionMatrix;
+matrix4_t viewMatrix;
+
 light_t globalLight;
 double updateTime, renderTime, waitTime;
 int trianglesCount, trianglesTotal;
-
-vec3_t cameraPosition =
-{
-    .x = 0,
-    .y = 0,
-    .z = 0,
-};
-
 triangle_t *projectedTriangles = NULL;
+const vec3_t origin = {0, 0, 0 };
+const vec3_t up = {0, 1, 0 };
 
 void setup()
 {
@@ -85,10 +82,9 @@ void setup()
     globalLight.direction.z = 1;
     vec3Normalize(&globalLight.direction);
 
-
     /* Load texture */
     //meshTexture = (colour_t *)REDBRICK_TEXTURE;
-    loadTextureDateFromPng("assets/cube.png");
+    loadTextureDateFromPng("assets/f22.png");
 
 
     /* Load object */
@@ -195,6 +191,7 @@ 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)
     {
@@ -205,9 +202,13 @@ void update()
     waitTime = (waitTime + (getMicroSecTime() - ticks) / 1000.) / 2.0;
     ticks = getMicroSecTime();
 
-    mesh.rotation.x += 0.001;
-    mesh.rotation.y += 0.01;
-    mesh.translation.z = 5;
+    //mesh.rotation.x += 0.02;
+    mesh.translation.z = 4;
+
+    camera.position.x += 0.008;
+    camera.position.y += 0.008;
+
+    viewMatrix = mat4LookAt(camera.position, target, up);
 
     worldMatrix = mat4Identity;
 
@@ -217,6 +218,9 @@ void update()
     worldMatrix = mat4ProdMat4(mat4RotationX(mesh.rotation.x), worldMatrix);
     worldMatrix = mat4ProdMat4(mat4Translate(mesh.translation.x, mesh.translation.y, mesh.translation.z), worldMatrix);
 
+    /* Not sure for now */
+    //worldMatrix = mat4ProdMat4(viewMatrix, worldMatrix);
+
     arrayEmpty(projectedTriangles);
 
     faceCount = arrayGetSize(mesh.faces);
@@ -246,6 +250,8 @@ void update()
             transformedVertices[j] = vec4FromVec3(vertices[j]);
 
             transformedVertices[j] = mat4ProdVec4(worldMatrix, transformedVertices[j]);
+
+            transformedVertices[j] = mat4ProdVec4(viewMatrix, transformedVertices[j]);
         }
 
         vec3_t vectorBA = vec3SubVectors(vec3FromVec4(transformedVertices[1]), vec3FromVec4(transformedVertices[0]));
@@ -257,7 +263,7 @@ void update()
 
         if (!doNotCull)
         {
-            vec3_t cameraRay = vec3SubVectors(cameraPosition, vec3FromVec4(transformedVertices[0]));
+            vec3_t cameraRay = vec3SubVectors(origin, vec3FromVec4(transformedVertices[0]));
             vec3Normalize(&cameraRay);
 
             double cameraDotTriangle = vec3Dot(triangleNormal, cameraRay);

+ 22 - 0
source/matrix.c

@@ -162,3 +162,25 @@ matrix4_t mat4Perspective(double aspectRatio, double FOV, double zNear, double z
     };
     return ret;
 }
+
+matrix4_t mat4LookAt(vec3_t eye, vec3_t target, vec3_t up)
+{
+    vec3_t forward = vec3SubVectors(target, eye);
+    vec3Normalize(&forward);
+    vec3_t right = vec3Cross(up, forward);
+    vec3Normalize(&right);
+    vec3_t trueUp = vec3Cross(forward, right);
+
+    matrix4_t ret =
+    {
+        .v =
+        {
+            right.x,   right.y,   right.z,   -vec3Dot(right, eye),
+            trueUp.x,  trueUp.y,  trueUp.z,  -vec3Dot(trueUp, eye),
+            forward.x, forward.y, forward.z, -vec3Dot(forward, eye),
+            0,         0,          0,        1
+        }
+    };
+
+    return ret;
+}