From 5442b2d4c9b4bfce4665799182149c5c93ccb003 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Mon, 6 Feb 2023 12:50:25 +0100 Subject: [PATCH] Added a draw function for flat colored quads, useful for debugging --- draw.h | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ world.c | 35 +++++++++++++++++++++-------------- 2 files changed, 70 insertions(+), 14 deletions(-) diff --git a/draw.h b/draw.h index 76e323a..82bac0d 100644 --- a/draw.h +++ b/draw.h @@ -80,6 +80,55 @@ static INLINE void draw_trianglestrip_lit(SVECTOR *verts, u_char numVerts, u_lon } } +static INLINE void draw_quadstrip_flat(const ps1bsp_vertex_t *vertices, const ps1bsp_facevertex_t *faceVerts, u_char numVerts, P_COLOR *color, u_long *ot) +{ + const ps1bsp_facevertex_t *v0, *v1, *v2, *v3; + u_char i0, i1, i2, i3; + u_char head = 0; + u_char tail = numVerts; + + // Initialize the first two vertices + i2 = --tail; + i3 = head++; + + // Normally a quad strip would have (N-2)/2 quads, but we might end up with a sole triangle at the end which will be drawn as a collapsed quad + u_char numQuads = (numVerts - 1) >> 1; + for (u_char quadIdx = 0; quadIdx < numQuads; ++quadIdx) + { + i0 = i2; + i1 = i3; + i2 = --tail; + i3 = head++; + + v0 = &faceVerts[i0]; + v1 = &faceVerts[i1]; + v2 = &faceVerts[i2]; + v3 = &faceVerts[i3]; + + // Transform the first three vertices + gte_ldv3(&vertices[v0->index], &vertices[v1->index], &vertices[v2->index]); + gte_rtpt(); // Rotation, translation, perspective projection + + // Draw a flat-shaded untextured colored quad + POLY_G4 *poly = (POLY_G4*)mem_prim(sizeof(POLY_G4)); + gte_stsxy3_g3(poly); + + // Transform the fourth vertex to complete the quad + gte_ldv0(&vertices[v3->index]); + gte_rtps(); + gte_stsxy(&poly->x3); + + setColorFast(&poly->r0, color); + setColorFast(&poly->r1, color); + setColorFast(&poly->r2, color); + setColorFast(&poly->r3, color); + + setPolyG4(poly); + addPrim(ot, poly); + ++polyCount; + } +} + static INLINE void draw_quadstrip_lit(const ps1bsp_vertex_t *vertices, const ps1bsp_polyvertex_t *polyVerts, u_char numVerts, u_long *ot) { const ps1bsp_polyvertex_t *v0, *v1, *v2, *v3; diff --git a/world.c b/world.c index 29b6e3b..4cfcd43 100644 --- a/world.c +++ b/world.c @@ -5,22 +5,22 @@ #include "draw.h" #include "frustum.h" -static CVECTOR colors[] = +static P_COLOR colors[] = { - { 255, 0, 0 }, - { 0, 255, 0 }, - { 0, 0, 255 }, - { 255, 255, 0 }, - { 255, 0, 255 }, - { 0, 255, 255 }, - { 128, 255, 0 }, - { 255, 128, 0 }, - { 128, 0, 255 }, - { 255, 0, 128 }, - { 0, 128, 255 }, - { 0, 255, 128 }, + { (255 << 16), 0 }, + { (255 << 8), 0 }, + { (255 << 0), 0 }, + { (255 << 16) | (255 << 8), 0 }, + { (255 << 16) | (255 << 0), 0 }, + { (255 << 8) | (255 << 0), 0 }, + { (128 << 8) | (255 << 8), 0 }, + { (255 << 16) | (128 << 8), 0 }, + { (128 << 16) | (255 << 0), 0 }, + { (255 << 16) | (128 << 0), 0 }, + { (128 << 8) | (255 << 0), 0 }, + { (128 << 8) | (128 << 0), 0 }, }; -static const int numColors = sizeof(colors) / sizeof(CVECTOR); +static const int numColors = sizeof(colors) / sizeof(P_COLOR); // Set data pointers directly from an in-memory byte buffer #define LOAD_CHUNK(type, dst, num, src, entry) \ @@ -110,6 +110,13 @@ static INLINE char world_cull_backface(const world_t *world, const ps1bsp_face_t return 1; } +static void world_drawface_flat(const world_t *world, const ps1bsp_face_t *face, P_COLOR *color, u_long *ot) +{ + // Draw untextured, vertex colored faces, skipping the entire polygon tessellation step + const ps1bsp_facevertex_t *faceVertices = &world->faceVertices[face->firstFaceVertex]; + draw_quadstrip_flat(world->vertices, faceVertices, face->numFaceVertices, color, ot); +} + static void world_drawface_fast(const world_t *world, const ps1bsp_face_t *face, u_long *ot) { // Draw untextured, vertex colored faces, skipping the entire polygon tessellation step