1#pragma helper_mask_gpr 0x30
2#pragma helper_mask_fpr 0x1000
3#pragma name_counter 548
14#include "collision.hpp"
15#include "framevu1.hpp"
18#include "renderinfo.hpp"
19#include "visualvu1.hpp"
21void *
operator new(
size_t size, u_long128 *block) {
25void *
operator new[](
size_t size, u_long128 *block) {
43static inline void MulFrameMatrix(sceVu0FMATRIX m0, sceVu0FMATRIX m1, sceVu0FMATRIX m2) {
44 register float *out = &m0[0][0];
45 register float *left = &m1[0][0];
46 register float *right = &m2[0][0];
82static inline void ScaleMatrix(sceVu0FMATRIX m0, sceVu0FMATRIX m1, sceVu0FVECTOR scale) {
83 register float *out = &m0[0][0];
84 register float *in = &m1[0][0];
85 register float *by = &scale[0];
108static inline void CopyMatrix(sceVu0FMATRIX m0, sceVu0FMATRIX m1) {
109 register float *in = &m1[0][0];
110 register float *out = &m0[0][0];
126static inline void ZeroMatrix(sceVu0FMATRIX m0) {
127 register float *out = &m0[0][0];
130 vsub vf15, vf15, vf15
143static inline void ScreenBound(sceVu0FVECTOR *screen, sceVu0FVECTOR max, sceVu0FVECTOR min) {
144 register float *in = &screen[0][0];
145 register float *hi = &max[0];
146 register float *lo = &min[0];
163 vmulq.xy vf10, vf10, Q
170 vmulq.xy vf11, vf11, Q
173 vmulq.xy vf12, vf12, Q
175 vmax vf30, vf10, vf11
176 vmini vf31, vf10, vf11
178 vmulq.xy vf13, vf13, Q
179 vmax vf30, vf30, vf12
180 vmini vf31, vf31, vf12
183 vmulq.xy vf14, vf14, Q
185 vmax vf30, vf30, vf13
186 vmini vf31, vf31, vf13
188 vmulq.xy vf15, vf15, Q
190 vmax vf30, vf30, vf14
191 vmini vf31, vf31, vf14
193 vmulq.xy vf16, vf16, Q
195 vmax vf30, vf30, vf15
196 vmini vf31, vf31, vf15
197 vmax vf30, vf30, vf16
198 vmini vf31, vf31, vf16
200 vmulq.xy vf17, vf17, Q
203 vmax vf30, vf30, vf17
204 vmini vf31, vf31, vf17
212static void QuatToMat(
float *quaternion, sceVu0FMATRIX matrix) {
226 x2 = quaternion[1] + quaternion[1];
227 y2 = quaternion[2] + quaternion[2];
228 z2 = quaternion[3] + quaternion[3];
230 xx = quaternion[1] * x2;
231 xy = quaternion[1] * y2;
232 xz = quaternion[1] * z2;
233 yy = quaternion[2] * y2;
234 yz = quaternion[2] * z2;
235 zz = quaternion[3] * z2;
236 wx = quaternion[0] * x2;
237 wy = quaternion[0] * y2;
238 wz = quaternion[0] * z2;
240 matrix[0][0] = 1.0f - (yy + zz);
241 matrix[1][0] = xy - wz;
242 matrix[2][0] = xz + wy;
245 matrix[0][1] = xy + wz;
246 matrix[1][1] = 1.0f - (xx + zz);
247 matrix[2][1] = yz - wx;
250 matrix[0][2] = xz - wy;
251 matrix[1][2] = yz + wx;
252 matrix[2][2] = 1.0f - (xx + yy);
261void CFrameAttr::Initialize() {
276 unk_40[0] = unk_40[2] = unk_40[3] = 0.0f;
280CFrameAttr::CFrameAttr() {
284void CFrame::Initialize() {
299 sceVu0UnitMatrix(
world);
301 sceVu0UnitMatrix(
local);
303 for (i = 0; i < 8; i++) {
320void CFrame::SetPosition(
float x,
float y,
float z) {
333void CFrame::SetPosition(
float *position) {
346void CFrame::SetScale(
float x,
float y,
float z) {
357void CFrame::SetScale(
float *scale) {
358 if (this->scale[0] ==
scale[0] && this->scale[1] ==
scale[1] && this->scale[2] ==
scale[2]) {
362 this->scale[0] =
scale[0];
363 this->scale[1] =
scale[1];
364 this->scale[2] =
scale[2];
368int CFrame::GetFrameNum() {
374 for (frame =
child; frame != 0; frame = frame->
brother) {
375 num += frame->GetFrameNum();
381void CFrame::SetParent(
CFrame *parent) {
382 if (this->parent != 0) {
395void CFrame::SetBrother(
CFrame *brother) {
400 if (this->brother != 0) {
401 this->brother->SetBrother(
brother);
406 this->brother->
elder =
this;
409void CFrame::SetChild(
CFrame *child) {
415 if (this->child != 0) {
416 this->child->SetBrother(
child);
421 this->child->
parent =
this;
424void CFrame::SetReference(
CFrame *reference) {
430 this->reference =
true;
433void CFrame::DeleteReference() {
441void CFrame::GetLWMatrix(sceVu0FMATRIX matrix) {
442 sceVu0FMATRIX parent_world;
443 sceVu0FMATRIX local_matrix;
444 sceVu0FVECTOR translation;
456 sceVu0CopyMatrix(matrix,
world);
470 sceVu0CopyMatrix(matrix,
world);
478 sibling = this->
child;
489 ScaleMatrix(local_matrix, this->
local, scale);
494 sceVu0CopyVector(translation, local_matrix[3]);
495 local_matrix[3][0] = local_matrix[3][1] = local_matrix[3][2] = 0.0f;
500 sceVu0RotMatrixX(local_matrix, local_matrix,
rotation[0]);
504 sceVu0RotMatrixY(local_matrix, local_matrix,
rotation[1]);
508 sceVu0RotMatrixZ(local_matrix, local_matrix,
rotation[2]);
513 sceVu0AddVector(local_matrix[3], translation,
position);
515 sceVu0AddVector(local_matrix[3], local_matrix[3],
position);
518 sceVu0CopyMatrix(local_matrix, this->
local);
524 sceVu0CopyMatrix(
world, local_matrix);
525 sceVu0CopyMatrix(matrix,
world);
531 sceVu0CopyMatrix(parent_world, frame->
world);
533 frame->GetLWMatrix(parent_world);
536 MulFrameMatrix(
world, parent_world, local_matrix);
537 sceVu0CopyMatrix(matrix,
world);
541float (*CFrame::GetInverseMatrix())[4] {
542 sceVu0FMATRIX world_matrix;
544 GetLWMatrix(world_matrix);
545 sceVu0InversMatrix(inverse, world_matrix);
550void CFrame::SetTransMatrix(sceVu0FMATRIX matrix) {
551 sceVu0CopyMatrix(
local, matrix);
557void CFrame::SetTransMatrix(
float *quaternion) {
558 sceVu0FVECTOR translation;
561 *(u_long128 *) translation = *(u_long128 *)
local[3];
562 QuatToMat(quaternion,
local);
563 *(u_long128 *)
local[3] = *(u_long128 *) translation;
568static int StrCmp(
char *left,
char *right) {
577 right_cursor = right;
581 while ((ch = *left_cursor) !=
'\0') {
582 if (ch ==
'_' && left_cursor[1] ==
'_') {
590 while ((ch = *right_cursor) !=
'\0') {
591 if (ch ==
'_' && right_cursor[1] ==
'_') {
599 if (left_length != right_length) {
603 for (i = 0; i < left_length; i++) {
604 if (*left != *right) {
615int FrameNameComp(
char *left,
char *right) {
616 return StrCmp(left, right);
619CFrame *CFrame::SearchFrame(
char *name) {
623 if (StrCmp(this->name,
name)) {
627 for (frame =
child; frame != 0; frame = frame->
brother) {
628 found = frame->SearchFrame(
name);
640void CFrame::GetBoundBox(
CBoxVu0 *box,
int children) {
659 sceVu0CopyVector(box->
min, low);
660 sceVu0CopyVector(box->
max, high);
663 sceVu0CopyVector(box->
min, box->
max);
665 for (i = 1; i < 8; i++) {
667 VectorMaxMin(box->
max, box->
min, point, box->
max, box->
min);
675 for (frame =
child; frame != 0; frame = frame->
brother) {
676 frame->GetBoundBox(&child_box, 1);
682void CFrame::ScaleBoundBox(
float *scale) {
683 sceVu0FVECTOR center;
686 sceVu0FVECTOR new_max;
687 sceVu0FVECTOR new_min;
691 new_max[0] = this->max[0];
692 new_max[1] = this->max[1];
693 new_max[2] = this->max[2];
695 new_min[0] = this->min[0];
696 new_min[1] = this->min[1];
697 new_min[2] = this->min[2];
699 sceVu0AddVector(center, new_max, new_min);
700 sceVu0ScaleVector(center, center, 0.5f);
701 sceVu0SubVector(dmax, new_max, center);
702 sceVu0SubVector(dmin, new_min, center);
712 sceVu0AddVector(new_max, dmax, center);
713 sceVu0AddVector(new_min, dmin, center);
715 this->max[0] = new_max[0];
716 this->max[1] = new_max[1];
717 this->max[2] = new_max[2];
719 this->min[0] = new_min[0];
720 this->min[1] = new_min[1];
721 this->min[2] = new_min[2];
723 bound[0] = this->
min;
724 bound[1] = this->
max;
726 for (i = 0; i < 8; i++) {
727 this->
corner[i][3] = 1.0f;
728 this->
corner[i][0] = bound[(i & 1) != 0][0];
729 this->
corner[i][1] = bound[(i & 2) != 0][1];
730 this->corner[i][2] = bound[(i & 4) != 0][2];
734void CFrame::SetAttr(
CFrameAttr &attr,
int children,
int mask) {
753 this->attr.unk_09 =
attr.unk_09;
804 if (mask & 0x10000) {
813 for (frame =
child; frame != 0; frame = frame->
brother) {
814 frame->SetAttr(
attr, 1, mask);
818void CFrame::GetWorldPosition(
float *world_point,
float *local_point) {
819 sceVu0FMATRIX matrix;
821 local_point[3] = 1.0f;
823 sceVu0ApplyMatrix(world_point, matrix, local_point);
826void CFrame::SetRotation(
float x,
float y,
float z) {
842void CFrame::GetRotation(
float *rotation) {
848 sceVu0CopyVector(
rotation, this->rotation);
851void CFrame::SetRotType(
int type) {
862 memcpy(
this, &other,
sizeof(
CFrame));
876static void ShadowMatrix(sceVu0FMATRIX matrix, sceVu0FMATRIX light,
float *point,
float *normal) {
877 sceVu0FVECTOR direction;
890 direction[0] = light[0][0];
891 direction[1] = light[1][0];
892 direction[2] = light[2][0];
895 sceVu0CopyVector(plane, point);
896 sceVu0CopyVector(axis, normal);
898 t = sceVu0InnerProduct(axis, plane);
901 plane[0] -= 0.1f * axis[0];
902 plane[1] -= 0.1f * axis[1];
903 plane[2] -= 0.1f * axis[2];
904 t = sceVu0InnerProduct(axis, plane);
912 sceVu0Normalize(direction, direction);
918 depth = dx * nx + dy * ny + dz * nz;
919 scale = -1.0f / depth;
921 matrix[0][0] = scale * (dx * nx - depth);
922 matrix[1][0] = scale * (dy * nx);
923 matrix[2][0] = scale * (dz * nx);
924 matrix[3][0] = scale * -nx;
925 matrix[0][1] = scale * (dx * ny);
926 matrix[1][1] = scale * (dy * ny - depth);
927 matrix[2][1] = scale * (dz * ny);
928 matrix[3][1] = scale * -ny;
929 matrix[0][2] = scale * (dx * nz);
930 matrix[1][2] = scale * (dy * nz);
931 matrix[2][2] = scale * (dz * nz - depth);
932 matrix[3][2] = scale * -nz;
936 matrix[3][3] = scale * -depth;
942static void ShadowClipBox(sceVu0FVECTOR *screen, sceVu0FVECTOR *corner, sceVu0FMATRIX matrix,
RenderInfo *info) {
943 sceVu0FMATRIX shadow;
944 sceVu0FVECTOR flat[8];
945 sceVu0FVECTOR world[8];
948 sceVu0FVECTOR extent[4];
949 sceVu0FVECTOR max[4];
950 sceVu0FVECTOR min[4];
953 ApplyMatrixN(world, matrix, corner, 8);
954 MulFrameMatrix(shadow, info->
shadow, matrix);
955 ApplyMatrixN(flat, shadow, corner, 8);
957 VectorMaxMin(max[0], min[0], world[0], world[1], world[2], world[3]);
958 VectorMaxMin(max[1], min[1], world[4], world[5], world[6], world[7]);
959 VectorMaxMin(max[2], min[2], flat[0], flat[1], flat[2], flat[3]);
960 VectorMaxMin(max[3], min[3], flat[4], flat[5], flat[6], flat[7]);
961 VectorMax(extent[0], max[0], max[1], max[2], max[3]);
962 VectorMin(extent[1], min[0], min[1], min[2], min[3]);
964 for (i = 0; i < 8; i++) {
966 world[i][0] = extent[(i & 1) != 0][0];
967 world[i][1] = extent[(i & 2) != 0][1];
968 world[i][2] = extent[(i & 4) != 0][2];
977static int SetGsReg3(u_int *p, u_long *test, u_long *zbuf, u_long *alpha) {
990 *(u_long *) (p + 12) = *test;
991 p[14] = SCE_GS_TEST_1;
993 *(u_long *) (p + 16) = *zbuf;
994 p[18] = SCE_GS_ZBUF_1;
996 *(u_long *) (p + 20) = *alpha;
997 p[22] = SCE_GS_ALPHA_1;
1019 u_int *start = packet;
1020 sceVu0FMATRIX matrix;
1021 sceVu0FMATRIX screen_matrix;
1023 sceVu0FMATRIX light;
1024 sceVu0FVECTOR ambient;
1025 sceVu0FVECTOR direction;
1026 sceVu0FVECTOR world_position;
1027 sceVu0FVECTOR origin = {0.0f, 0.0f, 0.0f, 1.0f};
1028 sceVu0FVECTOR screen_corner[8];
1029 sceVu0FMATRIX world_matrix;
1030 sceVu0FVECTOR screen_max;
1031 sceVu0FVECTOR screen_min;
1032 sceVu0FVECTOR color;
1056 GetWorldPosition(world_position, origin);
1057 GetLWMatrix(world_matrix);
1058 axis_x = DistVector(world_matrix[0]);
1059 axis_y = DistVector(world_matrix[1]);
1060 axis_z = DistVector(world_matrix[2]);
1063 sceVu0CopyMatrix(matrix, mgUnitMatrix);
1064 sceVu0SubVector(matrix[2], info->
view_position, world_position);
1065 matrix[2][1] = 0.0f;
1066 matrix[2][3] = 0.0f;
1067 sceVu0Normalize(matrix[2], matrix[2]);
1068 matrix[0][0] = matrix[2][2];
1069 matrix[0][2] = -matrix[2][0];
1073 sceVu0UnitMatrix(turn);
1074 sceVu0SubVector(direction, info->
view_position, world_position);
1075 sceVu0Normalize(direction, direction);
1076 height = direction[1];
1077 direction[1] = 0.0f;
1078 direction[3] = 0.0f;
1079 flat = DistVector(direction);
1081 turn[1][2] = -height;
1082 turn[2][1] = height;
1085 sceVu0CopyMatrix(matrix, mgUnitMatrix);
1086 sceVu0SubVector(matrix[2], info->
view_position, world_position);
1087 matrix[2][1] = 0.0f;
1088 matrix[2][3] = 0.0f;
1089 sceVu0Normalize(matrix[2], matrix[2]);
1090 matrix[0][0] = matrix[2][2];
1091 matrix[0][2] = -matrix[2][0];
1093 MulFrameMatrix(matrix, matrix, turn);
1096 sceVu0ScaleVector(matrix[0], matrix[0], axis_x);
1097 sceVu0ScaleVector(matrix[1], matrix[1], axis_y);
1098 sceVu0ScaleVector(matrix[2], matrix[2], axis_z);
1099 matrix[3][0] = world_position[0];
1100 matrix[3][1] = world_position[1];
1101 matrix[3][2] = world_position[2];
1102 CopyMatrix(this->world, matrix);
1106 sibling = this->
child;
1118 GetLWMatrix(matrix);
1130 MulFrameMatrix(screen_matrix, info->
view_scaled, matrix);
1131 inv_scale = 1.0f / info->
scale[0];
1135 ShadowClipBox(screen_corner, this->corner, matrix, info);
1136 ScreenBound(screen_corner, screen_max, screen_min);
1138 ApplyMatrixN(screen_corner, screen_matrix, this->corner, 8);
1139 ScreenBound(screen_corner, screen_max, screen_min);
1142 near_z = info->
near[2];
1149 half_width = 2.0f * (320.0f * inv_scale);
1150 half_height = 2.0f * (SCREEN_QUARTER_HEIGHT_F * inv_scale);
1152 half_width = 320.0f * inv_scale;
1153 half_height = SCREEN_QUARTER_HEIGHT_F * inv_scale;
1156 if (screen_min[0] > half_width) {
1160 if (screen_max[0] < -half_width) {
1164 if (screen_min[1] > half_height) {
1168 if (screen_max[1] < -half_height) {
1172 if (screen_max[2] < near_z) {
1176 if (screen_min[2] < far_z) {
1180 depth = 0.96f * (2048.0f * inv_scale);
1182 if (screen_min[2] > near_z && screen_max[0] < depth && screen_min[0] > -depth && screen_max[1] < depth && screen_min[1] > -depth) {
1244 packet += SetGsReg3(packet, (u_long *) &test, (u_long *) &zbuf, (u_long *) &alpha);
1254 *(u_long128 *) ambient = *(u_long128 *) info->
ambient;
1255 info->
ambient[0] += 0.3f * color[0];
1256 info->
ambient[1] += 0.3f * color[1];
1257 info->
ambient[2] += 0.3f * color[2];
1261 *(u_long128 *) ambient = *(u_long128 *) info->
ambient;
1274 packet +=
visual->
DrawVu1(packet, matrix, info, VU1_PROGRAM_UNKNOWN6, 0, 0, 0);
1278 *(u_long128 *) info->
ambient = *(u_long128 *) ambient;
1282 packet += SetGsReg3(packet, (u_long *) &mgPixelTest, (u_long *) &mgZBuffer, (u_long *) &mgAlpha);
1287 for (frame = this->child; frame; frame = frame->
brother) {
1292 packet += frame->DrawVu1(packet, info);
1296 return packet - start;
1308 u_int *current = work;
1310 sceVif1PkTerminate(packet);
1311 count =
DrawVu1(packet->pCurrent, info);
1312 sceVif1PkReserve(packet, count);
1317void CFrame::SetCollision(
CCollision *collision) {
1321int CFrame::DrawVu1(
unsigned int *packet,
RenderInfo *info) {
1330 CFrame::Initialize();
1342 CFrame::Initialize();
1354 CFrame::operator=(other);
1362static void pre_trance_normal(sceVu0FMATRIX matrix) {
1363 register float *m = &matrix[0][0];
1376static void trance_normal(
float *p0,
float *p1,
float *p2,
float *plane) {
1377 register float *a = p0;
1378 register float *b = p1;
1379 register float *c = p2;
1380 register float *normal = plane;
1386 vmulax ACC, vf10, vf16
1387 vmadday ACC, vf11, vf16
1388 vmaddaz ACC, vf12, vf16
1389 vmaddw vf16, vf13, vf16
1390 vmulax ACC, vf10, vf17
1391 vmadday ACC, vf11, vf17
1392 vmaddaz ACC, vf12, vf17
1393 vmaddw vf17, vf13, vf17
1394 vmulax ACC, vf10, vf18
1395 vmadday ACC, vf11, vf18
1396 vmaddaz ACC, vf12, vf18
1397 vmaddw vf18, vf13, vf18
1398 vsub vf20, vf17, vf16
1399 vsub vf21, vf18, vf16
1404 vopmula ACC, vf20, vf21
1405 vopmsub vf22, vf21, vf20
1406 sqc2 vf22, 0(normal)
1414int CFrame::PickUpNearPoly(
CCPoly *poly,
const CBoxVu0 &box) {
1415 sceVu0FVECTOR box_corner[8];
1416 sceVu0FVECTOR local_corner[8];
1421 sceVu0FVECTOR extent[2];
1422 sceVu0FMATRIX world_matrix;
1425 float (*inverse_matrix)[4];
1438 GetLWMatrix(world_matrix);
1439 inverse_matrix = GetInverseMatrix();
1441 *(u_long128 *) extent[0] = *(u_long128 *) box.
min;
1442 *(u_long128 *) extent[1] = *(u_long128 *) box.
max;
1444 for (i = 0; i < 8; i++) {
1445 box_corner[i][3] = 1.0f;
1446 box_corner[i][0] = extent[(i & 1) != 0][0];
1447 box_corner[i][1] = extent[(i & 2) != 0][1];
1448 box_corner[i][2] = extent[(i & 4) != 0][2];
1451 ApplyMatrixN(local_corner, inverse_matrix, box_corner, 8);
1452 VectorMaxMin(max0, min0, local_corner[0], local_corner[1], local_corner[2], local_corner[3]);
1453 VectorMaxMin(max1, min1, local_corner[4], local_corner[5], local_corner[6], local_corner[7]);
1454 VectorMaxMin(bound.
max, bound.
min, max0, max1, min0, min1);
1456 num =
collision->PickUpNearPoly(poly, bound);
1459 pre_trance_normal(world_matrix);
1461 for (i = 0; i < num; i++, found++) {
1466 if ((
flags & 2) == 0) {
1467 for (frame =
child; frame != 0; frame = frame->
brother) {
1472 num += frame->PickUpNearPoly(found, box);
CFrameVu1 & operator=(CFrameVu1 &other)
virtual void Initialize()
void SetVisual(CVisualVu1 *visual)
virtual int DrawVu1(unsigned int *packet, RenderInfo *info)
virtual int RemakeData(u_int *data)
virtual int DrawVu1(u_int *packet, float(*matrix)[4], RenderInfo *info, VU1_PROGRAM program, u_long128 *draw_state, int unknown1, int unknown2)
sceVu0FMATRIX light_color
sceVu0FMATRIX light_direction
sceVu0FVECTOR view_position
sceVu0FVECTOR shadow_point
sceVu0FMATRIX view_scaled
sceVu0FVECTOR shadow_normal