|
@@ -12,9 +12,9 @@
|
|
|
#include <SDL.h>
|
|
|
#include <SDL_ttf.h>
|
|
|
|
|
|
-#include <3dengine.h>
|
|
|
#include <log.h>
|
|
|
#include <display.h>
|
|
|
+#include <colour.h>
|
|
|
|
|
|
/***********************************************************************************************************************
|
|
|
* Variables
|
|
@@ -34,6 +34,14 @@ static TTF_Font *font = NULL;
|
|
|
#define MAX(_a, _b) ((_a) < (_b)) ? (_b) : (_a)
|
|
|
#define MIN(_a, _b) ((_a) > (_b)) ? (_b) : (_a)
|
|
|
|
|
|
+static inline void intSwap(int *a, int *b)
|
|
|
+{
|
|
|
+ int tmp = *a;
|
|
|
+ *a = *b;
|
|
|
+ *b = tmp;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
bool initialiseWindow(int32_t width, int32_t height, bool fullScreen)
|
|
|
{
|
|
|
bool ret = false;
|
|
@@ -270,18 +278,6 @@ void clearFrameBuffer(colour_t colour)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void clearZBuffer()
|
|
|
-{
|
|
|
- int32_t x;
|
|
|
- if (doZBuffer)
|
|
|
- {
|
|
|
- for (x = 0 ; x < windowWidth * windowHeight ; x++)
|
|
|
- {
|
|
|
- zBuffer[x] = 1.0;
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
void drawGrid(int spacing, colour_t colour)
|
|
|
{
|
|
|
int32_t x, y;
|
|
@@ -340,118 +336,3 @@ void drawLine(int32_t x0, int32_t y0, int32_t x1, int32_t y1, colour_t colour)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-static vec3_t barycentricWeights(vec2_t a, vec2_t b, vec2_t c, vec2_t p)
|
|
|
-{
|
|
|
- vec2_t ab = vec2SubVectors(b, a);
|
|
|
- vec2_t bc = vec2SubVectors(c, b);
|
|
|
- vec2_t ac = vec2SubVectors(c, a);
|
|
|
- vec2_t ap = vec2SubVectors(p, a);
|
|
|
- vec2_t bp = vec2SubVectors(p, b);
|
|
|
- vec3_t ret;
|
|
|
-
|
|
|
- double areaTriangleABC = (ab.x * ac.y) - (ab.y * ac.x);
|
|
|
-
|
|
|
- double alpha = ((bc.x * bp.y) - (bp.x * bc.y)) / areaTriangleABC;
|
|
|
- double beta = ((ap.x * ac.y) - (ac.x * ap.y)) / areaTriangleABC;
|
|
|
- double gamma = 1 - alpha - beta;
|
|
|
-
|
|
|
- ret.x = alpha;
|
|
|
- ret.y = beta;
|
|
|
- ret.z = gamma;
|
|
|
-
|
|
|
- return ret;
|
|
|
-}
|
|
|
-
|
|
|
-void drawZPixel(int32_t x, int32_t y, vec4_t a, vec4_t b, vec4_t c, colour_t colour)
|
|
|
-{
|
|
|
- vec2_t pointP = { x, y };
|
|
|
- vec2_t a2 = vec2FromVec4(a);
|
|
|
- vec2_t b2 = vec2FromVec4(b);
|
|
|
- vec2_t c2 = vec2FromVec4(c);
|
|
|
-
|
|
|
- if ((x < 0) || (y < 0) || (x > windowWidth) ||(y > windowHeight))
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- vec3_t weights = barycentricWeights(a2, b2, c2, pointP);
|
|
|
-
|
|
|
- double alpha = weights.x;
|
|
|
- double beta = weights.y;
|
|
|
- double gamma = weights.z;
|
|
|
- double interpolatedReciprocalW;
|
|
|
-
|
|
|
- interpolatedReciprocalW = (1 / a.w) * alpha + (1 / b.w) * beta + (1 / c.w) * gamma;
|
|
|
-
|
|
|
- /* Inverse to get logical W invrementing going further to us */
|
|
|
- interpolatedReciprocalW = 1 - interpolatedReciprocalW;
|
|
|
- if (interpolatedReciprocalW < zBuffer[(windowWidth * y) + x])
|
|
|
- {
|
|
|
- drawPixel(x, y, colour);
|
|
|
-
|
|
|
- /* Update Z Buffer */
|
|
|
- zBuffer[(windowWidth * y) + x] = interpolatedReciprocalW;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void drawTexel(int32_t x, int32_t y, vec4_t a, vec4_t b, vec4_t c, tex2_t ta, tex2_t tb, tex2_t tc, texture_t *texture)
|
|
|
-{
|
|
|
- vec2_t pointP = { x, y };
|
|
|
- vec2_t a2 = vec2FromVec4(a);
|
|
|
- vec2_t b2 = vec2FromVec4(b);
|
|
|
- vec2_t c2 = vec2FromVec4(c);
|
|
|
-
|
|
|
- if ((x < 0) || (y < 0) || (x > windowWidth) ||(y > windowHeight))
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- vec3_t weights = barycentricWeights(a2, b2, c2, pointP);
|
|
|
-
|
|
|
- double alpha = weights.x;
|
|
|
- double beta = weights.y;
|
|
|
- double gamma = weights.z;
|
|
|
- double interpolatedU, interpolatedV, interpolatedReciprocalW;
|
|
|
- int32_t texX, texY;
|
|
|
-
|
|
|
- interpolatedReciprocalW = (1 / a.w) * alpha + (1 / b.w) * beta + (1 / c.w) * gamma;
|
|
|
-
|
|
|
- if (doPerspectiveCorrection)
|
|
|
- {
|
|
|
- interpolatedU = (ta.u / a.w) * alpha + (tb.u / b.w) * beta + (tc.u / c.w) * gamma;
|
|
|
- interpolatedV = ((1 - ta.v) / a.w) * alpha + ((1 - tb.v) / b.w) * beta + ((1 - tc.v) / c.w) * gamma;
|
|
|
-
|
|
|
-
|
|
|
- interpolatedU /= interpolatedReciprocalW;
|
|
|
- interpolatedV /= interpolatedReciprocalW;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- interpolatedU = ta.u * alpha + tb.u * beta + tc.u * gamma;
|
|
|
- interpolatedV = (1 - ta.v) * alpha + (1 - tb.v) * beta + (1 - tc.v) * gamma;
|
|
|
- }
|
|
|
-
|
|
|
- texX = abs((int32_t)(interpolatedU * texture->width));
|
|
|
- texY = abs((int32_t)(interpolatedV * texture->height));
|
|
|
-
|
|
|
- texX = texX % texture->width;
|
|
|
- texY = texY % texture->height;
|
|
|
-
|
|
|
- if (doZBuffer)
|
|
|
- {
|
|
|
- /* Inverse to get logical W invrementing going further to us */
|
|
|
- interpolatedReciprocalW = 1 - interpolatedReciprocalW;
|
|
|
- if (interpolatedReciprocalW < zBuffer[(windowWidth * y) + x])
|
|
|
- {
|
|
|
- drawPixel(x, y, texture->data[(texY * texture->width) + texX]);
|
|
|
-
|
|
|
- /* Update Z Buffer */
|
|
|
- zBuffer[(windowWidth * y) + x] = interpolatedReciprocalW;
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- drawPixel(x, y, texture->data[(texY * texture->width) + texX]);
|
|
|
- }
|
|
|
-}
|