浏览代码

Lesson 10.2

Godzil 3 年之前
父节点
当前提交
95ceef4c24
共有 7 个文件被更改,包括 86 次插入34 次删除
  1. 2 2
      source/array.c
  2. 1 1
      source/display.c
  3. 1 0
      source/include/array.h
  4. 1 1
      source/include/display.h
  5. 18 5
      source/include/mesh.h
  6. 18 22
      source/main.c
  7. 45 3
      source/mesh.c

+ 2 - 2
source/array.c

@@ -1,6 +1,6 @@
 /*
- * 3D Engine 
- * array.c: 
+ * 3D Engine
+ * array.c:
  * Based on pikuma.com 3D software renderer in C
  * Copyright (c) 2021 986-Studio. All rights reserved.
  *

+ 1 - 1
source/display.c

@@ -83,7 +83,7 @@ exit:
     return ret;
 }
 
-void destroy_window()
+void destroyWindow()
 {
     if (frameBuffer != NULL)
     {

+ 1 - 0
source/include/array.h

@@ -10,6 +10,7 @@
 #ifndef THREEDENGINE_SOURCE_INCLUDE_ARRAY_H
 #define THREEDENGINE_SOURCE_INCLUDE_ARRAY_H
 
+#include <stdlib.h>
 #include <stdint.h>
 
 /***********************************************************************************************************************

+ 1 - 1
source/include/display.h

@@ -35,7 +35,7 @@ extern uint32_t windowHeight;
 
 /* --- Window functions --- */
 bool initialiseWindow(bool fullScreen);
-void destroy_window();
+void destroyWindow();
 void renderFrameBuffer();
 
 /* --- Drawing functions --- */

+ 18 - 5
source/include/mesh.h

@@ -14,13 +14,26 @@
 #include <triangle.h>
 
 /***********************************************************************************************************************
- * Global variables
+ * Data types
  **********************************************************************************************************************/
+typedef struct mesh_t
+{
+    vec3_t *vertices;    /**< List of vertices */
+    face_t *faces;       /**< List of faces */
+
+    vec3_t rotation;     /**< Rotation of the object */
+} mesh_t;
 
-#define N_MESH_VERTICES (8)
-extern vec3_t meshVertices[N_MESH_VERTICES];
 
-#define N_MESH_FACES (6 * 2)
-extern face_t meshFaces[N_MESH_FACES];
+/***********************************************************************************************************************
+ * Global variables
+ **********************************************************************************************************************/
+extern mesh_t mesh;
+
+/***********************************************************************************************************************
+ * Prototypes
+ **********************************************************************************************************************/
+void loadCubeMeshData();
+void meshFree();
 
 #endif /* THREEDENGINE_SOURCE_INCLUDE_MESH_H */

+ 18 - 22
source/main.c

@@ -29,12 +29,6 @@ vec3_t cameraPosition =
     .y = 0,
     .z = -5,
 };
-vec3_t cubeRotation =
-{
-    .x = 0,
-    .y = 0,
-    .z = 0,
-};
 
 triangle_t *projectedTriangles = NULL;
 
@@ -57,6 +51,8 @@ void setup()
         goto exit;
     }
 
+    loadCubeMeshData();
+
 exit:
     return;
 }
@@ -106,8 +102,8 @@ vec2_t perspectivePointProjection(vec3_t point)
 
 void update()
 {
-    int i, j;
-
+    uint32_t i, j;
+    uint32_t faceCount;
     uint32_t timeToWait = FRAME_TARGET_TIME - (SDL_GetTicks() - previousFrameTime);
     if (timeToWait <= FRAME_TARGET_TIME)
     {
@@ -115,29 +111,31 @@ void update()
     }
     previousFrameTime = SDL_GetTicks();
 
-    cubeRotation.x += 0.01;
-    cubeRotation.y += 0.01;
-    cubeRotation.z += 0.01;
+    mesh.rotation.x += 0.01;
+    mesh.rotation.y += 0.01;
+    mesh.rotation.z += 0.01;
 
     arrayEmpty(projectedTriangles);
 
-    for(i = 0; i < N_MESH_FACES; i++)
+    faceCount = arrayGetSize(mesh.faces);
+
+    for(i = 0; i < faceCount; i++)
     {
         triangle_t currentTriangle;
         vec3_t vertices[3] =
         {
-            meshVertices[meshFaces[i].a - 1],
-            meshVertices[meshFaces[i].b - 1],
-            meshVertices[meshFaces[i].c - 1],
+            mesh.vertices[mesh.faces[i].a - 1],
+            mesh.vertices[mesh.faces[i].b - 1],
+            mesh.vertices[mesh.faces[i].c - 1],
         };
 
         for(j = 0; j < 3; j++)
         {
             vec3_t transformed = vertices[j];
 
-            transformed = vec3RotateX(transformed, cubeRotation.x);
-            transformed = vec3RotateY(transformed, cubeRotation.y);
-            transformed = vec3RotateZ(transformed, cubeRotation.z);
+            transformed = vec3RotateX(transformed, mesh.rotation.x);
+            transformed = vec3RotateY(transformed, mesh.rotation.y);
+            transformed = vec3RotateZ(transformed, mesh.rotation.z);
 
             transformed.z -= cameraPosition.z;
 
@@ -146,8 +144,6 @@ void update()
             projected.x += (windowWidth  / 2);
             projected.y += (windowHeight / 2);
 
-
-
             currentTriangle.points[j] = projected;
         }
 
@@ -213,8 +209,8 @@ no_more_params:
     }
 
     arrayFree(projectedTriangles);
-
-    destroy_window();
+    meshFree();
+    destroyWindow();
 
     return 0;
 }

+ 45 - 3
source/mesh.c

@@ -6,10 +6,17 @@
  *
  * Created by Manoël Trapier on 04/03/2021.
  */
+#include <stdlib.h>
 
+#include <array.h>
 #include <mesh.h>
 
-vec3_t meshVertices[N_MESH_VERTICES] =
+#define N_CUBE_VERTICES (8)
+extern vec3_t cubeVertices[N_CUBE_VERTICES];
+#define N_CUBE_FACES (6 * 2)
+extern face_t cubeFaces[N_CUBE_FACES];
+
+vec3_t cubeVertices[N_CUBE_VERTICES] =
 {
     [0] = { .x = -1, .y = -1, .z = -1},
     [1] = { .x = -1, .y =  1, .z = -1},
@@ -21,7 +28,7 @@ vec3_t meshVertices[N_MESH_VERTICES] =
     [7] = { .x = -1, .y = -1, .z =  1},
 };
 
-face_t meshFaces[N_MESH_FACES] =
+face_t cubeFaces[N_CUBE_FACES] =
 {
     /* Front */
     { .a = 1, .b = 2, .c = 3 },
@@ -41,4 +48,39 @@ face_t meshFaces[N_MESH_FACES] =
     /* Bottom */
     { .a = 6, .b = 8, .c = 1 },
     { .a = 6, .b = 1, .c = 4 },
-};
+};
+
+mesh_t mesh =
+{
+    .vertices = NULL,
+    .faces = NULL,
+    .rotation = { 0, 0, 0 },
+};
+
+
+void loadCubeMeshData()
+{
+    int i;
+
+    arrayEmpty(mesh.vertices);
+    arrayEmpty(mesh.faces);
+
+    for(i = 0; i < N_CUBE_VERTICES; i++)
+    {
+        arrayAdd(mesh.vertices, cubeVertices[i]);
+    }
+    for(i = 0; i < N_CUBE_FACES; i++)
+    {
+        arrayAdd(mesh.faces, cubeFaces[i]);
+    }
+
+    mesh.rotation.x = 0;
+    mesh.rotation.y = 0;
+    mesh.rotation.z = 0;
+}
+
+void meshFree()
+{
+    arrayFree(mesh.faces);
+    arrayFree(mesh.vertices);
+}