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