From d61396866d4932a6e0d95764ea4a026798c206a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20Sobczy=C5=84ski?= <32263891+h4570@users.noreply.github.com> Date: Sat, 5 Dec 2020 21:54:03 +0100 Subject: [PATCH 1/6] Add .gitignore for dolphin sample --- src/samples/dolphin/.gitignore | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 src/samples/dolphin/.gitignore diff --git a/src/samples/dolphin/.gitignore b/src/samples/dolphin/.gitignore new file mode 100644 index 0000000..c13e7bb --- /dev/null +++ b/src/samples/dolphin/.gitignore @@ -0,0 +1,3 @@ +.vscode/ +bin/** +fonts/** From b8552330bde733cf4744d7dd9fee9e5729601ab9 Mon Sep 17 00:00:00 2001 From: h4570 Date: Sun, 6 Dec 2020 00:00:35 +0100 Subject: [PATCH 2/6] fixed rendering for non square sprites --- src/engine/modules/renderer.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/engine/modules/renderer.cpp b/src/engine/modules/renderer.cpp index c5b2a8e..ab0d4e9 100644 --- a/src/engine/modules/renderer.cpp +++ b/src/engine/modules/renderer.cpp @@ -102,10 +102,16 @@ void Renderer::changeTexture(Texture *t_tex) void Renderer::draw(Sprite &t_sprite) { texrect_t rect; - rect.t0.s = t_sprite.isFlippedHorizontally() ? 128.0F : 0.0F; - rect.t0.t = t_sprite.isFlippedVertically() ? 128.0F : 0.0F; - rect.t1.s = t_sprite.isFlippedHorizontally() ? 0.0F : 128.0F; - rect.t1.t = t_sprite.isFlippedVertically() ? 0.0F : 128.0F; + float texS = 128.0F; + float texT = 128.0F; + if (t_sprite.size.x > t_sprite.size.y) + texT = 128.0F / (t_sprite.size.x / t_sprite.size.y); + else if (t_sprite.size.y > t_sprite.size.x) + texS = 128.0F / (t_sprite.size.y / t_sprite.size.x); + rect.t0.s = t_sprite.isFlippedHorizontally() ? texS : 0.0F; + rect.t0.t = t_sprite.isFlippedVertically() ? texT : 0.0F; + rect.t1.s = t_sprite.isFlippedHorizontally() ? 0.0F : texS; + rect.t1.t = t_sprite.isFlippedVertically() ? 0.0F : texT; rect.color.r = t_sprite.color.r; rect.color.g = t_sprite.color.g; rect.color.b = t_sprite.color.b; From 67a10cb72400cda0add0e3f70ba6476304b28afa Mon Sep 17 00:00:00 2001 From: h4570 Date: Sun, 6 Dec 2020 00:13:37 +0100 Subject: [PATCH 3/6] Added tex max check --- src/engine/modules/renderer.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/engine/modules/renderer.cpp b/src/engine/modules/renderer.cpp index ab0d4e9..7bd7a60 100644 --- a/src/engine/modules/renderer.cpp +++ b/src/engine/modules/renderer.cpp @@ -102,12 +102,13 @@ void Renderer::changeTexture(Texture *t_tex) void Renderer::draw(Sprite &t_sprite) { texrect_t rect; - float texS = 128.0F; - float texT = 128.0F; + float texMax = t_sprite.size.x > t_sprite.size.y ? t_sprite.size.x : t_sprite.size.y; + float texS = texMax; + float texT = texMax; if (t_sprite.size.x > t_sprite.size.y) - texT = 128.0F / (t_sprite.size.x / t_sprite.size.y); + texT = texMax / (t_sprite.size.x / t_sprite.size.y); else if (t_sprite.size.y > t_sprite.size.x) - texS = 128.0F / (t_sprite.size.y / t_sprite.size.x); + texS = texMax / (t_sprite.size.y / t_sprite.size.x); rect.t0.s = t_sprite.isFlippedHorizontally() ? texS : 0.0F; rect.t0.t = t_sprite.isFlippedVertically() ? texT : 0.0F; rect.t1.s = t_sprite.isFlippedHorizontally() ? 0.0F : texS; From d0017f26f1a55bc89eaca715b2ead37a20a1ed3a Mon Sep 17 00:00:00 2001 From: h4570 Date: Sun, 6 Dec 2020 16:38:51 +0100 Subject: [PATCH 4/6] marked distanceTo as const --- src/engine/include/models/math/vector3.hpp | 2 +- src/engine/models/math/vector3.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/engine/include/models/math/vector3.hpp b/src/engine/include/models/math/vector3.hpp index 55f392c..a69d07a 100644 --- a/src/engine/include/models/math/vector3.hpp +++ b/src/engine/include/models/math/vector3.hpp @@ -54,7 +54,7 @@ public: void rotate(const Vector3 &v, u8 inversed = false); void set(const float &t_x, const float &t_y, const float &t_z); void copy(Vector3 &v); - float distanceTo(Vector3 &v); + float distanceTo(const Vector3 &v) const; const void print() const; }; diff --git a/src/engine/models/math/vector3.cpp b/src/engine/models/math/vector3.cpp index fbb772e..9680800 100644 --- a/src/engine/models/math/vector3.cpp +++ b/src/engine/models/math/vector3.cpp @@ -298,7 +298,7 @@ void Vector3::copy(Vector3 &v) : "$6"); } -float Vector3::distanceTo(Vector3 &v) +float Vector3::distanceTo(const Vector3 &v) const { register float result; asm volatile( // VU0 Macro program From 55cee63ace4c56ac04282231f4eb744f810ce37e Mon Sep 17 00:00:00 2001 From: h4570 Date: Sun, 6 Dec 2020 20:20:08 +0100 Subject: [PATCH 5/6] fixed path3 lighting on real ps2 --- src/engine/include/models/light_bulb.hpp | 2 +- src/engine/include/modules/light.hpp | 2 +- src/engine/modules/gif_sender.cpp | 8 ++--- src/engine/modules/light.cpp | 43 +++++++++++++----------- 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/engine/include/models/light_bulb.hpp b/src/engine/include/models/light_bulb.hpp index 84bec6f..7f4100a 100644 --- a/src/engine/include/models/light_bulb.hpp +++ b/src/engine/include/models/light_bulb.hpp @@ -17,7 +17,7 @@ struct LightBulb { Vector3 position; - u16 intensity; + u8 intensity; }; #endif diff --git a/src/engine/include/modules/light.hpp b/src/engine/include/modules/light.hpp index f2830c5..71f1cfd 100644 --- a/src/engine/include/modules/light.hpp +++ b/src/engine/include/modules/light.hpp @@ -24,7 +24,7 @@ public: ~Light(); static u16 getLightsCount(u32 t_bulbsCount); - static void calculateLight(VECTOR *t_lightDirections, VECTOR *t_lightColors, int *t_lightTypes, LightBulb *t_bulbs, u32 t_bulbsCount, Vector3 t_objPosition); + static void calculateLight(VECTOR *t_lightDirections, VECTOR *t_lightColors, int *t_lightTypes, LightBulb *t_bulbs, u32 t_lightsCount, Vector3 t_objPosition); private: }; diff --git a/src/engine/modules/gif_sender.cpp b/src/engine/modules/gif_sender.cpp index 494f38d..5e2d06c 100644 --- a/src/engine/modules/gif_sender.cpp +++ b/src/engine/modules/gif_sender.cpp @@ -188,16 +188,16 @@ void GifSender::calc3DObject(Matrix t_perspective, Mesh &t_mesh, u32 vertexCount VECTOR *lights = new VECTOR[vertexCount]; calculate_normals(normals, vertexCount, normals, localLight); - Light::calculateLight(lightDirections, lightColors, lightTypes, t_bulbs, t_bulbsCount, t_mesh.position); + Light::calculateLight(lightDirections, lightColors, lightTypes, t_bulbs, lightsCount, t_mesh.position); calculate_lights(lights, vertexCount, normals, lightDirections, lightColors, lightTypes, lightsCount); for (u32 i = 0; i < vertexCount; i++) { // Apply the light value to the colour. - colors[i][0] = (t_mesh.color.r * lights[i][0] / 128.0F); - colors[i][1] = (t_mesh.color.g * lights[i][1] / 128.0F); - colors[i][2] = (t_mesh.color.b * lights[i][2] / 128.0F); + colors[i][0] = (t_mesh.color.r * lights[i][0]); + colors[i][1] = (t_mesh.color.g * lights[i][1]); + colors[i][2] = (t_mesh.color.b * lights[i][2]); vector_clamp(colors[i], colors[i], 0.00F, 1.99F); } diff --git a/src/engine/modules/light.cpp b/src/engine/modules/light.cpp index b8bb95e..b8a1d09 100644 --- a/src/engine/modules/light.cpp +++ b/src/engine/modules/light.cpp @@ -23,11 +23,13 @@ Light::~Light() {} // ---- const u8 ADDITIONAL_LIGHTS = 1; // Ambient +const float LIGHT_MAX = 255.0F; +const float LIGHT_DIS_MOD = 4.0F; u16 Light::getLightsCount(u32 t_bulbsCount) { return t_bulbsCount + ADDITIONAL_LIGHTS; } /** Calculates lighting. Used in gifSender in object 3D calculations */ -void Light::calculateLight(VECTOR *t_lightDirections, VECTOR *t_lightColors, int *t_lightTypes, LightBulb *t_bulbs, u32 t_bulbsCount, Vector3 t_objPosition) +void Light::calculateLight(VECTOR *t_lightDirections, VECTOR *t_lightColors, int *t_lightTypes, LightBulb *t_bulbs, u32 t_lightsCount, Vector3 t_objPosition) { // --- Ambient light @@ -45,27 +47,30 @@ void Light::calculateLight(VECTOR *t_lightDirections, VECTOR *t_lightColors, int // --- - for (u8 i = 0; i < t_bulbsCount; i++) + for (u8 i = 1; i < t_lightsCount; i++) { - t_lightTypes[i + 1] = LIGHT_DIRECTIONAL; - - Vector3 newLight = Vector3(t_objPosition.x - t_bulbs[i].position.x, - t_objPosition.y - t_bulbs[i].position.y, - t_objPosition.z - t_bulbs[i].position.z); + t_lightTypes[i] = LIGHT_DIRECTIONAL; + Vector3 newLight = Vector3(t_objPosition.x - t_bulbs[i - 1].position.x, + t_objPosition.y - t_bulbs[i - 1].position.y, + t_objPosition.z - t_bulbs[i - 1].position.z); newLight.normalize(); - newLight = newLight * - (.1F + - (t_bulbs[i].intensity / t_bulbs[i].position.distanceTo(t_objPosition))); + t_lightDirections[i][0] = newLight.x; + t_lightDirections[i][1] = newLight.y; + t_lightDirections[i][2] = newLight.z; + t_lightDirections[i][3] = 1.0F; - t_lightDirections[i + 1][0] = newLight.x; - t_lightDirections[i + 1][1] = newLight.y; - t_lightDirections[i + 1][2] = newLight.z; - t_lightDirections[i + 1][3] = 1.0F; - - t_lightColors[i + 1][0] = 0.8F; - t_lightColors[i + 1][1] = 0.8F; - t_lightColors[i + 1][2] = 0.8F; - t_lightColors[i + 1][3] = 1.0F; + // Intensity range 0.0F - 1.0F + float intensity = t_bulbs[i - 1].intensity / LIGHT_MAX; + // Distance range 0.0F - LIGHT_MAX * LIGHT_DIS_MOD (1020.0F at this time) + float distance = t_bulbs[i - 1].position.distanceTo(t_objPosition); + // printf("Distance:%f\n", distance); + distance = distance > (LIGHT_MAX - LIGHT_DIS_MOD) * LIGHT_DIS_MOD ? LIGHT_MAX - LIGHT_DIS_MOD : distance; + intensity /= 1.0F + (distance / LIGHT_DIS_MOD); // intensity /= 1.0F - 251.0F + intensity /= 3.0F; // it looks better + t_lightColors[i][0] = intensity; + t_lightColors[i][1] = intensity; + t_lightColors[i][2] = intensity; + t_lightColors[i][3] = 1.0F; } } From f080a97dc56a833c6798c988c09ae15592fd1ede Mon Sep 17 00:00:00 2001 From: h4570 Date: Sun, 6 Dec 2020 20:25:52 +0100 Subject: [PATCH 6/6] fixes for real ps2 (vwaitq), and cosmetic changes --- src/engine/include/models/math/vector3.hpp | 3 +- src/engine/models/math/vector3.cpp | 44 +++++++++++++--------- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/engine/include/models/math/vector3.hpp b/src/engine/include/models/math/vector3.hpp index a69d07a..641f6b1 100644 --- a/src/engine/include/models/math/vector3.hpp +++ b/src/engine/include/models/math/vector3.hpp @@ -39,7 +39,8 @@ public: Vector3 operator+(Vector3 v); Vector3 operator-(const Vector3 &v); Vector3 operator*(Vector3 &v); - Vector3 operator*(float t); + Vector3 operator*(const float &t); + void operator*=(const float &t); Vector3 operator/(float t); Vector3 operator-(void); diff --git a/src/engine/models/math/vector3.cpp b/src/engine/models/math/vector3.cpp index 9680800..7adafd3 100644 --- a/src/engine/models/math/vector3.cpp +++ b/src/engine/models/math/vector3.cpp @@ -45,8 +45,7 @@ void Vector3::setByLerp(const Vector3 &v1, const Vector3 &v2, const float &t_int "vadd.xyz vf9, vf8, vf4 \n\t" // vf9 = vf8 + vf4 "sqc2 vf9, 0x0(%0) \n\t" // v0 = vf9 : - : "r"(&this->xyz), "r"(&v1.xyz), "r"(&v2.xyz), "f"(t_interp) - : "$8"); + : "r"(&this->xyz), "r"(&v1.xyz), "r"(&v2.xyz), "f"(t_interp)); x *= t_scale; y *= t_scale; z *= t_scale; @@ -120,7 +119,7 @@ Vector3 Vector3::operator*(Vector3 &v) return res; } -Vector3 Vector3::operator*(float t) +Vector3 Vector3::operator*(const float &t) { Vector3 result; asm volatile( @@ -130,11 +129,22 @@ Vector3 Vector3::operator*(float t) "vmulx.xyz vf6, vf4, vf5 \n\t" "sqc2 vf6, 0x0(%0) \n\t" : - : "r"(result.xyz), "r"(this->xyz), "f"(t) - : "$8"); + : "r"(result.xyz), "r"(this->xyz), "f"(t)); return result; } +void Vector3::operator*=(const float &t) +{ + asm volatile( + "lqc2 vf4, 0x0(%0) \n\t" + "mfc1 $8, %1 \n\t" + "qmtc2 $8, vf5 \n\t" + "vmulx.xyz vf4, vf4, vf5 \n\t" + "sqc2 vf4, 0x0(%0) \n\t" + : + : "r"(this->xyz), "f"(t)); +} + Vector3 Vector3::operator/(float t) { Vector3 result; @@ -164,8 +174,7 @@ u8 Vector3::shouldBeBackfaceCulled(const Vector3 *t_cameraPos, const Vector3 *v0 "qmfc2 $2, vf5 \n\t" // store result on `dot` variable "mtc1 $2, %0 \n\t" : "=f"(dot) - : "r"(t_cameraPos->xyz), "r"(v0->xyz), "r"(v1->xyz), "r"(v2->xyz) - : "$2"); + : "r"(t_cameraPos->xyz), "r"(v0->xyz), "r"(v1->xyz), "r"(v2->xyz)); return dot <= 0.0F; } @@ -190,12 +199,12 @@ float Vector3::length() "vaddy.x vf5, vf5, vf5 \n\t" "vaddz.x vf5, vf5, vf5 \n\t" "vsqrt Q , vf5x \n\t" + "vwaitq \n\t" "vaddq.x vf8, vf0, Q \n\t" "qmfc2 $2, vf8 \n\t" "mtc1 $2, %0 \n\t" : "=f"(result) - : "r"(this->xyz) - : "$2"); + : "r"(this->xyz)); return result; // return Math::sqrt(x * x + y * y + z * z); } @@ -208,6 +217,7 @@ void Vector3::normalize() "vaddy.x vf5, vf5, vf5 \n\t" "vaddz.x vf5, vf5, vf5 \n\t" "vrsqrt Q, vf0w, vf5x \n\t" + "vwaitq \n\t" "vsub.xyz vf6, vf0, vf0 \n\t" "vaddw.xyz vf6, vf6, vf4 \n\t" "vwaitq \n\t" @@ -230,8 +240,7 @@ float Vector3::innerProduct(Vector3 &v) "qmfc2 $2, vf6 \n\t" "mtc1 $2, %0 \n\t" : "=f"(result) - : "r"(this->xyz), "r"(v.xyz) - : "$2"); + : "r"(this->xyz), "r"(v.xyz)); return result; // return (x * v.x + y * v.y + z * v.z); } @@ -294,8 +303,7 @@ void Vector3::copy(Vector3 &v) "lq $6, 0x0(%1) \n\t" "sq $6, 0x0(%0) \n\t" : - : "r"(v.xyz), "r"(this->xyz) - : "$6"); + : "r"(v.xyz), "r"(this->xyz)); } float Vector3::distanceTo(const Vector3 &v) const @@ -309,16 +317,16 @@ float Vector3::distanceTo(const Vector3 &v) const "vaddy.x vf7, vf7, vf7 \n\t" "vaddz.x vf7, vf7, vf7 \n\t" "vsqrt Q , vf7x \n\t" + "vwaitq \n\t" "vaddq.x vf8, vf0, Q \n\t" "qmfc2 $2, vf8 \n\t" "mtc1 $2, %0 \n\t" : "=f"(result) - : "r"(this->xyz), "r"(v.xyz) - : "$2"); + : "r"(this->xyz), "r"(v.xyz)); return result; - // return Math::sqrt((this->x - another.x) * (this->x - another.x) + - // (this->y - another.y) * (this->y - another.y) + - // (this->z - another.z) * (this->z - another.z)); + // return Math::sqrt((this->x - v.x) * (this->x - v.x) + + // (this->y - v.y) * (this->y - v.y) + + // (this->z - v.z) * (this->z - v.z)); } const void Vector3::print() const