123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- /*
- * 3D Engine
- * triangle.c:
- * Based on pikuma.com 3D software renderer in C
- * Copyright (c) 2021 986-Studio. All rights reserved.
- *
- * Created by Manoël Trapier on 04/03/2021.
- */
- #include <stdint.h>
- #include <stdbool.h>
- #include <3dengine.h>
- #include <display.h>
- #include <triangle.h>
- #include <math.h>
- void drawTriangle(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, colour_t colour)
- {
- drawLine(x0, y0, x1, y1, colour);
- drawLine(x1, y1, x2, y2, colour);
- drawLine(x2, y2, x0, y0, colour);
- }
- /* ----------------------------------- Filled triangles ----------------------------------- */
- void drawFilledTriangle(struct triangle_t *t)
- {
- int32_t x, y;
- if (t->points[0].y > t->points[1].y)
- {
- doubleSwap(&t->points[0].x, &t->points[1].x); doubleSwap(&t->points[0].y, &t->points[1].y);
- doubleSwap(&t->points[0].z, &t->points[1].z); doubleSwap(&t->points[0].w, &t->points[1].w);
- }
- if (t->points[1].y > t->points[2].y)
- {
- doubleSwap(&t->points[1].x, &t->points[2].x); doubleSwap(&t->points[1].y, &t->points[2].y);
- doubleSwap(&t->points[1].z, &t->points[2].z); doubleSwap(&t->points[1].w, &t->points[2].w);
- }
- if (t->points[0].y > t->points[1].y)
- {
- doubleSwap(&t->points[0].x, &t->points[1].x); doubleSwap(&t->points[0].y, &t->points[1].y);
- doubleSwap(&t->points[0].z, &t->points[1].z); doubleSwap(&t->points[0].w, &t->points[1].w);
- }
- vec4_t a = t->points[0];
- vec4_t b = t->points[1];
- vec4_t c = t->points[2];
- /* Render the top part */
- double inverseSlope1 = 0;
- double inverseSlope2 = 0;
- int32_t x0 = a.x, y0 = a.y;
- int32_t x1 = b.x, y1 = b.y;
- int32_t x2 = c.x, y2 = c.y;
- if ((y1 - y0) != 0) { inverseSlope1 = (double)(x1 - x0) / abs(y1 - y0); }
- if ((y2 - y0) != 0) { inverseSlope2 = (double)(x2 - x0) / abs(y2 - y0); }
- if ((y1 - y0) != 0)
- {
- for (y = y0 ; y <= y1 ; y++)
- {
- int32_t xStart = x1 + (y - y1) * inverseSlope1;
- int32_t xEnd = x0 + (y - y0) * inverseSlope2;
- if (doZBuffer)
- {
- if (xEnd < xStart)
- {
- intSwap(&xStart, &xEnd);
- }
- for (x = xStart ; x <= xEnd ; x++)
- {
- drawZPixel(x, y, a, b, c,t->colour);
- }
- }
- else
- {
- drawHLine(xStart, y, xEnd, t->colour);
- }
- }
- }
- /* Render the bottom part */
- inverseSlope1 = 0;
- if ((y2 - y1) != 0) { inverseSlope1 = (double)(x2 - x1) / abs(y2 - y1); }
- if ((y2 - y1) != 0)
- {
- for (y = y1 ; y <= y2 ; y++)
- {
- int32_t xStart = x2 + (y - y2) * inverseSlope1;
- int32_t xEnd = x0 + (y - y0) * inverseSlope2;
- if (doZBuffer)
- {
- if (xEnd < xStart)
- {
- intSwap(&xStart, &xEnd);
- }
- for (x = xStart ; x <= xEnd ; x++)
- {
- drawZPixel(x, y, a, b, c,t->colour);
- }
- }
- else
- {
- drawHLine(xStart, y, xEnd, t->colour);
- }
- }
- }
- }
- /* ----------------------------------- Textured triangles ----------------------------------- */
- void drawTextureTriangle(struct triangle_t *t)
- {
- int32_t x, y;
- if (t->points[0].y > t->points[1].y)
- {
- doubleSwap(&t->points[0].x, &t->points[1].x); doubleSwap(&t->points[0].y, &t->points[1].y);
- doubleSwap(&t->points[0].z, &t->points[1].z); doubleSwap(&t->points[0].w, &t->points[1].w);
- doubleSwap(&t->textureCoordinates[0].u, &t->textureCoordinates[1].u);
- doubleSwap(&t->textureCoordinates[0].v, &t->textureCoordinates[1].v);
- }
- if (t->points[1].y > t->points[2].y)
- {
- doubleSwap(&t->points[1].x, &t->points[2].x); doubleSwap(&t->points[1].y, &t->points[2].y);
- doubleSwap(&t->points[1].z, &t->points[2].z); doubleSwap(&t->points[1].w, &t->points[2].w);
- doubleSwap(&t->textureCoordinates[1].u, &t->textureCoordinates[2].u);
- doubleSwap(&t->textureCoordinates[1].v, &t->textureCoordinates[2].v);
- }
- if (t->points[0].y > t->points[1].y)
- {
- doubleSwap(&t->points[0].x, &t->points[1].x); doubleSwap(&t->points[0].y, &t->points[1].y);
- doubleSwap(&t->points[0].z, &t->points[1].z); doubleSwap(&t->points[0].w, &t->points[1].w);
- doubleSwap(&t->textureCoordinates[0].u, &t->textureCoordinates[1].u);
- doubleSwap(&t->textureCoordinates[0].v, &t->textureCoordinates[1].v);
- }
- vec4_t a = t->points[0];
- vec4_t b = t->points[1];
- vec4_t c = t->points[2];
- /* Render the top part */
- double inverseSlope1 = 0;
- double inverseSlope2 = 0;
- int32_t x0 = a.x, y0 = a.y;
- int32_t x1 = b.x, y1 = b.y;
- int32_t x2 = c.x, y2 = c.y;
- if ((y1 - y0) != 0) { inverseSlope1 = (double)(x1 - x0) / abs(y1 - y0); }
- if ((y2 - y0) != 0) { inverseSlope2 = (double)(x2 - x0) / abs(y2 - y0); }
- if ((y1 - y0) != 0)
- {
- for (y = y0 ; y <= y1 ; y++)
- {
- int32_t xStart = x1 + (y - y1) * inverseSlope1;
- int32_t xEnd = x0 + (y - y0) * inverseSlope2;
- if (xEnd < xStart)
- {
- intSwap(&xStart, &xEnd);
- }
- for (x = xStart ; x <= xEnd ; x++)
- {
- drawTexel(x, y, a, b, c,
- t->textureCoordinates[0],
- t->textureCoordinates[1],
- t->textureCoordinates[2],
- t->texture);
- }
- }
- }
- /* Render the bottom part */
- inverseSlope1 = 0;
- if ((y2 - y1) != 0) { inverseSlope1 = (double)(x2 - x1) / abs(y2 - y1); }
- if ((y2 - y1) != 0)
- {
- for (y = y1 ; y <= y2 ; y++)
- {
- int32_t xStart = x2 + (y - y2) * inverseSlope1;
- int32_t xEnd = x0 + (y - y0) * inverseSlope2;
- if (xEnd < xStart)
- {
- intSwap(&xStart, &xEnd);
- }
- for (x = xStart ; x <= xEnd ; x++)
- {
- drawTexel(x, y, a, b, c,
- t->textureCoordinates[0],
- t->textureCoordinates[1],
- t->textureCoordinates[2],
- t->texture);
- }
- }
- }
- }
- /* ---- Utility ---- */
- int compareTrianglesZOrder(const void *p1, const void *p2)
- {
- triangle_t *t1 = (struct triangle_t *)p1;
- triangle_t *t2 = (struct triangle_t *)p2;
- if (t1->averageDepth > t2->averageDepth)
- {
- return -1;
- }
- else if (t1->averageDepth < t2->averageDepth)
- {
- return 1;
- }
- return 0;
- }
|