From 3284c5a598f35862cc38575366f9f5bdee3691f5 Mon Sep 17 00:00:00 2001 From: h4570 Date: Mon, 7 Dec 2020 21:02:06 +0100 Subject: [PATCH 1/3] added setAmbientLight(), setWorldColor() --- src/engine/include/modules/gif_sender.hpp | 8 +++++--- src/engine/include/modules/light.hpp | 8 ++++++-- src/engine/include/modules/renderer.hpp | 13 ++++++++++++- src/engine/include/modules/vif_sender.hpp | 4 +++- src/engine/modules/gif_sender.cpp | 16 ++++++++-------- src/engine/modules/light.cpp | 8 +++++--- src/engine/modules/renderer.cpp | 16 +++++++++++++--- src/engine/modules/vif_sender.cpp | 3 ++- 8 files changed, 54 insertions(+), 22 deletions(-) diff --git a/src/engine/include/modules/gif_sender.hpp b/src/engine/include/modules/gif_sender.hpp index 1d695e8..88b9676 100644 --- a/src/engine/include/modules/gif_sender.hpp +++ b/src/engine/include/modules/gif_sender.hpp @@ -17,6 +17,7 @@ #include #include #include +#include "./light.hpp" #include "../models/mesh.hpp" #include "../models/math/matrix.hpp" #include "../models/screen_settings.hpp" @@ -29,17 +30,18 @@ class GifSender { public: - GifSender(u32 t_packetSize, ScreenSettings *t_screen); + GifSender(u32 t_packetSize, ScreenSettings *t_screen, Light *t_light); ~GifSender(); void initPacket(u8 context); void addObject(RenderData *t_renderData, Mesh &t_mesh, u32 vertexCount, VECTOR *vertices, VECTOR *normals, VECTOR *coordinates, LightBulb *t_bulbs, u16 t_bulbsCount, texbuffer_t *textureBuffer); - void addClear(zbuffer_t *t_zBuffer); + void addClear(zbuffer_t *t_zBuffer, color_t *rgb); void sendPacket(); - void sendClear(zbuffer_t *t_zBuffer); + void sendClear(zbuffer_t *t_zBuffer, color_t *rgb); static void sendTexture(Texture &texture, texbuffer_t *t_texBuffer); private: + Light *light; xyz_t *xyz; color_t *rgbaq; texel_t *st; diff --git a/src/engine/include/modules/light.hpp b/src/engine/include/modules/light.hpp index 71f1cfd..ded1f2a 100644 --- a/src/engine/include/modules/light.hpp +++ b/src/engine/include/modules/light.hpp @@ -23,10 +23,14 @@ public: Light(); ~Light(); - static u16 getLightsCount(u32 t_bulbsCount); - static void calculateLight(VECTOR *t_lightDirections, VECTOR *t_lightColors, int *t_lightTypes, LightBulb *t_bulbs, u32 t_lightsCount, Vector3 t_objPosition); + u16 getLightsCount(u32 t_bulbsCount); + + /** Values from 0.0000F to 1.0000F */ + void setAmbientLight(const Vector3 &t_rgb); + void calculateLight(VECTOR *t_lightDirections, VECTOR *t_lightColors, int *t_lightTypes, LightBulb *t_bulbs, u32 t_lightsCount, Vector3 t_objPosition); private: + Vector3 ambientLight; }; #endif diff --git a/src/engine/include/modules/renderer.hpp b/src/engine/include/modules/renderer.hpp index c215eda..d2cdb76 100644 --- a/src/engine/include/modules/renderer.hpp +++ b/src/engine/include/modules/renderer.hpp @@ -16,6 +16,7 @@ #include #include "gif_sender.hpp" #include "vif_sender.hpp" +#include "light.hpp" #include "../models/math/plane.hpp" #include "../models/sprite.hpp" #include "../models/screen_settings.hpp" @@ -102,11 +103,19 @@ public: void setCameraDefinitions(Matrix *t_worldView, Vector3 *t_cameraPos, Plane *t_planes); + void setWorldColor(const color_t &t_rgb); + void endFrame(float fps); - TextureRepository *getTextureRepository() { return &textureRepo; }; + void setAmbientLight(const Vector3 &t_rgb) { light.setAmbientLight(t_rgb); } + + TextureRepository *getTextureRepository() + { + return &textureRepo; + }; private: + // We have some GCC bug here. Just try to reorder declarations. For example move worldColor up - game will crash. void changeTexture(Texture *t_tex); u32 lastTextureId; texbuffer_t textureBuffer; @@ -117,12 +126,14 @@ private: void beginFrameIfNeeded(); u8 isFrameEmpty; Matrix perspective; + Light light; RenderData renderData; TextureRepository textureRepo; ScreenSettings *screen; GifSender *gifSender; VifSender *vifSender; packet2_t *flipPacket; + color_t worldColor; void allocateBuffers(float t_screenW, float t_screenH); void initDrawingEnv(float t_screenW, float t_screenH); void setPrim(); diff --git a/src/engine/include/modules/vif_sender.hpp b/src/engine/include/modules/vif_sender.hpp index 6291a75..467e111 100644 --- a/src/engine/include/modules/vif_sender.hpp +++ b/src/engine/include/modules/vif_sender.hpp @@ -14,6 +14,7 @@ #include #include #include "../models/render_data.hpp" +#include "./light.hpp" #include "../models/light_bulb.hpp" #include "../models/mesh.hpp" #include "../models/math/matrix.hpp" @@ -24,7 +25,7 @@ class VifSender { public: - VifSender(); + VifSender(Light *t_light); ~VifSender(); // TODO refactor @@ -32,6 +33,7 @@ public: void sendMatrices(const RenderData &t_renderData, const Vector3 &t_position, const Vector3 &t_rotation); private: + Light *light; void uploadMicroProgram(); void setDoubleBuffer(); void drawVertices(Mesh &t_mesh, u32 t_start, u32 t_end, VECTOR *t_vertices, VECTOR *t_coordinates, prim_t *t_prim, texbuffer_t *textureBuffer); diff --git a/src/engine/modules/gif_sender.cpp b/src/engine/modules/gif_sender.cpp index 5e2d06c..d923049 100644 --- a/src/engine/modules/gif_sender.cpp +++ b/src/engine/modules/gif_sender.cpp @@ -12,7 +12,6 @@ #include "../include/utils/math.hpp" #include "../include/utils/debug.hpp" -#include "../include/modules/light.hpp" #include #include #include @@ -29,9 +28,10 @@ /** Initializes vars and creates data transfer packets * @param packetSize Size of data packet, should be increased when more data will be rendered */ -GifSender::GifSender(u32 t_packetSize, ScreenSettings *t_screen) : screen(t_screen) +GifSender::GifSender(u32 t_packetSize, ScreenSettings *t_screen, Light *t_light) : screen(t_screen) { PRINT_LOG("Initializing GifSender"); + light = t_light; packetSize = t_packetSize; packets[0] = packet2_create(t_packetSize, P2_TYPE_NORMAL, P2_MODE_CHAIN, false); packets[1] = packet2_create(t_packetSize, P2_TYPE_NORMAL, P2_MODE_CHAIN, false); @@ -76,7 +76,7 @@ void GifSender::sendTexture(Texture &texture, texbuffer_t *t_texBuffer) packet2_free(packet2); } -void GifSender::sendClear(zbuffer_t *t_zBuffer) +void GifSender::sendClear(zbuffer_t *t_zBuffer, color_t *rgb) { packet2_t *packet2 = packet2_create(36, P2_TYPE_NORMAL, P2_MODE_CHAIN, false); packet2_chain_open_end(packet2, 0, 0); @@ -84,7 +84,7 @@ void GifSender::sendClear(zbuffer_t *t_zBuffer) packet2_update(packet2, draw_clear(packet2->next, 0, 2048.0F - (screen->width / 2), 2048.0F - (screen->height / 2), screen->width, screen->height, - 0x10, 0x10, 0x10)); + rgb->r, rgb->g, rgb->b)); packet2_update(packet2, draw_enable_tests(packet2->next, 0, t_zBuffer)); packet2_update(packet2, draw_finish(packet2->next)); packet2_chain_close_tag(packet2); @@ -118,14 +118,14 @@ void GifSender::sendPacket() } /** Adds clear screen to current packet */ -void GifSender::addClear(zbuffer_t *t_zBuffer) +void GifSender::addClear(zbuffer_t *t_zBuffer, color_t *rgb) { packet2_chain_open_cnt(currentPacket, 0, 0, 0); packet2_update(currentPacket, draw_disable_tests(currentPacket->next, 0, t_zBuffer)); packet2_update(currentPacket, draw_clear(currentPacket->next, 0, 2048.0F - (screen->width / 2), 2048.0F - (screen->height / 2), screen->width, screen->height, - 0x10, 0x10, 0x10)); + rgb->r, rgb->g, rgb->b)); packet2_update(currentPacket, draw_enable_tests(currentPacket->next, 0, t_zBuffer)); packet2_chain_close_tag(currentPacket); } @@ -180,7 +180,7 @@ void GifSender::calc3DObject(Matrix t_perspective, Mesh &t_mesh, u32 vertexCount if (SHOULD_BE_LIGHTED) { - const u16 lightsCount = Light::getLightsCount(t_bulbsCount); + const u16 lightsCount = light->getLightsCount(t_bulbsCount); VECTOR *lightDirections = new VECTOR[lightsCount]; VECTOR *lightColors = new VECTOR[lightsCount]; int *lightTypes = new int[lightsCount]; @@ -188,7 +188,7 @@ 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, lightsCount, t_mesh.position); + light->calculateLight(lightDirections, lightColors, lightTypes, t_bulbs, lightsCount, t_mesh.position); calculate_lights(lights, vertexCount, normals, lightDirections, lightColors, lightTypes, lightsCount); diff --git a/src/engine/modules/light.cpp b/src/engine/modules/light.cpp index b8a1d09..19f2702 100644 --- a/src/engine/modules/light.cpp +++ b/src/engine/modules/light.cpp @@ -26,6 +26,8 @@ const u8 ADDITIONAL_LIGHTS = 1; // Ambient const float LIGHT_MAX = 255.0F; const float LIGHT_DIS_MOD = 4.0F; +void Light::setAmbientLight(const Vector3 &t_rgb) { ambientLight.set(t_rgb); } + u16 Light::getLightsCount(u32 t_bulbsCount) { return t_bulbsCount + ADDITIONAL_LIGHTS; } /** Calculates lighting. Used in gifSender in object 3D calculations */ @@ -40,9 +42,9 @@ void Light::calculateLight(VECTOR *t_lightDirections, VECTOR *t_lightColors, int t_lightDirections[0][2] = 0.0F; t_lightDirections[0][3] = 1.0F; - t_lightColors[0][0] = 0.0F; - t_lightColors[0][1] = 0.0F; - t_lightColors[0][2] = 0.0F; + t_lightColors[0][0] = ambientLight.x; + t_lightColors[0][1] = ambientLight.y; + t_lightColors[0][2] = ambientLight.z; t_lightColors[0][3] = 1.0F; // --- diff --git a/src/engine/modules/renderer.cpp b/src/engine/modules/renderer.cpp index 6e33355..9b55597 100644 --- a/src/engine/modules/renderer.cpp +++ b/src/engine/modules/renderer.cpp @@ -44,9 +44,12 @@ Renderer::Renderer(u32 t_packetSize, ScreenSettings *t_screen) allocateBuffers(t_screen->width, t_screen->height); initDrawingEnv(t_screen->width, t_screen->height); setPrim(); + worldColor.r = 0x10; + worldColor.g = 0x10; + worldColor.b = 0x10; screen = t_screen; - gifSender = new GifSender(t_packetSize, t_screen); - vifSender = new VifSender(); + gifSender = new GifSender(t_packetSize, t_screen, &light); + vifSender = new VifSender(&light); perspective.setPerspective(*t_screen); renderData.perspective = &perspective; PRINT_LOG("Renderer initialized!"); @@ -187,6 +190,13 @@ void Renderer::setPrim() PRINT_LOG("Prim set!"); } +void Renderer::setWorldColor(const color_t &t_rgb) +{ + worldColor.r = t_rgb.r; + worldColor.g = t_rgb.g; + worldColor.b = t_rgb.b; +} + /** Defines and allocates framebuffers and zbuffer */ void Renderer::allocateBuffers(float t_screenW, float t_screenH) { @@ -305,7 +315,7 @@ void Renderer::beginFrameIfNeeded() if (isFrameEmpty) { isFrameEmpty = false; - gifSender->sendClear(&zBuffer); + gifSender->sendClear(&zBuffer, &worldColor); } } diff --git a/src/engine/modules/vif_sender.cpp b/src/engine/modules/vif_sender.cpp index 7458a58..9158a8d 100644 --- a/src/engine/modules/vif_sender.cpp +++ b/src/engine/modules/vif_sender.cpp @@ -29,8 +29,9 @@ extern u32 VU1Draw3D_CodeStart __attribute__((section(".vudata"))); extern u32 VU1Draw3D_CodeEnd __attribute__((section(".vudata"))); // -VifSender::VifSender() +VifSender::VifSender(Light *t_light) { + light = t_light; PRINT_LOG("Initializing VifSender"); PRINT_LOG("VifSender initialized!"); dma_channel_initialize(DMA_CHANNEL_VIF1, NULL, 0); From e00e677d1ee9bc9c636f0f4a2d3a7d74d7b6a6fc Mon Sep 17 00:00:00 2001 From: h4570 Date: Mon, 7 Dec 2020 21:04:38 +0100 Subject: [PATCH 2/3] fixed naming convention --- src/engine/include/modules/gif_sender.hpp | 4 ++-- src/engine/modules/gif_sender.cpp | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/engine/include/modules/gif_sender.hpp b/src/engine/include/modules/gif_sender.hpp index 88b9676..0c7646b 100644 --- a/src/engine/include/modules/gif_sender.hpp +++ b/src/engine/include/modules/gif_sender.hpp @@ -35,9 +35,9 @@ public: void initPacket(u8 context); void addObject(RenderData *t_renderData, Mesh &t_mesh, u32 vertexCount, VECTOR *vertices, VECTOR *normals, VECTOR *coordinates, LightBulb *t_bulbs, u16 t_bulbsCount, texbuffer_t *textureBuffer); - void addClear(zbuffer_t *t_zBuffer, color_t *rgb); + void addClear(zbuffer_t *t_zBuffer, color_t *t_rgb); void sendPacket(); - void sendClear(zbuffer_t *t_zBuffer, color_t *rgb); + void sendClear(zbuffer_t *t_zBuffer, color_t *t_rgb); static void sendTexture(Texture &texture, texbuffer_t *t_texBuffer); private: diff --git a/src/engine/modules/gif_sender.cpp b/src/engine/modules/gif_sender.cpp index d923049..08dad58 100644 --- a/src/engine/modules/gif_sender.cpp +++ b/src/engine/modules/gif_sender.cpp @@ -76,7 +76,7 @@ void GifSender::sendTexture(Texture &texture, texbuffer_t *t_texBuffer) packet2_free(packet2); } -void GifSender::sendClear(zbuffer_t *t_zBuffer, color_t *rgb) +void GifSender::sendClear(zbuffer_t *t_zBuffer, color_t *t_rgb) { packet2_t *packet2 = packet2_create(36, P2_TYPE_NORMAL, P2_MODE_CHAIN, false); packet2_chain_open_end(packet2, 0, 0); @@ -84,7 +84,7 @@ void GifSender::sendClear(zbuffer_t *t_zBuffer, color_t *rgb) packet2_update(packet2, draw_clear(packet2->next, 0, 2048.0F - (screen->width / 2), 2048.0F - (screen->height / 2), screen->width, screen->height, - rgb->r, rgb->g, rgb->b)); + t_rgb->r, t_rgb->g, t_rgb->b)); packet2_update(packet2, draw_enable_tests(packet2->next, 0, t_zBuffer)); packet2_update(packet2, draw_finish(packet2->next)); packet2_chain_close_tag(packet2); @@ -118,14 +118,14 @@ void GifSender::sendPacket() } /** Adds clear screen to current packet */ -void GifSender::addClear(zbuffer_t *t_zBuffer, color_t *rgb) +void GifSender::addClear(zbuffer_t *t_zBuffer, color_t *t_rgb) { packet2_chain_open_cnt(currentPacket, 0, 0, 0); packet2_update(currentPacket, draw_disable_tests(currentPacket->next, 0, t_zBuffer)); packet2_update(currentPacket, draw_clear(currentPacket->next, 0, 2048.0F - (screen->width / 2), 2048.0F - (screen->height / 2), screen->width, screen->height, - rgb->r, rgb->g, rgb->b)); + t_rgb->r, t_rgb->g, t_rgb->b)); packet2_update(currentPacket, draw_enable_tests(currentPacket->next, 0, t_zBuffer)); packet2_chain_close_tag(currentPacket); } From 0474fc07a583dcd2ac252187ba8e07c557add7cb Mon Sep 17 00:00:00 2001 From: h4570 Date: Mon, 7 Dec 2020 21:05:46 +0100 Subject: [PATCH 3/3] doc: usefull tip for loadADPCM() --- src/engine/include/modules/audio.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/engine/include/modules/audio.hpp b/src/engine/include/modules/audio.hpp index 8bfa868..ea1dfe2 100644 --- a/src/engine/include/modules/audio.hpp +++ b/src/engine/include/modules/audio.hpp @@ -87,7 +87,8 @@ public: // ADPCM /** - * Load ADPCM sample. + * Load ADPCM sample. ADPCM sample is an output from + * "adpenc" tool, shipped with PS2SDK. * @param t_path Example: "hit.adpcm" or "folder/jump.adpcm" */ audsrv_adpcm_t *loadADPCM(char *t_path);