Browse Source

Cache the mesh transform
inside the mesh structure to avoid recalculating it per frame if un-needed

Also add a toggle for the lighting

Godzil 3 years ago
parent
commit
87fe868728
3 changed files with 37 additions and 23 deletions
  1. 12 8
      source/include/mesh.h
  2. 15 15
      source/main.c
  3. 10 0
      source/mesh.c

+ 12 - 8
source/include/mesh.h

@@ -11,6 +11,7 @@
 #define THREEDENGINE_SOURCE_INCLUDE_MESH_H
 
 #include <vector.h>
+#include <matrix.h>
 #include <triangle.h>
 #include <texture.h>
 
@@ -19,16 +20,18 @@
  **********************************************************************************************************************/
 typedef struct mesh_t
 {
-    vec3_t *vertices;        /**< List of vertices */
-    vec3_t *normalVertices;  /**< List of normal vertices */
-    tex2_t *textureVertices; /**< List of texture vertices */
-    face_t *faces;           /**< List of faces */
+    vec3_t *vertices;          /**< List of vertices */
+    vec3_t *normalVertices;    /**< List of normal vertices */
+    tex2_t *textureVertices;   /**< List of texture vertices */
+    face_t *faces;             /**< List of faces */
 
-    vec3_t rotation;         /**< Rotation of the object */
-    vec3_t scale;            /**< Scaling of the object */
-    vec3_t translation;      /**< Translation of the object */
+    vec3_t rotation;           /**< Rotation of the object */
+    vec3_t scale;              /**< Scaling of the object */
+    vec3_t translation;        /**< Translation of the object */
 
-    texture_t texture;       /**< Texture to apply to the object */
+    texture_t texture;         /**< Texture to apply to the object */
+
+    matrix4_t objectTransform; /**< matrix transformation for this mesh */
 } mesh_t;
 
 /***********************************************************************************************************************
@@ -36,6 +39,7 @@ typedef struct mesh_t
  **********************************************************************************************************************/
 void loadCubeMeshData(mesh_t *mesh);
 void loadOBJFile(mesh_t *mesh, const char *filepath);
+void meshUpdateTransform(mesh_t *mesh);
 
 void meshFree(mesh_t *mesh);
 

+ 15 - 15
source/main.c

@@ -45,6 +45,7 @@ bool displayVertex = false;
 bool doNotCull = false;
 bool doZBuffer = true;
 bool doClipping = true;
+bool doLighting = true;
 bool doPerspectiveCorrection = true;
 
 static mesh_t worldMesh[10];
@@ -91,21 +92,25 @@ void setup()
     worldMesh[0].scale = vec3(1, 1, 1);
     worldMesh[0].translation = vec3(0, -1.5, 23);
     worldMesh[0].rotation = vec3(0, 0, 0);
+    meshUpdateTransform(&worldMesh[0]);
 
     loadNewMesh("assets/f22.obj", "assets/f22.png");
     worldMesh[1].scale = vec3(1, 1, 1);
     worldMesh[1].translation = vec3(0, -1.3, 5);
     worldMesh[1].rotation = vec3(0, -M_PI/2., 0);
+    meshUpdateTransform(&worldMesh[1]);
 
     loadNewMesh("assets/efa.obj", "assets/efa.png");
     worldMesh[2].scale = vec3(1, 1, 1);
     worldMesh[2].translation = vec3(-2, -1.3, 9);
     worldMesh[2].rotation = vec3(0, -M_PI/2., 0);
+    meshUpdateTransform(&worldMesh[2]);
 
     loadNewMesh("assets/f117.obj", "assets/f117.png");
     worldMesh[3].scale = vec3(1, 1, 1);
     worldMesh[3].translation = vec3(2, -1.3, 9);
     worldMesh[3].rotation = vec3(0, -M_PI/2.,  0);
+    meshUpdateTransform(&worldMesh[3]);
 
     trianglesTotal = 0;
     for(i = 0; i < meshCount; i++)
@@ -170,6 +175,8 @@ void processInput()
             case SDLK_l: doZBuffer = false; break;
             case SDLK_y: doClipping = true; break;
             case SDLK_h: doClipping = false; break;
+            case SDLK_t: doLighting = true; break;
+            case SDLK_g: doLighting = false; break;
 
             case SDLK_w: cameraMoveForward(&camera, 5 * deltaTime); break;
             case SDLK_d: cameraRotateBy(&camera, 1.0 * deltaTime, 0); break;
@@ -219,14 +226,14 @@ static void projectTriangle(triangle_t *workTriangle, vec3_t *triangleNormal, te
         currentTriangle.points[j].z = projected.z;
     }
 
-    if (doNotCull)
+    if (doLighting)
     {
-        currentTriangle.colour = colour;
+        double lightLevel = -vec3Dot(*triangleNormal, globalLight.direction);
+        currentTriangle.colour = applyLight(colour, lightLevel);
     }
     else
     {
-        double lightLevel = -vec3Dot(*triangleNormal, globalLight.direction);
-        currentTriangle.colour = applyLight(colour, lightLevel);
+        currentTriangle.colour = colour;
     }
 
     currentTriangle.averageDepth = (workTriangle->points[0].z +
@@ -244,15 +251,7 @@ static void processMesh(mesh_t *mesh, matrix4_t *worldMatrix)
     uint32_t numberOfClippedTriangles;
     triangle_t trianglesAfterClipping[MAX_NUM_POLYGON_TRIANGLES];
     faceCount = arrayGetSize(mesh->faces);
-    matrix4_t transformMatrix = mat4Identity;
-
-
-    transformMatrix = mat4ProdMat4(mat4Scale(mesh->scale.x, mesh->scale.y, mesh->scale.z), transformMatrix);
-    transformMatrix = mat4ProdMat4(mat4RotationZ(mesh->rotation.z), transformMatrix);
-    transformMatrix = mat4ProdMat4(mat4RotationY(mesh->rotation.y), transformMatrix);
-    transformMatrix = mat4ProdMat4(mat4RotationX(mesh->rotation.x), transformMatrix);
-    transformMatrix = mat4ProdMat4(mat4Translate(mesh->translation.x, mesh->translation.y, mesh->translation.z), transformMatrix);
-    transformMatrix = mat4ProdMat4(*worldMatrix, transformMatrix);
+    matrix4_t transformMatrix = mat4ProdMat4(*worldMatrix, mesh->objectTransform);
 
     for(i = 0; i < faceCount; i++)
     {
@@ -430,8 +429,9 @@ void render()
     drawText(5, 29, 0x11FF22, "Render time: %02.2f ms", renderTime);
     drawText(5, 41, 0x11FF22, "FPS: %.1f | dT: %.3f", 1000 / (waitTime + updateTime + renderTime), deltaTime);
     drawText(5, 53, 0x11FF22, "Triangles: %d / %d", triangleCount, trianglesTotal);
-    drawText(5, 65, 0x11FF22, "Cull: %c | PeCo: %c | ZB: %c, | CP: %c",
-             doNotCull?'N':'Y', doPerspectiveCorrection ? 'Y' : 'N', doZBuffer ? 'Y' : 'N', doClipping ? 'Y' : 'N');
+    drawText(5, 65, 0x11FF22, "Cull: %c | PeCo: %c | ZB: %c, | CP: %c | L: %c",
+             doNotCull?'N':'Y', doPerspectiveCorrection ? 'Y' : 'N', doZBuffer ? 'Y' : 'N',
+             doClipping ? 'Y' : 'N', doLighting ? 'Y' : 'N');
 
     displayWindowRender();
 }

+ 10 - 0
source/mesh.c

@@ -121,6 +121,16 @@ static void updateFaceUVValues(mesh_t *mesh)
     }
 }
 
+void meshUpdateTransform(mesh_t *mesh)
+{
+    mesh->objectTransform = mat4Identity;
+    mesh->objectTransform = mat4ProdMat4(mat4Scale(mesh->scale.x, mesh->scale.y, mesh->scale.z), mesh->objectTransform);
+    mesh->objectTransform = mat4ProdMat4(mat4RotationZ(mesh->rotation.z), mesh->objectTransform);
+    mesh->objectTransform = mat4ProdMat4(mat4RotationY(mesh->rotation.y), mesh->objectTransform);
+    mesh->objectTransform = mat4ProdMat4(mat4RotationX(mesh->rotation.x), mesh->objectTransform);
+    mesh->objectTransform = mat4ProdMat4(mat4Translate(mesh->translation.x, mesh->translation.y, mesh->translation.z), mesh->objectTransform);
+}
+
 void loadOBJFile(mesh_t *mesh, const char *filepath)
 {
     FILE *fp;