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/models/math/vector3.hpp b/src/engine/include/models/math/vector3.hpp index 55f392c..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); @@ -54,7 +55,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/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/models/math/vector3.cpp b/src/engine/models/math/vector3.cpp index fbb772e..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,11 +303,10 @@ 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(Vector3 &v) +float Vector3::distanceTo(const Vector3 &v) const { register float result; asm volatile( // VU0 Macro program @@ -309,16 +317,16 @@ float Vector3::distanceTo(Vector3 &v) "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 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; } } diff --git a/src/engine/modules/renderer.cpp b/src/engine/modules/renderer.cpp index c5b2a8e..7bd7a60 100644 --- a/src/engine/modules/renderer.cpp +++ b/src/engine/modules/renderer.cpp @@ -102,10 +102,17 @@ 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 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 = texMax / (t_sprite.size.x / t_sprite.size.y); + else if (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; + 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; 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/**