Browse Source

Cleaned up and inlined the model drawing code, which bumps performance back up again.

master
Nico de Poel 3 years ago
parent
commit
9525e6e134
  1. 53
      main.c

53
main.c

@ -288,7 +288,7 @@ static int fakeLight(const int *norm)
lit = lit < 0 ? 0 : lit; // Clamp to [0..255] lit = lit < 0 ? 0 : lit; // Clamp to [0..255]
lit += 48; // Add some ambient light lit += 48; // Add some ambient light
lit = lit > 255 ? 255 : lit; // Clamp to [0..255] again lit = lit > 255 ? 255 : lit; // Clamp to [0..255] again
return lit << 16 | lit << 8 | lit;
return lit;
} }
void drawModel(ps1mdl_t *model, TIM_IMAGE *frontTex, TIM_IMAGE *backTex, int xOffset, int frameCounter) void drawModel(ps1mdl_t *model, TIM_IMAGE *frontTex, TIM_IMAGE *backTex, int xOffset, int frameCounter)
@ -304,41 +304,42 @@ void drawModel(ps1mdl_t *model, TIM_IMAGE *frontTex, TIM_IMAGE *backTex, int xOf
ps1mdl_vertex_t *v1 = &model->vertices[tri->vertexIndex[1] + vertOffs]; ps1mdl_vertex_t *v1 = &model->vertices[tri->vertexIndex[1] + vertOffs];
ps1mdl_vertex_t *v2 = &model->vertices[tri->vertexIndex[2] + vertOffs]; ps1mdl_vertex_t *v2 = &model->vertices[tri->vertexIndex[2] + vertOffs];
// Normally you'd have GTE do this but we're just going for a quick hack now
unsigned short depth = ((unsigned short)v0->position[1] + (unsigned short)v1->position[1] + (unsigned short)v2->position[1]) / 3;
if (depth >= OTLEN)
continue;
ps1mdl_texcoord_t *tc0 = &model->texCoords[tri->vertexIndex[0]]; ps1mdl_texcoord_t *tc0 = &model->texCoords[tri->vertexIndex[0]];
ps1mdl_texcoord_t *tc1 = &model->texCoords[tri->vertexIndex[1]]; ps1mdl_texcoord_t *tc1 = &model->texCoords[tri->vertexIndex[1]];
ps1mdl_texcoord_t *tc2 = &model->texCoords[tri->vertexIndex[2]]; ps1mdl_texcoord_t *tc2 = &model->texCoords[tri->vertexIndex[2]];
// Use normal indices to generate a greyscale value. Good for just giving the polygons some gradients.
unsigned int n0 = v0->normalIndex, n1 = v1->normalIndex, n2 = v2->normalIndex;
unsigned int rgb0 = (n0 << 16) | (n0 << 8) | n0;
unsigned int rgb1 = (n1 << 16) | (n1 << 8) | n1;
unsigned int rgb2 = (n2 << 16) | (n2 << 8) | n2;
// Visualize normals as RGB
rgb0 = normToRGB(anorms[v0->normalIndex]);
rgb1 = normToRGB(anorms[v1->normalIndex]);
rgb2 = normToRGB(anorms[v2->normalIndex]);
// Calculate some actual Lambert shading based on a static light vector
rgb0 = fakeLight(anorms[v0->normalIndex]);
rgb1 = fakeLight(anorms[v1->normalIndex]);
rgb2 = fakeLight(anorms[v2->normalIndex]);
// Normally you'd have GTE do this but we're just going for a quick hack now
unsigned short depth = ((unsigned short)v0->position[1] + (unsigned short)v1->position[1] + (unsigned short)v2->position[1]) / 3;
// Calculate some Lambert shading based on a static light vector
int lit0 = fakeLight(anorms[v0->normalIndex]);
int lit1 = fakeLight(anorms[v1->normalIndex]);
int lit2 = fakeLight(anorms[v2->normalIndex]);
// Since we have the texture split into two parts, we need to correct the UVs that are *not* on the seam, instead of the other way round // Since we have the texture split into two parts, we need to correct the UVs that are *not* on the seam, instead of the other way round
short u0 = !tri->frontFace && !tc0->onSeam ? tc0->u - (model->header->skinWidth >> 1) : tc0->u; short u0 = !tri->frontFace && !tc0->onSeam ? tc0->u - (model->header->skinWidth >> 1) : tc0->u;
short u1 = !tri->frontFace && !tc1->onSeam ? tc1->u - (model->header->skinWidth >> 1) : tc1->u; short u1 = !tri->frontFace && !tc1->onSeam ? tc1->u - (model->header->skinWidth >> 1) : tc1->u;
short u2 = !tri->frontFace && !tc2->onSeam ? tc2->u - (model->header->skinWidth >> 1) : tc2->u; short u2 = !tri->frontFace && !tc2->onSeam ? tc2->u - (model->header->skinWidth >> 1) : tc2->u;
addGouraudTexturedTriangle(
xOffset + v0->position[0], 240 - v0->position[2],
xOffset + v1->position[0], 240 - v1->position[2],
xOffset + v2->position[0], 240 - v2->position[2],
(unsigned char)depth,
rgb0, rgb1, rgb2,
u0, tc0->v, u1, tc1->v, u2, tc2->v, tri->frontFace ? frontTex : backTex);
TIM_IMAGE *tex = tri->frontFace ? frontTex : backTex;
short uoffs = (tex->prect->x % 64) << (2 - (tex->mode & 0x3));
short voffs = (tex->prect->y & 0xFF);
POLY_GT3 *poly = (POLY_GT3*)nextpri;
setPolyGT3(poly);
setXY3(poly, xOffset + v0->position[0], 240 - v0->position[2], xOffset + v1->position[0], 240 - v1->position[2], xOffset + v2->position[0], 240 - v2->position[2]);
setUV3(poly, uoffs + u0, voffs + tc0->v, uoffs + u1, voffs + tc1->v, uoffs + u2, voffs + tc2->v);
setRGB0(poly, lit0, lit0, lit0);
setRGB1(poly, lit1, lit1, lit1);
setRGB2(poly, lit2, lit2, lit2);
setClut(poly, tex->crect->x, tex->crect->y);
setTPage(poly, tex->mode & 0x3, 0, tex->prect->x, tex->prect->y);
addPrim(ot[db] + depth, poly);
nextpri += sizeof(POLY_GT3);
} }
} }

Loading…
Cancel
Save