Godzil пре 3 година
родитељ
комит
fff8d1c00c
5 измењених фајлова са 154 додато и 34 уклоњено
  1. 26 0
      source/include/mesh.h
  2. 33 0
      source/include/triangle.h
  3. 41 34
      source/main.c
  4. 44 0
      source/mesh.c
  5. 10 0
      source/triangle.c

+ 26 - 0
source/include/mesh.h

@@ -0,0 +1,26 @@
+/*
+ * 3D Engine 
+ * mesh.h: 
+ * Based on pikuma.com 3D software renderer in C
+ * Copyright (c) 2021 986-Studio. All rights reserved.
+ *
+ * Created by Manoël Trapier on 04/03/2021.
+ */
+
+#ifndef THREEDENGINE_SOURCE_INCLUDE_MESH_H
+#define THREEDENGINE_SOURCE_INCLUDE_MESH_H
+
+#include <vector.h>
+#include <triangle.h>
+
+/***********************************************************************************************************************
+ * Global variables
+ **********************************************************************************************************************/
+
+#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];
+
+#endif /* THREEDENGINE_SOURCE_INCLUDE_MESH_H */

+ 33 - 0
source/include/triangle.h

@@ -0,0 +1,33 @@
+/*
+ * 3D Engine 
+ * triangle.h: 
+ * Based on pikuma.com 3D software renderer in C
+ * Copyright (c) 2021 986-Studio. All rights reserved.
+ *
+ * Created by Manoël Trapier on 04/03/2021.
+ */
+
+#ifndef THREEDENGINE_SOURCE_INCLUDE_TRIANGLE_H
+#define THREEDENGINE_SOURCE_INCLUDE_TRIANGLE_H
+
+#include <vector.h>
+
+/***********************************************************************************************************************
+ * Data types
+ **********************************************************************************************************************/
+
+typedef struct face_t
+{
+    int a, b, c;
+} face_t;
+
+typedef struct triangle_t
+{
+    vec2_t points[3];
+} triangle_t;
+
+/***********************************************************************************************************************
+ * Prototypes
+ **********************************************************************************************************************/
+
+#endif /* THREEDENGINE_SOURCE_INCLUDE_TRIANGLE_H */

+ 41 - 34
source/main.c

@@ -17,13 +17,11 @@
 
 #include <display.h>
 #include <vector.h>
+#include <mesh.h>
 
 bool isRunning = true;
 int previousFrameTime = 0;
 
-#define N_POINTS (9*9*9)
-vec3_t cubePoints[N_POINTS];
-vec2_t projectedPoints[N_POINTS];
 vec3_t cameraPosition =
 {
     .x = 0,
@@ -37,7 +35,9 @@ vec3_t cubeRotation =
     .z = 0,
 };
 
-double fovFactor = 820 ;
+triangle_t projectedTriangles[N_MESH_FACES];
+
+double fovFactor = 640;
 
 void setup()
 {
@@ -56,20 +56,6 @@ void setup()
         goto exit;
     }
 
-    int pointCount = 0;
-
-    for(double x = -1; x <= 1 ; x += 0.25)
-    {
-        for(double y = -1; y <= 1 ; y += 0.25)
-        {
-            for(double z = -1; z <= 1 ; z += 0.25)
-            {
-                vec3_t new_point = { x, y, z };
-                cubePoints[pointCount++] = new_point;
-            }
-        }
-    }
-
 exit:
     return;
 }
@@ -101,8 +87,8 @@ vec2_t orthographicPointProjection(vec3_t point)
 {
     vec2_t ret;
 
-    ret.x = point.x * fovFactor + (windowWidth / 2.);
-    ret.y = point.y * fovFactor + (windowHeight / 2.);
+    ret.x = point.x * fovFactor;
+    ret.y = point.y * fovFactor;
 
     return ret;
 }
@@ -111,15 +97,15 @@ vec2_t perspectivePointProjection(vec3_t point)
 {
     vec2_t ret;
 
-    ret.x = (point.x/point.z) * (fovFactor/2.) + (windowWidth / 2.);
-    ret.y = (point.y/point.z) * (fovFactor/2.) + (windowHeight / 2.);
+    ret.x = (point.x/point.z) * (fovFactor/2.);
+    ret.y = (point.y/point.z) * (fovFactor/2.);
 
     return ret;
 }
 
 void update()
 {
-    int i;
+    int i, j;
 
     uint32_t timeToWait = FRAME_TARGET_TIME - (SDL_GetTicks() - previousFrameTime);
     if (timeToWait <= FRAME_TARGET_TIME)
@@ -128,21 +114,36 @@ void update()
     }
     previousFrameTime = SDL_GetTicks();
 
-    cubeRotation.@x += 0.01;
+    cubeRotation.x += 0.01;
     cubeRotation.y += 0.01;
     cubeRotation.z += 0.01;
 
-    for(i = 0; i < N_POINTS; i++)
+    for(i = 0; i < N_MESH_FACES; i++)
     {
-        vec3_t point = cubePoints[i];
+        vec3_t vertices[3] =
+        {
+            meshVertices[meshFaces[i].a - 1],
+            meshVertices[meshFaces[i].b - 1],
+            meshVertices[meshFaces[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);
 
-        point = vec3RotateX(point, cubeRotation.x);
-        point = vec3RotateY(point, cubeRotation.y);
-        point = vec3RotateZ(point, cubeRotation.z);
+            transformed.z -= cameraPosition.z;
 
-        point.z -= cameraPosition.z;
+            vec2_t projected = perspectivePointProjection(transformed);
 
-        projectedPoints[i] = perspectivePointProjection(point);
+            projected.x += (windowWidth  / 2);
+            projected.y += (windowHeight / 2);
+
+            projectedTriangles[i].points[j] = projected;
+        }
     }
 }
 
@@ -152,12 +153,18 @@ void render()
     SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
     SDL_RenderClear(renderer);
 
-    for(i = 0; i < N_POINTS; i++)
+
+    for(i = 0; i < N_MESH_FACES; i++)
     {
-        vec2_t projPoint = projectedPoints[i];
-        drawRectangle(projPoint.x, projPoint.y, 4, 4, 0xFFFFFF00);
+        drawRectangle(projectedTriangles[i].points[0].x,
+                      projectedTriangles[i].points[0].y,4, 4, 0xFFFFFF00);
+        drawRectangle(projectedTriangles[i].points[1].x,
+                      projectedTriangles[i].points[1].y,4, 4, 0xFFFF00FF);
+        drawRectangle(projectedTriangles[i].points[2].x,
+                      projectedTriangles[i].points[2].y,4, 4, 0xFF00FFFF);
     }
 
+
     renderFrameBuffer();
     clearFrameBuffer(0xFF000000);
 

+ 44 - 0
source/mesh.c

@@ -0,0 +1,44 @@
+/*
+ * 3D Engine 
+ * mesh.c: 
+ * Based on pikuma.com 3D software renderer in C
+ * Copyright (c) 2021 986-Studio. All rights reserved.
+ *
+ * Created by Manoël Trapier on 04/03/2021.
+ */
+
+#include <mesh.h>
+
+vec3_t meshVertices[N_MESH_VERTICES] =
+{
+    [0] = { .x = -1, .y = -1, .z = -1},
+    [1] = { .x = -1, .y =  1, .z = -1},
+    [2] = { .x =  1, .y =  1, .z = -1},
+    [3] = { .x =  1, .y = -1, .z = -1},
+    [4] = { .x =  1, .y =  1, .z =  1},
+    [5] = { .x =  1, .y = -1, .z =  1},
+    [6] = { .x = -1, .y =  1, .z =  1},
+    [7] = { .x = -1, .y = -1, .z =  1},
+};
+
+face_t meshFaces[N_MESH_FACES] =
+{
+    /* Front */
+    { .a = 1, .b = 2, .c = 3 },
+    { .a = 1, .b = 3, .c = 4 },
+    /* Right */
+    { .a = 4, .b = 3, .c = 5 },
+    { .a = 4, .b = 5, .c = 6 },
+    /* Back */
+    { .a = 6, .b = 5, .c = 7 },
+    { .a = 6, .b = 7, .c = 8 },
+    /* Left */
+    { .a = 8, .b = 7, .c = 2 },
+    { .a = 8, .b = 2, .c = 1 },
+    /* Top */
+    { .a = 2, .b = 7, .c = 5 },
+    { .a = 2, .b = 5, .c = 3 },
+    /* Bottom */
+    { .a = 6, .b = 8, .c = 1 },
+    { .a = 6, .b = 1, .c = 4 },
+};

+ 10 - 0
source/triangle.c

@@ -0,0 +1,10 @@
+/*
+ * 3D Engine 
+ * triangle.c: 
+ * Based on pikuma.com 3D software renderer in C
+ * Copyright (c) 2021 986-Studio. All rights reserved.
+ *
+ * Created by Manoël Trapier on 04/03/2021.
+ */
+
+#include <triangle.h>