Browse Source

Lesson 13.9

Godzil 3 years ago
parent
commit
b245f57362
2 changed files with 32 additions and 2 deletions
  1. 6 2
      source/main.c
  2. 26 0
      source/triangle.c

+ 6 - 2
source/main.c

@@ -185,15 +185,19 @@ void render()
     SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
     SDL_RenderClear(renderer);
 
-#if 0
+#if 1
     int triangleCount = arrayGetSize(projectedTriangles);
 
     for(i = 0; i < triangleCount; i++)
     {
+        drawFilledTriangle(projectedTriangles[i].points[0].x, projectedTriangles[i].points[0].y,
+                           projectedTriangles[i].points[1].x, projectedTriangles[i].points[1].y,
+                           projectedTriangles[i].points[2].x, projectedTriangles[i].points[2].y,
+                           0xFFFFFFFF);
         drawTriangle(projectedTriangles[i].points[0].x, projectedTriangles[i].points[0].y,
                      projectedTriangles[i].points[1].x, projectedTriangles[i].points[1].y,
                      projectedTriangles[i].points[2].x, projectedTriangles[i].points[2].y,
-                     0xFF00FFFF);
+                     0xFF000000);
     }
 #else
     drawTriangle(300, 100, 50, 400, 500, 700, 0xFF00FF00);

+ 26 - 0
source/triangle.c

@@ -43,9 +43,35 @@ static void drawFillBottomFlatTriangle(int32_t x0, int32_t y0, int32_t x1, int32
     }
 }
 
+/* This function expect Point 2 to be the bottom, 0 to be the top left, 1 to be the top right */
 static void drawFillTopFlatTriangle(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t colour)
 {
+    int i;
+    int32_t deltaXL = x0 - x2;
+    int32_t deltaXR = x1 - x2;
+    int32_t deltaY = y0 - y2;
+    int32_t sideLength = abs(deltaY);
+
+    if (sideLength == 0)
+    {
+        return;
+    }
+
+    double incrementXL = deltaXL / (double)sideLength;
+    double incrementXR = deltaXR / (double)sideLength;
+    double incrementY = deltaY / (double)sideLength;
+
+    double currentXL = x2;
+    double currentXR = x2;
+    double currentY = y2;
 
+    for(i = 0; i <= sideLength; i++)
+    {
+        drawHLine(round(currentXL), round(currentY), round(currentXR), colour);
+        currentXL += incrementXL;
+        currentXR += incrementXR;
+        currentY += incrementY;
+    }
 }
 
 void drawFilledTriangle(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t colour)