From f4017d5f5605d1f99a4aa57e9c81c82f0edb26b8 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Sun, 15 Jan 2023 14:47:03 +0100 Subject: [PATCH] Modified triangle rendering code so that it finally works on real hardware. Not sure why the original method doesn't work, probably some weird limitation on the memory/GTE register subsystems. This will get rewritten anyway so it doesn't matter much now. --- display.c | 1 + main.c | 5 +++-- world.c | 26 +++++++++++++++++++------- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/display.c b/display.c index 28ca2b6..c101479 100644 --- a/display.c +++ b/display.c @@ -155,6 +155,7 @@ void display_start() gte_SetBackColor(48, 48, 48); // Ambient light color gte_SetColorMatrix(&light_cols); // Light color (up to three different lights) + gte_SetLightMatrix(&identity); MATRIX proj_matrix = quake_swizzle; // Swizzle coordinates so that everything is in Quake coordinate system (Z up) ScaleMatrixL(&proj_matrix, &aspect_scale); // Apply aspect ratio correction for the current resolution diff --git a/main.c b/main.c index 2b6246f..a52f2a4 100644 --- a/main.c +++ b/main.c @@ -46,11 +46,12 @@ int main(int argc, const char *argv[]) display_start(); // Works + FntPrint(-1, "Time: %d, ticks = %d, delta time = %d, fps = %d\n", (time_getRealTime() * 1000) >> 12, time_getFrameNumber(), time_getDeltaTime(), time_getFrameRate() >> 8); + FntPrint(-1, "Camera pos = (%d, %d, %d) rot = (%d, %d, %d)\n", cam_pos.vx, cam_pos.vy, cam_pos.vz, cam_rot.vx, cam_rot.vy, cam_rot.vz); + // Draw stuff world_draw(&world); // Doesn't work >:( - FntPrint(-1, "Time: %d, ticks = %d, delta time = %d, fps = %d\n", (time_getRealTime() * 1000) >> 12, time_getFrameNumber(), time_getDeltaTime(), time_getFrameRate() >> 8); - FntPrint(-1, "Camera pos = (%d, %d, %d) rot = (%d, %d, %d)\n", cam_pos.vx, cam_pos.vy, cam_pos.vz, cam_rot.vx, cam_rot.vy, cam_rot.vz); FntFlush(-1); display_finish(); // Works diff --git a/world.c b/world.c index f87da95..ebc2f17 100644 --- a/world.c +++ b/world.c @@ -40,6 +40,7 @@ void world_load(const u_long *data, world_t *world) void world_draw(const world_t *world) { + SVECTOR vecs[3]; int p; // The world doesn't move, so we just set the camera view-projection matrix @@ -51,16 +52,27 @@ void world_draw(const world_t *world) const ps1bsp_face_t *face = &world->faces[faceIdx]; const CVECTOR *col = &colors[faceIdx % numColors]; - for (int triangleIdx = 0; triangleIdx < face->numTriangles; ++triangleIdx) + const ps1bsp_triangle_t *tri = &world->triangles[face->firstTriangleId]; + for (int triangleIdx = 0; triangleIdx < face->numTriangles; ++triangleIdx, ++tri) { - const ps1bsp_triangle_t *tri = &world->triangles[face->firstTriangleId + triangleIdx]; - // Naively draw the triangle with GTE, nothing special or optimized about this - SVECTOR *v0 = (SVECTOR*)&world->vertices[tri->vertex0]; - SVECTOR *v1 = (SVECTOR*)&world->vertices[tri->vertex1]; - SVECTOR *v2 = (SVECTOR*)&world->vertices[tri->vertex2]; + const ps1bsp_vertex_t *v0 = &world->vertices[tri->vertex0]; + const ps1bsp_vertex_t *v1 = &world->vertices[tri->vertex1]; + const ps1bsp_vertex_t *v2 = &world->vertices[tri->vertex2]; + + vecs[0].vx = v0->x; vecs[0].vy = v0->y; vecs[0].vz = v0->z; + vecs[1].vx = v1->x; vecs[1].vy = v1->y; vecs[1].vz = v1->z; + vecs[2].vx = v2->x; vecs[2].vy = v2->y; vecs[2].vz = v2->z; + gte_ldv3c(vecs); + + // This method *should* work but it doesn't on real hardware, for some reason. + // Perhaps the PS1 doesn't like loading data from random far-off memory locations directly into GTE registers? + // We probably want to use the scratchpad memory to prepare the vertex data for the GTE anyway. + // SVECTOR *v0 = (SVECTOR*)&world->vertices[tri->vertex0]; + // SVECTOR *v1 = (SVECTOR*)&world->vertices[tri->vertex1]; + // SVECTOR *v2 = (SVECTOR*)&world->vertices[tri->vertex2]; + // gte_ldv3(v0, v1, v2); - gte_ldv3(v0, v1, v2); gte_rtpt(); // Rotation, translation, perspective projection // Normal clipping for backface culling