From 9914c16d611e24918b8bb51e674b941584832860 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Sat, 10 Sep 2022 17:20:08 +0200 Subject: [PATCH] Cleaned up some things and made some fixes to allow the app to run on real hardware again. Turns out setting the padding to 0 on SVECTORS is quite necessary, to avoid getting into undefined behavior territory. --- main.c | 20 ++-- ps1anorms.h | 324 ++++++++++++++++++++++++++-------------------------- 2 files changed, 170 insertions(+), 174 deletions(-) diff --git a/main.c b/main.c index 8ef7e8e..0fe0032 100644 --- a/main.c +++ b/main.c @@ -31,7 +31,7 @@ #define SCREENHEIGHT 240 #define NUMVERTEXNORMALS 162 -static short anorms[NUMVERTEXNORMALS][3] = { +static short anorms[NUMVERTEXNORMALS][4] = { #include "ps1anorms.h" }; @@ -59,8 +59,6 @@ typedef struct ps1mdl_triangle_t* triangles; ps1mdl_vertex_t* vertices; - short halfSkinWidth; // Used for offsetting UVs on the back texture - VECTOR position; SVECTOR rotation; } ps1mdl_t; @@ -111,8 +109,6 @@ void loadModel(const u_long* data, ps1mdl_t *mdl) bytes += sizeof(ps1mdl_triangle_t) * mdl->header->triangleCount; mdl->vertices = (ps1mdl_vertex_t*)bytes; - - mdl->halfSkinWidth = mdl->header->skinWidth >> 1; } void loadTexture(const u_long* tim, ps1texture_t *texture) @@ -355,7 +351,7 @@ static int fakeLight(const int *norm) SVECTOR outPos; -void drawModel(ps1mdl_t *model, ps1skin_t *skin, int xOffset, int frameCounter) +void drawModel(ps1mdl_t *model, ps1skin_t *skin, int frameCounter) { ps1texture_t *tex; short u0, u1, u2, uoffs, voffs; @@ -373,7 +369,7 @@ void drawModel(ps1mdl_t *model, ps1skin_t *skin, int xOffset, int frameCounter) int frameNum = frameCounter % model->header->frameCount; int vertOffs = frameNum * model->header->vertexCount; - short halfSkinWidth = model->halfSkinWidth; + short halfSkinWidth = model->header->skinWidth >> 1; int *scale = model->header->scale; int *translate = model->header->translate; @@ -389,9 +385,9 @@ void drawModel(ps1mdl_t *model, ps1skin_t *skin, int xOffset, int frameCounter) // Swizzle the coordinates because Quake Z is up // TODO: scale and translate should be done by the GTE instead of through software here // Finding the appropriate scale range here is tricky. Normalize with >> 12 as usual and the coordinates become so small that you lose depth precision. - SVECTOR pos0 = { (v0->position[0] * scale[0] + translate[0]) >> 10, (-v0->position[2] * scale[2] + translate[2]) >> 10, (v0->position[1] * scale[1] + translate[1]) >> 10 }; - SVECTOR pos1 = { (v1->position[0] * scale[0] + translate[0]) >> 10, (-v1->position[2] * scale[2] + translate[2]) >> 10, (v1->position[1] * scale[1] + translate[1]) >> 10 }; - SVECTOR pos2 = { (v2->position[0] * scale[0] + translate[0]) >> 10, (-v2->position[2] * scale[2] + translate[2]) >> 10, (v2->position[1] * scale[1] + translate[1]) >> 10 }; + SVECTOR pos0 = { (scale[0] * v0->position[0] + translate[0]) >> 10, (scale[2] * -v0->position[2] + translate[2]) >> 10, (scale[1] * v0->position[1] + translate[1]) >> 10, 0 }; + SVECTOR pos1 = { (scale[0] * v1->position[0] + translate[0]) >> 10, (scale[2] * -v1->position[2] + translate[2]) >> 10, (scale[1] * v1->position[1] + translate[1]) >> 10, 0 }; + SVECTOR pos2 = { (scale[0] * v2->position[0] + translate[0]) >> 10, (scale[2] * -v2->position[2] + translate[2]) >> 10, (scale[1] * v2->position[1] + translate[1]) >> 10, 0 }; gte_ldv3(&pos0, &pos1, &pos2); gte_rtpt(); // Rotation, Translation and Perspective triplet (all three vertices at once) @@ -472,8 +468,8 @@ void drawStuff(int counter) gte_SetBackColor(48, 48, 48); // Ambient light color gte_SetColorMatrix(&color_mtx); // Light color and direction - drawModel(&playerModel, &playerSkin, -40, counter >> 2); - drawModel(&shamblerModel, &shamblerSkin, 80, counter >> 2); + drawModel(&playerModel, &playerSkin, counter >> 2); + drawModel(&shamblerModel, &shamblerSkin, counter >> 2); } // Main function, program entrypoint diff --git a/ps1anorms.h b/ps1anorms.h index 3eb399c..476c127 100644 --- a/ps1anorms.h +++ b/ps1anorms.h @@ -1,162 +1,162 @@ -{-2153, 0, 3484}, -{-1813, 978, 3539}, -{-1209, 0, 3913}, -{-1265, 2048, 3313}, -{-665, 1076, 3895}, -{0, 0, 4096}, -{0, 3484, 2153}, -{-604, 2935, 2792}, -{604, 2935, 2792}, -{0, 2153, 3484}, -{1265, 2048, 3313}, -{2153, 0, 3484}, -{1209, 0, 3913}, -{1813, 978, 3539}, -{665, 1076, 3895}, -{-2792, 604, 2935}, -{-3313, 1265, 2048}, -{-2407, 1742, 2818}, -{-3484, 2153, 0}, -{-3539, 1813, 978}, -{-2935, 2792, 604}, -{-2818, 2407, 1742}, -{-2048, 3313, 1265}, -{-978, 3539, 1813}, -{-1742, 2818, 2407}, -{-2935, 2792, -604}, -{-2048, 3313, -1265}, -{-2153, 3484, 0}, -{0, 3484, -2153}, -{-978, 3539, -1813}, -{0, 3913, -1209}, -{-1076, 3895, -665}, -{0, 4096, 0}, -{0, 3913, 1209}, -{-1076, 3895, 665}, -{978, 3539, 1813}, -{1076, 3895, 665}, -{2048, 3313, 1265}, -{978, 3539, -1813}, -{1076, 3895, -665}, -{2048, 3313, -1265}, -{3484, 2153, 0}, -{2935, 2792, 604}, -{2935, 2792, -604}, -{2153, 3484, 0}, -{1742, 2818, 2407}, -{3539, 1813, 978}, -{2818, 2407, 1742}, -{3313, 1265, 2048}, -{2792, 604, 2935}, -{2407, 1742, 2818}, -{3913, 1209, 0}, -{4096, 0, 0}, -{3895, 665, 1076}, -{3484, -2153, 0}, -{3913, -1209, 0}, -{3539, -1813, 978}, -{3895, -665, 1076}, -{3313, -1265, 2048}, -{2792, -604, 2935}, -{3484, 0, 2153}, -{3539, 1813, -978}, -{3313, 1265, -2048}, -{3895, 665, -1076}, -{2153, 0, -3484}, -{2792, 604, -2935}, -{2792, -604, -2935}, -{3484, 0, -2153}, -{3313, -1265, -2048}, -{3539, -1813, -978}, -{3895, -665, -1076}, -{604, 2935, -2792}, -{1265, 2048, -3313}, -{1742, 2818, -2407}, -{1813, 978, -3539}, -{2407, 1742, -2818}, -{2818, 2407, -1742}, -{-604, 2935, -2792}, -{-1265, 2048, -3313}, -{0, 2153, -3484}, -{-2153, 0, -3484}, -{-1813, 978, -3539}, -{-1209, 0, -3913}, -{-665, 1076, -3895}, -{0, 0, -4096}, -{1209, 0, -3913}, -{665, 1076, -3895}, -{-1813, -978, -3539}, -{-1265, -2048, -3313}, -{-665, -1076, -3895}, -{0, -3484, -2153}, -{-604, -2935, -2792}, -{604, -2935, -2792}, -{0, -2153, -3484}, -{1265, -2048, -3313}, -{1813, -978, -3539}, -{665, -1076, -3895}, -{978, -3539, -1813}, -{2048, -3313, -1265}, -{1742, -2818, -2407}, -{2935, -2792, -604}, -{2818, -2407, -1742}, -{2407, -1742, -2818}, -{0, -3913, -1209}, -{0, -4096, 0}, -{1076, -3895, -665}, -{0, -3484, 2153}, -{0, -3913, 1209}, -{978, -3539, 1813}, -{1076, -3895, 665}, -{2048, -3313, 1265}, -{2935, -2792, 604}, -{2153, -3484, 0}, -{-978, -3539, -1813}, -{-2048, -3313, -1265}, -{-1076, -3895, -665}, -{-3484, -2153, 0}, -{-2935, -2792, -604}, -{-2935, -2792, 604}, -{-2153, -3484, 0}, -{-2048, -3313, 1265}, -{-978, -3539, 1813}, -{-1076, -3895, 665}, -{-3539, -1813, 978}, -{-3313, -1265, 2048}, -{-2818, -2407, 1742}, -{-2792, -604, 2935}, -{-1813, -978, 3539}, -{-2407, -1742, 2818}, -{-1265, -2048, 3313}, -{-604, -2935, 2792}, -{-1742, -2818, 2407}, -{-665, -1076, 3895}, -{1813, -978, 3539}, -{665, -1076, 3895}, -{1265, -2048, 3313}, -{604, -2935, 2792}, -{0, -2153, 3484}, -{1742, -2818, 2407}, -{2407, -1742, 2818}, -{2818, -2407, 1742}, -{-3913, 1209, 0}, -{-3895, 665, 1076}, -{-4096, 0, 0}, -{-3484, 0, 2153}, -{-3913, -1209, 0}, -{-3895, -665, 1076}, -{-3539, 1813, -978}, -{-3895, 665, -1076}, -{-3313, 1265, -2048}, -{-3539, -1813, -978}, -{-3895, -665, -1076}, -{-3313, -1265, -2048}, -{-2792, 604, -2935}, -{-2792, -604, -2935}, -{-3484, 0, -2153}, -{-2818, 2407, -1742}, -{-2407, 1742, -2818}, -{-1742, 2818, -2407}, -{-1742, -2818, -2407}, -{-2407, -1742, -2818}, -{-2818, -2407, -1742}, +{-2153, 0, 3484, 0}, +{-1813, 978, 3539, 0}, +{-1209, 0, 3913, 0}, +{-1265, 2048, 3313, 0}, +{-665, 1076, 3895, 0}, +{0, 0, 4096, 0}, +{0, 3484, 2153, 0}, +{-604, 2935, 2792, 0}, +{604, 2935, 2792, 0}, +{0, 2153, 3484, 0}, +{1265, 2048, 3313, 0}, +{2153, 0, 3484, 0}, +{1209, 0, 3913, 0}, +{1813, 978, 3539, 0}, +{665, 1076, 3895, 0}, +{-2792, 604, 2935, 0}, +{-3313, 1265, 2048, 0}, +{-2407, 1742, 2818, 0}, +{-3484, 2153, 0, 0}, +{-3539, 1813, 978, 0}, +{-2935, 2792, 604, 0}, +{-2818, 2407, 1742, 0}, +{-2048, 3313, 1265, 0}, +{-978, 3539, 1813, 0}, +{-1742, 2818, 2407, 0}, +{-2935, 2792, -604, 0}, +{-2048, 3313, -1265, 0}, +{-2153, 3484, 0, 0}, +{0, 3484, -2153, 0}, +{-978, 3539, -1813, 0}, +{0, 3913, -1209, 0}, +{-1076, 3895, -665, 0}, +{0, 4096, 0, 0}, +{0, 3913, 1209, 0}, +{-1076, 3895, 665, 0}, +{978, 3539, 1813, 0}, +{1076, 3895, 665, 0}, +{2048, 3313, 1265, 0}, +{978, 3539, -1813, 0}, +{1076, 3895, -665, 0}, +{2048, 3313, -1265, 0}, +{3484, 2153, 0, 0}, +{2935, 2792, 604, 0}, +{2935, 2792, -604, 0}, +{2153, 3484, 0, 0}, +{1742, 2818, 2407, 0}, +{3539, 1813, 978, 0}, +{2818, 2407, 1742, 0}, +{3313, 1265, 2048, 0}, +{2792, 604, 2935, 0}, +{2407, 1742, 2818, 0}, +{3913, 1209, 0, 0}, +{4096, 0, 0, 0}, +{3895, 665, 1076, 0}, +{3484, -2153, 0, 0}, +{3913, -1209, 0, 0}, +{3539, -1813, 978, 0}, +{3895, -665, 1076, 0}, +{3313, -1265, 2048, 0}, +{2792, -604, 2935, 0}, +{3484, 0, 2153, 0}, +{3539, 1813, -978, 0}, +{3313, 1265, -2048, 0}, +{3895, 665, -1076, 0}, +{2153, 0, -3484, 0}, +{2792, 604, -2935, 0}, +{2792, -604, -2935, 0}, +{3484, 0, -2153, 0}, +{3313, -1265, -2048, 0}, +{3539, -1813, -978, 0}, +{3895, -665, -1076, 0}, +{604, 2935, -2792, 0}, +{1265, 2048, -3313, 0}, +{1742, 2818, -2407, 0}, +{1813, 978, -3539, 0}, +{2407, 1742, -2818, 0}, +{2818, 2407, -1742, 0}, +{-604, 2935, -2792, 0}, +{-1265, 2048, -3313, 0}, +{0, 2153, -3484, 0}, +{-2153, 0, -3484, 0}, +{-1813, 978, -3539, 0}, +{-1209, 0, -3913, 0}, +{-665, 1076, -3895, 0}, +{0, 0, -4096, 0}, +{1209, 0, -3913, 0}, +{665, 1076, -3895, 0}, +{-1813, -978, -3539, 0}, +{-1265, -2048, -3313, 0}, +{-665, -1076, -3895, 0}, +{0, -3484, -2153, 0}, +{-604, -2935, -2792, 0}, +{604, -2935, -2792, 0}, +{0, -2153, -3484, 0}, +{1265, -2048, -3313, 0}, +{1813, -978, -3539, 0}, +{665, -1076, -3895, 0}, +{978, -3539, -1813, 0}, +{2048, -3313, -1265, 0}, +{1742, -2818, -2407, 0}, +{2935, -2792, -604, 0}, +{2818, -2407, -1742, 0}, +{2407, -1742, -2818, 0}, +{0, -3913, -1209, 0}, +{0, -4096, 0, 0}, +{1076, -3895, -665, 0}, +{0, -3484, 2153, 0}, +{0, -3913, 1209, 0}, +{978, -3539, 1813, 0}, +{1076, -3895, 665, 0}, +{2048, -3313, 1265, 0}, +{2935, -2792, 604, 0}, +{2153, -3484, 0, 0}, +{-978, -3539, -1813, 0}, +{-2048, -3313, -1265, 0}, +{-1076, -3895, -665, 0}, +{-3484, -2153, 0, 0}, +{-2935, -2792, -604, 0}, +{-2935, -2792, 604, 0}, +{-2153, -3484, 0, 0}, +{-2048, -3313, 1265, 0}, +{-978, -3539, 1813, 0}, +{-1076, -3895, 665, 0}, +{-3539, -1813, 978, 0}, +{-3313, -1265, 2048, 0}, +{-2818, -2407, 1742, 0}, +{-2792, -604, 2935, 0}, +{-1813, -978, 3539, 0}, +{-2407, -1742, 2818, 0}, +{-1265, -2048, 3313, 0}, +{-604, -2935, 2792, 0}, +{-1742, -2818, 2407, 0}, +{-665, -1076, 3895, 0}, +{1813, -978, 3539, 0}, +{665, -1076, 3895, 0}, +{1265, -2048, 3313, 0}, +{604, -2935, 2792, 0}, +{0, -2153, 3484, 0}, +{1742, -2818, 2407, 0}, +{2407, -1742, 2818, 0}, +{2818, -2407, 1742, 0}, +{-3913, 1209, 0, 0}, +{-3895, 665, 1076, 0}, +{-4096, 0, 0, 0}, +{-3484, 0, 2153, 0}, +{-3913, -1209, 0, 0}, +{-3895, -665, 1076, 0}, +{-3539, 1813, -978, 0}, +{-3895, 665, -1076, 0}, +{-3313, 1265, -2048, 0}, +{-3539, -1813, -978, 0}, +{-3895, -665, -1076, 0}, +{-3313, -1265, -2048, 0}, +{-2792, 604, -2935, 0}, +{-2792, -604, -2935, 0}, +{-3484, 0, -2153, 0}, +{-2818, 2407, -1742, 0}, +{-2407, 1742, -2818, 0}, +{-1742, 2818, -2407, 0}, +{-1742, -2818, -2407, 0}, +{-2407, -1742, -2818, 0}, +{-2818, -2407, -1742, 0},