Browse Source

First checkpoint for picking apart and reworking transformations

master
Nico de Poel 3 years ago
parent
commit
891a010458
  1. 48
      main.c

48
main.c

@ -357,23 +357,45 @@ void drawModel(ps1mdl_t *model, ps1skin_t *skin, int frameCounter)
short u0, u1, u2, uoffs, voffs;
int p;
int frameNum = frameCounter % model->header->frameCount;
int vertOffs = frameNum * model->header->vertexCount;
short halfSkinWidth = model->header->skinWidth >> 1;
int *scale = model->header->scale;
int *translate = model->header->translate;
// Expected order of things:
// - Initial scale matrix to correct aspect ratio
// - Coordinate system swizzle (scale) (can probably be combined with the above)
// - Camera inverse translation
// - Camera inverse rotation (apply on top of camera translation)
// - Multiply light matrix (lights will be in Quake coordinate system)
// - Push matrix
// - Entity local rotation
// - Entity local translation
// - Model internal scale
// - Model internal translate
// - Render
// - Pop matrix
MATRIX mtx, lmtx;
RotMatrix(&model->rotation, &mtx);
ScaleMatrixL(&mtx, &cam_scale); // Aspect ratio correction
TransMatrix(&mtx, &model->position);
MulMatrix0(&light_mtx, &mtx, &lmtx);
// Model internal scale and translate, to transform vertices from [0..255] range to a larger dynamic range with appropriate world-relative size
// Swizzle the coordinates because Quake Z is up
// Finding the appropriate scale range here is tricky. Without the << 2 scale the vertex coordinates become so small that you lose depth precision.
MATRIX local = { .m = { ONE << 2, 0, 0, 0, 0, -ONE << 2, 0, ONE << 2, 0 }, .t = { 0 } };
ScaleMatrixL(&local, (VECTOR*)scale);
TransMatrix(&local, (VECTOR*)translate);
MulMatrix(&mtx, &local);
gte_SetRotMatrix(&mtx);
gte_SetTransMatrix(&mtx);
gte_SetLightMatrix(&lmtx);
int frameNum = frameCounter % model->header->frameCount;
int vertOffs = frameNum * model->header->vertexCount;
short halfSkinWidth = model->header->skinWidth >> 1;
int *scale = model->header->scale;
int *translate = model->header->translate;
for (int triIdx = 0; triIdx < model->header->triangleCount; ++triIdx)
{
ps1mdl_triangle_t *tri = &model->triangles[triIdx];
@ -382,12 +404,9 @@ void drawModel(ps1mdl_t *model, ps1skin_t *skin, int frameCounter)
ps1mdl_vertex_t *v1 = &model->vertices[tri->vertexIndex[1] + vertOffs];
ps1mdl_vertex_t *v2 = &model->vertices[tri->vertexIndex[2] + vertOffs];
// 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 = { (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 };
SVECTOR pos0 = { v0->position[0], v0->position[1], v0->position[2], 0 };
SVECTOR pos1 = { v1->position[0], v1->position[1], v1->position[2], 0 };
SVECTOR pos2 = { v2->position[0], v2->position[1], v2->position[2], 0 };
gte_ldv3(&pos0, &pos1, &pos2);
gte_rtpt(); // Rotation, Translation and Perspective triplet (all three vertices at once)
@ -439,6 +458,7 @@ void drawModel(ps1mdl_t *model, ps1skin_t *skin, int frameCounter)
outPos.vz = p;
// Calculate vertex color based on normal
// TODO: we could probably speed this up by precalculating the lighting for each normal, if we need to draw more than 162 vertices
gte_ldrgb(&poly->r0);
gte_ldv3(anorms[v0->normalIndex], anorms[v1->normalIndex], anorms[v2->normalIndex]);
gte_nct();
@ -466,7 +486,7 @@ void drawStuff(int counter)
FntFlush(-1);
gte_SetBackColor(48, 48, 48); // Ambient light color
gte_SetColorMatrix(&color_mtx); // Light color and direction
gte_SetColorMatrix(&color_mtx); // Light color (up to three different lights)
drawModel(&playerModel, &playerSkin, counter >> 2);
drawModel(&shamblerModel, &shamblerSkin, counter >> 2);

Loading…
Cancel
Save