|
|
|
@ -38,31 +38,118 @@ void world_load(const u_long *data, world_t *world) |
|
|
|
bytes += sizeof(ps1bsp_face_t) * world->header->numFaces; |
|
|
|
} |
|
|
|
|
|
|
|
void world_draw(const world_t *world) |
|
|
|
static inline void drawface_triangle_fan(const ps1bsp_face_t *face, SVECTOR *vecs) |
|
|
|
{ |
|
|
|
int p; |
|
|
|
|
|
|
|
// The world doesn't move, so we just set the camera view-projection matrix |
|
|
|
gte_SetRotMatrix(&vp_matrix); |
|
|
|
gte_SetTransMatrix(&vp_matrix); |
|
|
|
// Draw the face as a triangle fan |
|
|
|
u_char maxVert = face->numFaceVertices - 1; |
|
|
|
for (int vertIdx = 1; vertIdx < maxVert; ++vertIdx) |
|
|
|
{ |
|
|
|
const SVECTOR *v0 = &vecs[0]; |
|
|
|
const SVECTOR *v1 = &vecs[vertIdx]; |
|
|
|
const SVECTOR *v2 = &vecs[vertIdx + 1]; |
|
|
|
|
|
|
|
for (int faceIdx = 0; faceIdx < world->header->numFaces; ++faceIdx) |
|
|
|
// Naively draw the triangle with GTE, nothing special or optimized about this |
|
|
|
gte_ldv3(v0, v1, v2); |
|
|
|
gte_rtpt(); // Rotation, translation, perspective projection |
|
|
|
|
|
|
|
// Normal clipping for backface culling |
|
|
|
gte_nclip(); |
|
|
|
gte_stopz(&p); |
|
|
|
if (p < 0) |
|
|
|
continue; |
|
|
|
|
|
|
|
// Average Z for depth sorting and culling |
|
|
|
gte_avsz3(); |
|
|
|
gte_stotz(&p); |
|
|
|
unsigned short depth = p >> 2; |
|
|
|
if (depth <= 0 || depth >= OTLEN) |
|
|
|
continue; |
|
|
|
|
|
|
|
// Draw a flat-shaded untextured colored triangle |
|
|
|
POLY_G3 *poly = (POLY_G3*)mem_prim(sizeof(POLY_G3)); |
|
|
|
if (poly == NULL) |
|
|
|
break; |
|
|
|
|
|
|
|
setPolyG3(poly); |
|
|
|
gte_stsxy3_g3(poly); |
|
|
|
|
|
|
|
poly->r0 = poly->g0 = poly->b0 = (uint8_t)v0->pad; |
|
|
|
poly->r1 = poly->g1 = poly->b1 = (uint8_t)v1->pad; |
|
|
|
poly->r2 = poly->g2 = poly->b2 = (uint8_t)v2->pad; |
|
|
|
|
|
|
|
addPrim(curOT + depth, poly); |
|
|
|
++polyCount; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
static inline void drawface_triangle_strip(const ps1bsp_face_t *face, SVECTOR *vecs) |
|
|
|
{ |
|
|
|
const ps1bsp_face_t *face = &world->faces[faceIdx]; |
|
|
|
const CVECTOR *col = &colors[faceIdx % numColors]; |
|
|
|
int p; |
|
|
|
|
|
|
|
char *scratch = scratchpad; |
|
|
|
SVECTOR *vecs = (SVECTOR*)mem_scratch(&scratch, sizeof(SVECTOR) * face->numFaceVertices); |
|
|
|
// Draw the face as a triangle strip |
|
|
|
const SVECTOR *v0, *v1, *v2; |
|
|
|
const SVECTOR *head = vecs; |
|
|
|
const SVECTOR *tail = vecs + face->numFaceVertices; |
|
|
|
u_char reverse = 0; |
|
|
|
|
|
|
|
// Copy this face's vertices into scratch RAM for fast reuse |
|
|
|
ps1bsp_facevertex_t *faceVertex = &world->faceVertices[face->firstFaceVertex]; |
|
|
|
for (int vertIdx = 0; vertIdx < face->numFaceVertices; ++vertIdx, ++faceVertex) |
|
|
|
v2 = head++; // Initialize first vertex to index 0 and set head to index 1 |
|
|
|
|
|
|
|
u_char numTris = face->numFaceVertices - 2; |
|
|
|
for (u_char triIdx = 0; triIdx < numTris; ++triIdx) |
|
|
|
{ |
|
|
|
const ps1bsp_vertex_t *vert = &world->vertices[faceVertex->index]; |
|
|
|
vecs[vertIdx] = *((SVECTOR*)vert); |
|
|
|
vecs[vertIdx].pad = vert->baseLight; |
|
|
|
if (reverse ^= 1) |
|
|
|
{ |
|
|
|
v0 = v2; |
|
|
|
v1 = head; |
|
|
|
v2 = --tail; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
v0 = v1; |
|
|
|
v1 = ++head; |
|
|
|
v2 = tail; |
|
|
|
} |
|
|
|
|
|
|
|
// Naively draw the triangle with GTE, nothing special or optimized about this |
|
|
|
gte_ldv3(v0, v1, v2); |
|
|
|
gte_rtpt(); // Rotation, translation, perspective projection |
|
|
|
|
|
|
|
// Normal clipping for backface culling |
|
|
|
gte_nclip(); |
|
|
|
gte_stopz(&p); |
|
|
|
if (p < 0) |
|
|
|
continue; |
|
|
|
|
|
|
|
// Average Z for depth sorting and culling |
|
|
|
gte_avsz3(); |
|
|
|
gte_stotz(&p); |
|
|
|
unsigned short depth = p >> 2; |
|
|
|
if (depth <= 0 || depth >= OTLEN) |
|
|
|
continue; |
|
|
|
|
|
|
|
// Draw a flat-shaded untextured colored triangle |
|
|
|
POLY_G3 *poly = (POLY_G3*)mem_prim(sizeof(POLY_G3)); |
|
|
|
if (poly == NULL) |
|
|
|
break; |
|
|
|
|
|
|
|
setPolyG3(poly); |
|
|
|
gte_stsxy3_g3(poly); |
|
|
|
|
|
|
|
poly->r0 = poly->g0 = poly->b0 = (uint8_t)v0->pad; |
|
|
|
poly->r1 = poly->g1 = poly->b1 = (uint8_t)v1->pad; |
|
|
|
poly->r2 = poly->g2 = poly->b2 = (uint8_t)v2->pad; |
|
|
|
|
|
|
|
addPrim(curOT + depth, poly); |
|
|
|
++polyCount; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
static inline void drawface_quad_strip(const ps1bsp_face_t *face, SVECTOR *vecs) |
|
|
|
{ |
|
|
|
int p; |
|
|
|
|
|
|
|
// Draw the face as a quad strip |
|
|
|
const SVECTOR *v0, *v1, *v2, *v3; |
|
|
|
const SVECTOR *head = vecs; |
|
|
|
@ -104,7 +191,6 @@ void world_draw(const world_t *world) |
|
|
|
break; |
|
|
|
|
|
|
|
setPolyG4(poly); |
|
|
|
|
|
|
|
gte_stsxy0(&poly->x0); |
|
|
|
gte_stsxy1(&poly->x1); |
|
|
|
gte_stsxy2(&poly->x2); |
|
|
|
@ -123,4 +209,35 @@ void world_draw(const world_t *world) |
|
|
|
++polyCount; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void world_draw(const world_t *world) |
|
|
|
{ |
|
|
|
int p; |
|
|
|
|
|
|
|
// The world doesn't move, so we just set the camera view-projection matrix |
|
|
|
gte_SetRotMatrix(&vp_matrix); |
|
|
|
gte_SetTransMatrix(&vp_matrix); |
|
|
|
|
|
|
|
for (int faceIdx = 0; faceIdx < world->header->numFaces; ++faceIdx) |
|
|
|
{ |
|
|
|
const ps1bsp_face_t *face = &world->faces[faceIdx]; |
|
|
|
const CVECTOR *col = &colors[faceIdx % numColors]; |
|
|
|
|
|
|
|
char *scratch = scratchpad; |
|
|
|
SVECTOR *vecs = (SVECTOR*)mem_scratch(&scratch, sizeof(SVECTOR) * face->numFaceVertices); |
|
|
|
|
|
|
|
// Copy this face's vertices into scratch RAM for fast reuse |
|
|
|
ps1bsp_facevertex_t *faceVertex = &world->faceVertices[face->firstFaceVertex]; |
|
|
|
for (int vertIdx = 0; vertIdx < face->numFaceVertices; ++vertIdx, ++faceVertex) |
|
|
|
{ |
|
|
|
const ps1bsp_vertex_t *vert = &world->vertices[faceVertex->index]; |
|
|
|
vecs[vertIdx] = *((SVECTOR*)vert); |
|
|
|
vecs[vertIdx].pad = vert->baseLight; |
|
|
|
} |
|
|
|
|
|
|
|
if (face->numFaceVertices == 3) |
|
|
|
drawface_triangle_fan(face, vecs); |
|
|
|
else |
|
|
|
drawface_quad_strip(face, vecs); |
|
|
|
} |
|
|
|
} |