DCDecomp
A decompilation of Dark Cloud for the PlayStation 2
Loading...
Searching...
No Matches
visualshadow.cpp
1#include "visualshadow.hpp"
2
3#include <libvu0.h>
4
5#include "character.hpp"
6#include "dataalloc.hpp"
7#include "dataset.hpp"
8#include "mathutil.hpp"
9#include "mdt.hpp"
10#include "mglib.hpp"
11#include "renderinfo.hpp"
12
13int CVisualShadow::DrawVu1(u_int *packet, float (*matrix)[4], RenderInfo *info, VU1_PROGRAM program, u_long128 *draw_state, int unknown1, int unknown2) {
14 int result;
15 u_int *saved_primary;
16 u_int *saved_secondary;
17 u_int saved_size;
18
19 result = 0;
20
21 if (info->shadow_pass == 2) {
22 saved_primary = vu_data_buffer[0];
23 saved_secondary = vu_data_buffer[1];
24 saved_size = vu_size;
25 ActiveData->Align64();
26 ActiveData->Alloc(CreateVUdataShadowCLIP((u_int *) (ActiveData->base + ActiveData->used * 16), data, info, matrix));
29 result += CVisualMDTVu1::DrawVu1(packet, matrix, info, program, draw_state, unknown1, unknown2);
30 vu_data_buffer[0] = saved_primary;
31 vu_data_buffer[1] = saved_secondary;
32 vu_size = saved_size;
33 return result;
34 }
35
36 result += CVisualMDTVu1::DrawVu1(packet, matrix, info, program, draw_state, unknown1, unknown2);
37 return result;
38}
39
40int CVisualShadow::DrawVu1(sceVif1Packet *packet, float (*matrix)[4], RenderInfo *info, VU1_PROGRAM program, u_long128 *draw_state, int unknown1, int unknown2) {
41 int result;
42 u_int *saved_primary;
43 u_int *saved_secondary;
44 u_int saved_size;
45
46 result = 0;
47
48 if (info->shadow_pass == 2) {
49 saved_primary = vu_data_buffer[0];
50 saved_secondary = vu_data_buffer[1];
51 saved_size = vu_size;
52 ActiveData->Align64();
53 ActiveData->Alloc(CreateVUdataShadowCLIP((u_int *) (ActiveData->base + ActiveData->used * 16), data, info, matrix));
56 result += CVisualMDTVu1::DrawVu1(packet, matrix, info, program, draw_state, unknown1, unknown2);
57 vu_data_buffer[0] = saved_primary;
58 vu_data_buffer[1] = saved_secondary;
59 vu_size = saved_size;
60 return result;
61 }
62
63 return CVisualMDTVu1::DrawVu1(packet, matrix, info, program, draw_state, unknown1, unknown2);
64}
65
66int CVisualShadow::CreateVUdataShadow(u_int *block, u_int *model_data) {
67 int first;
68 MDT_HEADER *model;
69 MDT_SHADOW *shadow;
70 int shape_index;
71 int word;
72 int unpack;
73 int qwc;
74 sceVu0FVECTOR *vertices;
75 int shape_num;
76 MDT_SVERTEX *corner;
77 int index_num;
78 int continued;
79 int emitted;
80 int triangle;
81 int tag;
82 MDT_SVERTEX *source;
83 u_long128 *out;
84
85 first = 1;
86 word = 0;
87 u_int end[4] = {0x11000000, 0, 0, 0};
88 vu_data = block;
89 model = (MDT_HEADER *) model_data;
90 shadow = (MDT_SHADOW *) ((u_char *) model + model->mesh_ofs);
91 vertices = (sceVu0FVECTOR *) ((u_char *) model + model->vertex_ofs);
92 corner = (MDT_SVERTEX *) shadow->shape;
93 shape_num = shadow->shape_num;
94
95 for (shape_index = 0; shape_index < shape_num; shape_index++) {
96 index_num = ((MDT_SSHAPE *) corner)->index_num;
97 corner = ((MDT_SSHAPE *) corner)->vertex;
98 continued = 0;
99 emitted = 0;
100 sceVu0FVECTOR one = {1.0f, 1.0f, 1.0f, 1.0f};
101
102 while (emitted < index_num) {
103 qwc = 0;
104 block[word] = 0;
105 block[(u_int) (word + 1)] = 0;
106 block[(u_int) (word + 2)] = 0;
107 unpack = word + 3;
108 block[unpack] = 0x6C008000;
109 tag = word + 4;
110 block[tag] = 0x8000;
111 block[(u_int) (word + 5)] = 0x2022C000;
112 block[(u_int) (word + 6)] = 0x41;
113 block[(u_int) (word + 7)] = 0;
114 word += 8;
115 qwc += 1;
116 source = corner;
117 out = (u_long128 *) &block[word];
118
119 for (triangle = 0; triangle < 10 && emitted < index_num; triangle++, emitted += 3) {
120 out[0] = *(u_long128 *) one;
121 out[1] = *(u_long128 *) one;
122 out[2] = *(u_long128 *) one;
123 out[3] = *(u_long128 *) vertices[source[0].index];
124 out[4] = *(u_long128 *) vertices[source[1].index];
125 out[5] = *(u_long128 *) vertices[source[2].index];
126 out += 6;
127 source += 3;
128 }
129
130 word += triangle * 24;
131 qwc += triangle * 6;
132 corner += triangle * 3;
133 block[(u_int) tag] |= triangle;
134 block[(u_int) unpack] |= qwc << 16;
135 block[word] = 0;
136 block[(u_int) (word + 1)] = 0;
137 block[(u_int) (word + 2)] = 0;
138
139 if (!continued) {
140 if (first) {
141 block[(u_int) (word + 3)] = 0x14000000;
142 word += 4;
143 first = 0;
144 } else {
145 block[(u_int) (word + 3)] = 0x14000001;
146 word += 4;
147 }
148
149 continued = 1;
150 } else {
151 block[(u_int) (word + 3)] = 0x17000000;
152 word += 4;
153 }
154 }
155
156 *(u_long128 *) &block[word] = *(u_long128 *) end;
157 word += 4;
158 }
159
160 vu_size = word >> 2;
161 return vu_size;
162}
163
164int CVisualShadow::RemakeData(u_int *block) {
165 if (data == NULL) {
166 return 0;
167 }
168
169 return CreateVUdataShadow(vu_data_buffer[DBuffID], data);
170}
171
172int CVisualShadow::CreateVUdataShadowCLIP(u_int *block, u_int *model_data, RenderInfo *info, float (*matrix)[4]) {
173 int word;
174 int qwc;
175 int index_a;
176 MDT_HEADER *model;
177 int first;
178 int shape_index;
179 int unpack;
180 float nx;
181 MDT_SHADOW *shadow;
182 int pass;
183 sceVu0FVECTOR *eye;
184 sceVu0FVECTOR *vertices;
185 float scale;
186 int shape_num;
187 float ny;
188 MDT_SVERTEX *corner;
189 float plane_scale;
190 float nz;
191 sceVu0FVECTOR *projected;
192 int index_num;
193 u_long128 *out;
194 int continued;
195 int emitted;
196 int triangle;
197 int count;
198 int tag;
199 int clip_num;
200 int index_c;
201 int offset_a;
202 int offset_b;
203 int index_b;
204 u_int i;
205 int offset_c;
206 sceVu0FMATRIX clip;
207 sceVu0FMATRIX near_shadow;
208 sceVu0FVECTOR local_light;
209 sceVu0FVECTOR light;
210 sceVu0FVECTOR eye_light;
211 sceVu0FMATRIX shadow_matrix;
212 sceVu0FMATRIX local_to_clip;
213 sceVu0FMATRIX transpose;
214 sceVu0FMATRIX local_to_eye;
215 sceVu0FMATRIX shadow_to_eye;
216 sceVu0FVECTOR point;
217 sceVu0FVECTOR normal;
218 sceVu0FVECTOR direction;
219 sceVu0FVECTOR near_light;
220
221 if (model_data == NULL) {
222 return 0;
223 }
224
225 light[0] = info->light_direction[0][0];
226 light[1] = info->light_direction[1][0];
227 light[2] = info->light_direction[2][0];
228 light[3] = 0.0f;
229 sceVu0TransposeMatrix(transpose, matrix);
230 sceVu0ApplyMatrix(local_light, transpose, light);
231 MulMatrix(local_to_eye, info->view_scaled, matrix);
232 sceVu0ApplyMatrix(eye_light, info->view_scaled, light);
233 MulMatrix(local_to_clip, info->perspective, local_to_eye);
234 sceVu0Normalize(eye_light, eye_light);
235 sceVu0CopyVector(point, info->shadow_point);
236 sceVu0CopyVector(normal, info->shadow_normal);
237 direction[0] = info->light_direction[0][0];
238 direction[1] = info->light_direction[1][0];
239 direction[2] = info->light_direction[2][0];
240 direction[3] = 0.0f;
241 plane_scale = sceVu0InnerProduct(normal, point);
242
243 if (plane_scale == 0.0f) {
244 point[0] -= 0.1f * normal[0];
245 point[1] -= 0.1f * normal[1];
246 point[2] -= 0.1f * normal[2];
247 plane_scale = 1.0f;
248 }
249
250 plane_scale = 1.0f / plane_scale;
251 nx = normal[0] * plane_scale;
252 ny = normal[1] * plane_scale;
253 nz = normal[2] * plane_scale;
254 sceVu0Normalize(direction, direction);
255 sceVu0DropShadowMatrix(shadow_matrix, direction, nx, ny, nz, 0);
256 MulMatrix(shadow_matrix, shadow_matrix, matrix);
257 MulMatrix(shadow_to_eye, info->view_scaled, shadow_matrix);
258 MulMatrix(shadow_matrix, info->perspective, shadow_to_eye);
259 MulMatrix(clip, info->view_scaled, matrix);
260 sceVu0FVECTOR near_normal = {0.0f, 0.0f, 1.0f, 0.0f};
261 sceVu0FVECTOR near_point = {0.0f, 0.0f, 0.0f, 0.0f};
262 near_point[2] = info->near[2];
263 scale = 1.0f / sceVu0InnerProduct(near_normal, near_point);
264 nx = near_normal[0] * scale;
265 ny = near_normal[1] * scale;
266 nz = near_normal[2] * scale;
267 sceVu0Normalize(near_light, eye_light);
268 sceVu0DropShadowMatrix(near_shadow, near_light, nx, ny, nz, 0);
269 sceVu0CopyMatrix(clip, info->perspective);
270 first = 1;
271 word = 0;
272 u_int end[4] = {0, 0, 0, 0x11000000};
273 vu_data = block;
274 model = (MDT_HEADER *) model_data;
275 shadow = (MDT_SHADOW *) ((u_char *) model + model->mesh_ofs);
276 vertices = (sceVu0FVECTOR *) ((u_char *) model + model->vertex_ofs);
277 shape_num = shadow->shape_num;
278 eye = (sceVu0FVECTOR *) 0x70000000;
279 projected = &eye[model->vertex_num];
280
281 for (i = 0; i < model->vertex_num; i++) {
282 sceVu0ApplyMatrix((float *) eye[i], local_to_eye, vertices[i]);
283 sceVu0ApplyMatrix(projected[i], shadow_to_eye, vertices[i]);
284 }
285
286 for (pass = 0; pass < 2; pass++) {
287 block[word] = 0;
288 block[(u_int) (word + 1)] = 0;
289 block[(u_int) (word + 2)] = 0;
290 block[(u_int) (word + 3)] = 0x50000002;
291 block[(u_int) (word + 4)] = 0x8001;
292 block[(u_int) (word + 5)] = 0x102E8000;
293 block[(u_int) (word + 6)] = 0xE;
294 block[(u_int) (word + 7)] = 0;
295
296 if (pass == 0) {
297 block[(u_int) (word + 8)] = 0x68;
298 block[(u_int) (word + 9)] = 0x80;
299 block[(u_int) (word + 10)] = 0x42;
300 block[(u_int) (word + 11)] = 0;
301 word += 12;
302 } else {
303 block[(u_int) (word + 8)] = 0x62;
304 block[(u_int) (word + 9)] = 0x80;
305 block[(u_int) (word + 10)] = 0x42;
306 block[(u_int) (word + 11)] = 0;
307 word += 12;
308 }
309
310 corner = (MDT_SVERTEX *) shadow->shape;
311
312 for (shape_index = 0; shape_index < shape_num; shape_index++) {
313 index_num = ((MDT_SSHAPE *) corner)->index_num;
314 corner = ((MDT_SSHAPE *) corner)->vertex;
315 continued = 0;
316 emitted = 0;
317 u_int edges[4] = {0, 0, 0, 0};
318
319 while (emitted < index_num) {
320 qwc = 0;
321 block[word] = 0;
322 block[(u_int) (word + 1)] = 0;
323 block[(u_int) (word + 2)] = 0;
324 unpack = word + 3;
325 block[unpack] = 0x6C008000;
326 tag = word + 4;
327 block[tag] = 0x8000;
328 block[(u_int) (word + 5)] = 0x2022C000;
329 block[(u_int) (word + 6)] = 0x41;
330 block[(u_int) (word + 7)] = 0;
331 block[(u_int) (word + 8)] = pass != 0;
332 block[(u_int) (word + 9)] = 0;
333 block[(u_int) (word + 10)] = 0;
334 block[(u_int) (word + 11)] = 0;
335 word += 12;
336 qwc += 2;
337 out = (u_long128 *) &block[word];
338 triangle = 0;
339
340 for (count = 0; count < 12 && emitted < index_num; count++, emitted += 3, corner += 3) {
341 sceVu0FVECTOR face;
342 sceVu0FVECTOR edge0;
343 sceVu0FVECTOR edge1;
344 sceVu0FVECTOR near_side[4];
345 sceVu0FVECTOR clipped[5];
346 sceVu0FVECTOR far_side[4];
347 sceVu0FVECTOR side0;
348 sceVu0FVECTOR side1;
349
350 index_a = corner[0].index;
351 index_b = corner[1].index;
352 index_c = corner[2].index;
353 edges[0] = corner[0].edge;
354 edges[1] = corner[1].edge;
355 edges[2] = corner[2].edge;
356 // Each corner's byte offset is shared by the vertex, eye and projected arrays.
357 offset_a = index_a * sizeof(sceVu0FVECTOR);
358 out[1] = *(u_long128 *) ((u_char *) vertices + offset_a);
359 offset_b = index_b * sizeof(sceVu0FVECTOR);
360 out[2] = *(u_long128 *) ((u_char *) vertices + offset_b);
361 offset_c = index_c * sizeof(sceVu0FVECTOR);
362 out[3] = *(u_long128 *) ((u_char *) vertices + offset_c);
363 sceVu0SubVector(edge0, (float *) &out[2], (float *) &out[1]);
364 sceVu0SubVector(edge1, (float *) &out[3], (float *) &out[1]);
365 sceVu0OuterProduct(face, edge0, edge1);
366
367 if (sceVu0InnerProduct(face, local_light) <= 0.0f) {
368 out[4] = *(u_long128 *) vertices[index_c];
369 triangle++;
370 out[0] = *(u_long128 *) edges;
371 *(u_long128 *) near_side[0] = *(u_long128 *) ((u_char *) eye + offset_a);
372 *(u_long128 *) near_side[1] = *(u_long128 *) ((u_char *) eye + offset_b);
373 *(u_long128 *) near_side[2] = *(u_long128 *) ((u_char *) eye + offset_c);
374 *(u_long128 *) near_side[3] = *(u_long128 *) ((u_char *) eye + offset_a);
375 *(u_long128 *) far_side[0] = *(u_long128 *) ((u_char *) projected + offset_a);
376 *(u_long128 *) far_side[1] = *(u_long128 *) ((u_char *) projected + offset_b);
377 *(u_long128 *) far_side[2] = *(u_long128 *) ((u_char *) projected + offset_c);
378 *(u_long128 *) far_side[3] = *(u_long128 *) ((u_char *) projected + offset_a);
379 clip_num = scissior(clipped, near_side, far_side, info->near[2]);
380 sceVu0SubVector(side0, clipped[1], clipped[0]);
381 sceVu0SubVector(side1, clipped[2], clipped[0]);
382 sceVu0ApplyMatrix(near_side[0], clip, (float *) clipped[0]);
383 sceVu0ApplyMatrix(near_side[1], clip, clipped[1]);
384 sceVu0ApplyMatrix(near_side[2], clip, clipped[2]);
385 sceVu0ApplyMatrix(near_side[3], clip, (float *) clipped[3]);
386 sceVu0ApplyMatrix(clipped[0], clip, (float *) clipped[4]);
387 int counts[4] = {0, 0, 0, 1};
388 counts[0] = clip_num;
389
390 if (clip_num > 4) {
391 counts[0] = 4;
392 counts[1] = 3;
393 }
394
395 if (pass != 0) {
396 counts[0] = 0;
397 counts[1] = 0;
398 }
399
400 out[5] = *(u_long128 *) counts;
401
402 if (pass == 0) {
403 out[6] = *(u_long128 *) clipped[0];
404 out[7] = *(u_long128 *) near_side[3];
405 out[8] = *(u_long128 *) near_side[2];
406 out[9] = *(u_long128 *) near_side[1];
407 out[10] = *(u_long128 *) near_side[0];
408 out += 11;
409 } else {
410 out[6] = *(u_long128 *) near_side[0];
411 out[7] = *(u_long128 *) near_side[0];
412 out[8] = *(u_long128 *) near_side[0];
413 out[9] = *(u_long128 *) near_side[0];
414 out[10] = *(u_long128 *) near_side[0];
415 out += 11;
416 }
417 }
418 }
419
420 if (triangle > 0) {
421 word += triangle * 44;
422 qwc += triangle * 11;
423 block[(u_int) tag] |= triangle;
424 block[(u_int) unpack] |= qwc << 16;
425 block[word] = 0;
426 block[(u_int) (word + 1)] = 0;
427 block[(u_int) (word + 2)] = 0;
428
429 if (!continued) {
430 if (first) {
431 block[(u_int) (word + 3)] = 0x14000000;
432 word += 4;
433 first = 0;
434 } else {
435 block[(u_int) (word + 3)] = 0x14000001;
436 word += 4;
437 }
438
439 continued = 1;
440 } else {
441 block[(u_int) (word + 3)] = 0x17000000;
442 word += 4;
443 }
444 } else {
445 word -= 12;
446 }
447 }
448
449 *(u_long128 *) &block[word] = *(u_long128 *) end;
450 word += 4;
451 }
452 }
453
454 vu_size = word >> 2;
455 return vu_size;
456}
virtual int DrawVu1(u_int *packet, float(*matrix)[4], RenderInfo *info, VU1_PROGRAM program, u_long128 *draw_state, int unknown1, int unknown2)
u_int * data
Definition visual.hpp:145
u_int * vu_data_buffer[2]
Definition visual.hpp:147
virtual int DrawVu1(u_int *packet, float(*matrix)[4], RenderInfo *info, VU1_PROGRAM program, u_long128 *draw_state, int unknown1, int unknown2)
virtual int RemakeData(u_int *block)
int CreateVUdataShadowCLIP(u_int *block, u_int *model_data, RenderInfo *info, float(*matrix)[4])
int CreateVUdataShadow(u_int *block, u_int *model_data)
u_int * vu_data
Definition visual.hpp:74
u_int vu_size
Definition visual.hpp:75
int vertex_ofs
Definition mdt.hpp:14
int mesh_ofs
Definition mdt.hpp:19
int vertex_num
Definition mdt.hpp:13
MDT_SSHAPE shape[1]
Definition mdt.hpp:87
int shape_num
Definition mdt.hpp:85
int index
Definition mdt.hpp:65
int edge
Definition mdt.hpp:67
sceVu0FMATRIX light_direction
sceVu0FMATRIX perspective
sceVu0FVECTOR near
sceVu0FVECTOR shadow_point
sceVu0FMATRIX view_scaled
sceVu0FVECTOR shadow_normal