Browse Source

Lesson 27

Godzil 3 years ago
parent
commit
f14d738b3d
5 changed files with 48 additions and 14 deletions
  1. 15 0
      assets/runway.obj
  2. BIN
      assets/runway.png
  3. 2 0
      source/include/triangle.h
  4. 18 14
      source/main.c
  5. 13 0
      source/triangle.c

+ 15 - 0
assets/runway.obj

@@ -0,0 +1,15 @@
+
+# runway.obj
+
+v -3.000000 0.000000 -20.000000
+v -3.000000 0.000000 20.000000
+v 3.000000 0.000000 20.000000
+v 3.000000 0.000000 -20.000000
+
+vt 0.000000 0.000000
+vt 0.000000 1.000000
+vt 1.000000 1.000000
+vt 1.000000 0.000000
+
+f 1/1/1 2/2/1 3/3/1
+f 1/1/1 3/3/1 4/4/1

BIN
assets/runway.png


+ 2 - 0
source/include/triangle.h

@@ -46,6 +46,8 @@ void drawTriangle(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, in
 void drawFilledTriangle(struct triangle_t *t);
 void drawTextureTriangle(struct triangle_t *t);
 
+vec3_t getTriangleNormal(vec4_t vertices[3]);
+
 int compareTrianglesZOrder(const void *p1, const void *p2);
 
 #endif /* THREEDENGINE_SOURCE_INCLUDE_TRIANGLE_H */

+ 18 - 14
source/main.c

@@ -87,17 +87,25 @@ void setup()
     createLight(&globalLight, vec3(0, 0, 1));
 
     /* Load object */
+    loadNewMesh("assets/runway.obj", "assets/runway.png");
+    worldMesh[0].scale = vec3(1, 1, 1);
+    worldMesh[0].translation = vec3(0, -1.5, 23);
+    worldMesh[0].rotation = vec3(0, 0, 0);
+
     loadNewMesh("assets/f22.obj", "assets/f22.png");
-    worldMesh[0].translation.z = 5;
-    worldMesh[0].translation.x = -5;
-    worldMesh[0].rotation.y = 0.607;
-    worldMesh[0].rotation.x = -0.3;
+    worldMesh[1].scale = vec3(1, 1, 1);
+    worldMesh[1].translation = vec3(0, -1.3, 5);
+    worldMesh[1].rotation = vec3(0, -M_PI/2., 0);
+
+    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);
 
     loadNewMesh("assets/f117.obj", "assets/f117.png");
-    worldMesh[1].translation.z = 5;
-    worldMesh[0].translation.x = 5;
-    worldMesh[1].rotation.y = 0.607;
-    worldMesh[1].rotation.x = -0.3;
+    worldMesh[3].scale = vec3(1, 1, 1);
+    worldMesh[3].translation = vec3(2, -1.3, 9);
+    worldMesh[3].rotation = vec3(0, -M_PI/2.,  0);
 
     trianglesTotal = 0;
     for(i = 0; i < meshCount; i++)
@@ -220,6 +228,7 @@ static void projectTriangle(triangle_t *workTriangle, vec3_t *triangleNormal, te
         double lightLevel = -vec3Dot(*triangleNormal, globalLight.direction);
         currentTriangle.colour = applyLight(colour, lightLevel);
     }
+
     currentTriangle.averageDepth = (workTriangle->points[0].z +
                                     workTriangle->points[1].z +
                                     workTriangle->points[2].z) / 3.0;
@@ -264,12 +273,7 @@ static void processMesh(mesh_t *mesh, matrix4_t *worldMatrix)
             transformedVertices[j] = mat4ProdVec4(transformMatrix, transformedVertices[j]);
         }
 
-        vec3_t vectorBA = vec3SubVectors(vec3FromVec4(transformedVertices[1]), vec3FromVec4(transformedVertices[0]));
-        vec3_t vectorCA = vec3SubVectors(vec3FromVec4(transformedVertices[2]), vec3FromVec4(transformedVertices[0]));
-        vec3Normalize(&vectorBA);
-        vec3Normalize(&vectorCA);
-        triangleNormal = vec3Cross(vectorBA, vectorCA);
-        vec3Normalize(&triangleNormal);
+        triangleNormal = getTriangleNormal(transformedVertices);
 
         if (!doNotCull)
         {

+ 13 - 0
source/triangle.c

@@ -15,6 +15,19 @@
 #include <triangle.h>
 #include <math.h>
 
+vec3_t getTriangleNormal(vec4_t vertices[3])
+{
+    vec3_t ret;
+    vec3_t vectorBA = vec3SubVectors(vec3FromVec4(vertices[1]), vec3FromVec4(vertices[0]));
+    vec3_t vectorCA = vec3SubVectors(vec3FromVec4(vertices[2]), vec3FromVec4(vertices[0]));
+    vec3Normalize(&vectorBA);
+    vec3Normalize(&vectorCA);
+    ret = vec3Cross(vectorBA, vectorCA);
+    vec3Normalize(&ret);
+
+    return ret;
+}
+
 void drawTriangle(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, colour_t colour)
 {
     drawLine(x0, y0, x1, y1, colour);