Browse Source

Lesson 22.1

Godzil 3 years ago
parent
commit
80df437bf3
3 changed files with 11 additions and 10 deletions
  1. 5 5
      source/main.c
  2. 5 4
      source/mesh.c
  3. 1 1
      source/triangle.c

+ 5 - 5
source/main.c

@@ -84,8 +84,8 @@ void setup()
 
 
     /* Load object */
-    //loadOBJFile("assets/cube.obj");
-    loadCubeMeshData();
+    loadOBJFile("assets/f22.obj");
+    //loadCubeMeshData();
 
     trianglesTotal = arrayGetSize(mesh.faces);
 
@@ -219,9 +219,9 @@ void update()
 
         vec3_t vertices[3] =
         {
-            mesh.vertices[mesh.faces[i].a - 1],
-            mesh.vertices[mesh.faces[i].b - 1],
-            mesh.vertices[mesh.faces[i].c - 1],
+            mesh.vertices[mesh.faces[i].a],
+            mesh.vertices[mesh.faces[i].b],
+            mesh.vertices[mesh.faces[i].c],
         };
 
         for(j = 0; j < 3; j++)

+ 5 - 4
source/mesh.c

@@ -102,6 +102,7 @@ void meshFree()
 static int objFileLoaderParser(const char *content);
 static void objFileLoaderLineParser(char *line, uint32_t currentLine);
 static int objFileLoaderExecuteLine(int argc, char *argv[], uint32_t currentLine);
+static void updateFaceUVValues();
 
 static uint32_t objFileLoaderIgnoredLines = 0;
 static uint32_t objFileLoaderLoadedVertices = 0;
@@ -373,8 +374,8 @@ int objFileLoaderExecuteLine(int argc, char *argv[], uint32_t currentLine)
 
             face_t face =
             {
-                .a = v[1], .b = v[2], .c = v[3],
-                .at = vt[1], .bt = vt[2], .ct = vt[3],
+                .a = v[1] - 1, .b = v[2] - 1, .c = v[3] - 1,
+                .at = vt[1] - 1 , .bt = vt[2] - 1, .ct = vt[3] - 1,
                 .colour = MAKE_RGB(45, 170, 10)
             };
             arrayAdd(mesh.faces, face);
@@ -387,8 +388,8 @@ int objFileLoaderExecuteLine(int argc, char *argv[], uint32_t currentLine)
             for(i = 2; i < (argc - 1); i++)
             {
                 face_t face = {
-                    .a = v[1], .b = v[i], .c = v[i + 1],
-                    .at = vt[1], .bt = vt[i], .ct = vt[i + 1],
+                    .a = v[1] - 1, .b = v[i] - 1, .c = v[i + 1] - 1,
+                    .at = vt[1] - 1, .bt = vt[i] - 1, .ct = vt[i + 1] - 1,
                     .colour = MAKE_RGB(45, 170, 10) };
                 arrayAdd(mesh.faces, face);
 

+ 1 - 1
source/triangle.c

@@ -157,7 +157,7 @@ static void drawTexel(int32_t x, int32_t y, vec4_t a, vec4_t b, vec4_t c, tex2_t
         double interpolatedReciprocalW;
 
         interpolatedU = (ta.u / a.w) * alpha + (tb.u / b.w) * beta + (tc.u / c.w) * gamma;
-        interpolatedV = (ta.v / a.w) * alpha + (tb.v / b.w) * beta + (tc.v / c.w) * gamma;
+        interpolatedV = ((1 - ta.v) / a.w) * alpha + ((1 - tb.v) / b.w) * beta + ((1 - tc.v) / c.w) * gamma;
         interpolatedReciprocalW = (1 / a.w) * alpha + (1 / b.w) * beta + (1 / c.w) * gamma;
 
         interpolatedU /= interpolatedReciprocalW;