From 00ececa0b583cc7893d37898beab2d2ae3f36b7a Mon Sep 17 00:00:00 2001 From: elijah-thomas774 Date: Fri, 21 Mar 2025 19:38:30 -0400 Subject: [PATCH] Egg Camera a small bit closer --- include/egg/gfx/eggCamera.h | 23 +++++++++------ include/egg/math/eggRotation.h | 50 ++++++++++++++++++++++++++++++++ include/egg/math/eggVector.h | 26 +++++------------ src/egg/gfx/eggCamera.cpp | 53 ++++++++++++++++++++++++---------- 4 files changed, 109 insertions(+), 43 deletions(-) create mode 100644 include/egg/math/eggRotation.h diff --git a/include/egg/gfx/eggCamera.h b/include/egg/gfx/eggCamera.h index 220d91c7..c3104fb7 100644 --- a/include/egg/gfx/eggCamera.h +++ b/include/egg/gfx/eggCamera.h @@ -2,6 +2,7 @@ #define EGG_CAMERA_H #include "egg/math/eggMatrix.h" +#include "egg/math/eggRotation.h" #include "egg/math/eggVector.h" #include "nw4r/g3d/g3d_camera.h" @@ -10,7 +11,6 @@ namespace EGG { class BaseCamera { public: BaseCamera() {} - // Which way around? virtual Matrix34f &getViewMatrix() = 0; virtual const Matrix34f &getViewMatrix() const = 0; virtual void updateMatrix(); @@ -23,11 +23,18 @@ public: virtual Matrix34f &getViewMatrixOld() = 0; void setG3DCamera(nw4r::g3d::Camera &); + // Vector3f getRightVector(); + // Vector3f getLookVector(); + // Vector3f getUpVector(); + +protected: + /* 0x04 */ Matrix34f mViewMtx; + /* 0x34 */ Matrix34f mOtherMtx; }; class LookAtCamera : public BaseCamera { public: - LookAtCamera() : mPosition(0.0f, 10.0f, 0.0f), mRight(0.0f, 0.0f, 0.0f), mUp(0.0f, 1.0f, 0.0f) {} + LookAtCamera() : mPos(0.0f, 10.0f, 0.0f), mAt(0.0f, 0.0f, 0.0f), mUp(0.0f, 1.0f, 0.0f) {} // Which way around? virtual Matrix34f &getViewMatrix() override { return mViewMtx; @@ -40,17 +47,15 @@ public: virtual void loadMatrix() override; virtual void loadOldMatrix() override; virtual EGG::Vector3f getPosition() override { - return mPosition; + return mPos; } virtual void doDraw() override; virtual Matrix34f &getViewMatrixOld() override; protected: - /* 0x04 */ Matrix34f mViewMtx; - /* 0x34 */ Matrix34f mOtherMtx; - /* 0x64 */ Vector3f mPosition; - /* 0x70 */ Vector3f mRight; + /* 0x64 */ Vector3f mPos; + /* 0x70 */ Vector3f mAt; /* 0x7C */ Vector3f mUp; }; @@ -63,8 +68,8 @@ public: private: void update_parms(); - /* 0x88 */ f32 field_0x88; - /* 0x8C */ f32 field_0x8C; + /* 0x88 */ EGG::Rotation field_0x88; + /* 0x8C */ EGG::Rotation field_0x8C; }; } // namespace EGG diff --git a/include/egg/math/eggRotation.h b/include/egg/math/eggRotation.h new file mode 100644 index 00000000..97910e97 --- /dev/null +++ b/include/egg/math/eggRotation.h @@ -0,0 +1,50 @@ +#ifndef EGG_ROTATION_H +#define EGG_ROTATION_H + +#include "egg/math/eggMath.h" + +namespace EGG { + +template +struct Rotation { + Rotation() {} + Rotation(T val) : mRot(val) {} + Rotation(const Rotation &other) { + mRot = other.mRot; + } + Rotation &operator=(const Rotation &other) { + mRot = val; + return *this; + } + Rotation &operator=(T val) { + mRot = val; + return *this; + } + + operator T() const { + return mRot; + } + + Rotation &operator-=(T val) { + mRot -= val; + return *this; + } + Rotation &operator+=(T val) { + mRot -= val; + return *this; + } + + const T cos() const { + return Math::cos(*this); + } + const T sin() const { + return Math::sin(*this); + } + +protected: + T mRot; +}; + +} // namespace EGG + +#endif diff --git a/include/egg/math/eggVector.h b/include/egg/math/eggVector.h index b0ac18c4..3fa0c871 100644 --- a/include/egg/math/eggVector.h +++ b/include/egg/math/eggVector.h @@ -3,25 +3,19 @@ #include "common.h" #include "egg/math/eggMath.h" + #include "nw4r/math.h" namespace EGG { struct Vector3f : public nw4r::math::VEC3 { - // __ct__Q23EGG8Vector3fFv Vector3f() : nw4r::math::VEC3() {} - - // __ct__Q23EGG8Vector3fFfff Vector3f(f32 fx, f32 fy, f32 fz) : VEC3(fx, fy, fz) {} - // ~Vector3f() {} - - // __cl__Q23EGG8Vector3fFi f32 &operator()(int i) { return ((f32 *)this)[i]; } - // __ml__Q23EGG8Vector3fCFf Vector3f operator*(f32 f) const { return Vector3f(x * f, y * f, z * f); } @@ -30,12 +24,10 @@ struct Vector3f : public nw4r::math::VEC3 { return Vector3f(v.x * f, v.y * f, v.z * f); } - // __pl__Q23EGG8Vector3fCFRCQ23EGG8Vector3f Vector3f operator+(const Vector3f &v) const { return Vector3f(x + v.x, y + v.y, z + v.z); } - // __apl__Q23EGG8Vector3fFRCQ23EGG8Vector3f Vector3f &operator+=(const Vector3f &v) { x += v.x; y += v.y; @@ -43,7 +35,6 @@ struct Vector3f : public nw4r::math::VEC3 { return *this; } - // __mi__Q23EGG8Vector3fCFv Vector3f operator-() const { f32 z = this->z; f32 y = this->y; @@ -51,12 +42,10 @@ struct Vector3f : public nw4r::math::VEC3 { return Vector3f(-x, -y, -z); } - // __mi__Q23EGG8Vector3fCFRCQ23EGG8Vector3f Vector3f operator-(const Vector3f &v) { return Vector3f(x - v.x, y - v.y, z - v.z); } - // __ami__Q23EGG8Vector3fFRCQ23EGG8Vector3f Vector3f &operator-=(const Vector3f &v) { x -= v.x; y -= v.y; @@ -64,24 +53,20 @@ struct Vector3f : public nw4r::math::VEC3 { return *this; } - // __amu__Q23EGG8Vector3fFf Vector3f &operator*=(f32 f) { multScalar(f); return *this; } - // __dv__Q23EGG8Vector3fCFf Vector3f operator/(f32 f) const { return Vector3f(x / f, y / f, z / f); } - // __adv__Q23EGG8Vector3fCFf // assumed Vector3f &operator/=(f32 f) { divScalar(f); return *this; } - // __ne__Q23EGG8Vector3fCFRCQ23EGG8Vector3f bool operator!=(const Vector3f &v) { return x != v.x || y != v.y || z != v.z; } @@ -101,12 +86,10 @@ struct Vector3f : public nw4r::math::VEC3 { return EGG::Math::abs(EGG::Math::atan2(b, a)); } - // dot__Q23EGG8Vector3fCFRCQ23EGG8Vector3f f32 dot(const Vector3f &v) const { return x * v.x + y * v.y + z * v.z; } - // cross__Q23EGG8Vector3fCFRCQ23EGG8Vector3f Vector3f cross(const Vector3f &b) const { f32 _x = (y * b.z) - (z * b.y); f32 _y = (z * b.x) - (x * b.z); @@ -126,6 +109,13 @@ struct Vector3f : public nw4r::math::VEC3 { } f32 normalise(); + + Vector3f normalize() { + Vector3f other(x, y, z); + other.normalise(); + return other; + } + f32 setLength(const Vector3f &src, f32 len); f32 setLength(f32 len); diff --git a/src/egg/gfx/eggCamera.cpp b/src/egg/gfx/eggCamera.cpp index 77679d91..ed474b66 100644 --- a/src/egg/gfx/eggCamera.cpp +++ b/src/egg/gfx/eggCamera.cpp @@ -12,7 +12,7 @@ void BaseCamera::updateMatrix() { doUpdateMatrix(); } -void EGG::BaseCamera::draw(EGG::BaseCamera *cam) { +void BaseCamera::draw(EGG::BaseCamera *cam) { cam->loadMatrix(); doDraw(); } @@ -27,21 +27,37 @@ Matrix34f &LookAtCamera::getViewMatrixOld() { void LookAtCamera::doUpdateMatrix() { // NONMATCHING - mOtherMtx.copyFrom(mViewMtx); - EGG::Vector3f posRight = mPosition; - posRight -= mRight; - posRight.normalise(); - EGG::Vector3f forward = posRight.cross(mUp); - forward.normalise(); + mOtherMtx = mViewMtx; - EGG::Vector3f up = posRight.cross(forward); + Vector3f right(mPos); + + right -= mAt; + right.normalise(); + + Vector3f forward(mUp.cross(right).normalize()); + + Vector3f up(right.cross(forward)); up.normalise(); - mViewMtx.setTranslation(Vector3f((forward.dot(mPosition)), (up.dot(mPosition)), (posRight.dot(mPosition)))); - mViewMtx.setBase(0, forward); - mViewMtx.setBase(1, up); - mViewMtx.setBase(2, posRight); + f32 tx = -forward.dot(mPos); + f32 ty = -up.dot(mPos); + f32 tz = -right.dot(mPos); + + mViewMtx(0, 0) = forward(0); + mViewMtx(0, 1) = forward(1); + mViewMtx(0, 2) = forward(2); + mViewMtx(0, 3) = tx; + + mViewMtx(1, 0) = up(0); + mViewMtx(1, 1) = up(1); + mViewMtx(1, 2) = up(2); + mViewMtx(1, 3) = ty; + + mViewMtx(2, 0) = right(0); + mViewMtx(2, 1) = right(1); + mViewMtx(2, 2) = right(2); + mViewMtx(2, 3) = tz; } void LookAtCamera::doDraw() {} @@ -64,10 +80,15 @@ OrthoCamera::OrthoCamera() { void OrthoCamera::update_parms() { // NONMATCHING f32 z = field_0x8C; - f32 sin = Math::sin(field_0x88); - f32 cos = Math::cos(field_0x88); - mPosition.set(mRight.x, mRight.y, z); - mUp.set(sin, cos, 0.0f); + f32 sin = field_0x88.sin(); + f32 cos = field_0x88.cos(); + + mPos(2) = z; + mPos(1) = mAt(1); + mPos(0) = mAt(0); + mUp(2) = 0.f; + mUp(1) = cos; + mUp(0) = sin; } void OrthoCamera::doUpdateMatrix() {