Browse Source

First pass at implementing texturing of face polygons.

Not quite right yet but things are starting to work.
unrollquadloop
Nico de Poel 3 years ago
parent
commit
76a6dc92d5
  1. 2
      CMakeLists.txt
  2. BIN
      atlas-start.tim
  3. 7
      common.h
  4. 8
      main.c
  5. 24
      ps1bsp.h
  6. BIN
      test.ps1bsp
  7. 116
      world.c
  8. 3
      world.h

2
CMakeLists.txt

@ -14,7 +14,7 @@ project(
file(GLOB _sources *.c) file(GLOB _sources *.c)
psn00bsdk_add_executable(ps1bsp STATIC ${_sources}) 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_target_incbin(ps1bsp PRIVATE bsp_test test.ps1bsp)
psn00bsdk_add_cd_image( psn00bsdk_add_cd_image(

BIN
atlas-start.tim

7
common.h

@ -10,6 +10,11 @@
#include <psxgte.h> #include <psxgte.h>
#include <psxgpu.h> #include <psxgpu.h>
typedef struct _STVECTOR {
int16_t vx, vy, vz;
int16_t u, v, pad;
} STVECTOR;
#define INLINE __attribute__((always_inline)) inline #define INLINE __attribute__((always_inline)) inline
#include "memory.h" #include "memory.h"
@ -19,4 +24,6 @@ extern VECTOR cam_pos;
extern SVECTOR cam_rot; extern SVECTOR cam_rot;
extern u_short cam_leaf; extern u_short cam_leaf;
extern u_short quake_clut;
#endif // __COMMON_H__ #endif // __COMMON_H__

8
main.c

@ -5,7 +5,7 @@
#include "asset.h" #include "asset.h"
#include "world.h" #include "world.h"
extern u_long tim_e1m1[];
extern u_long tim_start[];
extern u_long bsp_test[]; extern u_long bsp_test[];
VECTOR cam_pos = { 2176, 1152, 272 }; // START VECTOR cam_pos = { 2176, 1152, 272 }; // START
@ -15,6 +15,8 @@ u_short cam_leaf = 0;
world_t world; world_t world;
u_short quake_clut;
// BSP face rendering: // BSP face rendering:
// - Gather vertex data from face start index + length // - Gather vertex data from face start index + length
// - Store vertex data in scratchpad memory (should be plenty of space for one face) // - Store vertex data in scratchpad memory (should be plenty of space for one face)
@ -29,7 +31,9 @@ void init(void)
display_init(); display_init();
time_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); world_load(bsp_test, &world);
} }

24
ps1bsp.h

@ -28,6 +28,7 @@ typedef struct
{ {
u_short version; u_short version;
ps1bsp_dentry_t textures;
ps1bsp_dentry_t vertices; ps1bsp_dentry_t vertices;
ps1bsp_dentry_t faces; ps1bsp_dentry_t faces;
ps1bsp_dentry_t faceVertices; ps1bsp_dentry_t faceVertices;
@ -41,17 +42,15 @@ typedef struct
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 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 unsigned short nextframe; // If non-zero, the texture is animated and this points to the next texture in the sequence
} ps1bsp_texture_t; } ps1bsp_texture_t;
// This matches the SVECTOR data type; we can use the extra padding to store some more data. // This matches the SVECTOR data type; we can use the extra padding to store some more data.
typedef struct typedef struct
{ {
short x;
short y;
short z;
short x, y, z;
short pad; short pad;
} ps1bsp_vertex_t; } ps1bsp_vertex_t;
@ -60,8 +59,13 @@ typedef struct
unsigned short index; unsigned short index;
unsigned short light; 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; } ps1bsp_facevertex_t;
typedef struct typedef struct
@ -70,7 +74,9 @@ typedef struct
unsigned short side; unsigned short side;
unsigned short firstFaceVertex; unsigned short firstFaceVertex;
unsigned short numFaceVertices;
unsigned char numFaceVertices;
unsigned char textureId;
SVECTOR center; SVECTOR center;
@ -120,7 +126,7 @@ typedef struct
typedef struct typedef struct
{ {
unsigned short length; unsigned short length;
char message[];
char message[1];
} ps1bsp_message_t; } ps1bsp_message_t;
#ifdef __cplusplus #ifdef __cplusplus

BIN
test.ps1bsp

116
world.c

@ -39,6 +39,7 @@ void world_load(const u_long *data, world_t *world)
ps1bsp_header_t* header = (ps1bsp_header_t*)bytes; 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_vertex_t, world->vertices, world->numVertices, bytes, header->vertices);
LOAD_CHUNK(ps1bsp_face_t, world->faces, world->numFaces, bytes, header->faces); LOAD_CHUNK(ps1bsp_face_t, world->faces, world->numFaces, bytes, header->faces);
LOAD_CHUNK(ps1bsp_facevertex_t, world->faceVertices, world->numFaceVertices, bytes, header->faceVertices); 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) 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 // 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) if ((dot >= 0) ^ face->side)
return; 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 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) static void world_drawnode(const world_t *world, short nodeIdx, u_char *pvs)

3
world.h

@ -5,6 +5,9 @@
typedef struct typedef struct
{ {
u_short numTextures;
ps1bsp_texture_t *textures;
u_short numVertices; u_short numVertices;
ps1bsp_vertex_t *vertices; ps1bsp_vertex_t *vertices;

Loading…
Cancel
Save