Skip to content

Commit

Permalink
Use struct returns in olib (zeldaret/oot#1574)
Browse files Browse the repository at this point in the history
  • Loading branch information
cadmic committed Nov 18, 2023
1 parent f8c2b18 commit aebd6f2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 69 deletions.
39 changes: 14 additions & 25 deletions src/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "sys_math.hpp"

#define ABS std::abs
#define CAM_DATA_SCALED(x) ((x)*0.01f)
#define CAM_DATA_SCALED(x) ((x) * 0.01f)

// a - b without implementation-defined overflow behavior
s16 angleDiff(u16 a, u16 b) {
Expand Down Expand Up @@ -117,7 +117,7 @@ Vec3f* Camera_AddVecGeoToVec3f(Vec3f* dest, Vec3f* a, VecGeo* geo) {
Vec3f sum;
Vec3f b;

OLib_VecGeoToVec3f(&b, geo);
b = OLib_VecGeoToVec3f(geo);

sum.x = a->x + b.x;
sum.y = a->y + b.y;
Expand All @@ -130,8 +130,7 @@ Vec3f* Camera_AddVecGeoToVec3f(Vec3f* dest, Vec3f* a, VecGeo* geo) {

bool Camera_BGCheckInfo(Camera* camera, Vec3f from, Vec3f to, Vec3f* result,
Vec3f* normal) {
VecGeo fromToOffset;
OLib_Vec3fDiffToVecGeo(&fromToOffset, &from, &to);
VecGeo fromToOffset = OLib_Vec3fDiffToVecGeo(&from, &to);
fromToOffset.r += 8.0f;

Vec3f toPoint;
Expand All @@ -146,8 +145,7 @@ bool Camera_BGCheckInfo(Camera* camera, Vec3f from, Vec3f to, Vec3f* result,
return true;
} else {
// TODO: check floors
Vec3f fromToNorm;
OLib_Vec3fDistNormalize(&fromToNorm, &from, &to);
Vec3f fromToNorm = OLib_Vec3fDistNormalize(&from, &to);
*normal = fromToNorm * -1.0f;
*result = to + *normal;
return false;
Expand Down Expand Up @@ -344,12 +342,10 @@ void Camera::initParallel(Vec3f pos, u16 angle, int setting) {
.yaw = (s16)(angle - 0x7fff),
};

Vec3f dir;
OLib_VecGeoToVec3f(&dir, &target);
Vec3f dir = OLib_VecGeoToVec3f(&target);
this->eyeNext = this->at + dir;

Vec3f norm;
OLib_Vec3fDistNormalize(&norm, &this->at, &this->eyeNext);
Vec3f norm = OLib_Vec3fDistNormalize(&this->at, &this->eyeNext);
this->eye = this->eyeNext - norm;

this->xzSpeed = 0;
Expand Down Expand Up @@ -400,11 +396,9 @@ void Camera_Normal1(Camera* camera, Vec3f pos, u16 angle, int setting) {
camera->normalRUpdateRateTimer--;
}

VecGeo atEyeGeo;
OLib_Vec3fDiffToVecGeo(&atEyeGeo, &camera->at, &camera->eye);
VecGeo atEyeGeo = OLib_Vec3fDiffToVecGeo(&camera->at, &camera->eye);

VecGeo atEyeNextGeo;
OLib_Vec3fDiffToVecGeo(&atEyeNextGeo, &camera->at, &camera->eyeNext);
VecGeo atEyeNextGeo = OLib_Vec3fDiffToVecGeo(&camera->at, &camera->eyeNext);

s16 slopePitchTarget =
Camera_GetPitchAdjFromFloorHeightDiffs(camera, atEyeGeo.yaw - 0x7FFF);
Expand All @@ -416,8 +410,7 @@ void Camera_Normal1(Camera* camera, Vec3f pos, u16 angle, int setting) {

Camera_CalcAtDefault(camera, yOffset);

VecGeo eyeAdjustment;
OLib_Vec3fDiffToVecGeo(&eyeAdjustment, &camera->at, &camera->eyeNext);
VecGeo eyeAdjustment = OLib_Vec3fDiffToVecGeo(&camera->at, &camera->eyeNext);

Camera_ClampDist(camera, eyeAdjustment.r, distMin, distMax,
camera->normalRUpdateRateTimer);
Expand Down Expand Up @@ -462,8 +455,7 @@ void Camera_Normal1(Camera* camera, Vec3f pos, u16 angle, int setting) {
// TODO: check both ways?
if (Camera_BGCheckInfo(camera, camera->at, camera->eyeNext, &collisionPoint,
&collisionNormal)) {
VecGeo geoNorm;
OLib_Vec3fToVecGeo(&geoNorm, &collisionNormal);
VecGeo geoNorm = OLib_Vec3fToVecGeo(&collisionNormal);
if (geoNorm.pitch >= 0x2EE1) {
geoNorm.yaw = eyeAdjustment.yaw;
}
Expand Down Expand Up @@ -511,11 +503,9 @@ void Camera_Parallel1(Camera* camera, Vec3f pos, u16 angle, int setting) {
camera->parallelYawTarget = angle - 0x7FFF;
}

VecGeo atEyeGeo;
OLib_Vec3fDiffToVecGeo(&atEyeGeo, &camera->at, &camera->eye);
VecGeo atEyeGeo = OLib_Vec3fDiffToVecGeo(&camera->at, &camera->eye);

VecGeo atEyeNextGeo;
OLib_Vec3fDiffToVecGeo(&atEyeNextGeo, &camera->at, &camera->eyeNext);
VecGeo atEyeNextGeo = OLib_Vec3fDiffToVecGeo(&camera->at, &camera->eyeNext);

camera->rUpdateRateInv = Camera_LERPCeilF(20.0f, camera->rUpdateRateInv,
camera->speedRatio * 0.5f, 0.1f);
Expand All @@ -542,7 +532,7 @@ void Camera_Parallel1(Camera* camera, Vec3f pos, u16 angle, int setting) {
} else {
camera->dist = Camera_LERPCeilF(distTarget, camera->dist,
1.0f / camera->rUpdateRateInv, 2.0f);
OLib_Vec3fDiffToVecGeo(&eyeAdjustment, &camera->at, &camera->eyeNext);
eyeAdjustment = OLib_Vec3fDiffToVecGeo(&camera->at, &camera->eyeNext);
eyeAdjustment.r = camera->dist;
eyeAdjustment.yaw =
Camera_LERPCeilS(camera->parallelYawTarget, atEyeNextGeo.yaw, 0.8f, 10);
Expand Down Expand Up @@ -603,7 +593,6 @@ void Camera::updateParallel(Vec3f pos, u16 angle, int setting) {
}

u16 Camera::yaw() {
VecGeo result;
OLib_Vec3fDiffToVecGeo(&result, &this->eye, &this->at);
VecGeo result = OLib_Vec3fDiffToVecGeo(&this->eye, &this->at);
return result.yaw;
}
60 changes: 23 additions & 37 deletions src/olib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ f32 OLib_ClampMaxDist(f32 val, f32 max) {
/**
* Takes the difference of points b and a, and creates a normal vector
*/
Vec3f* OLib_Vec3fDistNormalize(Vec3f* dest, Vec3f* a, Vec3f* b) {
Vec3f OLib_Vec3fDistNormalize(Vec3f* a, Vec3f* b) {
Vec3f v1;
Vec3f v2;
f32 dist;
Expand All @@ -68,15 +68,13 @@ Vec3f* OLib_Vec3fDistNormalize(Vec3f* dest, Vec3f* a, Vec3f* b) {
v2.y = v1.y / dist;
v2.z = v1.z / dist;

*dest = v2;

return dest;
return v2;
}

/**
* Takes the spherical coordinate `sph`, and converts it into a x,y,z position
*/
Vec3f* OLib_VecSphToVec3f(Vec3f* dest, VecSph* sph) {
Vec3f OLib_VecSphToVec3f(VecSph* sph) {
Vec3f v;
f32 sinPitch;
f32 cosPitch = Math_CosS(sph->pitch);
Expand All @@ -90,28 +88,26 @@ Vec3f* OLib_VecSphToVec3f(Vec3f* dest, VecSph* sph) {
v.y = sph->r * cosPitch;
v.z = sph->r * sinPitch * cosYaw;

*dest = v;

return dest;
return v;
}

/**
* Takes the geographic point `geo` and converts it into a x,y,z position
*/
Vec3f* OLib_VecGeoToVec3f(Vec3f* dest, VecGeo* geo) {
Vec3f OLib_VecGeoToVec3f(VecGeo* geo) {
VecSph sph;

sph.r = geo->r;
sph.pitch = 0x3FFF - geo->pitch;
sph.yaw = geo->yaw;

return OLib_VecSphToVec3f(dest, &sph);
return OLib_VecSphToVec3f(&sph);
}

/**
* Takes the point `vec`, and converts it into a spherical coordinate
*/
VecSph* OLib_Vec3fToVecSph(VecSph* dest, Vec3f* vec) {
VecSph OLib_Vec3fToVecSph(Vec3f* vec) {
VecSph sph;
f32 distXZSq = SQ(vec->x) + SQ(vec->z);
f32 distXZ = sqrtf(distXZSq);
Expand All @@ -129,101 +125,91 @@ VecSph* OLib_Vec3fToVecSph(VecSph* dest, Vec3f* vec) {
sph.yaw = CAM_DEG_TO_BINANG(RAD_TO_DEG(Math_FAtan2F(vec->x, vec->z)));
}

*dest = sph;

return dest;
return sph;
}

/**
* Takes the point `vec`, and converts it to a geographic coordinate
*/
VecGeo* OLib_Vec3fToVecGeo(VecGeo* dest, Vec3f* vec) {
VecGeo OLib_Vec3fToVecGeo(Vec3f* vec) {
VecSph sph;

OLib_Vec3fToVecSph(&sph, vec);
sph = OLib_Vec3fToVecSph(vec);
sph.pitch = 0x3FFF - sph.pitch;

*dest = sph;

return dest;
return sph;
}

/**
* Takes the differences of positions `a` and `b`, and converts them to
* spherical coordinates
*/
VecSph* OLib_Vec3fDiffToVecSph(VecSph* dest, Vec3f* a, Vec3f* b) {
VecSph OLib_Vec3fDiffToVecSph(Vec3f* a, Vec3f* b) {
Vec3f diff;

diff.x = b->x - a->x;
diff.y = b->y - a->y;
diff.z = b->z - a->z;

return OLib_Vec3fToVecSph(dest, &diff);
return OLib_Vec3fToVecSph(&diff);
}

/**
* Takes the difference of positions `a` and `b`, and converts them to
* geographic coordinates
*/
VecGeo* OLib_Vec3fDiffToVecGeo(VecGeo* dest, Vec3f* a, Vec3f* b) {
VecGeo OLib_Vec3fDiffToVecGeo(Vec3f* a, Vec3f* b) {
Vec3f diff;

diff.x = b->x - a->x;
diff.y = b->y - a->y;
diff.z = b->z - a->z;

return OLib_Vec3fToVecGeo(dest, &diff);
return OLib_Vec3fToVecGeo(&diff);
}

/**
* Gets the pitch/yaw of the vector formed from `b`-`a`, result is in radians
*/
Vec3f* OLib_Vec3fDiffRad(Vec3f* dest, Vec3f* a, Vec3f* b) {
Vec3f OLib_Vec3fDiffRad(Vec3f* a, Vec3f* b) {
Vec3f anglesRad;

anglesRad.x = Math_FAtan2F(b->z - a->z, b->y - a->y);
anglesRad.y = Math_FAtan2F(b->x - a->x, b->z - a->z);
anglesRad.z = 0;

*dest = anglesRad;

return dest;
return anglesRad;
}

/**
* Gets the pitch/yaw of the vector formed from `b`-`a`, result is in degrees
*/
Vec3f* OLib_Vec3fDiffDegF(Vec3f* dest, Vec3f* a, Vec3f* b) {
Vec3f OLib_Vec3fDiffDegF(Vec3f* a, Vec3f* b) {
Vec3f anglesRad;
Vec3f anglesDegrees;

OLib_Vec3fDiffRad(&anglesRad, a, b);
anglesRad = OLib_Vec3fDiffRad(a, b);

anglesDegrees.x = RAD_TO_DEG(anglesRad.x);
anglesDegrees.y = RAD_TO_DEG(anglesRad.y);
anglesDegrees.z = 0.0f;

*dest = anglesDegrees;

return dest;
return anglesDegrees;
}

/**
* Gets the pitch/yaw of the vector formed from `b`-`a`, result is in binary
* degrees
*/
Vec3s* OLib_Vec3fDiffBinAng(Vec3s* dest, Vec3f* a, Vec3f* b) {
Vec3s OLib_Vec3fDiffBinAng(Vec3f* a, Vec3f* b) {
Vec3f anglesRad;
Vec3s anglesBinAng;

OLib_Vec3fDiffRad(&anglesRad, a, b);
anglesRad = OLib_Vec3fDiffRad(a, b);

anglesBinAng.x = CAM_DEG_TO_BINANG(RAD_TO_DEG(anglesRad.x));
anglesBinAng.y = CAM_DEG_TO_BINANG(RAD_TO_DEG(anglesRad.y));
anglesBinAng.z = 0.0f;

*dest = anglesBinAng;

return dest;
return anglesBinAng;
}
14 changes: 7 additions & 7 deletions src/olib.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "global.hpp"

#define CAM_DEG_TO_BINANG(degrees) (s16)(s32)((degrees)*182.04167f + .5f)
#define CAM_DEG_TO_BINANG(degrees) (s16)(s32)((degrees) * 182.04167f + .5f)
#define RAD_TO_DEG(radians) ((radians) * (180.0f / M_PI))

struct VecSphGeo {
Expand All @@ -23,9 +23,9 @@ f32 OLib_Vec3fDist(Vec3f* a, Vec3f* b);
f32 OLib_Vec3fDistXZ(Vec3f* a, Vec3f* b);
f32 OLib_ClampMinDist(f32 val, f32 min);
f32 OLib_ClampMaxDist(f32 val, f32 max);
Vec3f* OLib_Vec3fDistNormalize(Vec3f* dest, Vec3f* a, Vec3f* b);
Vec3f* OLib_VecGeoToVec3f(Vec3f* dest, VecGeo* geo);
VecSph* OLib_Vec3fToVecSph(VecSph* dest, Vec3f* vec);
VecGeo* OLib_Vec3fToVecGeo(VecGeo* dest, Vec3f* vec);
VecGeo* OLib_Vec3fDiffToVecGeo(VecGeo* dest, Vec3f* a, Vec3f* b);
Vec3f* OLib_Vec3fDiffRad(Vec3f* dest, Vec3f* a, Vec3f* b);
Vec3f OLib_Vec3fDistNormalize(Vec3f* a, Vec3f* b);
Vec3f OLib_VecGeoToVec3f(VecGeo* geo);
VecSph OLib_Vec3fToVecSph(Vec3f* vec);
VecGeo OLib_Vec3fToVecGeo(Vec3f* vec);
VecGeo OLib_Vec3fDiffToVecGeo(Vec3f* a, Vec3f* b);
Vec3f OLib_Vec3fDiffRad(Vec3f* a, Vec3f* b);

0 comments on commit aebd6f2

Please sign in to comment.