triangle.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * 3D Engine
  3. * triangle.c:
  4. * Based on pikuma.com 3D software renderer in C
  5. * Copyright (c) 2021 986-Studio. All rights reserved.
  6. *
  7. * Created by Manoël Trapier on 04/03/2021.
  8. */
  9. #include <display.h>
  10. #include <triangle.h>
  11. void drawTriangle(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t colour)
  12. {
  13. drawLine(x0, y0, x1, y1, colour);
  14. drawLine(x1, y1, x2, y2, colour);
  15. drawLine(x2, y2, x0, y0, colour);
  16. }
  17. static void drawFillBottomFlatTriangle(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t colour)
  18. {
  19. }
  20. static void drawFillTopFlatTriangle(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t colour)
  21. {
  22. }
  23. void drawFilledTriangle(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t colour)
  24. {
  25. int32_t My, Mx;
  26. if (y0 > y1)
  27. {
  28. intSwap(&x0, &x1); intSwap(&y0, &y1);
  29. }
  30. if (y1 > y2)
  31. {
  32. intSwap(&x1, &x2); intSwap(&y1, &y2);
  33. }
  34. if (y0 > y1)
  35. {
  36. intSwap(&x0, &x1); intSwap(&y0, &y1);
  37. }
  38. /* Determine the mid intersection and point */
  39. My = y1;
  40. Mx = x0 + (double)((x2 - x0) * (y1 - y0)) / (double)(y2 - y0);
  41. /* Fill top */
  42. drawFillBottomFlatTriangle(x0, y0, x1, y1, Mx, My,colour);
  43. /* Fill bottom */
  44. drawFillTopFlatTriangle(x1, y1, Mx, My, x2, y2, colour);
  45. }