diff --git a/CMakeLists.txt b/CMakeLists.txt index e1d8e77..867de59 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,7 +14,7 @@ project( file(GLOB _sources *.c) psn00bsdk_add_executable(ps1bsp STATIC ${_sources}) -psn00bsdk_target_incbin(ps1bsp PRIVATE tim_e1m1 atlas-e1m1.tim) +psn00bsdk_target_incbin(ps1bsp PRIVATE tim_start atlas-start.tim) psn00bsdk_target_incbin(ps1bsp PRIVATE bsp_test test.ps1bsp) psn00bsdk_add_cd_image( diff --git a/atlas-start.tim b/atlas-start.tim new file mode 100644 index 0000000..70ec62b Binary files /dev/null and b/atlas-start.tim differ diff --git a/common.h b/common.h index 527a20f..4daf011 100644 --- a/common.h +++ b/common.h @@ -10,6 +10,11 @@ #include #include +typedef struct _STVECTOR { + int16_t vx, vy, vz; + int16_t u, v, pad; +} STVECTOR; + #define INLINE __attribute__((always_inline)) inline #include "memory.h" @@ -19,4 +24,6 @@ extern VECTOR cam_pos; extern SVECTOR cam_rot; extern u_short cam_leaf; +extern u_short quake_clut; + #endif // __COMMON_H__ diff --git a/main.c b/main.c index fb278dd..e773ebc 100644 --- a/main.c +++ b/main.c @@ -5,7 +5,7 @@ #include "asset.h" #include "world.h" -extern u_long tim_e1m1[]; +extern u_long tim_start[]; extern u_long bsp_test[]; VECTOR cam_pos = { 2176, 1152, 272 }; // START @@ -15,6 +15,8 @@ u_short cam_leaf = 0; world_t world; +u_short quake_clut; + // BSP face rendering: // - Gather vertex data from face start index + length // - Store vertex data in scratchpad memory (should be plenty of space for one face) @@ -29,7 +31,9 @@ void init(void) display_init(); time_init(); - //asset_loadTexture(tim_e1m1, NULL); + ps1texture_t maptex; + asset_loadTexture(tim_start, &maptex); + quake_clut = getClut(maptex.crect.x, maptex.crect.y); world_load(bsp_test, &world); } diff --git a/ps1bsp.h b/ps1bsp.h index 74fd2cb..b355ae4 100755 --- a/ps1bsp.h +++ b/ps1bsp.h @@ -28,6 +28,7 @@ typedef struct { u_short version; + ps1bsp_dentry_t textures; ps1bsp_dentry_t vertices; ps1bsp_dentry_t faces; ps1bsp_dentry_t faceVertices; @@ -41,17 +42,15 @@ typedef struct typedef struct { unsigned char w, h; // These may be necessary for scaling UVs, especially since we use a mix of mip0 and mip1 textures - int tpage; // Texture page in PS1 VRAM (precalculated when generating the texture atlas) - short uoffs, voffs; // Texture coordinate offset within the texture page + unsigned short tpage; // Texture page in PS1 VRAM (precalculated when generating the texture atlas) + unsigned char uoffs, voffs; // Texture coordinate offset within the texture page unsigned short nextframe; // If non-zero, the texture is animated and this points to the next texture in the sequence } ps1bsp_texture_t; // This matches the SVECTOR data type; we can use the extra padding to store some more data. typedef struct { - short x; - short y; - short z; + short x, y, z; short pad; } ps1bsp_vertex_t; @@ -60,8 +59,13 @@ typedef struct unsigned short index; unsigned short light; - // TODO: add texture uv's - // TODO: add sampled texture color * light, for untextured gouraud shaded drawing at range + unsigned char u, v; // TODO: make into unsigned short, clamp/mask/modulo to u_char at run-time. So we can build tiling polygons later. + + // Sampled texture color * light, for untextured gouraud shaded drawing at range + unsigned char a : 1; + unsigned char r : 5; + unsigned char g : 5; + unsigned char b : 5; } ps1bsp_facevertex_t; typedef struct @@ -70,7 +74,9 @@ typedef struct unsigned short side; unsigned short firstFaceVertex; - unsigned short numFaceVertices; + unsigned char numFaceVertices; + + unsigned char textureId; SVECTOR center; @@ -120,7 +126,7 @@ typedef struct typedef struct { unsigned short length; - char message[]; + char message[1]; } ps1bsp_message_t; #ifdef __cplusplus diff --git a/test.ps1bsp b/test.ps1bsp index 3b0d9cb..49a0e86 100755 Binary files a/test.ps1bsp and b/test.ps1bsp differ diff --git a/world.c b/world.c index 30b4a70..0898208 100644 --- a/world.c +++ b/world.c @@ -39,6 +39,7 @@ void world_load(const u_long *data, world_t *world) ps1bsp_header_t* header = (ps1bsp_header_t*)bytes; + LOAD_CHUNK(ps1bsp_texture_t, world->textures, world->numTextures, bytes, header->textures); LOAD_CHUNK(ps1bsp_vertex_t, world->vertices, world->numVertices, bytes, header->vertices); LOAD_CHUNK(ps1bsp_face_t, world->faces, world->numFaces, bytes, header->faces); LOAD_CHUNK(ps1bsp_facevertex_t, world->faceVertices, world->numFaceVertices, bytes, header->faceVertices); @@ -203,6 +204,70 @@ static INLINE void drawface_quad_strip(const ps1bsp_face_t *face, SVECTOR *vecs) } } +static INLINE void drawface_quad_strip_tex(const ps1bsp_face_t *face, STVECTOR *vecs, u_short tpage) +{ + int p; + + if (!mem_checkprim(sizeof(POLY_GT4), face->numFaceVertices)) + return; + + // Draw the face as a quad strip + const STVECTOR *v0, *v1, *v2, *v3; + const STVECTOR *head = vecs; + const STVECTOR *tail = vecs + face->numFaceVertices; + + // Initialize the first two vertices + v2 = --tail; + v3 = 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 = (face->numFaceVertices - 1) / 2; + 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_GT4 *poly = (POLY_GT4*)mem_prim(sizeof(POLY_GT4)); + setPolyGT4(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; + + // Vertex color lighting + 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; + poly->r3 = poly->g3 = poly->b3 = (uint8_t)v3->pad; + + addPrim(curOT + depth, poly); + ++polyCount; + } +} + static INLINE short world_pointPlaneDist(const VECTOR *point, const ps1bsp_plane_t *plane) { // Make use of axis-aligned planes to skip the need for a dot product @@ -237,22 +302,47 @@ static void world_drawface(const world_t *world, const ps1bsp_face_t *face) if ((dot >= 0) ^ face->side) return; - SVECTOR *vecs = (SVECTOR*)(scratchpad + 256); - - // Copy this face's vertices into scratch RAM for fast reuse - // TODO: this is the main performance bottleneck right now! - ps1bsp_facevertex_t *faceVertex = &world->faceVertices[face->firstFaceVertex]; - for (int vertIdx = 0; vertIdx < face->numFaceVertices; ++vertIdx, ++faceVertex) + if (0) { - const ps1bsp_vertex_t *vert = &world->vertices[faceVertex->index]; - vecs[vertIdx] = *((SVECTOR*)vert); - vecs[vertIdx].pad = faceVertex->light; - } + // Draw untextured, vertex colored polygons + SVECTOR *vecs = (SVECTOR*)(scratchpad + 256); + + // Copy this face's vertices into scratch RAM for fast reuse + // TODO: this is the main performance bottleneck right now! + ps1bsp_facevertex_t *faceVertex = &world->faceVertices[face->firstFaceVertex]; + SVECTOR *curVec = vecs; + for (int vertIdx = 0; vertIdx < face->numFaceVertices; ++vertIdx, ++faceVertex, ++curVec) + { + const ps1bsp_vertex_t *vert = &world->vertices[faceVertex->index]; + *curVec = *((SVECTOR*)vert); + curVec->pad = faceVertex->light; + } - if (face->numFaceVertices == 3) // Special case: draw single triangles using the simplest method - drawface_triangle_fan(face, vecs); + if (face->numFaceVertices == 3) // Special case: draw single triangles using the simplest method + drawface_triangle_fan(face, vecs); + else + drawface_quad_strip(face, vecs); + } else - drawface_quad_strip(face, vecs); + { + // Draw textured, vertex colored polygons + STVECTOR *vecs = (STVECTOR*)(scratchpad + 256); + + // Copy this face's vertices into scratch RAM for fast reuse + ps1bsp_facevertex_t *faceVertex = &world->faceVertices[face->firstFaceVertex]; + ps1bsp_texture_t *faceTexture = &world->textures[face->textureId]; + STVECTOR *curVec = vecs; + for (int vertIdx = 0; vertIdx < face->numFaceVertices; ++vertIdx, ++faceVertex, ++curVec) + { + const ps1bsp_vertex_t *vert = &world->vertices[faceVertex->index]; + *((SVECTOR*)curVec) = *((SVECTOR*)vert); + curVec->u = faceVertex->u + faceTexture->uoffs; + curVec->v = faceVertex->v + faceTexture->voffs; + curVec->pad = faceVertex->light; + } + + drawface_quad_strip_tex(face, vecs, faceTexture->tpage); + } } static void world_drawnode(const world_t *world, short nodeIdx, u_char *pvs) diff --git a/world.h b/world.h index fe23b3a..0f2bf6e 100644 --- a/world.h +++ b/world.h @@ -5,6 +5,9 @@ typedef struct { + u_short numTextures; + ps1bsp_texture_t *textures; + u_short numVertices; ps1bsp_vertex_t *vertices;