diff --git a/draw.h b/draw.h index a0317b8..e48f880 100644 --- a/draw.h +++ b/draw.h @@ -223,4 +223,65 @@ static INLINE void draw_quad_strip_tex(STVECTOR *verts, u_char numVerts, u_short } } +static INLINE void draw_quad_strip_water(STVECTOR *verts, u_char numVerts, u_short tpage) +{ + int p; + + // 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) / 2; + if (!mem_checkprim(sizeof(POLY_FT4), numQuads)) + return; + + // Draw the face as a quad strip + const STVECTOR *v0, *v1, *v2, *v3; + const STVECTOR *head = verts; + const STVECTOR *tail = verts + numVerts; + + // Initialize the first two vertices + v2 = --tail; + v3 = head++; + + for (u_char quadIdx = 0; quadIdx < numQuads; ++quadIdx) + { + v0 = v2; + v1 = v3; + v2 = --tail; + v3 = head++; + + // Naively draw the quad with GTE, nothing special or optimized about this + gte_ldv3(v0, v1, v2); + gte_rtpt(); // Rotation, translation, perspective projection + + // Average Z for depth sorting and culling + gte_avsz3(); + gte_stotz(&p); + short depth = p >> 2; + if (depth <= 0 || depth >= OTLEN) + continue; + + // Draw a flat-shaded untextured colored quad + POLY_FT4 *poly = (POLY_FT4*)mem_prim(sizeof(POLY_FT4)); + setPolyFT4(poly); + gte_stsxy0(&poly->x0); + gte_stsxy1(&poly->x1); + gte_stsxy2(&poly->x2); + + // Transform the fourth vertex to complete the quad + gte_ldv0(v3); + gte_rtps(); + gte_stsxy(&poly->x3); + + // Texture UVs + setUV4(poly, v0->u, v0->v, v1->u, v1->v, v2->u, v2->v, v3->u, v3->v); + poly->clut = quake_clut; + poly->tpage = tpage; + + // Unlit + poly->r0 = poly->g0 = poly->b0 = 255; + + addPrim(curOT + depth, poly); + ++polyCount; + } +} + #endif // __DRAW_H__ diff --git a/world.c b/world.c index 29b6c45..2770cdf 100644 --- a/world.c +++ b/world.c @@ -128,7 +128,10 @@ static void world_drawface(const world_t *world, const ps1bsp_face_t *face) curVert->pad = polyVertex->light; } - draw_quad_strip_tex(verts, poly->numPolyVertices, faceTexture->tpage); + if (face->flags & SURF_DRAWWATER) + draw_quad_strip_water(verts, poly->numPolyVertices, faceTexture->tpage); + else + draw_quad_strip_tex(verts, poly->numPolyVertices, faceTexture->tpage); } } }