Browse Source

Lesson 13.5

Godzil 3 years ago
parent
commit
e36f2b46fa
5 changed files with 55 additions and 9 deletions
  1. 0 7
      source/display.c
  2. 1 1
      source/include/display.h
  3. 2 0
      source/include/triangle.h
  4. 7 1
      source/main.c
  5. 45 0
      source/triangle.c

+ 0 - 7
source/display.c

@@ -228,10 +228,3 @@ void drawLine(int32_t x0, int32_t y0, int32_t x1, int32_t y1, uint32_t colour)
         }
     }
 }
-
-void drawTriangle(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t colour)
-{
-    drawLine(x0, y0, x1, y1, colour);
-    drawLine(x1, y1, x2, y2, colour);
-    drawLine(x2, y2, x0, y0, colour);
-}

+ 1 - 1
source/include/display.h

@@ -10,6 +10,7 @@
 #ifndef THREEDENGINE_SOURCE_INCLUDE_DISPLAY_H
 #define THREEDENGINE_SOURCE_INCLUDE_DISPLAY_H
 
+#include <stdbool.h>
 #include <SDL.h>
 
 /***********************************************************************************************************************
@@ -47,7 +48,6 @@ void drawRectangle(int32_t x, int32_t y, int32_t w, int32_t h, uint32_t colour);
 void drawVLine(int32_t x, int32_t y0, int32_t y1, uint32_t colour);
 void drawHLine(int32_t x0, int32_t y, int32_t x1, uint32_t colour);
 void drawLine(int32_t x0, int32_t y0, int32_t x1, int32_t y1, uint32_t colour);
-void drawTriangle(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t colour);
 
 static inline void intSwap(int *a, int *b)
 {

+ 2 - 0
source/include/triangle.h

@@ -30,5 +30,7 @@ typedef struct triangle_t
 /***********************************************************************************************************************
  * Prototypes
  **********************************************************************************************************************/
+void drawTriangle(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t colour);
+void drawFilledTriangle(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t colour);
 
 #endif /* THREEDENGINE_SOURCE_INCLUDE_TRIANGLE_H */

+ 7 - 1
source/main.c

@@ -16,6 +16,7 @@
 #include <log.h>
 
 #include <array.h>
+#include <triangle.h>
 #include <display.h>
 #include <vector.h>
 #include <mesh.h>
@@ -183,6 +184,8 @@ void render()
     int i;
     SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
     SDL_RenderClear(renderer);
+
+#if 0
     int triangleCount = arrayGetSize(projectedTriangles);
 
     for(i = 0; i < triangleCount; i++)
@@ -192,7 +195,10 @@ void render()
                      projectedTriangles[i].points[2].x, projectedTriangles[i].points[2].y,
                      0xFF00FFFF);
     }
-
+#else
+    drawTriangle(300, 100, 50, 400, 500, 700, 0xFF00FF00);
+    drawFilledTriangle(300, 100, 50, 400, 500, 700, 0xFFFF0000);
+#endif
     renderFrameBuffer();
     clearFrameBuffer(0xFF000000);
 

+ 45 - 0
source/triangle.c

@@ -7,4 +7,49 @@
  * Created by Manoël Trapier on 04/03/2021.
  */
 
+#include <display.h>
 #include <triangle.h>
+
+void drawTriangle(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t colour)
+{
+    drawLine(x0, y0, x1, y1, colour);
+    drawLine(x1, y1, x2, y2, colour);
+    drawLine(x2, y2, x0, y0, colour);
+}
+
+static void drawFillBottomFlatTriangle(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t colour)
+{
+
+}
+
+static void drawFillTopFlatTriangle(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t colour)
+{
+
+}
+
+void drawFilledTriangle(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t colour)
+{
+    int32_t My, Mx;
+    if (y0 > y1)
+    {
+        intSwap(&x0, &x1); intSwap(&y0, &y1);
+    }
+    if (y1 > y2)
+    {
+        intSwap(&x1, &x2); intSwap(&y1, &y2);
+    }
+    if (y0 > y1)
+    {
+        intSwap(&x0, &x1); intSwap(&y0, &y1);
+    }
+
+    /* 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);
+
+    /* Fill bottom */
+    drawFillTopFlatTriangle(x1, y1, Mx, My, x2, y2, colour);
+}