Browse Source

Lesson 24.15

Godzil 3 years ago
parent
commit
24a5351798
3 changed files with 59 additions and 30 deletions
  1. 34 11
      source/clipping.c
  2. 9 2
      source/include/clipping.h
  3. 16 17
      source/main.c

+ 34 - 11
source/clipping.c

@@ -50,50 +50,71 @@ void createTriangleFromPolygon(polygon_t *polygon, triangle_t *triangleList, uin
         triangleList[i - 1].points[1] = vec4FromVec3(polygon->vertices[i]);
         triangleList[i - 1].points[2] = vec4FromVec3(polygon->vertices[i + 1]);
 
+        triangleList[i - 1].textureCoordinates[0] = polygon->textureCoords[0];
+        triangleList[i - 1].textureCoordinates[1] = polygon->textureCoords[i];
+        triangleList[i - 1].textureCoordinates[2] = polygon->textureCoords[i + 1];
+
         (*outNumber)++;
         assert(*outNumber < MAX_NUM_POLYGON_TRIANGLES);
     }
 }
 
+/* Linear interpolation */
+static inline double floatLerp(double a, double b, double t)
+{
+    return a + t * (b - a);
+}
+
 static void clipPolygonAgainstPlane(polygon_t *poly, faceenum_t plane)
 {
     int i;
     vec3_t planePoint = frustum[plane].point;
     vec3_t planeNormal = frustum[plane].normal;
-    vec3_t *currentVertex;
-    vec3_t *previousVertex;
+    vec3_t *currentVertex, *previousVertex;
+    tex2_t *currentTexCoord, *previousTexCoord;
     double currentDot, previousDot;
 
     vec3_t newVertices[MAX_NUM_POLYGON_VERTICES];
+    tex2_t newTexCoords[MAX_NUM_POLYGON_VERTICES];
     int newVerticesCount = 0;
 
     previousVertex = &poly->vertices[poly->num_vertices - 1];
+    previousTexCoord = &poly->textureCoords[poly->num_vertices - 1];
 
     for(i = 0; i < poly->num_vertices; i++)
     {
         currentVertex = &poly->vertices[i];
+        currentTexCoord = &poly->textureCoords[i];
 
         currentDot = vec3Dot(vec3SubVectors(*currentVertex, planePoint), planeNormal);
         previousDot = vec3Dot(vec3SubVectors(*previousVertex, planePoint), planeNormal);
 
         if ((currentDot * previousDot) < 0.0)
         {
-            double t;
+            double t = previousDot / (previousDot - currentDot);
             /* we crossed the plane */
-            vec3_t intersectionVertice;
-            t = previousDot / (previousDot - currentDot);
-            intersectionVertice = *currentVertex;
-            intersectionVertice = vec3SubVectors(intersectionVertice, *previousVertex);
-            intersectionVertice = vec3ScalarMult(intersectionVertice, t);
-            intersectionVertice = vec3AddVectors(intersectionVertice, *previousVertex);
-
-            newVertices[newVerticesCount] = intersectionVertice;
+            vec3_t intersectionVertex =
+            {
+                .x = floatLerp(previousVertex->x, currentVertex->x, t),
+                .y = floatLerp(previousVertex->y, currentVertex->y, t),
+                .z = floatLerp(previousVertex->z, currentVertex->z, t),
+            };
+            newVertices[newVerticesCount] = intersectionVertex;
+
+            tex2_t intersectionTexCoord =
+            {
+                .u = floatLerp(previousTexCoord->u, currentTexCoord->u, t),
+                .v = floatLerp(previousTexCoord->v, currentTexCoord->v, t),
+            };
+            newTexCoords[newVerticesCount] = intersectionTexCoord;
+
             newVerticesCount++;
         }
 
         if (currentDot > 0.0)
         {
             newVertices[newVerticesCount] = *currentVertex;
+            newTexCoords[newVerticesCount] = *currentTexCoord;
             newVerticesCount++;
         }
 
@@ -101,11 +122,13 @@ static void clipPolygonAgainstPlane(polygon_t *poly, faceenum_t plane)
         assert(newVerticesCount < MAX_NUM_POLYGON_VERTICES);
 
         previousVertex = currentVertex;
+        previousTexCoord = currentTexCoord;
     }
 
     for(i = 0; i < newVerticesCount; i++)
     {
         poly->vertices[i] = newVertices[i];
+        poly->textureCoords[i] = newTexCoords[i];
     }
     poly->num_vertices = newVerticesCount;
 }

+ 9 - 2
source/include/clipping.h

@@ -44,6 +44,7 @@ typedef struct plane_t
 typedef struct polygon_t
 {
     vec3_t vertices[MAX_NUM_POLYGON_VERTICES];
+    tex2_t textureCoords[MAX_NUM_POLYGON_VERTICES];
     int num_vertices;
 } polygon_t;
 
@@ -56,7 +57,7 @@ void initFrustumPlanes(double fovY, double aspectRatio, double zNear, double zFa
 void clipPolygon(polygon_t *poly);
 
 void createTriangleFromPolygon(polygon_t *polygon, triangle_t *triangleList, uint32_t *outNumber);
-static inline polygon_t createPolygonFromTriangle(vec3_t p1, vec3_t p2, vec3_t p3)
+static inline polygon_t createPolygonFromTriangle(vec3_t p1, vec3_t p2, vec3_t p3, tex2_t a_uv, tex2_t b_uv, tex2_t c_uv)
 {
     polygon_t ret =
     {
@@ -66,7 +67,13 @@ static inline polygon_t createPolygonFromTriangle(vec3_t p1, vec3_t p2, vec3_t p
             [1] = p2,
             [2] = p3,
         },
-        .num_vertices = 3
+        .textureCoords =
+        {
+            [0] = a_uv,
+            [1] = b_uv,
+            [2] = c_uv,
+        },
+        .num_vertices = 3,
     };
     return ret;
 }

+ 16 - 17
source/main.c

@@ -94,11 +94,11 @@ void setup()
 
     /* Load texture */
     //meshTexture = (colour_t *)REDBRICK_TEXTURE;
-    loadTextureDateFromPng("assets/cube.png");
+    loadTextureDateFromPng("assets/f117.png");
 
 
     /* Load object */
-    loadOBJFile("assets/cube.obj");
+    loadOBJFile("assets/f117.obj");
     //loadCubeMeshData();
 
     trianglesTotal = arrayGetSize(mesh.faces);
@@ -227,10 +227,10 @@ uint32_t getMicroSecTime()
     return ((curTime.tv_sec) * 1000 * 1000) + (curTime.tv_usec);
 }
 
-static void projectTriangle(triangle_t *baseTriangle, triangle_t *workTriangle, vec3_t *triangleNormal, colour_t colour)
+static void projectTriangle(triangle_t *workTriangle, vec3_t *triangleNormal, colour_t colour)
 {
     int j;
-    struct triangle_t currentTriangle = *baseTriangle;
+    struct triangle_t currentTriangle = *workTriangle;
 
     for (j = 0 ; j < 3 ; j++)
     {
@@ -314,16 +314,6 @@ void update()
     for(i = 0; i < faceCount; i++)
     {
         polygon_t polygon;
-
-        triangle_t baseTriangle =
-        {
-            .textureCoordinates =
-            {
-                [0] = { mesh.faces[i].a_uv.u, mesh.faces[i].a_uv.v },
-                [1] = { mesh.faces[i].b_uv.u, mesh.faces[i].b_uv.v },
-                [2] = { mesh.faces[i].c_uv.u, mesh.faces[i].c_uv.v }
-            }
-        };
         vec4_t transformedVertices[3];
         vec3_t triangleNormal;
 
@@ -367,13 +357,16 @@ void update()
         {
             polygon = createPolygonFromTriangle(vec3FromVec4(transformedVertices[0]),
                                                 vec3FromVec4(transformedVertices[1]),
-                                                vec3FromVec4(transformedVertices[2]));
+                                                vec3FromVec4(transformedVertices[2]),
+                                                mesh.faces[i].a_uv,
+                                                mesh.faces[i].b_uv,
+                                                mesh.faces[i].c_uv);
 
             clipPolygon(&polygon);
             createTriangleFromPolygon(&polygon, trianglesAfterClipping, &numberOfClippedTriangles);
             for(j = 0; j < numberOfClippedTriangles; j++)
             {
-                projectTriangle(&baseTriangle, &trianglesAfterClipping[j], &triangleNormal, mesh.faces[i].colour);
+                projectTriangle(&trianglesAfterClipping[j], &triangleNormal, mesh.faces[i].colour);
             }
         }
         else
@@ -385,10 +378,16 @@ void update()
                     [0] = transformedVertices[0],
                     [1] = transformedVertices[1],
                     [2] = transformedVertices[2]
+                },
+                .textureCoordinates =
+                {
+                    [0] = { mesh.faces[i].a_uv.u, mesh.faces[i].a_uv.v },
+                    [1] = { mesh.faces[i].b_uv.u, mesh.faces[i].b_uv.v },
+                    [2] = { mesh.faces[i].c_uv.u, mesh.faces[i].c_uv.v }
                 }
             };
 
-            projectTriangle(&baseTriangle, &workTriangle, &triangleNormal, mesh.faces[i].colour);
+            projectTriangle(&workTriangle, &triangleNormal, mesh.faces[i].colour);
         };
     }