Path: blob/master/RSDKv5/RSDK/Graphics/Legacy/v3/Scene3DLegacyv3.cpp
1170 views
1RSDK::Legacy::Face RSDK::Legacy::v3::faceBuffer[LEGACY_v3_FACEBUFFER_SIZE];2RSDK::Legacy::Vertex RSDK::Legacy::v3::vertexBuffer[LEGACY_v3_VERTEXBUFFER_SIZE];3RSDK::Legacy::Vertex RSDK::Legacy::v3::vertexBufferT[LEGACY_v3_VERTEXBUFFER_SIZE];45RSDK::Legacy::DrawListEntry3D RSDK::Legacy::v3::drawList3D[LEGACY_v3_FACEBUFFER_SIZE];67void RSDK::Legacy::v3::SetIdentityMatrix(Matrix *matrix)8{9matrix->values[0][0] = 0x100;10matrix->values[0][1] = 0;11matrix->values[0][2] = 0;12matrix->values[0][3] = 0;13matrix->values[1][0] = 0;14matrix->values[1][1] = 0x100;15matrix->values[1][2] = 0;16matrix->values[1][3] = 0;17matrix->values[2][0] = 0;18matrix->values[2][1] = 0;19matrix->values[2][2] = 0x100;20matrix->values[2][3] = 0;21matrix->values[3][0] = 0;22matrix->values[3][0] = 0;23matrix->values[3][1] = 0;24matrix->values[3][2] = 0;25matrix->values[3][3] = 0x100;26}27void RSDK::Legacy::v3::MatrixMultiply(Matrix *matrixA, Matrix *matrixB)28{29int32 output[16];3031for (int32 i = 0; i < 0x10; ++i) {32uint32 RowB = i & 3;33uint32 RowA = (i & 0xC) / 4;34output[i] = (matrixA->values[RowA][3] * matrixB->values[3][RowB] >> 8) + (matrixA->values[RowA][2] * matrixB->values[2][RowB] >> 8)35+ (matrixA->values[RowA][1] * matrixB->values[1][RowB] >> 8) + (matrixA->values[RowA][0] * matrixB->values[0][RowB] >> 8);36}3738for (int32 i = 0; i < 0x10; ++i) matrixA->values[i / 4][i % 4] = output[i];39}40void RSDK::Legacy::v3::MatrixTranslateXYZ(Matrix *matrix, int32 x, int32 y, int32 z)41{42matrix->values[0][0] = 0x100;43matrix->values[0][1] = 0;44matrix->values[0][2] = 0;45matrix->values[0][3] = 0;46matrix->values[1][0] = 0;47matrix->values[1][1] = 0x100;48matrix->values[1][2] = 0;49matrix->values[1][3] = 0;50matrix->values[2][0] = 0;51matrix->values[2][1] = 0;52matrix->values[2][2] = 0x100;53matrix->values[2][3] = 0;54matrix->values[3][0] = x;55matrix->values[3][1] = y;56matrix->values[3][2] = z;57matrix->values[3][3] = 0x100;58}59void RSDK::Legacy::v3::MatrixScaleXYZ(Matrix *matrix, int32 scaleX, int32 scaleY, int32 scaleZ)60{61matrix->values[0][0] = scaleX;62matrix->values[0][1] = 0;63matrix->values[0][2] = 0;64matrix->values[0][3] = 0;65matrix->values[1][0] = 0;66matrix->values[1][1] = scaleY;67matrix->values[1][2] = 0;68matrix->values[1][3] = 0;69matrix->values[2][0] = 0;70matrix->values[2][1] = 0;71matrix->values[2][2] = scaleZ;72matrix->values[2][3] = 0;73matrix->values[3][0] = 0;74matrix->values[3][1] = 0;75matrix->values[3][2] = 0;76matrix->values[3][3] = 0x100;77}78void RSDK::Legacy::v3::MatrixRotateX(Matrix *matrix, int32 rotationX)79{80if (rotationX < 0)81rotationX = 0x200 - rotationX;82rotationX &= 0x1FF;83int32 sine = sin512LookupTable[rotationX] >> 1;84int32 cosine = cos512LookupTable[rotationX] >> 1;85matrix->values[0][0] = 0x100;86matrix->values[0][1] = 0;87matrix->values[0][2] = 0;88matrix->values[0][3] = 0;89matrix->values[1][0] = 0;90matrix->values[1][1] = cosine;91matrix->values[1][2] = sine;92matrix->values[1][3] = 0;93matrix->values[2][0] = 0;94matrix->values[2][1] = -sine;95matrix->values[2][2] = cosine;96matrix->values[2][3] = 0;97matrix->values[3][0] = 0;98matrix->values[3][1] = 0;99matrix->values[3][2] = 0;100matrix->values[3][3] = 0x100;101}102void RSDK::Legacy::v3::MatrixRotateY(Matrix *matrix, int32 rotationY)103{104if (rotationY < 0)105rotationY = 0x200 - rotationY;106rotationY &= 0x1FF;107int32 sine = sin512LookupTable[rotationY] >> 1;108int32 cosine = cos512LookupTable[rotationY] >> 1;109matrix->values[0][0] = cosine;110matrix->values[0][1] = 0;111matrix->values[0][2] = sine;112matrix->values[0][3] = 0;113matrix->values[1][0] = 0;114matrix->values[1][1] = 0x100;115matrix->values[1][2] = 0;116matrix->values[1][3] = 0;117matrix->values[2][0] = -sine;118matrix->values[2][1] = 0;119matrix->values[2][2] = cosine;120matrix->values[2][3] = 0;121matrix->values[3][0] = 0;122matrix->values[3][1] = 0;123matrix->values[3][2] = 0;124matrix->values[3][3] = 0x100;125}126void RSDK::Legacy::v3::MatrixRotateZ(Matrix *matrix, int32 rotationZ)127{128if (rotationZ < 0)129rotationZ = 0x200 - rotationZ;130rotationZ &= 0x1FF;131int32 sine = sin512LookupTable[rotationZ] >> 1;132int32 cosine = cos512LookupTable[rotationZ] >> 1;133matrix->values[0][0] = cosine;134matrix->values[0][1] = 0;135matrix->values[0][2] = sine;136matrix->values[0][3] = 0;137matrix->values[1][0] = 0;138matrix->values[1][1] = 0x100;139matrix->values[1][2] = 0;140matrix->values[1][3] = 0;141matrix->values[2][0] = -sine;142matrix->values[2][1] = 0;143matrix->values[2][2] = cosine;144matrix->values[2][3] = 0;145matrix->values[3][0] = 0;146matrix->values[3][1] = 0;147matrix->values[3][2] = 0;148matrix->values[3][3] = 0x100;149}150void RSDK::Legacy::v3::MatrixRotateXYZ(Matrix *matrix, int32 rotationX, int32 rotationY, int32 rotationZ)151{152if (rotationX < 0)153rotationX = 0x200 - rotationX;154rotationX &= 0x1FF;155if (rotationY < 0)156rotationY = 0x200 - rotationY;157rotationY &= 0x1FF;158if (rotationZ < 0)159rotationZ = 0x200 - rotationZ;160rotationZ &= 0x1FF;161int32 sineX = sin512LookupTable[rotationX] >> 1;162int32 cosineX = cos512LookupTable[rotationX] >> 1;163int32 sineY = sin512LookupTable[rotationY] >> 1;164int32 cosineY = cos512LookupTable[rotationY] >> 1;165int32 sineZ = sin512LookupTable[rotationZ] >> 1;166int32 cosineZ = cos512LookupTable[rotationZ] >> 1;167168matrix->values[0][0] = (sineZ * (sineY * sineX >> 8) >> 8) + (cosineZ * cosineY >> 8);169matrix->values[0][1] = (sineZ * cosineY >> 8) - (cosineZ * (sineY * sineX >> 8) >> 8);170matrix->values[0][2] = sineY * cosineX >> 8;171matrix->values[0][3] = 0;172matrix->values[1][0] = sineZ * -cosineX >> 8;173matrix->values[1][1] = cosineZ * cosineX >> 8;174matrix->values[1][2] = sineX;175matrix->values[1][3] = 0;176matrix->values[2][0] = (sineZ * (cosineY * sineX >> 8) >> 8) - (cosineZ * sineY >> 8);177matrix->values[2][1] = (sineZ * -sineY >> 8) - (cosineZ * (cosineY * sineX >> 8) >> 8);178matrix->values[2][2] = cosineY * cosineX >> 8;179matrix->values[2][3] = 0;180matrix->values[3][0] = 0;181matrix->values[3][1] = 0;182matrix->values[3][2] = 0;183matrix->values[3][3] = 0x100;184}185void RSDK::Legacy::v3::TransformVertexBuffer()186{187for (int32 y = 0; y < 4; ++y) {188for (int32 x = 0; x < 4; ++x) {189matFinal.values[y][x] = matWorld.values[y][x];190}191}192MatrixMultiply(&matFinal, &matView);193194if (vertexCount <= 0)195return;196197int32 inVertexID = 0;198int32 outVertexID = 0;199do {200int32 vx = vertexBuffer[inVertexID].x;201int32 vy = vertexBuffer[inVertexID].y;202int32 vz = vertexBuffer[inVertexID].z;203Vertex *vert = &vertexBufferT[inVertexID++];204205vert->x = (vx * matFinal.values[0][0] >> 8) + (vy * matFinal.values[1][0] >> 8) + (vz * matFinal.values[2][0] >> 8) + matFinal.values[3][0];206vert->y = (vx * matFinal.values[0][1] >> 8) + (vy * matFinal.values[1][1] >> 8) + (vz * matFinal.values[2][1] >> 8) + matFinal.values[3][1];207vert->z = (vx * matFinal.values[0][2] >> 8) + (vy * matFinal.values[1][2] >> 8) + (vz * matFinal.values[2][2] >> 8) + matFinal.values[3][2];208} while (++outVertexID != vertexCount);209}210void RSDK::Legacy::v3::TransformVerticies(Matrix *matrix, int32 startIndex, int32 endIndex)211{212if (startIndex > endIndex)213return;214215do {216int32 vx = vertexBuffer[startIndex].x;217int32 vy = vertexBuffer[startIndex].y;218int32 vz = vertexBuffer[startIndex].z;219Vertex *vert = &vertexBuffer[startIndex];220vert->x = (vx * matrix->values[0][0] >> 8) + (vy * matrix->values[1][0] >> 8) + (vz * matrix->values[2][0] >> 8) + matrix->values[3][0];221vert->y = (vx * matrix->values[0][1] >> 8) + (vy * matrix->values[1][1] >> 8) + (vz * matrix->values[2][1] >> 8) + matrix->values[3][1];222vert->z = (vx * matrix->values[0][2] >> 8) + (vy * matrix->values[1][2] >> 8) + (vz * matrix->values[2][2] >> 8) + matrix->values[3][2];223} while (++startIndex < endIndex);224}225void RSDK::Legacy::v3::Sort3DDrawList()226{227for (int32 i = 0; i < faceCount; ++i) {228drawList3D[i].depth = (vertexBufferT[faceBuffer[i].d].z + vertexBufferT[faceBuffer[i].c].z + vertexBufferT[faceBuffer[i].b].z229+ vertexBufferT[faceBuffer[i].a].z)230>> 2;231drawList3D[i].faceID = i;232}233234for (int32 i = 0; i < faceCount; ++i) {235for (int32 j = faceCount - 1; j > i; --j) {236if (drawList3D[j].depth > drawList3D[j - 1].depth) {237int32 faceID = drawList3D[j].faceID;238int32 depth = drawList3D[j].depth;239drawList3D[j].faceID = drawList3D[j - 1].faceID;240drawList3D[j].depth = drawList3D[j - 1].depth;241drawList3D[j - 1].faceID = faceID;242drawList3D[j - 1].depth = depth;243}244}245}246}247void RSDK::Legacy::v3::Draw3DScene(int32 spriteSheetID)248{249Vertex quad[4];250for (int32 i = 0; i < faceCount; ++i) {251Face *face = &faceBuffer[drawList3D[i].faceID];252memset(quad, 0, 4 * sizeof(Vertex));253254switch (face->flag) {255default: break;256257case FACE_FLAG_TEXTURED_3D:258if (vertexBufferT[face->a].z > 0x100 && vertexBufferT[face->b].z > 0x100 && vertexBufferT[face->c].z > 0x100259&& vertexBufferT[face->d].z > 0x100) {260quad[0].x = SCREEN_CENTERX + projectionX * vertexBufferT[face->a].x / vertexBufferT[face->a].z;261quad[0].y = SCREEN_CENTERY - projectionY * vertexBufferT[face->a].y / vertexBufferT[face->a].z;262quad[1].x = SCREEN_CENTERX + projectionX * vertexBufferT[face->b].x / vertexBufferT[face->b].z;263quad[1].y = SCREEN_CENTERY - projectionY * vertexBufferT[face->b].y / vertexBufferT[face->b].z;264quad[2].x = SCREEN_CENTERX + projectionX * vertexBufferT[face->c].x / vertexBufferT[face->c].z;265quad[2].y = SCREEN_CENTERY - projectionY * vertexBufferT[face->c].y / vertexBufferT[face->c].z;266quad[3].x = SCREEN_CENTERX + projectionX * vertexBufferT[face->d].x / vertexBufferT[face->d].z;267quad[3].y = SCREEN_CENTERY - projectionY * vertexBufferT[face->d].y / vertexBufferT[face->d].z;268quad[0].u = vertexBuffer[face->a].u;269quad[0].v = vertexBuffer[face->a].v;270quad[1].u = vertexBuffer[face->b].u;271quad[1].v = vertexBuffer[face->b].v;272quad[2].u = vertexBuffer[face->c].u;273quad[2].v = vertexBuffer[face->c].v;274quad[3].u = vertexBuffer[face->d].u;275quad[3].v = vertexBuffer[face->d].v;276DrawTexturedFace(quad, spriteSheetID);277}278break;279280case FACE_FLAG_TEXTURED_2D:281quad[0].x = vertexBuffer[face->a].x;282quad[0].y = vertexBuffer[face->a].y;283quad[1].x = vertexBuffer[face->b].x;284quad[1].y = vertexBuffer[face->b].y;285quad[2].x = vertexBuffer[face->c].x;286quad[2].y = vertexBuffer[face->c].y;287quad[3].x = vertexBuffer[face->d].x;288quad[3].y = vertexBuffer[face->d].y;289quad[0].u = vertexBuffer[face->a].u;290quad[0].v = vertexBuffer[face->a].v;291quad[1].u = vertexBuffer[face->b].u;292quad[1].v = vertexBuffer[face->b].v;293quad[2].u = vertexBuffer[face->c].u;294quad[2].v = vertexBuffer[face->c].v;295quad[3].u = vertexBuffer[face->d].u;296quad[3].v = vertexBuffer[face->d].v;297DrawTexturedFace(quad, spriteSheetID);298break;299300case FACE_FLAG_COLORED_3D:301if (vertexBufferT[face->a].z > 0x100 && vertexBufferT[face->b].z > 0x100 && vertexBufferT[face->c].z > 0x100302&& vertexBufferT[face->d].z > 0x100) {303quad[0].x = SCREEN_CENTERX + projectionX * vertexBufferT[face->a].x / vertexBufferT[face->a].z;304quad[0].y = SCREEN_CENTERY - projectionY * vertexBufferT[face->a].y / vertexBufferT[face->a].z;305quad[1].x = SCREEN_CENTERX + projectionX * vertexBufferT[face->b].x / vertexBufferT[face->b].z;306quad[1].y = SCREEN_CENTERY - projectionY * vertexBufferT[face->b].y / vertexBufferT[face->b].z;307quad[2].x = SCREEN_CENTERX + projectionX * vertexBufferT[face->c].x / vertexBufferT[face->c].z;308quad[2].y = SCREEN_CENTERY - projectionY * vertexBufferT[face->c].y / vertexBufferT[face->c].z;309quad[3].x = SCREEN_CENTERX + projectionX * vertexBufferT[face->d].x / vertexBufferT[face->d].z;310quad[3].y = SCREEN_CENTERY - projectionY * vertexBufferT[face->d].y / vertexBufferT[face->d].z;311DrawFace(quad, face->color);312}313break;314315case FACE_FLAG_COLORED_2D:316quad[0].x = vertexBuffer[face->a].x;317quad[0].y = vertexBuffer[face->a].y;318quad[1].x = vertexBuffer[face->b].x;319quad[1].y = vertexBuffer[face->b].y;320quad[2].x = vertexBuffer[face->c].x;321quad[2].y = vertexBuffer[face->c].y;322quad[3].x = vertexBuffer[face->d].x;323quad[3].y = vertexBuffer[face->d].y;324DrawFace(quad, face->color);325break;326}327}328}329330331