DCDecomp
A decompilation of Dark Cloud for the PlayStation 2
Loading...
Searching...
No Matches
collisionmdt.cpp
1#pragma helper_mask_gpr 0x30
2#pragma helper_mask_fpr 0x1000
3
4#include "collisionmdt.hpp"
5
6#include <libvu0.h>
7
8#include "mathutil.hpp"
9#ifndef PORT
10/* The extent of three points, which is the bound of one triangle. The unit answers both extremes
11 from one pass over the pair, so this costs less than the two the library would need. The
12 pointers are register variables because that is how an assembly block reaches a value the
13 compiler is holding. */
14static void vu_maxmin3(float *max, float *min, float *a, float *b, float *c) {
15 register float *out_max = max;
16 register float *out_min = min;
17 register float *p0 = a;
18 register float *p1 = b;
19 register float *p2 = c;
20
21 asm {
22 lqc2 vf15, 0(p0)
23 lqc2 vf16, 0(p1)
24 lqc2 vf17, 0(p2)
25 vmax vf18, vf15, vf16
26 vmini vf20, vf15, vf16
27 vmax vf19, vf18, vf17
28 vmini vf21, vf20, vf17
29 sqc2 vf19, 0(out_max)
30 sqc2 vf21, 0(out_min)
31 }
32}
33
34/* The plane normal of a triangle, as the outer product of two of its edges. */
35static void vu_normal(float *normal, float *a, float *b, float *c) {
36 register float *out = normal;
37 register float *p0 = a;
38 register float *p1 = b;
39 register float *p2 = c;
40
41 asm {
42 lqc2 vf15, 0(p0)
43 lqc2 vf16, 0(p1)
44 lqc2 vf17, 0(p2)
45 vsub vf10, vf16, vf15
46 vsub vf11, vf17, vf15
47 vopmula ACC, vf10, vf11
48 vopmsub vf12, vf11, vf10
49 sqc2 vf12, 0(out)
50 }
51}
52
53/* The query bound of a box search, parked in the unit's registers once instead of being reloaded
54 per triangle. */
55static inline void vu_hold_box(float *max, float *min) {
56 register float *p0 = max;
57 register float *p1 = min;
58
59 asm {
60 lqc2 vf10, 0(p0)
61 lqc2 vf11, 0(p1)
62 }
63}
64
65/* Whether a triangle's own bound misses the one held above, as the two subtractions that would
66 both have to come out positive for the two to overlap. Neither result is wanted — the answer is
67 the unit's sticky sign and zero flags, cleared before the pair and read back after them, which
68 is why the status register rather than a register file entry is what the block hands back. */
69static inline int vu_box_missed(float *max, float *min) {
70 register float *p0 = max;
71 register float *p1 = min;
72 register int status;
73
74 asm {
75 lqc2 vf12, 0(p0)
76 lqc2 vf13, 0(p1)
77 vnop
78 vnop
79 vnop
80 ctc2 $0, $vi16
81 vsub.xyz vf25, vf10, vf13
82 vsub.xyz vf25, vf12, vf11
83 vnop
84 vnop
85 vnop
86 vnop
87 vnop
88 cfc2 status, $vi16
89 }
90
91 return status & 0xc0;
92}
93#endif
94/* The bound of whatever the shape is made of, which the base can build because the vertices are
95 the one thing every shape answers. A shape with no vertices leaves the bound all zero. */
96void CCollision::CreateBBox() {
97 int i;
98 int vertex_count;
99 sceVu0FVECTOR *vertex;
100
101 vertex = GetVertexAddress(&vertex_count);
102
103 if (vertex == 0) {
104 max[0] = min[0] = 0.0f;
105 max[1] = min[1] = 0.0f;
106 max[2] = min[2] = 0.0f;
107 return;
108 }
109
110 max[0] = vertex[0][0];
111 max[1] = vertex[0][1];
112 max[2] = vertex[0][2];
113 min[0] = vertex[0][0];
114 min[1] = vertex[0][1];
115 min[2] = vertex[0][2];
116
117 for (i = 0; i < vertex_count; i++) {
118 if (max[0] < vertex[0][0]) {
119 max[0] = vertex[0][0];
120 }
121
122 if (max[1] < vertex[0][1]) {
123 max[1] = vertex[0][1];
124 }
125
126 if (max[2] < vertex[0][2]) {
127 max[2] = vertex[0][2];
128 }
129
130 if (min[0] > vertex[0][0]) {
131 min[0] = vertex[0][0];
132 }
133
134 if (min[1] > vertex[0][1]) {
135 min[1] = vertex[0][1];
136 }
137
138 if (min[2] > vertex[0][2]) {
139 min[2] = vertex[0][2];
140 }
141
142 vertex++;
143 }
144}
145
146sceVu0FVECTOR *CCollision::GetVertexAddress(int *count) {
147 return 0;
148}
149
150int CCollisionMDT::GetPolygon(int index, sceVu0FMATRIX v0, sceVu0FMATRIX v1, sceVu0FMATRIX v2) {
151 MDT_COLLISION *collision;
152
153 if (model == 0) {
154 return 0;
155 }
156
157 collision = (MDT_COLLISION *) ((char *) model + model->mesh_ofs);
158
159 if (index >= collision->set.num) {
160 return 0;
161 }
162
163 return 1;
164}
165
166/* The height of the mesh under a point, as the highest triangle a vertical line meets. The line is
167 spelled from y zero to y one and the intersection treats it as infinite, so nothing bounds the
168 search in y. */
169int CCollisionMDT::GetMaxY(float *position) {
170 sceVu0FVECTOR v0;
171 sceVu0FVECTOR v1;
172 sceVu0FVECTOR v2;
173 sceVu0FVECTOR from;
174 sceVu0FVECTOR to;
175 sceVu0FVECTOR hit;
176 sceVu0FVECTOR normal;
177 sceVu0FVECTOR tri_max;
178 sceVu0FVECTOR tri_min;
179 int i;
180 int found;
181 sceVu0FVECTOR *vertex;
182 MDT_CPOLY_SET *set;
183 MDT_CPOLY *poly;
184 float top;
185
186 found = 0;
187
188 if (model == 0) {
189 return 0;
190 }
191
192 /* An integer floor, converted rather than written as a float constant: the game spells it that
193 way and the conversion is in the instruction stream. */
194 top = -100000000;
195
196 from[0] = to[0] = position[0];
197 from[2] = to[2] = position[2];
198 from[1] = 0.0f;
199 to[1] = 1.0f;
200
201 vertex = (sceVu0FVECTOR *) ((char *) model + model->vertex_ofs);
202 set = &((MDT_COLLISION *) ((char *) model + model->mesh_ofs))->set;
203 poly = set->poly;
204
205 for (i = 0; i < set->num; i++) {
206 *(u_long128 *) v0 = *(u_long128 *) vertex[poly->vertex[0]];
207 *(u_long128 *) v1 = *(u_long128 *) vertex[poly->vertex[1]];
208 *(u_long128 *) v2 = *(u_long128 *) vertex[poly->vertex[2]];
209 poly++;
210
211 vu_maxmin3(tri_max, tri_min, v0, v1, v2);
212
213 if (position[0] < tri_min[0] || position[0] > tri_max[0]) {
214 continue;
215 }
216
217 if (position[2] < tri_min[2] || position[2] > tri_max[2]) {
218 continue;
219 }
220
221 vu_normal(normal, v0, v1, v2);
222
223 if (IntersectionPoint_line_poly3(from, to, v0, v1, v2, normal, hit) == 0) {
224 continue;
225 }
226
227 found = 1;
228
229 if (top < hit[1]) {
230 top = hit[1];
231 }
232 }
233
234 if (found != 0) {
235 position[1] = top;
236 }
237
238 return found;
239}
240
241/* The nearest point at which a line meets the mesh. Each triangle is rejected first by its own
242 bound and then by the plane it lies in, and only what survives both is intersected. */
243int CCollisionMDT::Intersection(float *from, float *to, float *hit) {
244 sceVu0FVECTOR v0;
245 sceVu0FVECTOR v1;
246 sceVu0FVECTOR v2;
247 sceVu0FVECTOR point;
248 sceVu0FVECTOR edge0;
249 sceVu0FVECTOR edge1;
250 sceVu0FVECTOR normal;
251 sceVu0FVECTOR tri_max;
252 sceVu0FVECTOR tri_min;
253 int i;
254 int found;
255 sceVu0FVECTOR *vertex;
256 MDT_CPOLY_SET *set;
257 MDT_CPOLY *poly;
258 float plane_dist;
259 float from_dist;
260 float to_dist;
261 float best_distance;
262 float distance;
263 float dx;
264 float dy;
265 float dz;
266
267 found = 0;
268
269 vertex = (sceVu0FVECTOR *) ((char *) model + model->vertex_ofs);
270 set = &((MDT_COLLISION *) ((char *) model + model->mesh_ofs))->set;
271 poly = set->poly;
272
273 for (i = 0; i < set->num; i++) {
274 *(u_long128 *) v0 = *(u_long128 *) vertex[poly->vertex[0]];
275 *(u_long128 *) v1 = *(u_long128 *) vertex[poly->vertex[1]];
276 *(u_long128 *) v2 = *(u_long128 *) vertex[poly->vertex[2]];
277 poly++;
278
279 vu_maxmin3(tri_max, tri_min, v0, v1, v2);
280
281 if (from[0] < tri_min[0] && to[0] < tri_min[0]) {
282 continue;
283 }
284
285 if (from[0] > tri_max[0] && to[0] > tri_max[0]) {
286 continue;
287 }
288
289 /* The far endpoint is held against the x extent in y and not the y one. That is what the
290 game does and what its bytes depend on, so it is what this does; the effect is a reject
291 that keeps more triangles than it needs to, and nothing below trusts it on its own. */
292 if (from[1] < tri_min[1] && to[1] < tri_min[0]) {
293 continue;
294 }
295
296 if (from[1] > tri_max[1] && to[1] > tri_max[0]) {
297 continue;
298 }
299
300 if (from[2] < tri_min[2] && to[2] < tri_min[2]) {
301 continue;
302 }
303
304 if (from[2] > tri_max[2] && to[2] > tri_max[2]) {
305 continue;
306 }
307
308 edge0[0] = v1[0] - v0[0];
309 edge0[1] = v1[1] - v0[1];
310 edge0[2] = v1[2] - v0[2];
311 edge0[3] = 0.0f;
312 edge1[0] = v2[0] - v0[0];
313 edge1[1] = v2[1] - v0[1];
314 edge1[2] = v2[2] - v0[2];
315 edge1[3] = 0.0f;
316
317 sceVu0OuterProduct(normal, edge0, edge1);
318 normal[3] = 0.0f;
319
320 plane_dist = sceVu0InnerProduct(normal, v0);
321 from_dist = sceVu0InnerProduct(normal, from);
322 to_dist = sceVu0InnerProduct(normal, to);
323
324 if (plane_dist - from_dist > 0.0f && plane_dist - to_dist > 0.0f) {
325 continue;
326 }
327
328 if (plane_dist - from_dist < 0.0f && plane_dist - to_dist < 0.0f) {
329 continue;
330 }
331
332 if (IntersectionPoint_line_poly3(from, to, v0, v1, v2, normal, point) == 0) {
333 continue;
334 }
335
336 dx = point[0] - from[0];
337 dy = point[1] - from[1];
338 dz = point[2] - from[2];
339 distance = dx * dx + dy * dy + dz * dz;
340
341 if (found == 0) {
342 best_distance = distance;
343 sceVu0CopyVector(hit, point);
344 found = 1;
345 } else {
346 if (distance < best_distance) {
347 best_distance = distance;
348 sceVu0CopyVector(hit, point);
349 }
350
351 found = 1;
352 }
353 }
354
355 return found;
356}
357
358sceVu0FVECTOR *CCollisionMDT::GetVertexAddress(int *count) {
359 if (model == 0) {
360 return 0;
361 }
362
363 *count = model->vertex_num;
364
365 if (*count <= 0) {
366 return 0;
367 }
368
369 return (sceVu0FVECTOR *) ((char *) model + model->vertex_ofs);
370}
371
372/* Every triangle of the mesh whose own bound reaches a cube around the point. */
373int CCollisionMDT::PickUpNearPoly(CCPoly *poly, float *position, float radius) {
374 sceVu0FVECTOR tri_max;
375 sceVu0FVECTOR tri_min;
376 int i;
377 int num;
378 sceVu0FVECTOR *vertex;
379 MDT_CPOLY_SET *set;
380 MDT_CPOLY *cpoly;
381 CCPoly *out;
382 float max_x;
383 float max_y;
384 float max_z;
385 float min_x;
386 float min_y;
387 float min_z;
388
389 out = poly;
390 num = 0;
391
392 max_x = position[0] + radius;
393 max_y = position[1] + radius;
394 max_z = position[2] + radius;
395 min_x = position[0] - radius;
396 min_y = position[1] - radius;
397 min_z = position[2] - radius;
398
399 vertex = (sceVu0FVECTOR *) ((char *) model + model->vertex_ofs);
400 set = &((MDT_COLLISION *) ((char *) model + model->mesh_ofs))->set;
401 cpoly = set->poly;
402
403 for (i = 0; i < set->num; i++) {
404 *(u_long128 *) out->vertex[0] = *(u_long128 *) vertex[cpoly->vertex[0]];
405 *(u_long128 *) out->vertex[1] = *(u_long128 *) vertex[cpoly->vertex[1]];
406 *(u_long128 *) out->vertex[2] = *(u_long128 *) vertex[cpoly->vertex[2]];
407 cpoly++;
408
409 vu_maxmin3(tri_max, tri_min, out->vertex[0], out->vertex[1], out->vertex[2]);
410
411 if (min_x > tri_max[0] || min_y > tri_max[1] || min_z > tri_max[2]) {
412 continue;
413 }
414
415 if (max_x < tri_min[0] || max_y < tri_min[1] || max_z < tri_min[2]) {
416 continue;
417 }
418
419 vu_normal(out->normal, out->vertex[0], out->vertex[1], out->vertex[2]);
420 out++;
421 num++;
422 }
423
424 return num;
425}
426
427/* Every built triangle whose bound overlaps the one the caller gives, tested two bounds at a time
428 on the unit and read back through its sticky flags rather than as a value. */
429int CCollisionMDT::PickUpNearPoly(CCPoly *poly, const CBoxVu0 &box) {
430 CBoxVu0 bound;
431 int i;
432 int num;
433 CCPolyBox *box_poly;
434
435 if (box.min[0] > max[0]) {
436 return 0;
437 }
438
439 if (box.min[1] > max[1]) {
440 return 0;
441 }
442
443 if (box.min[2] > max[2]) {
444 return 0;
445 }
446
447 if (box.max[0] < min[0]) {
448 return 0;
449 }
450
451 if (box.max[1] < min[1]) {
452 return 0;
453 }
454
455 if (box.max[2] < min[2]) {
456 return 0;
457 }
458
459 num = 0;
460
461 bound.max[0] = box.max[0];
462 bound.max[1] = box.max[1];
463 bound.max[2] = box.max[2];
464 bound.min[0] = box.min[0];
465 bound.min[1] = box.min[1];
466 bound.min[2] = box.min[2];
467
468 vu_hold_box(bound.max, bound.min);
469
470 box_poly = mesh;
471
472 for (i = 0; i < this->mesh_count; i++, box_poly++) {
473 if (vu_box_missed(box_poly->box.max, box_poly->box.min) == 0) {
474 *(u_long128 *) poly->vertex[0] = *(u_long128 *) box_poly->poly.vertex[0];
475 *(u_long128 *) poly->vertex[1] = *(u_long128 *) box_poly->poly.vertex[1];
476 *(u_long128 *) poly->vertex[2] = *(u_long128 *) box_poly->poly.vertex[2];
477 poly->info = box_poly->poly.info;
478 *(u_long128 *) poly->normal = *(u_long128 *) box_poly->poly.normal;
479 poly++;
480 num++;
481 }
482 }
483
484 return num;
485}
486
487/* The whole mesh, with no test at all. */
488int CCollisionMDT::PickUpNearPoly(CCPoly *poly) {
489 int i;
490 int num;
491 sceVu0FVECTOR *vertex;
492 MDT_CPOLY_SET *set;
493 MDT_CPOLY *cpoly;
494
495 num = 0;
496
497 vertex = (sceVu0FVECTOR *) ((char *) model + model->vertex_ofs);
498 set = &((MDT_COLLISION *) ((char *) model + model->mesh_ofs))->set;
499 cpoly = set->poly;
500
501 for (i = 0; i < set->num; i++) {
502 *(u_long128 *) poly->vertex[0] = *(u_long128 *) vertex[cpoly->vertex[0]];
503 *(u_long128 *) poly->vertex[1] = *(u_long128 *) vertex[cpoly->vertex[1]];
504 *(u_long128 *) poly->vertex[2] = *(u_long128 *) vertex[cpoly->vertex[2]];
505 cpoly++;
506
507 vu_normal(poly->normal, poly->vertex[0], poly->vertex[1], poly->vertex[2]);
508 poly++;
509 num++;
510 }
511
512 return num;
513}
sceVu0FVECTOR min
Definition collision.hpp:29
sceVu0FVECTOR max
Definition collision.hpp:28
CCPoly poly
Definition collision.hpp:69
CBoxVu0 box
Definition collision.hpp:70
CCPolyInfo info
Definition collision.hpp:52
sceVu0FVECTOR normal
Definition collision.hpp:49
sceVu0FVECTOR vertex[3]
Definition collision.hpp:48
CCPolyBox * mesh
Definition collision.hpp:99
MDT_HEADER * model
Definition collision.hpp:98
sceVu0FVECTOR min
Definition collision.hpp:79
sceVu0FVECTOR max
Definition collision.hpp:78
MDT_CPOLY_SET set
Definition mdt.hpp:58
MDT_CPOLY poly[1]
Definition mdt.hpp:50
unsigned int num
Definition mdt.hpp:49
int vertex[3]
Definition mdt.hpp:39
int vertex_ofs
Definition mdt.hpp:14
int mesh_ofs
Definition mdt.hpp:19
int vertex_num
Definition mdt.hpp:13