1#pragma helper_mask_gpr 0x30
2#pragma helper_mask_fpr 0x1000
3#pragma name_counter 199
14#include "collisionmdt.hpp"
15#include "dataalloc.hpp"
17#include "framevu1.hpp"
24void CCollisionMDT::Initialize() {
44void InitializeDataBuffer() {
45 GlobalDataBuffer.
used = 0;
46 memset(&GlobalDataBuffer.
block[GlobalDataBuffer.
used], 0, 1690000 * 16);
48 GlobalDataBuffer.Alloc64(10);
56 GlobalDataBuffer.Alloc64(25000);
61 ActiveData0.
limit = 25000;
64 GlobalDataBuffer.Alloc64(25000);
69 ActiveData1.
limit = 25000;
77 arena->
base = GlobalDataBuffer.Alloc64(quads);
82void SetPacketReadBuffer(
int packet_quads,
int read_quads) {
83 u_long128 *packet_buffer0;
84 u_long128 *packet_buffer1;
86 read_buffer = (u_int *) GlobalDataBuffer.Alloc64(read_quads);
87 packet_buffer0 = (u_long128 *) GlobalDataBuffer.Alloc64(packet_quads);
88 packet_buffer1 = (u_long128 *) GlobalDataBuffer.Alloc64(packet_quads);
89 MGInitVif1Packet(packet_buffer0, packet_buffer1);
90 workbuffer.
base = GlobalDataBuffer.Alloc64(2048);
91 workbuffer.
limit = 2048;
92 WorkBuffer = &workbuffer;
94 printf(
"%d/%d\n", GlobalDataBuffer.
used, 1690000);
97void BufferAllClear() {
98 GlobalDataBuffer.
used = 0;
100 lw $2, GlobalDataBuffer+27040000
102 la $2, GlobalDataBuffer
105 beq $0, $0, clear_test
114 bne $3, $0, clear_loop
117 VisualData.
base = GlobalDataBuffer.Alloc64(600000);
118 VisualData.
limit = 600000;
121 MotionData.
base = GlobalDataBuffer.Alloc64(200000);
122 MotionData.
limit = 200000;
125 TextureData.
base = GlobalDataBuffer.Alloc64(300000);
126 TextureData.
limit = 300000;
127 TextureData.
used = 0;
129 WaterData.
base = GlobalDataBuffer.Alloc64(90000);
130 WaterData.
limit = 90000;
133 ActiveData0.
base = GlobalDataBuffer.Alloc64(25000);
134 ActiveData0.
limit = 25000;
135 ActiveData0.
used = 0;
137 ActiveData1.
base = GlobalDataBuffer.Alloc64(25000);
138 ActiveData1.
limit = 25000;
139 ActiveData1.
used = 0;
141 read_buffer = (u_int *) GlobalDataBuffer.Alloc64(100000);
142 workbuffer.
base = GlobalDataBuffer.Alloc64(4096);
143 workbuffer.
limit = 4096;
144 WorkBuffer = &workbuffer;
145 WorkBuffer->
used = 0;
147 u_long128 *packet_buffer0 = (u_long128 *) GlobalDataBuffer.Alloc64(50000);
148 u_long128 *packet_buffer1 = (u_long128 *) GlobalDataBuffer.Alloc64(50000);
149 MGInitVif1Packet(packet_buffer0, packet_buffer1);
152 ActiveData0.
used = 0;
153 ActiveData1.
used = 0;
156static int htoi(
char *text) {
169 while (*cursor++ !=
'\0') {
175 for (i = 0; i < len; i++) {
176 ch = (
unsigned char) text[len - i - 1];
179 if (ch >=
'0' && ch <=
'9') {
183 if (ch >=
'a' && ch <=
'f') {
184 digit = ch -
'a' + 10;
187 if (ch >=
'A' && ch <=
'F') {
188 digit = ch -
'A' + 10;
191 value += digit * place;
198void SetFrameAttr(
CFrame *frame,
int recurse) {
207 cursor = frame->
name;
209 if (*cursor ==
'\0') {
216 if (*cursor ==
'\0') {
221 if (*cursor ==
'_' && cursor[-1] ==
'_') {
229 if (*cursor ==
'\0') {
248 blend_code[0] = cursor[1];
250 blend_code[1] = *cursor;
251 blend_code[2] =
'\0';
253 if (blend_code[0] >=
'a' && blend_code[0] <=
'z') {
257 if (blend_code[1] >=
'a' && blend_code[1] <=
'z') {
261 if (blend_code[0] ==
'P' && blend_code[1] ==
'P') {
263 }
else if (blend_code[0] ==
'N' && blend_code[1] ==
'N') {
290 if (*cursor ==
'\0') {
295 if (*cursor ==
'Y' || *cursor ==
'y') {
299 if (*cursor ==
'A' || *cursor ==
'a') {
323 for (child = frame->
child; child != 0; child = child->
brother) {
324 SetFrameAttr(child, recurse);
342static void ArrangeShadowMDT(u_int *data);
343static void CreateBBox(
CBox3<float> *box, sceVu0FVECTOR *vertex,
int num);
348CFrameVu1 *LoadMDSFile(u_int *data,
CDataAlloc2<1> *alloc,
int attr,
char **double_names,
char **retain_names) {
370 sceVu0FMATRIX matrix;
381 if ((
int) data % 16) {
382 printf(
"address error!! %d \n", data);
405 if (memcmp(object->
name,
"func_", 5) == 0) {
412 node->SetScale(sx, sy, sz);
414 node->SetRotation(rx, ry, rz);
415 node->SetPosition(0.0f, 0.0f, 0.0f);
417 for (j = 0; j < 4; j++) {
418 for (k = 0; k < 4; k++) {
419 matrix[k][j] =
object->matrix[k][j];
424 node->SetTransMatrix(matrix);
425 SetFrameAttr(node, 0);
430 node->SetParent(&frames[object->
parent]);
436 sceVu0FVECTOR *vertex = (sceVu0FVECTOR *) ((
char *) model + model->
vertex_ofs);
441 extremes[0] = node->
min;
442 extremes[1] = node->
max;
444 for (j = 0; j < 8; j++) {
445 node->
corner[j][3] = 1.0f;
446 node->
corner[j][0] = extremes[(j & 1) != 0][0];
447 node->
corner[j][1] = extremes[(j & 2) != 0][1];
448 node->
corner[j][2] = extremes[(j & 4) != 0][2];
452 double_entry = double_names;
455 while (*double_entry) {
456 if (FrameNameComp(node->
name, *double_entry)) {
466 retain_entry = retain_names;
469 while (*retain_entry) {
470 if (FrameNameComp(node->
name, *retain_entry)) {
479 visual = CreateVisual((u_int *) ((
char *) header + object->
data_ofs), alloc, attr);
527 ->CreateVUdataShadow((u_int *) (alloc->
base + alloc->
used * 16), data);
530 ArrangeShadowMDT(data);
536 dset_mds_packet += visual->
vu_size;
541 copy = (u_int *) alloc->Alloc((((
MDT_HEADER *) data)->size >> 4) + 1);
542 memcpy(copy, data, ((
MDT_HEADER *) data)->size);
552 if ((attr & 4) && !(attr & 0x10)) {
558 ->CreateVUdataShadow((u_int *) (alloc->
base + alloc->
used * 16), data);
581static void ArrangeShadowMDT(u_int *data) {
583 sceVu0FVECTOR normals[1024];
588 sceVu0FVECTOR *vertex;
603 vertex = (sceVu0FVECTOR *) ((
char *) model + model->
vertex_ofs);
609 for (i = 0; i < shape_num; i++) {
613 for (j = 0; j < index_count / 3; j++) {
614 sceVu0SubVector(side0, vertex[corner[1].index], vertex[corner[0].index]);
615 sceVu0SubVector(side1, vertex[corner[2].index], vertex[corner[0].index]);
616 sceVu0OuterProduct(face, side0, side1);
617 sceVu0Normalize(normals[edge_count], face);
618 sceVu0CopyVector(normals[edge_count + 1], normals[edge_count]);
619 sceVu0CopyVector(normals[edge_count + 2], normals[edge_count]);
621 edges[edge_count].
v0 = corner[0].
index;
622 edges[edge_count].
v1 = corner[1].
index;
623 edges[edge_count].
done = 0;
625 corner[0].
edge =
false;
628 edges[edge_count].
v0 = corner[1].
index;
629 edges[edge_count].
v1 = corner[2].
index;
630 edges[edge_count].
done = 0;
632 corner[1].
edge =
false;
635 edges[edge_count].
v0 = corner[2].
index;
636 edges[edge_count].
v1 = corner[0].
index;
637 edges[edge_count].
done = 0;
639 corner[2].
edge =
false;
642 if (edge_count > 1020) {
643 printf(
"shadow initialize failed\n");
655 for (k = 0; k < edge_count - 1; k++) {
660 for (m = k + 1; m < edge_count; m++) {
665 if (edges[k].v0 != edges[m].v1) {
669 if (edges[k].v1 != edges[m].v0) {
673 if (DistVector(normals[k], normals[m]) < 0.0008f) {
689 for (i = 0; i < 4; i++) {
693 frame[i] = LoadMDSFile(data[i], alloc, attr, 0, 0);
711 sceVu0FMATRIX matrix;
724 block = (u_long128 *) alloc->Alloc(((
MDS_HEADER *) data)->object_num *
sizeof(
CFrameVu1) / 16);
734 for (j = 0; j < 4; j++) {
735 for (k = 0; k < 4; k++) {
736 matrix[k][j] =
object->matrix[k][j];
741 node->SetTransMatrix(matrix);
746 node->SetParent(&frames[object->
parent]);
752 node->SetCollision(CreateCollisionMDT((u_int *) model, alloc));
753 sceVu0FVECTOR *vertex = (sceVu0FVECTOR *) ((
char *) model + model->vertex_ofs);
754 int vertex_num = model->vertex_num;
758 extremes[0] = node->
min;
759 extremes[1] = node->
max;
761 for (corner = 0; corner < 8; corner++) {
762 node->
corner[corner][3] = 1.0f;
763 node->
corner[corner][0] = extremes[(corner & 1) != 0][0];
764 node->
corner[corner][1] = extremes[(corner & 2) != 0][1];
765 node->
corner[corner][2] = extremes[(corner & 4) != 0][2];
792 memcpy(model, source, source->
size);
794 collision =
new ((u_long128 *) alloc->Alloc(4))
CCollisionMDT;
795 collision->
model = model;
796 collision->CreateBBox();
798 vertex = (u_long128 *) ((
char *) model + model->
vertex_ofs);
799 info = (u_long128 *) ((
char *) model + model->
info_ofs);
801 poly = poly_set->
poly;
805 for (i = 0; i < (poly_count = poly_set->
num); i++) {
806 *(u_long128 *) built[i].poly.
vertex[0] = vertex[poly->
vertex[0]];
807 *(u_long128 *) built[i].poly.
vertex[1] = vertex[poly->
vertex[1]];
808 *(u_long128 *) built[i].poly.
vertex[2] = vertex[poly->
vertex[2]];
812 if (!model->
info_ofs || index < 0) {
813 memset(&built[i].poly.info, 0, 16);
815 *(u_long128 *) &built[i].poly.info = info[index];
820 VectorMaxMin(built[i].box.max, built[i].box.min, built[i].poly.vertex[0], built[i].poly.vertex[1], built[i].poly.vertex[2]);
821 PlaneNormal(built[i].poly.normal, built[i].poly.
vertex[0], built[i].poly.vertex[1], built[i].poly.vertex[2]);
824 collision->
mesh = built;
830CFrameVu1 *LoadMDSFile(u_int *data,
int attr,
int unused) {
831 return LoadMDSFile(data, &VisualData, attr, 0, 0);
836static void CreateBBox(
CBox3<float> *box, sceVu0FVECTOR *vertex,
int num) {
840 box->
max[0] = box->
min[0] = 0.0f;
841 box->
max[1] = box->
min[1] = 0.0f;
842 box->
max[2] = box->
min[2] = 0.0f;
846 box->
max[0] = vertex[0][0];
847 box->
max[1] = vertex[0][1];
848 box->
max[2] = vertex[0][2];
849 box->
min[0] = vertex[0][0];
850 box->
min[1] = vertex[0][1];
851 box->
min[2] = vertex[0][2];
853 for (i = 0; i < num; i++) {
854 if (box->
max[0] < vertex[0][0]) {
855 box->
max[0] = vertex[0][0];
858 if (box->
max[1] < vertex[0][1]) {
859 box->
max[1] = vertex[0][1];
862 if (box->
max[2] < vertex[0][2]) {
863 box->
max[2] = vertex[0][2];
866 if (box->
min[0] > vertex[0][0]) {
867 box->
min[0] = vertex[0][0];
870 if (box->
min[1] > vertex[0][1]) {
871 box->
min[1] = vertex[0][1];
874 if (box->
min[2] > vertex[0][2]) {
875 box->
min[2] = vertex[0][2];
899 first_child = frame->
child;
901 for (child = first_child; child; child = child->
brother) {
902 CopyFrameVu1((
CFrameVu1 *) child, alloc)->SetParent(copy);
918 copy =
new ((u_long128 *) alloc->Alloc(
sizeof(
CFrame) / 16))
CFrame;
922 first_child = frame->
child;
924 for (child = first_child; child; child = child->
brother) {
925 CopyFrame(child, alloc)->SetParent(copy);
931void LoadLODData(
CFrameVu1 **frame,
char **name, u_int *data,
int attr) {
935CFrameVu1 *LoadCollisionFile(u_int *data) {
936 return LoadCollisionFile(data, &VisualData);
939int CCollision::GetPolygon(
int index, sceVu0FMATRIX v0, sceVu0FMATRIX v1, sceVu0FMATRIX v2) {
943int CCollision::GetMaxY(
float *position) {
947int CCollision::Intersection(
float *from,
float *to,
float *hit) {
951int CCollision::PickUpNearPoly(
CCPoly *poly) {
955int CCollision::PickUpNearPoly(
CCPoly *poly,
const CBoxVu0 &box) {
959int CCollision::PickUpNearPoly(
CCPoly *poly,
float *position,
float radius) {
963void CCollision::Initialize() {
virtual void Initialize()
void SetVisual(CVisualVu1 *visual)
u_int * vu_data_buffer[2]
virtual void SetMDTDataAddress(u_int *data)
virtual void Initialize()
int CreateVUdataFromMDT(u_int *block, u_int *data, int unknown0, int unknown1)