diff --git a/src/engine/include/models/math/matrix.hpp b/src/engine/include/models/math/matrix.hpp index 366794b..5e13bd7 100644 --- a/src/engine/include/models/math/matrix.hpp +++ b/src/engine/include/models/math/matrix.hpp @@ -26,14 +26,64 @@ public: float m31, float m32, float m33, float m34, float m41, float m42, float m43, float m44); Matrix(const Matrix &v); - Matrix operator*(Matrix &v); + Matrix operator*(const Matrix &v); + void operator*=(const Matrix &v); Matrix(); ~Matrix(); void lookAt(Vector3 &t_up, Vector3 &t_position, Vector3 &t_target); void identity(); - void makeZRotation(float t_radians); - void translate(float t_x, float t_y, float t_z); + inline void unit() { identity(); }; + + // Translation + + void translate(const Vector3 &t_val); + + inline void translation(const Vector3 &t_val) + { + identity(); + translate(t_val); + } + + // Rotation + + void rotateX(const float &t_radians); + void rotateY(const float &t_radians); + void rotateZ(const float &t_radians); + + inline void rotationX(const float &t_radians) + { + identity(); + rotateX(t_radians); + } + + inline void rotationY(const float &t_radians) + { + identity(); + rotateY(t_radians); + } + + inline void rotationZ(const float &t_radians) + { + identity(); + rotateZ(t_radians); + } + + inline void rotate(const Vector3 &t_val) + { + rotateX(t_val.x); + rotateY(t_val.y); + rotateX(t_val.z); + } + + inline void rotation(const Vector3 &t_val) + { + identity(); + rotate(t_val); + } + + // + void setPerspective(ScreenSettings &screen); const void print() const; }; diff --git a/src/engine/include/modules/vif_sender.hpp b/src/engine/include/modules/vif_sender.hpp index 467e111..a3e1e36 100644 --- a/src/engine/include/modules/vif_sender.hpp +++ b/src/engine/include/modules/vif_sender.hpp @@ -41,7 +41,6 @@ private: packet2_t *currPacket; u8 context; packet2_t *matricesPacket __attribute__((aligned(64))); - MATRIX localWorld, localScreen; VECTOR position, rotation; u32 vertCount; }; diff --git a/src/engine/include/utils/math.hpp b/src/engine/include/utils/math.hpp index 4c5f115..3218047 100644 --- a/src/engine/include/utils/math.hpp +++ b/src/engine/include/utils/math.hpp @@ -24,13 +24,23 @@ class Math { public: - const static float HALF_ANG2RAD = 3.14159265358979323846 / 360.0; + const static float HALF_ANG2RAD = 3.14159265358979323846F / 360.0F; + const static float ANG2RAD = 3.14159265358979323846F / 180.0F; const static float PI = 3.1415926535897932384626433832795F; const static float HALF_PI = 1.5707963267948966192313216916398F; static float cos(float x); + static float asin(float x); + static float mod(float x, float y); static inline float sin(float x) { return cos(x - HALF_PI); }; + static inline float tan(float x) { return sin(x) / cos(x); } static float sqrt(float x); static float invSqrt(float x); + static inline u32 max(u32 a, u32 b) { return (a > b) ? a : b; } + static inline s32 max(s32 a, s32 b) { return (a > b) ? a : b; } + static inline float max(float a, float b) { return (a > b) ? a : b; } + static inline u32 min(u32 a, u32 b) { return (a < b) ? a : b; } + static inline s32 min(s32 a, s32 b) { return (a < b) ? a : b; } + static inline float min(float a, float b) { return (a < b) ? a : b; } private: Math(); diff --git a/src/engine/models/math/matrix.cpp b/src/engine/models/math/matrix.cpp index 7aaaf37..ec927a1 100644 --- a/src/engine/models/math/matrix.cpp +++ b/src/engine/models/math/matrix.cpp @@ -84,17 +84,41 @@ void Matrix::identity() : "r"(this->data)); } -void Matrix::translate(float x, float y, float z) +void Matrix::translate(const Vector3 &t_val) { - this->identity(); - this->data[(3 << 2) + 0] = x; // 3,0 - this->data[(3 << 2) + 1] = y; // 3,1 - this->data[(3 << 2) + 2] = z; // 3,2 + this->data[12] += t_val.x; // 3,0 + this->data[13] += t_val.y; // 3,1 + this->data[14] += t_val.z; // 3,2 } -void Matrix::makeZRotation(float t_radians) +void Matrix::rotateX(const float &t_radians) { - this->identity(); + Matrix temp = Matrix(); + temp.identity(); + float c = Math::cos(t_radians); + float s = Math::sin(t_radians); + this->data[5] = c; // 1,1 + this->data[6] = s; // 1,2 + this->data[9] = -s; // 2,1 + this->data[10] = c; // 2,2 +} + +void Matrix::rotateY(const float &t_radians) +{ + Matrix temp = Matrix(); + temp.identity(); + float c = Math::cos(t_radians); + float s = Math::sin(t_radians); + this->data[0] = c; // 0,0 + this->data[2] = -s; // 0,3 + this->data[8] = s; // 2,0 + this->data[10] = c; // 2,2 +} + +void Matrix::rotateZ(const float &t_radians) +{ + Matrix temp = Matrix(); + temp.identity(); float c = Math::cos(t_radians); float s = Math::sin(t_radians); this->data[0] = c; // 0,0 @@ -103,7 +127,7 @@ void Matrix::makeZRotation(float t_radians) this->data[5] = c; // 1,1 } -Matrix Matrix::operator*(Matrix &t) +Matrix Matrix::operator*(const Matrix &t) { Matrix result; asm volatile( @@ -141,6 +165,42 @@ Matrix Matrix::operator*(Matrix &t) return result; } +void Matrix::operator*=(const Matrix &t) +{ + asm volatile( + "lqc2 vf1, 0x00(%1) \n\t" + "lqc2 vf2, 0x10(%1) \n\t" + "lqc2 vf3, 0x20(%1) \n\t" + "lqc2 vf4, 0x30(%1) \n\t" + "lqc2 vf5, 0x00(%2) \n\t" + "lqc2 vf6, 0x10(%2) \n\t" + "lqc2 vf7, 0x20(%2) \n\t" + "lqc2 vf8, 0x30(%2) \n\t" + "vmulax.xyzw ACC, vf5, vf1 \n\t" + "vmadday.xyzw ACC, vf6, vf1 \n\t" + "vmaddaz.xyzw ACC, vf7, vf1 \n\t" + "vmaddw.xyzw vf1, vf8, vf1 \n\t" + "vmulax.xyzw ACC, vf5, vf2 \n\t" + "vmadday.xyzw ACC, vf6, vf2 \n\t" + "vmaddaz.xyzw ACC, vf7, vf2 \n\t" + "vmaddw.xyzw vf2, vf8, vf2 \n\t" + "vmulax.xyzw ACC, vf5, vf3 \n\t" + "vmadday.xyzw ACC, vf6, vf3 \n\t" + "vmaddaz.xyzw ACC, vf7, vf3 \n\t" + "vmaddw.xyzw vf3, vf8, vf3 \n\t" + "vmulax.xyzw ACC, vf5, vf4 \n\t" + "vmadday.xyzw ACC, vf6, vf4 \n\t" + "vmaddaz.xyzw ACC, vf7, vf4 \n\t" + "vmaddw.xyzw vf4, vf8, vf4 \n\t" + "sqc2 vf1, 0x00(%0) \n\t" + "sqc2 vf2, 0x10(%0) \n\t" + "sqc2 vf3, 0x20(%0) \n\t" + "sqc2 vf4, 0x30(%0) \n\t" + : + : "r"(this->data), "r"(this->data), "r"(t.data) + : "memory"); +} + /** Create empty matrix */ Matrix::Matrix() { diff --git a/src/engine/modules/renderer.cpp b/src/engine/modules/renderer.cpp index 9b55597..02cadff 100644 --- a/src/engine/modules/renderer.cpp +++ b/src/engine/modules/renderer.cpp @@ -203,19 +203,19 @@ void Renderer::allocateBuffers(float t_screenW, float t_screenH) frameBuffers[0].width = (u16)t_screenW; frameBuffers[0].height = (u16)t_screenH; frameBuffers[0].mask = 0; - frameBuffers[0].psm = GS_PSM_32; + frameBuffers[0].psm = GS_PSM_24; frameBuffers[0].address = graph_vram_allocate((u16)t_screenW, (u16)t_screenH, frameBuffers[0].psm, GRAPH_ALIGN_PAGE); frameBuffers[1].width = (u16)t_screenW; frameBuffers[1].height = (u16)t_screenH; frameBuffers[1].mask = 0; - frameBuffers[1].psm = GS_PSM_32; + frameBuffers[1].psm = GS_PSM_24; frameBuffers[1].address = graph_vram_allocate((u16)t_screenW, (u16)t_screenH, frameBuffers[1].psm, GRAPH_ALIGN_PAGE); zBuffer.enable = DRAW_ENABLE; zBuffer.mask = 0; zBuffer.method = ZTEST_METHOD_GREATER_EQUAL; - zBuffer.zsm = GS_ZBUF_32; + zBuffer.zsm = GS_ZBUF_24; zBuffer.address = graph_vram_allocate((u16)t_screenW, (u16)t_screenH, zBuffer.zsm, GRAPH_ALIGN_PAGE); PRINT_LOG("Framebuffers, zBuffer set and allocated!"); diff --git a/src/engine/modules/vif_sender.cpp b/src/engine/modules/vif_sender.cpp index 3135e09..2ef738a 100644 --- a/src/engine/modules/vif_sender.cpp +++ b/src/engine/modules/vif_sender.cpp @@ -73,10 +73,17 @@ void VifSender::sendMatrices(const RenderData &t_renderData, const Vector3 &t_po { vec3ToNative(position, t_position, 1.0F); vec3ToNative(rotation, t_rotation, 1.0F); + MATRIX localWorld, localScreen; create_local_world(localWorld, position, rotation); create_local_screen(localScreen, localWorld, t_renderData.worldView->data, t_renderData.perspective->data); + // Small undo, because im testing new matrix funcs, which are DIFFERENT than PS2SDK, so cant be used right now. + // Matrix viewProj = Matrix(); + // viewProj.rotation(t_rotation); // model matrix + // viewProj.translate(t_position); // model matrix + // viewProj *= *t_renderData.worldView * *t_renderData.perspective; // viewproj = model * (view * projection) packet2_reset(matricesPacket, false); - packet2_utils_vu_add_unpack_data(matricesPacket, 0, &localScreen, 8, 0); + // packet2_utils_vu_add_unpack_data(matricesPacket, 0, &viewProj.data, 8, 0); + packet2_utils_vu_add_unpack_data(matricesPacket, 0, localScreen, 8, 0); packet2_utils_vu_add_end_tag(matricesPacket); dma_channel_wait(DMA_CHANNEL_VIF1, 0); dma_channel_send_packet2(matricesPacket, DMA_CHANNEL_VIF1, 1); diff --git a/src/engine/utils/math.cpp b/src/engine/utils/math.cpp index ddf960f..5c4846e 100644 --- a/src/engine/utils/math.cpp +++ b/src/engine/utils/math.cpp @@ -99,3 +99,87 @@ void manyVec3ToNative(VECTOR *o_result, Vector3 *t_vec, int t_amount, float t_fo for (int i = 0; i < t_amount; i++) vec3ToNative(o_result[i], t_vec[i], t_fourthVal); } + +float Math::asin(float x) +{ + float r; + asm volatile( + "lui $9, 0x3f00 \n\t" + ".set noreorder \n\t" + ".align 3 \n\t" + "abs.s %0, %1 \n\t" + "lui $8, 0xbe22 \n\t" + "mtc1 $9, $f1 \n\t" + "ori $8, $8, 0xf983 \n\t" + "mtc1 $8, $f8 \n\t" + "lui $9, 0x4b00 \n\t" + "mtc1 $9, $f3 \n\t" + "lui $8, 0x3f80 \n\t" + "mtc1 $8, $f2 \n\t" + "mula.s %0, $f8 \n\t" + "msuba.s $f3, $f2 \n\t" + "madda.s $f3, $f2 \n\t" + "lui $8, 0x40c9 \n\t" + "msuba.s %0, $f8 \n\t" + "ori $8, 0x0fdb \n\t" + "msub.s %0, $f1, $f2 \n\t" + "lui $9, 0xc225 \n\t" + "abs.s %0, %0 \n\t" + "lui $10, 0x3e80 \n\t" + "mtc1 $10, $f7 \n\t" + "ori $9, 0x5de1 \n\t" + "sub.s %0, %0, $f7 \n\t" + "lui $10, 0x42a3 \n\t" + "mtc1 $8, $f3 \n\t" + "ori $10, 0x3458 \n\t" + "mtc1 $9, $f4 \n\t" + "lui $8, 0xc299 \n\t" + "mtc1 $10, $f5 \n\t" + "ori $8, 0x2663 \n\t" + "mul.s $f8, %0, %0 \n\t" + "lui $9, 0x421e \n\t" + "mtc1 $8, $f6 \n\t" + "ori $9, 0xd7bb \n\t" + "mtc1 $9, $f7 \n\t" + "nop \n\t" + "mul.s $f1, %0, $f8 \n\t" + "mul.s $f9, $f8, $f8 \n\t" + "mula.s $f3, %0 \n\t" + "mul.s $f2, $f1, $f8 \n\t" + "madda.s $f4, $f1 \n\t" + "mul.s $f1, $f1, $f9 \n\t" + "mul.s %0, $f2, $f9 \n\t" + "madda.s $f5, $f2 \n\t" + "madda.s $f6, $f1 \n\t" + "madd.s %0, $f7, %0 \n\t" + ".set reorder \n\t" + : "=&f"(r) + : "f"(x) + : "$f1", "$f2", "$f3", "$f4", "$f5", "$f6", "$f7", "$f8", "$f9", "$8", "$9", "$10"); + return r; +} + +float Math::mod(float x, float y) +{ + /* + * Portable fmod(x,y) implementation for systems + * that don't have it. Adapted from code found here: + * http://www.opensource.apple.com/source/python/python-3/python/Python/fmod.c + */ + float i, f; + + if (y == 0.0f) + { + return 0.0f; + } + + i = floorf(x / y); + f = x - i * y; + + if ((x < 0.0f) != (y < 0.0f)) + { + f = f - y; + } + + return f; +}