Browse Source

Lesson 16.9

Godzil 3 years ago
parent
commit
45539c35e8
3 changed files with 32 additions and 14 deletions
  1. 4 2
      source/include/matrix.h
  2. 9 12
      source/main.c
  3. 19 0
      source/matrix.c

+ 4 - 2
source/include/matrix.h

@@ -38,6 +38,10 @@ static const struct matrix4_t mat4Identity =
  * Prototypes
  **********************************************************************************************************************/
 #define FastGet4(_m, _x, _y) ((_m).v[4 * (_x) + (_y)])
+#define FastSet4(_m, _x, _y, _v) (((_m).v[4 * (_x) + (_y)]) = (_v))
+
+vec4_t mat4ProdVec4(matrix4_t mat, vec4_t v);
+matrix4_t mat4ProdMat4(matrix4_t a, matrix4_t b);
 
 matrix4_t mat4Scale(double scaleX, double scaleY, double scaleZ);
 matrix4_t mat4Translate(double tX, double tY, double tZ);
@@ -46,6 +50,4 @@ 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 - 12
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, rotationXMatrix, rotationYMatrix, rotationZMatrix;
+    matrix4_t worldMatrix;
     if (timeToWait <= FRAME_TARGET_TIME)
     {
         SDL_Delay(timeToWait);
@@ -167,11 +167,13 @@ void update()
     mesh.translation.x += 0.01;
     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);
+    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);
+    worldMatrix = mat4ProdMat4(mat4RotationX(mesh.rotation.x), worldMatrix);
+    worldMatrix = mat4ProdMat4(mat4Translate(mesh.translation.x, mesh.translation.y, mesh.translation.z), worldMatrix);
 
     arrayEmpty(projectedTriangles);
 
@@ -192,12 +194,7 @@ 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]);
+            transformedVertices[j] = mat4ProdVec4(worldMatrix, transformedVertices[j]);
         }
 
         if (!doNotCull)

+ 19 - 0
source/matrix.c

@@ -36,6 +36,25 @@ vec4_t mat4ProdVec4(matrix4_t mat, vec4_t v)
     return ret;
 }
 
+/* Matrix operations */
+matrix4_t mat4ProdMat4(matrix4_t a, matrix4_t b)
+{
+    matrix4_t ret;
+    int x, y, k;
+    for (y = 0 ; y < 4 ; y++)
+    {
+        for (x = 0 ; x < 4 ; x++)
+        {
+            double v = FastGet4(a, x, 0) * FastGet4(b, 0, y) +
+                       FastGet4(a, x, 1) * FastGet4(b, 1, y) +
+                       FastGet4(a, x, 2) * FastGet4(b, 2, y) +
+                       FastGet4(a, x, 3) * FastGet4(b, 3, y);
+            FastSet4(ret, x, y, v);
+        }
+    }
+    return ret;
+}
+
 /* Matrix creations */
 matrix4_t mat4Scale(double scaleX, double scaleY, double scaleZ)
 {