Browse Source

Lesson 16.8

Godzil 3 years ago
parent
commit
da09577bc9
4 changed files with 51 additions and 4 deletions
  1. 4 0
      source/include/matrix.h
  2. 9 1
      source/main.c
  3. 36 1
      source/matrix.c
  4. 2 2
      source/vector.c

+ 4 - 0
source/include/matrix.h

@@ -39,6 +39,10 @@ static const struct matrix4_t mat4Identity =
 matrix4_t mat4Scale(double scaleX, double scaleY, double scaleZ);
 matrix4_t mat4Translate(double tX, double tY, double tZ);
 
+matrix4_t mat4RotationX(double angle);
+matrix4_t mat4RotationY(double angle);
+matrix4_t mat4RotationZ(double angle);
+
 vec4_t mat4ProdVec4(matrix4_t mat, vec4_t v);
 
 #endif /* THREEDENGINE_SOURCE_INCLUDE_MATRICE_H */

+ 9 - 1
source/main.c

@@ -150,7 +150,7 @@ void update()
     uint32_t i, j;
     uint32_t faceCount;
     uint32_t timeToWait = FRAME_TARGET_TIME - (SDL_GetTicks() - previousFrameTime);
-    matrix4_t scaleMatrix, translateMatrix;
+    matrix4_t scaleMatrix, translateMatrix, rotationXMatrix, rotationYMatrix, rotationZMatrix;
     if (timeToWait <= FRAME_TARGET_TIME)
     {
         SDL_Delay(timeToWait);
@@ -168,7 +168,11 @@ void update()
     mesh.translation.z = 5;
 
     scaleMatrix = mat4Scale(mesh.scale.x, mesh.scale.y, mesh.scale.z);
+    rotationXMatrix = mat4RotationX(mesh.rotation.x);
+    rotationYMatrix = mat4RotationY(mesh.rotation.y);
+    rotationZMatrix = mat4RotationZ(mesh.rotation.z);
     translateMatrix = mat4Translate(mesh.translation.x, mesh.translation.y, mesh.translation.z);
+
     arrayEmpty(projectedTriangles);
 
     faceCount = arrayGetSize(mesh.faces);
@@ -188,6 +192,10 @@ void update()
         {
             transformedVertices[j] = vec4FromVec3(vertices[j]);
 
+            transformedVertices[j] = mat4ProdVec4(rotationXMatrix, transformedVertices[j]);
+            transformedVertices[j] = mat4ProdVec4(rotationYMatrix, transformedVertices[j]);
+            transformedVertices[j] = mat4ProdVec4(rotationZMatrix, transformedVertices[j]);
+
             transformedVertices[j] = mat4ProdVec4(scaleMatrix, transformedVertices[j]);
             transformedVertices[j] = mat4ProdVec4(translateMatrix, transformedVertices[j]);
         }

+ 36 - 1
source/matrix.c

@@ -1,4 +1,4 @@
- /*
+/*
  * 3D Engine 
  * matrice.c: 
  * Based on pikuma.com 3D software renderer in C
@@ -7,6 +7,8 @@
  * Created by Manoël Trapier on 06/03/2021.
  */
 
+#include <math.h>
+
 #include <matrix.h>
 
 /* Matrix operations */
@@ -56,3 +58,36 @@ matrix4_t mat4Translate(double tX, double tY, double tZ)
     };
     return ret;
 }
+
+matrix4_t mat4RotationX(double angle)
+{
+    matrix4_t ret = {
+        1, 0, 0, 0,
+        0, cos(angle), -sin(angle), 0,
+        0, sin(angle), cos(angle), 0,
+        0, 0, 0, 1
+    };
+    return ret;
+}
+
+matrix4_t mat4RotationY(double angle)
+{
+    matrix4_t ret = {
+        cos(angle), 0, sin(angle), 0,
+        0, 1, 0, 0,
+        -sin(angle), 0, cos(angle), 0,
+        0, 0, 0, 1
+    };
+    return ret;
+}
+
+matrix4_t mat4RotationZ(double angle)
+{
+    matrix4_t ret = {
+        cos(angle), -sin(angle), 0, 0,
+        sin(angle), cos(angle), 0, 0,
+        0, 0, 1, 0,
+        0, 0, 0, 1
+    };
+    return ret;
+}

+ 2 - 2
source/vector.c

@@ -122,9 +122,9 @@ vec3_t vec3RotateX(vec3_t original, double angle)
 vec3_t vec3RotateY(vec3_t original, double angle)
 {
     vec3_t ret = {
-        .x = original.x * cos(angle) - original.z * sin(angle),
+        .x = original.x * cos(angle) + original.z * sin(angle),
         .y = original.y,
-        .z = original.x * sin(angle) + original.z * cos(angle)
+        .z = -original.x * sin(angle) + original.z * cos(angle)
     };
     return ret;
 }