Browse Source

Lesson 13.7

Godzil 3 years ago
parent
commit
c8e6ca9f7a
2 changed files with 24 additions and 3 deletions
  1. 2 2
      source/display.c
  2. 22 1
      source/triangle.c

+ 2 - 2
source/display.c

@@ -128,7 +128,7 @@ void drawVLine(int32_t x, int32_t y0, int32_t y1, uint32_t colour)
 
     uint32_t *currentPos = &frameBuffer[x + (y0 * windowWidth)];
 
-    for(i = y0; i < y1; i++, currentPos+=windowWidth)
+    for(i = y0; i <= y1; i++, currentPos+=windowWidth)
     {
         *currentPos = colour;
     }
@@ -152,7 +152,7 @@ void drawHLine(int32_t x0, int32_t y, int32_t x1, uint32_t colour)
 
     uint32_t *currentPos = &frameBuffer[x0 + (y * windowWidth)];
 
-    for(i = x0; i < x1; i++, currentPos++)
+    for(i = x0; i <= x1; i++, currentPos++)
     {
         *currentPos = colour;
     }

+ 22 - 1
source/triangle.c

@@ -17,9 +17,30 @@ void drawTriangle(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, in
     drawLine(x2, y2, x0, y0, colour);
 }
 
+/* This function expect Point 0 to be the top, 1 to be the bottom left, 2 to be the bottom right */
 static void drawFillBottomFlatTriangle(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 = x1 - x0;
+    int32_t deltaXR = x2 - x0;
+    int32_t deltaY = y1 - y0;
+    int32_t sideLength = abs(deltaY);
 
+    double incrementXL = deltaXL / (double)sideLength;
+    double incrementXR = deltaXR / (double)sideLength;
+    double incrementY = deltaY / (double)sideLength;
+
+    double currentXL = x0;
+    double currentXR = x0;
+    double currentY = y0;
+
+    for(i = 0; i < sideLength; i++)
+    {
+        drawHLine(round(currentXL), round(currentY), round(currentXR), colour);
+        currentXL += incrementXL;
+        currentXR += incrementXR;
+        currentY += incrementY;
+    }
 }
 
 static void drawFillTopFlatTriangle(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t colour)
@@ -46,7 +67,7 @@ void drawFilledTriangle(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t
     /* Determine the mid intersection and point */
     My = y1;
     Mx = x0 + (double)((x2 - x0) * (y1 - y0)) / (double)(y2 - y0);
-    
+
     /* Fill top */
     drawFillBottomFlatTriangle(x0, y0, x1, y1, Mx, My,colour);