From 51702d5ef72f78cde0e6d542576fecc78749217c Mon Sep 17 00:00:00 2001 From: Foxar Date: Tue, 1 Dec 2020 17:21:07 +0100 Subject: [PATCH 001/108] Switched audio filenames to available binaries --- src/samples/ari/ari.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/samples/ari/ari.cpp b/src/samples/ari/ari.cpp index bfdf545..e43adc4 100644 --- a/src/samples/ari/ari.cpp +++ b/src/samples/ari/ari.cpp @@ -34,8 +34,8 @@ void Ari::onInit() { engine->renderer->setCameraDefinitions(&camera.worldView, &camera.position, camera.planes); engine->audio.setSongVolume(40); - // engine->audio.loadSong("MOV-CIRC.WAV"); - engine->audio.loadSong("nob-else.wav"); + engine->audio.loadSong("MOV-CIRC.WAV"); + //engine->audio.loadSong("nob-else.wav"); adpcm1 = engine->audio.loadADPCM("ziobro1.adpcm"); adpcm2 = engine->audio.loadADPCM("ziobro2.adpcm"); From 7e676916a020e8991ec38ec85bce2da2caf7f787 Mon Sep 17 00:00:00 2001 From: Foxar Date: Wed, 2 Dec 2020 20:54:19 +0100 Subject: [PATCH 002/108] Added new sample dolphin. --- src/samples/dolphin/Makefile | 65 +++++++++++++++++++++++++ src/samples/dolphin/camera.cpp | 67 ++++++++++++++++++++++++++ src/samples/dolphin/camera.hpp | 41 ++++++++++++++++ src/samples/dolphin/driver.cpp | 48 ++++++++++++++++++ src/samples/dolphin/driver.hpp | 45 +++++++++++++++++ src/samples/dolphin/main.cpp | 21 ++++++++ src/samples/dolphin/objects/player.cpp | 52 ++++++++++++++++++++ src/samples/dolphin/objects/player.hpp | 32 ++++++++++++ 8 files changed, 371 insertions(+) create mode 100755 src/samples/dolphin/Makefile create mode 100755 src/samples/dolphin/camera.cpp create mode 100755 src/samples/dolphin/camera.hpp create mode 100755 src/samples/dolphin/driver.cpp create mode 100755 src/samples/dolphin/driver.hpp create mode 100755 src/samples/dolphin/main.cpp create mode 100755 src/samples/dolphin/objects/player.cpp create mode 100755 src/samples/dolphin/objects/player.hpp diff --git a/src/samples/dolphin/Makefile b/src/samples/dolphin/Makefile new file mode 100755 index 0000000..d59fe5e --- /dev/null +++ b/src/samples/dolphin/Makefile @@ -0,0 +1,65 @@ +EE_BIN = driver.elf + +EE_OBJS = \ + ../../engine/models/math/matrix.o \ + ../../engine/models/math/plane.o \ + ../../engine/models/math/point.o \ + ../../engine/models/math/vector3.o \ + ../../engine/models/mesh_frame.o \ + ../../engine/models/mesh_material.o \ + ../../engine/models/mesh.o \ + ../../engine/models/mesh_texture.o \ + ../../engine/modules/audio.o \ + ../../engine/modules/camera_base.o \ + ../../engine/modules/file_service.o \ + ../../engine/modules/gif_sender.o \ + ../../engine/modules/light.o \ + ../../engine/modules/pad.o \ + ../../engine/modules/renderer.o \ + ../../engine/modules/texture_repository.o \ + ../../engine/modules/timer.o \ + ../../engine/modules/vif_sender.o \ + ../../engine/utils/math.o \ + ../../engine/utils/string.o \ + ../../engine/loaders/bmp_loader.o \ + ../../engine/loaders/dff_loader.o \ + ../../engine/loaders/md2_loader.o \ + ../../engine/loaders/obj_loader.o \ + ../../engine/vu1_progs/draw3D.o \ + ../../engine/engine.o \ + camera.o \ + objects/player.o \ + driver.o \ + main.o + +EE_LIBS = -ldraw -lgraph -lmath3d -lpacket -ldma -lpacket2 -lpad -laudsrv -lc -lstdc++ +EE_INCS := -I./../../engine/include $(EE_INCS) +EE_DVP = dvp-as +EE_VCL = vcl + +all: $(EE_BIN) + $(EE_STRIP) --strip-all $(EE_BIN) + mv $(EE_BIN) bin/$(EE_BIN) + rm $(EE_OBJS) + +# https://github.com/microsoft/wsl/issues/2468#issuecomment-374904520 +#%.vsm: %.vcl # sudo service binfmt-support start +# $(EE_VCL) $< >> $@ + +%.o: %.vsm + $(EE_DVP) $< -o $@ + +clean: + rm -f $(EE_OBJS) + +run: $(EE_BIN) + killall -v ps2client || true + ps2client reset + ps2client reset + $(EE_STRIP) --strip-all $(EE_BIN) + mv $(EE_BIN) bin/$(EE_BIN) + rm $(EE_OBJS) + cd bin/ && ps2client execee host:$(EE_BIN) + +include $(PS2SDK)/samples/Makefile.pref +include $(PS2SDK)/samples/Makefile.eeglobal diff --git a/src/samples/dolphin/camera.cpp b/src/samples/dolphin/camera.cpp new file mode 100755 index 0000000..8ab5c12 --- /dev/null +++ b/src/samples/dolphin/camera.cpp @@ -0,0 +1,67 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Michał Mostowik +*/ + +#include "camera.hpp" + +#include + +const float CAMERA_Y = 15.0F; + + +Camera::Camera(ScreenSettings *t_screen) : CameraBase(t_screen, &position, &up) +{ + verticalLevel = 80.0F; + up.x = 0.0F; + up.y = 1.0F; + up.z = 0.0F; +} + +Camera::~Camera() {} + +void Camera::update(Pad &t_pad, Mesh &t_mesh) +{ + rotate(t_pad); + followBy(t_mesh); + Vector3 lookPos = Vector3(t_mesh.position.x, t_mesh.position.y + 10.0F, t_mesh.position.z); + lookAt(lookPos); +} + +void Camera::rotate(Pad &t_pad) +{ + if (t_pad.rJoyH <= 50) + horizontalLevel -= 0.08; + else if (t_pad.rJoyH >= 200) + horizontalLevel += 0.04; + if (t_pad.rJoyV <= 50 && verticalLevel > 25.0F) + verticalLevel -= 0.9F; + else if (t_pad.rJoyV >= 200 && verticalLevel < 80.0F) + verticalLevel += 0.9F; + unitCirclePosition.x = (Math::sin(horizontalLevel) * verticalLevel); + unitCirclePosition.y = CAMERA_Y; + unitCirclePosition.z = (Math::cos(horizontalLevel) * verticalLevel); +} + +void Camera::followBy(Mesh &t_mesh) +{ + position.x = unitCirclePosition.x + t_mesh.position.x; + position.y = unitCirclePosition.y + t_mesh.position.y; + position.z = unitCirclePosition.z + t_mesh.position.z; + + if (position.x > (-0.04F * verticalLevel) && + position.x < 0.0F && + position.z > (0.998F * verticalLevel)) + { + // TODO + // position.x = 0.0F; + // position.y = 0.0F; + // position.z = 0.0F; + // horizontalLevel = 0.0F; + } +} \ No newline at end of file diff --git a/src/samples/dolphin/camera.hpp b/src/samples/dolphin/camera.hpp new file mode 100755 index 0000000..7bcbde3 --- /dev/null +++ b/src/samples/dolphin/camera.hpp @@ -0,0 +1,41 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Michał Mostowik +*/ + +#ifndef _CAMERA_ +#define _CAMERA_ + +#include +#include +#include +#include +#include +#include + +class Camera : public CameraBase +{ + +public: + Vector3 position,up, unitCirclePosition; + float horizontalLevel, verticalLevel; + + Camera(ScreenSettings *t_screen); + ~Camera(); + + void update(Pad &t_pad, Mesh &t_mesh); + void rotate(Pad &t_pad); + void followBy(Mesh &t_mesh); + +protected: + Vector3 *getPosition() { return &position; }; + Vector3 *getUp() { return &up; }; + ScreenSettings screen; +}; + +#endif diff --git a/src/samples/dolphin/driver.cpp b/src/samples/dolphin/driver.cpp new file mode 100755 index 0000000..262f811 --- /dev/null +++ b/src/samples/dolphin/driver.cpp @@ -0,0 +1,48 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Michał Mostowik +*/ + +#include "driver.hpp" +#include "utils/math.hpp" + +Driver::Driver(Engine *t_engine) : engine(t_engine), camera(&engine->screen) +{ + +} + +Driver::~Driver() {} + +void Driver::onInit() +{ + engine->renderer->setCameraDefinitions(&camera.worldView, &camera.position, camera.planes); + + texRepo = engine->renderer->getTextureRepository(); + + engine->renderer->disableVSync(); + + island.loadDff("sunnyisl/", "sunnyisl", 1.0F, false); + island.rotation.x = -1.6F; + island.position.set(0.0F, 10.0F, 20.0F); + island.shouldBeBackfaceCulled = true; + island.shouldBeFrustumCulled = false; + + texRepo->addByMesh("dolphin/",player.mesh); + texRepo->addByMesh("sunnyisl/",island); + + bulb.intensity=55; + bulb.position.set(5.0F,10.0F,10.0f); +} + +void Driver::onUpdate() +{ + player.update(engine->pad); + camera.update(engine->pad,player.mesh); + engine->renderer->draw(island); + engine->renderer->draw(player.mesh); +} diff --git a/src/samples/dolphin/driver.hpp b/src/samples/dolphin/driver.hpp new file mode 100755 index 0000000..ce25589 --- /dev/null +++ b/src/samples/dolphin/driver.hpp @@ -0,0 +1,45 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Michał Mostowik +*/ + +#ifndef _DRIVER_ +#define _DRIVER_ + +#include +#include +#include +#include +#include +#include +#include "./camera.hpp" +#include "./objects/player.hpp" + + +class Driver : public Game +{ + +public: + Driver(Engine* t_engine); + ~Driver(); + + void onInit(); + void onUpdate(); + + Engine *engine; +private: + LightBulb bulb; + Player player; + Camera camera; + Mesh island; + TextureRepository *texRepo; + + +}; + +#endif diff --git a/src/samples/dolphin/main.cpp b/src/samples/dolphin/main.cpp new file mode 100755 index 0000000..4090b93 --- /dev/null +++ b/src/samples/dolphin/main.cpp @@ -0,0 +1,21 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Michał Mostowik +*/ + +#include "driver.hpp" + +int main() +{ + Engine engine = Engine(); + Driver game = Driver(&engine); + game.engine->init(&game,128); + SleepThread(); + return 0; + +} \ No newline at end of file diff --git a/src/samples/dolphin/objects/player.cpp b/src/samples/dolphin/objects/player.cpp new file mode 100755 index 0000000..851f372 --- /dev/null +++ b/src/samples/dolphin/objects/player.cpp @@ -0,0 +1,52 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Michał Mostowik +*/ + +#include "player.hpp" + +#include +#include +#include +#include +#include + +Player::Player() +{ + mesh.loadMD2("dolphin/","dolphin",0.05F,true); + mesh.shouldBeFrustumCulled = false; + mesh.position.set(0.0F,20.0F,-10.0f); + mesh.rotation.x = -1.6F; + mesh.setAnimSpeed(0.05F); +} + +Player::~Player() +{ + +} + +void Player::update(Pad& t_pad) +{ + velocity=0; + if(t_pad.lJoyV <= 100) + velocity=1; + else if(t_pad.lJoyV >= 200) + velocity=-1; + + if(t_pad.lJoyH >= 200) + mesh.rotation.z-=0.1; + else if(t_pad.lJoyH <= 100) + mesh.rotation.z+=0.1; + + if(velocity>0 && mesh.getCurrentAnimationFrame()==0) + mesh.playAnimation(0, 14); + + mesh.position.z+=Math::cos(mesh.rotation.z)*velocity; + mesh.position.x+=Math::sin(mesh.rotation.z)*velocity; + +} \ No newline at end of file diff --git a/src/samples/dolphin/objects/player.hpp b/src/samples/dolphin/objects/player.hpp new file mode 100755 index 0000000..b996c1c --- /dev/null +++ b/src/samples/dolphin/objects/player.hpp @@ -0,0 +1,32 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Michał Mostowik +*/ + +#ifndef _PLAYER_ +#define _PLAYER_ + +#include "../camera.hpp" +#include +#include + +class Player +{ + +public: + float gravity, velocity; + Mesh mesh; + Player(); + ~Player(); + void update(Pad &t_pad); +private: + Vector3 playerNextPosition; + +}; + +#endif From a6f8bb9b3617c204b5087d595da63c9eb276c598 Mon Sep 17 00:00:00 2001 From: h4570 Date: Wed, 2 Dec 2020 21:40:40 +0100 Subject: [PATCH 003/108] removed ari sample --- src/samples/ari/.gitignore | 3 - src/samples/ari/Makefile | 33 ------- src/samples/ari/README.MD | 19 ---- src/samples/ari/ari.cpp | 137 ----------------------------- src/samples/ari/ari.hpp | 50 ----------- src/samples/ari/camera.cpp | 82 ----------------- src/samples/ari/camera.hpp | 42 --------- src/samples/ari/main.cpp | 20 ----- src/samples/ari/objects/player.cpp | 44 --------- src/samples/ari/objects/player.hpp | 65 -------------- 10 files changed, 495 deletions(-) delete mode 100644 src/samples/ari/.gitignore delete mode 100644 src/samples/ari/Makefile delete mode 100644 src/samples/ari/README.MD delete mode 100644 src/samples/ari/ari.cpp delete mode 100644 src/samples/ari/ari.hpp delete mode 100644 src/samples/ari/camera.cpp delete mode 100644 src/samples/ari/camera.hpp delete mode 100644 src/samples/ari/main.cpp delete mode 100644 src/samples/ari/objects/player.cpp delete mode 100644 src/samples/ari/objects/player.hpp diff --git a/src/samples/ari/.gitignore b/src/samples/ari/.gitignore deleted file mode 100644 index 23c6dd9..0000000 --- a/src/samples/ari/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -.vscode/ -bin/** -fonts/** \ No newline at end of file diff --git a/src/samples/ari/Makefile b/src/samples/ari/Makefile deleted file mode 100644 index af3f457..0000000 --- a/src/samples/ari/Makefile +++ /dev/null @@ -1,33 +0,0 @@ -EE_BIN = ari.elf - -TYRA_DIR = ./../../engine - -EE_OBJS = \ - camera.o \ - objects/player.o \ - ari.o \ - main.o - -EE_LIBS = -ltyra - -all: $(EE_BIN) - $(EE_STRIP) --strip-all $(EE_BIN) - mv $(EE_BIN) bin/$(EE_BIN) - rm $(EE_OBJS) - -rebuild-engine: - cd $(TYRA_DIR) && make clean && make - -clean: - rm -f $(EE_OBJS) - -run: $(EE_BIN) - killall -v ps2client || true - ps2client reset - ps2client reset - $(EE_STRIP) --strip-all $(EE_BIN) - mv $(EE_BIN) bin/$(EE_BIN) - rm $(EE_OBJS) - cd bin/ && ps2client execee host:$(EE_BIN) - -include $(TYRA_DIR)/Makefile.pref diff --git a/src/samples/ari/README.MD b/src/samples/ari/README.MD deleted file mode 100644 index 46f3cdd..0000000 --- a/src/samples/ari/README.MD +++ /dev/null @@ -1,19 +0,0 @@ -## Ari sample - -### Description - -* .obj (skybox, water) -* .dff (island) -* .md2 (ari) -* mesh cloning (water) - -Testing demo. In future there will be Ariana Grande demo. - -### Assets -[Download](http://apgcglz.cluster028.hosting.ovh.net/tyra/1.1.0/samples/ari/assets.zip) - -### Screenshot - -[![Ari screenshot][ari-screenshot]](#) - -[ari-screenshot]: http://apgcglz.cluster028.hosting.ovh.net/tyra/1.1.0/samples/ari/ari.gif diff --git a/src/samples/ari/ari.cpp b/src/samples/ari/ari.cpp deleted file mode 100644 index 3c33919..0000000 --- a/src/samples/ari/ari.cpp +++ /dev/null @@ -1,137 +0,0 @@ -/* -# ______ ____ ___ -# | \/ ____| |___| -# | | | \ | | -#----------------------------------------------------------------------- -# Copyright 2020, tyra - https://github.com/h4570/tyra -# Licenced under Apache License 2.0 -# Sandro Sobczyński -*/ - -#include "ari.hpp" - -#include "utils/math.hpp" - -// ---- -// Constructors/Destructors -// ---- - -const u8 WATER_TILES_COUNT = 64; - -Ari::Ari(Engine *t_engine) : engine(t_engine), camera(&engine->screen) -{ - waterFloors = new Mesh[WATER_TILES_COUNT]; - spirals = new Point[WATER_TILES_COUNT]; -} - -Ari::~Ari() {} - -// ---- -// Methods -// ---- - -void Ari::onInit() -{ - engine->renderer->setCameraDefinitions(&camera.worldView, &camera.position, camera.planes); - engine->audio.setSongVolume(40); - // engine->audio.loadSong("MOV-CIRC.WAV"); - engine->audio.loadSong("nob-else.wav"); - - adpcm1 = engine->audio.loadADPCM("ziobro1.adpcm"); - adpcm2 = engine->audio.loadADPCM("ziobro2.adpcm"); - - texRepo = engine->renderer->getTextureRepository(); - engine->renderer->disableVSync(); - island.loadDff("sunnyisl/", "sunnyisl", 0.1F, false); - island.rotation.x = -1.6F; - island.position.set(0.0F, 10.0F, 20.0F); - island.shouldBeBackfaceCulled = true; - island.shouldBeFrustumCulled = false; - - fist.size.set(100.0F, 100.0F); - Texture *fistTex = texRepo->add("2d/", "fist", PNG); - fistTex->addLink(fist.getId()); - - islandAddons.loadDff("sunnyisl/", "sunnyisl3", 0.1F, false); - islandAddons.shouldBeBackfaceCulled = true; - islandAddons.rotation.x = -1.6F; - islandAddons.position.set(0.0F, 10.0F, 20.0F); - - skybox.loadObj("skybox/", "skybox", 100.0F, false); - skybox.shouldBeFrustumCulled = false; - - waterFloors[0].loadObj("water/", "water", 5.0F, false); - waterFloors[0].position.set(0.0F, 8.0F, 0.0F); - texRepo->addByMesh("water/", waterFloors[0], BMP); - for (u8 i = 0; i < WATER_TILES_COUNT; i++) - { - spirals[i].x = 1.0F; - spirals[i].y = 2.0F; - } - u32 spiralOffset = (u32)Math::sqrt(WATER_TILES_COUNT); - calcSpiral(spiralOffset, spiralOffset); - for (u8 i = 1; i < WATER_TILES_COUNT; i++) - { - waterFloors[i].loadFrom(waterFloors[0]); - waterFloors[i].position.set(10.0F * spirals[i].x, 8.0F, 10.0F * spirals[i].y); - texRepo->getByMesh(waterFloors[0].getId(), waterFloors[0].getMaterial(0).getId()) - ->addLink(waterFloors[i].getId(), waterFloors[i].getMaterial(0).getId()); - } - - texRepo->addByMesh("sunnyisl/", island, BMP); - texRepo->addByMesh("sunnyisl/", islandAddons, BMP); - texRepo->addByMesh("skybox/", skybox, BMP); - texRepo->addByMesh("ari/", player.mesh, BMP); - // engine->audio.playSong(); -} - -void Ari::initBulb() -{ - bulb.intensity = 15; - bulb.position.set(5.0F, 10.0F, 10.0F); -} - -void Ari::onUpdate() -{ - if (engine->pad.isCrossClicked) - { - // engine->audio.loadSong("nob-else.wav"); - engine->audio.playADPCM(adpcm1); - printf("FPS:%f\n", engine->fps); - } - if (engine->pad.isCircleClicked) - engine->audio.playADPCM(adpcm2); - camera.update(engine->pad, player.mesh); - engine->renderer->draw(skybox); - engine->renderer->draw(fist); - engine->renderer->draw(island); - engine->renderer->draw(islandAddons); - engine->renderer->draw(player.mesh); - for (u8 i = 0; i < WATER_TILES_COUNT; i++) - engine->renderer->draw(waterFloors[i]); -} - -void Ari::calcSpiral(int X, int Y) -{ - int x, y, dx; - x = y = dx = 0; - int dy = -1; - int t = X > Y ? X : Y; - int maxI = t * t; - for (int i = 0; i < maxI; i++) - { - if ((-X / 2 <= x) && (x <= X / 2) && (-Y / 2 <= y) && (y <= Y / 2)) - { - spirals[i].x = x; - spirals[i].y = y; - } - if ((x == y) || ((x < 0) && (x == -y)) || ((x > 0) && (x == 1 - y))) - { - t = dx; - dx = -dy; - dy = t; - } - x += dx; - y += dy; - } -} diff --git a/src/samples/ari/ari.hpp b/src/samples/ari/ari.hpp deleted file mode 100644 index 549d7dc..0000000 --- a/src/samples/ari/ari.hpp +++ /dev/null @@ -1,50 +0,0 @@ -/* -# ______ ____ ___ -# | \/ ____| |___| -# | | | \ | | -#----------------------------------------------------------------------- -# Copyright 2020, tyra - https://github.com/h4570/tyra -# Licenced under Apache License 2.0 -# Sandro Sobczyński -*/ - -#ifndef _ARI_ -#define _ARI_ - -#include -#include -#include -#include -#include "objects/player.hpp" -#include -#include -#include -#include "./camera.hpp" - -class Ari : public Game -{ - -public: - Ari(Engine *t_engine); - ~Ari(); - - void onInit(); - void onUpdate(); - - Engine *engine; - -private: - void initBulb(); - void calcSpiral(int X, int Y); - LightBulb bulb; - Player player; - Mesh island, islandAddons, skybox; - Mesh *waterFloors; - Sprite fist; - audsrv_adpcm_t *adpcm1, *adpcm2; - Point *spirals; - Camera camera; - TextureRepository *texRepo; -}; - -#endif diff --git a/src/samples/ari/camera.cpp b/src/samples/ari/camera.cpp deleted file mode 100644 index 9c506a0..0000000 --- a/src/samples/ari/camera.cpp +++ /dev/null @@ -1,82 +0,0 @@ -/* -# ______ ____ ___ -# | \/ ____| |___| -# | | | \ | | -#----------------------------------------------------------------------- -# Copyright 2020, tyra - https://github.com/h4570/tyra -# Licenced under Apache License 2.0 -# Sandro Sobczyński -*/ - -#include "camera.hpp" - -#include -#include - -const float CAMERA_Y = 15.0F; - -// ---- -// Constructors/Destructors -// ---- - -Camera::Camera(ScreenSettings *t_screen) : CameraBase(t_screen, &position, &up) -{ - PRINT_LOG("Initializing camera"); - verticalLevel = 80.0F; - up.x = 0.0F; - up.y = 1.0F; - up.z = 0.0F; - PRINT_LOG("Camera initialized!"); -} - -Camera::~Camera() {} - -// ---- -// Methods -// ---- - -void Camera::update(Pad &t_pad, Mesh &t_mesh) -{ - rotate(t_pad); - followBy(t_mesh); - Vector3 lookPos = Vector3(t_mesh.position.x, t_mesh.position.y + 10.0F, t_mesh.position.z); - lookAt(lookPos); -} - -/** Set camera rotation by pad right joy and update unit circle - * https://en.wikipedia.org/wiki/Unit_circle - * https://www.desmos.com/calculator/3e7iypw4ow - */ -void Camera::rotate(Pad &t_pad) -{ - if (t_pad.rJoyH <= 50) - horizontalLevel -= 0.08; - else if (t_pad.rJoyH >= 200) - horizontalLevel += 0.04; - if (t_pad.rJoyV <= 50 && verticalLevel > 25.0F) - verticalLevel -= 0.9F; - else if (t_pad.rJoyV >= 200 && verticalLevel < 80.0F) - verticalLevel += 0.9F; - unitCirclePosition.x = (Math::sin(horizontalLevel) * verticalLevel); - unitCirclePosition.y = CAMERA_Y; - unitCirclePosition.z = (Math::cos(horizontalLevel) * verticalLevel); -} - -/** Rotate camera around 3D object */ -void Camera::followBy(Mesh &t_mesh) -{ - position.x = unitCirclePosition.x + t_mesh.position.x; - position.y = unitCirclePosition.y + t_mesh.position.y; - position.z = unitCirclePosition.z + t_mesh.position.z; - - if (position.x > (-0.04F * verticalLevel) && - position.x < 0.0F && - position.z > (0.998F * verticalLevel)) - { - // TODO - // position.x = 0.0F; - // position.y = 0.0F; - // position.z = 0.0F; - // horizontalLevel = 0.0F; - } -} diff --git a/src/samples/ari/camera.hpp b/src/samples/ari/camera.hpp deleted file mode 100644 index 4d6adf8..0000000 --- a/src/samples/ari/camera.hpp +++ /dev/null @@ -1,42 +0,0 @@ -/* -# ______ ____ ___ -# | \/ ____| |___| -# | | | \ | | -#----------------------------------------------------------------------- -# Copyright 2020, tyra - https://github.com/h4570/tyra -# Licenced under Apache License 2.0 -# Sandro Sobczyński -*/ - -#ifndef _CAMERA_ -#define _CAMERA_ - -#include -#include -#include -#include -#include -#include - -/** 3D camera which follow by 3D object. Can be rotated via pad */ -class Camera : public CameraBase -{ - -public: - Vector3 up, position, unitCirclePosition; - float horizontalLevel, verticalLevel; - - Camera(ScreenSettings *t_screen); - ~Camera(); - - void update(Pad &t_pad, Mesh &t_mesh); - void rotate(Pad &t_pad); - void followBy(Mesh &t_mesh); - -protected: - Vector3 *getPosition() { return &position; }; - Vector3 *getUp() { return &up; }; - ScreenSettings screen; -}; - -#endif diff --git a/src/samples/ari/main.cpp b/src/samples/ari/main.cpp deleted file mode 100644 index f76ae54..0000000 --- a/src/samples/ari/main.cpp +++ /dev/null @@ -1,20 +0,0 @@ -/* -# ______ ____ ___ -# | \/ ____| |___| -# | | | \ | | -#----------------------------------------------------------------------- -# Copyright 2020, tyra - https://github.com/h4570/tyra -# Licenced under Apache License 2.0 -# Sandro Sobczyński -*/ - -#include "ari.hpp" - -int main() -{ - Engine engine = Engine(); - Ari game = Ari(&engine); - game.engine->init(&game, 128); - SleepThread(); - return 0; -} diff --git a/src/samples/ari/objects/player.cpp b/src/samples/ari/objects/player.cpp deleted file mode 100644 index 2f7da88..0000000 --- a/src/samples/ari/objects/player.cpp +++ /dev/null @@ -1,44 +0,0 @@ -/* -# ______ ____ ___ -# | \/ ____| |___| -# | | | \ | | -#----------------------------------------------------------------------- -# Copyright 2020, tyra - https://github.com/h4570/tyra -# Licenced under Apache License 2.0 -# Sandro Sobczyński -*/ - -#include "player.hpp" - -#include -#include -#include -#include -#include - -// ---- -// Constructors/Destructors -// ---- - -Player::Player() -{ - PRINT_LOG("Creating player object"); - this->gravity = 0.1F; - this->lift = -1.0F; - this->mesh.loadMD2("ari/", "ari", 0.0001F, true); - this->mesh.position.set(0.0F, 10.0F, 0.0F); - this->mesh.shouldBeBackfaceCulled = true; - this->mesh.shouldBeFrustumCulled = false; - this->mesh.shouldBeLighted = true; - this->mesh.setAnimSpeed(0.05F); - this->mesh.playAnimation(0, 1); - PRINT_LOG("Player object created!"); -} - -Player::~Player() -{ -} - -// ---- -// Methods -// ---- diff --git a/src/samples/ari/objects/player.hpp b/src/samples/ari/objects/player.hpp deleted file mode 100644 index 0ffb771..0000000 --- a/src/samples/ari/objects/player.hpp +++ /dev/null @@ -1,65 +0,0 @@ -/* -# ______ ____ ___ -# | \/ ____| |___| -# | | | \ | | -#----------------------------------------------------------------------- -# Copyright 2020, tyra - https://github.com/h4570/tyra -# Licenced under Apache License 2.0 -# Sandro Sobczyński -*/ - -#ifndef _PLAYER_ -#define _PLAYER_ - -#include "../camera.hpp" -#include -#include - -/** Player 3D object class */ -class Player -{ - -public: - float gravity, velocity, lift; - u8 isOnFloor, isCollideFloor, indexOfCurrentFloor; - Mesh mesh; - Player(); - ~Player(); - -private: - Vector3 playerNextPosition; -}; - -// TODO Rysowanie idzie wg tekstur - -// 2 Graczy, 1 mesh 2 tekstury -// sharedFrames = objLoader.load("meshes/", "block", 1.0F); -// var addedTextures = texturesRepo.addByMaterialNames("textures/dirt/", sharedFrames); -// var addedTexture = texturesRepo.add("textures/blocks/sand.bmp"); -// dirt = new Mesh(sharedFrames); -// sand = new Mesh(sharedFrames); -// addedTextures[0].addUsage(dirt.Id, sharedFrames.getMaterialId(0)); -// addedTexture.addUsage(sand.Id, sand.getMaterialId(0)); - -// 1 gracz, 2 dynamic tekstury -// sharedFrames = objLoader.load("meshes/", "block", 1.0F); -// var addedTexture1 = texturesRepo.add("textures/blocks/sand.bmp"); -// var addedTexture2 = texturesRepo.add("textures/blocks/sand_damaged.bmp"); -// sand = new Mesh(sharedFrames); -// addedTexture1.removeUsage(sand.Id, sand.getMaterialId(0)); -// draw() -// addedTexture2.addUsage(sand.Id, sand.getMaterialId(0)); - -// 2 różne obj, te same tekstury -// frames1 = objLoader.load("meshes/", "island", 1.0F); -// frames2 = objLoader.load("meshes/", "islandAddons", 1.0F); -// island = new Mesh(frames1); -// islandAddons = new Mesh(frames2); -// var addedTextures1 = texturesRepo.addByMaterialNames("textures/island/",frames1); -// var addedTextures2 = texturesRepo.addByMaterialNames("textures/island/",frames2); -// texturesRepo.remove(addedTextures2[0]); // duplicate -// addedTextures1[0].addUsage(islandAddons.Id, islandAddons.getMaterialId(0)); - -// Mesh -> position, rotation, color, scale, shouldBeLighted, clut, lod - -#endif From 72266d0c251fe7f36a1ddef2dc28751b102810fa Mon Sep 17 00:00:00 2001 From: h4570 Date: Thu, 3 Dec 2020 18:25:17 +0100 Subject: [PATCH 004/108] fixed 2d alpha blending --- src/engine/modules/renderer.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/engine/modules/renderer.cpp b/src/engine/modules/renderer.cpp index cd8b484..c5b2a8e 100644 --- a/src/engine/modules/renderer.cpp +++ b/src/engine/modules/renderer.cpp @@ -123,12 +123,14 @@ void Renderer::draw(Sprite &t_sprite) packet2_update(packet2, draw_primitive_xyoffset(packet2->next, 0, 2048, 2048)); packet2_utils_gif_add_set(packet2, 1); packet2_utils_gs_add_texbuff_clut(packet2, &textureBuffer, &t_sprite.clut); + draw_enable_blending(); packet2_update(packet2, draw_rect_textured(packet2->next, 0, &rect)); packet2_update(packet2, draw_primitive_xyoffset( packet2->next, 0, (2048 - (screen->width / 2)), (2048 - (screen->height / 2)))); + draw_disable_blending(); packet2_update(packet2, draw_finish(packet2->next)); dma_channel_wait(DMA_CHANNEL_GIF, 0); dma_channel_send_packet2(packet2, DMA_CHANNEL_GIF, true); From bae834e5991b5fe429f9657ec587964c27be69a8 Mon Sep 17 00:00:00 2001 From: Foxar Date: Thu, 3 Dec 2020 18:33:20 +0100 Subject: [PATCH 005/108] Added water mesh, jumping animation, skybox and diving. --- src/samples/dolphin/Makefile | 48 +++--------------- src/samples/dolphin/camera.cpp | 4 +- src/samples/dolphin/driver.cpp | 48 ------------------ src/samples/dolphin/driver.hpp | 45 ----------------- src/samples/dolphin/main.cpp | 7 ++- src/samples/dolphin/objects/player.cpp | 70 +++++++++++++++++++------- src/samples/dolphin/objects/player.hpp | 9 ++-- 7 files changed, 73 insertions(+), 158 deletions(-) delete mode 100755 src/samples/dolphin/driver.cpp delete mode 100755 src/samples/dolphin/driver.hpp diff --git a/src/samples/dolphin/Makefile b/src/samples/dolphin/Makefile index d59fe5e..c4e5f83 100755 --- a/src/samples/dolphin/Makefile +++ b/src/samples/dolphin/Makefile @@ -1,53 +1,22 @@ -EE_BIN = driver.elf +EE_BIN = dolphin.elf + +TYRA_DIR = ./../../engine EE_OBJS = \ - ../../engine/models/math/matrix.o \ - ../../engine/models/math/plane.o \ - ../../engine/models/math/point.o \ - ../../engine/models/math/vector3.o \ - ../../engine/models/mesh_frame.o \ - ../../engine/models/mesh_material.o \ - ../../engine/models/mesh.o \ - ../../engine/models/mesh_texture.o \ - ../../engine/modules/audio.o \ - ../../engine/modules/camera_base.o \ - ../../engine/modules/file_service.o \ - ../../engine/modules/gif_sender.o \ - ../../engine/modules/light.o \ - ../../engine/modules/pad.o \ - ../../engine/modules/renderer.o \ - ../../engine/modules/texture_repository.o \ - ../../engine/modules/timer.o \ - ../../engine/modules/vif_sender.o \ - ../../engine/utils/math.o \ - ../../engine/utils/string.o \ - ../../engine/loaders/bmp_loader.o \ - ../../engine/loaders/dff_loader.o \ - ../../engine/loaders/md2_loader.o \ - ../../engine/loaders/obj_loader.o \ - ../../engine/vu1_progs/draw3D.o \ - ../../engine/engine.o \ camera.o \ objects/player.o \ - driver.o \ + dolphin.o \ main.o -EE_LIBS = -ldraw -lgraph -lmath3d -lpacket -ldma -lpacket2 -lpad -laudsrv -lc -lstdc++ -EE_INCS := -I./../../engine/include $(EE_INCS) -EE_DVP = dvp-as -EE_VCL = vcl +EE_LIBS = -ltyra all: $(EE_BIN) $(EE_STRIP) --strip-all $(EE_BIN) mv $(EE_BIN) bin/$(EE_BIN) rm $(EE_OBJS) -# https://github.com/microsoft/wsl/issues/2468#issuecomment-374904520 -#%.vsm: %.vcl # sudo service binfmt-support start -# $(EE_VCL) $< >> $@ - -%.o: %.vsm - $(EE_DVP) $< -o $@ +rebuild-engine: + cd $(TYRA_DIR) && make clean && make clean: rm -f $(EE_OBJS) @@ -61,5 +30,4 @@ run: $(EE_BIN) rm $(EE_OBJS) cd bin/ && ps2client execee host:$(EE_BIN) -include $(PS2SDK)/samples/Makefile.pref -include $(PS2SDK)/samples/Makefile.eeglobal +include $(TYRA_DIR)/Makefile.pref diff --git a/src/samples/dolphin/camera.cpp b/src/samples/dolphin/camera.cpp index 8ab5c12..0fb871f 100755 --- a/src/samples/dolphin/camera.cpp +++ b/src/samples/dolphin/camera.cpp @@ -14,7 +14,6 @@ const float CAMERA_Y = 15.0F; - Camera::Camera(ScreenSettings *t_screen) : CameraBase(t_screen, &position, &up) { verticalLevel = 80.0F; @@ -35,14 +34,17 @@ void Camera::update(Pad &t_pad, Mesh &t_mesh) void Camera::rotate(Pad &t_pad) { + if (t_pad.rJoyH <= 50) horizontalLevel -= 0.08; else if (t_pad.rJoyH >= 200) horizontalLevel += 0.04; + /* if (t_pad.rJoyV <= 50 && verticalLevel > 25.0F) verticalLevel -= 0.9F; else if (t_pad.rJoyV >= 200 && verticalLevel < 80.0F) verticalLevel += 0.9F; + */ unitCirclePosition.x = (Math::sin(horizontalLevel) * verticalLevel); unitCirclePosition.y = CAMERA_Y; unitCirclePosition.z = (Math::cos(horizontalLevel) * verticalLevel); diff --git a/src/samples/dolphin/driver.cpp b/src/samples/dolphin/driver.cpp deleted file mode 100755 index 262f811..0000000 --- a/src/samples/dolphin/driver.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/* -# ______ ____ ___ -# | \/ ____| |___| -# | | | \ | | -#----------------------------------------------------------------------- -# Copyright 2020, tyra - https://github.com/h4570/tyra -# Licenced under Apache License 2.0 -# Michał Mostowik -*/ - -#include "driver.hpp" -#include "utils/math.hpp" - -Driver::Driver(Engine *t_engine) : engine(t_engine), camera(&engine->screen) -{ - -} - -Driver::~Driver() {} - -void Driver::onInit() -{ - engine->renderer->setCameraDefinitions(&camera.worldView, &camera.position, camera.planes); - - texRepo = engine->renderer->getTextureRepository(); - - engine->renderer->disableVSync(); - - island.loadDff("sunnyisl/", "sunnyisl", 1.0F, false); - island.rotation.x = -1.6F; - island.position.set(0.0F, 10.0F, 20.0F); - island.shouldBeBackfaceCulled = true; - island.shouldBeFrustumCulled = false; - - texRepo->addByMesh("dolphin/",player.mesh); - texRepo->addByMesh("sunnyisl/",island); - - bulb.intensity=55; - bulb.position.set(5.0F,10.0F,10.0f); -} - -void Driver::onUpdate() -{ - player.update(engine->pad); - camera.update(engine->pad,player.mesh); - engine->renderer->draw(island); - engine->renderer->draw(player.mesh); -} diff --git a/src/samples/dolphin/driver.hpp b/src/samples/dolphin/driver.hpp deleted file mode 100755 index ce25589..0000000 --- a/src/samples/dolphin/driver.hpp +++ /dev/null @@ -1,45 +0,0 @@ -/* -# ______ ____ ___ -# | \/ ____| |___| -# | | | \ | | -#----------------------------------------------------------------------- -# Copyright 2020, tyra - https://github.com/h4570/tyra -# Licenced under Apache License 2.0 -# Michał Mostowik -*/ - -#ifndef _DRIVER_ -#define _DRIVER_ - -#include -#include -#include -#include -#include -#include -#include "./camera.hpp" -#include "./objects/player.hpp" - - -class Driver : public Game -{ - -public: - Driver(Engine* t_engine); - ~Driver(); - - void onInit(); - void onUpdate(); - - Engine *engine; -private: - LightBulb bulb; - Player player; - Camera camera; - Mesh island; - TextureRepository *texRepo; - - -}; - -#endif diff --git a/src/samples/dolphin/main.cpp b/src/samples/dolphin/main.cpp index 4090b93..9c5f37e 100755 --- a/src/samples/dolphin/main.cpp +++ b/src/samples/dolphin/main.cpp @@ -8,14 +8,13 @@ # Michał Mostowik */ -#include "driver.hpp" +#include "dolphin.hpp" int main() { Engine engine = Engine(); - Driver game = Driver(&engine); - game.engine->init(&game,128); + Dolphin game = Dolphin(&engine); + game.engine->init(&game, 128); SleepThread(); return 0; - } \ No newline at end of file diff --git a/src/samples/dolphin/objects/player.cpp b/src/samples/dolphin/objects/player.cpp index 851f372..207dfdc 100755 --- a/src/samples/dolphin/objects/player.cpp +++ b/src/samples/dolphin/objects/player.cpp @@ -18,35 +18,71 @@ Player::Player() { - mesh.loadMD2("dolphin/","dolphin",0.05F,true); + mesh.loadMD2("dolphin/", "dolphin", 0.025F, true); mesh.shouldBeFrustumCulled = false; - mesh.position.set(0.0F,20.0F,-10.0f); + mesh.position.set(0.0F, 0.0F, 10.0f); mesh.rotation.x = -1.6F; mesh.setAnimSpeed(0.05F); + isJumping = false; } Player::~Player() { - } -void Player::update(Pad& t_pad) +void Player::update(Pad &t_pad) { - velocity=0; - if(t_pad.lJoyV <= 100) - velocity=1; - else if(t_pad.lJoyV >= 200) - velocity=-1; + if (lift > 0.0F) + lift -= 0.05F; + if (lift < 0.0F) + lift += 0.05F; + if (lift < 0.1F && lift > -0.1F) + lift = 0; + velocity = 0; + if (t_pad.lJoyV <= 100) + velocity = 1; + else if (t_pad.lJoyV >= 200) + velocity = -1; - if(t_pad.lJoyH >= 200) - mesh.rotation.z-=0.1; - else if(t_pad.lJoyH <= 100) - mesh.rotation.z+=0.1; + if (t_pad.lJoyH >= 200) + mesh.rotation.z -= 0.1; + else if (t_pad.lJoyH <= 100) + mesh.rotation.z += 0.1; - if(velocity>0 && mesh.getCurrentAnimationFrame()==0) - mesh.playAnimation(0, 14); + if (t_pad.rJoyV >= 200 && lift <= 1) + lift += 0.2F; + else if (t_pad.rJoyV <= 100 && lift >= -1) + lift -= 0.2F; - mesh.position.z+=Math::cos(mesh.rotation.z)*velocity; - mesh.position.x+=Math::sin(mesh.rotation.z)*velocity; + if (t_pad.isCrossClicked && mesh.getCurrentAnimationFrame() <= 14 && WATER_LEVEL - 15 < mesh.position.y) + { + printf("Cross\n"); + mesh.setAnimSpeed(0.8F); + mesh.playAnimation(14, 58, 0); + } + if (mesh.getCurrentAnimationFrame() > 25 && mesh.getCurrentAnimationFrame() < 47) + isJumping = true; + else + isJumping = false; + + if (lift < 0) + mesh.rotation.y = 1; + else if (lift > 0) + mesh.rotation.y = -1; + else + mesh.rotation.y = 0; + + if (velocity > 0 && mesh.getCurrentAnimationFrame() == 0) + { + mesh.setAnimSpeed(0.05F); + printf("Swim animation\n"); + mesh.playAnimation(1, 14, 0); + } + + mesh.position.z += + Math::cos(mesh.rotation.z) * velocity * (isJumping + 1); + mesh.position.x += + Math::sin(mesh.rotation.z) * velocity * (isJumping + 1); + mesh.position.y += lift; } \ No newline at end of file diff --git a/src/samples/dolphin/objects/player.hpp b/src/samples/dolphin/objects/player.hpp index b996c1c..bd990be 100755 --- a/src/samples/dolphin/objects/player.hpp +++ b/src/samples/dolphin/objects/player.hpp @@ -11,6 +11,8 @@ #ifndef _PLAYER_ #define _PLAYER_ +#define WATER_LEVEL -5.0F + #include "../camera.hpp" #include #include @@ -19,14 +21,15 @@ class Player { public: - float gravity, velocity; + float gravity, velocity, lift; Mesh mesh; Player(); ~Player(); void update(Pad &t_pad); -private: - Vector3 playerNextPosition; +private: + u8 isJumping; + Vector3 playerNextPosition; }; #endif From f45f5c39c74694ebc9c6f699d1d278e61dd06001 Mon Sep 17 00:00:00 2001 From: h4570 Date: Thu, 3 Dec 2020 20:04:35 +0100 Subject: [PATCH 006/108] small rework of timer class --- src/engine/engine.cpp | 2 +- src/engine/include/modules/timer.hpp | 3 ++- src/engine/modules/timer.cpp | 24 ++++++------------------ 3 files changed, 9 insertions(+), 20 deletions(-) diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index 76b7614..4a604ba 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -94,7 +94,7 @@ void Engine::gameLoop() fps = timer.getFPS(); fpsDelayer = 0; } - timer.primeTimer(); + timer.prime(); renderer->endFrame(fps); /** -6~ FPS */ SetAlarm(150, &Engine::wakeup, &mainThreadId); diff --git a/src/engine/include/modules/timer.hpp b/src/engine/include/modules/timer.hpp index 424e8ca..6e9dcd2 100644 --- a/src/engine/include/modules/timer.hpp +++ b/src/engine/include/modules/timer.hpp @@ -12,6 +12,7 @@ #define _TYRA_TIMER_ #include +#include /** Class responsible for fps counting */ class Timer @@ -22,7 +23,7 @@ public: ~Timer(); u32 getTimeDelta(); - void primeTimer(); + inline void prime() { lastTime = *T3_COUNT; } float getFPS(); private: diff --git a/src/engine/modules/timer.cpp b/src/engine/modules/timer.cpp index e62f76e..ac20ba8 100644 --- a/src/engine/modules/timer.cpp +++ b/src/engine/modules/timer.cpp @@ -10,16 +10,11 @@ #include "../include/modules/timer.hpp" -#include - // ---- // Constructors/Destructors // ---- -Timer::Timer() -{ - this->lastTime = *T3_COUNT; -} +Timer::Timer() { prime(); } Timer::~Timer() {} @@ -29,20 +24,13 @@ Timer::~Timer() {} u32 Timer::getTimeDelta() { - this->time = *T3_COUNT; + time = *T3_COUNT; - if (this->time < this->lastTime) // The counter has wrapped - this->change = this->time + (65536 - this->lastTime); + if (time < lastTime) // The counter has wrapped + change = time + (65536 - lastTime); else - this->change = this->time - this->lastTime; - - this->lastTime = this->time; - return this->change; -} - -void Timer::primeTimer() -{ - lastTime = *T3_COUNT; + change = time - lastTime; + return change; } float Timer::getFPS() From 17ef6b9fbc8196ad4fc8a0d47158373119d45140 Mon Sep 17 00:00:00 2001 From: Foxar Date: Thu, 3 Dec 2020 20:25:13 +0100 Subject: [PATCH 007/108] Revert "Switched audio filenames to available binaries" This reverts commit 51702d5ef72f78cde0e6d542576fecc78749217c. --- src/samples/ari/ari.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/samples/ari/ari.cpp b/src/samples/ari/ari.cpp index 52a00b4..3c33919 100644 --- a/src/samples/ari/ari.cpp +++ b/src/samples/ari/ari.cpp @@ -34,8 +34,8 @@ void Ari::onInit() { engine->renderer->setCameraDefinitions(&camera.worldView, &camera.position, camera.planes); engine->audio.setSongVolume(40); - engine->audio.loadSong("MOV-CIRC.WAV"); - //engine->audio.loadSong("nob-else.wav"); + // engine->audio.loadSong("MOV-CIRC.WAV"); + engine->audio.loadSong("nob-else.wav"); adpcm1 = engine->audio.loadADPCM("ziobro1.adpcm"); adpcm2 = engine->audio.loadADPCM("ziobro2.adpcm"); From 513ae84577106023a914be0d44420a753c90fddb Mon Sep 17 00:00:00 2001 From: h4570 Date: Thu, 3 Dec 2020 23:51:18 +0100 Subject: [PATCH 008/108] added const to some vector3 funcs --- src/engine/include/models/math/vector3.hpp | 4 ++-- src/engine/models/math/vector3.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/engine/include/models/math/vector3.hpp b/src/engine/include/models/math/vector3.hpp index a4bb4e0..55f392c 100644 --- a/src/engine/include/models/math/vector3.hpp +++ b/src/engine/include/models/math/vector3.hpp @@ -44,8 +44,8 @@ public: Vector3 operator-(void); static u8 shouldBeBackfaceCulled(const Vector3 *t_cameraPos, const Vector3 *v0, const Vector3 *v1, const Vector3 *v2); - u8 collidesSquare(Vector3 &t_min, Vector3 &t_max); - u8 isOnSquare(Vector3 &t_min, Vector3 &t_max); + u8 collidesSquare(const Vector3 &t_min, const Vector3 &t_max) const; + u8 isOnSquare(const Vector3 &t_min, const Vector3 &t_max) const; float length(); void normalize(); void setByLerp(const Vector3 &v1, const Vector3 &v2, const float &t_interp, const float &t_scale); diff --git a/src/engine/models/math/vector3.cpp b/src/engine/models/math/vector3.cpp index e05b36c..fbb772e 100644 --- a/src/engine/models/math/vector3.cpp +++ b/src/engine/models/math/vector3.cpp @@ -170,13 +170,13 @@ u8 Vector3::shouldBeBackfaceCulled(const Vector3 *t_cameraPos, const Vector3 *v0 } /** Checks intersection with given square */ -u8 Vector3::collidesSquare(Vector3 &t_min, Vector3 &t_max) +u8 Vector3::collidesSquare(const Vector3 &t_min, const Vector3 &t_max) const { return ((this->x <= t_max.x && this->x >= t_min.x) && (this->y < t_max.y && this->y >= t_min.y) && (this->z <= t_max.z && this->z >= t_min.z)) ? 1 : 0; } /** Checks is this vector is on given square */ -u8 Vector3::isOnSquare(Vector3 &t_min, Vector3 &t_max) +u8 Vector3::isOnSquare(const Vector3 &t_min, const Vector3 &t_max) const { return ((this->x <= t_max.x && this->x >= t_min.x) && (this->y >= t_max.y) && (this->z <= t_max.z && this->z >= t_min.z)) ? 1 : 0; } From b48d342adfb3e02af471e1dcd93928cee1837581 Mon Sep 17 00:00:00 2001 From: Foxar Date: Fri, 4 Dec 2020 00:20:43 +0100 Subject: [PATCH 009/108] Included the missing dolphin.cpp and hpp files. --- src/samples/dolphin/dolphin.cpp | 103 ++++++++++++++++++++++++++++++++ src/samples/dolphin/dolphin.hpp | 46 ++++++++++++++ 2 files changed, 149 insertions(+) create mode 100755 src/samples/dolphin/dolphin.cpp create mode 100755 src/samples/dolphin/dolphin.hpp diff --git a/src/samples/dolphin/dolphin.cpp b/src/samples/dolphin/dolphin.cpp new file mode 100755 index 0000000..8b622b3 --- /dev/null +++ b/src/samples/dolphin/dolphin.cpp @@ -0,0 +1,103 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Michał Mostowik +*/ + +#include "dolphin.hpp" +#include "utils/math.hpp" + +const u8 WATER_TILE_SCALE = 5.0F; +const u8 WATER_SIZE = 100.0F; + +Dolphin::Dolphin(Engine *t_engine) : engine(t_engine), camera(&engine->screen) +{ +} + +Dolphin::~Dolphin() {} + +void Dolphin::onInit() +{ + engine->renderer->setCameraDefinitions(&camera.worldView, &camera.position, camera.planes); + + texRepo = engine->renderer->getTextureRepository(); + + engine->renderer->disableVSync(); + + printf("loading island..\n"); + island.loadDff("sunnyisl/", "sunnyisl", 1.0F, false); + printf("Loaded..\n"); + island.rotation.x = -1.6F; + island.position.set(0.0F, 10.0F, 20.0F); + island.shouldBeBackfaceCulled = true; + island.shouldBeFrustumCulled = false; + + printf("Loading water overlay...\n"); + waterOverlay.size.set(100.0F, 100.0F); + Texture *watOverlayTex = texRepo->add("2d/", "underwater_overlay", PNG); + watOverlayTex->addLink(waterOverlay.getId()); + printf("Loaded.\n"); + + printf("Loading water...\n"); + water.loadObj("water/", "water", WATER_TILE_SCALE, false); + water.position.set(0, WATER_LEVEL, 0); + texRepo->addByMesh("water/", water, BMP); + water.shouldBeFrustumCulled = false; + water.shouldBeBackfaceCulled = false; + + skybox.loadObj("skybox/", "skybox", 400.0F, false); + skybox.shouldBeFrustumCulled = false; + + waterbox.loadObj("waterbox/", "waterbox", 250.0F, false); + waterbox.shouldBeFrustumCulled = false; + waterbox.shouldBeBackfaceCulled = false; + waterbox.rotation.x = Math::PI; + waterbox.position.y = -100.F; + + texRepo->addByMesh("dolphin/", player.mesh, BMP); + texRepo->addByMesh("sunnyisl/", island, BMP); + texRepo->addByMesh("skybox/", skybox, BMP); + texRepo->addByMesh("waterbox/", waterbox, BMP); + + bulb.intensity = 55; + bulb.position.set(5.0F, 10.0F, 10.0f); + printf("Finished initialization\n"); +} + +void Dolphin::onUpdate() +{ + if (engine->pad.isCrossClicked) + printf("PlayerPos (%f,%f), FPS:%f\n", player.mesh.position.x, player.mesh.position.z, this->engine->fps); + + float xDist = player.mesh.position.x - water.position.x; + float zDist = player.mesh.position.z - water.position.z; + if (xDist < 0) + xDist *= -1; + if (zDist < 0) + zDist *= -1; + if (xDist > WATER_SIZE * WATER_TILE_SCALE / 4 || + zDist > WATER_SIZE * WATER_TILE_SCALE / 4) + { + water.position.x = (player.mesh.position.x / WATER_SIZE) * WATER_SIZE; + water.position.z = (player.mesh.position.z / WATER_SIZE) * WATER_SIZE; + } + + if (player.mesh.position.y > WATER_LEVEL - 5) + player.mesh.position.y = WATER_LEVEL - 5; + + player.update(engine->pad); + camera.update(engine->pad, player.mesh); + skybox.position.set(player.mesh.position.x, +200.0F, player.mesh.position.z); + waterbox.position.set(player.mesh.position.x, -260.0F, player.mesh.position.z); + engine->renderer->draw(island); + engine->renderer->draw(water); + engine->renderer->draw(skybox); + engine->renderer->draw(waterbox); + engine->renderer->draw(player.mesh); + //if (player.mesh.position.y < WATER_LEVEL) + //engine->renderer->draw(waterOverlay); +} diff --git a/src/samples/dolphin/dolphin.hpp b/src/samples/dolphin/dolphin.hpp new file mode 100755 index 0000000..6c00556 --- /dev/null +++ b/src/samples/dolphin/dolphin.hpp @@ -0,0 +1,46 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Michał Mostowik +*/ + +#ifndef _DRIVER_ +#define _DRIVER_ + + +#include +#include +#include +#include +#include +#include +#include +#include "./camera.hpp" +#include "./objects/player.hpp" + +class Dolphin : public Game +{ + +public: + Dolphin(Engine *t_engine); + ~Dolphin(); + + void onInit(); + void onUpdate(); + + Engine *engine; + +private: + LightBulb bulb; + Player player; + Camera camera; + Mesh island, skybox, water, waterbox; + TextureRepository *texRepo; + Sprite waterOverlay; +}; + +#endif From b547de9cbbb34461786e915c22705605263b237a Mon Sep 17 00:00:00 2001 From: Foxar Date: Fri, 4 Dec 2020 16:50:25 +0100 Subject: [PATCH 010/108] Stretched water overlay to fullscreen, made it appear when camera's sugmerged, disabled the invalid rotation during submerging ( issue number #10 ), enlargened and fixed position of the island mesh. --- src/samples/dolphin/dolphin.cpp | 10 +++++----- src/samples/dolphin/objects/player.cpp | 3 ++- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/samples/dolphin/dolphin.cpp b/src/samples/dolphin/dolphin.cpp index 8b622b3..323d9f6 100755 --- a/src/samples/dolphin/dolphin.cpp +++ b/src/samples/dolphin/dolphin.cpp @@ -29,15 +29,15 @@ void Dolphin::onInit() engine->renderer->disableVSync(); printf("loading island..\n"); - island.loadDff("sunnyisl/", "sunnyisl", 1.0F, false); + island.loadDff("sunnyisl/", "sunnyisl", 5.0F, false); printf("Loaded..\n"); island.rotation.x = -1.6F; - island.position.set(0.0F, 10.0F, 20.0F); + island.position.set(0.0F, 60.0F, 20.0F); island.shouldBeBackfaceCulled = true; island.shouldBeFrustumCulled = false; printf("Loading water overlay...\n"); - waterOverlay.size.set(100.0F, 100.0F); + waterOverlay.size.set(640.0F, 480.0F); Texture *watOverlayTex = texRepo->add("2d/", "underwater_overlay", PNG); watOverlayTex->addLink(waterOverlay.getId()); printf("Loaded.\n"); @@ -98,6 +98,6 @@ void Dolphin::onUpdate() engine->renderer->draw(skybox); engine->renderer->draw(waterbox); engine->renderer->draw(player.mesh); - //if (player.mesh.position.y < WATER_LEVEL) - //engine->renderer->draw(waterOverlay); + if (camera.position.y < WATER_LEVEL) + engine->renderer->draw(waterOverlay); } diff --git a/src/samples/dolphin/objects/player.cpp b/src/samples/dolphin/objects/player.cpp index 207dfdc..09e49b6 100755 --- a/src/samples/dolphin/objects/player.cpp +++ b/src/samples/dolphin/objects/player.cpp @@ -65,13 +65,14 @@ void Player::update(Pad &t_pad) isJumping = true; else isJumping = false; - + /* if (lift < 0) mesh.rotation.y = 1; else if (lift > 0) mesh.rotation.y = -1; else mesh.rotation.y = 0; + */ if (velocity > 0 && mesh.getCurrentAnimationFrame() == 0) { 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 011/108] 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 c0ab97585c5b5ed4c1607ea93faa9052fa1f39d3 Mon Sep 17 00:00:00 2001 From: Foxar Date: Sat, 5 Dec 2020 22:04:38 +0100 Subject: [PATCH 012/108] Added: collectibles class. Collectibles are strewn around the level and can be 'picked up' (deactivated, won't be rendered), when the player gets close and jumps. --- src/samples/dolphin/Makefile | 1 + src/samples/dolphin/dolphin.cpp | 41 +++++++++++++++++++++ src/samples/dolphin/dolphin.hpp | 3 +- src/samples/dolphin/objects/collectible.cpp | 39 ++++++++++++++++++++ src/samples/dolphin/objects/collectible.hpp | 33 +++++++++++++++++ src/samples/dolphin/objects/player.cpp | 5 +++ src/samples/dolphin/objects/player.hpp | 1 + 7 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 src/samples/dolphin/objects/collectible.cpp create mode 100644 src/samples/dolphin/objects/collectible.hpp diff --git a/src/samples/dolphin/Makefile b/src/samples/dolphin/Makefile index c4e5f83..5c92574 100755 --- a/src/samples/dolphin/Makefile +++ b/src/samples/dolphin/Makefile @@ -5,6 +5,7 @@ TYRA_DIR = ./../../engine EE_OBJS = \ camera.o \ objects/player.o \ + objects/collectible.o \ dolphin.o \ main.o diff --git a/src/samples/dolphin/dolphin.cpp b/src/samples/dolphin/dolphin.cpp index 323d9f6..19f3df3 100755 --- a/src/samples/dolphin/dolphin.cpp +++ b/src/samples/dolphin/dolphin.cpp @@ -14,8 +14,11 @@ const u8 WATER_TILE_SCALE = 5.0F; const u8 WATER_SIZE = 100.0F; +const u8 OYSTERS_COUNT = 15.0F; + Dolphin::Dolphin(Engine *t_engine) : engine(t_engine), camera(&engine->screen) { + oysters = new Collectible[OYSTERS_COUNT]; } Dolphin::~Dolphin() {} @@ -58,6 +61,26 @@ void Dolphin::onInit() waterbox.rotation.x = Math::PI; waterbox.position.y = -100.F; + printf("Loading oyster dff\n"); + //oysters[0].loadDff("oyster/", "oyster", 1.F, false); + oysters[0].mesh.loadObj("oyster/", "oyster", 10.F, false); + printf("Loaded."); + oysters[0].mesh.shouldBeBackfaceCulled = false; + oysters[0].mesh.shouldBeFrustumCulled = false; + oysters[0].mesh.position.set(15, 0, 15); + texRepo->addByMesh("oyster/", oysters[0].mesh, BMP); + printf("Adding oyster texture.\n"); + for (int i = 1; i < OYSTERS_COUNT; i++) + { + printf("Processing oyster %d\n", i); + oysters[i].mesh.loadFrom(oysters[0].mesh); + oysters[i].mesh.shouldBeBackfaceCulled = false; + oysters[i].mesh.shouldBeFrustumCulled = false; + texRepo->getByMesh(oysters[0].mesh.getId(), oysters[0].mesh.getMaterial(0).getId()) + ->addLink(oysters[i].mesh.getId(), oysters[i].mesh.getMaterial(0).getId()); + oysters[i].mesh.position.set(i * -100, 5.0F, i * -100); + } + texRepo->addByMesh("dolphin/", player.mesh, BMP); texRepo->addByMesh("sunnyisl/", island, BMP); texRepo->addByMesh("skybox/", skybox, BMP); @@ -86,6 +109,17 @@ void Dolphin::onUpdate() water.position.z = (player.mesh.position.z / WATER_SIZE) * WATER_SIZE; } + for (int i = 0; i < OYSTERS_COUNT; i++) + { + Vector3 vecDist = oysters[i].mesh.position - player.mesh.position; + float dist = Math::sqrt(vecDist.x + vecDist.y + vecDist.z); + if (dist < 2 && player.getIsJumping() && oysters[i].isActive()) + { + printf("Pickup %d Dist %d\n", i, dist); + oysters[i].setActive(false); + } + } + if (player.mesh.position.y > WATER_LEVEL - 5) player.mesh.position.y = WATER_LEVEL - 5; @@ -97,6 +131,13 @@ void Dolphin::onUpdate() engine->renderer->draw(water); engine->renderer->draw(skybox); engine->renderer->draw(waterbox); + for (u8 i = 0; i < OYSTERS_COUNT; i++) + { + if (oysters[i].isActive()) + { + engine->renderer->draw(oysters[i].mesh); + } + } engine->renderer->draw(player.mesh); if (camera.position.y < WATER_LEVEL) engine->renderer->draw(waterOverlay); diff --git a/src/samples/dolphin/dolphin.hpp b/src/samples/dolphin/dolphin.hpp index 6c00556..9630f5e 100755 --- a/src/samples/dolphin/dolphin.hpp +++ b/src/samples/dolphin/dolphin.hpp @@ -11,7 +11,6 @@ #ifndef _DRIVER_ #define _DRIVER_ - #include #include #include @@ -21,6 +20,7 @@ #include #include "./camera.hpp" #include "./objects/player.hpp" +#include "./objects/collectible.hpp" class Dolphin : public Game { @@ -39,6 +39,7 @@ private: Player player; Camera camera; Mesh island, skybox, water, waterbox; + Collectible *oysters; TextureRepository *texRepo; Sprite waterOverlay; }; diff --git a/src/samples/dolphin/objects/collectible.cpp b/src/samples/dolphin/objects/collectible.cpp new file mode 100644 index 0000000..27ffb5c --- /dev/null +++ b/src/samples/dolphin/objects/collectible.cpp @@ -0,0 +1,39 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Michał Mostowik +*/ + +#include "collectible.hpp" + +#include +#include +#include +#include +#include + +Collectible::Collectible() +{ + mesh.loadObj("oyster/", "oyster", 10.F, false); + mesh.shouldBeFrustumCulled = false; + mesh.position.set(0.0F, 0.0F, 0.0f); + bActive = true; +} + +Collectible::~Collectible() +{ +} + +void Collectible::setActive(u8 b) +{ + bActive = b; +} + +u8 Collectible::isActive() +{ + return bActive; +} \ No newline at end of file diff --git a/src/samples/dolphin/objects/collectible.hpp b/src/samples/dolphin/objects/collectible.hpp new file mode 100644 index 0000000..a7adbd1 --- /dev/null +++ b/src/samples/dolphin/objects/collectible.hpp @@ -0,0 +1,33 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Michał Mostowik +*/ + +#ifndef _COLLECTIBLE_ +#define _COLLECTIBLE_ + +#include "../camera.hpp" +#include + +class Collectible +{ + +public: + float rotation; + Mesh mesh; + Collectible(); + ~Collectible(); + void update(); + void setActive(u8 b); + u8 isActive(); + +private: + u8 bActive; +}; + +#endif diff --git a/src/samples/dolphin/objects/player.cpp b/src/samples/dolphin/objects/player.cpp index 09e49b6..87c4953 100755 --- a/src/samples/dolphin/objects/player.cpp +++ b/src/samples/dolphin/objects/player.cpp @@ -86,4 +86,9 @@ void Player::update(Pad &t_pad) mesh.position.x += Math::sin(mesh.rotation.z) * velocity * (isJumping + 1); mesh.position.y += lift; +} + +u8 Player::getIsJumping() +{ + return isJumping; } \ No newline at end of file diff --git a/src/samples/dolphin/objects/player.hpp b/src/samples/dolphin/objects/player.hpp index bd990be..fecbd51 100755 --- a/src/samples/dolphin/objects/player.hpp +++ b/src/samples/dolphin/objects/player.hpp @@ -26,6 +26,7 @@ public: Player(); ~Player(); void update(Pad &t_pad); + u8 getIsJumping(); private: u8 isJumping; From b8552330bde733cf4744d7dd9fee9e5729601ab9 Mon Sep 17 00:00:00 2001 From: h4570 Date: Sun, 6 Dec 2020 00:00:35 +0100 Subject: [PATCH 013/108] 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 014/108] 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 015/108] 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 94e4f8ea3a36c5a50d0a283b5780a0e2f395ece5 Mon Sep 17 00:00:00 2001 From: h4570 Date: Sun, 6 Dec 2020 16:40:13 +0100 Subject: [PATCH 016/108] fixed camera movement speed --- src/samples/floors/camera.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/samples/floors/camera.cpp b/src/samples/floors/camera.cpp index b31d307..c55cad0 100644 --- a/src/samples/floors/camera.cpp +++ b/src/samples/floors/camera.cpp @@ -51,7 +51,7 @@ void Camera::rotate(Pad &t_pad) if (t_pad.rJoyH <= 50) horizontalLevel -= 0.08; else if (t_pad.rJoyH >= 200) - horizontalLevel += 0.04; + horizontalLevel += 0.08; if (t_pad.rJoyV <= 50 && verticalLevel > 25.0F) verticalLevel -= 0.9F; else if (t_pad.rJoyV >= 200 && verticalLevel < 80.0F) From bcad6f794104b0c72b8c11721065820533da8b11 Mon Sep 17 00:00:00 2001 From: h4570 Date: Sun, 6 Dec 2020 16:41:40 +0100 Subject: [PATCH 017/108] added ui, enemy, sounds, fixed anims --- src/samples/floors/Makefile | 2 + src/samples/floors/floors.cpp | 34 +- src/samples/floors/floors.hpp | 5 + src/samples/floors/managers/floor_manager.cpp | 16 +- src/samples/floors/managers/floor_manager.hpp | 2 +- src/samples/floors/objects/enemy.cpp | 72 +++++ src/samples/floors/objects/enemy.hpp | 38 +++ src/samples/floors/objects/floor.hpp | 2 +- src/samples/floors/objects/player.cpp | 296 +++++++++++------- src/samples/floors/objects/player.hpp | 34 +- src/samples/floors/ui.cpp | 71 +++++ src/samples/floors/ui.hpp | 35 +++ 12 files changed, 470 insertions(+), 137 deletions(-) create mode 100644 src/samples/floors/objects/enemy.cpp create mode 100644 src/samples/floors/objects/enemy.hpp create mode 100644 src/samples/floors/ui.cpp create mode 100644 src/samples/floors/ui.hpp diff --git a/src/samples/floors/Makefile b/src/samples/floors/Makefile index 9379326..d49ca98 100644 --- a/src/samples/floors/Makefile +++ b/src/samples/floors/Makefile @@ -6,8 +6,10 @@ EE_OBJS = \ managers/light_manager.o \ managers/floor_manager.o \ camera.o \ + objects/enemy.o \ objects/floor.o \ objects/player.o \ + ui.o \ utils.o \ floors.o \ main.o diff --git a/src/samples/floors/floors.cpp b/src/samples/floors/floors.cpp index 4a7868b..97a2dc0 100644 --- a/src/samples/floors/floors.cpp +++ b/src/samples/floors/floors.cpp @@ -14,15 +14,21 @@ // Constructors/Destructors // ---- -const u8 FLOORS_COUNT = 144; // Temp change it also in floor_manager.hpp +const u16 FLOORS_COUNT = 144; // Temp change it also in floor_manager.hpp Floors::Floors(Engine *t_engine) - : engine(t_engine), floorManager(FLOORS_COUNT), camera(&t_engine->screen) + : engine(t_engine), floorManager(FLOORS_COUNT), + player(&t_engine->audio), camera(&t_engine->screen) { audioTicks = 0; + skip1Beat = 0; } -Floors::~Floors() {} +Floors::~Floors() +{ + delete enemy; + delete ui; +} // ---- // Methods @@ -32,10 +38,13 @@ void Floors::onInit() { engine->renderer->setCameraDefinitions(&camera.worldView, &camera.unitCirclePosition, camera.planes); engine->audio.addSongListener(this); - engine->audio.loadSong("NF-CHILL.WAV"); - engine->audio.setSongVolume(100); + engine->audio.loadSong("nob-else.wav"); engine->audio.playSong(); texRepo = engine->renderer->getTextureRepository(); + enemy = new Enemy(texRepo); + ui = new Ui(texRepo); + engine->audio.setSongVolume(80); + texRepo->addByMesh("warrior/", player.mesh, BMP); texRepo->addByMesh("floor/", floorManager.floors[0].mesh, BMP); for (u32 i = 1; i < floorManager.floorAmount; i++) @@ -45,23 +54,26 @@ void Floors::onInit() void Floors::onUpdate() { - if (engine->pad.isCrossClicked) - printf("FPS:%f\n", engine->fps); + ui->update(player); lightManager.update(); camera.update(engine->pad, player.mesh); floorManager.update(player); - player.update(engine->pad, camera, floorManager); + player.update(engine->pad, camera, floorManager, *enemy); engine->renderer->draw(player.mesh); - for (u8 i = 0; i < FLOORS_COUNT; i++) + engine->renderer->draw(enemy->getMeshes(), enemy->getMeshesCount()); + for (u16 i = 0; i < FLOORS_COUNT; i++) engine->renderer->drawByPath3(floorManager.floors[i].mesh, lightManager.bulbs, lightManager.bulbsCount); + ui->render(engine->renderer); // 2D rendering ist LAST step, because layers gonna play there. } void Floors::onAudioTick() { - if ((++audioTicks + 20) % 41 == 0) + if ((++audioTicks + 20) % 28 == 0) { - floorManager.onAudioTick(); + if (skip1Beat) + floorManager.onAudioTick(); lightManager.onAudioTick(); + skip1Beat = !skip1Beat; } if (audioTicks > 10000) audioTicks = 0; diff --git a/src/samples/floors/floors.hpp b/src/samples/floors/floors.hpp index 4652233..c5a68c4 100644 --- a/src/samples/floors/floors.hpp +++ b/src/samples/floors/floors.hpp @@ -18,7 +18,9 @@ #include "managers/light_manager.hpp" #include "modules/texture_repository.hpp" #include "objects/player.hpp" +#include "objects/enemy.hpp" #include "./camera.hpp" +#include "./ui.hpp" class Floors : public Game, AudioListener { @@ -36,11 +38,14 @@ public: private: u32 audioTicks; + u8 skip1Beat; TextureRepository *texRepo; LightManager lightManager; FloorManager floorManager; Player player; Camera camera; + Ui *ui; + Enemy *enemy; }; #endif diff --git a/src/samples/floors/managers/floor_manager.cpp b/src/samples/floors/managers/floor_manager.cpp index 0265b17..4e5ae0d 100644 --- a/src/samples/floors/managers/floor_manager.cpp +++ b/src/samples/floors/managers/floor_manager.cpp @@ -80,7 +80,7 @@ void FloorManager::initFloors() floors[0].mesh.loadObj("floor/", "floor", 3.0F, false); floors[0].mesh.shouldBeFrustumCulled = true; floors[0].mesh.shouldBeLighted = true; - for (u8 i = 1; i < floorAmount; i++) + for (u16 i = 1; i < floorAmount; i++) floors[i].init(floors[0].mesh, spirals[i], i); PRINT_LOG("Floors initialized!"); } @@ -92,7 +92,7 @@ void FloorManager::initFloors() */ void FloorManager::update(Player &t_player) { - for (u8 i = 0; i < floorAmount; i++) + for (u16 i = 0; i < floorAmount; i++) { floors[i].animate(t_player); @@ -106,11 +106,11 @@ void FloorManager::update(Player &t_player) /** Called by audio thread */ void FloorManager::onAudioTick() { - if (audioTick++ > 16) + if (audioTick++ > 13) { if (audioMode == 0) { - for (u8 i = 0; i < floorAmount; i++) + for (u16 i = 0; i < floorAmount; i++) { if ((i + audioOffset) % 2 == 0) floors[i].isByAudioTriggered = true; @@ -125,7 +125,7 @@ void FloorManager::onAudioTick() } else if (audioMode == 1) { - for (u8 i = 0; i < floorAmount; i++) + for (u16 i = 0; i < floorAmount; i++) { if (i < floorAmount / (audioOffset + 1)) floors[i].isByAudioTriggered = true; @@ -140,7 +140,7 @@ void FloorManager::onAudioTick() } else if (audioMode == 2) { - for (u8 i = 0; i < floorAmount; i++) + for (u16 i = 0; i < floorAmount; i++) { if ((i + audioOffset) % 4 == 0) floors[i].isByAudioTriggered = true; @@ -155,9 +155,9 @@ void FloorManager::onAudioTick() } else if (audioMode == 3) { - for (u8 i = 0; i < floorAmount; i++) + for (u16 i = 0; i < floorAmount; i++) { - if (u8(i + 1) > floorAmount / (audioOffset + 1)) + if (u16(i + 1) > floorAmount / (audioOffset + 1)) floors[i].isByAudioTriggered = true; else floors[i].isByAudioTriggered = false; diff --git a/src/samples/floors/managers/floor_manager.hpp b/src/samples/floors/managers/floor_manager.hpp index bd11897..847fdca 100644 --- a/src/samples/floors/managers/floor_manager.hpp +++ b/src/samples/floors/managers/floor_manager.hpp @@ -27,7 +27,7 @@ public: FloorManager(int t_floorAmount); ~FloorManager(); Floor floors[144]; // Temp change it also in floors.cpp - u32 floorAmount; + u16 floorAmount; void update(Player &t_player); void onAudioTick(); diff --git a/src/samples/floors/objects/enemy.cpp b/src/samples/floors/objects/enemy.cpp new file mode 100644 index 0000000..691723e --- /dev/null +++ b/src/samples/floors/objects/enemy.cpp @@ -0,0 +1,72 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Sandro Sobczyński +*/ + +#include "enemy.hpp" + +#include +#include +#include +#include +#include + +// ---- +// Constructors/Destructors +// ---- + +Enemy::Enemy(TextureRepository *t_texRepo) +{ + PRINT_LOG("Creating enemy object"); + + meshes = new Mesh[getMeshesCount()]; + + meshes[0].loadMD2("poss/", "poss_head", 0.3F, false); + meshes[0].position.set(0.00F, 40.00F, 0.00F); + meshes[0].rotation.x = -1.566F; + meshes[0].rotation.z = 1.566F; + + meshes[1].loadMD2("poss/", "poss_body", 0.3F, false); + meshes[1].position.set(0.00F, 40.00F, 0.00F); + meshes[1].rotation.x = -1.566F; + meshes[1].rotation.z = 1.566F; + + meshes[2].loadMD2("poss/", "poss_weapon", 0.3F, false); + meshes[2].position.set(0.00F, 40.00F, 0.00F); + meshes[2].rotation.x = -1.566F; + meshes[2].rotation.z = 1.566F; + + t_texRepo->addByMesh("poss/", meshes[0], PNG); + t_texRepo->addByMesh("poss/", meshes[1], PNG); + t_texRepo->addByMesh("poss/", meshes[2], PNG); + + meshes[0].playAnimation(1, 6); + meshes[1].playAnimation(1, 6); + meshes[2].playAnimation(1, 6); + + PRINT_LOG("Enemy object created!"); +} + +Enemy::~Enemy() +{ + delete[] meshes; +} + +// ---- +// Methods +// ---- + +/** Called by player class. */ +void Enemy::kill(const Player &player) const +{ + printf("killed!\n"); +} + +void Enemy::update(const FloorManager &floorManager) +{ +} diff --git a/src/samples/floors/objects/enemy.hpp b/src/samples/floors/objects/enemy.hpp new file mode 100644 index 0000000..c5c298d --- /dev/null +++ b/src/samples/floors/objects/enemy.hpp @@ -0,0 +1,38 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Sandro Sobczyński +*/ + +#ifndef _ENEMY_ +#define _ENEMY_ + +#include +#include "../managers/floor_manager.hpp" +#include + +/** Player 3D object class */ +class Enemy +{ + +public: + s16 indexOfCurrentFloor; + Enemy(TextureRepository *t_texRepo); + ~Enemy(); + + inline Mesh *getMeshes() const { return meshes; } + inline u8 getMeshesCount() const { return 3; } + inline Vector3 &getPosition() const { return meshes[1].position; } + void kill(const Player &player) const; + void update(const FloorManager &t_floorManager); + +private: + /** 0 - head, 1 - body, 2 - weapon */ + Mesh *meshes; +}; + +#endif diff --git a/src/samples/floors/objects/floor.hpp b/src/samples/floors/objects/floor.hpp index 45d05b7..afd2fae 100644 --- a/src/samples/floors/objects/floor.hpp +++ b/src/samples/floors/objects/floor.hpp @@ -23,7 +23,7 @@ class Floor public: u16 animTimer; - u8 animDirection, initOffset, isByAudioTriggered; + s16 animDirection, initOffset, isByAudioTriggered; Mesh mesh; Floor(); diff --git a/src/samples/floors/objects/player.cpp b/src/samples/floors/objects/player.cpp index 10934ba..6a32148 100644 --- a/src/samples/floors/objects/player.cpp +++ b/src/samples/floors/objects/player.cpp @@ -10,7 +10,7 @@ #include "player.hpp" -#include +#include #include #include #include @@ -20,20 +20,33 @@ // Constructors/Destructors // ---- -Player::Player() +Player::Player(Audio *t_audio) { PRINT_LOG("Creating player object"); - this->gravity = 0.1F; - this->lift = -1.0F; + audio = t_audio; + gravity = 0.1F; + lift = -1.0F; + jumpCounter = 0; + killedEnemies = 0; + isWalking = false; + isFighting = false; + isWalkingAnimationSet = false; + isJumpingAnimationSet = false; + isFightingAnimationSet = false; - this->mesh.loadMD2("warrior/", "warrior", 0.2F, true); - this->mesh.position.set(0.00F, 20.00F, 0.00F); - this->mesh.rotation.x = -1.6F; - this->mesh.shouldBeBackfaceCulled = false; - this->mesh.shouldBeFrustumCulled = false; + mesh.loadMD2("warrior/", "warrior", 0.2F, true); + mesh.position.set(0.00F, 40.00F, 0.00F); + mesh.rotation.x = -1.566F; + mesh.rotation.z = 1.566F; + mesh.shouldBeBackfaceCulled = false; + mesh.shouldBeFrustumCulled = false; + mesh.setAnimSpeed(0.17F); + mesh.playAnimation(0, 0); + + walkAdpcm = audio->loadADPCM("walk.adpcm"); + jumpAdpcm = audio->loadADPCM("jump.adpcm"); + audio->setADPCMVolume(60, 0); - this->mesh.setAnimSpeed(0.2F); - this->mesh.playAnimation(0, 24); PRINT_LOG("Player object created!"); } @@ -46,138 +59,205 @@ Player::~Player() // ---- /** Update player position and control gravity */ -void Player::update(Pad &pad, Camera &camera, FloorManager &floorManager) +void Player::update(const Pad &t_pad, const Camera &t_camera, const FloorManager &floorManager, const Enemy &enemy) { - this->updatePosition(pad, camera, floorManager); - this->updateGravity(pad, floorManager); - if (mesh.position.y < -60.0F) + Vector3 *nextPos = getNextPosition(t_pad, t_camera); + FloorsCheck *floorsCheck = checkFloors(floorManager, *nextPos); + if (floorsCheck->currentFloor != NULL) + indexOfCurrentFloor = floorsCheck->currentFloor->initOffset; + updatePosition(t_pad, t_camera, floorManager, *floorsCheck, *nextPos, enemy); + updateGravity(floorsCheck); + delete floorsCheck; + delete nextPos; +} + +Vector3 *Player::getNextPosition(const Pad &t_pad, const Camera &t_camera) +{ + Vector3 *result = new Vector3(mesh.position); + Vector3 normalizedCamera = Vector3(t_camera.unitCirclePosition); + normalizedCamera.normalize(); + if (t_pad.lJoyV <= 100) { - mesh.position.y = 60.0F; - velocity = 0; + result->x += -normalizedCamera.x; + result->z += -normalizedCamera.z; } + else if (t_pad.lJoyV >= 200) + { + result->x += normalizedCamera.x; + result->z += normalizedCamera.z; + } + if (t_pad.lJoyH <= 100) + { + result->x += -normalizedCamera.z; + result->z += normalizedCamera.x; + } + else if (t_pad.lJoyH >= 200) + { + result->x += normalizedCamera.z; + result->z += -normalizedCamera.x; + } + return result; } /** Move player when pad move buttons are pressed and is not blocked by any floor side */ -void Player::updatePosition(Pad &pad, Camera &camera, FloorManager &floorManager) +void Player::updatePosition(const Pad &t_pad, const Camera &t_camera, const FloorManager &t_floorManager, const FloorsCheck &t_floorsCheck, const Vector3 &t_nextPos, const Enemy &enemy) { + if (t_pad.rJoyH >= 200) + mesh.rotation.z += 0.08; + else if (t_pad.rJoyH <= 100) + mesh.rotation.z -= 0.08; - if (pad.isDpadUpPressed == 1) + isWalking = mesh.position.x != t_nextPos.x || mesh.position.z != t_nextPos.z; + isFighting = isFighting || t_pad.isCircleClicked; + + if (isFighting) { - this->playerNextPosition.x = this->mesh.position.x + 0.01F * -camera.unitCirclePosition.x; - this->playerNextPosition.z = this->mesh.position.z + 0.01F * -camera.unitCirclePosition.z; + if (!isFightingAnimationSet) + { + isFightingAnimationSet = true; + mesh.playAnimation(8, 19, 0); + fightTimer.prime(); + } + if (fightTimer.getTimeDelta() > 25000) + { // end of fighting + float distance = getPosition().distanceTo(enemy.getPosition()); + if (12.0F > distance) + { + enemy.kill(*this); + killedEnemies++; + } + isFightingAnimationSet = false; + isFighting = false; + } } - if (pad.isDpadDownPressed == 1) + else if (isWalking) { - this->playerNextPosition.x = this->mesh.position.x + 0.01F * camera.unitCirclePosition.x; - this->playerNextPosition.z = this->mesh.position.z + 0.01F * camera.unitCirclePosition.z; + if (!isWalkingAnimationSet) + { + isWalkingAnimationSet = true; + this->mesh.playAnimation(1, 8); + } + if (walkTimer.getTimeDelta() > 8000) + { + walkTimer.prime(); + audio->playADPCM(walkAdpcm, 0); + } } - if (pad.isDpadLeftPressed == 1) + else { - this->playerNextPosition.x = this->mesh.position.x + 0.01F * -camera.unitCirclePosition.z; - this->playerNextPosition.z = this->mesh.position.z + 0.01F * camera.unitCirclePosition.x; + isWalkingAnimationSet = false; + mesh.playAnimation(0, 0); } - if (pad.isDpadRightPressed == 1) + + if (t_floorsCheck.willCollideFloor == NULL && !isFighting) { - this->playerNextPosition.x = this->mesh.position.x + 0.01F * camera.unitCirclePosition.z; - this->playerNextPosition.z = this->mesh.position.z + 0.01F * -camera.unitCirclePosition.x; + mesh.position.x = t_nextPos.x; + mesh.position.z = t_nextPos.z; } - this->playerNextPosition.y = this->mesh.position.y; - this->isCollideFloor = 0; - Vector3 min = Vector3(); - Vector3 max = Vector3(); - for (u32 i = 0; i < floorManager.floorAmount; i++) + + if (t_pad.isCrossClicked == 1) { - getMinMax(&floorManager.floors[i].mesh, &min, &max); - this->isCollideFloor = this->playerNextPosition.collidesSquare(min, max); - if (this->isCollideFloor == 1) - break; - } - if (this->isCollideFloor == 0) - { - this->mesh.position.x = this->playerNextPosition.x; - this->mesh.position.z = this->playerNextPosition.z; + velocity = lift; + audio->playADPCM(jumpAdpcm); + jumpCounter++; } } -/** Do not call this. +/** + * Do not call this. * This is called by Floor::animate() on player's floor move in Y direction */ -void Player::onBeforePlayerFloorMove(Floor *floor, float &newY) +void Player::onBeforePlayerFloorMove(Floor *t_floor, float &t_newY) { - if (floor->animDirection == 0 && newY > 0.0F) - this->mesh.position.y += newY * 1.05F; - else if ((floor->animDirection == 1 && -newY < 0.0F)) - this->mesh.position.y -= -newY * 1.05F; -} - -/** Calculates minimum and maximum X, Y, Z of mesh vertices + current position */ -void Player::getMinMax(Mesh *t_mesh, Vector3 *t_min, Vector3 *t_max) -{ - Vector3 calc = Vector3(); - u8 isInitialized = 0; - Vector3 *boundingBox = t_mesh->getCurrentBoundingBox(); - for (u32 i = 0; i < 8; i++) - { - calc.set( - boundingBox[i].x + t_mesh->position.x, - boundingBox[i].y + t_mesh->position.y, - boundingBox[i].z + t_mesh->position.z); - if (isInitialized == 0) - { - isInitialized = 1; - t_min->set(calc); - t_max->set(calc); - } - - if (t_min->x > calc.x) - t_min->x = calc.x; - if (calc.x > t_max->x) - t_max->x = calc.x; - - if (t_min->y > calc.y) - t_min->y = calc.y; - if (calc.y > t_max->y) - t_max->y = calc.y; - - if (t_min->z > calc.z) - t_min->z = calc.z; - if (calc.z > t_max->z) - t_max->z = calc.z; - } + if (t_floor->animDirection == 0 && t_newY > 0.0F) + this->mesh.position.y += t_newY * 1.05F; + else if ((t_floor->animDirection == 1 && -t_newY < 0.0F)) + this->mesh.position.y -= -t_newY * 1.05F; } /** Update player position by gravity and update index of current floor */ -void Player::updateGravity(Pad &pad, FloorManager &floorManager) +void Player::updateGravity(FloorsCheck *t_floorsCheck) { - Vector3 min = Vector3(); - Vector3 max = Vector3(); - - if (pad.isCrossClicked == 1) - this->velocity = this->lift; - - this->isOnFloor = 0; - for (u32 i = 0; i < floorManager.floorAmount; i++) - { - getMinMax(&floorManager.floors[i].mesh, &min, &max); - this->isOnFloor = this->mesh.position.isOnSquare(min, max); - if (this->isOnFloor == 1) - { - this->indexOfCurrentFloor = floorManager.floors[i].initOffset; - break; - } - } - this->velocity += this->gravity; this->mesh.position.y -= this->velocity; + u8 isOnFloor = t_floorsCheck->currentFloor != NULL && mesh.position.y < t_floorsCheck->currFloorMax.y; - if (this->mesh.position.y >= 60.0F) + if (this->mesh.position.y >= 60.0F || mesh.position.y < -60.0F) { this->mesh.position.y = 60.0F; this->velocity = 0; } - else if ((this->mesh.position.y) < max.y && this->isOnFloor) + else if (isOnFloor) { - this->mesh.position.y = max.y; + this->mesh.position.y = t_floorsCheck->currFloorMax.y; this->velocity = 0; } } + +// --- +// Private +// --- + +FloorsCheck *Player::checkFloors(const FloorManager &t_floorManager, const Vector3 &t_nextPos) +{ + FloorsCheck *result = new FloorsCheck; + result->currentFloor = NULL; + result->willCollideFloor = NULL; + Vector3 min = Vector3(); + Vector3 max = Vector3(); + for (u32 i = 0; i < t_floorManager.floorAmount; i++) + { + getMinMax(t_floorManager.floors[i].mesh, min, max); + if (result->currentFloor == NULL && this->mesh.position.isOnSquare(min, max)) + { + result->currentFloor = &t_floorManager.floors[i]; + result->currFloorMin.set(min); + result->currFloorMax.set(max); + } + if (result->willCollideFloor == NULL && t_nextPos.collidesSquare(min, max)) + { + result->willCollideFloor = &t_floorManager.floors[i]; + result->willCollideFloorMin.set(min); + result->willCollideFloorMax.set(max); + } + if (result->currentFloor != NULL && result->willCollideFloor != NULL) + break; + } + return result; +} + +/** Calculates minimum and maximum X, Y, Z of mesh vertices + current position */ +void Player::getMinMax(const Mesh &t_mesh, Vector3 &t_min, Vector3 &t_max) +{ + Vector3 calc = Vector3(); + u8 isInitialized = 0; + Vector3 *boundingBox = t_mesh.getCurrentBoundingBox(); + for (u32 i = 0; i < 8; i++) + { + calc.set( + boundingBox[i].x + t_mesh.position.x, + boundingBox[i].y + t_mesh.position.y, + boundingBox[i].z + t_mesh.position.z); + if (isInitialized == 0) + { + isInitialized = 1; + t_min.set(calc); + t_max.set(calc); + } + + if (t_min.x > calc.x) + t_min.x = calc.x; + if (calc.x > t_max.x) + t_max.x = calc.x; + + if (t_min.y > calc.y) + t_min.y = calc.y; + if (calc.y > t_max.y) + t_max.y = calc.y; + + if (t_min.z > calc.z) + t_min.z = calc.z; + if (calc.z > t_max.z) + t_max.z = calc.z; + } +} diff --git a/src/samples/floors/objects/player.hpp b/src/samples/floors/objects/player.hpp index c2cd3f8..c5386d4 100644 --- a/src/samples/floors/objects/player.hpp +++ b/src/samples/floors/objects/player.hpp @@ -13,8 +13,17 @@ #include "../managers/floor_manager.hpp" #include "../camera.hpp" +#include "./enemy.hpp" #include +#include #include +#include + +struct FloorsCheck +{ + const Floor *currentFloor, *willCollideFloor; + Vector3 currFloorMin, willCollideFloorMin, currFloorMax, willCollideFloorMax; +}; /** Player 3D object class */ class Player @@ -22,19 +31,28 @@ class Player public: float gravity, velocity, lift; - u8 isOnFloor, isCollideFloor, indexOfCurrentFloor; Mesh mesh; - Player(); + s16 indexOfCurrentFloor; + Player(Audio *t_audio); ~Player(); - void update(Pad &pad, Camera &camera, FloorManager &floorManager); - void onBeforePlayerFloorMove(Floor *floor, float &newY); + void update(const Pad &t_pad, const Camera &t_camera, const FloorManager &t_floorManager, const Enemy &enemy); + void onBeforePlayerFloorMove(Floor *t_floor, float &t_newY); + const inline u32 &getJumpCount() const { return jumpCounter; } + inline const Vector3 &getPosition() const { return mesh.position; } private: - Vector3 playerNextPosition; - void getMinMax(Mesh *t_mesh, Vector3 *t_min, Vector3 *t_max); - void updatePosition(Pad &pad, Camera &camera, FloorManager &floorManager); - void updateGravity(Pad &pad, FloorManager &floorManager); + u32 jumpCounter, killedEnemies; + Vector3 *getNextPosition(const Pad &t_pad, const Camera &t_camera); + FloorsCheck *checkFloors(const FloorManager &t_floorManager, const Vector3 &t_nextPos); + u8 isWalkingAnimationSet, isJumpingAnimationSet, isFightingAnimationSet; + u8 isFighting, isWalking; + Audio *audio; + Timer walkTimer, fightTimer; + audsrv_adpcm_t *walkAdpcm, *jumpAdpcm; + void getMinMax(const Mesh &t_mesh, Vector3 &t_min, Vector3 &t_max); + void updatePosition(const Pad &t_pad, const Camera &t_camera, const FloorManager &t_floorManager, const FloorsCheck &t_floorsCheck, const Vector3 &nextPos, const Enemy &enemy); + void updateGravity(FloorsCheck *t_floorsCheck); }; #endif diff --git a/src/samples/floors/ui.cpp b/src/samples/floors/ui.cpp new file mode 100644 index 0000000..97f4a53 --- /dev/null +++ b/src/samples/floors/ui.cpp @@ -0,0 +1,71 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Sandro Sobczyński +*/ + +#include "ui.hpp" + +#include + +// ---- +// Constructors/Destructors +// ---- + +Ui::Ui(TextureRepository *t_texRepo) +{ + PRINT_LOG("Initializing ui"); + + isTask1Done = false; + isTask2Done = false; + + task1.size.set(128.0F, 32.0F); + task1.position.set(500.0F, 10.0F); + + task2.size.set(128.0F, 32.0F); + task2.position.set(500.0F, 50.0F); + + task1Checkbox.size.set(32.0F, 32.0F); + task1Checkbox.position.set(460.0F, 10.0F); + + task2Checkbox.size.set(32.0F, 32.0F); + task2Checkbox.position.set(460.0F, 50.0F); + + t_texRepo->add("2d/", "check1_text", PNG)->addLink(task1.getId()); + t_texRepo->add("2d/", "check2_text", PNG)->addLink(task2.getId()); + grayCheckboxTex = t_texRepo->add("2d/", "gray_checkmark", PNG); + blueCheckboxTex = t_texRepo->add("2d/", "blue_checkmark", PNG); + + grayCheckboxTex->addLink(task1Checkbox.getId()); + grayCheckboxTex->addLink(task2Checkbox.getId()); + + PRINT_LOG("Ui initialized!"); +} + +Ui::~Ui() {} + +// ---- +// Methods +// ---- + +void Ui::update(const Player &player) +{ + if (!isTask1Done && player.getJumpCount() >= 10) + { + isTask1Done = true; + grayCheckboxTex->removeLinkBySprite(task1Checkbox.getId()); + blueCheckboxTex->addLink(task1Checkbox.getId()); + } +} + +void Ui::render(Renderer *t_renderer) +{ + t_renderer->draw(task1); + t_renderer->draw(task2); + t_renderer->draw(task1Checkbox); + t_renderer->draw(task2Checkbox); +} diff --git a/src/samples/floors/ui.hpp b/src/samples/floors/ui.hpp new file mode 100644 index 0000000..7b59455 --- /dev/null +++ b/src/samples/floors/ui.hpp @@ -0,0 +1,35 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Sandro Sobczyński +*/ + +#ifndef _UI_ +#define _UI_ + +#include +#include +#include "objects/player.hpp" +#include + +class Ui +{ + +public: + Ui(TextureRepository *t_texRepo); + ~Ui(); + + void update(const Player &player); + void render(Renderer *t_renderer); + +private: + u8 isTask1Done, isTask2Done; + Sprite task1, task2, task1Checkbox, task2Checkbox; + Texture *grayCheckboxTex, *blueCheckboxTex; +}; + +#endif From 55cee63ace4c56ac04282231f4eb744f810ce37e Mon Sep 17 00:00:00 2001 From: h4570 Date: Sun, 6 Dec 2020 20:20:08 +0100 Subject: [PATCH 018/108] 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 019/108] 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 From 97c9a5cd901bd05f39824e94fe373846c8c0dc4b Mon Sep 17 00:00:00 2001 From: h4570 Date: Sun, 6 Dec 2020 20:36:04 +0100 Subject: [PATCH 020/108] fixed var initializing --- src/samples/floors/managers/floor_manager.cpp | 2 +- src/samples/floors/objects/floor.cpp | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/samples/floors/managers/floor_manager.cpp b/src/samples/floors/managers/floor_manager.cpp index 0265b17..3c38887 100644 --- a/src/samples/floors/managers/floor_manager.cpp +++ b/src/samples/floors/managers/floor_manager.cpp @@ -96,7 +96,7 @@ void FloorManager::update(Player &t_player) { floors[i].animate(t_player); - if (floors[i].isByAudioTriggered == true) + if (floors[i].isByAudioTriggered) floors[i].mesh.color.a = 0x20; else floors[i].mesh.color.a = 0x80; diff --git a/src/samples/floors/objects/floor.cpp b/src/samples/floors/objects/floor.cpp index 7da6c79..ac859b8 100644 --- a/src/samples/floors/objects/floor.cpp +++ b/src/samples/floors/objects/floor.cpp @@ -23,7 +23,13 @@ const float FLOOR_ANIMATION_Y_MAX = 10.0F; // Constructors/Destructors // ---- -Floor::Floor() {} +Floor::Floor() +{ + animTimer = 0; + animDirection = 0; + initOffset = 0; + isByAudioTriggered = false; +} Floor::~Floor() {} @@ -44,7 +50,6 @@ void Floor::init(const Mesh &mother, const Point &spiral, const u8 &initOffset) this->animTimer = rand() % FLOOR_ANIMATION_LENGTH; this->mesh.shouldBeLighted = true; this->mesh.shouldBeFrustumCulled = true; - this->animDirection = 0; this->mesh.position.x = spiral.x * 80.0F; this->mesh.position.z = spiral.y * 80.0F; } From fbbb41580cac616e2523462eb488954b182697dd Mon Sep 17 00:00:00 2001 From: h4570 Date: Sun, 6 Dec 2020 21:20:24 +0100 Subject: [PATCH 021/108] decreased vol --- src/samples/floors/floors.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/samples/floors/floors.cpp b/src/samples/floors/floors.cpp index 97a2dc0..68bebac 100644 --- a/src/samples/floors/floors.cpp +++ b/src/samples/floors/floors.cpp @@ -43,7 +43,7 @@ void Floors::onInit() texRepo = engine->renderer->getTextureRepository(); enemy = new Enemy(texRepo); ui = new Ui(texRepo); - engine->audio.setSongVolume(80); + engine->audio.setSongVolume(20); texRepo->addByMesh("warrior/", player.mesh, BMP); texRepo->addByMesh("floor/", floorManager.floors[0].mesh, BMP); From 0423e484a568af58a100e3d123b7c157a3175628 Mon Sep 17 00:00:00 2001 From: Foxar Date: Mon, 7 Dec 2020 14:28:56 +0100 Subject: [PATCH 022/108] Changed: Dolphin sample FOV is now 75.F, zoomed in the camera, increased the distance for collectibles pickup. --- src/samples/dolphin/camera.cpp | 2 +- src/samples/dolphin/dolphin.cpp | 2 +- src/samples/dolphin/main.cpp | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/samples/dolphin/camera.cpp b/src/samples/dolphin/camera.cpp index 0fb871f..5a9e61f 100755 --- a/src/samples/dolphin/camera.cpp +++ b/src/samples/dolphin/camera.cpp @@ -16,7 +16,7 @@ const float CAMERA_Y = 15.0F; Camera::Camera(ScreenSettings *t_screen) : CameraBase(t_screen, &position, &up) { - verticalLevel = 80.0F; + verticalLevel = 30.0F; up.x = 0.0F; up.y = 1.0F; up.z = 0.0F; diff --git a/src/samples/dolphin/dolphin.cpp b/src/samples/dolphin/dolphin.cpp index 19f3df3..bc3f35e 100755 --- a/src/samples/dolphin/dolphin.cpp +++ b/src/samples/dolphin/dolphin.cpp @@ -113,7 +113,7 @@ void Dolphin::onUpdate() { Vector3 vecDist = oysters[i].mesh.position - player.mesh.position; float dist = Math::sqrt(vecDist.x + vecDist.y + vecDist.z); - if (dist < 2 && player.getIsJumping() && oysters[i].isActive()) + if (dist < 2.5F && player.getIsJumping() && oysters[i].isActive()) { printf("Pickup %d Dist %d\n", i, dist); oysters[i].setActive(false); diff --git a/src/samples/dolphin/main.cpp b/src/samples/dolphin/main.cpp index 9c5f37e..d094a17 100755 --- a/src/samples/dolphin/main.cpp +++ b/src/samples/dolphin/main.cpp @@ -13,6 +13,7 @@ int main() { Engine engine = Engine(); + engine.screen.fov = 75.F; Dolphin game = Dolphin(&engine); game.engine->init(&game, 128); SleepThread(); From 8a1bc254c56bc06dcce8c2f132882f4b7322e3a8 Mon Sep 17 00:00:00 2001 From: h4570 Date: Mon, 7 Dec 2020 17:18:43 +0100 Subject: [PATCH 023/108] added sprite modes (stretch/repeat) --- src/engine/include/models/sprite.hpp | 13 +++++++++++++ src/engine/models/sprite.cpp | 1 + src/engine/modules/renderer.cpp | 28 ++++++++++++++++++++-------- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/engine/include/models/sprite.hpp b/src/engine/include/models/sprite.hpp index 14772c5..a606d2c 100644 --- a/src/engine/include/models/sprite.hpp +++ b/src/engine/include/models/sprite.hpp @@ -15,6 +15,12 @@ #include #include +enum SpriteMode +{ + MODE_STRETCH, + MODE_REPEAT +}; + /** * Class which have contain 2D image data. */ @@ -37,10 +43,16 @@ public: /** Auto generated unique Id. */ inline const u32 &getId() const { return id; }; + /** Get sprite drawing mode. */ + inline const SpriteMode &getMode() const { return mode; }; + // ---- // Setters // ---- + /** Set sprite drawing mode. */ + void setMode(const SpriteMode &t_val) { mode = t_val; } + // ---- // Other // ---- @@ -59,6 +71,7 @@ public: private: u32 id; + SpriteMode mode; u8 _isSizeSet, _flipH, _flipV; void setDefaultColor(); void setDefaultLODAndClut(); diff --git a/src/engine/models/sprite.cpp b/src/engine/models/sprite.cpp index 160d8c5..5151a0f 100644 --- a/src/engine/models/sprite.cpp +++ b/src/engine/models/sprite.cpp @@ -24,6 +24,7 @@ Sprite::Sprite() _flipH = false; _flipV = false; scale = 1.0F; + mode = MODE_REPEAT; setDefaultColor(); setDefaultLODAndClut(); } diff --git a/src/engine/modules/renderer.cpp b/src/engine/modules/renderer.cpp index 7bd7a60..6e33355 100644 --- a/src/engine/modules/renderer.cpp +++ b/src/engine/modules/renderer.cpp @@ -101,14 +101,26 @@ void Renderer::changeTexture(Texture *t_tex) void Renderer::draw(Sprite &t_sprite) { + Texture *texture = textureRepo.getBySprite(t_sprite.getId()); texrect_t rect; - 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); + float sizeX, sizeY; + if (t_sprite.getMode() == MODE_REPEAT) + { + sizeX = t_sprite.size.x; + sizeY = t_sprite.size.y; + } + else + { + sizeX = (float)texture->getWidth(); + sizeY = (float)texture->getHeight(); + } + + float texS, texT; + float texMax = texT = texS = sizeX > sizeY ? sizeX : sizeY; + if (sizeX > sizeY) + texT = texMax / (sizeX / sizeY); + else if (sizeY > sizeX) + texS = texMax / (sizeY / sizeX); 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; @@ -125,7 +137,7 @@ void Renderer::draw(Sprite &t_sprite) rect.v1.y = (t_sprite.size.y * t_sprite.scale) + t_sprite.position.y; rect.v1.z = (u32)-1; beginFrameIfNeeded(); - changeTexture(textureRepo.getBySprite(t_sprite.getId())); + changeTexture(texture); packet2_t *packet2 = packet2_create(12, P2_TYPE_NORMAL, P2_MODE_NORMAL, 0); packet2_update(packet2, draw_primitive_xyoffset(packet2->next, 0, 2048, 2048)); packet2_utils_gif_add_set(packet2, 1); From 5fb685867822226e1f9bb43e2dd1417d85056333 Mon Sep 17 00:00:00 2001 From: h4570 Date: Mon, 7 Dec 2020 17:19:34 +0100 Subject: [PATCH 024/108] marked getters as inline --- src/engine/include/models/texture.hpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/engine/include/models/texture.hpp b/src/engine/include/models/texture.hpp index d1fd2bb..6872e9f 100644 --- a/src/engine/include/models/texture.hpp +++ b/src/engine/include/models/texture.hpp @@ -47,38 +47,38 @@ public: * again texture in renderer, when last sent texture id * by renderer will be equal to this id. */ - const u32 &getId() const { return id; }; + inline const u32 getId() const { return id; }; - const u8 &getWidth() const { return width; }; + inline const u16 getWidth() const { return width; }; - const u8 &getHeight() const { return height; }; + inline const u16 getHeight() const { return height; }; - const TextureType &getType() const { return _type; }; + inline const TextureType getType() const { return _type; }; - u32 getTextureLinksCount() const { return static_cast(texLinks.size()); }; + inline u32 getTextureLinksCount() const { return static_cast(texLinks.size()); }; - texwrap_t *getWrapSettings() { return &wrapSettings; }; + inline texwrap_t *getWrapSettings() { return &wrapSettings; }; /** * Returns always width * width * 3/4. * 3 for RGB, 4 for RGBA. */ - u32 getDataSize() const { return _type == TEX_TYPE_RGB ? width * height * 3 : width * height * 4; }; + inline u32 getDataSize() const { return _type == TEX_TYPE_RGB ? width * height * 3 : width * height * 4; }; /** * Texture data, used by renderer. * Array of size getDataSize(). */ - unsigned char *getData() const { return data; }; + inline unsigned char *getData() const { return data; }; /** * Get texture name. * For "textures/abc.bmp" result will be: "abc" */ - char *getName() const { return name; }; + inline char *getName() const { return name; }; /** Array of texture links. Size of getTextureLinksCount() */ - const std::vector &getTextureLinks() const { return texLinks; }; + inline const std::vector &getTextureLinks() const { return texLinks; }; /** * Returns index of link. From 4b158781af83386c859ed77e3eada4a2d5238c16 Mon Sep 17 00:00:00 2001 From: h4570 Date: Mon, 7 Dec 2020 17:31:37 +0100 Subject: [PATCH 025/108] fixed var type bug --- src/engine/include/models/texture.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/engine/include/models/texture.hpp b/src/engine/include/models/texture.hpp index 6872e9f..0b9aaaf 100644 --- a/src/engine/include/models/texture.hpp +++ b/src/engine/include/models/texture.hpp @@ -189,7 +189,8 @@ private: TextureType _type; std::vector texLinks; u32 id; - u8 width, height, _isNameSet, _isSizeSet; + u16 width, height; + u8 _isNameSet, _isSizeSet; unsigned char *data; }; From 6292c27c505c5f2a555ebbe5f184e2950c200534 Mon Sep 17 00:00:00 2001 From: Foxar Date: Mon, 7 Dec 2020 19:30:07 +0100 Subject: [PATCH 026/108] Added: Camera automatically shifts to an angle from behind the player. --- src/samples/dolphin/camera.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/samples/dolphin/camera.cpp b/src/samples/dolphin/camera.cpp index 5a9e61f..5c122f7 100755 --- a/src/samples/dolphin/camera.cpp +++ b/src/samples/dolphin/camera.cpp @@ -26,6 +26,10 @@ Camera::~Camera() {} void Camera::update(Pad &t_pad, Mesh &t_mesh) { + float cameraRot = (t_mesh.rotation.z * 180 / Math::PI) - (horizontalLevel * 180 / Math::PI); + cameraRot += 180; + cameraRot /= 15; + horizontalLevel += cameraRot / (180 / Math::PI); rotate(t_pad); followBy(t_mesh); Vector3 lookPos = Vector3(t_mesh.position.x, t_mesh.position.y + 10.0F, t_mesh.position.z); @@ -35,10 +39,10 @@ void Camera::update(Pad &t_pad, Mesh &t_mesh) void Camera::rotate(Pad &t_pad) { - if (t_pad.rJoyH <= 50) + if (t_pad.rJoyH <= 100) horizontalLevel -= 0.08; else if (t_pad.rJoyH >= 200) - horizontalLevel += 0.04; + horizontalLevel += 0.08; /* if (t_pad.rJoyV <= 50 && verticalLevel > 25.0F) verticalLevel -= 0.9F; From 32422b87eba981c8d1c5372d724b6829d013717e Mon Sep 17 00:00:00 2001 From: Foxar Date: Mon, 7 Dec 2020 20:08:11 +0100 Subject: [PATCH 027/108] Added: Collectibles now rotate, float up before disapeparing after being picked up. --- src/samples/dolphin/dolphin.cpp | 4 +++- src/samples/dolphin/objects/collectible.cpp | 22 +++++++++++++++++++++ src/samples/dolphin/objects/collectible.hpp | 4 +++- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/samples/dolphin/dolphin.cpp b/src/samples/dolphin/dolphin.cpp index bc3f35e..841be31 100755 --- a/src/samples/dolphin/dolphin.cpp +++ b/src/samples/dolphin/dolphin.cpp @@ -111,12 +111,14 @@ void Dolphin::onUpdate() for (int i = 0; i < OYSTERS_COUNT; i++) { + oysters[i].update(); Vector3 vecDist = oysters[i].mesh.position - player.mesh.position; float dist = Math::sqrt(vecDist.x + vecDist.y + vecDist.z); if (dist < 2.5F && player.getIsJumping() && oysters[i].isActive()) { printf("Pickup %d Dist %d\n", i, dist); - oysters[i].setActive(false); + //oysters[i].setActive(false); + oysters[i].disappear(); } } diff --git a/src/samples/dolphin/objects/collectible.cpp b/src/samples/dolphin/objects/collectible.cpp index 27ffb5c..e1c76a1 100644 --- a/src/samples/dolphin/objects/collectible.cpp +++ b/src/samples/dolphin/objects/collectible.cpp @@ -22,6 +22,7 @@ Collectible::Collectible() mesh.shouldBeFrustumCulled = false; mesh.position.set(0.0F, 0.0F, 0.0f); bActive = true; + bDisappearing = false; } Collectible::~Collectible() @@ -33,7 +34,28 @@ void Collectible::setActive(u8 b) bActive = b; } +void Collectible::disappear() +{ + bDisappearing = true; + lift = 10; +} + u8 Collectible::isActive() { return bActive; +} + +void Collectible::update() +{ + mesh.rotation.y += 0.08F; + if (bDisappearing) + { + mesh.position.y += lift; + lift = lift > 0 ? lift / 2 : 0; + if (lift < 1) + { + setActive(false); + bDisappearing = false; + } + } } \ No newline at end of file diff --git a/src/samples/dolphin/objects/collectible.hpp b/src/samples/dolphin/objects/collectible.hpp index a7adbd1..775eb70 100644 --- a/src/samples/dolphin/objects/collectible.hpp +++ b/src/samples/dolphin/objects/collectible.hpp @@ -23,11 +23,13 @@ public: Collectible(); ~Collectible(); void update(); + void disappear(); void setActive(u8 b); u8 isActive(); private: - u8 bActive; + u8 bActive, bDisappearing; + u8 lift; }; #endif From 83dce5cb9222e9974eb3e1ad3bcea4bcc0399795 Mon Sep 17 00:00:00 2001 From: Foxar Date: Mon, 7 Dec 2020 20:09:17 +0100 Subject: [PATCH 028/108] Changed: Slowed player rotation speed. --- src/samples/dolphin/objects/player.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/samples/dolphin/objects/player.cpp b/src/samples/dolphin/objects/player.cpp index 87c4953..125a3a5 100755 --- a/src/samples/dolphin/objects/player.cpp +++ b/src/samples/dolphin/objects/player.cpp @@ -45,9 +45,9 @@ void Player::update(Pad &t_pad) velocity = -1; if (t_pad.lJoyH >= 200) - mesh.rotation.z -= 0.1; + mesh.rotation.z -= 0.05; else if (t_pad.lJoyH <= 100) - mesh.rotation.z += 0.1; + mesh.rotation.z += 0.05; if (t_pad.rJoyV >= 200 && lift <= 1) lift += 0.2F; From dd49fc165e450e5887e2ac71faf5bba0c75420b4 Mon Sep 17 00:00:00 2001 From: h4570 Date: Mon, 7 Dec 2020 20:47:41 +0100 Subject: [PATCH 029/108] lot of changes in floors --- src/samples/floors/camera.cpp | 2 +- src/samples/floors/floors.cpp | 19 +++++- src/samples/floors/floors.hpp | 1 + src/samples/floors/managers/floor_manager.cpp | 62 ++++++++++++++++++- src/samples/floors/managers/floor_manager.hpp | 5 ++ src/samples/floors/managers/light_manager.cpp | 4 +- src/samples/floors/objects/enemy.cpp | 43 +++++++++++-- src/samples/floors/objects/enemy.hpp | 10 ++- src/samples/floors/objects/player.cpp | 50 +++------------ src/samples/floors/objects/player.hpp | 8 ++- src/samples/floors/ui.cpp | 6 ++ src/samples/floors/utils.cpp | 36 +++++++++++ src/samples/floors/utils.hpp | 3 + 13 files changed, 190 insertions(+), 59 deletions(-) diff --git a/src/samples/floors/camera.cpp b/src/samples/floors/camera.cpp index c55cad0..b9c583e 100644 --- a/src/samples/floors/camera.cpp +++ b/src/samples/floors/camera.cpp @@ -48,7 +48,7 @@ void Camera::update(Pad &t_pad, Mesh &t_mesh) */ void Camera::rotate(Pad &t_pad) { - if (t_pad.rJoyH <= 50) + if (t_pad.rJoyH <= 100) horizontalLevel -= 0.08; else if (t_pad.rJoyH >= 200) horizontalLevel += 0.08; diff --git a/src/samples/floors/floors.cpp b/src/samples/floors/floors.cpp index 68bebac..823a850 100644 --- a/src/samples/floors/floors.cpp +++ b/src/samples/floors/floors.cpp @@ -36,14 +36,15 @@ Floors::~Floors() void Floors::onInit() { + setBgColorAndAmbientColor(); engine->renderer->setCameraDefinitions(&camera.worldView, &camera.unitCirclePosition, camera.planes); engine->audio.addSongListener(this); - engine->audio.loadSong("nob-else.wav"); + engine->audio.loadSong("mafikizolo-loot.wav"); engine->audio.playSong(); texRepo = engine->renderer->getTextureRepository(); enemy = new Enemy(texRepo); ui = new Ui(texRepo); - engine->audio.setSongVolume(20); + engine->audio.setSongVolume(80); texRepo->addByMesh("warrior/", player.mesh, BMP); texRepo->addByMesh("floor/", floorManager.floors[0].mesh, BMP); @@ -55,6 +56,7 @@ void Floors::onInit() void Floors::onUpdate() { ui->update(player); + enemy->update(floorManager); lightManager.update(); camera.update(engine->pad, player.mesh); floorManager.update(player); @@ -68,7 +70,7 @@ void Floors::onUpdate() void Floors::onAudioTick() { - if ((++audioTicks + 20) % 28 == 0) + if ((++audioTicks + 20) % 21 == 0) { if (skip1Beat) floorManager.onAudioTick(); @@ -79,6 +81,17 @@ void Floors::onAudioTick() audioTicks = 0; } +void Floors::setBgColorAndAmbientColor() +{ + color_t bgColor; + bgColor.r = 0x20; + bgColor.g = 0x20; + bgColor.b = 0x20; + engine->renderer->setWorldColor(bgColor); + Vector3 ambient = Vector3(0.003F, 0.003F, 0.003F); + engine->renderer->setAmbientLight(ambient); +} + void Floors::onAudioFinish() { audioTicks = 0; diff --git a/src/samples/floors/floors.hpp b/src/samples/floors/floors.hpp index c5a68c4..bf2905f 100644 --- a/src/samples/floors/floors.hpp +++ b/src/samples/floors/floors.hpp @@ -37,6 +37,7 @@ public: Engine *engine; private: + void setBgColorAndAmbientColor(); u32 audioTicks; u8 skip1Beat; TextureRepository *texRepo; diff --git a/src/samples/floors/managers/floor_manager.cpp b/src/samples/floors/managers/floor_manager.cpp index 251491c..d97523e 100644 --- a/src/samples/floors/managers/floor_manager.cpp +++ b/src/samples/floors/managers/floor_manager.cpp @@ -27,6 +27,10 @@ FloorManager::FloorManager(int t_floorAmount) int floorSpiralMaxOffset = (int)Math::sqrt(t_floorAmount); calcSpiral(floorSpiralMaxOffset, floorSpiralMaxOffset); initFloors(); + trick = 0.0F; + trickMode = 0; + isTimeForChangeTriggerColor = true; + isTimeForChangeDefaultColor = true; audioOffset = audioMode = audioTick = 0; } @@ -92,17 +96,67 @@ void FloorManager::initFloors() */ void FloorManager::update(Player &t_player) { + if (isTimeForChangeTriggerColor) + { + isTimeForChangeTriggerColor = false; + trigColor.r = rand() % (128 - 64 + 1) + 64; + trigColor.g = rand() % (128 - 64 + 1) + 64; + trigColor.b = rand() % (128 - 64 + 1) + 64; + } + if (isTimeForChangeDefaultColor) + { + isTimeForChangeDefaultColor = false; + defaultColor.r = rand() % (64 + 1); + defaultColor.g = rand() % (64 + 1); + defaultColor.b = rand() % (64 + 1); + } + doTheTrick(); for (u16 i = 0; i < floorAmount; i++) { floors[i].animate(t_player); if (floors[i].isByAudioTriggered) - floors[i].mesh.color.a = 0x20; + { + floors[i].mesh.color.r = trigColor.r; + floors[i].mesh.color.g = trigColor.g; + floors[i].mesh.color.b = trigColor.b; + } else - floors[i].mesh.color.a = 0x80; + { + floors[i].mesh.color.r = defaultColor.r; + floors[i].mesh.color.g = defaultColor.g; + floors[i].mesh.color.b = defaultColor.b; + } } } +void FloorManager::doTheTrick() +{ + if (trickMode == 0) + { + if (trick > 3.0F) + { + isTimeForChangeTriggerColor = true; + trickMode = 1; + } + trick += 0.01F; + } + else if (trickMode == 1) + { + if (trick <= 0.1F) + { + isTimeForChangeTriggerColor = true; + trickMode = 0; + } + trick -= 0.01F; + } + floors[0].mesh.getFrames()[0].getST(1).set(trick, trick); + floors[0].mesh.getFrames()[0].getST(4).set(trick, trick); + floors[0].mesh.getFrames()[0].getST(7).set(trick, trick); + floors[0].mesh.getFrames()[0].getST(10).set(trick, trick); + floors[0].mesh.getFrames()[0].getST(13).set(trick, trick); +} + /** Called by audio thread */ void FloorManager::onAudioTick() { @@ -120,6 +174,7 @@ void FloorManager::onAudioTick() if (++audioOffset > 3) { audioOffset = 0; + isTimeForChangeDefaultColor = true; audioMode = 1; } } @@ -135,6 +190,7 @@ void FloorManager::onAudioTick() if (++audioOffset > 3) { audioOffset = 0; + isTimeForChangeDefaultColor = true; audioMode = 2; } } @@ -150,6 +206,7 @@ void FloorManager::onAudioTick() if (++audioOffset > 3) { audioOffset = 0; + isTimeForChangeDefaultColor = true; audioMode = 3; } } @@ -165,6 +222,7 @@ void FloorManager::onAudioTick() if (++audioOffset > 3) { audioOffset = 0; + isTimeForChangeDefaultColor = true; audioMode = 0; } } diff --git a/src/samples/floors/managers/floor_manager.hpp b/src/samples/floors/managers/floor_manager.hpp index 847fdca..c7eb14a 100644 --- a/src/samples/floors/managers/floor_manager.hpp +++ b/src/samples/floors/managers/floor_manager.hpp @@ -34,8 +34,13 @@ public: private: u8 audioOffset, audioMode; u32 audioTick; + float trick; + u8 isTimeForChangeTriggerColor, isTimeForChangeDefaultColor; + color_t trigColor, defaultColor; + u8 trickMode; Point *spirals; void calcSpiral(int X, int Y); + void doTheTrick(); void initFloors(); }; diff --git a/src/samples/floors/managers/light_manager.cpp b/src/samples/floors/managers/light_manager.cpp index c9c33a0..a87f322 100644 --- a/src/samples/floors/managers/light_manager.cpp +++ b/src/samples/floors/managers/light_manager.cpp @@ -142,7 +142,7 @@ void LightManager::onAudioTick() } else { - bulbs[0].intensity += 6; - bulbs[1].intensity += 6; + bulbs[0].intensity += 9; + bulbs[1].intensity += 9; } } diff --git a/src/samples/floors/objects/enemy.cpp b/src/samples/floors/objects/enemy.cpp index 691723e..4d11344 100644 --- a/src/samples/floors/objects/enemy.cpp +++ b/src/samples/floors/objects/enemy.cpp @@ -9,6 +9,7 @@ */ #include "enemy.hpp" +#include "../utils.hpp" #include #include @@ -27,20 +28,20 @@ Enemy::Enemy(TextureRepository *t_texRepo) meshes = new Mesh[getMeshesCount()]; meshes[0].loadMD2("poss/", "poss_head", 0.3F, false); - meshes[0].position.set(0.00F, 40.00F, 0.00F); meshes[0].rotation.x = -1.566F; meshes[0].rotation.z = 1.566F; meshes[1].loadMD2("poss/", "poss_body", 0.3F, false); - meshes[1].position.set(0.00F, 40.00F, 0.00F); meshes[1].rotation.x = -1.566F; meshes[1].rotation.z = 1.566F; meshes[2].loadMD2("poss/", "poss_weapon", 0.3F, false); - meshes[2].position.set(0.00F, 40.00F, 0.00F); meshes[2].rotation.x = -1.566F; meshes[2].rotation.z = 1.566F; + Vector3 initPos = Vector3(0.00F, 40.00F, 0.00F); + setPosition(initPos); + t_texRepo->addByMesh("poss/", meshes[0], PNG); t_texRepo->addByMesh("poss/", meshes[1], PNG); t_texRepo->addByMesh("poss/", meshes[2], PNG); @@ -49,6 +50,8 @@ Enemy::Enemy(TextureRepository *t_texRepo) meshes[1].playAnimation(1, 6); meshes[2].playAnimation(1, 6); + isKilled = true; + PRINT_LOG("Enemy object created!"); } @@ -61,12 +64,40 @@ Enemy::~Enemy() // Methods // ---- -/** Called by player class. */ -void Enemy::kill(const Player &player) const +void Enemy::setPosition(const Vector3 &t_vec) { - printf("killed!\n"); + position.set(t_vec); + for (u8 i = 0; i < getMeshesCount(); i++) + meshes[i].position.set(t_vec); +} + +void Enemy::setPosition(const float &t_y) +{ + position.y = t_y; + for (u8 i = 0; i < getMeshesCount(); i++) + meshes[i].position.y = t_y; +} + +/** Called by player class. */ +void Enemy::kill(const Player &player) +{ + isKilled = true; } void Enemy::update(const FloorManager &floorManager) { + if (isKilled) + { + isKilled = false; + int randFloor = rand() % (floorManager.floorAmount + 1); + currentFloor = &floorManager.floors[randFloor]; + setPosition(currentFloor->mesh.position); + } + updateGravity(); +} + +void Enemy::updateGravity() +{ + Utils::getMinMax(currentFloor->mesh, currFloorMin, currFloorMax); + setPosition(currFloorMax.y); } diff --git a/src/samples/floors/objects/enemy.hpp b/src/samples/floors/objects/enemy.hpp index c5c298d..601a4e9 100644 --- a/src/samples/floors/objects/enemy.hpp +++ b/src/samples/floors/objects/enemy.hpp @@ -26,13 +26,19 @@ public: inline Mesh *getMeshes() const { return meshes; } inline u8 getMeshesCount() const { return 3; } - inline Vector3 &getPosition() const { return meshes[1].position; } - void kill(const Player &player) const; + inline Vector3 getPosition() const { return position; } + void setPosition(const Vector3 &t_vec); + void setPosition(const float &t_y); + void kill(const Player &player); void update(const FloorManager &t_floorManager); private: /** 0 - head, 1 - body, 2 - weapon */ Mesh *meshes; + const Floor *currentFloor; + Vector3 position, currFloorMin, currFloorMax; + u8 isKilled; + void updateGravity(); }; #endif diff --git a/src/samples/floors/objects/player.cpp b/src/samples/floors/objects/player.cpp index 6a32148..0946a8e 100644 --- a/src/samples/floors/objects/player.cpp +++ b/src/samples/floors/objects/player.cpp @@ -9,6 +9,7 @@ */ #include "player.hpp" +#include "../utils.hpp" #include #include @@ -27,6 +28,7 @@ Player::Player(Audio *t_audio) gravity = 0.1F; lift = -1.0F; jumpCounter = 0; + speed = 1.0F; killedEnemies = 0; isWalking = false; isFighting = false; @@ -45,6 +47,7 @@ Player::Player(Audio *t_audio) walkAdpcm = audio->loadADPCM("walk.adpcm"); jumpAdpcm = audio->loadADPCM("jump.adpcm"); + boomAdpcm = audio->loadADPCM("boom.adpcm"); audio->setADPCMVolume(60, 0); PRINT_LOG("Player object created!"); @@ -59,7 +62,7 @@ Player::~Player() // ---- /** Update player position and control gravity */ -void Player::update(const Pad &t_pad, const Camera &t_camera, const FloorManager &floorManager, const Enemy &enemy) +void Player::update(const Pad &t_pad, const Camera &t_camera, const FloorManager &floorManager, Enemy &enemy) { Vector3 *nextPos = getNextPosition(t_pad, t_camera); FloorsCheck *floorsCheck = checkFloors(floorManager, *nextPos); @@ -76,6 +79,7 @@ Vector3 *Player::getNextPosition(const Pad &t_pad, const Camera &t_camera) Vector3 *result = new Vector3(mesh.position); Vector3 normalizedCamera = Vector3(t_camera.unitCirclePosition); normalizedCamera.normalize(); + normalizedCamera *= speed; if (t_pad.lJoyV <= 100) { result->x += -normalizedCamera.x; @@ -100,7 +104,7 @@ Vector3 *Player::getNextPosition(const Pad &t_pad, const Camera &t_camera) } /** Move player when pad move buttons are pressed and is not blocked by any floor side */ -void Player::updatePosition(const Pad &t_pad, const Camera &t_camera, const FloorManager &t_floorManager, const FloorsCheck &t_floorsCheck, const Vector3 &t_nextPos, const Enemy &enemy) +void Player::updatePosition(const Pad &t_pad, const Camera &t_camera, const FloorManager &t_floorManager, const FloorsCheck &t_floorsCheck, const Vector3 &t_nextPos, Enemy &enemy) { if (t_pad.rJoyH >= 200) mesh.rotation.z += 0.08; @@ -121,9 +125,11 @@ void Player::updatePosition(const Pad &t_pad, const Camera &t_camera, const Floo if (fightTimer.getTimeDelta() > 25000) { // end of fighting float distance = getPosition().distanceTo(enemy.getPosition()); - if (12.0F > distance) + if (16.0F > distance) { enemy.kill(*this); + audio->playADPCM(boomAdpcm); + speed += .5F; killedEnemies++; } isFightingAnimationSet = false; @@ -207,7 +213,7 @@ FloorsCheck *Player::checkFloors(const FloorManager &t_floorManager, const Vecto Vector3 max = Vector3(); for (u32 i = 0; i < t_floorManager.floorAmount; i++) { - getMinMax(t_floorManager.floors[i].mesh, min, max); + Utils::getMinMax(t_floorManager.floors[i].mesh, min, max); if (result->currentFloor == NULL && this->mesh.position.isOnSquare(min, max)) { result->currentFloor = &t_floorManager.floors[i]; @@ -225,39 +231,3 @@ FloorsCheck *Player::checkFloors(const FloorManager &t_floorManager, const Vecto } return result; } - -/** Calculates minimum and maximum X, Y, Z of mesh vertices + current position */ -void Player::getMinMax(const Mesh &t_mesh, Vector3 &t_min, Vector3 &t_max) -{ - Vector3 calc = Vector3(); - u8 isInitialized = 0; - Vector3 *boundingBox = t_mesh.getCurrentBoundingBox(); - for (u32 i = 0; i < 8; i++) - { - calc.set( - boundingBox[i].x + t_mesh.position.x, - boundingBox[i].y + t_mesh.position.y, - boundingBox[i].z + t_mesh.position.z); - if (isInitialized == 0) - { - isInitialized = 1; - t_min.set(calc); - t_max.set(calc); - } - - if (t_min.x > calc.x) - t_min.x = calc.x; - if (calc.x > t_max.x) - t_max.x = calc.x; - - if (t_min.y > calc.y) - t_min.y = calc.y; - if (calc.y > t_max.y) - t_max.y = calc.y; - - if (t_min.z > calc.z) - t_min.z = calc.z; - if (calc.z > t_max.z) - t_max.z = calc.z; - } -} diff --git a/src/samples/floors/objects/player.hpp b/src/samples/floors/objects/player.hpp index c5386d4..75b1ccb 100644 --- a/src/samples/floors/objects/player.hpp +++ b/src/samples/floors/objects/player.hpp @@ -36,9 +36,10 @@ public: Player(Audio *t_audio); ~Player(); - void update(const Pad &t_pad, const Camera &t_camera, const FloorManager &t_floorManager, const Enemy &enemy); + void update(const Pad &t_pad, const Camera &t_camera, const FloorManager &t_floorManager, Enemy &enemy); void onBeforePlayerFloorMove(Floor *t_floor, float &t_newY); const inline u32 &getJumpCount() const { return jumpCounter; } + const inline u32 &getKilledEnemiesCount() const { return killedEnemies; } inline const Vector3 &getPosition() const { return mesh.position; } private: @@ -49,9 +50,10 @@ private: u8 isFighting, isWalking; Audio *audio; Timer walkTimer, fightTimer; - audsrv_adpcm_t *walkAdpcm, *jumpAdpcm; + audsrv_adpcm_t *walkAdpcm, *jumpAdpcm, *boomAdpcm; + float speed; void getMinMax(const Mesh &t_mesh, Vector3 &t_min, Vector3 &t_max); - void updatePosition(const Pad &t_pad, const Camera &t_camera, const FloorManager &t_floorManager, const FloorsCheck &t_floorsCheck, const Vector3 &nextPos, const Enemy &enemy); + void updatePosition(const Pad &t_pad, const Camera &t_camera, const FloorManager &t_floorManager, const FloorsCheck &t_floorsCheck, const Vector3 &nextPos, Enemy &enemy); void updateGravity(FloorsCheck *t_floorsCheck); }; diff --git a/src/samples/floors/ui.cpp b/src/samples/floors/ui.cpp index 97f4a53..3f3dc14 100644 --- a/src/samples/floors/ui.cpp +++ b/src/samples/floors/ui.cpp @@ -60,6 +60,12 @@ void Ui::update(const Player &player) grayCheckboxTex->removeLinkBySprite(task1Checkbox.getId()); blueCheckboxTex->addLink(task1Checkbox.getId()); } + if (!isTask2Done && player.getKilledEnemiesCount() >= 10) + { + isTask2Done = true; + grayCheckboxTex->removeLinkBySprite(task2Checkbox.getId()); + blueCheckboxTex->addLink(task2Checkbox.getId()); + } } void Ui::render(Renderer *t_renderer) diff --git a/src/samples/floors/utils.cpp b/src/samples/floors/utils.cpp index eb7d2ba..5195cf9 100644 --- a/src/samples/floors/utils.cpp +++ b/src/samples/floors/utils.cpp @@ -32,3 +32,39 @@ float Utils::expoEaseInOut(float t, float b, float c, float d) return c / 2 * (-powf(2, -10 * --t) + 2) + b; } + +/** Calculates minimum and maximum X, Y, Z of mesh vertices + current position */ +void Utils::getMinMax(const Mesh &t_mesh, Vector3 &t_min, Vector3 &t_max) +{ + Vector3 calc = Vector3(); + u8 isInitialized = 0; + Vector3 *boundingBox = t_mesh.getCurrentBoundingBox(); + for (u32 i = 0; i < 8; i++) + { + calc.set( + boundingBox[i].x + t_mesh.position.x, + boundingBox[i].y + t_mesh.position.y, + boundingBox[i].z + t_mesh.position.z); + if (isInitialized == 0) + { + isInitialized = 1; + t_min.set(calc); + t_max.set(calc); + } + + if (t_min.x > calc.x) + t_min.x = calc.x; + if (calc.x > t_max.x) + t_max.x = calc.x; + + if (t_min.y > calc.y) + t_min.y = calc.y; + if (calc.y > t_max.y) + t_max.y = calc.y; + + if (t_min.z > calc.z) + t_min.z = calc.z; + if (calc.z > t_max.z) + t_max.z = calc.z; + } +} diff --git a/src/samples/floors/utils.hpp b/src/samples/floors/utils.hpp index da8af9f..2d7293f 100644 --- a/src/samples/floors/utils.hpp +++ b/src/samples/floors/utils.hpp @@ -12,12 +12,15 @@ #define _TYRA_UTILS_ #include +#include +#include class Utils { public: static float expoEaseInOut(float t, float b, float c, float d); + static void getMinMax(const Mesh &t_mesh, Vector3 &t_min, Vector3 &t_max); }; #endif From 3284c5a598f35862cc38575366f9f5bdee3691f5 Mon Sep 17 00:00:00 2001 From: h4570 Date: Mon, 7 Dec 2020 21:02:06 +0100 Subject: [PATCH 030/108] 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 031/108] 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 032/108] 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); From 87ae4b05074455eac1e333425f3af072d08be8fc Mon Sep 17 00:00:00 2001 From: h4570 Date: Mon, 7 Dec 2020 21:20:08 +0100 Subject: [PATCH 033/108] added info banner --- src/samples/floors/ui.cpp | 5 +++++ src/samples/floors/ui.hpp | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/samples/floors/ui.cpp b/src/samples/floors/ui.cpp index 3f3dc14..33ace3a 100644 --- a/src/samples/floors/ui.cpp +++ b/src/samples/floors/ui.cpp @@ -23,6 +23,9 @@ Ui::Ui(TextureRepository *t_texRepo) isTask1Done = false; isTask2Done = false; + info.size.set(128.0F, 32.0F); + info.position.set(500.0F, 430.0F); + task1.size.set(128.0F, 32.0F); task1.position.set(500.0F, 10.0F); @@ -35,6 +38,7 @@ Ui::Ui(TextureRepository *t_texRepo) task2Checkbox.size.set(32.0F, 32.0F); task2Checkbox.position.set(460.0F, 50.0F); + t_texRepo->add("2d/", "info_text", PNG)->addLink(info.getId()); t_texRepo->add("2d/", "check1_text", PNG)->addLink(task1.getId()); t_texRepo->add("2d/", "check2_text", PNG)->addLink(task2.getId()); grayCheckboxTex = t_texRepo->add("2d/", "gray_checkmark", PNG); @@ -70,6 +74,7 @@ void Ui::update(const Player &player) void Ui::render(Renderer *t_renderer) { + t_renderer->draw(info); t_renderer->draw(task1); t_renderer->draw(task2); t_renderer->draw(task1Checkbox); diff --git a/src/samples/floors/ui.hpp b/src/samples/floors/ui.hpp index 7b59455..2eee558 100644 --- a/src/samples/floors/ui.hpp +++ b/src/samples/floors/ui.hpp @@ -28,7 +28,7 @@ public: private: u8 isTask1Done, isTask2Done; - Sprite task1, task2, task1Checkbox, task2Checkbox; + Sprite task1, task2, task1Checkbox, task2Checkbox, info; Texture *grayCheckboxTex, *blueCheckboxTex; }; From 11a92c32eebf99e35f3c8c530d9f7e5acdef30d1 Mon Sep 17 00:00:00 2001 From: h4570 Date: Mon, 7 Dec 2020 21:51:44 +0100 Subject: [PATCH 034/108] audio changes --- src/samples/floors/managers/floor_manager.cpp | 2 +- src/samples/floors/objects/player.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/samples/floors/managers/floor_manager.cpp b/src/samples/floors/managers/floor_manager.cpp index d97523e..ba2216e 100644 --- a/src/samples/floors/managers/floor_manager.cpp +++ b/src/samples/floors/managers/floor_manager.cpp @@ -160,7 +160,7 @@ void FloorManager::doTheTrick() /** Called by audio thread */ void FloorManager::onAudioTick() { - if (audioTick++ > 13) + if (audioTick++ > 19) { if (audioMode == 0) { diff --git a/src/samples/floors/objects/player.cpp b/src/samples/floors/objects/player.cpp index 0946a8e..e5d9ce9 100644 --- a/src/samples/floors/objects/player.cpp +++ b/src/samples/floors/objects/player.cpp @@ -48,7 +48,7 @@ Player::Player(Audio *t_audio) walkAdpcm = audio->loadADPCM("walk.adpcm"); jumpAdpcm = audio->loadADPCM("jump.adpcm"); boomAdpcm = audio->loadADPCM("boom.adpcm"); - audio->setADPCMVolume(60, 0); + audio->setADPCMVolume(70, 0); PRINT_LOG("Player object created!"); } From 96fe7c380ebaa7d919edafbc8e522880daf0f10c Mon Sep 17 00:00:00 2001 From: h4570 Date: Mon, 7 Dec 2020 21:51:48 +0100 Subject: [PATCH 035/108] added reward --- src/samples/floors/ui.cpp | 7 +++++++ src/samples/floors/ui.hpp | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/samples/floors/ui.cpp b/src/samples/floors/ui.cpp index 33ace3a..6639dc3 100644 --- a/src/samples/floors/ui.cpp +++ b/src/samples/floors/ui.cpp @@ -26,6 +26,10 @@ Ui::Ui(TextureRepository *t_texRepo) info.size.set(128.0F, 32.0F); info.position.set(500.0F, 430.0F); + reward.size.set(128.0F, 128.0F); + reward.position.set(500.0F, 280.0F); + reward.flipVertically(true); + task1.size.set(128.0F, 32.0F); task1.position.set(500.0F, 10.0F); @@ -38,6 +42,7 @@ Ui::Ui(TextureRepository *t_texRepo) task2Checkbox.size.set(32.0F, 32.0F); task2Checkbox.position.set(460.0F, 50.0F); + t_texRepo->add("2d/", "reward", BMP)->addLink(reward.getId()); t_texRepo->add("2d/", "info_text", PNG)->addLink(info.getId()); t_texRepo->add("2d/", "check1_text", PNG)->addLink(task1.getId()); t_texRepo->add("2d/", "check2_text", PNG)->addLink(task2.getId()); @@ -74,6 +79,8 @@ void Ui::update(const Player &player) void Ui::render(Renderer *t_renderer) { + if (isTask1Done && isTask2Done) + t_renderer->draw(reward); t_renderer->draw(info); t_renderer->draw(task1); t_renderer->draw(task2); diff --git a/src/samples/floors/ui.hpp b/src/samples/floors/ui.hpp index 2eee558..f96edb9 100644 --- a/src/samples/floors/ui.hpp +++ b/src/samples/floors/ui.hpp @@ -28,7 +28,7 @@ public: private: u8 isTask1Done, isTask2Done; - Sprite task1, task2, task1Checkbox, task2Checkbox, info; + Sprite task1, task2, task1Checkbox, task2Checkbox, info, reward; Texture *grayCheckboxTex, *blueCheckboxTex; }; From 6d9b93998aeee04a14f1ac80251e5c7aefc20e39 Mon Sep 17 00:00:00 2001 From: h4570 Date: Mon, 7 Dec 2020 22:05:06 +0100 Subject: [PATCH 036/108] changed paths --- src/samples/floors/floors.cpp | 6 +++--- src/samples/floors/managers/floor_manager.cpp | 2 +- src/samples/floors/objects/enemy.cpp | 12 ++++++------ src/samples/floors/objects/player.cpp | 8 ++++---- src/samples/floors/ui.cpp | 12 ++++++------ 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/samples/floors/floors.cpp b/src/samples/floors/floors.cpp index 823a850..1c1324d 100644 --- a/src/samples/floors/floors.cpp +++ b/src/samples/floors/floors.cpp @@ -39,15 +39,15 @@ void Floors::onInit() setBgColorAndAmbientColor(); engine->renderer->setCameraDefinitions(&camera.worldView, &camera.unitCirclePosition, camera.planes); engine->audio.addSongListener(this); - engine->audio.loadSong("mafikizolo-loot.wav"); + engine->audio.loadSong("sounds/mafikizolo-loot.wav"); engine->audio.playSong(); texRepo = engine->renderer->getTextureRepository(); enemy = new Enemy(texRepo); ui = new Ui(texRepo); engine->audio.setSongVolume(80); - texRepo->addByMesh("warrior/", player.mesh, BMP); - texRepo->addByMesh("floor/", floorManager.floors[0].mesh, BMP); + texRepo->addByMesh("meshes/player/", player.mesh, BMP); + texRepo->addByMesh("meshes/floor/", floorManager.floors[0].mesh, BMP); for (u32 i = 1; i < floorManager.floorAmount; i++) texRepo->getByMesh(floorManager.floors[0].mesh.getId(), floorManager.floors[0].mesh.getMaterial(0).getId()) ->addLink(floorManager.floors[i].mesh.getId(), floorManager.floors[i].mesh.getMaterial(0).getId()); diff --git a/src/samples/floors/managers/floor_manager.cpp b/src/samples/floors/managers/floor_manager.cpp index ba2216e..6c81df1 100644 --- a/src/samples/floors/managers/floor_manager.cpp +++ b/src/samples/floors/managers/floor_manager.cpp @@ -81,7 +81,7 @@ void FloorManager::calcSpiral(int X, int Y) void FloorManager::initFloors() { PRINT_LOG("Initializing floors"); - floors[0].mesh.loadObj("floor/", "floor", 3.0F, false); + floors[0].mesh.loadObj("meshes/floor/", "floor", 3.0F, false); floors[0].mesh.shouldBeFrustumCulled = true; floors[0].mesh.shouldBeLighted = true; for (u16 i = 1; i < floorAmount; i++) diff --git a/src/samples/floors/objects/enemy.cpp b/src/samples/floors/objects/enemy.cpp index 4d11344..858e79c 100644 --- a/src/samples/floors/objects/enemy.cpp +++ b/src/samples/floors/objects/enemy.cpp @@ -27,24 +27,24 @@ Enemy::Enemy(TextureRepository *t_texRepo) meshes = new Mesh[getMeshesCount()]; - meshes[0].loadMD2("poss/", "poss_head", 0.3F, false); + meshes[0].loadMD2("meshes/enemy/", "poss_head", 0.3F, false); meshes[0].rotation.x = -1.566F; meshes[0].rotation.z = 1.566F; - meshes[1].loadMD2("poss/", "poss_body", 0.3F, false); + meshes[1].loadMD2("meshes/enemy/", "poss_body", 0.3F, false); meshes[1].rotation.x = -1.566F; meshes[1].rotation.z = 1.566F; - meshes[2].loadMD2("poss/", "poss_weapon", 0.3F, false); + meshes[2].loadMD2("meshes/enemy/", "poss_weapon", 0.3F, false); meshes[2].rotation.x = -1.566F; meshes[2].rotation.z = 1.566F; Vector3 initPos = Vector3(0.00F, 40.00F, 0.00F); setPosition(initPos); - t_texRepo->addByMesh("poss/", meshes[0], PNG); - t_texRepo->addByMesh("poss/", meshes[1], PNG); - t_texRepo->addByMesh("poss/", meshes[2], PNG); + t_texRepo->addByMesh("meshes/enemy/", meshes[0], PNG); + t_texRepo->addByMesh("meshes/enemy/", meshes[1], PNG); + t_texRepo->addByMesh("meshes/enemy/", meshes[2], PNG); meshes[0].playAnimation(1, 6); meshes[1].playAnimation(1, 6); diff --git a/src/samples/floors/objects/player.cpp b/src/samples/floors/objects/player.cpp index e5d9ce9..1de292f 100644 --- a/src/samples/floors/objects/player.cpp +++ b/src/samples/floors/objects/player.cpp @@ -36,7 +36,7 @@ Player::Player(Audio *t_audio) isJumpingAnimationSet = false; isFightingAnimationSet = false; - mesh.loadMD2("warrior/", "warrior", 0.2F, true); + mesh.loadMD2("meshes/player/", "warrior", 0.2F, true); mesh.position.set(0.00F, 40.00F, 0.00F); mesh.rotation.x = -1.566F; mesh.rotation.z = 1.566F; @@ -45,9 +45,9 @@ Player::Player(Audio *t_audio) mesh.setAnimSpeed(0.17F); mesh.playAnimation(0, 0); - walkAdpcm = audio->loadADPCM("walk.adpcm"); - jumpAdpcm = audio->loadADPCM("jump.adpcm"); - boomAdpcm = audio->loadADPCM("boom.adpcm"); + walkAdpcm = audio->loadADPCM("sounds/walk.adpcm"); + jumpAdpcm = audio->loadADPCM("sounds/jump.adpcm"); + boomAdpcm = audio->loadADPCM("sounds/boom.adpcm"); audio->setADPCMVolume(70, 0); PRINT_LOG("Player object created!"); diff --git a/src/samples/floors/ui.cpp b/src/samples/floors/ui.cpp index 6639dc3..52c43ff 100644 --- a/src/samples/floors/ui.cpp +++ b/src/samples/floors/ui.cpp @@ -42,12 +42,12 @@ Ui::Ui(TextureRepository *t_texRepo) task2Checkbox.size.set(32.0F, 32.0F); task2Checkbox.position.set(460.0F, 50.0F); - t_texRepo->add("2d/", "reward", BMP)->addLink(reward.getId()); - t_texRepo->add("2d/", "info_text", PNG)->addLink(info.getId()); - t_texRepo->add("2d/", "check1_text", PNG)->addLink(task1.getId()); - t_texRepo->add("2d/", "check2_text", PNG)->addLink(task2.getId()); - grayCheckboxTex = t_texRepo->add("2d/", "gray_checkmark", PNG); - blueCheckboxTex = t_texRepo->add("2d/", "blue_checkmark", PNG); + t_texRepo->add("ui/", "reward", BMP)->addLink(reward.getId()); + t_texRepo->add("ui/", "info-text", PNG)->addLink(info.getId()); + t_texRepo->add("ui/", "check1-text", PNG)->addLink(task1.getId()); + t_texRepo->add("ui/", "check2-text", PNG)->addLink(task2.getId()); + grayCheckboxTex = t_texRepo->add("ui/", "gray-checkmark", PNG); + blueCheckboxTex = t_texRepo->add("ui/", "blue-checkmark", PNG); grayCheckboxTex->addLink(task1Checkbox.getId()); grayCheckboxTex->addLink(task2Checkbox.getId()); From 5787b6a920244baa93b127ab6c858d809e1c2d78 Mon Sep 17 00:00:00 2001 From: h4570 Date: Mon, 7 Dec 2020 22:11:21 +0100 Subject: [PATCH 037/108] refactor #1 --- src/samples/floors/floors.cpp | 31 +++++++++---------- src/samples/floors/floors.hpp | 4 +-- src/samples/floors/managers/floor_manager.cpp | 8 ++++- src/samples/floors/managers/floor_manager.hpp | 4 ++- src/samples/floors/objects/player.cpp | 5 ++- src/samples/floors/objects/player.hpp | 3 +- 6 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/samples/floors/floors.cpp b/src/samples/floors/floors.cpp index 1c1324d..2a55ab1 100644 --- a/src/samples/floors/floors.cpp +++ b/src/samples/floors/floors.cpp @@ -17,8 +17,7 @@ const u16 FLOORS_COUNT = 144; // Temp change it also in floor_manager.hpp Floors::Floors(Engine *t_engine) - : engine(t_engine), floorManager(FLOORS_COUNT), - player(&t_engine->audio), camera(&t_engine->screen) + : engine(t_engine), floorManager(), camera(&t_engine->screen) { audioTicks = 0; skip1Beat = 0; @@ -28,6 +27,8 @@ Floors::~Floors() { delete enemy; delete ui; + delete floorManager; + delete player; } // ---- @@ -41,30 +42,26 @@ void Floors::onInit() engine->audio.addSongListener(this); engine->audio.loadSong("sounds/mafikizolo-loot.wav"); engine->audio.playSong(); + engine->audio.setSongVolume(80); texRepo = engine->renderer->getTextureRepository(); enemy = new Enemy(texRepo); ui = new Ui(texRepo); - engine->audio.setSongVolume(80); - - texRepo->addByMesh("meshes/player/", player.mesh, BMP); - texRepo->addByMesh("meshes/floor/", floorManager.floors[0].mesh, BMP); - for (u32 i = 1; i < floorManager.floorAmount; i++) - texRepo->getByMesh(floorManager.floors[0].mesh.getId(), floorManager.floors[0].mesh.getMaterial(0).getId()) - ->addLink(floorManager.floors[i].mesh.getId(), floorManager.floors[i].mesh.getMaterial(0).getId()); + floorManager = new FloorManager(FLOORS_COUNT, texRepo); + player = new Player(&engine->audio, texRepo); } void Floors::onUpdate() { - ui->update(player); - enemy->update(floorManager); + ui->update(*player); + enemy->update(*floorManager); lightManager.update(); - camera.update(engine->pad, player.mesh); - floorManager.update(player); - player.update(engine->pad, camera, floorManager, *enemy); - engine->renderer->draw(player.mesh); + camera.update(engine->pad, player->mesh); + floorManager->update(*player); + player->update(engine->pad, camera, *floorManager, *enemy); + engine->renderer->draw(player->mesh); engine->renderer->draw(enemy->getMeshes(), enemy->getMeshesCount()); for (u16 i = 0; i < FLOORS_COUNT; i++) - engine->renderer->drawByPath3(floorManager.floors[i].mesh, lightManager.bulbs, lightManager.bulbsCount); + engine->renderer->drawByPath3(floorManager->floors[i].mesh, lightManager.bulbs, lightManager.bulbsCount); ui->render(engine->renderer); // 2D rendering ist LAST step, because layers gonna play there. } @@ -73,7 +70,7 @@ void Floors::onAudioTick() if ((++audioTicks + 20) % 21 == 0) { if (skip1Beat) - floorManager.onAudioTick(); + floorManager->onAudioTick(); lightManager.onAudioTick(); skip1Beat = !skip1Beat; } diff --git a/src/samples/floors/floors.hpp b/src/samples/floors/floors.hpp index bf2905f..dc9f8b8 100644 --- a/src/samples/floors/floors.hpp +++ b/src/samples/floors/floors.hpp @@ -42,8 +42,8 @@ private: u8 skip1Beat; TextureRepository *texRepo; LightManager lightManager; - FloorManager floorManager; - Player player; + FloorManager *floorManager; + Player *player; Camera camera; Ui *ui; Enemy *enemy; diff --git a/src/samples/floors/managers/floor_manager.cpp b/src/samples/floors/managers/floor_manager.cpp index 6c81df1..e1b1d09 100644 --- a/src/samples/floors/managers/floor_manager.cpp +++ b/src/samples/floors/managers/floor_manager.cpp @@ -20,8 +20,9 @@ /** Calculate square spiral offsets and initialize floors * @param floorAmount must be a number from square root table */ -FloorManager::FloorManager(int t_floorAmount) +FloorManager::FloorManager(int t_floorAmount, TextureRepository *t_texRepo) { + texRepo = t_texRepo; floorAmount = t_floorAmount; spirals = new Point[t_floorAmount]; int floorSpiralMaxOffset = (int)Math::sqrt(t_floorAmount); @@ -84,8 +85,13 @@ void FloorManager::initFloors() floors[0].mesh.loadObj("meshes/floor/", "floor", 3.0F, false); floors[0].mesh.shouldBeFrustumCulled = true; floors[0].mesh.shouldBeLighted = true; + texRepo->addByMesh("meshes/floor/", floors[0].mesh, BMP); for (u16 i = 1; i < floorAmount; i++) + { floors[i].init(floors[0].mesh, spirals[i], i); + texRepo->getByMesh(floors[0].mesh.getId(), floors[0].mesh.getMaterial(0).getId()) + ->addLink(floors[i].mesh.getId(), floors[i].mesh.getMaterial(0).getId()); + } PRINT_LOG("Floors initialized!"); } diff --git a/src/samples/floors/managers/floor_manager.hpp b/src/samples/floors/managers/floor_manager.hpp index c7eb14a..61a64b7 100644 --- a/src/samples/floors/managers/floor_manager.hpp +++ b/src/samples/floors/managers/floor_manager.hpp @@ -14,6 +14,7 @@ class Player; // Forward definition #include +#include #include #include @@ -24,7 +25,7 @@ class FloorManager { public: - FloorManager(int t_floorAmount); + FloorManager(int t_floorAmount, TextureRepository *t_texRepo); ~FloorManager(); Floor floors[144]; // Temp change it also in floors.cpp u16 floorAmount; @@ -32,6 +33,7 @@ public: void onAudioTick(); private: + TextureRepository *texRepo; u8 audioOffset, audioMode; u32 audioTick; float trick; diff --git a/src/samples/floors/objects/player.cpp b/src/samples/floors/objects/player.cpp index 1de292f..a79a833 100644 --- a/src/samples/floors/objects/player.cpp +++ b/src/samples/floors/objects/player.cpp @@ -21,9 +21,10 @@ // Constructors/Destructors // ---- -Player::Player(Audio *t_audio) +Player::Player(Audio *t_audio, TextureRepository *t_texRepo) { PRINT_LOG("Creating player object"); + texRepo = t_texRepo; audio = t_audio; gravity = 0.1F; lift = -1.0F; @@ -45,6 +46,8 @@ Player::Player(Audio *t_audio) mesh.setAnimSpeed(0.17F); mesh.playAnimation(0, 0); + texRepo->addByMesh("meshes/player/", mesh, BMP); + walkAdpcm = audio->loadADPCM("sounds/walk.adpcm"); jumpAdpcm = audio->loadADPCM("sounds/jump.adpcm"); boomAdpcm = audio->loadADPCM("sounds/boom.adpcm"); diff --git a/src/samples/floors/objects/player.hpp b/src/samples/floors/objects/player.hpp index 75b1ccb..b233a12 100644 --- a/src/samples/floors/objects/player.hpp +++ b/src/samples/floors/objects/player.hpp @@ -33,7 +33,7 @@ public: float gravity, velocity, lift; Mesh mesh; s16 indexOfCurrentFloor; - Player(Audio *t_audio); + Player(Audio *t_audio, TextureRepository *t_texRepo); ~Player(); void update(const Pad &t_pad, const Camera &t_camera, const FloorManager &t_floorManager, Enemy &enemy); @@ -43,6 +43,7 @@ public: inline const Vector3 &getPosition() const { return mesh.position; } private: + TextureRepository *texRepo; u32 jumpCounter, killedEnemies; Vector3 *getNextPosition(const Pad &t_pad, const Camera &t_camera); FloorsCheck *checkFloors(const FloorManager &t_floorManager, const Vector3 &t_nextPos); From 8a0f5f7b847a48b2ee58a96acaedf7782fecd676 Mon Sep 17 00:00:00 2001 From: h4570 Date: Mon, 7 Dec 2020 22:14:36 +0100 Subject: [PATCH 038/108] refactor #2 --- src/samples/floors/camera.cpp | 11 ----------- src/samples/floors/objects/floor.cpp | 5 ----- src/samples/floors/objects/player.cpp | 6 ------ 3 files changed, 22 deletions(-) diff --git a/src/samples/floors/camera.cpp b/src/samples/floors/camera.cpp index b9c583e..56c4aa1 100644 --- a/src/samples/floors/camera.cpp +++ b/src/samples/floors/camera.cpp @@ -67,15 +67,4 @@ void Camera::followBy(Mesh &t_mesh) position.x = unitCirclePosition.x + t_mesh.position.x; position.y = unitCirclePosition.y + t_mesh.position.y; position.z = unitCirclePosition.z + t_mesh.position.z; - - if (position.x > (-0.04F * verticalLevel) && - position.x < 0.0F && - position.z > (0.998F * verticalLevel)) - { - // TODO - // position.x = 0.0F; - // position.y = 0.0F; - // position.z = 0.0F; - // horizontalLevel = 0.0F; - } } diff --git a/src/samples/floors/objects/floor.cpp b/src/samples/floors/objects/floor.cpp index ac859b8..61662f4 100644 --- a/src/samples/floors/objects/floor.cpp +++ b/src/samples/floors/objects/floor.cpp @@ -37,11 +37,6 @@ Floor::~Floor() {} // Methods // ---- -/** Init floor position and animation timer position - * @param spec pointer to 3D object specification - * @param spiral Spiral position - * @param initOffset Number of floor (index) - */ void Floor::init(const Mesh &mother, const Point &spiral, const u8 &initOffset) { this->mesh.position.set(0.00F, -10.00F, 0.00F); diff --git a/src/samples/floors/objects/player.cpp b/src/samples/floors/objects/player.cpp index a79a833..6d6ae79 100644 --- a/src/samples/floors/objects/player.cpp +++ b/src/samples/floors/objects/player.cpp @@ -64,7 +64,6 @@ Player::~Player() // Methods // ---- -/** Update player position and control gravity */ void Player::update(const Pad &t_pad, const Camera &t_camera, const FloorManager &floorManager, Enemy &enemy) { Vector3 *nextPos = getNextPosition(t_pad, t_camera); @@ -106,7 +105,6 @@ Vector3 *Player::getNextPosition(const Pad &t_pad, const Camera &t_camera) return result; } -/** Move player when pad move buttons are pressed and is not blocked by any floor side */ void Player::updatePosition(const Pad &t_pad, const Camera &t_camera, const FloorManager &t_floorManager, const FloorsCheck &t_floorsCheck, const Vector3 &t_nextPos, Enemy &enemy) { if (t_pad.rJoyH >= 200) @@ -203,10 +201,6 @@ void Player::updateGravity(FloorsCheck *t_floorsCheck) } } -// --- -// Private -// --- - FloorsCheck *Player::checkFloors(const FloorManager &t_floorManager, const Vector3 &t_nextPos) { FloorsCheck *result = new FloorsCheck; From 582b42c29e4af7560df7af32ff230b9c463ba620 Mon Sep 17 00:00:00 2001 From: Foxar Date: Tue, 8 Dec 2020 13:36:13 +0100 Subject: [PATCH 039/108] Changed: Optimization from code review PR #42 --- src/samples/dolphin/dolphin.cpp | 2 +- src/samples/dolphin/objects/collectible.cpp | 12 +----------- src/samples/dolphin/objects/collectible.hpp | 4 ++-- src/samples/dolphin/objects/player.cpp | 15 +++++---------- src/samples/dolphin/objects/player.hpp | 4 ++-- 5 files changed, 11 insertions(+), 26 deletions(-) diff --git a/src/samples/dolphin/dolphin.cpp b/src/samples/dolphin/dolphin.cpp index 841be31..36b766f 100755 --- a/src/samples/dolphin/dolphin.cpp +++ b/src/samples/dolphin/dolphin.cpp @@ -114,7 +114,7 @@ void Dolphin::onUpdate() oysters[i].update(); Vector3 vecDist = oysters[i].mesh.position - player.mesh.position; float dist = Math::sqrt(vecDist.x + vecDist.y + vecDist.z); - if (dist < 2.5F && player.getIsJumping() && oysters[i].isActive()) + if (dist < 2.5F && player.isJumping() && oysters[i].isActive()) { printf("Pickup %d Dist %d\n", i, dist); //oysters[i].setActive(false); diff --git a/src/samples/dolphin/objects/collectible.cpp b/src/samples/dolphin/objects/collectible.cpp index e1c76a1..9cd7d78 100644 --- a/src/samples/dolphin/objects/collectible.cpp +++ b/src/samples/dolphin/objects/collectible.cpp @@ -29,22 +29,12 @@ Collectible::~Collectible() { } -void Collectible::setActive(u8 b) -{ - bActive = b; -} - void Collectible::disappear() { bDisappearing = true; lift = 10; } -u8 Collectible::isActive() -{ - return bActive; -} - void Collectible::update() { mesh.rotation.y += 0.08F; @@ -58,4 +48,4 @@ void Collectible::update() bDisappearing = false; } } -} \ No newline at end of file +} diff --git a/src/samples/dolphin/objects/collectible.hpp b/src/samples/dolphin/objects/collectible.hpp index 775eb70..32795ea 100644 --- a/src/samples/dolphin/objects/collectible.hpp +++ b/src/samples/dolphin/objects/collectible.hpp @@ -24,8 +24,8 @@ public: ~Collectible(); void update(); void disappear(); - void setActive(u8 b); - u8 isActive(); + void setActive(const u8 &b) { bActive = b; } + u8 isActive() { return bActive; } private: u8 bActive, bDisappearing; diff --git a/src/samples/dolphin/objects/player.cpp b/src/samples/dolphin/objects/player.cpp index 125a3a5..aafe048 100755 --- a/src/samples/dolphin/objects/player.cpp +++ b/src/samples/dolphin/objects/player.cpp @@ -23,7 +23,7 @@ Player::Player() mesh.position.set(0.0F, 0.0F, 10.0f); mesh.rotation.x = -1.6F; mesh.setAnimSpeed(0.05F); - isJumping = false; + bIsJumping = false; } Player::~Player() @@ -62,9 +62,9 @@ void Player::update(Pad &t_pad) } if (mesh.getCurrentAnimationFrame() > 25 && mesh.getCurrentAnimationFrame() < 47) - isJumping = true; + bIsJumping = true; else - isJumping = false; + bIsJumping = false; /* if (lift < 0) mesh.rotation.y = 1; @@ -82,13 +82,8 @@ void Player::update(Pad &t_pad) } mesh.position.z += - Math::cos(mesh.rotation.z) * velocity * (isJumping + 1); + Math::cos(mesh.rotation.z) * velocity * (bIsJumping + 1); mesh.position.x += - Math::sin(mesh.rotation.z) * velocity * (isJumping + 1); + Math::sin(mesh.rotation.z) * velocity * (bIsJumping + 1); mesh.position.y += lift; } - -u8 Player::getIsJumping() -{ - return isJumping; -} \ No newline at end of file diff --git a/src/samples/dolphin/objects/player.hpp b/src/samples/dolphin/objects/player.hpp index fecbd51..c615d13 100755 --- a/src/samples/dolphin/objects/player.hpp +++ b/src/samples/dolphin/objects/player.hpp @@ -26,10 +26,10 @@ public: Player(); ~Player(); void update(Pad &t_pad); - u8 getIsJumping(); + const u8 &isJumping() { return bIsJumping; } private: - u8 isJumping; + u8 bIsJumping; Vector3 playerNextPosition; }; From 4557485cab0d53f9ca18cef6ae4304400d428291 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Mostowik?= Date: Tue, 8 Dec 2020 14:20:00 +0100 Subject: [PATCH 040/108] Fixed datatype typos in dolphin.cpp --- src/samples/dolphin/dolphin.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/samples/dolphin/dolphin.cpp b/src/samples/dolphin/dolphin.cpp index 36b766f..704a04b 100755 --- a/src/samples/dolphin/dolphin.cpp +++ b/src/samples/dolphin/dolphin.cpp @@ -11,10 +11,10 @@ #include "dolphin.hpp" #include "utils/math.hpp" -const u8 WATER_TILE_SCALE = 5.0F; -const u8 WATER_SIZE = 100.0F; +const u8 WATER_TILE_SCALE = 5; +const u8 WATER_SIZE = 100; -const u8 OYSTERS_COUNT = 15.0F; +const u8 OYSTERS_COUNT = 15; Dolphin::Dolphin(Engine *t_engine) : engine(t_engine), camera(&engine->screen) { From 45edf7aedba655391ed4d04a751aaf846baaa2e7 Mon Sep 17 00:00:00 2001 From: Foxar Date: Tue, 8 Dec 2020 14:42:43 +0100 Subject: [PATCH 041/108] Added: Static fps member to dolphin.cpp, auto-updated with engine->fps, made player movement multiplied by deltatime of last frame. --- src/samples/dolphin/dolphin.cpp | 5 ++++- src/samples/dolphin/dolphin.hpp | 1 + src/samples/dolphin/objects/player.cpp | 5 ++++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/samples/dolphin/dolphin.cpp b/src/samples/dolphin/dolphin.cpp index 704a04b..32f0175 100755 --- a/src/samples/dolphin/dolphin.cpp +++ b/src/samples/dolphin/dolphin.cpp @@ -16,6 +16,8 @@ const u8 WATER_SIZE = 100; const u8 OYSTERS_COUNT = 15; +float Dolphin::engineFPS = 60.F; + Dolphin::Dolphin(Engine *t_engine) : engine(t_engine), camera(&engine->screen) { oysters = new Collectible[OYSTERS_COUNT]; @@ -93,8 +95,9 @@ void Dolphin::onInit() void Dolphin::onUpdate() { + Dolphin::engineFPS = engine->fps; if (engine->pad.isCrossClicked) - printf("PlayerPos (%f,%f), FPS:%f\n", player.mesh.position.x, player.mesh.position.z, this->engine->fps); + printf("Delta multiplier: %f\n", 60.0F / engine->fps); float xDist = player.mesh.position.x - water.position.x; float zDist = player.mesh.position.z - water.position.z; diff --git a/src/samples/dolphin/dolphin.hpp b/src/samples/dolphin/dolphin.hpp index 9630f5e..273f0ef 100755 --- a/src/samples/dolphin/dolphin.hpp +++ b/src/samples/dolphin/dolphin.hpp @@ -33,6 +33,7 @@ public: void onUpdate(); Engine *engine; + static float engineFPS; private: LightBulb bulb; diff --git a/src/samples/dolphin/objects/player.cpp b/src/samples/dolphin/objects/player.cpp index aafe048..8051b55 100755 --- a/src/samples/dolphin/objects/player.cpp +++ b/src/samples/dolphin/objects/player.cpp @@ -9,6 +9,7 @@ */ #include "player.hpp" +#include "dolphin.hpp" #include #include @@ -38,7 +39,6 @@ void Player::update(Pad &t_pad) lift += 0.05F; if (lift < 0.1F && lift > -0.1F) lift = 0; - velocity = 0; if (t_pad.lJoyV <= 100) velocity = 1; else if (t_pad.lJoyV >= 200) @@ -81,9 +81,12 @@ void Player::update(Pad &t_pad) mesh.playAnimation(1, 14, 0); } + velocity *= 60.0F / Dolphin::engineFPS; //Moving by deltatime of last frame. mesh.position.z += Math::cos(mesh.rotation.z) * velocity * (bIsJumping + 1); mesh.position.x += Math::sin(mesh.rotation.z) * velocity * (bIsJumping + 1); mesh.position.y += lift; + + velocity = 0; } From 220caba84af7b4f9cc7df2a59ba1a6efcdcb366a Mon Sep 17 00:00:00 2001 From: Foxar Date: Tue, 8 Dec 2020 15:30:48 +0100 Subject: [PATCH 042/108] Fixed: Added MODE_STRETCH to wateroverlay, fixing the tiling issue. --- src/samples/dolphin/dolphin.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/samples/dolphin/dolphin.cpp b/src/samples/dolphin/dolphin.cpp index 32f0175..76526ae 100755 --- a/src/samples/dolphin/dolphin.cpp +++ b/src/samples/dolphin/dolphin.cpp @@ -44,6 +44,7 @@ void Dolphin::onInit() printf("Loading water overlay...\n"); waterOverlay.size.set(640.0F, 480.0F); Texture *watOverlayTex = texRepo->add("2d/", "underwater_overlay", PNG); + waterOverlay.setMode(MODE_STRETCH); watOverlayTex->addLink(waterOverlay.getId()); printf("Loaded.\n"); From a017c3a3b6faa5842a85ae05a2e400b2cfc61dcb Mon Sep 17 00:00:00 2001 From: h4570 Date: Tue, 8 Dec 2020 15:59:24 +0100 Subject: [PATCH 043/108] added color blending for draw() --- src/engine/modules/vif_sender.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/engine/modules/vif_sender.cpp b/src/engine/modules/vif_sender.cpp index 9158a8d..3135e09 100644 --- a/src/engine/modules/vif_sender.cpp +++ b/src/engine/modules/vif_sender.cpp @@ -138,8 +138,10 @@ void VifSender::drawVertices(Mesh &t_mesh, u32 t_start, u32 t_end, VECTOR *t_ver packet2_utils_gs_add_texbuff_clut(currPacket, textureBuffer, &t_mesh.clut); packet2_utils_gs_add_prim_giftag(currPacket, t_prim, vertCount, DRAW_STQ2_REGLIST, 3, 0); - for (u8 j = 0; j < 4; j++) - packet2_add_u32(currPacket, 128); + packet2_add_u32(currPacket, t_mesh.color.r); + packet2_add_u32(currPacket, t_mesh.color.g); + packet2_add_u32(currPacket, t_mesh.color.b); + packet2_add_u32(currPacket, t_mesh.color.a); // Clipping tests start From f4421b659d629d7970012d5213ed31575dbc0e83 Mon Sep 17 00:00:00 2001 From: Foxar Date: Tue, 8 Dec 2020 16:48:43 +0100 Subject: [PATCH 044/108] Added: Seabed model/code. --- src/samples/dolphin/dolphin.cpp | 8 ++++++++ src/samples/dolphin/dolphin.hpp | 8 +++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/samples/dolphin/dolphin.cpp b/src/samples/dolphin/dolphin.cpp index 76526ae..cc4de00 100755 --- a/src/samples/dolphin/dolphin.cpp +++ b/src/samples/dolphin/dolphin.cpp @@ -55,6 +55,13 @@ void Dolphin::onInit() water.shouldBeFrustumCulled = false; water.shouldBeBackfaceCulled = false; + printf("Loading seabed...\n"); + seabed.loadObj("seabed/", "seabed", 10.0F, false); + seabed.position.set(0, -250.0F, 0); + texRepo->addByMesh("seabed/", seabed, BMP); + seabed.shouldBeBackfaceCulled = false; + seabed.shouldBeFrustumCulled = false; + skybox.loadObj("skybox/", "skybox", 400.0F, false); skybox.shouldBeFrustumCulled = false; @@ -137,6 +144,7 @@ void Dolphin::onUpdate() engine->renderer->draw(water); engine->renderer->draw(skybox); engine->renderer->draw(waterbox); + engine->renderer->draw(seabed); for (u8 i = 0; i < OYSTERS_COUNT; i++) { if (oysters[i].isActive()) diff --git a/src/samples/dolphin/dolphin.hpp b/src/samples/dolphin/dolphin.hpp index 273f0ef..0a42568 100755 --- a/src/samples/dolphin/dolphin.hpp +++ b/src/samples/dolphin/dolphin.hpp @@ -36,13 +36,19 @@ public: static float engineFPS; private: + /*DO NOT TOUCH ORDER OF DECLARATION! + GCC BUG CAUSES RENDERING TO FAIL IN ANY OTHER ORDER OF DECLARATION!*/ + Mesh seabed; + Mesh island; + Mesh skybox; LightBulb bulb; Player player; Camera camera; - Mesh island, skybox, water, waterbox; Collectible *oysters; TextureRepository *texRepo; Sprite waterOverlay; + Mesh water; + Mesh waterbox; }; #endif From c4fc52b043a5367126c5f99d52176bd0968fba68 Mon Sep 17 00:00:00 2001 From: Foxar Date: Tue, 8 Dec 2020 19:27:30 +0100 Subject: [PATCH 045/108] Added: Sprites representing lifes player has left, and life variable. --- src/samples/dolphin/dolphin.cpp | 14 ++++++++++++++ src/samples/dolphin/dolphin.hpp | 1 + src/samples/dolphin/objects/player.cpp | 1 + src/samples/dolphin/objects/player.hpp | 2 ++ 4 files changed, 18 insertions(+) diff --git a/src/samples/dolphin/dolphin.cpp b/src/samples/dolphin/dolphin.cpp index cc4de00..1c12dee 100755 --- a/src/samples/dolphin/dolphin.cpp +++ b/src/samples/dolphin/dolphin.cpp @@ -91,6 +91,16 @@ void Dolphin::onInit() oysters[i].mesh.position.set(i * -100, 5.0F, i * -100); } + Texture *pLifeTex = texRepo->add("2d/", "life", PNG); + //pLifeTex->addLink(lifeSprites[0].getId()); + for (int i = 0; i < 3; i++) + { + lifeSprites[i].size.set(64.0F, 64.0F); + lifeSprites[i].setMode(MODE_STRETCH); + lifeSprites[i].position.set(395 + (64 * i), 0); + pLifeTex->addLink(lifeSprites[i].getId()); + } + texRepo->addByMesh("dolphin/", player.mesh, BMP); texRepo->addByMesh("sunnyisl/", island, BMP); texRepo->addByMesh("skybox/", skybox, BMP); @@ -152,6 +162,10 @@ void Dolphin::onUpdate() engine->renderer->draw(oysters[i].mesh); } } + for (u8 i = 0; i < player.getLifes(); i++) + { + engine->renderer->draw(lifeSprites[i]); + } engine->renderer->draw(player.mesh); if (camera.position.y < WATER_LEVEL) engine->renderer->draw(waterOverlay); diff --git a/src/samples/dolphin/dolphin.hpp b/src/samples/dolphin/dolphin.hpp index 0a42568..1d1ce9c 100755 --- a/src/samples/dolphin/dolphin.hpp +++ b/src/samples/dolphin/dolphin.hpp @@ -49,6 +49,7 @@ private: Sprite waterOverlay; Mesh water; Mesh waterbox; + Sprite lifeSprites[3]; }; #endif diff --git a/src/samples/dolphin/objects/player.cpp b/src/samples/dolphin/objects/player.cpp index 8051b55..a430173 100755 --- a/src/samples/dolphin/objects/player.cpp +++ b/src/samples/dolphin/objects/player.cpp @@ -25,6 +25,7 @@ Player::Player() mesh.rotation.x = -1.6F; mesh.setAnimSpeed(0.05F); bIsJumping = false; + lifes = 3; } Player::~Player() diff --git a/src/samples/dolphin/objects/player.hpp b/src/samples/dolphin/objects/player.hpp index c615d13..39d6f8b 100755 --- a/src/samples/dolphin/objects/player.hpp +++ b/src/samples/dolphin/objects/player.hpp @@ -27,10 +27,12 @@ public: ~Player(); void update(Pad &t_pad); const u8 &isJumping() { return bIsJumping; } + const u8 &getLifes() { return lifes; } private: u8 bIsJumping; Vector3 playerNextPosition; + u8 lifes; }; #endif From f86a1bf190663366bb7a1338531bf3af3f6b8d7d Mon Sep 17 00:00:00 2001 From: Foxar Date: Wed, 9 Dec 2020 14:27:40 +0100 Subject: [PATCH 046/108] Added: Underwater/Above water soundscape, misc sound effects. Reworked player class a bit to contain pointer to the game engine for sound playing. --- src/samples/dolphin/dolphin.cpp | 24 +++++++++++++++++++++++- src/samples/dolphin/dolphin.hpp | 2 ++ src/samples/dolphin/objects/player.cpp | 19 +++++++++++++++++++ src/samples/dolphin/objects/player.hpp | 7 ++++++- 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/samples/dolphin/dolphin.cpp b/src/samples/dolphin/dolphin.cpp index 1c12dee..9df9247 100755 --- a/src/samples/dolphin/dolphin.cpp +++ b/src/samples/dolphin/dolphin.cpp @@ -27,8 +27,20 @@ Dolphin::~Dolphin() {} void Dolphin::onInit() { + player.init(engine); engine->renderer->setCameraDefinitions(&camera.worldView, &camera.position, camera.planes); + engine->audio.loadSong("sound/dirediredocks.wav"); + engine->audio.playSong(); + engine->audio.setSongVolume(60); + + surfaceAmbient = engine->audio.loadADPCM("sound/surface.sad"); + underwaterAmbient = engine->audio.loadADPCM("sound/underwater.sad"); + pickupSound = engine->audio.loadADPCM("sound/pickup.sad"); + + engine->audio.playADPCM(surfaceAmbient, 0); + engine->audio.playADPCM(underwaterAmbient, 1); + texRepo = engine->renderer->getTextureRepository(); engine->renderer->disableVSync(); @@ -138,8 +150,9 @@ void Dolphin::onUpdate() if (dist < 2.5F && player.isJumping() && oysters[i].isActive()) { printf("Pickup %d Dist %d\n", i, dist); - //oysters[i].setActive(false); oysters[i].disappear(); + engine->audio.setADPCMVolume(30, 3); + engine->audio.playADPCM(pickupSound, 3); } } @@ -168,5 +181,14 @@ void Dolphin::onUpdate() } engine->renderer->draw(player.mesh); if (camera.position.y < WATER_LEVEL) + { engine->renderer->draw(waterOverlay); + engine->audio.setADPCMVolume(0, 0); //Surface ambient + engine->audio.setADPCMVolume(65, 1); //Underwater ambient + } + else + { + engine->audio.setADPCMVolume(50, 0); //Surface ambient + engine->audio.setADPCMVolume(0, 1); //Underwater ambient + } } diff --git a/src/samples/dolphin/dolphin.hpp b/src/samples/dolphin/dolphin.hpp index 1d1ce9c..614b94f 100755 --- a/src/samples/dolphin/dolphin.hpp +++ b/src/samples/dolphin/dolphin.hpp @@ -50,6 +50,8 @@ private: Mesh water; Mesh waterbox; Sprite lifeSprites[3]; + audsrv_adpcm_t *underwaterAmbient, *surfaceAmbient; + audsrv_adpcm_t *pickupSound; }; #endif diff --git a/src/samples/dolphin/objects/player.cpp b/src/samples/dolphin/objects/player.cpp index a430173..7a8d70a 100755 --- a/src/samples/dolphin/objects/player.cpp +++ b/src/samples/dolphin/objects/player.cpp @@ -32,6 +32,12 @@ Player::~Player() { } +void Player::init(Engine *t_engine) +{ + pEngine = t_engine; + waterImpact = t_engine->audio.loadADPCM("sound/water_impact.sad"); +} + void Player::update(Pad &t_pad) { if (lift > 0.0F) @@ -81,6 +87,19 @@ void Player::update(Pad &t_pad) printf("Swim animation\n"); mesh.playAnimation(1, 14, 0); } + if (mesh.getCurrentAnimationFrame() == 42) + { + if (!bIsImpactingWater) + { + pEngine->audio.setADPCMVolume(50, 2); + pEngine->audio.playADPCM(waterImpact, 2); + } + bIsImpactingWater = true; + } + else if (bIsImpactingWater) + { + bIsImpactingWater = false; + } velocity *= 60.0F / Dolphin::engineFPS; //Moving by deltatime of last frame. mesh.position.z += diff --git a/src/samples/dolphin/objects/player.hpp b/src/samples/dolphin/objects/player.hpp index 39d6f8b..3c37938 100755 --- a/src/samples/dolphin/objects/player.hpp +++ b/src/samples/dolphin/objects/player.hpp @@ -14,9 +14,10 @@ #define WATER_LEVEL -5.0F #include "../camera.hpp" +#include #include #include - +class Engine; class Player { @@ -25,6 +26,7 @@ public: Mesh mesh; Player(); ~Player(); + void init(Engine *t_engine); void update(Pad &t_pad); const u8 &isJumping() { return bIsJumping; } const u8 &getLifes() { return lifes; } @@ -33,6 +35,9 @@ private: u8 bIsJumping; Vector3 playerNextPosition; u8 lifes; + audsrv_adpcm_t *waterImpact; + Engine *pEngine; + u8 bIsImpactingWater; }; #endif From 1c9351e5e95f8b2451de85b28e71d02411234354 Mon Sep 17 00:00:00 2001 From: Foxar Date: Thu, 10 Dec 2020 12:52:54 +0100 Subject: [PATCH 047/108] Added: Sea mines, when fully implemented, will explode and take lifes from player. --- src/samples/dolphin/dolphin.cpp | 5 +++++ src/samples/dolphin/dolphin.hpp | 1 + 2 files changed, 6 insertions(+) diff --git a/src/samples/dolphin/dolphin.cpp b/src/samples/dolphin/dolphin.cpp index 9df9247..053e813 100755 --- a/src/samples/dolphin/dolphin.cpp +++ b/src/samples/dolphin/dolphin.cpp @@ -74,6 +74,10 @@ void Dolphin::onInit() seabed.shouldBeBackfaceCulled = false; seabed.shouldBeFrustumCulled = false; + mine.loadObj("mine/", "mine", 5.0F, false); + mine.position.set(0, 0, 0); + texRepo->addByMesh("mine/", mine, BMP); + skybox.loadObj("skybox/", "skybox", 400.0F, false); skybox.shouldBeFrustumCulled = false; @@ -168,6 +172,7 @@ void Dolphin::onUpdate() engine->renderer->draw(skybox); engine->renderer->draw(waterbox); engine->renderer->draw(seabed); + engine->renderer->draw(mine); for (u8 i = 0; i < OYSTERS_COUNT; i++) { if (oysters[i].isActive()) diff --git a/src/samples/dolphin/dolphin.hpp b/src/samples/dolphin/dolphin.hpp index 614b94f..f48c149 100755 --- a/src/samples/dolphin/dolphin.hpp +++ b/src/samples/dolphin/dolphin.hpp @@ -41,6 +41,7 @@ private: Mesh seabed; Mesh island; Mesh skybox; + Mesh mine; LightBulb bulb; Player player; Camera camera; From e7aa4d13cc40e17f2fe9a9bd90832cc306e54729 Mon Sep 17 00:00:00 2001 From: Foxar Date: Thu, 10 Dec 2020 13:52:45 +0100 Subject: [PATCH 048/108] Added: Mines now explode and hurt the player, added new makefile and made more mines spawn on game start. --- src/samples/dolphin/Makefile | 1 + src/samples/dolphin/dolphin.cpp | 56 +++++++++++++++++++++++--- src/samples/dolphin/dolphin.hpp | 4 ++ src/samples/dolphin/objects/player.cpp | 2 +- src/samples/dolphin/objects/player.hpp | 1 + 5 files changed, 57 insertions(+), 7 deletions(-) diff --git a/src/samples/dolphin/Makefile b/src/samples/dolphin/Makefile index 5c92574..c0b302d 100755 --- a/src/samples/dolphin/Makefile +++ b/src/samples/dolphin/Makefile @@ -6,6 +6,7 @@ EE_OBJS = \ camera.o \ objects/player.o \ objects/collectible.o \ + objects/mine.o \ dolphin.o \ main.o diff --git a/src/samples/dolphin/dolphin.cpp b/src/samples/dolphin/dolphin.cpp index 053e813..cf506a6 100755 --- a/src/samples/dolphin/dolphin.cpp +++ b/src/samples/dolphin/dolphin.cpp @@ -14,13 +14,15 @@ const u8 WATER_TILE_SCALE = 5; const u8 WATER_SIZE = 100; -const u8 OYSTERS_COUNT = 15; +const u8 MINES_COUNT = 15; +const u8 OYSTERS_COUNT = 5; float Dolphin::engineFPS = 60.F; Dolphin::Dolphin(Engine *t_engine) : engine(t_engine), camera(&engine->screen) { oysters = new Collectible[OYSTERS_COUNT]; + mines = new Mine[MINES_COUNT]; } Dolphin::~Dolphin() {} @@ -37,6 +39,7 @@ void Dolphin::onInit() surfaceAmbient = engine->audio.loadADPCM("sound/surface.sad"); underwaterAmbient = engine->audio.loadADPCM("sound/underwater.sad"); pickupSound = engine->audio.loadADPCM("sound/pickup.sad"); + boomSound = engine->audio.loadADPCM("sound/boom.sad"); engine->audio.playADPCM(surfaceAmbient, 0); engine->audio.playADPCM(underwaterAmbient, 1); @@ -74,10 +77,6 @@ void Dolphin::onInit() seabed.shouldBeBackfaceCulled = false; seabed.shouldBeFrustumCulled = false; - mine.loadObj("mine/", "mine", 5.0F, false); - mine.position.set(0, 0, 0); - texRepo->addByMesh("mine/", mine, BMP); - skybox.loadObj("skybox/", "skybox", 400.0F, false); skybox.shouldBeFrustumCulled = false; @@ -107,6 +106,25 @@ void Dolphin::onInit() oysters[i].mesh.position.set(i * -100, 5.0F, i * -100); } + printf("Loading mine .obj\n"); + mines[0].mesh.loadObj("mine/", "mine", 10.F, false); + printf("Loaded."); + mines[0].mesh.shouldBeBackfaceCulled = false; + mines[0].mesh.shouldBeFrustumCulled = false; + mines[0].mesh.position.set(5, -15, 32); + texRepo->addByMesh("mine/", mines[0].mesh, BMP); + printf("Adding mine texture.\n"); + for (int i = 1; i < MINES_COUNT; i++) + { + printf("Processing mine %d\n", i); + mines[i].mesh.loadFrom(mines[0].mesh); + mines[i].mesh.shouldBeBackfaceCulled = false; + mines[i].mesh.shouldBeFrustumCulled = false; + texRepo->getByMesh(mines[0].mesh.getId(), mines[0].mesh.getMaterial(0).getId()) + ->addLink(mines[i].mesh.getId(), mines[i].mesh.getMaterial(0).getId()); + mines[i].mesh.position.set(i * -10, 0.0F, i * -68); + } + Texture *pLifeTex = texRepo->add("2d/", "life", PNG); //pLifeTex->addLink(lifeSprites[0].getId()); for (int i = 0; i < 3; i++) @@ -129,6 +147,7 @@ void Dolphin::onInit() void Dolphin::onUpdate() { + Dolphin::engineFPS = engine->fps; if (engine->pad.isCrossClicked) printf("Delta multiplier: %f\n", 60.0F / engine->fps); @@ -155,11 +174,32 @@ void Dolphin::onUpdate() { printf("Pickup %d Dist %d\n", i, dist); oysters[i].disappear(); + player.setLife(player.getLifes() + 1); engine->audio.setADPCMVolume(30, 3); engine->audio.playADPCM(pickupSound, 3); } } + for (int i = 0; i < MINES_COUNT; i++) + { + mines[i].update(); + Vector3 vecDist = mines[i].mesh.position - player.mesh.position; + float dist = Math::sqrt((vecDist.x * vecDist.x) + + (vecDist.y * vecDist.y) + + (vecDist.z * vecDist.z)); + if (dist < 20.F && !mines[i].getExplosionTicks()) + { + printf("Vecdist %f,%f,%f\n", vecDist.x, vecDist.y, vecDist.z); + printf("Explode %d Dist %d\n", i, dist); + mines[i].explode(); + player.setLife(player.getLifes() - 1); + //Buffer switching to prevent playing two sounds on one buffer + engine->audio.setADPCMVolume(65, Mine::lastSoundBuffer); + engine->audio.playADPCM(boomSound, Mine::lastSoundBuffer); + Mine::lastSoundBuffer = Mine::lastSoundBuffer == 4 ? 5 : 4; + } + } + if (player.mesh.position.y > WATER_LEVEL - 5) player.mesh.position.y = WATER_LEVEL - 5; @@ -172,7 +212,6 @@ void Dolphin::onUpdate() engine->renderer->draw(skybox); engine->renderer->draw(waterbox); engine->renderer->draw(seabed); - engine->renderer->draw(mine); for (u8 i = 0; i < OYSTERS_COUNT; i++) { if (oysters[i].isActive()) @@ -180,6 +219,11 @@ void Dolphin::onUpdate() engine->renderer->draw(oysters[i].mesh); } } + for (u8 i = 0; i < MINES_COUNT; i++) + { + if (mines[i].getExplosionTicks() < MINE_EXPLOSION_TICKS) + engine->renderer->draw(mines[i].mesh); + } for (u8 i = 0; i < player.getLifes(); i++) { engine->renderer->draw(lifeSprites[i]); diff --git a/src/samples/dolphin/dolphin.hpp b/src/samples/dolphin/dolphin.hpp index f48c149..ad7b3c1 100755 --- a/src/samples/dolphin/dolphin.hpp +++ b/src/samples/dolphin/dolphin.hpp @@ -21,6 +21,7 @@ #include "./camera.hpp" #include "./objects/player.hpp" #include "./objects/collectible.hpp" +#include "./objects/mine.hpp" class Dolphin : public Game { @@ -51,8 +52,11 @@ private: Mesh water; Mesh waterbox; Sprite lifeSprites[3]; + Sprite gameOver; audsrv_adpcm_t *underwaterAmbient, *surfaceAmbient; audsrv_adpcm_t *pickupSound; + audsrv_adpcm_t *boomSound; + Mine *mines; }; #endif diff --git a/src/samples/dolphin/objects/player.cpp b/src/samples/dolphin/objects/player.cpp index 7a8d70a..84669cc 100755 --- a/src/samples/dolphin/objects/player.cpp +++ b/src/samples/dolphin/objects/player.cpp @@ -23,7 +23,7 @@ Player::Player() mesh.shouldBeFrustumCulled = false; mesh.position.set(0.0F, 0.0F, 10.0f); mesh.rotation.x = -1.6F; - mesh.setAnimSpeed(0.05F); + mesh.setAnimSpeed(0.04F); bIsJumping = false; lifes = 3; } diff --git a/src/samples/dolphin/objects/player.hpp b/src/samples/dolphin/objects/player.hpp index 3c37938..3aed608 100755 --- a/src/samples/dolphin/objects/player.hpp +++ b/src/samples/dolphin/objects/player.hpp @@ -30,6 +30,7 @@ public: void update(Pad &t_pad); const u8 &isJumping() { return bIsJumping; } const u8 &getLifes() { return lifes; } + void setLife(const u8 &l) { lifes = lifes = l < 4 ? l : 3; } private: u8 bIsJumping; From 0c0512b37f972fab001cf1750545f4bf3fb2a64c Mon Sep 17 00:00:00 2001 From: Foxar Date: Thu, 10 Dec 2020 14:38:12 +0100 Subject: [PATCH 049/108] Added: Game over screen --- src/samples/dolphin/dolphin.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/samples/dolphin/dolphin.cpp b/src/samples/dolphin/dolphin.cpp index cf506a6..782b529 100755 --- a/src/samples/dolphin/dolphin.cpp +++ b/src/samples/dolphin/dolphin.cpp @@ -56,6 +56,11 @@ void Dolphin::onInit() island.shouldBeBackfaceCulled = true; island.shouldBeFrustumCulled = false; + gameOver.size.set(640.0F, 480.0F); + Texture *gameOverTex = texRepo->add("2d/", "gameover", PNG); + gameOver.setMode(MODE_STRETCH); + gameOverTex->addLink(gameOver.getId()); + printf("Loading water overlay...\n"); waterOverlay.size.set(640.0F, 480.0F); Texture *watOverlayTex = texRepo->add("2d/", "underwater_overlay", PNG); @@ -147,7 +152,14 @@ void Dolphin::onInit() void Dolphin::onUpdate() { - + if (player.getLifes() <= 0) + { + engine->audio.stopSong(); + engine->audio.setADPCMVolume(0, 0); + engine->audio.setADPCMVolume(75, 1); + engine->renderer->draw(gameOver); + return; + } Dolphin::engineFPS = engine->fps; if (engine->pad.isCrossClicked) printf("Delta multiplier: %f\n", 60.0F / engine->fps); From 5129d53b272c00dee7074b198d1fa8e2a22fd745 Mon Sep 17 00:00:00 2001 From: Foxar Date: Thu, 10 Dec 2020 16:32:01 +0100 Subject: [PATCH 050/108] Added: Missing mine class files. --- src/samples/dolphin/objects/mine.cpp | 35 ++++++++++++++++++++++++++++ src/samples/dolphin/objects/mine.hpp | 35 ++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 src/samples/dolphin/objects/mine.cpp create mode 100644 src/samples/dolphin/objects/mine.hpp diff --git a/src/samples/dolphin/objects/mine.cpp b/src/samples/dolphin/objects/mine.cpp new file mode 100644 index 0000000..aff4140 --- /dev/null +++ b/src/samples/dolphin/objects/mine.cpp @@ -0,0 +1,35 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Michał Mostowik +*/ + +#include "mine.hpp" + +u8 Mine::lastSoundBuffer = 4; + +Mine::Mine() +{ + //mesh.loadObj("mine/", "mine", 10.F, false); + mesh.position.set(0.0F, 0.0F, 0.0F); + + bExploding = false; + explosionTicks = 0; +} + +Mine::~Mine() +{ +} + +void Mine::update() +{ + if (bExploding && explosionTicks < MINE_EXPLOSION_TICKS) + { + explosionTicks++; + mesh.scale++; + } +} \ No newline at end of file diff --git a/src/samples/dolphin/objects/mine.hpp b/src/samples/dolphin/objects/mine.hpp new file mode 100644 index 0000000..5dce106 --- /dev/null +++ b/src/samples/dolphin/objects/mine.hpp @@ -0,0 +1,35 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Michał Mostowik +*/ + +#ifndef _MINE_ +#define _MINE_ + +#include +#include + +#define MINE_EXPLOSION_TICKS 10 + +class Mine +{ +public: + static u8 lastSoundBuffer; + Mesh mesh; + void explode() { bExploding = true; } + void update(); + Mine(); + ~Mine(); + const u8 &getExplosionTicks() { return explosionTicks; } + +private: + u8 bExploding; + u8 explosionTicks; +}; + +#endif From 97151ffc78d888df1ccc244ccaf9cce58fda6933 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Mostowik?= Date: Thu, 10 Dec 2020 17:37:44 +0100 Subject: [PATCH 051/108] PR Review, removing commented line --- src/samples/dolphin/objects/mine.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/samples/dolphin/objects/mine.cpp b/src/samples/dolphin/objects/mine.cpp index aff4140..9a7a19b 100644 --- a/src/samples/dolphin/objects/mine.cpp +++ b/src/samples/dolphin/objects/mine.cpp @@ -14,7 +14,6 @@ u8 Mine::lastSoundBuffer = 4; Mine::Mine() { - //mesh.loadObj("mine/", "mine", 10.F, false); mesh.position.set(0.0F, 0.0F, 0.0F); bExploding = false; @@ -32,4 +31,4 @@ void Mine::update() explosionTicks++; mesh.scale++; } -} \ No newline at end of file +} From 4bee938106fef599f7bc5ff191786e8ee9041d7f Mon Sep 17 00:00:00 2001 From: Foxar Date: Thu, 10 Dec 2020 17:49:53 +0100 Subject: [PATCH 052/108] PR Review: Memory de-allocation, misc. fixes. --- src/samples/dolphin/dolphin.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/samples/dolphin/dolphin.cpp b/src/samples/dolphin/dolphin.cpp index 782b529..6471834 100755 --- a/src/samples/dolphin/dolphin.cpp +++ b/src/samples/dolphin/dolphin.cpp @@ -25,7 +25,11 @@ Dolphin::Dolphin(Engine *t_engine) : engine(t_engine), camera(&engine->screen) mines = new Mine[MINES_COUNT]; } -Dolphin::~Dolphin() {} +Dolphin::~Dolphin() +{ + delete[] oysters; + delete[] mines; +} void Dolphin::onInit() { @@ -181,8 +185,8 @@ void Dolphin::onUpdate() { oysters[i].update(); Vector3 vecDist = oysters[i].mesh.position - player.mesh.position; - float dist = Math::sqrt(vecDist.x + vecDist.y + vecDist.z); - if (dist < 2.5F && player.isJumping() && oysters[i].isActive()) + float dist = vecDist.length(); + if (dist < 20.F && player.isJumping() && oysters[i].isActive()) { printf("Pickup %d Dist %d\n", i, dist); oysters[i].disappear(); @@ -196,9 +200,8 @@ void Dolphin::onUpdate() { mines[i].update(); Vector3 vecDist = mines[i].mesh.position - player.mesh.position; - float dist = Math::sqrt((vecDist.x * vecDist.x) + - (vecDist.y * vecDist.y) + - (vecDist.z * vecDist.z)); + float dist = vecDist.length(); + if (dist < 20.F && !mines[i].getExplosionTicks()) { printf("Vecdist %f,%f,%f\n", vecDist.x, vecDist.y, vecDist.z); From ac3601254554e6fe310a26645376a213f599878b Mon Sep 17 00:00:00 2001 From: h4570 Date: Sat, 12 Dec 2020 14:07:23 +0100 Subject: [PATCH 053/108] added some matrix/math funcs --- src/engine/include/models/math/matrix.hpp | 56 ++++++++++++++- src/engine/include/utils/math.hpp | 2 + src/engine/models/math/matrix.cpp | 76 +++++++++++++++++--- src/engine/utils/math.cpp | 84 +++++++++++++++++++++++ 4 files changed, 207 insertions(+), 11 deletions(-) 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/utils/math.hpp b/src/engine/include/utils/math.hpp index 4c5f115..92af50c 100644 --- a/src/engine/include/utils/math.hpp +++ b/src/engine/include/utils/math.hpp @@ -28,6 +28,8 @@ public: 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 float sqrt(float x); static float invSqrt(float x); 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/utils/math.cpp b/src/engine/utils/math.cpp index ddf960f..c273467 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); } + +static float 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; +} + +static float 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; +} From 8256efa7c8e4e5b206a820bcc0a9fb65a1625dde Mon Sep 17 00:00:00 2001 From: h4570 Date: Sat, 12 Dec 2020 14:10:50 +0100 Subject: [PATCH 054/108] switched zbuffer to 24bit mode --- src/engine/modules/renderer.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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!"); From 3dd51607f0eab05be370c8ef31c87b2fe444ba69 Mon Sep 17 00:00:00 2001 From: h4570 Date: Sat, 12 Dec 2020 14:12:30 +0100 Subject: [PATCH 055/108] refactored viewproj matrix calcs a bit --- src/engine/include/modules/vif_sender.hpp | 1 - src/engine/modules/vif_sender.cpp | 10 +++++----- 2 files changed, 5 insertions(+), 6 deletions(-) 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/modules/vif_sender.cpp b/src/engine/modules/vif_sender.cpp index 3135e09..771b05f 100644 --- a/src/engine/modules/vif_sender.cpp +++ b/src/engine/modules/vif_sender.cpp @@ -71,12 +71,12 @@ void VifSender::uploadMicroProgram() void VifSender::sendMatrices(const RenderData &t_renderData, const Vector3 &t_position, const Vector3 &t_rotation) { - vec3ToNative(position, t_position, 1.0F); - vec3ToNative(rotation, t_rotation, 1.0F); - create_local_world(localWorld, position, rotation); - create_local_screen(localScreen, localWorld, t_renderData.worldView->data, t_renderData.perspective->data); + 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_end_tag(matricesPacket); dma_channel_wait(DMA_CHANNEL_VIF1, 0); dma_channel_send_packet2(matricesPacket, DMA_CHANNEL_VIF1, 1); From b050e4643bcc26a07b4e7d488110283c4574b5aa Mon Sep 17 00:00:00 2001 From: h4570 Date: Sat, 12 Dec 2020 23:18:31 +0100 Subject: [PATCH 056/108] multiply hotfix! added scale, angle rotation --- src/engine/include/models/math/matrix.hpp | 4 +- src/engine/models/math/matrix.cpp | 167 ++++++++++++++-------- 2 files changed, 109 insertions(+), 62 deletions(-) diff --git a/src/engine/include/models/math/matrix.hpp b/src/engine/include/models/math/matrix.hpp index 5e13bd7..975486f 100644 --- a/src/engine/include/models/math/matrix.hpp +++ b/src/engine/include/models/math/matrix.hpp @@ -82,8 +82,10 @@ public: rotate(t_val); } - // + void rotationByAngle(const float &t_angle, const Vector3 &t_axis); + // + void setScale(const Vector3 &t_val); void setPerspective(ScreenSettings &screen); const void print() const; }; diff --git a/src/engine/models/math/matrix.cpp b/src/engine/models/math/matrix.cpp index ec927a1..3c1d330 100644 --- a/src/engine/models/math/matrix.cpp +++ b/src/engine/models/math/matrix.cpp @@ -84,6 +84,15 @@ void Matrix::identity() : "r"(this->data)); } +void Matrix::setScale(const Vector3 &t_val) +{ + this->identity(); + this->data[0] = t_val.x; + this->data[5] = t_val.y; + this->data[10] = t_val.z; + this->data[15] = 1.0F; +} + void Matrix::translate(const Vector3 &t_val) { this->data[12] += t_val.x; // 3,0 @@ -130,75 +139,79 @@ void Matrix::rotateZ(const float &t_radians) Matrix Matrix::operator*(const Matrix &t) { Matrix result; - 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" + asm __volatile__( + "lqc2 vf4, 0x00(%1) \n\t" + "lqc2 vf5, 0x10(%1) \n\t" + "lqc2 vf6, 0x20(%1) \n\t" + "lqc2 vf7, 0x30(%1) \n\t" + + "lqc2 vf8, 0x00(%2) \n\t" + "lqc2 vf9, 0x10(%2) \n\t" + "lqc2 vf10, 0x20(%2) \n\t" + "lqc2 vf11, 0x30(%2) \n\t" + + "vmulax.xyzw ACC, vf4, vf8 \n\t" + "vmadday.xyzw ACC, vf5, vf8 \n\t" + "vmaddaz.xyzw ACC, vf6, vf8 \n\t" + "vmaddw.xyzw vf12, vf7, vf8 \n\t" + "vmulax.xyzw ACC, vf4, vf9 \n\t" + "vmadday.xyzw ACC, vf5, vf9 \n\t" + "vmaddaz.xyzw ACC, vf6, vf9 \n\t" + "vmaddw.xyzw vf13, vf7, vf9 \n\t" + "vmulax.xyzw ACC, vf4, vf10 \n\t" + "vmadday.xyzw ACC, vf5, vf10 \n\t" + "vmaddaz.xyzw ACC, vf6, vf10 \n\t" + "vmaddw.xyzw vf14, vf7, vf10 \n\t" + "vmulax.xyzw ACC, vf4, vf11 \n\t" + "vmadday.xyzw ACC, vf5, vf11 \n\t" + "vmaddaz.xyzw ACC, vf6, vf11 \n\t" + "vmaddw.xyzw vf15, vf7, vf11 \n\t" + + "sqc2 vf12, 0x00(%0) \n\t" + "sqc2 vf13, 0x10(%0) \n\t" + "sqc2 vf14, 0x20(%0) \n\t" + "sqc2 vf15, 0x30(%0) \n\t" : - : "r"(result.data), "r"(this->data), "r"(t.data) - : "memory"); + : "r"(result.data), "r"(this->data), "r"(t.data)); 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" + "lqc2 vf4, 0x00(%1) \n\t" + "lqc2 vf5, 0x10(%1) \n\t" + "lqc2 vf6, 0x20(%1) \n\t" + "lqc2 vf7, 0x30(%1) \n\t" + + "lqc2 vf8, 0x00(%2) \n\t" + "lqc2 vf9, 0x10(%2) \n\t" + "lqc2 vf10, 0x20(%2) \n\t" + "lqc2 vf11, 0x30(%2) \n\t" + + "vmulax.xyzw ACC, vf4, vf8 \n\t" + "vmadday.xyzw ACC, vf5, vf8 \n\t" + "vmaddaz.xyzw ACC, vf6, vf8 \n\t" + "vmaddw.xyzw vf12, vf7, vf8 \n\t" + "vmulax.xyzw ACC, vf4, vf9 \n\t" + "vmadday.xyzw ACC, vf5, vf9 \n\t" + "vmaddaz.xyzw ACC, vf6, vf9 \n\t" + "vmaddw.xyzw vf13, vf7, vf9 \n\t" + "vmulax.xyzw ACC, vf4, vf10 \n\t" + "vmadday.xyzw ACC, vf5, vf10 \n\t" + "vmaddaz.xyzw ACC, vf6, vf10 \n\t" + "vmaddw.xyzw vf14, vf7, vf10 \n\t" + "vmulax.xyzw ACC, vf4, vf11 \n\t" + "vmadday.xyzw ACC, vf5, vf11 \n\t" + "vmaddaz.xyzw ACC, vf6, vf11 \n\t" + "vmaddw.xyzw vf15, vf7, vf11 \n\t" + + "sqc2 vf12, 0x00(%0) \n\t" + "sqc2 vf13, 0x10(%0) \n\t" + "sqc2 vf14, 0x20(%0) \n\t" + "sqc2 vf15, 0x30(%0) \n\t" : - : "r"(this->data), "r"(this->data), "r"(t.data) - : "memory"); + : "r"(this->data), "r"(this->data), "r"(t.data)); } /** Create empty matrix */ @@ -275,6 +288,38 @@ void Matrix::setPerspective(ScreenSettings &t_screen) this->data[15] = 0.0F; } +void Matrix::rotationByAngle(const float &t_angle, const Vector3 &t_axis) +{ + Vector3 localAxis = Vector3(t_axis); + localAxis.normalize(); + float x = localAxis.x; + float y = localAxis.y; + float z = localAxis.z; + + float c = Math::cos(t_angle); + float s = Math::sin(t_angle); + + this->data[0] = x * x * (1 - c) + c; + this->data[1] = y * x * (1 - c) + z * s; + this->data[2] = x * z * (1 - c) - y * s; + this->data[3] = 0.0F; + + this->data[4] = x * y * (1 - c) - z * s; + this->data[5] = y * y * (1 - c) + c; + this->data[6] = y * z * (1 - c) + x * s; + this->data[7] = 0.0F; + + this->data[8] = x * z * (1 - c) + y * s; + this->data[9] = y * z * (1 - c) - x * s; + this->data[10] = z * z * (1 - c) + c; + this->data[11] = 0.0F; + + this->data[12] = 0.0F; + this->data[13] = 0.0F; + this->data[14] = 0.0F; + this->data[15] = 1.0F; +} + /** Create a view matrix that transforms coordinates in * such a way that the user looks at a target vector * direction from a position vector. From f4e699025abe21a78bfea337b5dbd6a5bc31b9e0 Mon Sep 17 00:00:00 2001 From: h4570 Date: Sat, 12 Dec 2020 23:19:12 +0100 Subject: [PATCH 057/108] added useful funcs --- src/engine/include/utils/math.hpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/engine/include/utils/math.hpp b/src/engine/include/utils/math.hpp index 92af50c..3218047 100644 --- a/src/engine/include/utils/math.hpp +++ b/src/engine/include/utils/math.hpp @@ -24,15 +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(); From 3f4d2c55212eccbcc0d906649ff3208cd4970572 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20Sobczy=C5=84ski?= <32263891+h4570@users.noreply.github.com> Date: Sun, 13 Dec 2020 00:19:18 +0100 Subject: [PATCH 058/108] Revert "Urgent hotfix in matrix multiply, added few funcs" --- src/engine/include/models/math/matrix.hpp | 4 +- src/engine/models/math/matrix.cpp | 167 ++++++++-------------- 2 files changed, 62 insertions(+), 109 deletions(-) diff --git a/src/engine/include/models/math/matrix.hpp b/src/engine/include/models/math/matrix.hpp index 975486f..5e13bd7 100644 --- a/src/engine/include/models/math/matrix.hpp +++ b/src/engine/include/models/math/matrix.hpp @@ -82,10 +82,8 @@ public: rotate(t_val); } - void rotationByAngle(const float &t_angle, const Vector3 &t_axis); - // - void setScale(const Vector3 &t_val); + void setPerspective(ScreenSettings &screen); const void print() const; }; diff --git a/src/engine/models/math/matrix.cpp b/src/engine/models/math/matrix.cpp index 3c1d330..ec927a1 100644 --- a/src/engine/models/math/matrix.cpp +++ b/src/engine/models/math/matrix.cpp @@ -84,15 +84,6 @@ void Matrix::identity() : "r"(this->data)); } -void Matrix::setScale(const Vector3 &t_val) -{ - this->identity(); - this->data[0] = t_val.x; - this->data[5] = t_val.y; - this->data[10] = t_val.z; - this->data[15] = 1.0F; -} - void Matrix::translate(const Vector3 &t_val) { this->data[12] += t_val.x; // 3,0 @@ -139,79 +130,75 @@ void Matrix::rotateZ(const float &t_radians) Matrix Matrix::operator*(const Matrix &t) { Matrix result; - asm __volatile__( - "lqc2 vf4, 0x00(%1) \n\t" - "lqc2 vf5, 0x10(%1) \n\t" - "lqc2 vf6, 0x20(%1) \n\t" - "lqc2 vf7, 0x30(%1) \n\t" - - "lqc2 vf8, 0x00(%2) \n\t" - "lqc2 vf9, 0x10(%2) \n\t" - "lqc2 vf10, 0x20(%2) \n\t" - "lqc2 vf11, 0x30(%2) \n\t" - - "vmulax.xyzw ACC, vf4, vf8 \n\t" - "vmadday.xyzw ACC, vf5, vf8 \n\t" - "vmaddaz.xyzw ACC, vf6, vf8 \n\t" - "vmaddw.xyzw vf12, vf7, vf8 \n\t" - "vmulax.xyzw ACC, vf4, vf9 \n\t" - "vmadday.xyzw ACC, vf5, vf9 \n\t" - "vmaddaz.xyzw ACC, vf6, vf9 \n\t" - "vmaddw.xyzw vf13, vf7, vf9 \n\t" - "vmulax.xyzw ACC, vf4, vf10 \n\t" - "vmadday.xyzw ACC, vf5, vf10 \n\t" - "vmaddaz.xyzw ACC, vf6, vf10 \n\t" - "vmaddw.xyzw vf14, vf7, vf10 \n\t" - "vmulax.xyzw ACC, vf4, vf11 \n\t" - "vmadday.xyzw ACC, vf5, vf11 \n\t" - "vmaddaz.xyzw ACC, vf6, vf11 \n\t" - "vmaddw.xyzw vf15, vf7, vf11 \n\t" - - "sqc2 vf12, 0x00(%0) \n\t" - "sqc2 vf13, 0x10(%0) \n\t" - "sqc2 vf14, 0x20(%0) \n\t" - "sqc2 vf15, 0x30(%0) \n\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"(result.data), "r"(this->data), "r"(t.data)); + : "r"(result.data), "r"(this->data), "r"(t.data) + : "memory"); return result; } void Matrix::operator*=(const Matrix &t) { asm volatile( - "lqc2 vf4, 0x00(%1) \n\t" - "lqc2 vf5, 0x10(%1) \n\t" - "lqc2 vf6, 0x20(%1) \n\t" - "lqc2 vf7, 0x30(%1) \n\t" - - "lqc2 vf8, 0x00(%2) \n\t" - "lqc2 vf9, 0x10(%2) \n\t" - "lqc2 vf10, 0x20(%2) \n\t" - "lqc2 vf11, 0x30(%2) \n\t" - - "vmulax.xyzw ACC, vf4, vf8 \n\t" - "vmadday.xyzw ACC, vf5, vf8 \n\t" - "vmaddaz.xyzw ACC, vf6, vf8 \n\t" - "vmaddw.xyzw vf12, vf7, vf8 \n\t" - "vmulax.xyzw ACC, vf4, vf9 \n\t" - "vmadday.xyzw ACC, vf5, vf9 \n\t" - "vmaddaz.xyzw ACC, vf6, vf9 \n\t" - "vmaddw.xyzw vf13, vf7, vf9 \n\t" - "vmulax.xyzw ACC, vf4, vf10 \n\t" - "vmadday.xyzw ACC, vf5, vf10 \n\t" - "vmaddaz.xyzw ACC, vf6, vf10 \n\t" - "vmaddw.xyzw vf14, vf7, vf10 \n\t" - "vmulax.xyzw ACC, vf4, vf11 \n\t" - "vmadday.xyzw ACC, vf5, vf11 \n\t" - "vmaddaz.xyzw ACC, vf6, vf11 \n\t" - "vmaddw.xyzw vf15, vf7, vf11 \n\t" - - "sqc2 vf12, 0x00(%0) \n\t" - "sqc2 vf13, 0x10(%0) \n\t" - "sqc2 vf14, 0x20(%0) \n\t" - "sqc2 vf15, 0x30(%0) \n\t" + "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)); + : "r"(this->data), "r"(this->data), "r"(t.data) + : "memory"); } /** Create empty matrix */ @@ -288,38 +275,6 @@ void Matrix::setPerspective(ScreenSettings &t_screen) this->data[15] = 0.0F; } -void Matrix::rotationByAngle(const float &t_angle, const Vector3 &t_axis) -{ - Vector3 localAxis = Vector3(t_axis); - localAxis.normalize(); - float x = localAxis.x; - float y = localAxis.y; - float z = localAxis.z; - - float c = Math::cos(t_angle); - float s = Math::sin(t_angle); - - this->data[0] = x * x * (1 - c) + c; - this->data[1] = y * x * (1 - c) + z * s; - this->data[2] = x * z * (1 - c) - y * s; - this->data[3] = 0.0F; - - this->data[4] = x * y * (1 - c) - z * s; - this->data[5] = y * y * (1 - c) + c; - this->data[6] = y * z * (1 - c) + x * s; - this->data[7] = 0.0F; - - this->data[8] = x * z * (1 - c) + y * s; - this->data[9] = y * z * (1 - c) - x * s; - this->data[10] = z * z * (1 - c) + c; - this->data[11] = 0.0F; - - this->data[12] = 0.0F; - this->data[13] = 0.0F; - this->data[14] = 0.0F; - this->data[15] = 1.0F; -} - /** Create a view matrix that transforms coordinates in * such a way that the user looks at a target vector * direction from a position vector. From 4a1bca12aa0d1f4de4e2cf9018a89f4bd2086aba Mon Sep 17 00:00:00 2001 From: h4570 Date: Sun, 13 Dec 2020 00:23:04 +0100 Subject: [PATCH 059/108] small undo in rendering pipeline --- src/engine/modules/vif_sender.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/engine/modules/vif_sender.cpp b/src/engine/modules/vif_sender.cpp index 771b05f..2ef738a 100644 --- a/src/engine/modules/vif_sender.cpp +++ b/src/engine/modules/vif_sender.cpp @@ -71,12 +71,19 @@ void VifSender::uploadMicroProgram() void VifSender::sendMatrices(const RenderData &t_renderData, const Vector3 &t_position, const Vector3 &t_rotation) { - 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) + 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, &viewProj.data, 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); From ea6d5215d1b0da693ccd0c377c045fad7421a653 Mon Sep 17 00:00:00 2001 From: h4570 Date: Sun, 13 Dec 2020 00:39:32 +0100 Subject: [PATCH 060/108] fixed funcs declaration --- src/engine/utils/math.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/engine/utils/math.cpp b/src/engine/utils/math.cpp index c273467..5c4846e 100644 --- a/src/engine/utils/math.cpp +++ b/src/engine/utils/math.cpp @@ -100,7 +100,7 @@ void manyVec3ToNative(VECTOR *o_result, Vector3 *t_vec, int t_amount, float t_fo vec3ToNative(o_result[i], t_vec[i], t_fourthVal); } -static float asin(float x) +float Math::asin(float x) { float r; asm volatile( @@ -159,7 +159,7 @@ static float asin(float x) return r; } -static float mod(float x, float y) +float Math::mod(float x, float y) { /* * Portable fmod(x,y) implementation for systems From 12ef639516f0c6514e539f391b93eb32476bbbbf Mon Sep 17 00:00:00 2001 From: Foxar Date: Thu, 17 Dec 2020 13:33:22 +0100 Subject: [PATCH 061/108] Changed: Allowed obj_loader to load different .obj face element configurations. New authorised .obj face elements configurations: v, v/t, v/vt/vn, v//vn --- src/engine/loaders/obj_loader.cpp | 144 ++++++++++++++++++++++++++---- 1 file changed, 128 insertions(+), 16 deletions(-) diff --git a/src/engine/loaders/obj_loader.cpp b/src/engine/loaders/obj_loader.cpp index a200589..dcebc70 100644 --- a/src/engine/loaders/obj_loader.cpp +++ b/src/engine/loaders/obj_loader.cpp @@ -71,31 +71,143 @@ void ObjLoader::load(MeshFrame *o_result, char *t_filename, float t_scale, u8 t_ } else if (strcmp(lineHeader, "f") == 0) { - int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", - &vertexIndex[0], &coordIndex[0], &normalIndex[0], - &vertexIndex[1], &coordIndex[1], &normalIndex[1], - &vertexIndex[2], &coordIndex[2], &normalIndex[2]); - if (matches != 9) - PRINT_ERR(".obj can't be read by this simple parser. Try exporting with other options"); + + int* x; + fpos_t start; + fgetpos(file,&start); + int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n",x,x,x,x,x,x,x,x,x); + fsetpos(file,&start); + int newerMatches =0; + + switch(matches) + { + /* Vs, VTs and VNs all set */ + case 9: + { + printf("Case 9\n\n"); + fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", + &vertexIndex[0], &coordIndex[0], &normalIndex[0], + &vertexIndex[1], &coordIndex[1], &normalIndex[1], + &vertexIndex[2], &coordIndex[2], &normalIndex[2]); + + } + break; + /* Loaded only two digits (V, VT) succesfuly. Not setting VN. */ + case 2: + { + printf("Case 2\n"); + fscanf(file, "%d/%d/ %d/%d/ %d/%d/\n", + &vertexIndex[0], &coordIndex[0], + &vertexIndex[1], &coordIndex[1], + &vertexIndex[2], &coordIndex[2]); + + } + break; + /* Only V set. Checking for existance of VT or VN. */ + case 1: + { + printf("Case 1\n"); + /* Check for existance of V/// configuration.. */ + newerMatches = fscanf(file, "%d/// %d/// %d///\n",x,x,x); + fsetpos(file,&start); + if(newerMatches==3){ + /* Configuration confirmed. */ + fscanf(file, "%d/// %d/// %d///\n",&vertexIndex[0],&vertexIndex[1],&vertexIndex[2]); + + }else + { + /* Failed, checking configuration V//VN */ + newerMatches = fscanf(file, "%d//%d %d//%d %d//%d",x,x,x,x,x,x); + fsetpos(file,&start); + if(newerMatches==6){ + /* Configuration confirmed. */ + newerMatches = fscanf(file, "%d//%d %d//%d %d//%d", + &vertexIndex[0], &normalIndex[0], + &vertexIndex[1], &normalIndex[1], + &vertexIndex[2], &normalIndex[2]); + } + else{ + /*Unknown configuration.*/ + PRINT_ERR("Unknown .obj face for .obj file!"); + printf("Obj file mentioned: \"%s\"\n",path); + } + } + } + default: + PRINT_ERR("Unknown faces format in .obj file!"); + printf(" \"%s\"\n",path); + break; + + } +/* if(ferror(file)) + PRINT_ERR("Failed to load .obj file \"%s\"."); else { - o_result->getMaterial(materialsI).setVertexFace(faceI, vertexIndex[0] - 1); - o_result->getMaterial(materialsI).setVertexFace(faceI + 1, vertexIndex[1] - 1); - o_result->getMaterial(materialsI).setVertexFace(faceI + 2, vertexIndex[2] - 1); + int result=0; + char line [128]={'\0'}; + fgets(line,128,file); + + //if(fscanf - o_result->getMaterial(materialsI).setSTFace(faceI, coordIndex[0] - 1); - o_result->getMaterial(materialsI).setSTFace(faceI + 1, coordIndex[1] - 1); - o_result->getMaterial(materialsI).setSTFace(faceI + 2, coordIndex[2] - 1); + }*/ + switch(matches) + { + case 9: + { + o_result->getMaterial(materialsI).setVertexFace(faceI, vertexIndex[0] - 1); + o_result->getMaterial(materialsI).setVertexFace(faceI + 1, vertexIndex[1] - 1); + o_result->getMaterial(materialsI).setVertexFace(faceI + 2, vertexIndex[2] - 1); + + o_result->getMaterial(materialsI).setSTFace(faceI, coordIndex[0] - 1); + o_result->getMaterial(materialsI).setSTFace(faceI + 1, coordIndex[1] - 1); + o_result->getMaterial(materialsI).setSTFace(faceI + 2, coordIndex[2] - 1); + + o_result->getMaterial(materialsI).setNormalFace(faceI, normalIndex[0] - 1); + o_result->getMaterial(materialsI).setNormalFace(faceI + 1, normalIndex[1] - 1); + o_result->getMaterial(materialsI).setNormalFace(faceI + 2, normalIndex[2] - 1); + faceI += 3; + break; + } + case 2: + { + o_result->getMaterial(materialsI).setVertexFace(faceI, vertexIndex[0] - 1); + o_result->getMaterial(materialsI).setVertexFace(faceI + 1, vertexIndex[1] - 1); + o_result->getMaterial(materialsI).setVertexFace(faceI + 2, vertexIndex[2] - 1); + + o_result->getMaterial(materialsI).setSTFace(faceI, coordIndex[0] - 1); + o_result->getMaterial(materialsI).setSTFace(faceI + 1, coordIndex[1] - 1); + o_result->getMaterial(materialsI).setSTFace(faceI + 2, coordIndex[2] - 1); + + break; + } + case 1: + { + if(newerMatches==3) + { + o_result->getMaterial(materialsI).setVertexFace(faceI, vertexIndex[0] - 1); + o_result->getMaterial(materialsI).setVertexFace(faceI + 1, vertexIndex[1] - 1); + o_result->getMaterial(materialsI).setVertexFace(faceI + 2, vertexIndex[2] - 1); + } + else if(newerMatches==6) + { + o_result->getMaterial(materialsI).setVertexFace(faceI, vertexIndex[0] - 1); + o_result->getMaterial(materialsI).setVertexFace(faceI + 1, vertexIndex[1] - 1); + o_result->getMaterial(materialsI).setVertexFace(faceI + 2, vertexIndex[2] - 1); + + o_result->getMaterial(materialsI).setNormalFace(faceI, normalIndex[0] - 1); + o_result->getMaterial(materialsI).setNormalFace(faceI + 1, normalIndex[1] - 1); + o_result->getMaterial(materialsI).setNormalFace(faceI + 2, normalIndex[2] - 1); + + } + break; + } - o_result->getMaterial(materialsI).setNormalFace(faceI, normalIndex[0] - 1); - o_result->getMaterial(materialsI).setNormalFace(faceI + 1, normalIndex[1] - 1); - o_result->getMaterial(materialsI).setNormalFace(faceI + 2, normalIndex[2] - 1); - faceI += 3; } } } else break; + } o_result->calculateBoundingBoxes(); fclose(file); From d4f2a1e1a224fc5e3f9015afac8a857ccb8bb6d5 Mon Sep 17 00:00:00 2001 From: Foxar Date: Thu, 17 Dec 2020 14:19:23 +0100 Subject: [PATCH 062/108] Restored: Undid the objloader changes, they should be in its own branch. --- src/engine/loaders/obj_loader.cpp | 144 ++++-------------------------- 1 file changed, 16 insertions(+), 128 deletions(-) diff --git a/src/engine/loaders/obj_loader.cpp b/src/engine/loaders/obj_loader.cpp index dcebc70..a200589 100644 --- a/src/engine/loaders/obj_loader.cpp +++ b/src/engine/loaders/obj_loader.cpp @@ -71,143 +71,31 @@ void ObjLoader::load(MeshFrame *o_result, char *t_filename, float t_scale, u8 t_ } else if (strcmp(lineHeader, "f") == 0) { - - int* x; - fpos_t start; - fgetpos(file,&start); - int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n",x,x,x,x,x,x,x,x,x); - fsetpos(file,&start); - int newerMatches =0; - - switch(matches) - { - /* Vs, VTs and VNs all set */ - case 9: - { - printf("Case 9\n\n"); - fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", - &vertexIndex[0], &coordIndex[0], &normalIndex[0], - &vertexIndex[1], &coordIndex[1], &normalIndex[1], - &vertexIndex[2], &coordIndex[2], &normalIndex[2]); - - } - break; - /* Loaded only two digits (V, VT) succesfuly. Not setting VN. */ - case 2: - { - printf("Case 2\n"); - fscanf(file, "%d/%d/ %d/%d/ %d/%d/\n", - &vertexIndex[0], &coordIndex[0], - &vertexIndex[1], &coordIndex[1], - &vertexIndex[2], &coordIndex[2]); - - } - break; - /* Only V set. Checking for existance of VT or VN. */ - case 1: - { - printf("Case 1\n"); - /* Check for existance of V/// configuration.. */ - newerMatches = fscanf(file, "%d/// %d/// %d///\n",x,x,x); - fsetpos(file,&start); - if(newerMatches==3){ - /* Configuration confirmed. */ - fscanf(file, "%d/// %d/// %d///\n",&vertexIndex[0],&vertexIndex[1],&vertexIndex[2]); - - }else - { - /* Failed, checking configuration V//VN */ - newerMatches = fscanf(file, "%d//%d %d//%d %d//%d",x,x,x,x,x,x); - fsetpos(file,&start); - if(newerMatches==6){ - /* Configuration confirmed. */ - newerMatches = fscanf(file, "%d//%d %d//%d %d//%d", - &vertexIndex[0], &normalIndex[0], - &vertexIndex[1], &normalIndex[1], - &vertexIndex[2], &normalIndex[2]); - } - else{ - /*Unknown configuration.*/ - PRINT_ERR("Unknown .obj face for .obj file!"); - printf("Obj file mentioned: \"%s\"\n",path); - } - } - } - default: - PRINT_ERR("Unknown faces format in .obj file!"); - printf(" \"%s\"\n",path); - break; - - } -/* if(ferror(file)) - PRINT_ERR("Failed to load .obj file \"%s\"."); + int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", + &vertexIndex[0], &coordIndex[0], &normalIndex[0], + &vertexIndex[1], &coordIndex[1], &normalIndex[1], + &vertexIndex[2], &coordIndex[2], &normalIndex[2]); + if (matches != 9) + PRINT_ERR(".obj can't be read by this simple parser. Try exporting with other options"); else { - int result=0; - char line [128]={'\0'}; - fgets(line,128,file); - - //if(fscanf + o_result->getMaterial(materialsI).setVertexFace(faceI, vertexIndex[0] - 1); + o_result->getMaterial(materialsI).setVertexFace(faceI + 1, vertexIndex[1] - 1); + o_result->getMaterial(materialsI).setVertexFace(faceI + 2, vertexIndex[2] - 1); - }*/ - switch(matches) - { - case 9: - { - o_result->getMaterial(materialsI).setVertexFace(faceI, vertexIndex[0] - 1); - o_result->getMaterial(materialsI).setVertexFace(faceI + 1, vertexIndex[1] - 1); - o_result->getMaterial(materialsI).setVertexFace(faceI + 2, vertexIndex[2] - 1); - - o_result->getMaterial(materialsI).setSTFace(faceI, coordIndex[0] - 1); - o_result->getMaterial(materialsI).setSTFace(faceI + 1, coordIndex[1] - 1); - o_result->getMaterial(materialsI).setSTFace(faceI + 2, coordIndex[2] - 1); - - o_result->getMaterial(materialsI).setNormalFace(faceI, normalIndex[0] - 1); - o_result->getMaterial(materialsI).setNormalFace(faceI + 1, normalIndex[1] - 1); - o_result->getMaterial(materialsI).setNormalFace(faceI + 2, normalIndex[2] - 1); - faceI += 3; - break; - } - case 2: - { - o_result->getMaterial(materialsI).setVertexFace(faceI, vertexIndex[0] - 1); - o_result->getMaterial(materialsI).setVertexFace(faceI + 1, vertexIndex[1] - 1); - o_result->getMaterial(materialsI).setVertexFace(faceI + 2, vertexIndex[2] - 1); - - o_result->getMaterial(materialsI).setSTFace(faceI, coordIndex[0] - 1); - o_result->getMaterial(materialsI).setSTFace(faceI + 1, coordIndex[1] - 1); - o_result->getMaterial(materialsI).setSTFace(faceI + 2, coordIndex[2] - 1); - - break; - } - case 1: - { - if(newerMatches==3) - { - o_result->getMaterial(materialsI).setVertexFace(faceI, vertexIndex[0] - 1); - o_result->getMaterial(materialsI).setVertexFace(faceI + 1, vertexIndex[1] - 1); - o_result->getMaterial(materialsI).setVertexFace(faceI + 2, vertexIndex[2] - 1); - } - else if(newerMatches==6) - { - o_result->getMaterial(materialsI).setVertexFace(faceI, vertexIndex[0] - 1); - o_result->getMaterial(materialsI).setVertexFace(faceI + 1, vertexIndex[1] - 1); - o_result->getMaterial(materialsI).setVertexFace(faceI + 2, vertexIndex[2] - 1); - - o_result->getMaterial(materialsI).setNormalFace(faceI, normalIndex[0] - 1); - o_result->getMaterial(materialsI).setNormalFace(faceI + 1, normalIndex[1] - 1); - o_result->getMaterial(materialsI).setNormalFace(faceI + 2, normalIndex[2] - 1); - - } - break; - } + o_result->getMaterial(materialsI).setSTFace(faceI, coordIndex[0] - 1); + o_result->getMaterial(materialsI).setSTFace(faceI + 1, coordIndex[1] - 1); + o_result->getMaterial(materialsI).setSTFace(faceI + 2, coordIndex[2] - 1); + o_result->getMaterial(materialsI).setNormalFace(faceI, normalIndex[0] - 1); + o_result->getMaterial(materialsI).setNormalFace(faceI + 1, normalIndex[1] - 1); + o_result->getMaterial(materialsI).setNormalFace(faceI + 2, normalIndex[2] - 1); + faceI += 3; } } } else break; - } o_result->calculateBoundingBoxes(); fclose(file); From 44776f7abad3834ee43a5f7003064d7af12550b5 Mon Sep 17 00:00:00 2001 From: Foxar Date: Sun, 20 Dec 2020 13:13:03 +0100 Subject: [PATCH 063/108] Added: Incomplete implenetation of BoundingBox class and BoundingBoxFace struct. --- src/engine/include/models/bounding_box.hpp | 69 ++++++++++++++++++++++ src/engine/models/bounding_box.cpp | 33 +++++++++++ 2 files changed, 102 insertions(+) create mode 100644 src/engine/include/models/bounding_box.hpp create mode 100644 src/engine/models/bounding_box.cpp diff --git a/src/engine/include/models/bounding_box.hpp b/src/engine/include/models/bounding_box.hpp new file mode 100644 index 0000000..98a9128 --- /dev/null +++ b/src/engine/include/models/bounding_box.hpp @@ -0,0 +1,69 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Michał Mostowik +*/ + +#ifndef _TYRA_BOUNDING_BOX_ +#define _TYRA_BOUNDING_BOX_ + +#include +#include +#include "math/point.hpp" +#include "math/vector3.hpp" + +/** + * Struct containing face data used in + * bounding box class. + */ +struct BoundingBoxFace +{ + BoundingBoxFace(Vector3 t_minCorner, Vector3 t_maxCorner, float t_axisPos) + { + minCorner = t_minCorner; + maxCorner = t_maxCorner; + axisPosition = t_axisPos; + } + /** Position of the face on it's respective axis. */ + float axisPosition; + /** Corner of the face with lower coordinates */ + Vector3 minCorner; + /** Corner of the face with higher coordinates */ + Vector3 maxCorner; +}; + +/** + * Class containing bounding box data + * which can be used for collision detection. + */ + +class BoundingBox +{ +public: + BoundingBox(Vector3 *t_vertices); + ~BoundingBox(); + const float &getHeight() { return _height; }; + const float &getDepth() { return _depth; }; + const float &getWidth() { return _width; }; + /** Return the vector directly in middle of the bounding box. */ + const Vector3 &getCenter() { return _centerVector; }; + /** Return the front face (furhter on z-axis) */ + const BoundingBoxFace &getFrontFace() { return _frontFace; }; + +private: + Vector3 _vertices[8]; + float _height, _depth, _width; + Vector3 _centerVector; + BoundingBoxFace _frontFace; + BoundingBoxFace _backFace; + BoundingBoxFace _leftFace; + BoundingBoxFace _rightFace; + BoundingBoxFace _topFace; + BoundingBoxFace _bottomFace; +}; + +#endif \ No newline at end of file diff --git a/src/engine/models/bounding_box.cpp b/src/engine/models/bounding_box.cpp new file mode 100644 index 0000000..e16f04e --- /dev/null +++ b/src/engine/models/bounding_box.cpp @@ -0,0 +1,33 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Michał Mostowik +*/ + +#include "../include/models/bounding_box.hpp" +#include "../include/utils/debug.hpp" + +BoundingBox::BoundingBox(Vector3 *t_vectorArray) +{ + if (sizeof(t_vectorArray) / sizeof(Vector3) != 8) + PRINT_ERR("Attempted to create bounding box with invalid count of vectors!"); + + //Perform a deep copy of vertex array parameter + for (int i = 0; i < 9; i++) + _vertices[i] = t_vectorArray[i]; + + //This might be shortened with Vector3 operator overloading, but current + //implementation is more human readable. + _height = _vertices[0].y - _vertices[2].y; + _width = _vertices[0].x - _vertices[4].x; + _depth = _vertices[0].z - _vertices[1].z; + + _centerVector = _vertices[0]; + _centerVector.x += (_width / 2); + _centerVector.y += (_height / 2); + _centerVector.z += (_depth / 2); +} \ No newline at end of file From 9b2d39505b9a35c0fd2b94d96eeab970c93f8095 Mon Sep 17 00:00:00 2001 From: Foxar Date: Sun, 20 Dec 2020 13:24:50 +0100 Subject: [PATCH 064/108] Added: BoundingBoxFaces assignments, calculations and doxygen comments. --- src/engine/include/models/bounding_box.hpp | 20 ++++++++++++++++++-- src/engine/models/bounding_box.cpp | 10 ++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/engine/include/models/bounding_box.hpp b/src/engine/include/models/bounding_box.hpp index 98a9128..b95244c 100644 --- a/src/engine/include/models/bounding_box.hpp +++ b/src/engine/include/models/bounding_box.hpp @@ -22,7 +22,7 @@ */ struct BoundingBoxFace { - BoundingBoxFace(Vector3 t_minCorner, Vector3 t_maxCorner, float t_axisPos) + BoundingBoxFace(const Vector3 &t_minCorner, const Vector3 &t_maxCorner, float t_axisPos) { minCorner = t_minCorner; maxCorner = t_maxCorner; @@ -51,18 +51,34 @@ public: const float &getWidth() { return _width; }; /** Return the vector directly in middle of the bounding box. */ const Vector3 &getCenter() { return _centerVector; }; - /** Return the front face (furhter on z-axis) */ + /** Return the front face (further on z-axis) */ const BoundingBoxFace &getFrontFace() { return _frontFace; }; + /** Return the back face (nearer on z-axis) */ + const BoundingBoxFace &getBackFace() { return _backFace; }; + /** Return the left face (further on x-axis) */ + const BoundingBoxFace &getLeftFace() { return _leftFace; }; + /** Return the right face (nearer on x-axis) */ + const BoundingBoxFace &getRightFace() { return _rightFace; }; + /** Return the top face (further on y-axis) */ + const BoundingBoxFace &getTopFace() { return _frontFace; }; + /** Return the bottom face (nearer on y-axis) */ + const BoundingBoxFace &getBottomFace() { return _bottomFace; }; private: Vector3 _vertices[8]; float _height, _depth, _width; Vector3 _centerVector; + /** Front face (further on z-axis) */ BoundingBoxFace _frontFace; + /** Back face (nearer on z-axis) */ BoundingBoxFace _backFace; + /** Left face (nearer on x-axis) */ BoundingBoxFace _leftFace; + /** Right face (further on x-axis) */ BoundingBoxFace _rightFace; + /** Top face (further on y-axis) */ BoundingBoxFace _topFace; + /** Bottom face (nearer on y-axis) */ BoundingBoxFace _bottomFace; }; diff --git a/src/engine/models/bounding_box.cpp b/src/engine/models/bounding_box.cpp index e16f04e..9191021 100644 --- a/src/engine/models/bounding_box.cpp +++ b/src/engine/models/bounding_box.cpp @@ -30,4 +30,14 @@ BoundingBox::BoundingBox(Vector3 *t_vectorArray) _centerVector.x += (_width / 2); _centerVector.y += (_height / 2); _centerVector.z += (_depth / 2); + + //Z-Axis faces + _frontFace = BoundingBoxFace(_vertices[1], _vertices[7], _vertices[1].z); + _backFace = BoundingBoxFace(_vertices[0], _vertices[6], _vertices[0].z); + //X-Axis faces + _leftFace = BoundingBoxFace(_vertices[0], _vertices[3], _vertices[0].x); + _rightFace = BoundingBoxFace(_vertices[4], _vertices[7], _vertices[4].x); + //Y-Axis faces + _topFace = BoundingBoxFace(_vertices[2], _vertices[7], _vertices[2].y); + _bottomFace = BoundingBoxFace(_vertices[0], _vertices[5], _vertices[0].y); } \ No newline at end of file From 53c134b0d526907710c03e4ee9282e4ffdf06bbc Mon Sep 17 00:00:00 2001 From: Foxar Date: Sun, 20 Dec 2020 13:38:26 +0100 Subject: [PATCH 065/108] Added: Integrated new BoundingBox class implentation with mesh_frame boundingbox calculations. --- src/engine/include/models/mesh_frame.hpp | 3 +++ src/engine/models/mesh_frame.cpp | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/src/engine/include/models/mesh_frame.hpp b/src/engine/include/models/mesh_frame.hpp index 99ec249..f57b581 100644 --- a/src/engine/include/models/mesh_frame.hpp +++ b/src/engine/include/models/mesh_frame.hpp @@ -13,6 +13,7 @@ #include #include +#include "bounding_box.hpp" #include "math/point.hpp" #include "math/vector3.hpp" #include "./mesh_material.hpp" @@ -139,7 +140,9 @@ public: void calculateBoundingBoxes(); private: + /** Deprecated. It is advised to use BoundingBox class instead. */ Vector3 boundingBox[8]; + BoundingBox *boundingBoxObj; u8 _areSTsAllocated, _areVerticesAllocated, _areNormalsAllocated, diff --git a/src/engine/models/mesh_frame.cpp b/src/engine/models/mesh_frame.cpp index ff9883d..2180a57 100644 --- a/src/engine/models/mesh_frame.cpp +++ b/src/engine/models/mesh_frame.cpp @@ -37,6 +37,7 @@ MeshFrame::~MeshFrame() delete[] normals; if (_areMaterialsAllocated) delete[] materials; + delete boundingBoxObj; } // ---- @@ -121,6 +122,7 @@ void MeshFrame::calculateBoundingBoxes() if (hiZ < vertices[i].z) hiZ = vertices[i].z; } + boundingBox[0].set(lowX, lowY, lowZ); boundingBox[1].set(lowX, lowY, hiZ); boundingBox[2].set(lowX, hiY, lowZ); @@ -131,4 +133,12 @@ void MeshFrame::calculateBoundingBoxes() boundingBox[6].set(hiX, hiY, lowZ); boundingBox[7].set(hiX, hiY, hiZ); _isBoundingBoxCalculated = true; + + //Function temporarily is a hybrid between old Vector3[8] boundingbox and + //the new class implementation. Upon confirmation that new implementation + //works correctly, delete the old code. + + //BoundingBox is declared on the heap to prevent any ill-formed default + //constructor instantiated BoundingBox objects. + boundingBoxObj = new BoundingBox(boundingBox); } From 596bf2d89a517cd0e8a7277d9e1c76e8730d6137 Mon Sep 17 00:00:00 2001 From: Foxar Date: Sun, 20 Dec 2020 13:44:48 +0100 Subject: [PATCH 066/108] Added: Mesh_frame getter for BoundingBox object pointer. --- src/engine/include/models/mesh_frame.hpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/engine/include/models/mesh_frame.hpp b/src/engine/include/models/mesh_frame.hpp index f57b581..1630078 100644 --- a/src/engine/include/models/mesh_frame.hpp +++ b/src/engine/include/models/mesh_frame.hpp @@ -88,6 +88,11 @@ public: */ Vector3 &getBoundingBoxVertex(const u8 &i) { return boundingBox[i]; }; + /** + * Returns bounding box (AABB) object pointer. + */ + BoundingBox *getBoundingBoxP() { return boundingBoxObj; }; + // ---- // Setters // ---- From 11a4e2ee7652e26a4dc25cd78af0a6c0fe62f5de Mon Sep 17 00:00:00 2001 From: Foxar Date: Sun, 20 Dec 2020 14:11:50 +0100 Subject: [PATCH 067/108] Changed: Updated makefile to include new boundingboxes. --- src/engine/Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/src/engine/Makefile b/src/engine/Makefile index 4164e49..78c621f 100644 --- a/src/engine/Makefile +++ b/src/engine/Makefile @@ -6,6 +6,7 @@ EE_OBJS = \ models/math/point.o \ models/math/vector3.o \ models/mesh_frame.o \ + models/bounding_box.o \ models/mesh_material.o \ models/mesh.o \ models/sprite.o \ From 55d8e445db27a6a5294ef1d6cff5cff367e1b6d4 Mon Sep 17 00:00:00 2001 From: Foxar Date: Sun, 20 Dec 2020 14:12:52 +0100 Subject: [PATCH 068/108] Fixed: Typo in bounding box getters. --- src/engine/include/models/bounding_box.hpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/engine/include/models/bounding_box.hpp b/src/engine/include/models/bounding_box.hpp index b95244c..1e7b364 100644 --- a/src/engine/include/models/bounding_box.hpp +++ b/src/engine/include/models/bounding_box.hpp @@ -22,6 +22,9 @@ */ struct BoundingBoxFace { + BoundingBoxFace(){ + + }; BoundingBoxFace(const Vector3 &t_minCorner, const Vector3 &t_maxCorner, float t_axisPos) { minCorner = t_minCorner; @@ -60,7 +63,7 @@ public: /** Return the right face (nearer on x-axis) */ const BoundingBoxFace &getRightFace() { return _rightFace; }; /** Return the top face (further on y-axis) */ - const BoundingBoxFace &getTopFace() { return _frontFace; }; + const BoundingBoxFace &getTopFace() { return _topFace; }; /** Return the bottom face (nearer on y-axis) */ const BoundingBoxFace &getBottomFace() { return _bottomFace; }; From 71ce30a8fca3852fcafa398a0bb180f8af5e6fd8 Mon Sep 17 00:00:00 2001 From: Foxar Date: Sun, 20 Dec 2020 14:13:47 +0100 Subject: [PATCH 069/108] Added: Destructor for boundingbox class. --- src/engine/models/bounding_box.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/engine/models/bounding_box.cpp b/src/engine/models/bounding_box.cpp index 9191021..ec487b2 100644 --- a/src/engine/models/bounding_box.cpp +++ b/src/engine/models/bounding_box.cpp @@ -13,9 +13,6 @@ BoundingBox::BoundingBox(Vector3 *t_vectorArray) { - if (sizeof(t_vectorArray) / sizeof(Vector3) != 8) - PRINT_ERR("Attempted to create bounding box with invalid count of vectors!"); - //Perform a deep copy of vertex array parameter for (int i = 0; i < 9; i++) _vertices[i] = t_vectorArray[i]; @@ -40,4 +37,8 @@ BoundingBox::BoundingBox(Vector3 *t_vectorArray) //Y-Axis faces _topFace = BoundingBoxFace(_vertices[2], _vertices[7], _vertices[2].y); _bottomFace = BoundingBoxFace(_vertices[0], _vertices[5], _vertices[0].y); +} + +BoundingBox::~BoundingBox() +{ } \ No newline at end of file From ae15906c9928d8ea2f854304b844c605ec314a97 Mon Sep 17 00:00:00 2001 From: Foxar Date: Mon, 21 Dec 2020 18:30:18 +0100 Subject: [PATCH 070/108] Added: Accessors in bounding_box class allowing access to vertices array. --- src/engine/include/models/bounding_box.hpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/engine/include/models/bounding_box.hpp b/src/engine/include/models/bounding_box.hpp index 1e7b364..e2aaccc 100644 --- a/src/engine/include/models/bounding_box.hpp +++ b/src/engine/include/models/bounding_box.hpp @@ -66,6 +66,13 @@ public: const BoundingBoxFace &getTopFace() { return _topFace; }; /** Return the bottom face (nearer on y-axis) */ const BoundingBoxFace &getBottomFace() { return _bottomFace; }; + /** + * Returns bounding box raw vertices array. + * Total length: 8 + */ + Vector3 &getVertex(const u8 &i) { return _vertices[i]; }; + /** Returns single vertex from raw vertices array.*/ + Vector3 *getVertices() { return _vertices; }; private: Vector3 _vertices[8]; From 974138d64eb9514f501b9f1fb8316c985fc53985 Mon Sep 17 00:00:00 2001 From: Foxar Date: Mon, 21 Dec 2020 18:31:23 +0100 Subject: [PATCH 071/108] Changed: Replaced old getBoundingBox and getBoundingBoxVertex functions with new ones, using the new bounding_box class infrastructure. --- src/engine/include/models/mesh_frame.hpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/engine/include/models/mesh_frame.hpp b/src/engine/include/models/mesh_frame.hpp index 1630078..e3e2466 100644 --- a/src/engine/include/models/mesh_frame.hpp +++ b/src/engine/include/models/mesh_frame.hpp @@ -80,13 +80,15 @@ public: * Returns bounding box (AABB). * Total length: 8 */ - Vector3 *getBoundingBox() { return boundingBox; }; + //Vector3 *getBoundingBox() { return boundingBox; }; + Vector3 *getBoundingBox() { return boundingBoxObj->getVertices(); }; /** * Returns bounding box (AABB) vertex. * Total length: 8 */ - Vector3 &getBoundingBoxVertex(const u8 &i) { return boundingBox[i]; }; + //Vector3 &getBoundingBoxVertex(const u8 &i) { return boundingBox[i]; }; + Vector3 &getBoundingBoxVertex(const u8 &i) { return boundingBoxObj->getVertex(i); }; /** * Returns bounding box (AABB) object pointer. From d835f83e517f2b5a9b33aff5e04360c702bcafc4 Mon Sep 17 00:00:00 2001 From: Foxar Date: Mon, 21 Dec 2020 18:48:11 +0100 Subject: [PATCH 072/108] Added: New BoundingBox infrastructure into mesh_material, analogous to mesh_frame. --- src/engine/include/models/mesh_material.hpp | 13 +++++++++++-- src/engine/models/mesh_material.cpp | 2 ++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/engine/include/models/mesh_material.hpp b/src/engine/include/models/mesh_material.hpp index 0b5447e..976a32b 100644 --- a/src/engine/include/models/mesh_material.hpp +++ b/src/engine/include/models/mesh_material.hpp @@ -12,6 +12,7 @@ #define _TYRA_MESH_MATERIAL_ #include +#include "bounding_box.hpp" #include "./math/vector3.hpp" #include "./math/plane.hpp" @@ -65,13 +66,20 @@ public: * Returns bounding box (AABB). * Total length: 8 */ - Vector3 *getBoundingBox() { return boundingBox; }; + //Vector3 *getBoundingBox() { return boundingBox; }; + Vector3 *getBoundingBox() { return boundingBoxObj->getVertices(); }; /** * Returns bounding box (AABB) vertex. * Total length: 8 */ - Vector3 &getBoundingBoxVertex(const u8 &i) { return boundingBox[i]; }; + //Vector3 &getBoundingBoxVertex(const u8 &i) { return boundingBox[i]; }; + Vector3 &getBoundingBoxVertex(const u8 &i) { return boundingBoxObj->getVertex(i); }; + + /** + * Returns bounding box (AABB) object pointer. + */ + BoundingBox *getBoundingBoxP() { return boundingBoxObj; }; // ---- // Setters @@ -120,6 +128,7 @@ public: private: Vector3 boundingBox[8]; + BoundingBox *boundingBoxObj; u32 facesCount, id; u32 *vertexFaces, *stFaces, *normalFaces; u8 _isNameSet, _areFacesAllocated, _isBoundingBoxCalculated; diff --git a/src/engine/models/mesh_material.cpp b/src/engine/models/mesh_material.cpp index f435ce6..bf4667e 100644 --- a/src/engine/models/mesh_material.cpp +++ b/src/engine/models/mesh_material.cpp @@ -9,6 +9,7 @@ */ #include "../include/models/mesh_material.hpp" +#include "../include/models/bounding_box.hpp" #include "../include/utils/debug.hpp" #include "../include/utils/string.hpp" #include @@ -132,4 +133,5 @@ void MeshMaterial::calculateBoundingBox(Vector3 *t_vertices, u32 t_vertCount) boundingBox[6].set(hiX, hiY, lowZ); boundingBox[7].set(hiX, hiY, hiZ); _isBoundingBoxCalculated = true; + boundingBoxObj = new BoundingBox(boundingBox); } From 1765885bec0fb94668995ad5530c2053dcee57a4 Mon Sep 17 00:00:00 2001 From: Foxar Date: Mon, 21 Dec 2020 18:57:01 +0100 Subject: [PATCH 073/108] Removed: Commented-out ('disabled') method declarations. --- src/engine/include/models/mesh_frame.hpp | 2 -- src/engine/include/models/mesh_material.hpp | 2 -- 2 files changed, 4 deletions(-) diff --git a/src/engine/include/models/mesh_frame.hpp b/src/engine/include/models/mesh_frame.hpp index e3e2466..8ffe8e0 100644 --- a/src/engine/include/models/mesh_frame.hpp +++ b/src/engine/include/models/mesh_frame.hpp @@ -80,14 +80,12 @@ public: * Returns bounding box (AABB). * Total length: 8 */ - //Vector3 *getBoundingBox() { return boundingBox; }; Vector3 *getBoundingBox() { return boundingBoxObj->getVertices(); }; /** * Returns bounding box (AABB) vertex. * Total length: 8 */ - //Vector3 &getBoundingBoxVertex(const u8 &i) { return boundingBox[i]; }; Vector3 &getBoundingBoxVertex(const u8 &i) { return boundingBoxObj->getVertex(i); }; /** diff --git a/src/engine/include/models/mesh_material.hpp b/src/engine/include/models/mesh_material.hpp index 976a32b..dffcf8d 100644 --- a/src/engine/include/models/mesh_material.hpp +++ b/src/engine/include/models/mesh_material.hpp @@ -66,14 +66,12 @@ public: * Returns bounding box (AABB). * Total length: 8 */ - //Vector3 *getBoundingBox() { return boundingBox; }; Vector3 *getBoundingBox() { return boundingBoxObj->getVertices(); }; /** * Returns bounding box (AABB) vertex. * Total length: 8 */ - //Vector3 &getBoundingBoxVertex(const u8 &i) { return boundingBox[i]; }; Vector3 &getBoundingBoxVertex(const u8 &i) { return boundingBoxObj->getVertex(i); }; /** From 1cc1e62ed6c9baab88620bca778ef07fec3528e7 Mon Sep 17 00:00:00 2001 From: Foxar Date: Mon, 21 Dec 2020 19:03:40 +0100 Subject: [PATCH 074/108] Fixed: Fixed incorrect vertex array for-loop iteration. --- src/engine/models/bounding_box.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/engine/models/bounding_box.cpp b/src/engine/models/bounding_box.cpp index ec487b2..271632f 100644 --- a/src/engine/models/bounding_box.cpp +++ b/src/engine/models/bounding_box.cpp @@ -14,7 +14,7 @@ BoundingBox::BoundingBox(Vector3 *t_vectorArray) { //Perform a deep copy of vertex array parameter - for (int i = 0; i < 9; i++) + for (int i = 0; i < 8; i++) _vertices[i] = t_vectorArray[i]; //This might be shortened with Vector3 operator overloading, but current From 02d79169255638f0c1ddf341981b951626542c11 Mon Sep 17 00:00:00 2001 From: Foxar Date: Mon, 21 Dec 2020 19:04:34 +0100 Subject: [PATCH 075/108] Fixed: Fixed incorrect naming of boundingbox constructor parameters. --- src/engine/models/bounding_box.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/engine/models/bounding_box.cpp b/src/engine/models/bounding_box.cpp index 271632f..948bacb 100644 --- a/src/engine/models/bounding_box.cpp +++ b/src/engine/models/bounding_box.cpp @@ -11,11 +11,11 @@ #include "../include/models/bounding_box.hpp" #include "../include/utils/debug.hpp" -BoundingBox::BoundingBox(Vector3 *t_vectorArray) +BoundingBox::BoundingBox(Vector3 *t_vertices) { //Perform a deep copy of vertex array parameter for (int i = 0; i < 8; i++) - _vertices[i] = t_vectorArray[i]; + _vertices[i] = t_vertices[i]; //This might be shortened with Vector3 operator overloading, but current //implementation is more human readable. From 586fe136ea0ef78d58621449eddb8f3fddc64fcc Mon Sep 17 00:00:00 2001 From: Foxar Date: Mon, 21 Dec 2020 19:05:33 +0100 Subject: [PATCH 076/108] Changed: Removed old vertex array boundingbox declaration, forced usage of temporary vector array in initial boundingbox calculations. --- src/engine/include/models/mesh_frame.hpp | 2 -- src/engine/include/models/mesh_material.hpp | 1 - src/engine/models/mesh_frame.cpp | 5 +---- src/engine/models/mesh_material.cpp | 10 +++++++--- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/engine/include/models/mesh_frame.hpp b/src/engine/include/models/mesh_frame.hpp index 8ffe8e0..8b4214f 100644 --- a/src/engine/include/models/mesh_frame.hpp +++ b/src/engine/include/models/mesh_frame.hpp @@ -145,8 +145,6 @@ public: void calculateBoundingBoxes(); private: - /** Deprecated. It is advised to use BoundingBox class instead. */ - Vector3 boundingBox[8]; BoundingBox *boundingBoxObj; u8 _areSTsAllocated, _areVerticesAllocated, diff --git a/src/engine/include/models/mesh_material.hpp b/src/engine/include/models/mesh_material.hpp index dffcf8d..b2588f9 100644 --- a/src/engine/include/models/mesh_material.hpp +++ b/src/engine/include/models/mesh_material.hpp @@ -125,7 +125,6 @@ public: u8 isInFrustum(Plane *t_frustumPlanes, const Vector3 &position); private: - Vector3 boundingBox[8]; BoundingBox *boundingBoxObj; u32 facesCount, id; u32 *vertexFaces, *stFaces, *normalFaces; diff --git a/src/engine/models/mesh_frame.cpp b/src/engine/models/mesh_frame.cpp index 2180a57..0e224cd 100644 --- a/src/engine/models/mesh_frame.cpp +++ b/src/engine/models/mesh_frame.cpp @@ -94,6 +94,7 @@ void MeshFrame::allocateMaterials(const u32 &t_val) void MeshFrame::calculateBoundingBoxes() { + Vector3 boundingBox[8]; if (!_areVerticesAllocated) { PRINT_ERR("Can't calculate bounding box, because vertices were not allocated!"); @@ -134,10 +135,6 @@ void MeshFrame::calculateBoundingBoxes() boundingBox[7].set(hiX, hiY, hiZ); _isBoundingBoxCalculated = true; - //Function temporarily is a hybrid between old Vector3[8] boundingbox and - //the new class implementation. Upon confirmation that new implementation - //works correctly, delete the old code. - //BoundingBox is declared on the heap to prevent any ill-formed default //constructor instantiated BoundingBox objects. boundingBoxObj = new BoundingBox(boundingBox); diff --git a/src/engine/models/mesh_material.cpp b/src/engine/models/mesh_material.cpp index bf4667e..59e5daa 100644 --- a/src/engine/models/mesh_material.cpp +++ b/src/engine/models/mesh_material.cpp @@ -83,9 +83,9 @@ u8 MeshMaterial::isInFrustum(Plane *t_frustumPlanes, const Vector3 &position) for (u8 y = 0; y < 8 && (boxIn == 0 || boxOut == 0); y++) { boxCalcTemp.set( - boundingBox[y].x + position.x, - boundingBox[y].y + position.y, - boundingBox[y].z + position.z); + boundingBoxObj->getVertices()[y].x + position.x, + boundingBoxObj->getVertices()[y].y + position.y, + boundingBoxObj->getVertices()[y].z + position.z); if (t_frustumPlanes[i].distanceTo(boxCalcTemp) < 0) boxOut++; else @@ -102,6 +102,7 @@ u8 MeshMaterial::isInFrustum(Plane *t_frustumPlanes, const Vector3 &position) void MeshMaterial::calculateBoundingBox(Vector3 *t_vertices, u32 t_vertCount) { + Vector3 boundingBox[8]; float lowX, lowY, lowZ, hiX, hiY, hiZ; lowX = hiX = t_vertices[vertexFaces[0]].x; lowY = hiY = t_vertices[vertexFaces[0]].y; @@ -133,5 +134,8 @@ void MeshMaterial::calculateBoundingBox(Vector3 *t_vertices, u32 t_vertCount) boundingBox[6].set(hiX, hiY, lowZ); boundingBox[7].set(hiX, hiY, hiZ); _isBoundingBoxCalculated = true; + + //BoundingBox is declared on the heap to prevent any ill-formed default + //constructor instantiated BoundingBox objects. boundingBoxObj = new BoundingBox(boundingBox); } From 5c232aebce4b7df964ed219f3864c5bcb52814f6 Mon Sep 17 00:00:00 2001 From: Foxar Date: Mon, 21 Dec 2020 19:32:40 +0100 Subject: [PATCH 077/108] Removed: Newline below a doxygen comment. --- src/engine/include/models/bounding_box.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/engine/include/models/bounding_box.hpp b/src/engine/include/models/bounding_box.hpp index e2aaccc..1dec3ee 100644 --- a/src/engine/include/models/bounding_box.hpp +++ b/src/engine/include/models/bounding_box.hpp @@ -43,7 +43,6 @@ struct BoundingBoxFace * Class containing bounding box data * which can be used for collision detection. */ - class BoundingBox { public: From 9e1a9e8b3c51f0d10a2be3bd2ae16b90be3e8741 Mon Sep 17 00:00:00 2001 From: Foxar Date: Mon, 21 Dec 2020 19:41:29 +0100 Subject: [PATCH 078/108] Changed: Renamed getBoundingBox() to getBoundingBoxVertices() as the function returns the array of vertices from the BoundingBox object. --- src/engine/include/models/mesh.hpp | 2 +- src/engine/include/models/mesh_frame.hpp | 4 ++-- src/engine/include/models/mesh_material.hpp | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/engine/include/models/mesh.hpp b/src/engine/include/models/mesh.hpp index aec1470..1de33bb 100644 --- a/src/engine/include/models/mesh.hpp +++ b/src/engine/include/models/mesh.hpp @@ -97,7 +97,7 @@ public: Vector3 &getCurrentBoundingBoxVertex(const u8 &i) { return frames[animState.currentFrame].getBoundingBoxVertex(i); }; /** Returns bounding box of current frame. Size: 8 */ - Vector3 *getCurrentBoundingBox() const { return frames[animState.currentFrame].getBoundingBox(); }; + Vector3 *getCurrentBoundingBox() const { return frames[animState.currentFrame].getBoundingBoxVertices(); }; // ---- // Setters diff --git a/src/engine/include/models/mesh_frame.hpp b/src/engine/include/models/mesh_frame.hpp index 8b4214f..6c33e89 100644 --- a/src/engine/include/models/mesh_frame.hpp +++ b/src/engine/include/models/mesh_frame.hpp @@ -80,7 +80,7 @@ public: * Returns bounding box (AABB). * Total length: 8 */ - Vector3 *getBoundingBox() { return boundingBoxObj->getVertices(); }; + Vector3 *getBoundingBoxVertices() { return boundingBoxObj->getVertices(); }; /** * Returns bounding box (AABB) vertex. @@ -91,7 +91,7 @@ public: /** * Returns bounding box (AABB) object pointer. */ - BoundingBox *getBoundingBoxP() { return boundingBoxObj; }; + BoundingBox *getBoundingBox() { return boundingBoxObj; }; // ---- // Setters diff --git a/src/engine/include/models/mesh_material.hpp b/src/engine/include/models/mesh_material.hpp index b2588f9..5099901 100644 --- a/src/engine/include/models/mesh_material.hpp +++ b/src/engine/include/models/mesh_material.hpp @@ -66,7 +66,7 @@ public: * Returns bounding box (AABB). * Total length: 8 */ - Vector3 *getBoundingBox() { return boundingBoxObj->getVertices(); }; + Vector3 *getBoundingBoxVertices() { return boundingBoxObj->getVertices(); }; /** * Returns bounding box (AABB) vertex. @@ -77,7 +77,7 @@ public: /** * Returns bounding box (AABB) object pointer. */ - BoundingBox *getBoundingBoxP() { return boundingBoxObj; }; + BoundingBox *getBoundingBox() { return boundingBoxObj; }; // ---- // Setters From 13f6a2ecb7a3ba5959234e0169209846f3e87220 Mon Sep 17 00:00:00 2001 From: Foxar Date: Mon, 21 Dec 2020 20:02:51 +0100 Subject: [PATCH 079/108] Changed: Changed manual deep-copy of parameter vertex array to usage of memcpy() --- src/engine/models/bounding_box.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/engine/models/bounding_box.cpp b/src/engine/models/bounding_box.cpp index 948bacb..15eb78d 100644 --- a/src/engine/models/bounding_box.cpp +++ b/src/engine/models/bounding_box.cpp @@ -10,12 +10,12 @@ #include "../include/models/bounding_box.hpp" #include "../include/utils/debug.hpp" +#include BoundingBox::BoundingBox(Vector3 *t_vertices) { //Perform a deep copy of vertex array parameter - for (int i = 0; i < 8; i++) - _vertices[i] = t_vertices[i]; + memcpy(_vertices, t_vertices, 8 * sizeof(Vector3)); //This might be shortened with Vector3 operator overloading, but current //implementation is more human readable. From cf07cf78c84b95cda6aed982c334653afe7e761f Mon Sep 17 00:00:00 2001 From: Foxar Date: Mon, 21 Dec 2020 20:06:55 +0100 Subject: [PATCH 080/108] Added: Comment for BoundingBox constructor parameters. --- src/engine/models/bounding_box.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/engine/models/bounding_box.cpp b/src/engine/models/bounding_box.cpp index 15eb78d..dc90364 100644 --- a/src/engine/models/bounding_box.cpp +++ b/src/engine/models/bounding_box.cpp @@ -12,6 +12,11 @@ #include "../include/utils/debug.hpp" #include +/** + * Construct a BoundingBox class + * Allows for user friendly access to bounding box data. + * @param t_vertices Array of 8 Vector3 elements. + */ BoundingBox::BoundingBox(Vector3 *t_vertices) { //Perform a deep copy of vertex array parameter From 8137bd56d8b49dc961ab2bf0340281ab21475ea7 Mon Sep 17 00:00:00 2001 From: Foxar Date: Mon, 21 Dec 2020 20:07:34 +0100 Subject: [PATCH 081/108] Changed: Usage of getVertices()[y] to getVertex(y) --- src/engine/models/mesh_material.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/engine/models/mesh_material.cpp b/src/engine/models/mesh_material.cpp index 59e5daa..d6108da 100644 --- a/src/engine/models/mesh_material.cpp +++ b/src/engine/models/mesh_material.cpp @@ -83,9 +83,9 @@ u8 MeshMaterial::isInFrustum(Plane *t_frustumPlanes, const Vector3 &position) for (u8 y = 0; y < 8 && (boxIn == 0 || boxOut == 0); y++) { boxCalcTemp.set( - boundingBoxObj->getVertices()[y].x + position.x, - boundingBoxObj->getVertices()[y].y + position.y, - boundingBoxObj->getVertices()[y].z + position.z); + boundingBoxObj->getVertex(y).x + position.x, + boundingBoxObj->getVertex(y).y + position.y, + boundingBoxObj->getVertex(y).z + position.z); if (t_frustumPlanes[i].distanceTo(boxCalcTemp) < 0) boxOut++; else From f8f91c6b4954809675337e20b204ad6a04433d86 Mon Sep 17 00:00:00 2001 From: Foxar Date: Mon, 21 Dec 2020 20:09:00 +0100 Subject: [PATCH 082/108] Changed: Return type of getVertex() to be const. --- src/engine/include/models/bounding_box.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/engine/include/models/bounding_box.hpp b/src/engine/include/models/bounding_box.hpp index 1dec3ee..9ffafd5 100644 --- a/src/engine/include/models/bounding_box.hpp +++ b/src/engine/include/models/bounding_box.hpp @@ -69,7 +69,7 @@ public: * Returns bounding box raw vertices array. * Total length: 8 */ - Vector3 &getVertex(const u8 &i) { return _vertices[i]; }; + const Vector3 &getVertex(const u8 &i) { return _vertices[i]; }; /** Returns single vertex from raw vertices array.*/ Vector3 *getVertices() { return _vertices; }; From 115dac21bceee17e827be4647ad3025ecc616bc8 Mon Sep 17 00:00:00 2001 From: Foxar Date: Mon, 21 Dec 2020 20:15:18 +0100 Subject: [PATCH 083/108] Changed: Return types in mesh_frame and mesh_materials for getBoundingBoxVertex() to const. --- src/engine/include/models/mesh_frame.hpp | 2 +- src/engine/include/models/mesh_material.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/engine/include/models/mesh_frame.hpp b/src/engine/include/models/mesh_frame.hpp index 6c33e89..9b57699 100644 --- a/src/engine/include/models/mesh_frame.hpp +++ b/src/engine/include/models/mesh_frame.hpp @@ -86,7 +86,7 @@ public: * Returns bounding box (AABB) vertex. * Total length: 8 */ - Vector3 &getBoundingBoxVertex(const u8 &i) { return boundingBoxObj->getVertex(i); }; + const Vector3 &getBoundingBoxVertex(const u8 &i) { return boundingBoxObj->getVertex(i); }; /** * Returns bounding box (AABB) object pointer. diff --git a/src/engine/include/models/mesh_material.hpp b/src/engine/include/models/mesh_material.hpp index 5099901..47cf64b 100644 --- a/src/engine/include/models/mesh_material.hpp +++ b/src/engine/include/models/mesh_material.hpp @@ -72,7 +72,7 @@ public: * Returns bounding box (AABB) vertex. * Total length: 8 */ - Vector3 &getBoundingBoxVertex(const u8 &i) { return boundingBoxObj->getVertex(i); }; + const Vector3 &getBoundingBoxVertex(const u8 &i) { return boundingBoxObj->getVertex(i); }; /** * Returns bounding box (AABB) object pointer. From 23ea1a28f6a47c9e8ac7e7a9a727601dee25388c Mon Sep 17 00:00:00 2001 From: Foxar Date: Mon, 21 Dec 2020 20:22:40 +0100 Subject: [PATCH 084/108] Changed: Comments, boundingbox getters in mesh.hpp --- src/engine/include/models/mesh.hpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/engine/include/models/mesh.hpp b/src/engine/include/models/mesh.hpp index 1de33bb..1f9e7ea 100644 --- a/src/engine/include/models/mesh.hpp +++ b/src/engine/include/models/mesh.hpp @@ -94,10 +94,13 @@ public: * Returns bounding vertex of current frame. * @param i 0-7. Because, bounding box have 8 corners */ - Vector3 &getCurrentBoundingBoxVertex(const u8 &i) { return frames[animState.currentFrame].getBoundingBoxVertex(i); }; + const Vector3 &getCurrentBoundingBoxVertex(const u8 &i) { return frames[animState.currentFrame].getBoundingBoxVertex(i); }; - /** Returns bounding box of current frame. Size: 8 */ - Vector3 *getCurrentBoundingBox() const { return frames[animState.currentFrame].getBoundingBoxVertices(); }; + /** @returns bounding box vertex array of current frame. Size: 8 */ + const Vector3 *getCurrentBoundingBoxVertices() const { return frames[animState.currentFrame].getBoundingBoxVertices(); }; + + /** @returns bounding box object of current frame. */ + const BoundingBox *getCurrentBoundingBox() const { return frames[animState.currentFrame].getBoundingBox(); }; // ---- // Setters From ae3dd737e5662753d10d6426993a756b4a817ce7 Mon Sep 17 00:00:00 2001 From: Foxar Date: Mon, 21 Dec 2020 20:28:26 +0100 Subject: [PATCH 085/108] Changed: Multiple const additions. --- src/engine/models/mesh.cpp | 2 +- src/samples/floors/utils.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/engine/models/mesh.cpp b/src/engine/models/mesh.cpp index 84cddce..5a47159 100644 --- a/src/engine/models/mesh.cpp +++ b/src/engine/models/mesh.cpp @@ -347,7 +347,7 @@ u8 Mesh::isInFrustum(Plane *t_frustumPlanes) { Vector3 boxCalcTemp; u8 boxResult = 1, boxIn = 0, boxOut = 0; - Vector3 *currentBoundingBox = getCurrentBoundingBox(); + const Vector3 *currentBoundingBox = getCurrentBoundingBoxVertices(); for (int i = 0; i < 6; i++) { boxOut = 0; diff --git a/src/samples/floors/utils.cpp b/src/samples/floors/utils.cpp index 5195cf9..d8b1021 100644 --- a/src/samples/floors/utils.cpp +++ b/src/samples/floors/utils.cpp @@ -38,7 +38,7 @@ void Utils::getMinMax(const Mesh &t_mesh, Vector3 &t_min, Vector3 &t_max) { Vector3 calc = Vector3(); u8 isInitialized = 0; - Vector3 *boundingBox = t_mesh.getCurrentBoundingBox(); + const Vector3 *boundingBox = t_mesh.getCurrentBoundingBoxVertices(); for (u32 i = 0; i < 8; i++) { calc.set( From 3af357620372670078023a33e9812b3a7db622e2 Mon Sep 17 00:00:00 2001 From: Foxar Date: Mon, 21 Dec 2020 20:35:09 +0100 Subject: [PATCH 086/108] Added: Multiple doxygen @returns comments. --- src/engine/include/models/bounding_box.hpp | 18 +++++++++--------- src/engine/include/models/mesh_frame.hpp | 6 +++--- src/engine/include/models/mesh_material.hpp | 6 +++--- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/engine/include/models/bounding_box.hpp b/src/engine/include/models/bounding_box.hpp index 9ffafd5..cab4a9b 100644 --- a/src/engine/include/models/bounding_box.hpp +++ b/src/engine/include/models/bounding_box.hpp @@ -51,26 +51,26 @@ public: const float &getHeight() { return _height; }; const float &getDepth() { return _depth; }; const float &getWidth() { return _width; }; - /** Return the vector directly in middle of the bounding box. */ + /** @returns the vector directly in middle of the bounding box. */ const Vector3 &getCenter() { return _centerVector; }; - /** Return the front face (further on z-axis) */ + /** @returns the front face (further on z-axis) */ const BoundingBoxFace &getFrontFace() { return _frontFace; }; - /** Return the back face (nearer on z-axis) */ + /** @returns the back face (nearer on z-axis) */ const BoundingBoxFace &getBackFace() { return _backFace; }; - /** Return the left face (further on x-axis) */ + /** @returns the left face (further on x-axis) */ const BoundingBoxFace &getLeftFace() { return _leftFace; }; - /** Return the right face (nearer on x-axis) */ + /** @returns the right face (nearer on x-axis) */ const BoundingBoxFace &getRightFace() { return _rightFace; }; - /** Return the top face (further on y-axis) */ + /** @returns the top face (further on y-axis) */ const BoundingBoxFace &getTopFace() { return _topFace; }; - /** Return the bottom face (nearer on y-axis) */ + /** @returns the bottom face (nearer on y-axis) */ const BoundingBoxFace &getBottomFace() { return _bottomFace; }; /** - * Returns bounding box raw vertices array. + * @returns bounding box raw vertices array. * Total length: 8 */ const Vector3 &getVertex(const u8 &i) { return _vertices[i]; }; - /** Returns single vertex from raw vertices array.*/ + /** @returns single vertex from raw vertices array.*/ Vector3 *getVertices() { return _vertices; }; private: diff --git a/src/engine/include/models/mesh_frame.hpp b/src/engine/include/models/mesh_frame.hpp index 9b57699..cd7c299 100644 --- a/src/engine/include/models/mesh_frame.hpp +++ b/src/engine/include/models/mesh_frame.hpp @@ -77,19 +77,19 @@ public: MeshMaterial *getMaterials() const { return materials; }; /** - * Returns bounding box (AABB). + * @returns bounding box (AABB). * Total length: 8 */ Vector3 *getBoundingBoxVertices() { return boundingBoxObj->getVertices(); }; /** - * Returns bounding box (AABB) vertex. + * @returns bounding box (AABB) vertex. * Total length: 8 */ const Vector3 &getBoundingBoxVertex(const u8 &i) { return boundingBoxObj->getVertex(i); }; /** - * Returns bounding box (AABB) object pointer. + * @returns bounding box (AABB) object pointer. */ BoundingBox *getBoundingBox() { return boundingBoxObj; }; diff --git a/src/engine/include/models/mesh_material.hpp b/src/engine/include/models/mesh_material.hpp index 47cf64b..3ba2621 100644 --- a/src/engine/include/models/mesh_material.hpp +++ b/src/engine/include/models/mesh_material.hpp @@ -63,19 +63,19 @@ public: u32 *getNormalFaces() const { return normalFaces; }; /** - * Returns bounding box (AABB). + * @returns bounding box (AABB). * Total length: 8 */ Vector3 *getBoundingBoxVertices() { return boundingBoxObj->getVertices(); }; /** - * Returns bounding box (AABB) vertex. + * @returns bounding box (AABB) vertex. * Total length: 8 */ const Vector3 &getBoundingBoxVertex(const u8 &i) { return boundingBoxObj->getVertex(i); }; /** - * Returns bounding box (AABB) object pointer. + * @returns bounding box (AABB) object pointer. */ BoundingBox *getBoundingBox() { return boundingBoxObj; }; From b935d4da135e195b06b57051407f9e5706c3e03b Mon Sep 17 00:00:00 2001 From: h4570 Date: Wed, 23 Dec 2020 20:24:08 +0100 Subject: [PATCH 087/108] fixed newline warning --- src/engine/include/models/bounding_box.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/engine/include/models/bounding_box.hpp b/src/engine/include/models/bounding_box.hpp index cab4a9b..a2f6c44 100644 --- a/src/engine/include/models/bounding_box.hpp +++ b/src/engine/include/models/bounding_box.hpp @@ -91,4 +91,4 @@ private: BoundingBoxFace _bottomFace; }; -#endif \ No newline at end of file +#endif From f95e805d338cd5a50b32f228777b2f9b018d43a4 Mon Sep 17 00:00:00 2001 From: h4570 Date: Wed, 23 Dec 2020 20:28:28 +0100 Subject: [PATCH 088/108] added data alignment --- src/engine/include/models/math/matrix.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/engine/include/models/math/matrix.hpp b/src/engine/include/models/math/matrix.hpp index 5e13bd7..03f229e 100644 --- a/src/engine/include/models/math/matrix.hpp +++ b/src/engine/include/models/math/matrix.hpp @@ -19,7 +19,7 @@ class Matrix { public: - float data[16]; + float data[16] __attribute__((aligned(64))); Matrix(float m11, float m12, float m13, float m14, float m21, float m22, float m23, float m24, From e0ad58f72f9f3fc3a116a877c7c4d34b3cef8d1a Mon Sep 17 00:00:00 2001 From: h4570 Date: Wed, 23 Dec 2020 20:28:41 +0100 Subject: [PATCH 089/108] fixed bug with matrix multiply --- src/engine/models/math/matrix.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/engine/models/math/matrix.cpp b/src/engine/models/math/matrix.cpp index ec927a1..b0e9e2f 100644 --- a/src/engine/models/math/matrix.cpp +++ b/src/engine/models/math/matrix.cpp @@ -160,7 +160,7 @@ Matrix Matrix::operator*(const Matrix &t) "sqc2 vf3, 0x20(%0) \n\t" "sqc2 vf4, 0x30(%0) \n\t" : - : "r"(result.data), "r"(this->data), "r"(t.data) + : "r"(result.data), "r"(t.data), "r"(this->data) : "memory"); return result; } @@ -197,7 +197,7 @@ void Matrix::operator*=(const Matrix &t) "sqc2 vf3, 0x20(%0) \n\t" "sqc2 vf4, 0x30(%0) \n\t" : - : "r"(this->data), "r"(this->data), "r"(t.data) + : "r"(this->data), "r"(t.data), "r"(this->data) : "memory"); } From 4b2deb8b3999aa82c9dfec1d740378919b539de7 Mon Sep 17 00:00:00 2001 From: h4570 Date: Wed, 23 Dec 2020 22:10:12 +0100 Subject: [PATCH 090/108] matrix refactor --- src/engine/include/models/math/matrix.hpp | 128 +++++-- src/engine/models/math/matrix.cpp | 413 ++++++++++++---------- 2 files changed, 318 insertions(+), 223 deletions(-) diff --git a/src/engine/include/models/math/matrix.hpp b/src/engine/include/models/math/matrix.hpp index 03f229e..6d97b7d 100644 --- a/src/engine/include/models/math/matrix.hpp +++ b/src/engine/include/models/math/matrix.hpp @@ -13,79 +13,139 @@ #include "vector3.hpp" #include "../screen_settings.hpp" +#include -/** https://en.wikipedia.org/wiki/Matrix_(mathematics) */ +/** 4x4 Matrix class. */ class Matrix { public: float data[16] __attribute__((aligned(64))); - Matrix(float m11, float m12, float m13, float m14, - float m21, float m22, float m23, float m24, - float m31, float m32, float m33, float m34, - float m41, float m42, float m43, float m44); - Matrix(const Matrix &v); - Matrix operator*(const Matrix &v); - void operator*=(const Matrix &v); - Matrix(); - ~Matrix(); + // ---- + // Constructors/Destructors + // ---- - void lookAt(Vector3 &t_up, Vector3 &t_position, Vector3 &t_target); + /** Create empty matrix. */ + Matrix(); + + /** + * Create identity/empty matrix. + * @param t_do_identity Identity matrix if true, random matrix values otherwise + */ + Matrix(const u8 &t_do_identity) + { + if (t_do_identity) + identity(); + } + + /** Init matrix with given values. */ + Matrix(const float &m11, const float &m12, const float &m13, const float &m14, + const float &m21, const float &m22, const float &m23, const float &m24, + const float &m31, const float &m32, const float &m33, const float &m34, + const float &m41, const float &m42, const float &m43, const float &m44); + + /** Init matrix with other matrix values. */ + Matrix(const Matrix &v) { memcpy(data, v.data, 16 * sizeof(float)); } + + // ---- + // Operators + // ---- + + Matrix operator*(const Matrix &v) + { + Matrix result; + cross(result.data, this->data, v.data); + return result; + } + + inline void operator*=(const Matrix &v) { cross(this->data, this->data, v.data); } + + float &operator[](const u8 &t_index) { return data[t_index]; } + + // ---- + // Functions + // ---- + + /** + * 1 0 0 0 + * 0 1 0 0 + * 0 0 1 0 + * 0 0 0 1 + */ void identity(); + + /** + * 1 0 0 0 + * 0 1 0 0 + * 0 0 1 0 + * 0 0 0 1 + */ inline void unit() { identity(); }; - // Translation - - void translate(const Vector3 &t_val); - + /** Create translation matrix. */ 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); - + /** Create X rotation matrix. */ inline void rotationX(const float &t_radians) { identity(); rotateX(t_radians); } + /** Create Y rotation matrix. */ inline void rotationY(const float &t_radians) { identity(); rotateY(t_radians); } + /** Create Z rotation matrix. */ 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); - } + /** Create angle rotation matrix. */ + void rotationByAngle(const float &t_angle, const Vector3 &t_axis); - inline void rotation(const Vector3 &t_val) - { - identity(); - rotate(t_val); - } + /** Create scale matrix. */ + void setScale(const Vector3 &t_val); - // + /** + * Create perspective projection matrix. + * + * Complies with OpenGL standard (gluPerspective). + * @see https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluPerspective.xml + * @param screen Screen settings + */ + void setPerspective(const ScreenSettings &screen); - void setPerspective(ScreenSettings &screen); + /** + * Create a view matrix that transforms coordinates in + * such a way that the user looks at a target vector + * direction from a position vector. + * + * Complies with OpenGL standard (glLookAt). + * @see https://learnopengl.com/Getting-started/Camera + */ + void lookAt(const Vector3 &t_position, const Vector3 &t_target); + + /** Print matrix (printf). */ const void print() const; + +private: + void rotateX(const float &t_radians); + void rotateY(const float &t_radians); + void rotateZ(const float &t_radians); + void translate(const Vector3 &t_val); + void cross(float res[16], const float a[16], const float b[16]); + void setCamera(const float t_pos[4], const float t_vz[4], const float t_vy[4]); }; #endif diff --git a/src/engine/models/math/matrix.cpp b/src/engine/models/math/matrix.cpp index b0e9e2f..71032b7 100644 --- a/src/engine/models/math/matrix.cpp +++ b/src/engine/models/math/matrix.cpp @@ -17,11 +17,16 @@ // Constructors/Destructors // ---- -/** Create by specifying all points */ -Matrix::Matrix(float m11, float m12, float m13, float m14, - float m21, float m22, float m23, float m24, - float m31, float m32, float m33, float m34, - float m41, float m42, float m43, float m44) +Matrix::Matrix() +{ + for (u8 i = 0; i < 16; i++) + data[i] = 0.0F; +} + +Matrix::Matrix(const float &m11, const float &m12, const float &m13, const float &m14, + const float &m21, const float &m22, const float &m23, const float &m24, + const float &m31, const float &m32, const float &m33, const float &m34, + const float &m41, const float &m42, const float &m43, const float &m44) { data[0] = m11; data[1] = m12; @@ -44,29 +49,13 @@ Matrix::Matrix(float m11, float m12, float m13, float m14, data[15] = m44; } -/** Create with another matrix values */ -Matrix::Matrix(const Matrix &v) -{ - data[0] = v.data[0]; - data[1] = v.data[1]; - data[2] = v.data[2]; - data[3] = v.data[3]; +// ---- +// Operators +// ---- - data[4] = v.data[4]; - data[5] = v.data[5]; - data[6] = v.data[6]; - data[7] = v.data[7]; - - data[8] = v.data[8]; - data[9] = v.data[9]; - data[10] = v.data[10]; - data[11] = v.data[11]; - - data[12] = v.data[12]; - data[13] = v.data[13]; - data[14] = v.data[14]; - data[15] = v.data[15]; -} +// ---- +// Functions +// ---- void Matrix::identity() { @@ -84,13 +73,145 @@ void Matrix::identity() : "r"(this->data)); } -void Matrix::translate(const Vector3 &t_val) +void Matrix::rotationByAngle(const float &t_angle, const Vector3 &t_axis) { - this->data[12] += t_val.x; // 3,0 - this->data[13] += t_val.y; // 3,1 - this->data[14] += t_val.z; // 3,2 + Vector3 localAxis = Vector3(t_axis); + localAxis.normalize(); + float x = localAxis.x; + float y = localAxis.y; + float z = localAxis.z; + + float c = Math::cos(t_angle); + float s = Math::sin(t_angle); + + this->data[0] = x * x * (1 - c) + c; + this->data[1] = y * x * (1 - c) + z * s; + this->data[2] = x * z * (1 - c) - y * s; + this->data[3] = 0.0F; + + this->data[4] = x * y * (1 - c) - z * s; + this->data[5] = y * y * (1 - c) + c; + this->data[6] = y * z * (1 - c) + x * s; + this->data[7] = 0.0F; + + this->data[8] = x * z * (1 - c) + y * s; + this->data[9] = y * z * (1 - c) - x * s; + this->data[10] = z * z * (1 - c) + c; + this->data[11] = 0.0F; + + this->data[12] = 0.0F; + this->data[13] = 0.0F; + this->data[14] = 0.0F; + this->data[15] = 1.0F; } +void Matrix::setScale(const Vector3 &t_val) +{ + this->identity(); + this->data[0] = t_val.x; + this->data[5] = t_val.y; + this->data[10] = t_val.z; + this->data[15] = 1.0F; +} + +void Matrix::setPerspective(const ScreenSettings &t_screen) +{ + float fovYdiv2 = Math::HALF_ANG2RAD * t_screen.fov; + float cotFOV = 1.0F / (Math::sin(fovYdiv2) / Math::cos(fovYdiv2)); + float w = cotFOV * (t_screen.width / t_screen.projectionScale) / t_screen.aspectRatio; + float h = cotFOV * (t_screen.height / t_screen.projectionScale); + + this->data[0] = w; + this->data[1] = 0.0F; + this->data[2] = 0.0F; + this->data[3] = 0.0F; + + this->data[4] = 0.0F; + this->data[5] = -h; + this->data[6] = 0.0F; + this->data[7] = 0.0F; + + this->data[8] = 0.0F; + this->data[9] = 0.0F; + this->data[10] = + (t_screen.farPlaneDist + t_screen.nearPlaneDist) / + (t_screen.farPlaneDist - t_screen.nearPlaneDist); + this->data[11] = -1.0F; + + this->data[12] = 0.0F; + this->data[13] = 0.0F; + this->data[14] = + (2.0F * t_screen.farPlaneDist * t_screen.nearPlaneDist) / + (t_screen.farPlaneDist - t_screen.nearPlaneDist); + this->data[15] = 0.0F; +} + +void Matrix::lookAt(const Vector3 &t_position, const Vector3 &t_target) +{ + VECTOR up_vec, view_vec; + VECTOR eye = {t_position.x, t_position.y, t_position.z, 1.0F}; + VECTOR obj = {t_target.x, t_target.y, t_target.z, 1.0F}; + asm volatile( + "lqc2 vf4, 0x00(%2) # eye \n\t" + "lqc2 vf5, 0x00(%3) # obj \n\t" + "vsub.xyz vf7, vf4, vf5 # view_vec = vf7 \n\t" + "vmove.xyzw vf6, vf0 \n\t" + "vaddw.y vf6, vf0, vf0 # vf6 = { 0.0f, 1.0f, 0.0f, 1.0f } \n\t" + "vopmula.xyz ACC, vf6, vf7 \n\t" + "vopmsub.xyz vf9, vf7, vf6 # vec = vf9 \n\t" + "vopmula.xyz ACC, vf7, vf9 \n\t" + "vopmsub.xyz vf8, vf9, vf7 # up_vec = vf8 \n\t" + "sqc2 vf7, 0x00(%0) # view_vec \n\t" + "sqc2 vf6, 0x00(%1) # up_vec \n\t" + : + : "r"(view_vec), "r"(up_vec), "r"(eye), "r"(obj)); + Matrix temp; + temp.setCamera(eye, view_vec, up_vec); + identity(); + cross(this->data, this->data, temp.data); + + // Vector3 camForward, camUp, camRight; + // Vector3 t_up = Vector3(0.0F, 1.0F, 0.0F); + // camForward = t_position - t_target; + // camForward.normalize(); + // camRight = t_up * camForward; + // camRight.normalize(); + // camUp = camForward * camRight; + + // data[0] = camRight.x; + // data[4] = camRight.y; + // data[8] = camRight.z; + // data[12] = -camRight.innerProduct(t_position); + + // data[1] = camUp.x; + // data[5] = camUp.y; + // data[9] = camUp.z; + // data[13] = -camUp.innerProduct(t_position); + + // data[2] = camForward.x; + // data[6] = camForward.y; + // data[10] = camForward.z; + // data[14] = -camForward.innerProduct(t_position); + + // data[3] = 0; + // data[7] = 0; + // data[11] = 0; + // data[15] = 1; +} + +const void Matrix::print() const +{ + printf("MATRIX(\n%f, %f, %f, %f\n%f, %f, %f, %f\n%f, %f, %f, %f\n%f, %f, %f, %f\n)\n", + data[0], data[1], data[2], data[3], + data[4], data[5], data[6], data[7], + data[8], data[9], data[10], data[11], + data[12], data[13], data[14], data[15]); +} + +// ---- +// Private +// ---- + void Matrix::rotateX(const float &t_radians) { Matrix temp = Matrix(); @@ -127,45 +248,80 @@ void Matrix::rotateZ(const float &t_radians) this->data[5] = c; // 1,1 } -Matrix Matrix::operator*(const Matrix &t) +void Matrix::translate(const Vector3 &t_val) { - Matrix result; - 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"(result.data), "r"(t.data), "r"(this->data) - : "memory"); - return result; + 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::operator*=(const Matrix &t) +void Matrix::setCamera(const float t_pos[4], const float t_vz[4], const float t_vy[4]) +{ + // Matrix vf4, vf5, vf6, vf7 + // t_pos vf8 + // t_vz vf9 + // t_vy vf10 + // vtmp vf11 + asm volatile( + "lqc2 vf9, 0x00(%2) \n\t" + "lqc2 vf10, 0x00(%3) \n\t" + // mtmp.unit() + "vsub.w vf5, vf0, vf0 # mtmp[1][PW] = 0.0F \n\t" + // vtmp.outerProduct(vy, vz); + "vopmula.xyz ACC, vf10, vf9 \n\t" + "vopmsub.xyz vf11, vf9, vf10 \n\t" + // mtmp[0] = vtmp.normalize(); + "vmul.xyz vf12, vf11, vf11 \n\t" + "vaddy.x vf12, vf12, vf12 \n\t" + "vaddz.x vf12, vf12, vf12 \n\t" + "vrsqrt Q, vf0w, vf12x \n\t" + "vsub.xyzw vf4, vf0, vf0 \n\t" + "vwaitq \n\t" + "vmulq.xyz vf4, vf11, Q \n\t" + // mtmp[2] = vz.normalize(); + "vmul.xyz vf12, vf9, vf9 \n\t" + "vaddy.x vf12, vf12, vf12 \n\t" + "vaddz.x vf12, vf12, vf12 \n\t" + "vrsqrt Q, vf0w, vf12x \n\t" + "vsub.xyzw vf6, vf0, vf0 \n\t" + "vwaitq \n\t" + "vmulq.xyz vf6, vf9, Q \n\t" + // mtmp[1].outerProduct(mtmp[2], mtmp[0]); + "vopmula.xyz ACC, vf6, vf4 \n\t" + "vopmsub.xyz vf5, vf4, vf6 \n\t" + // mtmp.transpose(pos); + "lqc2 vf7, 0x00(%1) \n\t" + // m = mtmp.inverse(); + "qmfc2.ni $11, vf0 \n\t" + "qmfc2.ni $8, vf4 \n\t" + "qmfc2.ni $9, vf5 \n\t" + "qmfc2.ni $10, vf6 \n\t" + + "pextlw $12, $9, $8 \n\t" + "pextuw $13, $9, $8 \n\t" + "pextlw $14, $11, $10 \n\t" + "pextuw $15, $11, $10 \n\t" + "pcpyld $8, $14, $12 \n\t" + "pcpyud $9, $12, $14 \n\t" + "pcpyld $10, $15, $13 \n\t" + + "qmtc2.ni $8, vf16 \n\t" + "qmtc2.ni $9, vf17 \n\t" + "qmtc2.ni $10, vf18 \n\t" + "vmulax.xyz ACC, vf16, vf7 \n\t" + "vmadday.xyz ACC, vf17, vf7 \n\t" + "vmaddz.xyz vf5, vf18, vf7 \n\t" + "vsub.xyzw vf5, vf0, vf5 \n\t" + + "sq $8, 0x00(%0) \n\t" + "sq $9, 0x10(%0) \n\t" + "sq $10, 0x20(%0) \n\t" + "sqc2 vf5, 0x30(%0) \n\t" + : + : "r"(this->data), "r"(t_pos), "r"(t_vz), "r"(t_vy)); +} + +void Matrix::cross(float res[16], const float a[16], const float b[16]) { asm volatile( "lqc2 vf1, 0x00(%1) \n\t" @@ -197,127 +353,6 @@ void Matrix::operator*=(const Matrix &t) "sqc2 vf3, 0x20(%0) \n\t" "sqc2 vf4, 0x30(%0) \n\t" : - : "r"(this->data), "r"(t.data), "r"(this->data) + : "r"(res), "r"(b), "r"(a) : "memory"); } - -/** Create empty matrix */ -Matrix::Matrix() -{ - data[0] = 0.0F; - data[1] = 0.0F; - data[2] = 0.0F; - data[3] = 0.0F; - - data[4] = 0.0F; - data[5] = 0.0F; - data[6] = 0.0F; - data[7] = 0.0F; - - data[8] = 0.0F; - data[9] = 0.0F; - data[10] = 0.0F; - data[11] = 0.0F; - - data[12] = 0.0F; - data[13] = 0.0F; - data[14] = 0.0F; - data[15] = 0.0F; -} - -Matrix::~Matrix() {} - -// ---- -// Methods -// ---- - -/** Set up a perspective projection matrix - * - * Clone of gluPerspective() - * https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluPerspective.xml - * @param fov (FOV in radians)/2 - * @param aspect Aspect ratio - * @param scrW Half of screen width - * @param scrH Half of screen height - * @param zNear Distance to near plane - * @param zFar Distance to far plane - * @param projScale Projection scale - */ -void Matrix::setPerspective(ScreenSettings &t_screen) -{ - float fovYdiv2 = Math::HALF_ANG2RAD * t_screen.fov; - float cotFOV = 1.0F / (Math::sin(fovYdiv2) / Math::cos(fovYdiv2)); - float w = cotFOV * (t_screen.width / t_screen.projectionScale) / t_screen.aspectRatio; - float h = cotFOV * (t_screen.height / t_screen.projectionScale); - - this->data[0] = w; - this->data[1] = 0.0F; - this->data[2] = 0.0F; - this->data[3] = 0.0F; - - this->data[4] = 0.0F; - this->data[5] = -h; - this->data[6] = 0.0F; - this->data[7] = 0.0F; - - this->data[8] = 0.0F; - this->data[9] = 0.0F; - this->data[10] = - (t_screen.farPlaneDist + t_screen.nearPlaneDist) / - (t_screen.farPlaneDist - t_screen.nearPlaneDist); - this->data[11] = -1.0F; - - this->data[12] = 0.0F; - this->data[13] = 0.0F; - this->data[14] = - (2.0F * t_screen.farPlaneDist * t_screen.nearPlaneDist) / - (t_screen.farPlaneDist - t_screen.nearPlaneDist); - this->data[15] = 0.0F; -} - -/** Create a view matrix that transforms coordinates in - * such a way that the user looks at a target vector - * direction from a position vector. - * - * Clone of OpenGL lookAt function - * https://learnopengl.com/Getting-started/Camera -*/ -void Matrix::lookAt(Vector3 &t_up, Vector3 &t_position, Vector3 &t_target) -{ - Vector3 camForward, camUp, camRight; - - camForward = t_position - t_target; - camForward.normalize(); - camRight = t_up * camForward; - camRight.normalize(); - camUp = camForward * camRight; - - data[0] = camRight.x; - data[4] = camRight.y; - data[8] = camRight.z; - data[12] = -camRight.innerProduct(t_position); - - data[1] = camUp.x; - data[5] = camUp.y; - data[9] = camUp.z; - data[13] = -camUp.innerProduct(t_position); - - data[2] = camForward.x; - data[6] = camForward.y; - data[10] = camForward.z; - data[14] = -camForward.innerProduct(t_position); - - data[3] = 0; - data[7] = 0; - data[11] = 0; - data[15] = 1; -} - -const void Matrix::print() const -{ - printf("MATRIX(\n%f, %f, %f, %f\n%f, %f, %f, %f\n%f, %f, %f, %f\n%f, %f, %f, %f\n)\n", - data[0], data[1], data[2], data[3], - data[4], data[5], data[6], data[7], - data[8], data[9], data[10], data[11], - data[12], data[13], data[14], data[15]); -} From 7dd89476126431948953c981b84ddf778398bc50 Mon Sep 17 00:00:00 2001 From: h4570 Date: Wed, 23 Dec 2020 22:38:57 +0100 Subject: [PATCH 091/108] added vec3 multiply --- src/engine/include/models/math/matrix.hpp | 13 +++++++++++-- src/engine/models/math/matrix.cpp | 20 +++++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/engine/include/models/math/matrix.hpp b/src/engine/include/models/math/matrix.hpp index 6d97b7d..b30f6d1 100644 --- a/src/engine/include/models/math/matrix.hpp +++ b/src/engine/include/models/math/matrix.hpp @@ -52,7 +52,16 @@ public: // Operators // ---- - Matrix operator*(const Matrix &v) + Matrix operator*(const Matrix &v) const + { + Matrix result; + cross(result.data, this->data, v.data); + return result; + } + + Vector3 operator*(const Vector3 &v) const; + + Matrix operator*(const Matrix &v) const { Matrix result; cross(result.data, this->data, v.data); @@ -144,7 +153,7 @@ private: void rotateY(const float &t_radians); void rotateZ(const float &t_radians); void translate(const Vector3 &t_val); - void cross(float res[16], const float a[16], const float b[16]); + void cross(float res[16], const float a[16], const float b[16]) const; void setCamera(const float t_pos[4], const float t_vz[4], const float t_vy[4]); }; diff --git a/src/engine/models/math/matrix.cpp b/src/engine/models/math/matrix.cpp index 71032b7..1f4ff64 100644 --- a/src/engine/models/math/matrix.cpp +++ b/src/engine/models/math/matrix.cpp @@ -53,6 +53,24 @@ Matrix::Matrix(const float &m11, const float &m12, const float &m13, const float // Operators // ---- +Vector3 Matrix::operator*(const Vector3 &v) const +{ + Vector3 result; + asm volatile( + "lqc2 vf4, 0x00(%1) \n\t" + "lqc2 vf5, 0x10(%1) \n\t" + "lqc2 vf6, 0x20(%1) \n\t" + "lqc2 vf7, 0x30(%1) \n\t" + "lqc2 vf8, 0x00(%2) \n\t" + "vmulax.xyzw ACC, vf4, vf8 \n\t" + "vmadday.xyzw ACC, vf5, vf8 \n\t" + "vmaddaz.xyzw ACC, vf6, vf8 \n\t" + "vmaddw.xyzw vf9, vf7, vf8 \n\t" + "sqc2 vf9, 0x00(%0) \n\t" + : + : "r"(result.xyz), "r"(this->data), "r"(v.xyz)); +} + // ---- // Functions // ---- @@ -321,7 +339,7 @@ void Matrix::setCamera(const float t_pos[4], const float t_vz[4], const float t_ : "r"(this->data), "r"(t_pos), "r"(t_vz), "r"(t_vy)); } -void Matrix::cross(float res[16], const float a[16], const float b[16]) +void Matrix::cross(float res[16], const float a[16], const float b[16]) const { asm volatile( "lqc2 vf1, 0x00(%1) \n\t" From 5a932b35ca8d904edf26609acfd15dbb25a56b84 Mon Sep 17 00:00:00 2001 From: h4570 Date: Thu, 24 Dec 2020 10:32:51 +0100 Subject: [PATCH 092/108] fixed bug, added full rotation --- src/engine/include/models/math/matrix.hpp | 26 +++++++++++++++++------ src/engine/models/math/matrix.cpp | 6 ++++-- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/engine/include/models/math/matrix.hpp b/src/engine/include/models/math/matrix.hpp index b30f6d1..d14855b 100644 --- a/src/engine/include/models/math/matrix.hpp +++ b/src/engine/include/models/math/matrix.hpp @@ -52,13 +52,6 @@ public: // Operators // ---- - Matrix operator*(const Matrix &v) const - { - Matrix result; - cross(result.data, this->data, v.data); - return result; - } - Vector3 operator*(const Vector3 &v) const; Matrix operator*(const Matrix &v) const @@ -99,6 +92,25 @@ public: translate(t_val); } + /** Create rotation matrix. */ + inline void rotation(const Vector3 &t_val) + { + identity(); + Matrix temp = Matrix(); + + temp.identity(); + temp.rotateX(t_val.x); + operator*=(temp); + + temp.identity(); + temp.rotateY(t_val.y); + operator*=(temp); + + temp.identity(); + temp.rotateZ(t_val.z); + operator*=(temp); + } + /** Create X rotation matrix. */ inline void rotationX(const float &t_radians) { diff --git a/src/engine/models/math/matrix.cpp b/src/engine/models/math/matrix.cpp index 1f4ff64..805c433 100644 --- a/src/engine/models/math/matrix.cpp +++ b/src/engine/models/math/matrix.cpp @@ -55,7 +55,8 @@ Matrix::Matrix(const float &m11, const float &m12, const float &m13, const float Vector3 Matrix::operator*(const Vector3 &v) const { - Vector3 result; + float a[4] = {v.x, v.y, v.z, 1.0F}; + float res[4]; asm volatile( "lqc2 vf4, 0x00(%1) \n\t" "lqc2 vf5, 0x10(%1) \n\t" @@ -68,7 +69,8 @@ Vector3 Matrix::operator*(const Vector3 &v) const "vmaddw.xyzw vf9, vf7, vf8 \n\t" "sqc2 vf9, 0x00(%0) \n\t" : - : "r"(result.xyz), "r"(this->data), "r"(v.xyz)); + : "r"(res), "r"(this->data), "r"(a)); + return Vector3(res[0], res[1], res[2]); } // ---- From 8589a298da3a7effebf57d590d6d681bc70a88d1 Mon Sep 17 00:00:00 2001 From: h4570 Date: Thu, 24 Dec 2020 10:33:30 +0100 Subject: [PATCH 093/108] added comments, increased performance --- src/engine/include/models/math/vector3.hpp | 99 ++++++-- src/engine/models/math/vector3.cpp | 281 ++++++++------------- 2 files changed, 180 insertions(+), 200 deletions(-) diff --git a/src/engine/include/models/math/vector3.hpp b/src/engine/include/models/math/vector3.hpp index 641f6b1..53939af 100644 --- a/src/engine/include/models/math/vector3.hpp +++ b/src/engine/include/models/math/vector3.hpp @@ -12,6 +12,7 @@ #define _TYRA_VECTOR3_ #include +#include class Math; // Forward definition @@ -20,6 +21,7 @@ class Vector3 { public: + /** This trick will allow use to use vec.x and vex[0] */ union { struct @@ -31,32 +33,91 @@ public: float xyz[3] __attribute__((__aligned__(16))); }; - Vector3(float t_x, float t_y, float t_z); - Vector3(const Vector3 &v); + // ---- + // Constructors/Destructors + // ---- + + /** Create empty vector. */ Vector3(); - ~Vector3(); - Vector3 operator+(Vector3 v); - Vector3 operator-(const Vector3 &v); - Vector3 operator*(Vector3 &v); - Vector3 operator*(const float &t); + /** Create vector with given vector values. */ + Vector3(const Vector3 &t_v) { set(t_v); } + + /** Create vector with given values. */ + Vector3(const float &t_x, const float &t_y, const float &t_z) { set(t_x, t_y, t_z); } + + // ---- + // Operators + // ---- + + Vector3 operator+(const Vector3 &v) const; + Vector3 operator-(const Vector3 &v) const; + /** Also called "cross product" */ + Vector3 operator*(const Vector3 &v) const; + Vector3 operator*(const float &t) const; + Vector3 operator/(const float &t) const; + Vector3 operator-(void) const { return Vector3(-x, -y, -z); } + void operator+=(const Vector3 &t); void operator*=(const float &t); - Vector3 operator/(float t); - Vector3 operator-(void); - static u8 shouldBeBackfaceCulled(const Vector3 *t_cameraPos, const Vector3 *v0, const Vector3 *v1, const Vector3 *v2); - u8 collidesSquare(const Vector3 &t_min, const Vector3 &t_max) const; - u8 isOnSquare(const Vector3 &t_min, const Vector3 &t_max) const; - float length(); - void normalize(); - void setByLerp(const Vector3 &v1, const Vector3 &v2, const float &t_interp, const float &t_scale); - float innerProduct(Vector3 &v); + // ---- + // Functions + // ---- + + /** Set vector values via VU0. */ void set(const Vector3 &v); - void rotate(const Vector3 &v, u8 inversed = false); + + /** Set vector values. */ void set(const float &t_x, const float &t_y, const float &t_z); - void copy(Vector3 &v); + + /** Also called dot3. */ + float innerProduct(const Vector3 &v) const; + + /** Get vector length */ + float length() const; + + /** Normalize vector. */ + void normalize(); + + /** Returns distance between two vectors */ float distanceTo(const Vector3 &v) const; - const void print() const; + + /** + * Checks intersection with given box + * @param t_min Opposite min vertex (ex. near, left, down vertex of bounding box) + * @param t_max Opposite max vertex (ex. far, right, up vertex of bounding box) + */ + u8 collidesBox(const Vector3 &t_min, const Vector3 &t_max) const + { + return ((this->x <= t_max.x && this->x >= t_min.x) && (this->y < t_max.y && this->y >= t_min.y) && (this->z <= t_max.z && this->z >= t_min.z)) ? 1 : 0; + } + + /** + * Checks if this vector is on given box (this->y >= box.y) + * @param t_min Opposite min vertex (ex. near, left, down vertex of bounding box) + * @param t_max Opposite max vertex (ex. far, right, up vertex of bounding box) + */ + u8 isOnBox(const Vector3 &t_min, const Vector3 &t_max) const + { + return ((this->x <= t_max.x && this->x >= t_min.x) && (this->y >= t_max.y) && (this->z <= t_max.z && this->z >= t_min.z)) ? 1 : 0; + } + + /** Check if given triangle should be backface culled. */ + static u8 shouldBeBackfaceCulled(const Vector3 *t_cameraPos, const Vector3 *t_v0, const Vector3 *t_v1, const Vector3 *t_v2); + + /** + * Set by linear interpolation between two vertices (animation). + * @param t_v1 Before vertex + * @param t_v2 After vertex + * @param t_interp State of interpolation + * @param t_scale Scale + */ + void setByLerp(const Vector3 &t_v1, const Vector3 &t_v2, const float &t_interp, const float &t_scale); + + const void print() const + { + printf("Vector3(%f, %f, %f)\n", x, y, z); + } }; #endif diff --git a/src/engine/models/math/vector3.cpp b/src/engine/models/math/vector3.cpp index 7adafd3..57f4303 100644 --- a/src/engine/models/math/vector3.cpp +++ b/src/engine/models/math/vector3.cpp @@ -9,63 +9,24 @@ */ #include "../../include/models/math/vector3.hpp" - #include "../../include/utils/math.hpp" -#include // ---- // Constructors/Destructors // ---- -/** Create by specifying 3 points */ -Vector3::Vector3(float t_x, float t_y, float t_z) -{ - x = t_x; - y = t_y; - z = t_z; -} - -/** Create with another vector values */ -Vector3::Vector3(const Vector3 &another) -{ - x = another.x; - y = another.y; - z = another.z; -} - -void Vector3::setByLerp(const Vector3 &v1, const Vector3 &v2, const float &t_interp, const float &t_scale) -{ - asm volatile( - "lqc2 vf4, 0x0(%1) \n\t" // vf4 = v1 - "lqc2 vf5, 0x0(%2) \n\t" // vf5 = v2 - "mfc1 $8, %3 \n\t" // vf6 = t - "qmtc2 $8, vf6 \n\t" // lerp: - "vsub.xyz vf7, vf5, vf4 \n\t" // vf7 = v2 - v1 - "vmulx.xyz vf8, vf7, vf6 \n\t" // vf8 = vf7 * t - "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)); - x *= t_scale; - y *= t_scale; - z *= t_scale; -} - -/** Create empty vector */ Vector3::Vector3() { - x = 0; - y = 0; - z = 0; + x = 0.0F; + y = 0.0F; + z = 0.0F; } -Vector3::~Vector3() {} - // ---- -// Methods +// Operators // ---- -Vector3 Vector3::operator+(Vector3 v) +Vector3 Vector3::operator+(const Vector3 &v) const { Vector3 result; asm volatile( // VU0 Macro program @@ -78,7 +39,7 @@ Vector3 Vector3::operator+(Vector3 v) return result; } -Vector3 Vector3::operator-(const Vector3 &v) +Vector3 Vector3::operator-(const Vector3 &v) const { Vector3 result; asm volatile( // VU0 Macro program @@ -91,17 +52,7 @@ Vector3 Vector3::operator-(const Vector3 &v) return result; } -Vector3 Vector3::operator-(void) -{ - Vector3 result; - result.x = -x; - result.y = -y; - result.z = -z; - return result; -} - -/** Also called "cross product" */ -Vector3 Vector3::operator*(Vector3 &v) +Vector3 Vector3::operator*(const Vector3 &v) const { Vector3 res; asm volatile( // VU0 Macro program @@ -119,7 +70,7 @@ Vector3 Vector3::operator*(Vector3 &v) return res; } -Vector3 Vector3::operator*(const float &t) +Vector3 Vector3::operator*(const float &t) const { Vector3 result; asm volatile( @@ -133,6 +84,26 @@ Vector3 Vector3::operator*(const float &t) return result; } +Vector3 Vector3::operator/(const float &t) const +{ + Vector3 result; + result.x = x / t; + result.y = y / t; + result.z = z / t; + return result; +} + +void Vector3::operator+=(const Vector3 &t) +{ + asm volatile( // VU0 Macro program + "lqc2 vf4, 0x0(%0) \n\t" + "lqc2 vf5, 0x0(%1) \n\t" + "vadd.xyz vf4, vf4, vf5 \n\t" + "sqc2 vf4, 0x0(%0) \n\t" + : + : "r"(this->xyz), "r"(t.xyz)); +} + void Vector3::operator*=(const float &t) { asm volatile( @@ -145,54 +116,46 @@ void Vector3::operator*=(const float &t) : "r"(this->xyz), "f"(t)); } -Vector3 Vector3::operator/(float t) +// ---- +// Functions +// ---- + +void Vector3::set(const Vector3 &v) { - Vector3 result; - result.x = x / t; - result.y = y / t; - result.z = z / t; + asm volatile( // VU0 Macro program + "lq $6, 0x0(%1) \n\t" + "sq $6, 0x0(%0) \n\t" + : + : "r"(this->xyz), "r"(v.xyz)); +} + +void Vector3::set(const float &t_x, const float &t_y, const float &t_z) +{ + x = t_x; + y = t_y; + z = t_z; +} + +float Vector3::innerProduct(const Vector3 &v) const +{ + float result; + asm volatile( // VU0 Macro program + "lqc2 vf4, 0x0(%1) \n\t" + "lqc2 vf5, 0x0(%2) \n\t" + "vmul.xyz vf6, vf4, vf5 \n\t" + "vaddy.x vf6, vf6, vf6 \n\t" + "vaddz.x vf6, vf6, vf6 \n\t" + "qmfc2 $2, vf6 \n\t" + "mtc1 $2, %0 \n\t" + : "=f"(result) + : "r"(this->xyz), "r"(v.xyz)); return result; + // return (x * v.x + y * v.y + z * v.z); } -u8 Vector3::shouldBeBackfaceCulled(const Vector3 *t_cameraPos, const Vector3 *v0, const Vector3 *v1, const Vector3 *v2) +float Vector3::length() const { - register float dot; - asm volatile( - "lqc2 vf4, 0x0(%1) \n\t" // vf4 = cameraPos - "lqc2 vf5, 0x0(%2) \n\t" // vf5 = v0 - "lqc2 vf6, 0x0(%3) \n\t" // vf6 = v1 - "lqc2 vf7, 0x0(%4) \n\t" // vf7 = v2 - "vsub.xyz vf8, vf7, vf5 \n\t" // vf8 = vf7(v2) - vf5(v0) - "vsub.xyz vf9, vf6, vf5 \n\t" // vf9 = vf6(v1) - vf5(v0) - "vopmula.xyz ACC, vf8, vf9 \n\t" // vf6 = cross(vf8, vf9) - "vopmsub.xyz vf6, vf9, vf8 \n\t" - "vsub.w vf6, vf6, vf6 \n\t" - "vsub.xyz vf7, vf5, vf4 \n\t" // vf7 = vf5(v0) - vf4(cameraPos) - "vmul.xyz vf5, vf7, vf6 \n\t" // vf5 = dot(vf7, vf6) - "vaddy.x vf5, vf5, vf5 \n\t" - "vaddz.x vf5, vf5, vf5 \n\t" - "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)); - return dot <= 0.0F; -} - -/** Checks intersection with given square */ -u8 Vector3::collidesSquare(const Vector3 &t_min, const Vector3 &t_max) const -{ - return ((this->x <= t_max.x && this->x >= t_min.x) && (this->y < t_max.y && this->y >= t_min.y) && (this->z <= t_max.z && this->z >= t_min.z)) ? 1 : 0; -} - -/** Checks is this vector is on given square */ -u8 Vector3::isOnSquare(const Vector3 &t_min, const Vector3 &t_max) const -{ - return ((this->x <= t_max.x && this->x >= t_min.x) && (this->y >= t_max.y) && (this->z <= t_max.z && this->z >= t_min.z)) ? 1 : 0; -} - -float Vector3::length() -{ - register float result; + float result; asm volatile( // VU0 Macro program "lqc2 vf4, 0x0(%1) \n\t" "vmul.xyz vf5, vf4, vf4 \n\t" @@ -227,85 +190,6 @@ void Vector3::normalize() : "r"(this->xyz)); } -/** Also called dot3 */ -float Vector3::innerProduct(Vector3 &v) -{ - register float result; - asm volatile( // VU0 Macro program - "lqc2 vf4, 0x0(%1) \n\t" - "lqc2 vf5, 0x0(%2) \n\t" - "vmul.xyz vf6, vf4, vf5 \n\t" - "vaddy.x vf6, vf6, vf6 \n\t" - "vaddz.x vf6, vf6, vf6 \n\t" - "qmfc2 $2, vf6 \n\t" - "mtc1 $2, %0 \n\t" - : "=f"(result) - : "r"(this->xyz), "r"(v.xyz)); - return result; - // return (x * v.x + y * v.y + z * v.z); -} - -void Vector3::set(const float &t_x, const float &t_y, const float &t_z) -{ - x = t_x; - y = t_y; - z = t_z; -} - -void Vector3::rotate(const Vector3 &v, u8 inversed) -{ - VECTOR cameraPos = {x, y, z, 0.0F}; - VECTOR rotation; - if (inversed) - { - rotation[0] = -v.x; - rotation[1] = -v.y; - rotation[2] = -v.z; - } - else - { - rotation[0] = v.x; - rotation[1] = v.y; - rotation[2] = v.z; - } - rotation[3] = 0.0F; - - MATRIX rotationMatrix; - matrix_unit(rotationMatrix); - matrix_rotate(rotationMatrix, rotationMatrix, rotation); - VECTOR result; - asm volatile( - "lqc2 vf4, 0x0(%1) \n\t" - "lqc2 vf5, 0x10(%1) \n\t" - "lqc2 vf6, 0x20(%1) \n\t" - "lqc2 vf7, 0x30(%1) \n\t" - "lqc2 vf8, 0x0(%2) \n\t" - "vmulax.xyzw ACC, vf4, vf8 \n\t" - "vmadday.xyzw ACC, vf5, vf8 \n\t" - "vmaddaz.xyzw ACC, vf6, vf8 \n\t" - "vmaddw.xyzw vf9, vf7, vf8 \n\t" - "sqc2 vf9, 0x0(%0) \n\t" - : - : "r"(&result), "r"(&rotationMatrix), "r"(&cameraPos)); - set(Vector3(result[0], result[1], result[2])); -} - -void Vector3::set(const Vector3 &v) -{ - this->x = v.x; - this->y = v.y; - this->z = v.z; -} - -void Vector3::copy(Vector3 &v) -{ - asm volatile( // VU0 Macro program - "lq $6, 0x0(%1) \n\t" - "sq $6, 0x0(%0) \n\t" - : - : "r"(v.xyz), "r"(this->xyz)); -} - float Vector3::distanceTo(const Vector3 &v) const { register float result; @@ -329,7 +213,42 @@ float Vector3::distanceTo(const Vector3 &v) const // (this->z - v.z) * (this->z - v.z)); } -const void Vector3::print() const +u8 Vector3::shouldBeBackfaceCulled(const Vector3 *t_cameraPos, const Vector3 *t_v0, const Vector3 *t_v1, const Vector3 *t_v2) { - printf("Vector3(%f, %f, %f)\n", x, y, z); + register float dot; + asm volatile( + "lqc2 vf4, 0x0(%1) \n\t" // vf4 = cameraPos + "lqc2 vf5, 0x0(%2) \n\t" // vf5 = v0 + "lqc2 vf6, 0x0(%3) \n\t" // vf6 = v1 + "lqc2 vf7, 0x0(%4) \n\t" // vf7 = v2 + "vsub.xyz vf8, vf7, vf5 \n\t" // vf8 = vf7(v2) - vf5(v0) + "vsub.xyz vf9, vf6, vf5 \n\t" // vf9 = vf6(v1) - vf5(v0) + "vopmula.xyz ACC, vf8, vf9 \n\t" // vf6 = cross(vf8, vf9) + "vopmsub.xyz vf6, vf9, vf8 \n\t" + "vsub.w vf6, vf6, vf6 \n\t" + "vsub.xyz vf7, vf5, vf4 \n\t" // vf7 = vf5(v0) - vf4(cameraPos) + "vmul.xyz vf5, vf7, vf6 \n\t" // vf5 = dot(vf7, vf6) + "vaddy.x vf5, vf5, vf5 \n\t" + "vaddz.x vf5, vf5, vf5 \n\t" + "qmfc2 $2, vf5 \n\t" // store result on `dot` variable + "mtc1 $2, %0 \n\t" + : "=f"(dot) + : "r"(t_cameraPos->xyz), "r"(t_v0->xyz), "r"(t_v1->xyz), "r"(t_v2->xyz)); + return dot <= 0.0F; +} + +void Vector3::setByLerp(const Vector3 &t_v1, const Vector3 &t_v2, const float &t_interp, const float &t_scale) +{ + asm volatile( + "lqc2 vf4, 0x0(%1) \n\t" // vf4 = v1 + "lqc2 vf5, 0x0(%2) \n\t" // vf5 = v2 + "mfc1 $8, %3 \n\t" // vf6 = t + "qmtc2 $8, vf6 \n\t" // lerp: + "vsub.xyz vf7, vf5, vf4 \n\t" // vf7 = v2 - v1 + "vmulx.xyz vf8, vf7, vf6 \n\t" // vf8 = vf7 * t + "vadd.xyz vf9, vf8, vf4 \n\t" // vf9 = vf8 + vf4 + "sqc2 vf9, 0x0(%0) \n\t" // v0 = vf9 + : + : "r"(&this->xyz), "r"(&t_v1.xyz), "r"(&t_v2.xyz), "f"(t_interp)); + operator*=(t_scale); } From e39c8c5de88feaa255729e2b2515e5be75f9a26d Mon Sep 17 00:00:00 2001 From: h4570 Date: Thu, 24 Dec 2020 10:34:56 +0100 Subject: [PATCH 094/108] changes to match latest matrix/vector3 api --- src/engine/include/modules/camera_base.hpp | 5 +++-- src/engine/include/modules/renderer.hpp | 2 +- src/engine/modules/camera_base.cpp | 8 ++++---- src/engine/modules/renderer.cpp | 4 ++-- src/samples/dolphin/camera.cpp | 5 +---- src/samples/dolphin/camera.hpp | 3 +-- src/samples/floors/camera.cpp | 5 +---- src/samples/floors/camera.hpp | 1 - src/samples/floors/objects/player.cpp | 4 ++-- 9 files changed, 15 insertions(+), 22 deletions(-) diff --git a/src/engine/include/modules/camera_base.hpp b/src/engine/include/modules/camera_base.hpp index 342acc7..3cefcad 100644 --- a/src/engine/include/modules/camera_base.hpp +++ b/src/engine/include/modules/camera_base.hpp @@ -21,7 +21,7 @@ class CameraBase { public: - CameraBase(ScreenSettings *t_screen, Vector3 *t_position, Vector3 *t_up); + CameraBase(ScreenSettings *t_screen, Vector3 *t_position); virtual ~CameraBase(){}; /** @@ -56,9 +56,10 @@ protected: void updatePlanes(Vector3 t_target); private: - Vector3 *p_position, *p_up; + Vector3 *p_position; float farPlaneDist, nearPlaneDist, nearHeight, nearWidth, farHeight, farWidth; Vector3 ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr; + Vector3 up; // always 0.0F, 1.0F, 0.0F }; #endif diff --git a/src/engine/include/modules/renderer.hpp b/src/engine/include/modules/renderer.hpp index d2cdb76..1ad7acb 100644 --- a/src/engine/include/modules/renderer.hpp +++ b/src/engine/include/modules/renderer.hpp @@ -125,7 +125,7 @@ private: void flipBuffers(); void beginFrameIfNeeded(); u8 isFrameEmpty; - Matrix perspective; + Matrix perspective, camRotation; Light light; RenderData renderData; TextureRepository textureRepo; diff --git a/src/engine/modules/camera_base.cpp b/src/engine/modules/camera_base.cpp index 124ee77..9576306 100644 --- a/src/engine/modules/camera_base.cpp +++ b/src/engine/modules/camera_base.cpp @@ -18,7 +18,7 @@ // Constructors/Destructors // ---- -CameraBase::CameraBase(ScreenSettings *t_screen, Vector3 *t_position, Vector3 *t_up) +CameraBase::CameraBase(ScreenSettings *t_screen, Vector3 *t_position) : screen(t_screen) { PRINT_LOG("Initializing frustum"); @@ -30,7 +30,7 @@ CameraBase::CameraBase(ScreenSettings *t_screen, Vector3 *t_position, Vector3 *t farHeight = tang * farPlaneDist; farWidth = farHeight * screen->aspectRatio; p_position = t_position; - p_up = t_up; + up.set(0.0F, 1.0F, 0.0F); PRINT_LOG("CameraBase initialized!"); } @@ -41,7 +41,7 @@ CameraBase::CameraBase(ScreenSettings *t_screen, Vector3 *t_position, Vector3 *t void CameraBase::lookAt(Vector3 &t_target) { updatePlanes(t_target); - worldView.lookAt(*p_up, *p_position, t_target); + worldView.lookAt(*p_position, t_target); } void CameraBase::updatePlanes(Vector3 t_target) @@ -51,7 +51,7 @@ void CameraBase::updatePlanes(Vector3 t_target) Z = *p_position - t_target; Z.normalize(); // X axis of camera of given "up" vector and Z axis - X = *p_up * Z; + X = up * Z; X.normalize(); // the real "up" vector is the cross product of Z and X Y = Z * X; diff --git a/src/engine/modules/renderer.cpp b/src/engine/modules/renderer.cpp index 02cadff..1a7cc2d 100644 --- a/src/engine/modules/renderer.cpp +++ b/src/engine/modules/renderer.cpp @@ -277,8 +277,8 @@ void Renderer::draw(Mesh &t_mesh, LightBulb *t_bulbs, u16 t_bulbsCount) if (!t_mesh.isDataLoaded()) PRINT_ERR("Can't draw, because no mesh data was loaded!"); - Vector3 rotatedCamera = Vector3(*renderData.cameraPosition); - rotatedCamera.rotate(t_mesh.rotation, true); + camRotation.rotation(-t_mesh.rotation); + Vector3 rotatedCamera = Vector3(camRotation * *renderData.cameraPosition); if (t_mesh.getCurrentAnimationFrame() != t_mesh.getNextAnimationFrame()) t_mesh.animate(); diff --git a/src/samples/dolphin/camera.cpp b/src/samples/dolphin/camera.cpp index 5c122f7..855428f 100755 --- a/src/samples/dolphin/camera.cpp +++ b/src/samples/dolphin/camera.cpp @@ -14,12 +14,9 @@ const float CAMERA_Y = 15.0F; -Camera::Camera(ScreenSettings *t_screen) : CameraBase(t_screen, &position, &up) +Camera::Camera(ScreenSettings *t_screen) : CameraBase(t_screen, &position) { verticalLevel = 30.0F; - up.x = 0.0F; - up.y = 1.0F; - up.z = 0.0F; } Camera::~Camera() {} diff --git a/src/samples/dolphin/camera.hpp b/src/samples/dolphin/camera.hpp index 7bcbde3..d13d38c 100755 --- a/src/samples/dolphin/camera.hpp +++ b/src/samples/dolphin/camera.hpp @@ -22,7 +22,7 @@ class Camera : public CameraBase { public: - Vector3 position,up, unitCirclePosition; + Vector3 position, up, unitCirclePosition; float horizontalLevel, verticalLevel; Camera(ScreenSettings *t_screen); @@ -34,7 +34,6 @@ public: protected: Vector3 *getPosition() { return &position; }; - Vector3 *getUp() { return &up; }; ScreenSettings screen; }; diff --git a/src/samples/floors/camera.cpp b/src/samples/floors/camera.cpp index 56c4aa1..e797c0f 100644 --- a/src/samples/floors/camera.cpp +++ b/src/samples/floors/camera.cpp @@ -19,13 +19,10 @@ const float CAMERA_Y = 30.0F; // Constructors/Destructors // ---- -Camera::Camera(ScreenSettings *t_screen) : CameraBase(t_screen, &position, &up) +Camera::Camera(ScreenSettings *t_screen) : CameraBase(t_screen, &position) { PRINT_LOG("Initializing camera"); verticalLevel = 80.0F; - up.x = 0.0F; - up.y = 1.0F; - up.z = 0.0F; PRINT_LOG("Camera initialized!"); } diff --git a/src/samples/floors/camera.hpp b/src/samples/floors/camera.hpp index 4d6adf8..49735d2 100644 --- a/src/samples/floors/camera.hpp +++ b/src/samples/floors/camera.hpp @@ -35,7 +35,6 @@ public: protected: Vector3 *getPosition() { return &position; }; - Vector3 *getUp() { return &up; }; ScreenSettings screen; }; diff --git a/src/samples/floors/objects/player.cpp b/src/samples/floors/objects/player.cpp index 6d6ae79..738d03a 100644 --- a/src/samples/floors/objects/player.cpp +++ b/src/samples/floors/objects/player.cpp @@ -211,13 +211,13 @@ FloorsCheck *Player::checkFloors(const FloorManager &t_floorManager, const Vecto for (u32 i = 0; i < t_floorManager.floorAmount; i++) { Utils::getMinMax(t_floorManager.floors[i].mesh, min, max); - if (result->currentFloor == NULL && this->mesh.position.isOnSquare(min, max)) + if (result->currentFloor == NULL && this->mesh.position.isOnBox(min, max)) { result->currentFloor = &t_floorManager.floors[i]; result->currFloorMin.set(min); result->currFloorMax.set(max); } - if (result->willCollideFloor == NULL && t_nextPos.collidesSquare(min, max)) + if (result->willCollideFloor == NULL && t_nextPos.collidesBox(min, max)) { result->willCollideFloor = &t_floorManager.floors[i]; result->willCollideFloorMin.set(min); From 91934df5e884dbbfaf68ad85f902f443152f6cc1 Mon Sep 17 00:00:00 2001 From: h4570 Date: Thu, 24 Dec 2020 10:35:04 +0100 Subject: [PATCH 095/108] fixed warning --- src/engine/models/bounding_box.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/engine/models/bounding_box.cpp b/src/engine/models/bounding_box.cpp index dc90364..2b22fc9 100644 --- a/src/engine/models/bounding_box.cpp +++ b/src/engine/models/bounding_box.cpp @@ -46,4 +46,4 @@ BoundingBox::BoundingBox(Vector3 *t_vertices) BoundingBox::~BoundingBox() { -} \ No newline at end of file +} From b4fe16dcccfe38c025b1f0cfee72f2251bd96b37 Mon Sep 17 00:00:00 2001 From: h4570 Date: Thu, 24 Dec 2020 12:45:09 +0100 Subject: [PATCH 096/108] added manual calcs of model-view-proj matrix --- src/engine/engine.cpp | 4 +- src/engine/include/models/math/matrix.hpp | 88 ++++++++++-------- src/engine/include/models/render_data.hpp | 16 +++- src/engine/include/modules/camera_base.hpp | 6 +- src/engine/include/modules/vif_sender.hpp | 10 ++ src/engine/models/math/matrix.cpp | 101 ++++++++++----------- src/engine/modules/camera_base.cpp | 2 +- src/engine/modules/gif_sender.cpp | 16 +--- src/engine/modules/renderer.cpp | 13 +-- src/engine/modules/vif_sender.cpp | 26 +++--- src/samples/dolphin/dolphin.cpp | 2 +- src/samples/floors/floors.cpp | 4 +- src/samples/floors/objects/enemy.cpp | 3 + 13 files changed, 156 insertions(+), 135 deletions(-) diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index 4a604ba..e9ce377 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -41,13 +41,13 @@ Engine::~Engine() {} void Engine::setDefaultScreen() { - screen.projectionScale = 4096.0F; screen.nearPlaneDist = 2.0F; screen.farPlaneDist = 2000.0F; screen.fov = 60.0F; - screen.aspectRatio = 4.0F / 3.0F; screen.width = 640.0F; screen.height = 480.0F; + screen.aspectRatio = screen.width / screen.height; + screen.projectionScale = 4096.0F; // PS2 have 4k drawing area } void Engine::init(Game *t_game, u32 t_gifPacketSize) diff --git a/src/engine/include/models/math/matrix.hpp b/src/engine/include/models/math/matrix.hpp index d14855b..46b5140 100644 --- a/src/engine/include/models/math/matrix.hpp +++ b/src/engine/include/models/math/matrix.hpp @@ -85,58 +85,70 @@ public: */ inline void unit() { identity(); }; - /** Create translation matrix. */ - inline void translation(const Vector3 &t_val) + /** Translate matrix. */ + inline void translate(const Vector3 &t_val) { - identity(); - translate(t_val); + Matrix temp = Matrix(true); + temp.translation(t_val); + cross(this->data, temp.data, this->data); } - /** Create rotation matrix. */ - inline void rotation(const Vector3 &t_val) + /** Rotate matrix. */ + inline void rotate(const Vector3 &t_val) { - identity(); - Matrix temp = Matrix(); + Matrix temp = Matrix(true); + + temp.rotationZ(t_val.z); + cross(this->data, temp.data, this->data); temp.identity(); - temp.rotateX(t_val.x); - operator*=(temp); + temp.rotationY(t_val.y); + cross(this->data, temp.data, this->data); temp.identity(); - temp.rotateY(t_val.y); - operator*=(temp); - - temp.identity(); - temp.rotateZ(t_val.z); - operator*=(temp); + temp.rotationX(t_val.x); + cross(this->data, temp.data, this->data); } - /** Create X rotation matrix. */ - inline void rotationX(const float &t_radians) + /** Rotate matrix by X. */ + inline void rotateX(const float &t_radians) { - identity(); - rotateX(t_radians); + Matrix temp = Matrix(true); + temp.rotationX(t_radians); + cross(this->data, temp.data, this->data); } - /** Create Y rotation matrix. */ - inline void rotationY(const float &t_radians) + /** Rotate matrix by Y. */ + inline void rotateY(const float &t_radians) { - identity(); - rotateY(t_radians); + Matrix temp = Matrix(true); + temp.rotationY(t_radians); + cross(this->data, temp.data, this->data); } - /** Create Z rotation matrix. */ - inline void rotationZ(const float &t_radians) + /** Rotate matrix by Z. */ + inline void rotateZ(const float &t_radians) { - identity(); - rotateZ(t_radians); + Matrix temp = Matrix(true); + temp.rotationZ(t_radians); + cross(this->data, temp.data, this->data); } - /** Create angle rotation matrix. */ - void rotationByAngle(const float &t_angle, const Vector3 &t_axis); + /** Rotate matrix by angle. */ + void rotateByAngle(const float &t_angle, const Vector3 &t_axis) + { + Matrix temp; + temp.rotationByAngle(t_angle, t_axis); + cross(this->data, temp.data, this->data); + } - /** Create scale matrix. */ - void setScale(const Vector3 &t_val); + /** Scale matrix. */ + void scale(const Vector3 &t_val) + { + Matrix temp = Matrix(true); + temp.setScale(t_val); + cross(this->data, temp.data, this->data); + } /** * Create perspective projection matrix. @@ -161,12 +173,14 @@ public: const void print() const; private: - void rotateX(const float &t_radians); - void rotateY(const float &t_radians); - void rotateZ(const float &t_radians); - void translate(const Vector3 &t_val); - void cross(float res[16], const float a[16], const float b[16]) const; + void rotationX(const float &t_radians); + void rotationY(const float &t_radians); + void rotationZ(const float &t_radians); + void rotationByAngle(const float &t_angle, const Vector3 &t_axis); + void translation(const Vector3 &t_val); + void setScale(const Vector3 &t_val); void setCamera(const float t_pos[4], const float t_vz[4], const float t_vy[4]); + void cross(float res[16], const float a[16], const float b[16]) const; }; #endif diff --git a/src/engine/include/models/render_data.hpp b/src/engine/include/models/render_data.hpp index 4045fd8..f4a89c7 100644 --- a/src/engine/include/models/render_data.hpp +++ b/src/engine/include/models/render_data.hpp @@ -22,10 +22,18 @@ struct RenderData { LightBulb *bulbs; u16 bulbsCount; - /** Camera (lookAt) */ - Matrix *worldView; - /** Perspective projection */ - Matrix *perspective; + /** + * Camera matrix (glLookAt) + * OpenGL name: View + * Sony name: World view + */ + Matrix *view; + /** + * Projection matrix (glPerspective) + * OpenGL name: Projection + * Sony name: View screen + */ + Matrix *projection; Vector3 *cameraPosition; Plane *frustumPlanes; prim_t *prim; diff --git a/src/engine/include/modules/camera_base.hpp b/src/engine/include/modules/camera_base.hpp index 3cefcad..b74ff89 100644 --- a/src/engine/include/modules/camera_base.hpp +++ b/src/engine/include/modules/camera_base.hpp @@ -31,10 +31,10 @@ public: Plane planes[6]; /** - * Projection matrix + * View matrix * Set by lookAt() method. */ - Matrix worldView; + Matrix view; protected: /** Pointer to screen settings */ @@ -42,7 +42,7 @@ protected: /** * Adaption of OpenGL lookAt camera. - * Calculates worldView matrix and update frustum planes + * Calculates view matrix and update frustum planes * Should be called every frame. */ void lookAt(Vector3 &t_target); diff --git a/src/engine/include/modules/vif_sender.hpp b/src/engine/include/modules/vif_sender.hpp index a3e1e36..53153c5 100644 --- a/src/engine/include/modules/vif_sender.hpp +++ b/src/engine/include/modules/vif_sender.hpp @@ -39,6 +39,16 @@ private: void drawVertices(Mesh &t_mesh, u32 t_start, u32 t_end, VECTOR *t_vertices, VECTOR *t_coordinates, prim_t *t_prim, texbuffer_t *textureBuffer); packet2_t *packets[2] __attribute__((aligned(64))); packet2_t *currPacket; + /** + * OpenGL name: Model + * Sony name: local world + */ + Matrix model; + /** + * OpenGL name: ModelViewProjection + * Sony name: local screen + */ + Matrix modelViewProj; u8 context; packet2_t *matricesPacket __attribute__((aligned(64))); VECTOR position, rotation; diff --git a/src/engine/models/math/matrix.cpp b/src/engine/models/math/matrix.cpp index 805c433..41896a5 100644 --- a/src/engine/models/math/matrix.cpp +++ b/src/engine/models/math/matrix.cpp @@ -93,47 +93,6 @@ void Matrix::identity() : "r"(this->data)); } -void Matrix::rotationByAngle(const float &t_angle, const Vector3 &t_axis) -{ - Vector3 localAxis = Vector3(t_axis); - localAxis.normalize(); - float x = localAxis.x; - float y = localAxis.y; - float z = localAxis.z; - - float c = Math::cos(t_angle); - float s = Math::sin(t_angle); - - this->data[0] = x * x * (1 - c) + c; - this->data[1] = y * x * (1 - c) + z * s; - this->data[2] = x * z * (1 - c) - y * s; - this->data[3] = 0.0F; - - this->data[4] = x * y * (1 - c) - z * s; - this->data[5] = y * y * (1 - c) + c; - this->data[6] = y * z * (1 - c) + x * s; - this->data[7] = 0.0F; - - this->data[8] = x * z * (1 - c) + y * s; - this->data[9] = y * z * (1 - c) - x * s; - this->data[10] = z * z * (1 - c) + c; - this->data[11] = 0.0F; - - this->data[12] = 0.0F; - this->data[13] = 0.0F; - this->data[14] = 0.0F; - this->data[15] = 1.0F; -} - -void Matrix::setScale(const Vector3 &t_val) -{ - this->identity(); - this->data[0] = t_val.x; - this->data[5] = t_val.y; - this->data[10] = t_val.z; - this->data[15] = 1.0F; -} - void Matrix::setPerspective(const ScreenSettings &t_screen) { float fovYdiv2 = Math::HALF_ANG2RAD * t_screen.fov; @@ -232,10 +191,8 @@ const void Matrix::print() const // Private // ---- -void Matrix::rotateX(const float &t_radians) +void Matrix::rotationX(const float &t_radians) { - Matrix temp = Matrix(); - temp.identity(); float c = Math::cos(t_radians); float s = Math::sin(t_radians); this->data[5] = c; // 1,1 @@ -244,10 +201,8 @@ void Matrix::rotateX(const float &t_radians) this->data[10] = c; // 2,2 } -void Matrix::rotateY(const float &t_radians) +void Matrix::rotationY(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 @@ -256,10 +211,8 @@ void Matrix::rotateY(const float &t_radians) this->data[10] = c; // 2,2 } -void Matrix::rotateZ(const float &t_radians) +void Matrix::rotationZ(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 @@ -268,11 +221,51 @@ void Matrix::rotateZ(const float &t_radians) this->data[5] = c; // 1,1 } -void Matrix::translate(const Vector3 &t_val) +void Matrix::rotationByAngle(const float &t_angle, const Vector3 &t_axis) { - this->data[12] += t_val.x; // 3,0 - this->data[13] += t_val.y; // 3,1 - this->data[14] += t_val.z; // 3,2 + Vector3 localAxis = Vector3(t_axis); + localAxis.normalize(); + float x = localAxis.x; + float y = localAxis.y; + float z = localAxis.z; + + float c = Math::cos(t_angle); + float s = Math::sin(t_angle); + + this->data[0] = x * x * (1 - c) + c; + this->data[1] = y * x * (1 - c) + z * s; + this->data[2] = x * z * (1 - c) - y * s; + this->data[3] = 0.0F; + + this->data[4] = x * y * (1 - c) - z * s; + this->data[5] = y * y * (1 - c) + c; + this->data[6] = y * z * (1 - c) + x * s; + this->data[7] = 0.0F; + + this->data[8] = x * z * (1 - c) + y * s; + this->data[9] = y * z * (1 - c) - x * s; + this->data[10] = z * z * (1 - c) + c; + this->data[11] = 0.0F; + + this->data[12] = 0.0F; + this->data[13] = 0.0F; + this->data[14] = 0.0F; + this->data[15] = 1.0F; +} + +void Matrix::translation(const Vector3 &t_val) +{ + 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::setScale(const Vector3 &t_val) +{ + this->data[0] = t_val.x; + this->data[5] = t_val.y; + this->data[10] = t_val.z; + this->data[15] = 1.0F; } void Matrix::setCamera(const float t_pos[4], const float t_vz[4], const float t_vy[4]) diff --git a/src/engine/modules/camera_base.cpp b/src/engine/modules/camera_base.cpp index 9576306..8453012 100644 --- a/src/engine/modules/camera_base.cpp +++ b/src/engine/modules/camera_base.cpp @@ -41,7 +41,7 @@ CameraBase::CameraBase(ScreenSettings *t_screen, Vector3 *t_position) void CameraBase::lookAt(Vector3 &t_target) { updatePlanes(t_target); - worldView.lookAt(*p_position, t_target); + view.lookAt(*p_position, t_target); } void CameraBase::updatePlanes(Vector3 t_target) diff --git a/src/engine/modules/gif_sender.cpp b/src/engine/modules/gif_sender.cpp index 08dad58..ab55fc1 100644 --- a/src/engine/modules/gif_sender.cpp +++ b/src/engine/modules/gif_sender.cpp @@ -130,12 +130,7 @@ void GifSender::addClear(zbuffer_t *t_zBuffer, color_t *t_rgb) packet2_chain_close_tag(currentPacket); } -/** Adds 3D objects to current packet - * @param worldView Matrix - * @param perspective Matrix with .setPerpsective() used [clone of gluPerspective] - * @param objects3D Array of 3D objects pointers - * @param amount Amount of 3D objects - */ +/** Adds meshes to current packet */ void GifSender::addObject(RenderData *t_renderData, Mesh &t_mesh, u32 vertexCount, VECTOR *vertices, VECTOR *normals, VECTOR *coordinates, LightBulb *t_bulbs, u16 t_bulbsCount, texbuffer_t *textureBuffer) { packet2_chain_open_cnt(currentPacket, 0, 0, 0); @@ -145,7 +140,7 @@ void GifSender::addObject(RenderData *t_renderData, Mesh &t_mesh, u32 vertexCoun xyz = new xyz_t[vertexCount]; rgbaq = new color_t[vertexCount]; st = new texel_t[vertexCount]; - calc3DObject(*t_renderData->perspective, t_mesh, vertexCount, vertices, normals, coordinates, t_renderData, t_bulbs, t_bulbsCount); + calc3DObject(*t_renderData->projection, t_mesh, vertexCount, vertices, normals, coordinates, t_renderData, t_bulbs, t_bulbsCount); addCurrentCalcs(vertexCount); delete[] xyz; delete[] rgbaq; @@ -155,10 +150,7 @@ void GifSender::addObject(RenderData *t_renderData, Mesh &t_mesh, u32 vertexCoun } /** Calculates 3D object data into xyz, rgbq, st - * After it addCurrentSTQ() can be done - * @param worldView Matrix - * @param perspective Matrix with .setPerpsective() used [clone of gluPerspective] - * @param mesh 3D object + * After it addCurrentCalcs() can be done */ void GifSender::calc3DObject(Matrix t_perspective, Mesh &t_mesh, u32 vertexCount, VECTOR *vertices, VECTOR *normals, VECTOR *coordinates, RenderData *t_renderData, LightBulb *t_bulbs, u16 t_bulbsCount) { @@ -176,7 +168,7 @@ void GifSender::calc3DObject(Matrix t_perspective, Mesh &t_mesh, u32 vertexCount create_local_light(localLight, rotation); // I cant put perspective from renderData here. ee-gcc bug? - create_local_screen(localScreen, localWorld, t_renderData->worldView->data, t_perspective.data); + create_local_screen(localScreen, localWorld, t_renderData->view->data, t_perspective.data); if (SHOULD_BE_LIGHTED) { diff --git a/src/engine/modules/renderer.cpp b/src/engine/modules/renderer.cpp index 1a7cc2d..5ed1027 100644 --- a/src/engine/modules/renderer.cpp +++ b/src/engine/modules/renderer.cpp @@ -51,7 +51,7 @@ Renderer::Renderer(u32 t_packetSize, ScreenSettings *t_screen) gifSender = new GifSender(t_packetSize, t_screen, &light); vifSender = new VifSender(&light); perspective.setPerspective(*t_screen); - renderData.perspective = &perspective; + renderData.projection = &perspective; PRINT_LOG("Renderer initialized!"); } @@ -277,7 +277,8 @@ void Renderer::draw(Mesh &t_mesh, LightBulb *t_bulbs, u16 t_bulbsCount) if (!t_mesh.isDataLoaded()) PRINT_ERR("Can't draw, because no mesh data was loaded!"); - camRotation.rotation(-t_mesh.rotation); + camRotation.identity(); + camRotation.rotate(-t_mesh.rotation); Vector3 rotatedCamera = Vector3(camRotation * *renderData.cameraPosition); if (t_mesh.getCurrentAnimationFrame() != t_mesh.getNextAnimationFrame()) @@ -287,9 +288,9 @@ void Renderer::draw(Mesh &t_mesh, LightBulb *t_bulbs, u16 t_bulbsCount) if (t_mesh.shouldBeFrustumCulled && !t_mesh.getMaterial(i).isInFrustum(renderData.frustumPlanes, t_mesh.position)) return; u32 vertCount = t_mesh.getMaterial(i).getFacesCount(); - VECTOR __attribute__((aligned(16))) vertices[vertCount]; - VECTOR __attribute__((aligned(16))) normals[vertCount]; - VECTOR __attribute__((aligned(16))) coordinates[vertCount]; + VECTOR vertices[vertCount] __attribute__((aligned(16))); + VECTOR normals[vertCount] __attribute__((aligned(16))); + VECTOR coordinates[vertCount] __attribute__((aligned(16))); Texture *tex = textureRepo.getByMesh(t_mesh.getId(), t_mesh.getMaterial(i).getId()); changeTexture(tex); vertCount = t_mesh.getDrawData(i, vertices, normals, coordinates, rotatedCamera); @@ -305,7 +306,7 @@ void Renderer::draw(Mesh &t_mesh) { draw(t_mesh, NULL, 0); } void Renderer::setCameraDefinitions(Matrix *t_worldView, Vector3 *t_cameraPos, Plane *t_planes) { - renderData.worldView = t_worldView; + renderData.view = t_worldView; renderData.cameraPosition = t_cameraPos; renderData.frustumPlanes = t_planes; } diff --git a/src/engine/modules/vif_sender.cpp b/src/engine/modules/vif_sender.cpp index 2ef738a..2b7f20c 100644 --- a/src/engine/modules/vif_sender.cpp +++ b/src/engine/modules/vif_sender.cpp @@ -68,22 +68,22 @@ void VifSender::uploadMicroProgram() dma_channel_send_packet2(packet2, DMA_CHANNEL_VIF1, 1); packet2_free(packet2); } - +#include void VifSender::sendMatrices(const RenderData &t_renderData, const Vector3 &t_position, const Vector3 &t_rotation) { - 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) + + Matrix model; + model.identity(); + model.rotate(t_rotation); + model.translate(t_position); + + modelViewProj.identity(); + modelViewProj = model * modelViewProj; + modelViewProj = *t_renderData.view * modelViewProj; + modelViewProj = *t_renderData.projection * modelViewProj; + packet2_reset(matricesPacket, false); - // 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_unpack_data(matricesPacket, 0, &modelViewProj.data, 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/samples/dolphin/dolphin.cpp b/src/samples/dolphin/dolphin.cpp index 6471834..11f4032 100755 --- a/src/samples/dolphin/dolphin.cpp +++ b/src/samples/dolphin/dolphin.cpp @@ -34,7 +34,7 @@ Dolphin::~Dolphin() void Dolphin::onInit() { player.init(engine); - engine->renderer->setCameraDefinitions(&camera.worldView, &camera.position, camera.planes); + engine->renderer->setCameraDefinitions(&camera.view, &camera.position, camera.planes); engine->audio.loadSong("sound/dirediredocks.wav"); engine->audio.playSong(); diff --git a/src/samples/floors/floors.cpp b/src/samples/floors/floors.cpp index 2a55ab1..10e9872 100644 --- a/src/samples/floors/floors.cpp +++ b/src/samples/floors/floors.cpp @@ -38,7 +38,7 @@ Floors::~Floors() void Floors::onInit() { setBgColorAndAmbientColor(); - engine->renderer->setCameraDefinitions(&camera.worldView, &camera.unitCirclePosition, camera.planes); + engine->renderer->setCameraDefinitions(&camera.view, &camera.unitCirclePosition, camera.planes); engine->audio.addSongListener(this); engine->audio.loadSong("sounds/mafikizolo-loot.wav"); engine->audio.playSong(); @@ -61,7 +61,7 @@ void Floors::onUpdate() engine->renderer->draw(player->mesh); engine->renderer->draw(enemy->getMeshes(), enemy->getMeshesCount()); for (u16 i = 0; i < FLOORS_COUNT; i++) - engine->renderer->drawByPath3(floorManager->floors[i].mesh, lightManager.bulbs, lightManager.bulbsCount); + engine->renderer->draw(floorManager->floors[i].mesh, lightManager.bulbs, lightManager.bulbsCount); ui->render(engine->renderer); // 2D rendering ist LAST step, because layers gonna play there. } diff --git a/src/samples/floors/objects/enemy.cpp b/src/samples/floors/objects/enemy.cpp index 858e79c..b7a46a8 100644 --- a/src/samples/floors/objects/enemy.cpp +++ b/src/samples/floors/objects/enemy.cpp @@ -30,14 +30,17 @@ Enemy::Enemy(TextureRepository *t_texRepo) meshes[0].loadMD2("meshes/enemy/", "poss_head", 0.3F, false); meshes[0].rotation.x = -1.566F; meshes[0].rotation.z = 1.566F; + meshes[0].shouldBeFrustumCulled = false; meshes[1].loadMD2("meshes/enemy/", "poss_body", 0.3F, false); meshes[1].rotation.x = -1.566F; meshes[1].rotation.z = 1.566F; + meshes[1].shouldBeFrustumCulled = false; meshes[2].loadMD2("meshes/enemy/", "poss_weapon", 0.3F, false); meshes[2].rotation.x = -1.566F; meshes[2].rotation.z = 1.566F; + meshes[2].shouldBeFrustumCulled = false; Vector3 initPos = Vector3(0.00F, 40.00F, 0.00F); setPosition(initPos); From c0ece7206c4de180509f2730c653fca6339d6e81 Mon Sep 17 00:00:00 2001 From: h4570 Date: Thu, 24 Dec 2020 13:08:03 +0100 Subject: [PATCH 097/108] tweaked vu1 packets a bit --- src/engine/include/modules/renderer.hpp | 4 +-- src/engine/modules/renderer.cpp | 44 ++++++++++++++----------- src/engine/modules/vif_sender.cpp | 4 +-- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/engine/include/modules/renderer.hpp b/src/engine/include/modules/renderer.hpp index 1ad7acb..be874d9 100644 --- a/src/engine/include/modules/renderer.hpp +++ b/src/engine/include/modules/renderer.hpp @@ -134,8 +134,8 @@ private: 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 allocateBuffers(int t_screenW, int t_screenH); + void initDrawingEnv(); void setPrim(); }; diff --git a/src/engine/modules/renderer.cpp b/src/engine/modules/renderer.cpp index 5ed1027..2287b88 100644 --- a/src/engine/modules/renderer.cpp +++ b/src/engine/modules/renderer.cpp @@ -22,6 +22,9 @@ // Constructors/Destructors // ---- +static const float GS_CENTER = 4096.0F; +static const float SCREEN_CENTER = GS_CENTER / 2.0F; + /** Initialize DMA<->GIF channel * Allocate buffers * Initialize screen @@ -35,19 +38,19 @@ Renderer::Renderer(u32 t_packetSize, ScreenSettings *t_screen) PRINT_LOG("Initializing renderer"); dma_channel_initialize(DMA_CHANNEL_GIF, NULL, 0); // Initialize DMA to enable data transfer dma_channel_fast_waits(DMA_CHANNEL_GIF); + screen = t_screen; context = 0; isTextureVRAMAllocated = false; isVSyncEnabled = true; isFrameEmpty = false; lastTextureId = 0; flipPacket = packet2_create(4, P2_TYPE_UNCACHED_ACCL, P2_MODE_NORMAL, 0); - allocateBuffers(t_screen->width, t_screen->height); - initDrawingEnv(t_screen->width, t_screen->height); + allocateBuffers((int)t_screen->width, (int)t_screen->height); + initDrawingEnv(); setPrim(); worldColor.r = 0x10; worldColor.g = 0x10; worldColor.b = 0x10; - screen = t_screen; gifSender = new GifSender(t_packetSize, t_screen, &light); vifSender = new VifSender(&light); perspective.setPerspective(*t_screen); @@ -142,7 +145,7 @@ void Renderer::draw(Sprite &t_sprite) beginFrameIfNeeded(); changeTexture(texture); packet2_t *packet2 = packet2_create(12, P2_TYPE_NORMAL, P2_MODE_NORMAL, 0); - packet2_update(packet2, draw_primitive_xyoffset(packet2->next, 0, 2048, 2048)); + packet2_update(packet2, draw_primitive_xyoffset(packet2->next, 0, SCREEN_CENTER, SCREEN_CENTER)); packet2_utils_gif_add_set(packet2, 1); packet2_utils_gs_add_texbuff_clut(packet2, &textureBuffer, &t_sprite.clut); draw_enable_blending(); @@ -151,7 +154,8 @@ void Renderer::draw(Sprite &t_sprite) draw_primitive_xyoffset( packet2->next, 0, - (2048 - (screen->width / 2)), (2048 - (screen->height / 2)))); + SCREEN_CENTER - (screen->width / 2.0F), + SCREEN_CENTER - (screen->height / 2.0F))); draw_disable_blending(); packet2_update(packet2, draw_finish(packet2->next)); dma_channel_wait(DMA_CHANNEL_GIF, 0); @@ -160,14 +164,14 @@ void Renderer::draw(Sprite &t_sprite) } /** Initializes drawing environment (1st app packet) */ -void Renderer::initDrawingEnv(float t_screenW, float t_screenH) +void Renderer::initDrawingEnv() { PRINT_LOG("Initializing drawing environment"); - u16 halfW = (u16)t_screenW / 2; - u16 halfH = (u16)t_screenH / 2; packet2_t *packet2 = packet2_create(20, P2_TYPE_NORMAL, P2_MODE_NORMAL, 0); packet2_update(packet2, draw_setup_environment(packet2->base, 0, frameBuffers, &(zBuffer))); - packet2_update(packet2, draw_primitive_xyoffset(packet2->next, 0, (2048 - halfW), (2048 - halfH))); + packet2_update(packet2, draw_primitive_xyoffset(packet2->next, 0, + SCREEN_CENTER - (screen->width / 2.0F), + SCREEN_CENTER - (screen->height / 2.0F))); packet2_update(packet2, draw_finish(packet2->next)); dma_channel_send_packet2(packet2, DMA_CHANNEL_GIF, true); dma_channel_wait(DMA_CHANNEL_GIF, 0); @@ -198,29 +202,29 @@ void Renderer::setWorldColor(const color_t &t_rgb) } /** Defines and allocates framebuffers and zbuffer */ -void Renderer::allocateBuffers(float t_screenW, float t_screenH) +void Renderer::allocateBuffers(int t_screenW, int t_screenH) { - frameBuffers[0].width = (u16)t_screenW; - frameBuffers[0].height = (u16)t_screenH; + frameBuffers[0].width = (unsigned int)t_screenW; + frameBuffers[0].height = (unsigned int)t_screenH; frameBuffers[0].mask = 0; - 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[0].psm = GS_PSM_32; + frameBuffers[0].address = graph_vram_allocate(t_screenW, t_screenH, frameBuffers[0].psm, GRAPH_ALIGN_PAGE); - frameBuffers[1].width = (u16)t_screenW; - frameBuffers[1].height = (u16)t_screenH; + frameBuffers[1].width = (unsigned int)t_screenW; + frameBuffers[1].height = (unsigned int)t_screenH; frameBuffers[1].mask = 0; - frameBuffers[1].psm = GS_PSM_24; - frameBuffers[1].address = graph_vram_allocate((u16)t_screenW, (u16)t_screenH, frameBuffers[1].psm, GRAPH_ALIGN_PAGE); + frameBuffers[1].psm = GS_PSM_32; + frameBuffers[1].address = graph_vram_allocate(t_screenW, 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_24; - zBuffer.address = graph_vram_allocate((u16)t_screenW, (u16)t_screenH, zBuffer.zsm, GRAPH_ALIGN_PAGE); + zBuffer.address = graph_vram_allocate(t_screenW, t_screenH, zBuffer.zsm, GRAPH_ALIGN_PAGE); PRINT_LOG("Framebuffers, zBuffer set and allocated!"); // Initialize the screen and tie the first framebuffer to the read circuits. - graph_initialize(frameBuffers[1].address, frameBuffers[1].width, frameBuffers[1].height, frameBuffers[1].psm, 0, 0); + graph_initialize(frameBuffers[0].address, frameBuffers[0].width, frameBuffers[0].height, frameBuffers[0].psm, 0, 0); } /// --- Draw: PATH3 diff --git a/src/engine/modules/vif_sender.cpp b/src/engine/modules/vif_sender.cpp index 2b7f20c..b318a37 100644 --- a/src/engine/modules/vif_sender.cpp +++ b/src/engine/modules/vif_sender.cpp @@ -17,8 +17,8 @@ #include "../include/utils/debug.hpp" const u32 VU1_PACKAGE_VERTS_PER_BUFF = 96; // Remember to modify buffer size in vu1 also -const u32 VU1_PACKAGES_PER_PACKET = 6; -const u32 VU1_PACKET_SIZE = 128; +const u32 VU1_PACKAGES_PER_PACKET = 9; +const u32 VU1_PACKET_SIZE = 256; // should be 128, but 256 is more safe for future // ---- // Constructors/Destructors From d60d42cac6c156dcf2a6d0c3436df86a685dedbd Mon Sep 17 00:00:00 2001 From: h4570 Date: Thu, 24 Dec 2020 13:09:13 +0100 Subject: [PATCH 098/108] removed unecessary include --- src/engine/modules/vif_sender.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/engine/modules/vif_sender.cpp b/src/engine/modules/vif_sender.cpp index b318a37..647650c 100644 --- a/src/engine/modules/vif_sender.cpp +++ b/src/engine/modules/vif_sender.cpp @@ -68,7 +68,7 @@ void VifSender::uploadMicroProgram() dma_channel_send_packet2(packet2, DMA_CHANNEL_VIF1, 1); packet2_free(packet2); } -#include + void VifSender::sendMatrices(const RenderData &t_renderData, const Vector3 &t_position, const Vector3 &t_rotation) { From aeb25ad398c2978fdcc80eb5176db0e699ef98b5 Mon Sep 17 00:00:00 2001 From: h4570 Date: Thu, 24 Dec 2020 14:35:13 +0100 Subject: [PATCH 099/108] new vu1 renderer, added vcl preprocessor --- src/engine/Makefile | 6 +- src/engine/Makefile.pref | 1 + src/engine/include/modules/vif_sender.hpp | 3 +- src/engine/modules/renderer.cpp | 2 +- src/engine/modules/vif_sender.cpp | 75 ++--------- src/engine/vu1_progs/draw3D.vcl | 154 ---------------------- src/engine/vu1_progs/draw3D.vclpp | 143 ++++++++++++++++++++ src/engine/vu1_progs/draw3D.vsm | 110 +++++++++------- src/engine/vu1_progs/geometry.inc | 7 + src/engine/vu1_progs/matrix.inc | 13 ++ src/engine/vu1_progs/vector.inc | 27 ++++ 11 files changed, 273 insertions(+), 268 deletions(-) delete mode 100644 src/engine/vu1_progs/draw3D.vcl create mode 100644 src/engine/vu1_progs/draw3D.vclpp create mode 100644 src/engine/vu1_progs/geometry.inc create mode 100644 src/engine/vu1_progs/matrix.inc create mode 100644 src/engine/vu1_progs/vector.inc diff --git a/src/engine/Makefile b/src/engine/Makefile index 78c621f..949c572 100644 --- a/src/engine/Makefile +++ b/src/engine/Makefile @@ -35,8 +35,10 @@ all: $(EE_OBJS) ar rcs $(LIB_NAME) $(EE_OBJS) rm -f $(EE_OBJS) -# https://github.com/microsoft/wsl/issues/2468#issuecomment-374904520 -#%.vsm: %.vcl # sudo service binfmt-support start +#%.vcl: %.vclpp +# $(EE_VCLPP) $< $@ + +#%.vsm: %.vcl # $(EE_VCL) $< >> $@ %.o: %.vsm diff --git a/src/engine/Makefile.pref b/src/engine/Makefile.pref index 389166e..574715b 100644 --- a/src/engine/Makefile.pref +++ b/src/engine/Makefile.pref @@ -5,6 +5,7 @@ EE_CFLAGS := -DHAVE_LIBPNG -DHAVE_ZLIB $(EE_CFLAGS) EE_CXXFLAGS := -DHAVE_LIBPNG -DHAVE_ZLIB $(EE_CXXFLAGS) EE_DVP = dvp-as EE_VCL = vcl +EE_VCLPP = vclpp LIB_NAME = libtyra.a include $(PS2SDK)/samples/Makefile.pref diff --git a/src/engine/include/modules/vif_sender.hpp b/src/engine/include/modules/vif_sender.hpp index 53153c5..43c2d7e 100644 --- a/src/engine/include/modules/vif_sender.hpp +++ b/src/engine/include/modules/vif_sender.hpp @@ -30,7 +30,7 @@ public: // TODO refactor void drawMesh(RenderData *t_renderData, Matrix t_perspective, u32 vertCount2, VECTOR *vertices, VECTOR *normals, VECTOR *coordinates, Mesh &t_mesh, LightBulb *t_bulbs, u16 t_bulbsCount, texbuffer_t *textureBuffer); - void sendMatrices(const RenderData &t_renderData, const Vector3 &t_position, const Vector3 &t_rotation); + void calcMatrix(const RenderData &t_renderData, const Vector3 &t_position, const Vector3 &t_rotation); private: Light *light; @@ -50,7 +50,6 @@ private: */ Matrix modelViewProj; u8 context; - packet2_t *matricesPacket __attribute__((aligned(64))); VECTOR position, rotation; u32 vertCount; }; diff --git a/src/engine/modules/renderer.cpp b/src/engine/modules/renderer.cpp index 2287b88..425fb0d 100644 --- a/src/engine/modules/renderer.cpp +++ b/src/engine/modules/renderer.cpp @@ -277,7 +277,7 @@ void Renderer::draw(Mesh *t_meshes, u16 t_amount, LightBulb *t_bulbs, u16 t_bulb void Renderer::draw(Mesh &t_mesh, LightBulb *t_bulbs, u16 t_bulbsCount) { beginFrameIfNeeded(); - vifSender->sendMatrices(renderData, t_mesh.position, t_mesh.rotation); + vifSender->calcMatrix(renderData, t_mesh.position, t_mesh.rotation); if (!t_mesh.isDataLoaded()) PRINT_ERR("Can't draw, because no mesh data was loaded!"); diff --git a/src/engine/modules/vif_sender.cpp b/src/engine/modules/vif_sender.cpp index 647650c..78dd0ce 100644 --- a/src/engine/modules/vif_sender.cpp +++ b/src/engine/modules/vif_sender.cpp @@ -39,7 +39,6 @@ VifSender::VifSender(Light *t_light) uploadMicroProgram(); packets[0] = packet2_create(VU1_PACKET_SIZE, P2_TYPE_NORMAL, P2_MODE_CHAIN, true); packets[1] = packet2_create(VU1_PACKET_SIZE, P2_TYPE_NORMAL, P2_MODE_CHAIN, true); - matricesPacket = packet2_create(4, P2_TYPE_NORMAL, P2_MODE_CHAIN, true); context = 0; setDoubleBuffer(); } @@ -48,7 +47,6 @@ VifSender::~VifSender() { packet2_free(packets[0]); packet2_free(packets[1]); - packet2_free(matricesPacket); } // ---- @@ -69,10 +67,8 @@ void VifSender::uploadMicroProgram() packet2_free(packet2); } -void VifSender::sendMatrices(const RenderData &t_renderData, const Vector3 &t_position, const Vector3 &t_rotation) +void VifSender::calcMatrix(const RenderData &t_renderData, const Vector3 &t_position, const Vector3 &t_rotation) { - - Matrix model; model.identity(); model.rotate(t_rotation); model.translate(t_position); @@ -81,12 +77,6 @@ void VifSender::sendMatrices(const RenderData &t_renderData, const Vector3 &t_po modelViewProj = model * modelViewProj; modelViewProj = *t_renderData.view * modelViewProj; modelViewProj = *t_renderData.projection * modelViewProj; - - packet2_reset(matricesPacket, false); - packet2_utils_vu_add_unpack_data(matricesPacket, 0, &modelViewProj.data, 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); } void VifSender::drawMesh(RenderData *t_renderData, Matrix t_perspective, u32 vertCount2, VECTOR *vertices, VECTOR *normals, VECTOR *coordinates, Mesh &t_mesh, LightBulb *t_bulbs, u16 t_bulbsCount, texbuffer_t *textureBuffer) @@ -122,9 +112,9 @@ void VifSender::drawMesh(RenderData *t_renderData, Matrix t_perspective, u32 ver void VifSender::setDoubleBuffer() { packet2_t *settings = packet2_create(2, P2_TYPE_NORMAL, P2_MODE_CHAIN, true); - packet2_utils_vu_add_double_buffer(settings, 8, 496); + packet2_utils_vu_add_double_buffer(settings, 10, 498); packet2_utils_vu_add_end_tag(settings); - dma_channel_send_packet2(settings, DMA_CHANNEL_VIF1, 1); + dma_channel_send_packet2(settings, DMA_CHANNEL_VIF1, true); dma_channel_wait(DMA_CHANNEL_VIF1, 0); packet2_free(settings); } @@ -134,67 +124,24 @@ void VifSender::drawVertices(Mesh &t_mesh, u32 t_start, u32 t_end, VECTOR *t_ver { const u32 vertCount = t_end - t_start; u32 vif_added_bytes = 0; - packet2_utils_vu_open_unpack(currPacket, 0, 1); - // TODO get this via screensettings - packet2_add_float(currPacket, 2048.0F); // scale - packet2_add_float(currPacket, 2048.0F); // scale - packet2_add_float(currPacket, ((float)0xFFFFFF) / 32.0F); // scale - packet2_add_u32(currPacket, vertCount); // vertex count + packet2_utils_vu_open_unpack(currPacket, 0, true); + packet2_add_data(currPacket, modelViewProj.data, 4); + packet2_add_u32(currPacket, 0); // Free + packet2_add_u32(currPacket, vertCount); // Vertex count + packet2_add_u32(currPacket, vertCount / 3); // Triangles count + packet2_add_u32(currPacket, 0); // Free packet2_utils_gif_add_set(currPacket, 1); packet2_utils_gs_add_lod(currPacket, &t_mesh.lod); packet2_utils_gs_add_texbuff_clut(currPacket, textureBuffer, &t_mesh.clut); packet2_utils_gs_add_prim_giftag(currPacket, t_prim, vertCount, DRAW_STQ2_REGLIST, 3, 0); - packet2_add_u32(currPacket, t_mesh.color.r); packet2_add_u32(currPacket, t_mesh.color.g); packet2_add_u32(currPacket, t_mesh.color.b); packet2_add_u32(currPacket, t_mesh.color.a); - - // Clipping tests start - - // // const float minZ = 1; - // // const float maxZ = 65535; - // // const int iGuardDimXY = 2048; - - // // vu1.addFloat(1.0F); // f_TODO clipping maybe there is problem? - // // vu1.addFloat(1.0F); - // // vu1.addFloat(1.0F); - // // vu1.addFloat(1.0F); - // // float xClip = (float)2048.0f/(drawContext.GetFBWidth() * 0.5f * 2.0f); - // // packet += Math::Max( xClip, 1.0f ); - // // float yClip = (float)2048.0f/(drawContext.GetFBHeight() * 0.5f * 2.0f); - // // packet += Math::Max( yClip, 1.0f ); - // // float depthClip = 2048.0f / depthClipToGs; - // // // F_FIXME: maybe these 2048's should be 2047.5s... - // // depthClip *= 1.003f; // round up a bit for fp error (????) - // // packet += depthClip; - // // // enable/disable clipping - // // packet += (drawContext.GetDoClipping()) ? 1 : 0; - - // u32 depthBits = 24; // or 28(fog) or 16 - // float depthClipToGs = (float)((1 << depthBits) - 1) / 2.0f; - // vu1.addFloat(2048.0f / (640.0F * 0.5f * 2.0f)); - // vu1.addFloat(2048.0f / (480.0F * 0.5f * 2.0f)); - // vu1.addFloat((2048.0f / depthClipToGs) * 1.003F); - // // vu1.addFloat(2048.0F); // scale - // // vu1.addFloat(2048.0F); // scale - // // vu1.addFloat(((float)0xFFFFFF) / 32.0F); // scale - // vu1.addFloat(0.0F); - // // vu1.addFloat(0.5f * iGuardDimXY); - // // vu1.addFloat(-0.5f * iGuardDimXY); - // // vu1.addFloat(1.0F); - // // vu1.addFloat(500.0F); // far - - packet2_add_float(currPacket, 0.0F); - packet2_add_float(currPacket, 0.0F); - packet2_add_float(currPacket, 0.0F); - packet2_add_float(currPacket, 0.0F); - - //// Clipping tests end vif_added_bytes += packet2_utils_vu_close_unpack(currPacket); - packet2_utils_vu_add_unpack_data(currPacket, vif_added_bytes, t_vertices + t_start, vertCount, 1); + packet2_utils_vu_add_unpack_data(currPacket, vif_added_bytes, t_vertices + t_start, vertCount, true); vif_added_bytes += vertCount; - packet2_utils_vu_add_unpack_data(currPacket, vif_added_bytes, t_coordinates + t_start, vertCount, 1); + packet2_utils_vu_add_unpack_data(currPacket, vif_added_bytes, t_coordinates + t_start, vertCount, true); vif_added_bytes += vertCount; packet2_utils_vu_add_start_program(currPacket, 0); } diff --git a/src/engine/vu1_progs/draw3D.vcl b/src/engine/vu1_progs/draw3D.vcl deleted file mode 100644 index 777d84f..0000000 --- a/src/engine/vu1_progs/draw3D.vcl +++ /dev/null @@ -1,154 +0,0 @@ -; ______ ____ ___ -; | \/ ____| |___| -; | | | \ | | -;--------------------------- -; Copyright 2020, tyra - https://github.com/h4570/tyra -; Sandro Sobczyński -; -;--------------------------------------------------------------- -; draw3D.vcl | -;--------------------------------------------------------------- -; A VU1 microprogram to draw 3D object using XYZ2, RGBAQ and ST| -; This program uses double buffering (xtop) | -; | -; Many thanks to: | -; - Dr Henry Fortuna | -; - Jesper Svennevid, Daniel Collin | -; - Guilherme Lampert | -;--------------------------------------------------------------- - -; TODO -; - Fix vertex clipping (is clipping visible triangles :/) -; - Move lerp() from EE to VU1 (performance) -; - Add dynamic lighting (feature) -; - Add --cont + MSCNT instead of alltime MSCAL (performance) - -.syntax new -.name VU1Draw3D -.vu -.init_vf_all -.init_vi_all - ---enter ---endenter - - ;//////////// --- Load data 1 --- ///////////// - ; Updated once per frame - lq matrixRow0, 0(vi00) ; load view-projection matrix - lq matrixRow1, 1(vi00) - lq matrixRow2, 2(vi00) - lq matrixRow3, 3(vi00) - ;///////////////////////////////////////////// - - fcset 0x000000 ; VCL won't let us use CLIP without first zeroing - ; the clip flags - - ;//////////// --- Load data 2 --- ///////////// - ; Updated dynamically - xtop iBase - - lq.xyz scale, 0(iBase) ; load program params - ; float : X, Y, Z - scale vector that we will use to scale the verts after projecting them. - ; float : W - vert count. - lq gifSetTag, 1(iBase) ; GIF tag - set - lq texGifTag1, 2(iBase) ; GIF tag - texture LOD - lq texGifTag2, 3(iBase) ; GIF tag - texture buffer & CLUT - lq primTag, 4(iBase) ; GIF tag - tell GS how many data we will send - lq rgba, 5(iBase) ; RGBA - ;lq clipScale, 6(iBase) ; TODO clipping tests - ; u32 : R, G, B, A (0-128) - iaddiu vertexData, iBase, 7 ; pointer to vertex data - ilw.w vertCount, 0(iBase) ; load vert count from scale vector - iadd stqData, vertexData, vertCount ; pointer to stq - iadd kickAddress, stqData, vertCount ; pointer for XGKICK - iadd destAddress, stqData, vertCount ; helper pointer for data inserting - ;//////////////////////////////////////////// - - ;/////////// --- Store tags --- ///////////// - sqi gifSetTag, (destAddress++) ; - sqi texGifTag1, (destAddress++) ; texture LOD tag - sqi gifSetTag, (destAddress++) ; - sqi texGifTag2, (destAddress++) ; texture buffer & CLUT tag - sqi primTag, (destAddress++) ; prim + tell gs how many data will be - ;//////////////////////////////////////////// - - ;/////////////// --- Loop --- /////////////// - iadd vertexCounter, iBase, vertCount ; loop vertCount times - vertexLoop: - - ;////////// --- Load loop data --- ////////// - lq vertex, 0(vertexData) ; load xyz - ; float : X, Y, Z - ; any32 : _ = 0 - lq stq, 0(stqData) ; load stq - ; float : S, T - ; any32 : Q = 1 ; 1, because we will mul this by 1/vert[w] and this - ; will be our q for texture perspective correction - ; any32 : _ = 0 - ;//////////////////////////////////////////// - - - ;////////////// --- Vertex --- ////////////// - mul acc, matrixRow0, vertex[x] ; transform each vertex by the matrix - madd acc, matrixRow1, vertex[y] - madd acc, matrixRow2, vertex[z] - madd vertex, matrixRow3, vertex[w] - - ;add.z acc, vf0, clipScale[w] ; TODO clipping maybe this? - ;madd.z clipVec, clipScale, vertex - ;mul.xy clipVec, clipScale, vertex - ;mul.w clipVec, vf0, vertex[z] - - ;mul.xyz clipVec, vertex, clipScale ; TODO clipping maybe this? - - clipw.xyz vertex, vertex ; Dr. Fortuna: This instruction checks if the vertex is outside - ; the viewing frustum. If it is, then the appropriate - ; clipping flags are set - fcand VI01, 0x3FFFF ; Bitwise AND the clipping flags with 0x3FFFF, this makes - ; sure that we get the clipping judgement for the last three - ; verts (i.e. that make up the triangle we are about to draw) - iaddiu iADC, VI01, 0x7FFF ; Add 0x7FFF. If any of the clipping flags were set this will - ; cause the triangle not to be drawn (any values above 0x8000 - ; that are stored in the w component of XYZ2 will set the ADC - ; bit, which tells the GS not to perform a drawing kick on this - ; triangle. - - ;ilw.w iNoDraw, UVStart(Counter) ; Load the iNoDraw flag. If true we should set the ADC bit so the vert isn't drawn - ;iadd iADC, iADC, iNoDraw ; TODO clipping maybe this? - - isw.w iADC, 2(destAddress) - - div q, vf00[w], vertex[w] ; perspective divide (1/vert[w]): - mul.xyz vertex, vertex, q - mula.xyz acc, scale, vf00[w] ; scale to GS screen space - madd.xyz vertex, vertex, scale ; multiply and add the scales -> vert = vert * scale + scale - ftoi4.xyz vertex, vertex ; convert vertex to 12:4 fixed point format - ;//////////////////////////////////////////// - - - ;//////////////// --- ST --- //////////////// - mulq modStq, stq, q - ;//////////////////////////////////////////// - - - ;//////////// --- Store data --- //////////// - sq modStq, 0(destAddress) ; STQ - sq rgba, 1(destAddress) ; RGBA ; q is grabbed from stq - sq.xyz vertex, 2(destAddress) ; XYZ2 - ;//////////////////////////////////////////// - - iaddiu vertexData, vertexData, 1 - iaddiu stqData, stqData, 1 - iaddiu destAddress, destAddress, 3 - - iaddi vertexCounter, vertexCounter, -1 ; decrement the loop counter - ibne vertexCounter, iBase, vertexLoop ; and repeat if needed - - ;//////////////////////////////////////////// - - --barrier - - xgkick kickAddress ; dispatch to the GS rasterizer. - ---exit ---endexit diff --git a/src/engine/vu1_progs/draw3D.vclpp b/src/engine/vu1_progs/draw3D.vclpp new file mode 100644 index 0000000..f38fc42 --- /dev/null +++ b/src/engine/vu1_progs/draw3D.vclpp @@ -0,0 +1,143 @@ +; ______ ____ ___ +; | \/ ____| |___| +; | | | \ | | +;--------------------------- +; Copyright 2020, tyra - https://github.com/h4570/tyra +; Sandro Sobczyński +; +;--------------------------------------------------------------- +; draw3D.vcl | +;--------------------------------------------------------------- +; First tyra VU1 microprogram. | +; Features: | +; - Draw triangles (no strip) with STQ (textures) and 1 RGBA. | +; This program uses double buffering (xtop) | +; | +; I want to say thank you to: | +; - Dr Henry Fortuna - for teaching how things work | +; - Jesper Svennevid, Daniel Collin - for openvcl samples | +; - Guilherme Lampert - for VU1 idea for PS2 Quake and vclpp | +; - Tyler Daniel - for PS2GL source code for PS2 Linux | +;--------------------------------------------------------------- + +; TODO +; - Add dynamic lighting (feature) +; - Add --cont + MSCNT instead of alltime MSCAL + +#include "/repos/tyra/src/engine/vu1_progs/geometry.inc" +#include "/repos/tyra/src/engine/vu1_progs/matrix.inc" +#include "/repos/tyra/src/engine/vu1_progs/vector.inc" + +#vuprog draw3D + +.syntax new +.name VU1Draw3D +.vu +.init_vf_all +.init_vi_all + +--enter +--endenter + +xtop double_buffer + +;//////// LOAD CURRENT BUFFER DATA ////////// +MatrixLoad{ matrix, 0, double_buffer } +ilw.z triangles_count, 4(double_buffer) ; Triangles count +ilw.y vertex_count, 4(double_buffer) ; Vertex count +lq gif_set_tag, 5(double_buffer) ; GIF tag - set +lq tex_gif_tag_1, 6(double_buffer) ; GIF tag - texture LOD +lq tex_gif_tag_2, 7(double_buffer) ; GIF tag - texture buffer & CLUT +lq prim_tag, 8(double_buffer) ; GIF tag - tell GS how many data we will send +lq rgba, 9(double_buffer) ; Mesh RGBA + +iaddiu vertex_data, double_buffer, 10 ; pointer to vertex data +iadd stq_data, vertex_data, vertex_count ; pointer to stq +iadd kick_address, stq_data, vertex_count ; pointer for XGKICK +iadd dest_address, stq_data, vertex_count ; helper pointer for data inserting +;//////////////////////////////////////////// + +LoadScaleConstant{ gs_scale } +fcset 0x000000 ; VCL won't let us use CLIP without first zeroing the clip flags + +;/////////////// STORE TAGS ///////////////// +sqi gif_set_tag, (dest_address++) ; +sqi tex_gif_tag_1, (dest_address++) ; texture LOD tag +sqi gif_set_tag, (dest_address++) ; +sqi tex_gif_tag_2, (dest_address++) ; texture buffer & CLUT tag +sqi prim_tag, (dest_address++) ; prim + tell gs how many data will be +;//////////////////////////////////////////// + +;//////// FIX ADC BIT FOR CLIPPING ////////// +iaddiu adc_bit, vi00, 0x7FFF +iaddiu adc_bit, adc_bit, 1 +;//////////////////////////////////////////// + +;/////////// START TRIANGLE LOOP //////////// +iaddiu triangle_counter, vi00, 0 ; Reset counter +triangle_loop: --LoopCS 1,3 + + ;//////////////// VERTEX 1 ////////////////// + VectorLoad{ vertex, vertex_data, 0 } + VectorLoad{ stq1, stq_data, 0 } + MatrixXForm{ xformed_vertex, matrix, vertex } + VectorClip{ gs_vertex, xformed_vertex } + VectorPerspectiveDivide{ xformed_vertex } + VectorAddGSScales{ gs_vertex, xformed_vertex, gs_scale } + VectorTexturePerspectiveCorrection{ pers_stq, stq1 } + VectorStore{ pers_stq, dest_address, 0 } + VectorStore{ rgba, dest_address, 1 } + VectorStore{ gs_vertex, dest_address, 2 } + ;//////////////////////////////////////////// + + ;//////////////// VERTEX 2 ////////////////// + VectorLoad{ vertex, vertex_data, 1 } + VectorLoad{ stq2, stq_data, 1 } + MatrixXForm{ xformed_vertex, matrix, vertex } + VectorClip{ gs_vertex, xformed_vertex } + VectorPerspectiveDivide{ xformed_vertex } + VectorAddGSScales{ gs_vertex, xformed_vertex, gs_scale } + VectorTexturePerspectiveCorrection{ pers_stq, stq2 } + VectorStore{ pers_stq, dest_address, 3 } + VectorStore{ rgba, dest_address, 4 } + VectorStore{ gs_vertex, dest_address, 5 } + ;//////////////////////////////////////////// + + ;//////////////// VERTEX 3 ////////////////// + VectorLoad{ vertex, vertex_data, 2 } + VectorLoad{ stq3, stq_data, 2 } + MatrixXForm{ xformed_vertex, matrix, vertex } + VectorClip{ gs_vertex, xformed_vertex } + VectorPerspectiveDivide{ xformed_vertex } + VectorAddGSScales{ gs_vertex, xformed_vertex, gs_scale } + VectorTexturePerspectiveCorrection{ pers_stq, stq3 } + VectorStore{ pers_stq, dest_address, 6 } + VectorStore{ rgba, dest_address, 7 } + + fcand vi01, 0x03FFFF + iaddiu new_adc_bit, vi01, 0x7FFF + mfir.w gs_vertex, new_adc_bit + VectorStore{ gs_vertex, dest_address, 8 } + ;//////////////////////////////////////////// + + ;////////////// LOOP CONTROL //////////////// + iaddiu vertex_data, vertex_data, 3 + iaddiu stq_data, stq_data, 3 + iaddiu dest_address, dest_address, 9 + + iaddiu triangle_counter, triangle_counter, 1 // Incrementing this + // by other value than 1 is causing HUGE problems, but.. why? + // My first idea was do vertex_counter and incrementing by 3 + ibne triangle_counter, triangles_count, triangle_loop + ;//////////////////////////////////////////// + +;//////////////////////////////////////////// + +--barrier + +xgkick kick_address ; dispatch to the GS rasterizer. + +--exit +--endexit + +#endvuprog \ No newline at end of file diff --git a/src/engine/vu1_progs/draw3D.vsm b/src/engine/vu1_progs/draw3D.vsm index f8c1e0e..f557b72 100644 --- a/src/engine/vu1_progs/draw3D.vsm +++ b/src/engine/vu1_progs/draw3D.vsm @@ -11,63 +11,83 @@ .global VU1Draw3D_CodeEnd VU1Draw3D_CodeStart: __v_draw3D_vcl_4: -; _LNOPT_w=[ normal2 ] 23 [23 0] 23 [__v_draw3D_vcl_4] - NOP lq VF01,0(VI00) - NOP xtop VI02 - NOP lq VF02,1(VI00) - NOP lq VF06,1(VI02) - NOP lq VF09,2(VI02) - NOP lq VF08,3(VI02) - NOP lq VF07,4(VI02) - NOP lq VF03,2(VI00) - NOP iaddiu VI03,VI02,0x00000007 - NOP ilw.w VI07,0(VI02) - NOP lq VF04,3(VI00) - NOP fcset 0 - NOP lq.xyz VF05,0(VI02) - NOP iadd VI04,VI03,VI07 - NOP iadd VI06,VI04,VI07 - NOP sqi VF06,(VI06++) +; _LNOPT_w=[ normal2 ] 27 [27 0] 27 [__v_draw3D_vcl_4] + NOP xtop VI07 + NOP lq VF01,0(VI07) + NOP ilw.y VI05,4(VI07) + NOP lq VF05,5(VI07) + NOP lq VF09,6(VI07) + NOP lq VF08,7(VI07) + NOP lq VF07,8(VI07) + NOP lq VF02,1(VI07) + NOP iaddiu VI03,VI07,0x0000000a + NOP iadd VI04,VI03,VI05 + NOP lq VF03,2(VI07) + NOP iadd VI06,VI04,VI05 + NOP loi 0x44fff000 + NOP lq VF04,3(VI07) + NOP ilw.z VI02,4(VI07) + NOP sqi VF05,(VI06++) NOP sqi VF09,(VI06++) - NOP sqi VF06,(VI06++) + NOP sqi VF05,(VI06++) NOP sqi VF08,(VI06++) - NOP lq VF06,5(VI02) - NOP iadd VI05,VI04,VI07 + NOP lq VF05,9(VI07) + NOP iaddiu VI07,VI00,0x00007fff + NOP iadd VI05,VI04,VI05 + addi.xy VF06,VF00,I loi 0x492aaaaa + NOP fcset 0 NOP sqi VF07,(VI06++) - NOP iadd VI07,VI02,VI07 -vertexLoop: -; _LNOPT_w=[ normal2 ] 21 [31 14] 31 [vertexLoop] + NOP iaddiu VI07,VI07,0x00000001 + addi.z VF06,VF00,I iaddiu VI08,VI00,0 +triangle_loop: +; _LNOPT_w=[ normal ] 37 [31 30] 45 [triangle_loop] NOP lq VF07,0(VI03) - mulax ACC,VF01,VF07x sq VF06,1(VI06) ; STALL_LATENCY ?3 - madday ACC,VF02,VF07y lq VF08,0(VI04) - maddaz ACC,VF03,VF07z iaddiu VI06,VI06,0x00000003 + mulax ACC,VF01,VF07x iaddiu VI08,VI08,0x00000001 ; STALL_LATENCY ?3 + madday ACC,VF02,VF07y NOP + maddaz ACC,VF03,VF07z NOP + maddw VF10,VF04,VF07w lq VF08,2(VI03) + mulax ACC,VF01,VF08x div Q,VF00w,VF10w ; STALL_LATENCY ?3 + madday ACC,VF02,VF08y NOP + maddaz ACC,VF03,VF08z lq VF07,1(VI03) + maddw VF08,VF04,VF08w NOP + clipw.xyz VF10xyz,VF10w NOP + mulax ACC,VF01,VF07x NOP ; STALL_LATENCY ?1 + madday ACC,VF02,VF07y div Q,VF00w,VF08w + maddaz ACC,VF03,VF07z lq VF09,0(VI04) maddw VF07,VF04,VF07w NOP - clipw.xyz VF07xyz,VF07w div Q,VF00w,VF07w ; STALL_LATENCY ?3 - NOP NOP - NOP NOP - NOP NOP - NOP NOP - NOP NOP - NOP NOP - mulq.xyz VF07,VF07,Q fcand VI01,262143 - mulaw.xyz ACC,VF05,VF00w iaddiu VI03,VI03,0x00000001 - madd.xyz VF07,VF07,VF05 iaddiu VI04,VI04,0x00000001 ; STALL_LATENCY ?2 - mulq VF08,VF08,Q isubiu VI07,VI07,1 - ftoi4.xyz VF07,VF07 iaddiu VI01,VI01,0x00007fff ; STALL_LATENCY ?2 - NOP isw.w VI01,-1(VI06) - NOP sq VF08,-3(VI06) - NOP ibne VI07,VI02,vertexLoop - NOP sq.xyz VF07,-1(VI06) + mulaw.xyz ACC,VF06,VF00w NOP + mulq.xyz VF10,VF10,Q NOP + mulq VF09,VF09,Q sq VF05,1(VI06) + clipw.xyz VF07xyz,VF07w lq VF11,2(VI04) + clipw.xyz VF08xyz,VF08w div Q,VF00w,VF07w + mulq.xyz VF08,VF08,Q sq VF05,4(VI06) + madd.xyz VF09,VF10,VF06 sq VF09,0(VI06) + mulq VF10,VF11,Q sq VF05,7(VI06) + mulaw.xyz ACC,VF06,VF00w mfir.w VF09,VI07 + madd.xyz VF08,VF08,VF06 fcand VI01,262143 + NOP iaddiu VI01,VI01,0x00007fff + mulq.xyz VF10,VF07,Q sq VF10,6(VI06) + ftoi4.xyz VF09,VF09 mfir.w VF07,VI01 + ftoi4.xyz VF07,VF08 lq VF11,1(VI04) + mulaw.xyz ACC,VF06,VF00w iaddiu VI03,VI03,0x00000003 + madd.xyz VF08,VF10,VF06 iaddiu VI04,VI04,0x00000003 + NOP sq VF09,2(VI06) + mulq VF09,VF11,Q sq VF07,8(VI06) + NOP mfir.w VF07,VI07 + ftoi4.xyz VF07,VF08 iaddiu VI06,VI06,0x00000009 + NOP sq VF09,-6(VI06) ; STALL_LATENCY ?1 + NOP ibne VI08,VI02,triangle_loop + NOP sq VF07,-4(VI06) ; _LNOPT_w=[ normal2 ] 3 [1 0] 3 [__v_draw3D_vcl_7] NOP xgkick VI05 NOP[E] NOP NOP NOP .align 4 VU1Draw3D_CodeEnd: -; iCount=47 +; iCount=67 ; register stats: -; 8 VU User integer -; 10 VU User floating point +; 9 VU User integer +; 12 VU User floating point ;------------------------- ;------------------------- ;------------------------- diff --git a/src/engine/vu1_progs/geometry.inc b/src/engine/vu1_progs/geometry.inc new file mode 100644 index 0000000..2562412 --- /dev/null +++ b/src/engine/vu1_progs/geometry.inc @@ -0,0 +1,7 @@ +; Load GS offsets to center xformed vertex in gs coord space and color clamping constant +#macro LoadScaleConstant: gs_scale + loi 2047.5 + addi.xy gs_scale, vf00, i + loi 699050.625000 // ((float)0xFFFFFF) / 24.0F + addi.z gs_scale, vf00, i +#endmacro diff --git a/src/engine/vu1_progs/matrix.inc b/src/engine/vu1_progs/matrix.inc new file mode 100644 index 0000000..47615e5 --- /dev/null +++ b/src/engine/vu1_progs/matrix.inc @@ -0,0 +1,13 @@ +#macro MatrixLoad: matrix, offset, vumem + lq matrix[0], offset+0(vumem) + lq matrix[1], offset+1(vumem) + lq matrix[2], offset+2(vumem) + lq matrix[3], offset+3(vumem) +#endmacro + +#macro MatrixXForm: output_vertex, matrix, input_vertex + mul acc, matrix[0], input_vertex[x] + madd acc, matrix[1], input_vertex[y] + madd acc, matrix[2], input_vertex[z] + madd output_vertex, matrix[3], input_vertex[w] +#endmacro diff --git a/src/engine/vu1_progs/vector.inc b/src/engine/vu1_progs/vector.inc new file mode 100644 index 0000000..c492916 --- /dev/null +++ b/src/engine/vu1_progs/vector.inc @@ -0,0 +1,27 @@ +#macro VectorPerspectiveDivide: vertex + div q, vf00[w], vertex[w] + mulq.xyz vertex, vertex, q +#endmacro + +#macro VectorAddGSScales: output_vertex, input_vertex, gs_scale + mula.xyz acc, gs_scale, vf00[w] + madd.xyz output_vertex, input_vertex, gs_scale + ftoi4.xyz output_vertex, output_vertex +#endmacro + +#macro VectorTexturePerspectiveCorrection: output_vertex, input_vertex + mulq output_vertex, input_vertex, q +#endmacro + +#macro VectorStore: vertex, vumem, num + sq vertex, num(vumem) +#endmacro + +#macro VectorClip: output_vertex, input_vertex + clipw.xyz input_vertex, input_vertex + mfir.w output_vertex, adc_bit +#endmacro + +#macro VectorLoad: output_vertex, vumem, offset + lq output_vertex, offset(vumem) +#endmacro From 8406dcee72d096b4e6daf96543a2a5689bc47d2d Mon Sep 17 00:00:00 2001 From: h4570 Date: Sat, 26 Dec 2020 13:29:37 +0100 Subject: [PATCH 100/108] added optimized array mode to draw() --- src/engine/include/modules/renderer.hpp | 33 +++- src/engine/include/modules/vif_sender.hpp | 9 +- src/engine/modules/renderer.cpp | 33 +++- src/engine/modules/vif_sender.cpp | 104 ++++++++-- src/engine/vu1_progs/draw3D.vclpp | 24 ++- src/engine/vu1_progs/draw3D.vsm | 222 +++++++++++++++------- 6 files changed, 324 insertions(+), 101 deletions(-) diff --git a/src/engine/include/modules/renderer.hpp b/src/engine/include/modules/renderer.hpp index be874d9..913770d 100644 --- a/src/engine/include/modules/renderer.hpp +++ b/src/engine/include/modules/renderer.hpp @@ -13,6 +13,8 @@ #include #include +#include +#include #include #include "gif_sender.hpp" #include "vif_sender.hpp" @@ -38,12 +40,20 @@ public: void enableVSync() { isVSyncEnabled = true; } void disableVSync() { isVSyncEnabled = false; } + void clearAndWaitForRender() + { + *GS_REG_CSR |= 2; // Reset wait flag + draw_wait_finish(); + } + void waitForRender() { draw_wait_finish(); } + + /** 2D draw. */ + void draw(Sprite &t_sprite); /// --- Draw: PATH3 - void draw(Sprite &t_sprite); - /** + * WARNING: THIS FUNC CAUSE VISUAL ARTIFACTS! * Draw many meshes with lighting information. * Slowest way of rendering (PATH 3, using EE and GIF). * NOTICE: Animation supported, lighting supported @@ -51,6 +61,7 @@ public: void drawByPath3(Mesh *t_meshes, u16 t_amount, LightBulb *t_bulbs, u16 t_bulbsCount); /** + * WARNING: THIS FUNC CAUSE VISUAL ARTIFACTS! * Draw mesh with lighting information. * Slowest way of rendering (PATH 3, using EE and GIF). * NOTICE: Animation supported, lighting supported @@ -58,6 +69,7 @@ public: void drawByPath3(Mesh &t_mesh, LightBulb *t_bulbs, u16 t_bulbsCount); /** + * WARNING: THIS FUNC CAUSE VISUAL ARTIFACTS! * Draw many meshes without lighting information. * Slowest way of rendering (PATH 3, using EE and GIF). * NOTICE: Animation supported, lighting supported @@ -65,6 +77,7 @@ public: void drawByPath3(Mesh *t_meshes, u16 t_amount); /** + * WARNING: THIS FUNC CAUSE VISUAL ARTIFACTS! * Draw mesh without lighting information. * Slowest way of rendering (PATH 3, using EE and GIF). * NOTICE: Animation supported, lighting supported @@ -74,25 +87,27 @@ public: /// --- Draw: PATH1 /** - * Draw many meshes with lighting information. + * Draw many meshes with lighting information. + * Draw in array mode, can be A LOT faster than for looping! * Fastest way of rendering (PATH 1, using VU1). * NOTICE: Animation supported, lighting NOT supported (at this moment) */ - void draw(Mesh *t_meshes, u16 t_amount, LightBulb *t_bulbs, u16 t_bulbsCount); + void draw(Mesh **t_meshes, u16 t_amount, LightBulb *t_bulbs, u16 t_bulbsCount); /** - * Draw mesh with lighting information. - * Fastest way of rendering (PATH 1, using VU1). + * Draw mesh with lighting information. + * Fastest way of rendering (PATH 1, using VU1). * NOTICE: Animation supported, lighting NOT supported (at this moment) */ void draw(Mesh &t_mesh, LightBulb *t_bulbs, u16 t_bulbsCount); /** - * Draw many meshes without lighting information. - * Fastest way of rendering (PATH 1, using VU1). + * Draw many meshes without lighting information. + * Draw in array mode, can be A LOT faster than for looping! + * Fastest way of rendering (PATH 1, using VU1). * NOTICE: Animation supported, lighting NOT supported (at this moment) */ - void draw(Mesh *t_meshes, u16 t_amount); + void draw(Mesh **t_meshes, u16 t_amount); /** * Draw mesh without lighting information. diff --git a/src/engine/include/modules/vif_sender.hpp b/src/engine/include/modules/vif_sender.hpp index 43c2d7e..c1fdbc6 100644 --- a/src/engine/include/modules/vif_sender.hpp +++ b/src/engine/include/modules/vif_sender.hpp @@ -31,12 +31,17 @@ public: // TODO refactor void drawMesh(RenderData *t_renderData, Matrix t_perspective, u32 vertCount2, VECTOR *vertices, VECTOR *normals, VECTOR *coordinates, Mesh &t_mesh, LightBulb *t_bulbs, u16 t_bulbsCount, texbuffer_t *textureBuffer); void calcMatrix(const RenderData &t_renderData, const Vector3 &t_position, const Vector3 &t_rotation); + void drawTheSameWithOtherMatrices(const RenderData &t_renderData, Mesh **t_meshes, const u32 &t_skip, const u32 &t_count); + void enableWait() { isDrawWaitEnabled = true; } + void disableWait() { isDrawWaitEnabled = false; } private: + u32 lastVertCount; // needed for drawTheSameWithOtherMatrices() + u8 isDrawWaitEnabled; 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); + void setDoubleBufferAddStaticData(); + void drawVertices(Mesh &t_mesh, u32 t_start, u32 t_end, VECTOR *t_vertices, VECTOR *t_coordinates, prim_t *t_prim, texbuffer_t *t_textureBuffer, u8 t_addDrawWait); packet2_t *packets[2] __attribute__((aligned(64))); packet2_t *currPacket; /** diff --git a/src/engine/modules/renderer.cpp b/src/engine/modules/renderer.cpp index 425fb0d..35961b3 100644 --- a/src/engine/modules/renderer.cpp +++ b/src/engine/modules/renderer.cpp @@ -13,8 +13,6 @@ #include #include #include -#include -#include #include "../include/utils/debug.hpp" #include "../include/utils/math.hpp" @@ -268,10 +266,33 @@ void Renderer::drawByPath3(Mesh &t_mesh) { drawByPath3(t_mesh, NULL, 0); } /// --- Draw: PATH1 -void Renderer::draw(Mesh *t_meshes, u16 t_amount, LightBulb *t_bulbs, u16 t_bulbsCount) +void Renderer::draw(Mesh **t_meshes, u16 t_amount, LightBulb *t_bulbs, u16 t_bulbsCount) { - for (u16 i = 0; i < t_amount; i++) - draw(t_meshes[i], t_bulbs, t_bulbsCount); + beginFrameIfNeeded(); + if (!t_meshes[0]->isDataLoaded()) + PRINT_ERR("Can't draw, because no mesh data was loaded!"); + else if ( + t_amount >= 3 && + !t_meshes[0]->shouldBeBackfaceCulled && + t_meshes[0]->getFramesCount() == 1 && + t_meshes[0]->getMaterialsCount() == 1 && + t_meshes[0]->getFrame(0).getVertexCount() <= 96) + { + vifSender->disableWait(); + Mesh **meshesInFrustum = new Mesh *[t_amount]; + u16 addedMeshes = 0; + for (u16 i = 0; i < t_amount; i++) + if (t_meshes[i]->getMaterial(0).isInFrustum(renderData.frustumPlanes, t_meshes[i]->position)) + meshesInFrustum[addedMeshes++] = t_meshes[i]; + draw(*meshesInFrustum[0], t_bulbs, t_bulbsCount); + draw(*meshesInFrustum[1], t_bulbs, t_bulbsCount); + vifSender->enableWait(); + vifSender->drawTheSameWithOtherMatrices(renderData, meshesInFrustum, 2, addedMeshes); + delete[] meshesInFrustum; + } + else + for (u16 i = 0; i < t_amount; i++) + draw(*t_meshes[i], t_bulbs, t_bulbsCount); } void Renderer::draw(Mesh &t_mesh, LightBulb *t_bulbs, u16 t_bulbsCount) @@ -302,7 +323,7 @@ void Renderer::draw(Mesh &t_mesh, LightBulb *t_bulbs, u16 t_bulbsCount) } } -void Renderer::draw(Mesh *t_meshes, u16 t_amount) { draw(t_meshes, t_amount, NULL, 0); } +void Renderer::draw(Mesh **t_meshes, u16 t_amount) { draw(t_meshes, t_amount, NULL, 0); } void Renderer::draw(Mesh &t_mesh) { draw(t_mesh, NULL, 0); } diff --git a/src/engine/modules/vif_sender.cpp b/src/engine/modules/vif_sender.cpp index 78dd0ce..a72c50a 100644 --- a/src/engine/modules/vif_sender.cpp +++ b/src/engine/modules/vif_sender.cpp @@ -19,6 +19,8 @@ const u32 VU1_PACKAGE_VERTS_PER_BUFF = 96; // Remember to modify buffer size in vu1 also const u32 VU1_PACKAGES_PER_PACKET = 9; const u32 VU1_PACKET_SIZE = 256; // should be 128, but 256 is more safe for future +const u8 VU1_PARAMS_ADDRESS = 4; +const u8 VU1_RGBA_ADDRESS = 8; // ---- // Constructors/Destructors @@ -31,16 +33,18 @@ extern u32 VU1Draw3D_CodeEnd __attribute__((section(".vudata"))); VifSender::VifSender(Light *t_light) { - light = t_light; PRINT_LOG("Initializing VifSender"); - PRINT_LOG("VifSender initialized!"); + light = t_light; + lastVertCount = 0; + isDrawWaitEnabled = true; dma_channel_initialize(DMA_CHANNEL_VIF1, NULL, 0); dma_channel_fast_waits(DMA_CHANNEL_VIF1); uploadMicroProgram(); packets[0] = packet2_create(VU1_PACKET_SIZE, P2_TYPE_NORMAL, P2_MODE_CHAIN, true); packets[1] = packet2_create(VU1_PACKET_SIZE, P2_TYPE_NORMAL, P2_MODE_CHAIN, true); context = 0; - setDoubleBuffer(); + setDoubleBufferAddStaticData(); + PRINT_LOG("VifSender initialized!"); } VifSender::~VifSender() @@ -93,7 +97,7 @@ void VifSender::drawMesh(RenderData *t_renderData, Matrix t_perspective, u32 ver i -= 3; const u32 endI = i + (VU1_PACKAGE_VERTS_PER_BUFF - 1) > vertCount2 ? vertCount2 : i + (VU1_PACKAGE_VERTS_PER_BUFF - 1); - drawVertices(t_mesh, i, endI, vertices, coordinates, t_renderData->prim, textureBuffer); + drawVertices(t_mesh, i, endI, vertices, coordinates, t_renderData->prim, textureBuffer, isDrawWaitEnabled ? endI == vertCount2 : false); if (endI == vertCount2) // if there are no more vertices to draw, break { i = vertCount2; @@ -109,9 +113,15 @@ void VifSender::drawMesh(RenderData *t_renderData, Matrix t_perspective, u32 ver } } -void VifSender::setDoubleBuffer() +void VifSender::setDoubleBufferAddStaticData() { - packet2_t *settings = packet2_create(2, P2_TYPE_NORMAL, P2_MODE_CHAIN, true); + packet2_t *settings = packet2_create(10, P2_TYPE_NORMAL, P2_MODE_CHAIN, true); + packet2_utils_vu_open_unpack(settings, 0, false); + { + packet2_utils_gs_add_draw_finish_giftag(settings); + packet2_utils_gif_add_set(settings, 1); + } + packet2_utils_vu_close_unpack(settings); packet2_utils_vu_add_double_buffer(settings, 10, 498); packet2_utils_vu_add_end_tag(settings); dma_channel_send_packet2(settings, DMA_CHANNEL_VIF1, true); @@ -120,28 +130,98 @@ void VifSender::setDoubleBuffer() } /** Draw using PATH1 */ -void VifSender::drawVertices(Mesh &t_mesh, u32 t_start, u32 t_end, VECTOR *t_vertices, VECTOR *t_coordinates, prim_t *t_prim, texbuffer_t *textureBuffer) +void VifSender::drawVertices(Mesh &t_mesh, u32 t_start, u32 t_end, VECTOR *t_vertices, VECTOR *t_coordinates, prim_t *t_prim, texbuffer_t *t_texBuff, u8 t_addDrawWait) { const u32 vertCount = t_end - t_start; - u32 vif_added_bytes = 0; + lastVertCount = vertCount; packet2_utils_vu_open_unpack(currPacket, 0, true); packet2_add_data(currPacket, modelViewProj.data, 4); - packet2_add_u32(currPacket, 0); // Free + packet2_add_u32(currPacket, t_addDrawWait); // Draw finish? packet2_add_u32(currPacket, vertCount); // Vertex count packet2_add_u32(currPacket, vertCount / 3); // Triangles count packet2_add_u32(currPacket, 0); // Free - packet2_utils_gif_add_set(currPacket, 1); packet2_utils_gs_add_lod(currPacket, &t_mesh.lod); - packet2_utils_gs_add_texbuff_clut(currPacket, textureBuffer, &t_mesh.clut); + packet2_utils_gs_add_texbuff_clut(currPacket, t_texBuff, &t_mesh.clut); packet2_utils_gs_add_prim_giftag(currPacket, t_prim, vertCount, DRAW_STQ2_REGLIST, 3, 0); packet2_add_u32(currPacket, t_mesh.color.r); packet2_add_u32(currPacket, t_mesh.color.g); packet2_add_u32(currPacket, t_mesh.color.b); packet2_add_u32(currPacket, t_mesh.color.a); - vif_added_bytes += packet2_utils_vu_close_unpack(currPacket); + u32 vif_added_bytes = packet2_utils_vu_close_unpack(currPacket); packet2_utils_vu_add_unpack_data(currPacket, vif_added_bytes, t_vertices + t_start, vertCount, true); vif_added_bytes += vertCount; packet2_utils_vu_add_unpack_data(currPacket, vif_added_bytes, t_coordinates + t_start, vertCount, true); vif_added_bytes += vertCount; packet2_utils_vu_add_start_program(currPacket, 0); } + +void VifSender::drawTheSameWithOtherMatrices(const RenderData &t_renderData, Mesh **t_meshes, const u32 &t_skip, const u32 &t_count) +{ + packet2_t *packet2 = packet2_create(300, P2_TYPE_NORMAL, P2_MODE_CHAIN, true); + u8 switchCounter = 0; + u32 i; + for (i = t_skip; i < t_count; i++) + { + model.identity(); + model.rotate(t_meshes[i]->rotation); + model.translate(t_meshes[i]->position); + + modelViewProj.identity(); + modelViewProj = model * modelViewProj; + modelViewProj = *t_renderData.view * modelViewProj; + modelViewProj = *t_renderData.projection * modelViewProj; + + packet2_utils_vu_open_unpack(packet2, 0, true); + { + packet2_add_data(packet2, modelViewProj.data, 4); + } + packet2_utils_vu_close_unpack(packet2); + packet2_utils_vu_open_unpack(packet2, VU1_RGBA_ADDRESS, true); + { + packet2_add_u32(packet2, t_meshes[i]->color.r); + packet2_add_u32(packet2, t_meshes[i]->color.g); + packet2_add_u32(packet2, t_meshes[i]->color.b); + packet2_add_u32(packet2, t_meshes[i]->color.a); + } + packet2_utils_vu_close_unpack(packet2); + if (i != t_count - 1) + packet2_utils_vu_add_start_program(packet2, 0); + + if (switchCounter++ >= 32) + { + switchCounter = 0; + if (i == t_count - 1) // is last + { + packet2_utils_vu_open_unpack(packet2, VU1_PARAMS_ADDRESS, true); + { + packet2_add_u32(packet2, true); // Draw wait finish? + packet2_add_u32(packet2, lastVertCount); // Vertex count + packet2_add_u32(packet2, lastVertCount / 3); // Triangles count + packet2_add_u32(packet2, 0); // Free + } + packet2_utils_vu_close_unpack(packet2); + packet2_utils_vu_add_start_program(packet2, 0); + } + packet2_utils_vu_add_end_tag(packet2); + dma_channel_wait(DMA_CHANNEL_VIF1, 0); + dma_channel_send_packet2(packet2, DMA_CHANNEL_VIF1, 1); + packet2_reset(packet2, false); + } + } + if (switchCounter != 0) + { + packet2_utils_vu_open_unpack(packet2, VU1_PARAMS_ADDRESS, true); + { + packet2_add_u32(packet2, true); // Draw wait finish? + packet2_add_u32(packet2, lastVertCount); // Vertex count + packet2_add_u32(packet2, lastVertCount / 3); // Triangles count + packet2_add_u32(packet2, 0); // Free + } + packet2_utils_vu_close_unpack(packet2); + packet2_utils_vu_add_start_program(packet2, 0); + packet2_utils_vu_add_end_tag(packet2); + dma_channel_wait(DMA_CHANNEL_VIF1, 0); + dma_channel_send_packet2(packet2, DMA_CHANNEL_VIF1, 1); + } + packet2_free(packet2); +} diff --git a/src/engine/vu1_progs/draw3D.vclpp b/src/engine/vu1_progs/draw3D.vclpp index f38fc42..f50a7be 100644 --- a/src/engine/vu1_progs/draw3D.vclpp +++ b/src/engine/vu1_progs/draw3D.vclpp @@ -39,19 +39,24 @@ --enter --endenter +;///////////// LOAD STATIC DATA ///////////// +lq gif_draw_finish_tag, 0(vi00) ; GIF tag - wait +lq gif_set_tag, 1(vi00) ; GIF tag - set +;//////////////////////////////////////////// + xtop double_buffer ;//////// LOAD CURRENT BUFFER DATA ////////// MatrixLoad{ matrix, 0, double_buffer } -ilw.z triangles_count, 4(double_buffer) ; Triangles count +ilw.x do_draw_finish, 4(double_buffer) ; Add draw finish tag? (sync) ilw.y vertex_count, 4(double_buffer) ; Vertex count -lq gif_set_tag, 5(double_buffer) ; GIF tag - set -lq tex_gif_tag_1, 6(double_buffer) ; GIF tag - texture LOD -lq tex_gif_tag_2, 7(double_buffer) ; GIF tag - texture buffer & CLUT -lq prim_tag, 8(double_buffer) ; GIF tag - tell GS how many data we will send -lq rgba, 9(double_buffer) ; Mesh RGBA +ilw.z triangles_count, 4(double_buffer) ; Triangles count +lq tex_gif_tag_1, 5(double_buffer) ; GIF tag - texture LOD +lq tex_gif_tag_2, 6(double_buffer) ; GIF tag - texture buffer & CLUT +lq prim_tag, 7(double_buffer) ; GIF tag - tell GS how many data we will send +lq rgba, 8(double_buffer) ; Mesh RGBA -iaddiu vertex_data, double_buffer, 10 ; pointer to vertex data +iaddiu vertex_data, double_buffer, 9 ; pointer to vertex data iadd stq_data, vertex_data, vertex_count ; pointer to stq iadd kick_address, stq_data, vertex_count ; pointer for XGKICK iadd dest_address, stq_data, vertex_count ; helper pointer for data inserting @@ -65,6 +70,10 @@ sqi gif_set_tag, (dest_address++) ; sqi tex_gif_tag_1, (dest_address++) ; texture LOD tag sqi gif_set_tag, (dest_address++) ; sqi tex_gif_tag_2, (dest_address++) ; texture buffer & CLUT tag +iblez do_draw_finish, kick +sqi gif_set_tag, (dest_address++) ; +sqi gif_draw_finish_tag, (dest_address++) ; do draw_finish is needed +kick: sqi prim_tag, (dest_address++) ; prim + tell gs how many data will be ;//////////////////////////////////////////// @@ -135,6 +144,7 @@ triangle_loop: --LoopCS 1,3 --barrier + xgkick kick_address ; dispatch to the GS rasterizer. --exit diff --git a/src/engine/vu1_progs/draw3D.vsm b/src/engine/vu1_progs/draw3D.vsm index f557b72..e312e7e 100644 --- a/src/engine/vu1_progs/draw3D.vsm +++ b/src/engine/vu1_progs/draw3D.vsm @@ -3,6 +3,10 @@ ;-----VCL CODE------------ ;------------------------- ;------------------------- +; === __LP__ EXPL_draw3D_vcl_triangle_loop__MAIN_LOOP: +; === hDown : optimal=30 clid=0 mlid=2 size=(31) +; === dUp : optimal=30 clid=0 mlid=2 size=(31) +; === another : optimal=30 clid=0 mlid=2 size=(31) ; ================================================= ; flowMon::Emit() vcl 1.4beta7 produced this code: .vu @@ -11,83 +15,171 @@ .global VU1Draw3D_CodeEnd VU1Draw3D_CodeStart: __v_draw3D_vcl_4: -; _LNOPT_w=[ normal2 ] 27 [27 0] 27 [__v_draw3D_vcl_4] - NOP xtop VI07 - NOP lq VF01,0(VI07) - NOP ilw.y VI05,4(VI07) - NOP lq VF05,5(VI07) - NOP lq VF09,6(VI07) - NOP lq VF08,7(VI07) - NOP lq VF07,8(VI07) - NOP lq VF02,1(VI07) - NOP iaddiu VI03,VI07,0x0000000a - NOP iadd VI04,VI03,VI05 - NOP lq VF03,2(VI07) - NOP iadd VI06,VI04,VI05 +; _LNOPT_w=[ normal2 ] 26 [26 0] 26 [__v_draw3D_vcl_4] + NOP xtop VI05 + NOP lq VF02,1(VI00) + NOP lq VF01,0(VI00) + NOP ilw.x VI01,4(VI05) + NOP ilw.y VI07,4(VI05) + NOP lq VF03,0(VI05) + NOP lq VF08,5(VI05) + NOP lq VF10,6(VI05) + NOP lq VF04,1(VI05) + NOP lq VF05,2(VI05) + NOP iaddiu VI03,VI05,0x00000009 + NOP iadd VI04,VI03,VI07 + NOP lq VF06,3(VI05) + NOP iadd VI06,VI04,VI07 NOP loi 0x44fff000 - NOP lq VF04,3(VI07) - NOP ilw.z VI02,4(VI07) - NOP sqi VF05,(VI06++) - NOP sqi VF09,(VI06++) - NOP sqi VF05,(VI06++) + NOP ilw.z VI02,4(VI05) + NOP lq VF07,7(VI05) + NOP sqi VF02,(VI06++) NOP sqi VF08,(VI06++) - NOP lq VF05,9(VI07) - NOP iaddiu VI07,VI00,0x00007fff - NOP iadd VI05,VI04,VI05 - addi.xy VF06,VF00,I loi 0x492aaaaa + NOP sqi VF02,(VI06++) + NOP lq VF08,8(VI05) + NOP iadd VI05,VI04,VI07 + addi.xy VF09,VF00,I loi 0x492aaaaa NOP fcset 0 + NOP iblez VI01,kick + addi.z VF09,VF00,I sqi VF10,(VI06++) +; _LNOPT_w=[ normal2 ] 2 [2 0] 2 [__v_draw3D_vcl_5] + NOP sqi VF02,(VI06++) + NOP sqi VF01,(VI06++) +kick: +; _LNOPT_w=[ normal2 ] 27 [27 0] 32 [kick] NOP sqi VF07,(VI06++) - NOP iaddiu VI07,VI07,0x00000001 - addi.z VF06,VF00,I iaddiu VI08,VI00,0 -triangle_loop: -; _LNOPT_w=[ normal ] 37 [31 30] 45 [triangle_loop] NOP lq VF07,0(VI03) - mulax ACC,VF01,VF07x iaddiu VI08,VI08,0x00000001 ; STALL_LATENCY ?3 - madday ACC,VF02,VF07y NOP - maddaz ACC,VF03,VF07z NOP - maddw VF10,VF04,VF07w lq VF08,2(VI03) - mulax ACC,VF01,VF08x div Q,VF00w,VF10w ; STALL_LATENCY ?3 - madday ACC,VF02,VF08y NOP - maddaz ACC,VF03,VF08z lq VF07,1(VI03) - maddw VF08,VF04,VF08w NOP - clipw.xyz VF10xyz,VF10w NOP - mulax ACC,VF01,VF07x NOP ; STALL_LATENCY ?1 - madday ACC,VF02,VF07y div Q,VF00w,VF08w - maddaz ACC,VF03,VF07z lq VF09,0(VI04) - maddw VF07,VF04,VF07w NOP - mulaw.xyz ACC,VF06,VF00w NOP - mulq.xyz VF10,VF10,Q NOP - mulq VF09,VF09,Q sq VF05,1(VI06) - clipw.xyz VF07xyz,VF07w lq VF11,2(VI04) - clipw.xyz VF08xyz,VF08w div Q,VF00w,VF07w - mulq.xyz VF08,VF08,Q sq VF05,4(VI06) - madd.xyz VF09,VF10,VF06 sq VF09,0(VI06) - mulq VF10,VF11,Q sq VF05,7(VI06) - mulaw.xyz ACC,VF06,VF00w mfir.w VF09,VI07 - madd.xyz VF08,VF08,VF06 fcand VI01,262143 - NOP iaddiu VI01,VI01,0x00007fff - mulq.xyz VF10,VF07,Q sq VF10,6(VI06) - ftoi4.xyz VF09,VF09 mfir.w VF07,VI01 - ftoi4.xyz VF07,VF08 lq VF11,1(VI04) - mulaw.xyz ACC,VF06,VF00w iaddiu VI03,VI03,0x00000003 - madd.xyz VF08,VF10,VF06 iaddiu VI04,VI04,0x00000003 - NOP sq VF09,2(VI06) - mulq VF09,VF11,Q sq VF07,8(VI06) - NOP mfir.w VF07,VI07 - ftoi4.xyz VF07,VF08 iaddiu VI06,VI06,0x00000009 - NOP sq VF09,-6(VI06) ; STALL_LATENCY ?1 - NOP ibne VI08,VI02,triangle_loop - NOP sq VF07,-4(VI06) -; _LNOPT_w=[ normal2 ] 3 [1 0] 3 [__v_draw3D_vcl_7] + mulax ACC,VF03,VF07x lq VF14,1(VI03) ; STALL_LATENCY ?3 + madday ACC,VF04,VF07y sq VF08,1(VI06) + maddaz ACC,VF05,VF07z sq VF08,7(VI06) + maddw VF11,VF06,VF07w NOP + mulax ACC,VF03,VF14x lq VF07,2(VI03) + madday ACC,VF04,VF14y NOP + maddaz ACC,VF05,VF14z NOP + maddw VF14,VF06,VF14w div Q,VF00w,VF11w + mulax ACC,VF03,VF07x NOP + madday ACC,VF04,VF07y iaddiu VI07,VI00,0x00007fff + maddaz ACC,VF05,VF07z iaddiu VI07,VI07,0x00000001 + maddw VF07,VF06,VF07w iaddiu VI08,VI00,0 + clipw.xyz VF11xyz,VF11w iaddiu VI04,VI04,0 + clipw.xyz VF14xyz,VF14w div Q,VF00w,VF14w ; STALL_THRUPUT ?1 + clipw.xyz VF07xyz,VF07w lq VF15,0(VI04) + mulq.xyz VF02,VF11,Q sq VF08,4(VI06) + mulaw.xyz ACC,VF09,VF00w iaddiu VI03,VI03,0 + mulq VF11,VF15,Q iaddiu VI06,VI06,0 ; STALL_LATENCY ?1 + madd.xyz VF01,VF02,VF09 lq VF02,1(VI04) + NOP div Q,VF00w,VF07w + mulq.xyz VF15,VF14,Q iaddiu VI08,VI08,0x00000001 + mulaw.xyz ACC,VF09,VF00w sq VF11,0(VI06) + mulq VF14,VF02,Q mfir.w VF11,VI07 + ftoi4.xyz VF11,VF01 ibeq VI08,VI02,EXPL_draw3D_vcl_triangle_loop__EPI1 + madd.xyz VF02,VF15,VF09 lq VF15,2(VI04) +; _LNOPT_w=[ ] 31 [29 0] 31 [EXPL_draw3D_vcl_triangle_loop__PRO1] + NOP lq VF12,3(VI03) + NOP NOP + mulq.xyz VF01,VF07,Q NOP + mulq VF11,VF15,Q sq VF11,2(VI06) + mulax ACC,VF03,VF12x lq VF10,4(VI03) + madday ACC,VF04,VF12y NOP + maddaz ACC,VF05,VF12z NOP + maddw VF11,VF06,VF12w sq VF11,6(VI06) + mulax ACC,VF03,VF10x lq VF07,5(VI03) + madday ACC,VF04,VF10y sq VF14,3(VI06) + maddaz ACC,VF05,VF10z sq VF08,10(VI06) + maddw VF10,VF06,VF10w div Q,VF00w,VF11w + mulax ACC,VF03,VF07x sq VF08,13(VI06) + madday ACC,VF04,VF07y sq VF08,16(VI06) + maddaz ACC,VF05,VF07z iaddiu VI03,VI03,0x00000006 + maddw VF07,VF06,VF07w iaddiu VI04,VI04,0x00000006 + clipw.xyz VF11xyz,VF11w lq VF12,-3(VI04) + clipw.xyz VF10xyz,VF10w iaddiu VI06,VI06,0x00000012 + mulq.xyz VF14,VF11,Q div Q,VF00w,VF10w + clipw.xyz VF07xyz,VF07w fcand VI01,262143 + mulq VF12,VF12,Q iaddiu VI08,VI08,0x00000001 + ftoi4.xyz VF11,VF02 iaddiu VI01,VI01,0x00007fff + mulaw.xyz ACC,VF09,VF00w mfir.w VF11,VI07 + madd.xyz VF14,VF14,VF09 mfir.w VF10,VI01 + mulaw.xyz ACC,VF09,VF00w lq VF02,-2(VI04) + madd.xyz VF01,VF01,VF09 div Q,VF00w,VF07w + mulq.xyz VF10,VF10,Q lq VF15,-1(VI04) + ftoi4.xyz VF11,VF14 sq VF11,-13(VI06) + mulq VF14,VF02,Q sq VF12,-9(VI06) + mulaw.xyz ACC,VF09,VF00w ibeq VI08,VI02,EXPL_draw3D_vcl_triangle_loop__EPI0 + madd.xyz VF02,VF10,VF09 mfir.w VF11,VI07 +EXPL_draw3D_vcl_triangle_loop__MAIN_LOOP: +; _LPOPT_w=[ hDown ] 31 [30 30] 31 [EXPL_draw3D_vcl_triangle_loop__MAIN_LOOP] + ftoi4.xyz VF10,VF01 lq VF13,0(VI03) + NOP lq VF12,1(VI03) + mulq.xyz VF01,VF07,Q sq VF14,-6(VI06) + mulq VF11,VF15,Q sq VF11,-7(VI06) + mulax ACC,VF03,VF13x sq VF10,-10(VI06) + madday ACC,VF04,VF13y sq VF08,1(VI06) + maddaz ACC,VF05,VF13z NOP + maddw VF11,VF06,VF13w sq VF11,-3(VI06) + mulax ACC,VF03,VF12x lq VF07,2(VI03) + madday ACC,VF04,VF12y sq VF08,4(VI06) + maddaz ACC,VF05,VF12z sq VF08,7(VI06) + maddw VF10,VF06,VF12w div Q,VF00w,VF11w + mulax ACC,VF03,VF07x NOP + madday ACC,VF04,VF07y iaddiu VI03,VI03,0x00000003 + maddaz ACC,VF05,VF07z NOP + maddw VF07,VF06,VF07w iaddiu VI04,VI04,0x00000003 + clipw.xyz VF11xyz,VF11w lq VF12,-3(VI04) + clipw.xyz VF10xyz,VF10w iaddiu VI06,VI06,0x00000009 + mulq.xyz VF13,VF11,Q fcand VI01,262143 + clipw.xyz VF07xyz,VF07w div Q,VF00w,VF10w + mulq VF12,VF12,Q iaddiu VI08,VI08,0x00000001 + ftoi4.xyz VF11,VF02 iaddiu VI01,VI01,0x00007fff + mulaw.xyz ACC,VF09,VF00w mfir.w VF10,VI01 + madd.xyz VF13,VF13,VF09 mfir.w VF11,VI07 + mulaw.xyz ACC,VF09,VF00w lq VF02,-2(VI04) + madd.xyz VF01,VF01,VF09 sq VF12,-9(VI06) + mulq.xyz VF10,VF10,Q div Q,VF00w,VF07w + ftoi4.xyz VF11,VF13 sq VF11,-13(VI06) + mulq VF14,VF02,Q mfir.w VF11,VI07 + mulaw.xyz ACC,VF09,VF00w ibne VI08,VI02,EXPL_draw3D_vcl_triangle_loop__MAIN_LOOP + madd.xyz VF02,VF10,VF09 lq VF15,-1(VI04) +EXPL_draw3D_vcl_triangle_loop__EPI0: +; _LNOPT_w=[ ] 13 [13 0] 15 [EXPL_draw3D_vcl_triangle_loop__EPI0] + NOP NOP + ftoi4.xyz VF10,VF01 fcand VI01,262143 + mulq.xyz VF01,VF07,Q sq VF14,-6(VI06) + mulq VF11,VF15,Q sq VF11,-7(VI06) + mulaw.xyz ACC,VF09,VF00w iaddiu VI01,VI01,0x00007fff + madd.xyz VF01,VF01,VF09 sq VF10,-10(VI06) ; STALL_LATENCY ?1 + ftoi4.xyz VF11,VF02 sq VF11,-3(VI06) + NOP mfir.w VF11,VI07 + NOP mfir.w VF10,VI01 + ftoi4.xyz VF10,VF01 NOP + NOP sq VF11,-4(VI06) ; STALL_LATENCY ?1 + NOP b EXPL_draw3D_vcl_triangle_loop__EXIT_POINT + NOP sq VF10,-1(VI06) +EXPL_draw3D_vcl_triangle_loop__EPI1: +; _LNOPT_w=[ ] 12 [13 0] 14 [EXPL_draw3D_vcl_triangle_loop__EPI1] + NOP NOP + mulq.xyz VF14,VF07,Q sq VF14,3(VI06) + NOP NOP + mulq VF11,VF15,Q sq VF11,2(VI06) + mulaw.xyz ACC,VF09,VF00w fcand VI01,262143 + madd.xyz VF14,VF14,VF09 iaddiu VI01,VI01,0x00007fff + NOP mfir.w VF14,VI01 + ftoi4.xyz VF11,VF02 sq VF11,6(VI06) + NOP mfir.w VF11,VI07 + ftoi4.xyz VF14,VF14 NOP + NOP sq VF11,5(VI06) ; STALL_LATENCY ?2 + NOP sq VF14,8(VI06) +EXPL_draw3D_vcl_triangle_loop__EXIT_POINT: +; _LNOPT_w=[ ] 0 [0 0] 0 [EXPL_draw3D_vcl_triangle_loop__EXIT_POINT] +; _LNOPT_w=[ normal2 ] 3 [1 0] 3 [__v_draw3D_vcl_9] NOP xgkick VI05 NOP[E] NOP NOP NOP .align 4 VU1Draw3D_CodeEnd: -; iCount=67 +; iCount=145 ; register stats: ; 9 VU User integer -; 12 VU User floating point +; 16 VU User floating point ;------------------------- ;------------------------- ;------------------------- From 91772a905c6ae0c3f187146050635ab614e1ab30 Mon Sep 17 00:00:00 2001 From: h4570 Date: Sat, 26 Dec 2020 13:31:30 +0100 Subject: [PATCH 101/108] cleanup --- src/samples/floors/objects/enemy.cpp | 30 ++++++++++------------------ src/samples/floors/objects/enemy.hpp | 4 +++- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/src/samples/floors/objects/enemy.cpp b/src/samples/floors/objects/enemy.cpp index b7a46a8..57bf4b9 100644 --- a/src/samples/floors/objects/enemy.cpp +++ b/src/samples/floors/objects/enemy.cpp @@ -26,33 +26,25 @@ Enemy::Enemy(TextureRepository *t_texRepo) PRINT_LOG("Creating enemy object"); meshes = new Mesh[getMeshesCount()]; + drawMeshes = new Mesh *[getMeshesCount()]; meshes[0].loadMD2("meshes/enemy/", "poss_head", 0.3F, false); - meshes[0].rotation.x = -1.566F; - meshes[0].rotation.z = 1.566F; - meshes[0].shouldBeFrustumCulled = false; - meshes[1].loadMD2("meshes/enemy/", "poss_body", 0.3F, false); - meshes[1].rotation.x = -1.566F; - meshes[1].rotation.z = 1.566F; - meshes[1].shouldBeFrustumCulled = false; - meshes[2].loadMD2("meshes/enemy/", "poss_weapon", 0.3F, false); - meshes[2].rotation.x = -1.566F; - meshes[2].rotation.z = 1.566F; - meshes[2].shouldBeFrustumCulled = false; + + for (u8 i = 0; i < 3; i++) + { + meshes[i].rotation.x = -1.566F; + meshes[i].rotation.z = 1.566F; + meshes[i].shouldBeFrustumCulled = false; + t_texRepo->addByMesh("meshes/enemy/", meshes[i], PNG); + meshes[i].playAnimation(1, 6); + drawMeshes[i] = &meshes[i]; + } Vector3 initPos = Vector3(0.00F, 40.00F, 0.00F); setPosition(initPos); - t_texRepo->addByMesh("meshes/enemy/", meshes[0], PNG); - t_texRepo->addByMesh("meshes/enemy/", meshes[1], PNG); - t_texRepo->addByMesh("meshes/enemy/", meshes[2], PNG); - - meshes[0].playAnimation(1, 6); - meshes[1].playAnimation(1, 6); - meshes[2].playAnimation(1, 6); - isKilled = true; PRINT_LOG("Enemy object created!"); diff --git a/src/samples/floors/objects/enemy.hpp b/src/samples/floors/objects/enemy.hpp index 601a4e9..4ed530a 100644 --- a/src/samples/floors/objects/enemy.hpp +++ b/src/samples/floors/objects/enemy.hpp @@ -24,7 +24,7 @@ public: Enemy(TextureRepository *t_texRepo); ~Enemy(); - inline Mesh *getMeshes() const { return meshes; } + inline Mesh **getMeshes() const { return drawMeshes; } inline u8 getMeshesCount() const { return 3; } inline Vector3 getPosition() const { return position; } void setPosition(const Vector3 &t_vec); @@ -35,6 +35,8 @@ public: private: /** 0 - head, 1 - body, 2 - weapon */ Mesh *meshes; + /** Only for draw() method. */ + Mesh **drawMeshes; const Floor *currentFloor; Vector3 position, currFloorMin, currFloorMax; u8 isKilled; From 7f9f86d7c37ad3fb7bc9e7c76b7ae28e9e28b6aa Mon Sep 17 00:00:00 2001 From: h4570 Date: Sat, 26 Dec 2020 13:31:43 +0100 Subject: [PATCH 102/108] added draw() in array mode --- src/samples/floors/floors.cpp | 9 +++++++-- src/samples/floors/managers/floor_manager.cpp | 4 ++++ src/samples/floors/managers/floor_manager.hpp | 2 ++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/samples/floors/floors.cpp b/src/samples/floors/floors.cpp index 10e9872..82e5766 100644 --- a/src/samples/floors/floors.cpp +++ b/src/samples/floors/floors.cpp @@ -60,8 +60,13 @@ void Floors::onUpdate() player->update(engine->pad, camera, *floorManager, *enemy); engine->renderer->draw(player->mesh); engine->renderer->draw(enemy->getMeshes(), enemy->getMeshesCount()); - for (u16 i = 0; i < FLOORS_COUNT; i++) - engine->renderer->draw(floorManager->floors[i].mesh, lightManager.bulbs, lightManager.bulbsCount); + + // You can draw array of meshes in draw() function also. It can be A LOT faster than for looping! + // Why? When given mesh is small, is not animated and is not backface culled, vertex data are send once! + engine->renderer->draw(floorManager->getMeshes(), FLOORS_COUNT, lightManager.bulbs, lightManager.bulbsCount); + // draw() in array mode, sometimes will force you to do synchronization between EE <-> VU1 <-> GS via: + // renderer->clearAndWaitForRender(); or renderer->waitForRender(); + ui->render(engine->renderer); // 2D rendering ist LAST step, because layers gonna play there. } diff --git a/src/samples/floors/managers/floor_manager.cpp b/src/samples/floors/managers/floor_manager.cpp index e1b1d09..24a6b40 100644 --- a/src/samples/floors/managers/floor_manager.cpp +++ b/src/samples/floors/managers/floor_manager.cpp @@ -22,6 +22,7 @@ */ FloorManager::FloorManager(int t_floorAmount, TextureRepository *t_texRepo) { + meshes = new Mesh *[t_floorAmount]; texRepo = t_texRepo; floorAmount = t_floorAmount; spirals = new Point[t_floorAmount]; @@ -85,12 +86,15 @@ void FloorManager::initFloors() floors[0].mesh.loadObj("meshes/floor/", "floor", 3.0F, false); floors[0].mesh.shouldBeFrustumCulled = true; floors[0].mesh.shouldBeLighted = true; + meshes[0] = &floors[0].mesh; texRepo->addByMesh("meshes/floor/", floors[0].mesh, BMP); for (u16 i = 1; i < floorAmount; i++) { + floors[i].mesh.shouldBeFrustumCulled = true; floors[i].init(floors[0].mesh, spirals[i], i); texRepo->getByMesh(floors[0].mesh.getId(), floors[0].mesh.getMaterial(0).getId()) ->addLink(floors[i].mesh.getId(), floors[i].mesh.getMaterial(0).getId()); + meshes[i] = &floors[i].mesh; } PRINT_LOG("Floors initialized!"); } diff --git a/src/samples/floors/managers/floor_manager.hpp b/src/samples/floors/managers/floor_manager.hpp index 61a64b7..342aefd 100644 --- a/src/samples/floors/managers/floor_manager.hpp +++ b/src/samples/floors/managers/floor_manager.hpp @@ -31,8 +31,10 @@ public: u16 floorAmount; void update(Player &t_player); void onAudioTick(); + Mesh **getMeshes() { return meshes; } private: + Mesh **meshes; TextureRepository *texRepo; u8 audioOffset, audioMode; u32 audioTick; From d0c2dfcaa3f74aeefc26ed568e878a4eb5dde76a Mon Sep 17 00:00:00 2001 From: h4570 Date: Sat, 26 Dec 2020 17:29:07 +0100 Subject: [PATCH 103/108] disabled file service --- src/engine/engine.cpp | 5 +- src/engine/include/engine.hpp | 2 +- src/engine/include/modules/audio.hpp | 2 +- src/engine/loaders/png_loader.cpp | 99 ++++++---------------------- src/engine/modules/audio.cpp | 17 ++--- src/samples/floors/floors.cpp | 6 +- 6 files changed, 38 insertions(+), 93 deletions(-) diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index e9ce377..65a111e 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -77,8 +77,9 @@ void Engine::firePS2() { SifInitRpc(0); srand(time(NULL)); - fileService.startThread(); - audio.startThread(&fileService); + // fileService.startThread(); + // audio.startThread(&fileService); + audio.startThread(NULL); isInitialized = 0; mainThreadId = GetThreadId(); } diff --git a/src/engine/include/engine.hpp b/src/engine/include/engine.hpp index 5f10f65..17be5a8 100644 --- a/src/engine/include/engine.hpp +++ b/src/engine/include/engine.hpp @@ -32,7 +32,7 @@ public: void init(Game *t_game, u32 t_gifPacketSize); void setDefaultScreen(); Renderer *renderer; - FileService fileService; + // FileService fileService; Audio audio; ScreenSettings screen; Pad pad; diff --git a/src/engine/include/modules/audio.hpp b/src/engine/include/modules/audio.hpp index ea1dfe2..be2709a 100644 --- a/src/engine/include/modules/audio.hpp +++ b/src/engine/include/modules/audio.hpp @@ -126,7 +126,7 @@ public: private: u8 songLoaded, volume, realVolume, songPlaying, songInLoop, songFinished; - std::vector songListeners; + std::vector songListeners; FILE *wav; audsrv_fmt_t format; FileService *fileService; diff --git a/src/engine/loaders/png_loader.cpp b/src/engine/loaders/png_loader.cpp index bc46b9f..402800c 100644 --- a/src/engine/loaders/png_loader.cpp +++ b/src/engine/loaders/png_loader.cpp @@ -38,13 +38,11 @@ void PngLoader::load(Texture &o_texture, char *t_subfolder, char *t_name, char * char *path = String::createConcatenated(path_part2, t_extension); delete[] path_part1; delete[] path_part2; + FILE *file = fopen(path, "rb"); if (file == NULL) - { - PRINT_ERR("Failed to load .png file!"); - return; - } + PRINT_ERR("Failed to open .png file!"); png_structp png_ptr; png_infop info_ptr; @@ -56,29 +54,15 @@ void PngLoader::load(Texture &o_texture, char *t_subfolder, char *t_name, char * png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, NULL, NULL); if (!png_ptr) - { - printf("PNG Read Struct Init Failed\n"); - fclose(file); - return; - } + PRINT_ERR("PNG struct info init failed(1)!"); info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) - { - printf("PNG Info Struct Init Failed\n"); - fclose(file); - png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL); - return; - } + PRINT_ERR("PNG struct info init failed(2)!"); if (setjmp(png_jmpbuf(png_ptr))) - { - printf("Got PNG Error!\n"); - png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); - fclose(file); - return; - } + PRINT_ERR("PNG reader fatal error!"); png_init_io(png_ptr, file); @@ -114,80 +98,39 @@ void PngLoader::load(Texture &o_texture, char *t_subfolder, char *t_name, char * type = TEX_TYPE_RGB; break; default: - PRINT_ERR("This png format is not supported!"); + PRINT_ERR("This png format is not supported! RGB/RGBA only."); } o_texture.setSize(width, height, type); printf("PNGLoader - width: %d | height: %d\n", width, height); - if (type == TEX_TYPE_RGBA) - { - int row_bytes = png_get_rowbytes(png_ptr, info_ptr); - png_byte *row_pointers[height]; + size_t row_bytes = png_get_rowbytes(png_ptr, info_ptr); + png_byte *row_pointers[height]; + for (row = 0; row < height; row++) + row_pointers[row] = new png_byte[row_bytes]; - for (row = 0; row < height; row++) - row_pointers[row] = new png_byte[row_bytes]; + png_read_image(png_ptr, row_pointers); - png_read_image(png_ptr, row_pointers); + u32 x = 0; - struct pixel + for (i = 0; i < height; i++) + for (j = 0; j < width; j++) { - u8 r, g, b, a; - }; - - u32 x = 0; - for (i = 0; i < height; i++) - { - for (j = 0; j < width; j++) + o_texture.setData(x, row_pointers[i][4 * j]); + o_texture.setData(x + 1, row_pointers[i][4 * j + 1]); + o_texture.setData(x + 2, row_pointers[i][4 * j + 2]); + if (type == TEX_TYPE_RGBA) { - o_texture.setData(x, row_pointers[i][4 * j]); - o_texture.setData(x + 1, row_pointers[i][4 * j + 1]); - o_texture.setData(x + 2, row_pointers[i][4 * j + 2]); o_texture.setData(x + 3, ((int)row_pointers[i][4 * j + 3] * 128 / 255)); x += 4; } - } - - for (row = 0; row < height; row++) - delete row_pointers[row]; - } - else if (type == TEX_TYPE_RGB) - { - int row_bytes = png_get_rowbytes(png_ptr, info_ptr); - png_byte *row_pointers[height]; - - for (row = 0; row < height; row++) - row_pointers[row] = new png_byte[row_bytes]; - - png_read_image(png_ptr, row_pointers); - - struct pixel3 - { - u8 r, g, b; - }; - - u32 x = 0; - for (i = 0; i < height; i++) - { - for (j = 0; j < width; j++) - { - o_texture.setData(x, row_pointers[i][4 * j]); - o_texture.setData(x + 1, row_pointers[i][4 * j + 1]); - o_texture.setData(x + 2, row_pointers[i][4 * j + 2]); + else if (type == TEX_TYPE_RGB) x += 3; - } } - for (row = 0; row < height; row++) - delete row_pointers[row]; - } - else - { - printf("This texture depth is not supported yet!\n"); - return; - } + for (row = 0; row < height; row++) + delete[] row_pointers[row]; - // Texture->Filter = GS_FILTER_NEAREST; png_read_end(png_ptr, NULL); png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); diff --git a/src/engine/modules/audio.cpp b/src/engine/modules/audio.cpp index 2d7e836..369413c 100644 --- a/src/engine/modules/audio.cpp +++ b/src/engine/modules/audio.cpp @@ -89,24 +89,25 @@ void Audio::setSongVolume(const u8 &t_vol) u32 Audio::addSongListener(AudioListener *t_listener) { - AudioListenerRef ref; - ref.id = rand() % 1000000; - ref.listener = t_listener; + AudioListenerRef *ref = new AudioListenerRef; + ref->id = rand() % 1000000; + ref->listener = t_listener; songListeners.push_back(ref); - return ref.id; + return ref->id; } void Audio::removeSongListener(const u32 &t_id) { s32 index = -1; for (u32 i = 0; i < songListeners.size(); i++) - if (songListeners[i].id == t_id) + if (songListeners[i]->id == t_id) { index = i; break; } if (index == -1) PRINT_ERR("Cant remove listener because given id was not found!"); + delete songListeners[index]; songListeners.erase(songListeners.begin() + index); } @@ -160,7 +161,7 @@ void Audio::playADPCM(audsrv_adpcm_t *t_adpcm, const s8 &t_ch) void Audio::startThread(FileService *t_fileService) { PRINT_LOG("Creating audio thread"); - fileService = t_fileService; + // fileService = t_fileService; extern void *_gp; thread.func = (void *)Audio::mainThread; thread.stack = threadStack; @@ -186,7 +187,7 @@ void Audio::threadLoop() { printf("Running again.\n"); for (u32 i = 0; i < getSongListenersCount(); i++) - songListeners[i].listener->onAudioFinish(); + songListeners[i]->listener->onAudioFinish(); rewindSongToStart(); } else @@ -202,7 +203,7 @@ void Audio::threadLoop() WaitSema(fillbufferSema); // wait until previous chunk wasn't finished audsrv_play_audio(wavChunk, chunkReadStatus); for (u32 i = 0; i < getSongListenersCount(); i++) - songListeners[i].listener->onAudioTick(); + songListeners[i]->listener->onAudioTick(); } chunkReadStatus = fread(wavChunk, 1, sizeof(wavChunk), wav); diff --git a/src/samples/floors/floors.cpp b/src/samples/floors/floors.cpp index 82e5766..d83aaee 100644 --- a/src/samples/floors/floors.cpp +++ b/src/samples/floors/floors.cpp @@ -37,15 +37,15 @@ Floors::~Floors() void Floors::onInit() { + texRepo = engine->renderer->getTextureRepository(); setBgColorAndAmbientColor(); engine->renderer->setCameraDefinitions(&camera.view, &camera.unitCirclePosition, camera.planes); + enemy = new Enemy(texRepo); // I know that here is bug with PNG loading + ui = new Ui(texRepo); // Will be resolved in December version engine->audio.addSongListener(this); engine->audio.loadSong("sounds/mafikizolo-loot.wav"); engine->audio.playSong(); engine->audio.setSongVolume(80); - texRepo = engine->renderer->getTextureRepository(); - enemy = new Enemy(texRepo); - ui = new Ui(texRepo); floorManager = new FloorManager(FLOORS_COUNT, texRepo); player = new Player(&engine->audio, texRepo); } From e987089df0ed074e7a2e3a7a0a188c9c1b1d66ad Mon Sep 17 00:00:00 2001 From: h4570 Date: Sun, 27 Dec 2020 13:39:44 +0100 Subject: [PATCH 104/108] fixed floors bug --- src/samples/floors/floors.cpp | 10 +++++----- src/samples/floors/managers/floor_manager.cpp | 3 +-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/samples/floors/floors.cpp b/src/samples/floors/floors.cpp index d83aaee..15e8346 100644 --- a/src/samples/floors/floors.cpp +++ b/src/samples/floors/floors.cpp @@ -38,16 +38,16 @@ Floors::~Floors() void Floors::onInit() { texRepo = engine->renderer->getTextureRepository(); + enemy = new Enemy(texRepo); + ui = new Ui(texRepo); + floorManager = new FloorManager(FLOORS_COUNT, texRepo); // we must init FloorManager before starting audio! + player = new Player(&engine->audio, texRepo); setBgColorAndAmbientColor(); engine->renderer->setCameraDefinitions(&camera.view, &camera.unitCirclePosition, camera.planes); - enemy = new Enemy(texRepo); // I know that here is bug with PNG loading - ui = new Ui(texRepo); // Will be resolved in December version engine->audio.addSongListener(this); engine->audio.loadSong("sounds/mafikizolo-loot.wav"); engine->audio.playSong(); - engine->audio.setSongVolume(80); - floorManager = new FloorManager(FLOORS_COUNT, texRepo); - player = new Player(&engine->audio, texRepo); + engine->audio.setSongVolume(1); } void Floors::onUpdate() diff --git a/src/samples/floors/managers/floor_manager.cpp b/src/samples/floors/managers/floor_manager.cpp index 24a6b40..2662527 100644 --- a/src/samples/floors/managers/floor_manager.cpp +++ b/src/samples/floors/managers/floor_manager.cpp @@ -30,10 +30,9 @@ FloorManager::FloorManager(int t_floorAmount, TextureRepository *t_texRepo) calcSpiral(floorSpiralMaxOffset, floorSpiralMaxOffset); initFloors(); trick = 0.0F; - trickMode = 0; isTimeForChangeTriggerColor = true; isTimeForChangeDefaultColor = true; - audioOffset = audioMode = audioTick = 0; + audioOffset = audioMode = audioTick = trickMode = 0; } FloorManager::~FloorManager() From c7fe915174a7ddd9a9aad72be2c0dea64245cf07 Mon Sep 17 00:00:00 2001 From: h4570 Date: Sun, 27 Dec 2020 13:41:32 +0100 Subject: [PATCH 105/108] fixed volume --- src/samples/floors/floors.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/samples/floors/floors.cpp b/src/samples/floors/floors.cpp index 15e8346..e1d7179 100644 --- a/src/samples/floors/floors.cpp +++ b/src/samples/floors/floors.cpp @@ -47,7 +47,7 @@ void Floors::onInit() engine->audio.addSongListener(this); engine->audio.loadSong("sounds/mafikizolo-loot.wav"); engine->audio.playSong(); - engine->audio.setSongVolume(1); + engine->audio.setSongVolume(80); } void Floors::onUpdate() From 1d7730504bc01e2c38d35e48f7eb0e4cd4a034d5 Mon Sep 17 00:00:00 2001 From: h4570 Date: Sun, 27 Dec 2020 19:09:50 +0100 Subject: [PATCH 106/108] fixed sync between processors --- src/engine/include/modules/renderer.hpp | 22 ++++++-- src/engine/modules/renderer.cpp | 5 ++ src/engine/modules/vif_sender.cpp | 72 +++++++++++++++---------- src/samples/floors/floors.cpp | 2 - 4 files changed, 66 insertions(+), 35 deletions(-) diff --git a/src/engine/include/modules/renderer.hpp b/src/engine/include/modules/renderer.hpp index 913770d..f86d07b 100644 --- a/src/engine/include/modules/renderer.hpp +++ b/src/engine/include/modules/renderer.hpp @@ -40,12 +40,26 @@ public: void enableVSync() { isVSyncEnabled = true; } void disableVSync() { isVSyncEnabled = false; } - void clearAndWaitForRender() + + /** Reset draw wait flag. */ + inline void resetWaitFlag() { - *GS_REG_CSR |= 2; // Reset wait flag - draw_wait_finish(); + *GS_REG_CSR |= 2; + } + + /** Checks if draw wait flag is set */ + inline u8 isWaitFlagSet() const + { + return *GS_REG_CSR & 2; + } + + /** Waits for draw wait flag and reset it. */ + inline void waitForRender() + { + while (!isWaitFlagSet()) + ; + resetWaitFlag(); } - void waitForRender() { draw_wait_finish(); } /** 2D draw. */ void draw(Sprite &t_sprite); diff --git a/src/engine/modules/renderer.cpp b/src/engine/modules/renderer.cpp index 35961b3..b76c8be 100644 --- a/src/engine/modules/renderer.cpp +++ b/src/engine/modules/renderer.cpp @@ -287,7 +287,12 @@ void Renderer::draw(Mesh **t_meshes, u16 t_amount, LightBulb *t_bulbs, u16 t_bul draw(*meshesInFrustum[0], t_bulbs, t_bulbsCount); draw(*meshesInFrustum[1], t_bulbs, t_bulbsCount); vifSender->enableWait(); + resetWaitFlag(); vifSender->drawTheSameWithOtherMatrices(renderData, meshesInFrustum, 2, addedMeshes); + if (isWaitFlagSet()) + resetWaitFlag(); + else + waitForRender(); delete[] meshesInFrustum; } else diff --git a/src/engine/modules/vif_sender.cpp b/src/engine/modules/vif_sender.cpp index a72c50a..2215685 100644 --- a/src/engine/modules/vif_sender.cpp +++ b/src/engine/modules/vif_sender.cpp @@ -157,7 +157,10 @@ void VifSender::drawVertices(Mesh &t_mesh, u32 t_start, u32 t_end, VECTOR *t_ver void VifSender::drawTheSameWithOtherMatrices(const RenderData &t_renderData, Mesh **t_meshes, const u32 &t_skip, const u32 &t_count) { + packet2_t *packet1 = packet2_create(300, P2_TYPE_NORMAL, P2_MODE_CHAIN, true); packet2_t *packet2 = packet2_create(300, P2_TYPE_NORMAL, P2_MODE_CHAIN, true); + packet2_t *currMPacket = packet1; + u8 currPacketIndex = 1; u8 switchCounter = 0; u32 i; for (i = t_skip; i < t_count; i++) @@ -171,57 +174,68 @@ void VifSender::drawTheSameWithOtherMatrices(const RenderData &t_renderData, Mes modelViewProj = *t_renderData.view * modelViewProj; modelViewProj = *t_renderData.projection * modelViewProj; - packet2_utils_vu_open_unpack(packet2, 0, true); + packet2_utils_vu_open_unpack(currMPacket, 0, true); { - packet2_add_data(packet2, modelViewProj.data, 4); + packet2_add_data(currMPacket, modelViewProj.data, 4); } - packet2_utils_vu_close_unpack(packet2); - packet2_utils_vu_open_unpack(packet2, VU1_RGBA_ADDRESS, true); + packet2_utils_vu_close_unpack(currMPacket); + packet2_utils_vu_open_unpack(currMPacket, VU1_RGBA_ADDRESS, true); { - packet2_add_u32(packet2, t_meshes[i]->color.r); - packet2_add_u32(packet2, t_meshes[i]->color.g); - packet2_add_u32(packet2, t_meshes[i]->color.b); - packet2_add_u32(packet2, t_meshes[i]->color.a); + packet2_add_u32(currMPacket, t_meshes[i]->color.r); + packet2_add_u32(currMPacket, t_meshes[i]->color.g); + packet2_add_u32(currMPacket, t_meshes[i]->color.b); + packet2_add_u32(currMPacket, t_meshes[i]->color.a); } - packet2_utils_vu_close_unpack(packet2); + packet2_utils_vu_close_unpack(currMPacket); if (i != t_count - 1) - packet2_utils_vu_add_start_program(packet2, 0); + packet2_utils_vu_add_start_program(currMPacket, 0); if (switchCounter++ >= 32) { switchCounter = 0; if (i == t_count - 1) // is last { - packet2_utils_vu_open_unpack(packet2, VU1_PARAMS_ADDRESS, true); + packet2_utils_vu_open_unpack(currMPacket, VU1_PARAMS_ADDRESS, true); { - packet2_add_u32(packet2, true); // Draw wait finish? - packet2_add_u32(packet2, lastVertCount); // Vertex count - packet2_add_u32(packet2, lastVertCount / 3); // Triangles count - packet2_add_u32(packet2, 0); // Free + packet2_add_u32(currMPacket, true); // Draw wait finish? + packet2_add_u32(currMPacket, lastVertCount); // Vertex count + packet2_add_u32(currMPacket, lastVertCount / 3); // Triangles count + packet2_add_u32(currMPacket, 0); // Free } - packet2_utils_vu_close_unpack(packet2); - packet2_utils_vu_add_start_program(packet2, 0); + packet2_utils_vu_close_unpack(currMPacket); + packet2_utils_vu_add_start_program(currMPacket, 0); } - packet2_utils_vu_add_end_tag(packet2); + packet2_utils_vu_add_end_tag(currMPacket); dma_channel_wait(DMA_CHANNEL_VIF1, 0); - dma_channel_send_packet2(packet2, DMA_CHANNEL_VIF1, 1); - packet2_reset(packet2, false); + dma_channel_send_packet2(currMPacket, DMA_CHANNEL_VIF1, 1); + if (currPacketIndex == 1) + { + currPacketIndex = 2; + currMPacket = packet2; + } + else + { + currPacketIndex = 1; + currMPacket = packet1; + } + packet2_reset(currMPacket, false); } } if (switchCounter != 0) { - packet2_utils_vu_open_unpack(packet2, VU1_PARAMS_ADDRESS, true); + packet2_utils_vu_open_unpack(currMPacket, VU1_PARAMS_ADDRESS, true); { - packet2_add_u32(packet2, true); // Draw wait finish? - packet2_add_u32(packet2, lastVertCount); // Vertex count - packet2_add_u32(packet2, lastVertCount / 3); // Triangles count - packet2_add_u32(packet2, 0); // Free + packet2_add_u32(currMPacket, true); // Draw wait finish? + packet2_add_u32(currMPacket, lastVertCount); // Vertex count + packet2_add_u32(currMPacket, lastVertCount / 3); // Triangles count + packet2_add_u32(currMPacket, 0); // Free } - packet2_utils_vu_close_unpack(packet2); - packet2_utils_vu_add_start_program(packet2, 0); - packet2_utils_vu_add_end_tag(packet2); + packet2_utils_vu_close_unpack(currMPacket); + packet2_utils_vu_add_start_program(currMPacket, 0); + packet2_utils_vu_add_end_tag(currMPacket); dma_channel_wait(DMA_CHANNEL_VIF1, 0); - dma_channel_send_packet2(packet2, DMA_CHANNEL_VIF1, 1); + dma_channel_send_packet2(currMPacket, DMA_CHANNEL_VIF1, 1); } + packet2_free(packet1); packet2_free(packet2); } diff --git a/src/samples/floors/floors.cpp b/src/samples/floors/floors.cpp index e1d7179..330c68a 100644 --- a/src/samples/floors/floors.cpp +++ b/src/samples/floors/floors.cpp @@ -64,8 +64,6 @@ void Floors::onUpdate() // You can draw array of meshes in draw() function also. It can be A LOT faster than for looping! // Why? When given mesh is small, is not animated and is not backface culled, vertex data are send once! engine->renderer->draw(floorManager->getMeshes(), FLOORS_COUNT, lightManager.bulbs, lightManager.bulbsCount); - // draw() in array mode, sometimes will force you to do synchronization between EE <-> VU1 <-> GS via: - // renderer->clearAndWaitForRender(); or renderer->waitForRender(); ui->render(engine->renderer); // 2D rendering ist LAST step, because layers gonna play there. } From 55f476e9ed4a1e1067f98105cec50038997de971 Mon Sep 17 00:00:00 2001 From: h4570 Date: Sun, 27 Dec 2020 19:10:14 +0100 Subject: [PATCH 107/108] fixed sync between draw many --- src/engine/modules/vif_sender.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/engine/modules/vif_sender.cpp b/src/engine/modules/vif_sender.cpp index 2215685..394c653 100644 --- a/src/engine/modules/vif_sender.cpp +++ b/src/engine/modules/vif_sender.cpp @@ -157,13 +157,15 @@ void VifSender::drawVertices(Mesh &t_mesh, u32 t_start, u32 t_end, VECTOR *t_ver void VifSender::drawTheSameWithOtherMatrices(const RenderData &t_renderData, Mesh **t_meshes, const u32 &t_skip, const u32 &t_count) { + // Standard double buffering (packets) packet2_t *packet1 = packet2_create(300, P2_TYPE_NORMAL, P2_MODE_CHAIN, true); packet2_t *packet2 = packet2_create(300, P2_TYPE_NORMAL, P2_MODE_CHAIN, true); packet2_t *currMPacket = packet1; u8 currPacketIndex = 1; + + // We are sending 32 matrices max per one VU1 send u8 switchCounter = 0; - u32 i; - for (i = t_skip; i < t_count; i++) + for (u32 i = t_skip; i < t_count; i++) { model.identity(); model.rotate(t_meshes[i]->rotation); @@ -187,7 +189,8 @@ void VifSender::drawTheSameWithOtherMatrices(const RenderData &t_renderData, Mes packet2_add_u32(currMPacket, t_meshes[i]->color.a); } packet2_utils_vu_close_unpack(currMPacket); - if (i != t_count - 1) + + if (i != t_count - 1) // if it is last, we must also add draw wait finish interrupt. packet2_utils_vu_add_start_program(currMPacket, 0); if (switchCounter++ >= 32) @@ -203,12 +206,12 @@ void VifSender::drawTheSameWithOtherMatrices(const RenderData &t_renderData, Mes packet2_add_u32(currMPacket, 0); // Free } packet2_utils_vu_close_unpack(currMPacket); - packet2_utils_vu_add_start_program(currMPacket, 0); + packet2_utils_vu_add_start_program(currMPacket, 0); // and start program } packet2_utils_vu_add_end_tag(currMPacket); dma_channel_wait(DMA_CHANNEL_VIF1, 0); dma_channel_send_packet2(currMPacket, DMA_CHANNEL_VIF1, 1); - if (currPacketIndex == 1) + if (currPacketIndex == 1) // Switch double buffer (packets) { currPacketIndex = 2; currMPacket = packet2; @@ -221,7 +224,7 @@ void VifSender::drawTheSameWithOtherMatrices(const RenderData &t_renderData, Mes packet2_reset(currMPacket, false); } } - if (switchCounter != 0) + if (switchCounter != 0) // if there are any not sended matrices { packet2_utils_vu_open_unpack(currMPacket, VU1_PARAMS_ADDRESS, true); { From ea0d47e09c070526c6c303008b402711828774a4 Mon Sep 17 00:00:00 2001 From: h4570 Date: Sun, 27 Dec 2020 19:49:36 +0100 Subject: [PATCH 108/108] cleanup & changed water logic --- src/samples/dolphin/camera.cpp | 20 +--- src/samples/dolphin/dolphin.cpp | 111 ++++++++++++++------ src/samples/dolphin/dolphin.hpp | 13 ++- src/samples/dolphin/main.cpp | 2 +- src/samples/dolphin/objects/collectible.cpp | 4 - src/samples/dolphin/objects/collectible.hpp | 1 - src/samples/dolphin/objects/mine.cpp | 4 - src/samples/dolphin/objects/mine.hpp | 1 - src/samples/dolphin/objects/player.cpp | 18 +--- src/samples/dolphin/objects/player.hpp | 1 - 10 files changed, 91 insertions(+), 84 deletions(-) diff --git a/src/samples/dolphin/camera.cpp b/src/samples/dolphin/camera.cpp index 855428f..69d8669 100755 --- a/src/samples/dolphin/camera.cpp +++ b/src/samples/dolphin/camera.cpp @@ -12,7 +12,7 @@ #include -const float CAMERA_Y = 15.0F; +const float CAMERA_Y = 25.0F; Camera::Camera(ScreenSettings *t_screen) : CameraBase(t_screen, &position) { @@ -40,12 +40,7 @@ void Camera::rotate(Pad &t_pad) horizontalLevel -= 0.08; else if (t_pad.rJoyH >= 200) horizontalLevel += 0.08; - /* - if (t_pad.rJoyV <= 50 && verticalLevel > 25.0F) - verticalLevel -= 0.9F; - else if (t_pad.rJoyV >= 200 && verticalLevel < 80.0F) - verticalLevel += 0.9F; - */ + unitCirclePosition.x = (Math::sin(horizontalLevel) * verticalLevel); unitCirclePosition.y = CAMERA_Y; unitCirclePosition.z = (Math::cos(horizontalLevel) * verticalLevel); @@ -56,15 +51,4 @@ void Camera::followBy(Mesh &t_mesh) position.x = unitCirclePosition.x + t_mesh.position.x; position.y = unitCirclePosition.y + t_mesh.position.y; position.z = unitCirclePosition.z + t_mesh.position.z; - - if (position.x > (-0.04F * verticalLevel) && - position.x < 0.0F && - position.z > (0.998F * verticalLevel)) - { - // TODO - // position.x = 0.0F; - // position.y = 0.0F; - // position.z = 0.0F; - // horizontalLevel = 0.0F; - } } \ No newline at end of file diff --git a/src/samples/dolphin/dolphin.cpp b/src/samples/dolphin/dolphin.cpp index 11f4032..f016115 100755 --- a/src/samples/dolphin/dolphin.cpp +++ b/src/samples/dolphin/dolphin.cpp @@ -11,14 +11,12 @@ #include "dolphin.hpp" #include "utils/math.hpp" -const u8 WATER_TILE_SCALE = 5; -const u8 WATER_SIZE = 100; - +float Dolphin::engineFPS = 60.0F; +const float WATER_TILE_SCALE = 6.0F; +const u8 WATER_SIZE = 4; const u8 MINES_COUNT = 15; const u8 OYSTERS_COUNT = 5; -float Dolphin::engineFPS = 60.F; - Dolphin::Dolphin(Engine *t_engine) : engine(t_engine), camera(&engine->screen) { oysters = new Collectible[OYSTERS_COUNT]; @@ -29,6 +27,7 @@ Dolphin::~Dolphin() { delete[] oysters; delete[] mines; + delete[] waterDrawList; } void Dolphin::onInit() @@ -73,11 +72,26 @@ void Dolphin::onInit() printf("Loaded.\n"); printf("Loading water...\n"); - water.loadObj("water/", "water", WATER_TILE_SCALE, false); - water.position.set(0, WATER_LEVEL, 0); - texRepo->addByMesh("water/", water, BMP); - water.shouldBeFrustumCulled = false; - water.shouldBeBackfaceCulled = false; + + u32 spiralOffset = (u32)Math::sqrt(WATER_TILES_COUNT); + waterDrawList = new Mesh *[WATER_TILES_COUNT]; + calcSpiral(spiralOffset, spiralOffset); + water[0].loadObj("water/", "water", WATER_TILE_SCALE, false); + water[0].position.set(0.0F, WATER_LEVEL, 0.0F); + texRepo->addByMesh("water/", water[0], BMP); + water[0].shouldBeFrustumCulled = true; + water[0].shouldBeBackfaceCulled = false; + waterDrawList[0] = &water[0]; + for (size_t i = 1; i < WATER_TILES_COUNT; i++) + { + water[i].loadFrom(water[0]); + water[i].shouldBeFrustumCulled = true; + water[i].shouldBeBackfaceCulled = false; + water[i].position.set(WATER_TILE_SCALE * 2.0F * spirals[i].x, WATER_LEVEL, WATER_TILE_SCALE * 2.0F * spirals[i].y); + texRepo->getByMesh(water[0].getId(), water[0].getMaterial(0).getId()) + ->addLink(water[i].getId(), water[i].getMaterial(0).getId()); + waterDrawList[i] = &water[i]; + } printf("Loading seabed...\n"); seabed.loadObj("seabed/", "seabed", 10.0F, false); @@ -96,7 +110,6 @@ void Dolphin::onInit() waterbox.position.y = -100.F; printf("Loading oyster dff\n"); - //oysters[0].loadDff("oyster/", "oyster", 1.F, false); oysters[0].mesh.loadObj("oyster/", "oyster", 10.F, false); printf("Loaded."); oysters[0].mesh.shouldBeBackfaceCulled = false; @@ -117,7 +130,6 @@ void Dolphin::onInit() printf("Loading mine .obj\n"); mines[0].mesh.loadObj("mine/", "mine", 10.F, false); - printf("Loaded."); mines[0].mesh.shouldBeBackfaceCulled = false; mines[0].mesh.shouldBeFrustumCulled = false; mines[0].mesh.position.set(5, -15, 32); @@ -135,7 +147,8 @@ void Dolphin::onInit() } Texture *pLifeTex = texRepo->add("2d/", "life", PNG); - //pLifeTex->addLink(lifeSprites[0].getId()); + // pLifeTex->addLink(lifeSprites[0].getId()); + for (int i = 0; i < 3; i++) { lifeSprites[i].size.set(64.0F, 64.0F); @@ -164,12 +177,13 @@ void Dolphin::onUpdate() engine->renderer->draw(gameOver); return; } - Dolphin::engineFPS = engine->fps; - if (engine->pad.isCrossClicked) - printf("Delta multiplier: %f\n", 60.0F / engine->fps); - float xDist = player.mesh.position.x - water.position.x; - float zDist = player.mesh.position.z - water.position.z; + Dolphin::engineFPS = engine->fps; + // if (engine->pad.isCrossClicked) + // printf("Delta multiplier: %f\n", 60.0F / engine->fps); + + float xDist = player.mesh.position.x - water[0].position.x; + float zDist = player.mesh.position.z - water[0].position.z; if (xDist < 0) xDist *= -1; if (zDist < 0) @@ -177,8 +191,11 @@ void Dolphin::onUpdate() if (xDist > WATER_SIZE * WATER_TILE_SCALE / 4 || zDist > WATER_SIZE * WATER_TILE_SCALE / 4) { - water.position.x = (player.mesh.position.x / WATER_SIZE) * WATER_SIZE; - water.position.z = (player.mesh.position.z / WATER_SIZE) * WATER_SIZE; + for (size_t i = 0; i < WATER_TILES_COUNT; i++) + { + water[i].position.x = player.mesh.position.x + (WATER_TILE_SCALE * 2.0F * spirals[i].x); + water[i].position.z = player.mesh.position.z + (WATER_TILE_SCALE * 2.0F * spirals[i].y); + } } for (int i = 0; i < OYSTERS_COUNT; i++) @@ -188,7 +205,7 @@ void Dolphin::onUpdate() float dist = vecDist.length(); if (dist < 20.F && player.isJumping() && oysters[i].isActive()) { - printf("Pickup %d Dist %d\n", i, dist); + printf("Pickup:%d Dist:%f\n", i, dist); oysters[i].disappear(); player.setLife(player.getLifes() + 1); engine->audio.setADPCMVolume(30, 3); @@ -204,11 +221,11 @@ void Dolphin::onUpdate() if (dist < 20.F && !mines[i].getExplosionTicks()) { - printf("Vecdist %f,%f,%f\n", vecDist.x, vecDist.y, vecDist.z); - printf("Explode %d Dist %d\n", i, dist); + printf("Vecdist: %f,%f,%f\n", vecDist.x, vecDist.y, vecDist.z); + printf("Explode:%d Dist:%f\n", i, dist); mines[i].explode(); player.setLife(player.getLifes() - 1); - //Buffer switching to prevent playing two sounds on one buffer + // Buffer switching to prevent playing two sounds on one buffer engine->audio.setADPCMVolume(65, Mine::lastSoundBuffer); engine->audio.playADPCM(boomSound, Mine::lastSoundBuffer); Mine::lastSoundBuffer = Mine::lastSoundBuffer == 4 ? 5 : 4; @@ -220,35 +237,34 @@ void Dolphin::onUpdate() player.update(engine->pad); camera.update(engine->pad, player.mesh); + skybox.position.set(player.mesh.position.x, +200.0F, player.mesh.position.z); waterbox.position.set(player.mesh.position.x, -260.0F, player.mesh.position.z); + engine->renderer->draw(island); - engine->renderer->draw(water); + engine->renderer->draw(waterDrawList, WATER_TILES_COUNT); engine->renderer->draw(skybox); engine->renderer->draw(waterbox); engine->renderer->draw(seabed); + for (u8 i = 0; i < OYSTERS_COUNT; i++) - { if (oysters[i].isActive()) - { engine->renderer->draw(oysters[i].mesh); - } - } + for (u8 i = 0; i < MINES_COUNT; i++) - { if (mines[i].getExplosionTicks() < MINE_EXPLOSION_TICKS) engine->renderer->draw(mines[i].mesh); - } + for (u8 i = 0; i < player.getLifes(); i++) - { engine->renderer->draw(lifeSprites[i]); - } + engine->renderer->draw(player.mesh); + if (camera.position.y < WATER_LEVEL) { engine->renderer->draw(waterOverlay); - engine->audio.setADPCMVolume(0, 0); //Surface ambient - engine->audio.setADPCMVolume(65, 1); //Underwater ambient + engine->audio.setADPCMVolume(0, 0); // Surface ambient + engine->audio.setADPCMVolume(65, 1); // Underwater ambient } else { @@ -256,3 +272,28 @@ void Dolphin::onUpdate() engine->audio.setADPCMVolume(0, 1); //Underwater ambient } } + +void Dolphin::calcSpiral(int X, int Y) +{ + int x, y, dx; + x = y = dx = 0; + int dy = -1; + int t = X > Y ? X : Y; + int maxI = t * t; + for (int i = 0; i < maxI; i++) + { + if ((-X / 2 <= x) && (x <= X / 2) && (-Y / 2 <= y) && (y <= Y / 2)) + { + spirals[i].x = x; + spirals[i].y = y; + } + if ((x == y) || ((x < 0) && (x == -y)) || ((x > 0) && (x == 1 - y))) + { + t = dx; + dx = -dy; + dy = t; + } + x += dx; + y += dy; + } +} diff --git a/src/samples/dolphin/dolphin.hpp b/src/samples/dolphin/dolphin.hpp index ad7b3c1..5ee4d05 100755 --- a/src/samples/dolphin/dolphin.hpp +++ b/src/samples/dolphin/dolphin.hpp @@ -37,8 +37,15 @@ public: static float engineFPS; private: - /*DO NOT TOUCH ORDER OF DECLARATION! - GCC BUG CAUSES RENDERING TO FAIL IN ANY OTHER ORDER OF DECLARATION!*/ + void calcSpiral(int X, int Y); + static const u16 WATER_TILES_COUNT = 1024; + + Point spirals[WATER_TILES_COUNT]; + Mesh **waterDrawList; + /** + * DO NOT TOUCH ORDER OF DECLARATION! + * GCC BUG CAUSES RENDERING TO FAIL IN ANY OTHER ORDER OF DECLARATION! + */ Mesh seabed; Mesh island; Mesh skybox; @@ -49,7 +56,7 @@ private: Collectible *oysters; TextureRepository *texRepo; Sprite waterOverlay; - Mesh water; + Mesh water[WATER_TILES_COUNT]; Mesh waterbox; Sprite lifeSprites[3]; Sprite gameOver; diff --git a/src/samples/dolphin/main.cpp b/src/samples/dolphin/main.cpp index d094a17..fdf11cb 100755 --- a/src/samples/dolphin/main.cpp +++ b/src/samples/dolphin/main.cpp @@ -18,4 +18,4 @@ int main() game.engine->init(&game, 128); SleepThread(); return 0; -} \ No newline at end of file +} diff --git a/src/samples/dolphin/objects/collectible.cpp b/src/samples/dolphin/objects/collectible.cpp index 9cd7d78..0bdd01c 100644 --- a/src/samples/dolphin/objects/collectible.cpp +++ b/src/samples/dolphin/objects/collectible.cpp @@ -25,10 +25,6 @@ Collectible::Collectible() bDisappearing = false; } -Collectible::~Collectible() -{ -} - void Collectible::disappear() { bDisappearing = true; diff --git a/src/samples/dolphin/objects/collectible.hpp b/src/samples/dolphin/objects/collectible.hpp index 32795ea..bac0b37 100644 --- a/src/samples/dolphin/objects/collectible.hpp +++ b/src/samples/dolphin/objects/collectible.hpp @@ -21,7 +21,6 @@ public: float rotation; Mesh mesh; Collectible(); - ~Collectible(); void update(); void disappear(); void setActive(const u8 &b) { bActive = b; } diff --git a/src/samples/dolphin/objects/mine.cpp b/src/samples/dolphin/objects/mine.cpp index 9a7a19b..a64da3a 100644 --- a/src/samples/dolphin/objects/mine.cpp +++ b/src/samples/dolphin/objects/mine.cpp @@ -20,10 +20,6 @@ Mine::Mine() explosionTicks = 0; } -Mine::~Mine() -{ -} - void Mine::update() { if (bExploding && explosionTicks < MINE_EXPLOSION_TICKS) diff --git a/src/samples/dolphin/objects/mine.hpp b/src/samples/dolphin/objects/mine.hpp index 5dce106..51faa71 100644 --- a/src/samples/dolphin/objects/mine.hpp +++ b/src/samples/dolphin/objects/mine.hpp @@ -24,7 +24,6 @@ public: void explode() { bExploding = true; } void update(); Mine(); - ~Mine(); const u8 &getExplosionTicks() { return explosionTicks; } private: diff --git a/src/samples/dolphin/objects/player.cpp b/src/samples/dolphin/objects/player.cpp index 84669cc..718f33a 100755 --- a/src/samples/dolphin/objects/player.cpp +++ b/src/samples/dolphin/objects/player.cpp @@ -28,10 +28,6 @@ Player::Player() lifes = 3; } -Player::~Player() -{ -} - void Player::init(Engine *t_engine) { pEngine = t_engine; @@ -63,8 +59,7 @@ void Player::update(Pad &t_pad) if (t_pad.isCrossClicked && mesh.getCurrentAnimationFrame() <= 14 && WATER_LEVEL - 15 < mesh.position.y) { - printf("Cross\n"); - mesh.setAnimSpeed(0.8F); + mesh.setAnimSpeed(1.0F); mesh.playAnimation(14, 58, 0); } @@ -72,19 +67,10 @@ void Player::update(Pad &t_pad) bIsJumping = true; else bIsJumping = false; - /* - if (lift < 0) - mesh.rotation.y = 1; - else if (lift > 0) - mesh.rotation.y = -1; - else - mesh.rotation.y = 0; - */ if (velocity > 0 && mesh.getCurrentAnimationFrame() == 0) { mesh.setAnimSpeed(0.05F); - printf("Swim animation\n"); mesh.playAnimation(1, 14, 0); } if (mesh.getCurrentAnimationFrame() == 42) @@ -101,7 +87,7 @@ void Player::update(Pad &t_pad) bIsImpactingWater = false; } - velocity *= 60.0F / Dolphin::engineFPS; //Moving by deltatime of last frame. + velocity *= 60.0F / Dolphin::engineFPS; // Moving by deltatime of last frame. mesh.position.z += Math::cos(mesh.rotation.z) * velocity * (bIsJumping + 1); mesh.position.x += diff --git a/src/samples/dolphin/objects/player.hpp b/src/samples/dolphin/objects/player.hpp index 3aed608..93cde51 100755 --- a/src/samples/dolphin/objects/player.hpp +++ b/src/samples/dolphin/objects/player.hpp @@ -25,7 +25,6 @@ public: float gravity, velocity, lift; Mesh mesh; Player(); - ~Player(); void init(Engine *t_engine); void update(Pad &t_pad); const u8 &isJumping() { return bIsJumping; }