Some optimizations + cleanup

This commit is contained in:
Arceveti committed 2021-09-28 21:13:26 -07:00
1 parent 868dd8f97b
commit 28e8d99707
22 files changed
+339 -619

No files matched your search

-5
View File
@@ -13,11 +13,6 @@
s16 identityMtx[4][4] = { { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 1 } };
s16 zeroMtx[4][4] = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };
Vec3f gVec3fZero = { 0.0f, 0.0f, 0.0f };
Vec3s gVec3sZero = { 0, 0, 0 };
Vec3f gVec3fOne = { 1.0f, 1.0f, 1.0f };
UNUSED Vec3s gVec3sOne = { 1, 1, 1 };
/**
* Initialize a geo node with a given type. Sets all links such that there
* are no siblings, parent or children for this node.
-5
View File
@@ -376,11 +376,6 @@ extern struct GraphNode *gCurGraphNodeList[];
extern s16 gCurGraphNodeIndex;
extern Vec3f gVec3fZero;
extern Vec3s gVec3sZero;
extern Vec3f gVec3fOne;
extern Vec3s gVec3sOne;
void init_scene_graph_node_links(struct GraphNode *graphNode, s32 type);
struct GraphNodeRoot *init_graph_node_root(struct AllocOnlyPool *pool, struct GraphNodeRoot *graphNode,
+92 -64
View File
@@ -12,8 +12,40 @@
#include "config.h"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wreturn-local-addr"
Vec3f gVec3fX = { 1.0f, 0.0f, 0.0f };
Vec3f gVec3fY = { 0.0f, 1.0f, 0.0f };
Vec3f gVec3fZ = { 0.0f, 0.0f, 1.0f };
Vec3f gVec3fNX = { -1.0f, 0.0f, 0.0f };
Vec3f gVec3fNY = { 0.0f, -1.0f, 0.0f };
Vec3f gVec3fNZ = { 0.0f, 0.0f, -1.0f };
Vec3f gVec3fZero = { 0.0f, 0.0f, 0.0f };
Vec3f gVec3fOne = { 1.0f, 1.0f, 1.0f };
Vec3s gVec3sZero = { 0, 0, 0 };
Vec3i gVec3iZero = { 0, 0, 0 };
Vec3s gVec3sOne = { 1, 1, 1 };
/// From Wiseguy
static inline s32 roundf(f32 in) {
f32 tmp;
s32 out;
__asm__("round.w.s %0,%1" : "=f" (tmp) : "f" (in));
__asm__("mfc1 %0,%1" : "=r" (out) : "f" (tmp));
return out;
}
// static inline float absf(float in) {
// f32 out;
// __asm__("abs.s %0,%1" : "=f" (out) : "f" (in));
// return out;
// }
f32 absf(f32 x) {
if (x >= 0) {
return x;
} else {
return -x;
}
}
/// Returns the lowest of three values.
s32 min_3i(s32 a0, s32 a1, s32 a2) { if (a1 < a0) a0 = a1; if (a2 < a0) a0 = a2; return a0; }
@@ -112,7 +144,7 @@ void vec3f_cross(Vec3f dest, Vec3f a, Vec3f b) {
/// Scale vector 'dest' so it has length 1
void vec3f_normalize(Vec3f dest) {
f32 mag = sqrtf(sqr(dest[0]) + sqr(dest[1]) + sqr(dest[2]));
if (mag > __FLT_EPSILON__) {
if (mag > NEAR_ZERO) {
register f32 invsqrt = 1.0f / mag;
vec3_mul_val(dest, invsqrt);
} else {
@@ -125,7 +157,7 @@ void vec3f_normalize(Vec3f dest) {
/// Scale vector 'dest' so it has length -1
void vec3f_normalize_negative(Vec3f dest) {
f32 mag = sqrtf(sqr(dest[0]) + sqr(dest[1]) + sqr(dest[2]));
if (mag > __FLT_EPSILON__) {
if (mag > NEAR_ZERO) {
register f32 invsqrt = -1.0f / mag;
vec3_mul_val(dest, invsqrt);
} else {
@@ -135,7 +167,6 @@ void vec3f_normalize_negative(Vec3f dest) {
}
}
#pragma GCC diagnostic pop
struct CopyMe {
f32 x; f32 y; f32 z; f32 w;
f32 x1; f32 y1; f32 z1; f32 w1;
@@ -173,33 +204,31 @@ void mtxf_translate(Mat4 dest, Vec3f b) {
void mtxf_rot_trans_mul(Vec3s rot, Vec3f trans, Mat4 dest, Mat4 src) {
register f32 sx = sins(rot[0]);
register f32 cx = coss(rot[0]);
register f32 sy = sins(rot[1]);
register f32 cy = coss(rot[1]);
register f32 sz = sins(rot[2]);
register f32 cz = coss(rot[2]);
register Vec3f entry;
entry[0] = cy * cz;
entry[1] = cy * sz;
entry[0] = (cy * cz);
entry[1] = (cy * sz);
entry[2] = -sy;
dest[0][0] = entry[0] * src[0][0] + entry[1] * src[1][0] + entry[2] * src[2][0];
dest[0][1] = entry[0] * src[0][1] + entry[1] * src[1][1] + entry[2] * src[2][1];
dest[0][2] = entry[0] * src[0][2] + entry[1] * src[1][2] + entry[2] * src[2][2];
entry[1] = sx * sy;
entry[1] = (sx * sy);
entry[0] = (entry[1] * cz) - (cx * sz);
entry[1] = (entry[1] * sz) + (cx * cz);
entry[2] = sx * cy;
entry[2] = (sx * cy);
dest[1][0] = entry[0] * src[0][0] + entry[1] * src[1][0] + entry[2] * src[2][0];
dest[1][1] = entry[0] * src[0][1] + entry[1] * src[1][1] + entry[2] * src[2][1];
dest[1][2] = entry[0] * src[0][2] + entry[1] * src[1][2] + entry[2] * src[2][2];
entry[1] = cx * sy;
entry[1] = (cx * sy);
entry[0] = (entry[1] * cz) + (sx * sz);
entry[1] = (entry[1] * sz) - (sx * cz);
entry[2] = cx * cy;
entry[2] = (cx * cy);
dest[2][0] = entry[0] * src[0][0] + entry[1] * src[1][0] + entry[2] * src[2][0];
dest[2][1] = entry[0] * src[0][1] + entry[1] * src[1][1] + entry[2] * src[2][1];
dest[2][2] = entry[0] * src[0][2] + entry[1] * src[1][2] + entry[2] * src[2][2];
@@ -223,7 +252,7 @@ void mtxf_lookat(Mat4 mtx, Vec3f from, Vec3f to, s32 roll) {
register f32 dx = (to[0] - from[0]);
register f32 dz = (to[2] - from[2]);
register f32 invLength = sqrtf(sqr(dx) + sqr(dz));
invLength = -(1.0f / MAX(invLength, __FLT_EPSILON__));
invLength = -(1.0f / MAX(invLength, NEAR_ZERO));
dx *= invLength;
dz *= invLength;
f32 sr = sins(roll);
@@ -256,30 +285,26 @@ void mtxf_lookat(Mat4 mtx, Vec3f from, Vec3f to, s32 roll) {
* axis, and then translates.
*/
void mtxf_rotate_zxy_and_translate(Mat4 dest, Vec3f translate, Vec3s rotate) {
register f32 sx = sins(rotate[0]);
register f32 cx = coss(rotate[0]);
register f32 sy = sins(rotate[1]);
register f32 cy = coss(rotate[1]);
register f32 sz = sins(rotate[2]);
register f32 cz = coss(rotate[2]);
dest[0][0] = cy * cz + sx * sy * sz;
dest[1][0] = -cy * sz + sx * sy * cz;
dest[2][0] = cx * sy;
dest[3][0] = translate[0];
dest[0][1] = cx * sz;
dest[1][1] = cx * cz;
register f32 sx = sins(rotate[0]);
register f32 cx = coss(rotate[0]);
register f32 sy = sins(rotate[1]);
register f32 cy = coss(rotate[1]);
register f32 sz = sins(rotate[2]);
register f32 cz = coss(rotate[2]);
register f32 cycz = (cy * cz);
register f32 cysz = (cy * sz);
register f32 sycz = (sy * cz);
register f32 sysz = (sy * sz);
dest[0][0] = ((sx * sysz) + cycz);
dest[1][0] = ((sx * sycz) - cysz);
dest[2][0] = (cx * sy);
dest[0][1] = (cx * sz);
dest[1][1] = (cx * cz);
dest[2][1] = -sx;
dest[3][1] = translate[1];
dest[0][2] = -sy * cz + sx * cy * sz;
dest[1][2] = sy * sz + sx * cy * cz;
dest[0][2] = ((sx * cysz) - sycz);
dest[1][2] = ((sx * cycz) + sysz);
dest[2][2] = cx * cy;
dest[3][2] = translate[2];
vec3_copy(dest[3], translate);
dest[0][3] = dest[1][3] = dest[2][3] = 0.;
((u32 *) dest)[15] = 0x3F800000;
}
@@ -289,33 +314,29 @@ void mtxf_rotate_zxy_and_translate(Mat4 dest, Vec3f translate, Vec3s rotate) {
* axis, and then translates.
*/
void mtxf_rotate_xyz_and_translate(Mat4 dest, Vec3f b, Vec3s c) {
register f32 sx = sins(c[0]);
register f32 cx = coss(c[0]);
register f32 sy = sins(c[1]);
register f32 cy = coss(c[1]);
register f32 sz = sins(c[2]);
register f32 cz = coss(c[2]);
dest[0][0] = cy * cz;
dest[0][1] = cy * sz;
register f32 sx = sins(c[0]);
register f32 cx = coss(c[0]);
register f32 sy = sins(c[1]);
register f32 cy = coss(c[1]);
register f32 sz = sins(c[2]);
register f32 cz = coss(c[2]);
register f32 cxsz = (cx * sz);
register f32 cxcz = (cx * cz);
register f32 sxsz = (sx * sz);
register f32 sxcz = (sx * cz);
dest[0][0] = (cy * cz);
dest[0][1] = (cy * sz);
dest[0][2] = -sy;
dest[1][0] = ((sxcz * sy) - cxsz);
dest[1][1] = ((sxsz * sy) + cxcz);
dest[1][2] = (sx * cy);
dest[2][0] = ((cxcz * sy) + sxsz);
dest[2][1] = ((cxsz * sy) - sxcz);
dest[2][2] = (cx * cy);
dest[0][3] = 0;
dest[1][0] = sx * sy * cz - cx * sz;
dest[1][1] = sx * sy * sz + cx * cz;
dest[1][2] = sx * cy;
dest[1][3] = 0;
dest[2][0] = cx * sy * cz + sx * sz;
dest[2][1] = cx * sy * sz - sx * cz;
dest[2][2] = cx * cy;
dest[2][3] = 0;
dest[3][0] = b[0];
dest[3][1] = b[1];
dest[3][2] = b[2];
vec3_copy(dest[3], b);
((u32 *) dest)[15] = 0x3F800000;
}
@@ -333,10 +354,17 @@ void mtxf_billboard(Mat4 dest, Mat4 mtx, Vec3f position, s32 angle) {
*temp = 0;
temp++;
}
dest[0][0] = coss(angle);
dest[0][1] = sins(angle);
dest[1][0] = -dest[0][1];
dest[1][1] = dest[0][0];
if (angle == 0x0) {
dest[0][0] = 1;
dest[0][1] = 0;
dest[1][0] = 0;
dest[1][1] = 1;
} else {
dest[0][0] = coss(angle);
dest[0][1] = sins(angle);
dest[1][0] = -dest[0][1];
dest[1][1] = dest[0][0];
}
((u32 *) dest)[10] = 0x3F800000;
dest[2][3] = 0;
((u32 *) dest)[15] = 0x3F800000;
@@ -344,7 +372,7 @@ void mtxf_billboard(Mat4 dest, Mat4 mtx, Vec3f position, s32 angle) {
temp = (f32 *)dest;
temp2 = (f32 *)mtx;
for (i = 0; i < 3; i++) {
temp[12] = temp2[0] * position[0] + temp2[4] * position[1] + temp2[8] * position[2] + temp2[12];
temp[12] = (temp2[0] * position[0]) + (temp2[4] * position[1]) + (temp2[8] * position[2]) + temp2[12];
temp++;
temp2++;
}
+15 -3
View File
@@ -5,9 +5,20 @@
#include "types.h"
#define NEAR_ZERO 0.00001f
#define NEARER_ZERO 0.000001f
#define NEAR_ONE 0.99999f
#define NEAR_ZERO __FLT_EPSILON__
#define NEAR_ONE (1.0f - __FLT_EPSILON__)
extern Vec3f gVec3fX;
extern Vec3f gVec3fY;
extern Vec3f gVec3fZ;
extern Vec3f gVec3fNX;
extern Vec3f gVec3fNY;
extern Vec3f gVec3fNZ;
extern Vec3f gVec3fZero;
extern Vec3s gVec3sZero;
extern Vec3i gVec3iZero;
extern Vec3f gVec3fOne;
extern Vec3s gVec3sOne;
/**
* Converts an angle in degrees to sm64's s16 angle units. For example, DEGREES(90) == 0x4000
@@ -433,6 +444,7 @@ extern f32 gSineTable[];
} \
}
f32 absf(f32 x);
s32 min_3i(s32 a0, s32 a1, s32 a2);
f32 min_3f(f32 a0, f32 a1, f32 a2);
s32 max_3i(s32 a0, s32 a1, s32 a2);
+109 -226
View File
@@ -5,7 +5,6 @@
#include "game/level_update.h"
#include "game/mario.h"
#include "game/object_list_processor.h"
// #include "game/rendering_graph_node.h"
#include "math_util.h"
#include "surface_collision.h"
#include "surface_load.h"
@@ -46,14 +45,6 @@ static s32 find_wall_collisions_from_list(struct SurfaceNode *surfaceNode, struc
register f32 margin_radius = (radius - 1.0f);
register TerrainData type = SURFACE_DEFAULT;
s32 numCols = 0;
// #if EXTENDED_BOUNDS_MODE
// const float down_scale = (1.0f / gWorldScale);
// radius *= down_scale;
// x *= down_scale;
// y *= down_scale;
// z *= down_scale;
// margin_radius *= down_scale;
// #endif
// Max collision radius = 200
if (radius > 200.0f) {
radius = 200.0f;
@@ -67,34 +58,22 @@ static s32 find_wall_collisions_from_list(struct SurfaceNode *surfaceNode, struc
if ((type == SURFACE_NEW_WATER) || (type == SURFACE_NEW_WATER_BOTTOM)) continue;
// Determine if checking for the camera or not.
if (gCheckingSurfaceCollisionsForCamera) {
if (surf->flags & SURFACE_FLAG_NO_CAM_COLLISION) {
continue;
}
if (surf->flags & SURFACE_FLAG_NO_CAM_COLLISION) continue;
} else {
// Ignore camera only surfaces.
if (type == SURFACE_CAMERA_BOUNDARY) {
continue;
}
if (type == SURFACE_CAMERA_BOUNDARY) continue;
// If an object can pass through a vanish cap wall, pass through.
if (type == SURFACE_VANISH_CAP_WALLS && gCurrentObject != NULL) {
// If an object can pass through a vanish cap wall, pass through.
if (gCurrentObject->activeFlags & ACTIVE_FLAG_MOVE_THROUGH_GRATE) {
continue;
}
if (gCurrentObject->activeFlags & ACTIVE_FLAG_MOVE_THROUGH_GRATE) continue;
// If Mario has a vanish cap, pass through the vanish cap wall.
if (gCurrentObject == gMarioObject && (gMarioState->flags & MARIO_VANISH_CAP)) {
continue;
}
if (gCurrentObject == gMarioObject && (gMarioState->flags & MARIO_VANISH_CAP)) continue;
}
}
if (pos[1] < surf->lowerY || pos[1] > surf->upperY) {
continue;
}
if (pos[1] < surf->lowerY || pos[1] > surf->upperY) continue;
// Dot of normal and pos, + origin offset
offset = (surf->normal.x * pos[0]) + (surf->normal.y * pos[1]) + (surf->normal.z * pos[2]) + surf->originOffset;
if (offset < -radius || offset > radius) {
continue;
}
if (offset < -radius || offset > radius) continue;
vec3_diff(v0, surf->vertex2, surf->vertex1);
vec3_diff(v1, surf->vertex3, surf->vertex1);
vec3_diff(v2, pos, surf->vertex1);
@@ -106,13 +85,9 @@ static s32 find_wall_collisions_from_list(struct SurfaceNode *surfaceNode, struc
d21 = vec3_dot(v2, v1);
invDenom = 1.0f / ((d00 * d11) - (d01 * d01));
v = ((d11 * d20) - (d01 * d21)) * invDenom;
if (v < 0.0f || v > 1.0f) {
goto edge_1_2;
}
w = (d00 * d21 - d01 * d20) * invDenom;
if (w < 0.0f || w > 1.0f || v + w > 1.0f) {
goto edge_1_2;
}
if (v < 0.0f || v > 1.0f) goto edge_1_2;
w = ((d00 * d21) - (d01 * d20)) * invDenom;
if (w < 0.0f || w > 1.0f || v + w > 1.0f) goto edge_1_2;
pos[0] += surf->normal.x * (radius - offset);
pos[2] += surf->normal.z * (radius - offset);
goto hasCollision;
@@ -130,9 +105,7 @@ static s32 find_wall_collisions_from_list(struct SurfaceNode *surfaceNode, struc
pos[0] += (d00 *= invDenom);
pos[2] += (d01 *= invDenom);
margin_radius += 0.01f;
if ((d00 * surf->normal.x) + (d01 * surf->normal.z) < (corner_threshold * offset)) {
continue;
}
if ((d00 * surf->normal.x) + (d01 * surf->normal.z) < (corner_threshold * offset)) continue;
hasCollision:
// (Unreferenced Walls) Since this only returns the first MAX_REFEREMCED_WALLS walls,
// this can lead to wall interaction being missed. Typically unreferenced walls
@@ -142,11 +115,6 @@ static s32 find_wall_collisions_from_list(struct SurfaceNode *surfaceNode, struc
}
numCols++;
}
// #if EXTENDED_BOUNDS_MODE
// x *= gWorldScale;
// y *= gWorldScale;
// z *= gWorldScale;
// #endif
data->x = pos[0];
data->z = pos[2];
return numCols;
@@ -157,23 +125,17 @@ static s32 find_wall_collisions_from_list(struct SurfaceNode *surfaceNode, struc
*/
s32 f32_find_wall_collision(f32 *xPtr, f32 *yPtr, f32 *zPtr, f32 offsetY, f32 radius) {
struct WallCollisionData collision;
s32 numCollisions = 0;
collision.offsetY = offsetY;
collision.radius = radius;
collision.x = *xPtr;
collision.y = *yPtr;
collision.z = *zPtr;
s32 numCollisions = 0;
collision.offsetY = offsetY;
collision.radius = radius;
collision.x = *xPtr;
collision.y = *yPtr;
collision.z = *zPtr;
collision.numWalls = 0;
numCollisions = find_wall_collisions(&collision);
*xPtr = collision.x;
*yPtr = collision.y;
*zPtr = collision.z;
numCollisions = find_wall_collisions(&collision);
*xPtr = collision.x;
*yPtr = collision.y;
*zPtr = collision.z;
return numCollisions;
}
@@ -186,9 +148,9 @@ s32 find_wall_collisions(struct WallCollisionData *colData) {
s32 numCollisions = 0;
s32 x = colData->x;
s32 z = colData->z;
#if PUPPYPRINT_DEBUG
#if PUPPYPRINT_DEBUG
OSTime first = osGetTime();
#endif
#endif
colData->numWalls = 0;
@@ -212,9 +174,9 @@ s32 find_wall_collisions(struct WallCollisionData *colData) {
// Increment the debug tracker.
gNumCalls.wall++;
#if PUPPYPRINT_DEBUG
collisionTime[perfIteration] += osGetTime()-first;
#endif
#if PUPPYPRINT_DEBUG
collisionTime[perfIteration] += osGetTime() - first;
#endif
return numCollisions;
}
@@ -229,7 +191,7 @@ void resolve_and_return_wall_collisions(Vec3f pos, f32 offset, f32 radius, struc
collisionData->radius = radius;
collisionData->offsetY = offset;
find_wall_collisions(collisionData);
find_wall_collisions(collisionData);
pos[0] = collisionData->x;
pos[1] = collisionData->y;
@@ -241,12 +203,12 @@ void resolve_and_return_wall_collisions(Vec3f pos, f32 offset, f32 radius, struc
**************************************************/
void add_ceil_margin(s32 *x, s32 *z, Vec3s target1, Vec3s target2, f32 margin) {
register f32 diff_x, diff_z, invDenom;
diff_x = target1[0] - *x + target2[0] - *x;
diff_z = target1[2] - *z + target2[2] - *z;
invDenom = margin / sqrtf(sqr(diff_x) + sqr(diff_z));
*x += diff_x * invDenom;
*z += diff_z * invDenom;
register f32 diff_x, diff_z, invDenom;
diff_x = target1[0] - *x + target2[0] - *x;
diff_z = target1[2] - *z + target2[2] - *z;
invDenom = margin / sqrtf(sqr(diff_x) + sqr(diff_z));
*x += diff_x * invDenom;
*z += diff_z * invDenom;
}
/**
@@ -263,64 +225,38 @@ static struct Surface *find_ceil_from_list(struct SurfaceNode *surfaceNode, s32
while (surfaceNode != NULL) {
surf = surfaceNode->surface;
surfaceNode = surfaceNode->next;
if (y > surf->upperY) {
continue;
}
if (y > surf->upperY) continue;
vx[0] = surf->vertex1[0];
vz[0] = surf->vertex1[2];
if (surf->type != SURFACE_HANGABLE) {
add_ceil_margin(&vx[0], &vz[0], surf->vertex2, surf->vertex3, margin);
}
if (surf->type != SURFACE_HANGABLE) add_ceil_margin(&vx[0], &vz[0], surf->vertex2, surf->vertex3, margin);
vx[1] = surf->vertex2[0];
vz[1] = surf->vertex2[2];
if (surf->type != SURFACE_HANGABLE) {
add_ceil_margin(&vx[1], &vz[1], surf->vertex3, surf->vertex1, margin);
}
if (surf->type != SURFACE_HANGABLE) add_ceil_margin(&vx[1], &vz[1], surf->vertex3, surf->vertex1, margin);
// Checking if point is in bounds of the triangle laterally.
if ((vz[0] - z) * (vx[1] - vx[0]) - (vx[0] - x) * (vz[1] - vz[0]) > 0) {
continue;
}
if ((vz[0] - z) * (vx[1] - vx[0]) - (vx[0] - x) * (vz[1] - vz[0]) > 0) continue;
// Slight optimization by checking these later.
vx[2] = surf->vertex3[0];
vz[2] = surf->vertex3[2];
if (surf->type != SURFACE_HANGABLE) {
add_ceil_margin(&vx[2], &vz[2], surf->vertex1, surf->vertex2, margin);
}
if ((vz[1] - z) * (vx[2] - vx[1]) - (vx[1] - x) * (vz[2] - vz[1]) > 0) {
continue;
}
if ((vz[2] - z) * (vx[0] - vx[2]) - (vx[2] - x) * (vz[0] - vz[2]) > 0) {
continue;
}
if (surf->type != SURFACE_HANGABLE) add_ceil_margin(&vx[2], &vz[2], surf->vertex1, surf->vertex2, margin);
if ((vz[1] - z) * (vx[2] - vx[1]) - (vx[1] - x) * (vz[2] - vz[1]) > 0) continue;
if ((vz[2] - z) * (vx[0] - vx[2]) - (vx[2] - x) * (vz[0] - vz[2]) > 0) continue;
// Determine if checking for the camera or not.
if (surf->type == SURFACE_NEW_WATER || surf->type == SURFACE_NEW_WATER_BOTTOM) {
continue;
}
if (surf->type == SURFACE_NEW_WATER || surf->type == SURFACE_NEW_WATER_BOTTOM) continue;
if (gCheckingSurfaceCollisionsForCamera) {
if (surf->flags & SURFACE_FLAG_NO_CAM_COLLISION) {
continue;
}
if (surf->flags & SURFACE_FLAG_NO_CAM_COLLISION) continue;
} else if (surf->type == SURFACE_CAMERA_BOUNDARY) {
// Ignore camera only surfaces.
continue;
}
// Find the ceil height at the specific point.
height = get_surface_height_at_location(x, z, surf);
if (height > *pheight) {
continue;
}
if (height > *pheight) continue;
// Checks for ceiling interaction
if (y > height) {
continue;
}
if (y >= surf->upperY) {
continue;
}
if (y > height) continue;
if (y >= surf->upperY) continue;
*pheight = height;
ceil = surf;
if (height == y) {
break;
}
if (height == y) break;
}
return ceil;
}
@@ -335,10 +271,9 @@ f32 find_ceil(f32 posX, f32 posY, f32 posZ, struct Surface **pceil) {
f32 height = CELL_HEIGHT_LIMIT;
f32 dynamicHeight = CELL_HEIGHT_LIMIT;
s32 x, y, z;
#if PUPPYPRINT_DEBUG
#if PUPPYPRINT_DEBUG
OSTime first = osGetTime();
#endif
#endif
x = posX;
y = posY;
z = posZ;
@@ -370,9 +305,9 @@ f32 find_ceil(f32 posX, f32 posY, f32 posZ, struct Surface **pceil) {
// Increment the debug tracker.
gNumCalls.ceil++;
#if PUPPYPRINT_DEBUG
collisionTime[perfIteration] += osGetTime()-first;
#endif
#if PUPPYPRINT_DEBUG
collisionTime[perfIteration] += osGetTime() - first;
#endif
return height;
}
@@ -403,49 +338,33 @@ static struct Surface *find_floor_from_list(struct SurfaceNode *surfaceNode, s32
while (surfaceNode != NULL) {
surf = surfaceNode->surface;
surfaceNode = surfaceNode->next;
if (y < surf->lowerY - 30) {
continue;
}
if (y < (surf->lowerY - 30)) continue;
vx[0] = surf->vertex1[0];
vz[0] = surf->vertex1[2];
vx[1] = surf->vertex2[0];
vz[1] = surf->vertex2[2];
// Check that the point is within the triangle bounds.
if ((vz[0] - z) * (vx[1] - vx[0]) - (vx[0] - x) * (vz[1] - vz[0]) < 0) {
continue;
}
if ((vz[0] - z) * (vx[1] - vx[0]) - (vx[0] - x) * (vz[1] - vz[0]) < 0) continue;
// To slightly save on computation time, set this later.
vx[2] = surf->vertex3[0];
vz[2] = surf->vertex3[2];
if ((vz[1] - z) * (vx[2] - vx[1]) - (vx[1] - x) * (vz[2] - vz[1]) < 0) {
continue;
}
if ((vz[2] - z) * (vx[0] - vx[2]) - (vx[2] - x) * (vz[0] - vz[2]) < 0) {
continue;
}
if ((vz[1] - z) * (vx[2] - vx[1]) - (vx[1] - x) * (vz[2] - vz[1]) < 0) continue;
if ((vz[2] - z) * (vx[0] - vx[2]) - (vx[2] - x) * (vz[0] - vz[2]) < 0) continue;
// Determine if we are checking for the camera or not.
if (gCheckingSurfaceCollisionsForCamera != 0) {
if (surf->flags & SURFACE_FLAG_NO_CAM_COLLISION || surf->type == SURFACE_NEW_WATER || surf->type == SURFACE_NEW_WATER_BOTTOM) {
continue;
}
if (surf->flags & SURFACE_FLAG_NO_CAM_COLLISION || surf->type == SURFACE_NEW_WATER || surf->type == SURFACE_NEW_WATER_BOTTOM) continue;
} else if (surf->type == SURFACE_CAMERA_BOUNDARY) {
// If we are not checking for the camera, ignore camera only floors.
continue;
}
// Find the height of the floor at a given location.
height = get_surface_height_at_location(x, z, surf);
if (height < *pheight) {
continue;
}
if (height < *pheight) continue;
// Checks for floor interaction with a 78 unit buffer.
if (y < (height - 78.0f)) {
continue;
}
if (y < (height - 78.0f)) continue;
*pheight = height;
floor = surf;
if (height - 78.0f == y) {
break;
}
if ((height - 78.0f) == y) break;
}
return floor;
}
@@ -496,7 +415,7 @@ struct Surface *find_water_floor_from_list(struct SurfaceNode *surfaceNode, s32
surf = topSurfaceNode->surface;
topSurfaceNode = topSurfaceNode->next;
if (surf->type == SURFACE_NEW_WATER_BOTTOM || !check_within_triangle_bounds(x, z, surf)) continue;
if ((surf->type == SURFACE_NEW_WATER_BOTTOM) || !check_within_triangle_bounds(x, z, surf)) continue;
curHeight = get_surface_height_at_location(x, z, surf);
@@ -517,10 +436,7 @@ struct Surface *find_water_floor_from_list(struct SurfaceNode *surfaceNode, s32
*/
f32 find_floor_height(f32 x, f32 y, f32 z) {
struct Surface *floor;
f32 floorHeight = find_floor(x, y, z, &floor);
return floorHeight;
return find_floor(x, y, z, &floor);
}
/**
@@ -554,9 +470,9 @@ f32 unused_find_dynamic_floor(f32 xPos, f32 yPos, f32 zPos, struct Surface **pfl
*/
f32 find_floor(f32 xPos, f32 yPos, f32 zPos, struct Surface **pfloor) {
s32 cellZ, cellX;
#if PUPPYPRINT_DEBUG
#if PUPPYPRINT_DEBUG
OSTime first = osGetTime();
#endif
#endif
struct Surface *floor, *dynamicFloor;
struct SurfaceNode *surfaceList;
@@ -574,9 +490,9 @@ f32 find_floor(f32 xPos, f32 yPos, f32 zPos, struct Surface **pfloor) {
*pfloor = NULL;
if (is_outside_level_bounds(x, z)) {
#if PUPPYPRINT_DEBUG
#if PUPPYPRINT_DEBUG
collisionTime[perfIteration] += osGetTime() - first;
#endif
#endif
return height;
}
@@ -645,9 +561,7 @@ f32 find_water_floor(s32 xPos, s32 yPos, s32 zPos, struct Surface **pfloor) {
s32 y = yPos;
s32 z = zPos;
if (is_outside_level_bounds(x, z)) {
return height;
}
if (is_outside_level_bounds(x, z)) return height;
// Each level is split into cells to limit load, find the appropriate cell.
cellX = ((x + LEVEL_BOUNDARY_MAX) / CELL_SIZE) & NUM_CELLS_INDEX;
@@ -681,15 +595,10 @@ s32 find_water_level_and_floor(s32 x, s32 z, struct Surface **pfloor) {
s32 waterLevel = FLOOR_LOWER_LIMIT;
TerrainData *p = gEnvironmentRegions;
struct Surface *floor = NULL;
#if PUPPYPRINT_DEBUG
#if PUPPYPRINT_DEBUG
OSTime first = osGetTime();
#endif
if (gCheckingSurfaceCollisionsForCamera) {
waterLevel = find_water_floor(x, gLakituState.pos[1], z, &floor);
} else {
waterLevel = find_water_floor(x, gMarioState->pos[1], z, &floor);
}
#endif
waterLevel = find_water_floor(x, (gCheckingSurfaceCollisionsForCamera ? gLakituState.pos[1] : gMarioState->pos[1]), z, &floor);
if (p != NULL && waterLevel == FLOOR_LOWER_LIMIT) {
numRegions = *p++;
@@ -714,10 +623,9 @@ s32 find_water_level_and_floor(s32 x, s32 z, struct Surface **pfloor) {
*pfloor = floor;
}
#if PUPPYPRINT_DEBUG
collisionTime[perfIteration] += osGetTime()-first;
#endif
#if PUPPYPRINT_DEBUG
collisionTime[perfIteration] += osGetTime() - first;
#endif
return waterLevel;
}
@@ -763,9 +671,9 @@ s32 find_water_level(s32 x, s32 z) {
}
}
#if PUPPYPRINT_DEBUG
collisionTime[perfIteration] += osGetTime()-first;
#endif
#if PUPPYPRINT_DEBUG
collisionTime[perfIteration] += osGetTime() - first;
#endif
return waterLevel;
}
@@ -780,9 +688,9 @@ s32 find_poison_gas_level(s32 x, s32 z) {
s32 loX, hiX, loZ, hiZ;
s32 gasLevel = FLOOR_LOWER_LIMIT;
TerrainData *p = gEnvironmentRegions;
#if PUPPYPRINT_DEBUG
#if PUPPYPRINT_DEBUG
OSTime first = osGetTime();
#endif
#endif
if (p != NULL) {
numRegions = *p++;
@@ -809,9 +717,9 @@ s32 find_poison_gas_level(s32 x, s32 z) {
}
}
#if PUPPYPRINT_DEBUG
collisionTime[perfIteration] += osGetTime()-first;
#endif
#if PUPPYPRINT_DEBUG
collisionTime[perfIteration] += osGetTime() - first;
#endif
return gasLevel;
}
@@ -825,12 +733,10 @@ s32 find_poison_gas_level(s32 x, s32 z) {
*/
static s32 surface_list_length(struct SurfaceNode *list) {
s32 count = 0;
while (list != NULL) {
list = list->next;
count++;
}
return count;
}
@@ -946,51 +852,34 @@ s32 ray_surface_intersect(Vec3f orig, Vec3f dir, f32 dir_length, struct Surface
f32 a, f, u, v;
Vec3f add_dir;
Vec3f norm;
//Ignore certain surface types.
if (surface->type == SURFACE_INTANGIBLE || surface->flags & SURFACE_FLAG_NO_CAM_COLLISION)
return FALSE;
// Ignore certain surface types.
if (surface->type == SURFACE_INTANGIBLE || surface->flags & SURFACE_FLAG_NO_CAM_COLLISION) return FALSE;
// Get surface normal and some other stuff
vec3_set(norm, 0, surface->normal.y, 0);
vec3_mul_val(norm, RAY_OFFSET);
vec3_copy(v0, surface->vertex1);
vec3_copy(v1, surface->vertex2);
vec3_copy(v2, surface->vertex3);
vec3_add(v0, norm);
vec3_add(v1, norm);
vec3_add(v2, norm);
vec3_add( v0, norm);
vec3_add( v1, norm);
vec3_add( v2, norm);
vec3_diff(e1, v1, v0);
vec3_diff(e2, v2, v0);
vec3_cross(h, dir, e2);
// Check if we're perpendicular from the surface
a = vec3_dot(e1, h);
if (a > -0.00001f && a < 0.00001f) {
return FALSE;
}
if ((a > -NEAR_ZERO) && (a < NEAR_ZERO)) return FALSE;
// Check if we're making contact with the surface
f = 1.0f / a;
vec3_diff(s, orig, v0);
u = f * vec3_dot(s, h);
if (u < 0.0f || u > 1.0f) {
return FALSE;
}
if ((u < 0.0f) || (u > 1.0f)) return FALSE;
vec3_cross(q, s, e1);
v = f * vec3_dot(dir, q);
if (v < 0.0f || u + v > 1.0f) {
return FALSE;
}
if ((v < 0.0f) || ((u + v) > 1.0f)) return FALSE;
// Get the length between our origin and the surface contact point
*length = f * vec3_dot(e2, q);
if (*length <= 0.00001 || *length > dir_length) {
return FALSE;
}
if (*length <= 0.00001 || *length > dir_length) return FALSE;
// Successful contact
vec3f_copy(add_dir, dir);
vec3_mul_val(add_dir, *length);
@@ -1009,42 +898,38 @@ void find_surface_on_ray_list(struct SurfaceNode *list, Vec3f orig, Vec3f dir, f
// Get upper and lower bounds of ray
if (dir[1] >= 0.0f) {
top = orig[1] + dir[1] * dir_length;
top = (orig[1] + (dir[1] * dir_length));
bottom = orig[1];
} else {
top = orig[1];
bottom = orig[1] + dir[1] * dir_length;
top = orig[1];
bottom = (orig[1] + (dir[1] * dir_length));
}
// Iterate through every surface of the list
for (; list != NULL; list = list->next) {
// Reject surface if out of vertical bounds
if (list->surface->lowerY > top || list->surface->upperY < bottom) {
continue;
}
if (list->surface->lowerY > top || list->surface->upperY < bottom) continue;
// Check intersection between the ray and this surface
if ((hit = ray_surface_intersect(orig, dir, dir_length, list->surface, chk_hit_pos, &length)) != 0) {
if (length <= *max_length) {
*hit_surface = list->surface;
vec3f_copy(hit_pos, chk_hit_pos);
*max_length = length;
}
if ((hit = ray_surface_intersect(orig, dir, dir_length, list->surface, chk_hit_pos, &length)) && (length <= *max_length)) {
*hit_surface = list->surface;
vec3f_copy(hit_pos, chk_hit_pos);
*max_length = length;
}
}
#if PUPPYPRINT_DEBUG
collisionTime[perfIteration] += osGetTime()-first;
#endif
#if PUPPYPRINT_DEBUG
collisionTime[perfIteration] += (osGetTime() - first);
#endif
}
void find_surface_on_ray_cell(s32 cellX, s32 cellZ, Vec3f orig, Vec3f normalized_dir, f32 dir_length, struct Surface **hit_surface, Vec3f hit_pos, f32 *max_length, s32 flags) {
// Skip if OOB
if (cellX >= 0 && cellX <= (NUM_CELLS - 1) && cellZ >= 0 && cellZ <= (NUM_CELLS - 1)) {
// Iterate through each surface in this partition
if (normalized_dir[1] > -0.99999f && flags & RAYCAST_FIND_CEIL) {
if ((normalized_dir[1] > -NEAR_ONE) && (flags & RAYCAST_FIND_CEIL)) {
find_surface_on_ray_list(gStaticSurfacePartition[cellZ][cellX][SPATIAL_PARTITION_CEILS].next, orig, normalized_dir, dir_length, hit_surface, hit_pos, max_length);
find_surface_on_ray_list(gDynamicSurfacePartition[cellZ][cellX][SPATIAL_PARTITION_CEILS].next, orig, normalized_dir, dir_length, hit_surface, hit_pos, max_length);
}
if (normalized_dir[1] < 0.99999f && flags & RAYCAST_FIND_FLOOR) {
if ((normalized_dir[1] < NEAR_ONE) && (flags & RAYCAST_FIND_FLOOR)) {
find_surface_on_ray_list(gStaticSurfacePartition[cellZ][cellX][SPATIAL_PARTITION_FLOORS].next, orig, normalized_dir, dir_length, hit_surface, hit_pos, max_length);
find_surface_on_ray_list(gDynamicSurfacePartition[cellZ][cellX][SPATIAL_PARTITION_FLOORS].next, orig, normalized_dir, dir_length, hit_surface, hit_pos, max_length);
}
@@ -1060,12 +945,10 @@ void find_surface_on_ray_cell(s32 cellX, s32 cellZ, Vec3f orig, Vec3f normalized
}
void find_surface_on_ray(Vec3f orig, Vec3f dir, struct Surface **hit_surface, Vec3f hit_pos, s32 flags) {
f32 max_length;
s32 cellZ, cellX, cellPrevX, cellPrevZ;
f32 fCellZ, fCellX;
f32 dir_length;
Vec3f normalized_dir;
f32 step, dx, dz;
f32 step;
s32 i;
// Set that no surface has been hit
@@ -1073,8 +956,8 @@ void find_surface_on_ray(Vec3f orig, Vec3f dir, struct Surface **hit_surface, Ve
vec3_sum(hit_pos, orig, dir);
// Get normalized direction
dir_length = vec3_mag(dir);
max_length = dir_length;
f32 dir_length = vec3_mag(dir);
f32 max_length = dir_length;
vec3f_copy(normalized_dir, dir);
vec3f_normalize(normalized_dir);
@@ -1093,13 +976,13 @@ void find_surface_on_ray(Vec3f orig, Vec3f dir, struct Surface **hit_surface, Ve
}
// Get cells we cross using DDA
if (ABS(dir[0]) >= ABS(dir[2]))
if (ABS(dir[0]) >= ABS(dir[2])) {
step = RAY_STEPS * ABS(dir[0]) / CELL_SIZE;
else
} else {
step = RAY_STEPS * ABS(dir[2]) / CELL_SIZE;
dx = dir[0] / step / CELL_SIZE;
dz = dir[2] / step / CELL_SIZE;
}
f32 dx = dir[0] / step / CELL_SIZE;
f32 dz = dir[2] / step / CELL_SIZE;
for (i = 0; i < step && *hit_surface == NULL; i++) {
find_surface_on_ray_cell(cellX, cellZ, orig, normalized_dir, dir_length, hit_surface, hit_pos, &max_length, flags);