fixed frustum culling, static game class

This commit is contained in:
Sandro Sobczyński
2020-11-04 13:20:58 +01:00
parent 5399b1744f
commit 02560f2d6f
19 changed files with 73 additions and 70 deletions
+18 -16
View File
@@ -30,13 +30,14 @@ extern u32 VU1Draw3D_CodeEnd __attribute__((section(".vudata")));
Engine::Engine() Engine::Engine()
{ {
SifInitRpc(0); setDefaultScreen();
srand(time(NULL)); firePS2();
VU1::uploadProgram(0, &VU1Draw3D_CodeStart, &VU1Draw3D_CodeEnd); }
audio.startThread();
isInitialized = 0; Engine::Engine(const ScreenSettings &t_screen)
isScreenInitialized = 0; {
mainThreadId = GetThreadId(); screen = t_screen;
firePS2();
} }
Engine::~Engine() {} Engine::~Engine() {}
@@ -45,12 +46,6 @@ Engine::~Engine() {}
// Methods // Methods
// ---- // ----
void Engine::setScreen(ScreenSettings &t_settings)
{
screen = t_settings;
isScreenInitialized = true;
}
void Engine::setDefaultScreen() void Engine::setDefaultScreen()
{ {
screen.projectionScale = 4096.0F; screen.projectionScale = 4096.0F;
@@ -60,13 +55,10 @@ void Engine::setDefaultScreen()
screen.aspectRatio = 4.0F / 3.0F; screen.aspectRatio = 4.0F / 3.0F;
screen.width = 640.0F; screen.width = 640.0F;
screen.height = 480.0F; screen.height = 480.0F;
isScreenInitialized = true;
} }
void Engine::init(Game *t_game, u32 t_gifPacketSize) void Engine::init(Game *t_game, u32 t_gifPacketSize)
{ {
if (!isScreenInitialized)
setDefaultScreen();
if (isInitialized) if (isInitialized)
PRINT_ERR("Already initialized!"); PRINT_ERR("Already initialized!");
else else
@@ -88,6 +80,16 @@ void Engine::wakeup(s32 t_alarmId, u16 t_time, void *t_common)
ExitHandler(); ExitHandler();
} }
void Engine::firePS2()
{
SifInitRpc(0);
srand(time(NULL));
VU1::uploadProgram(0, &VU1Draw3D_CodeStart, &VU1Draw3D_CodeEnd);
audio.startThread();
isInitialized = 0;
mainThreadId = GetThreadId();
}
void Engine::gameLoop() void Engine::gameLoop()
{ {
for (;;) for (;;)
+3 -2
View File
@@ -24,12 +24,12 @@ class Engine
{ {
public: public:
Engine(const ScreenSettings &screen);
Engine(); Engine();
~Engine(); ~Engine();
void init(Game *t_game, u32 t_gifPacketSize); void init(Game *t_game, u32 t_gifPacketSize);
void setDefaultScreen(); void setDefaultScreen();
void setScreen(ScreenSettings &t_settings);
Renderer *renderer; Renderer *renderer;
Audio audio; Audio audio;
ScreenSettings screen; ScreenSettings screen;
@@ -37,9 +37,10 @@ public:
float fps; float fps;
private: private:
void firePS2();
u8 fpsDelayer; u8 fpsDelayer;
Timer timer; Timer timer;
u8 isInitialized, isScreenInitialized; u8 isInitialized;
void gameLoop(); void gameLoop();
Game *game; Game *game;
s32 mainThreadId; s32 mainThreadId;
+1 -1
View File
@@ -35,7 +35,7 @@ public:
void makeZRotation(float t_radians); void makeZRotation(float t_radians);
void translate(float t_x, float t_y, float t_z); void translate(float t_x, float t_y, float t_z);
void setPerspective(ScreenSettings &screen); void setPerspective(ScreenSettings &screen);
void print(); const void print() const;
}; };
#endif #endif
+1 -1
View File
@@ -27,7 +27,7 @@ public:
void update(Vector3 &a, Vector3 &b, Vector3 &c); void update(Vector3 &a, Vector3 &b, Vector3 &c);
inline float distanceTo(Vector3 &t_vec) { return this->distance + this->normal.innerProduct(t_vec); } inline float distanceTo(Vector3 &t_vec) { return this->distance + this->normal.innerProduct(t_vec); }
void print(); const void print() const;
}; };
#endif #endif
+1 -1
View File
@@ -33,7 +33,7 @@ public:
void set(const float &t_x, const float &t_y); void set(const float &t_x, const float &t_y);
void set(const Point &v); void set(const Point &v);
void print(); const void print() const;
}; };
#endif #endif
+1 -1
View File
@@ -54,7 +54,7 @@ public:
void set(const float &x, const float &y, const float &z); void set(const float &x, const float &y, const float &z);
void copy(Vector3 &v); void copy(Vector3 &v);
float distanceTo(Vector3 &v); float distanceTo(Vector3 &v);
void print(); const void print() const;
}; };
#endif #endif
@@ -21,7 +21,6 @@ struct ScreenSettings
float aspectRatio; float aspectRatio;
float nearPlaneDist; float nearPlaneDist;
float farPlaneDist; float farPlaneDist;
/** change it to 4096.0F to check out frustum culling! 😎 */
float projectionScale; float projectionScale;
}; };
+3 -3
View File
@@ -187,8 +187,8 @@ void Matrix::setPerspective(ScreenSettings &t_screen)
{ {
float fovYdiv2 = Math::HALF_ANG2RAD * t_screen.fov; float fovYdiv2 = Math::HALF_ANG2RAD * t_screen.fov;
float cotFOV = 1.0F / (Math::sin(fovYdiv2) / Math::cos(fovYdiv2)); float cotFOV = 1.0F / (Math::sin(fovYdiv2) / Math::cos(fovYdiv2));
float w = cotFOV * (t_screen.width / 4096.0F) / t_screen.aspectRatio; float w = cotFOV * (t_screen.width / t_screen.projectionScale) / t_screen.aspectRatio;
float h = cotFOV * (t_screen.height / 4096.0F); float h = cotFOV * (t_screen.height / t_screen.projectionScale);
this->data[0] = w; this->data[0] = w;
this->data[1] = 0.0F; this->data[1] = 0.0F;
@@ -253,7 +253,7 @@ void Matrix::lookAt(Vector3 &t_up, Vector3 &t_position, Vector3 &t_target)
data[15] = 1; data[15] = 1;
} }
void Matrix::print() 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", 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[0], data[1], data[2], data[3],
+1 -1
View File
@@ -44,7 +44,7 @@ void Plane::update(Vector3 &a, Vector3 &b, Vector3 &c)
this->distance = -this->normal.innerProduct(b); this->distance = -this->normal.innerProduct(b);
} }
void Plane::print() const void Plane::print() const
{ {
printf("Plane(Vector3(%f, %f, %f), %f)\n", this->normal.x, this->normal.y, this->normal.z, this->distance); printf("Plane(Vector3(%f, %f, %f), %f)\n", this->normal.x, this->normal.y, this->normal.z, this->distance);
} }
+1 -1
View File
@@ -55,7 +55,7 @@ void Point::set(const Point &v)
y = v.y; y = v.y;
} }
void Point::print() const void Point::print() const
{ {
printf("Point(%f, %f)\n", x, y); printf("Point(%f, %f)\n", x, y);
} }
+1 -1
View File
@@ -295,7 +295,7 @@ float Vector3::distanceTo(Vector3 &v)
// (this->z - another.z) * (this->z - another.z)); // (this->z - another.z) * (this->z - another.z));
} }
void Vector3::print() const void Vector3::print() const
{ {
printf("Vector3(%f, %f, %f)\n", x, y, z); printf("Vector3(%f, %f, %f)\n", x, y, z);
} }
+3 -2
View File
@@ -71,14 +71,15 @@ u8 MeshMaterial::isInFrustum(Plane *t_frustumPlanes, const Vector3 &position)
{ {
Vector3 boxCalcTemp; Vector3 boxCalcTemp;
u8 boxResult = 1, boxIn = 0, boxOut = 0; u8 boxResult = 1, boxIn = 0, boxOut = 0;
for (int i = 0; i < 6; i++)
for (u8 i = 0; i < 6; i++)
{ {
boxOut = 0; boxOut = 0;
boxIn = 0; boxIn = 0;
// for each corner of the box do ... // for each corner of the box do ...
// get out of the cycle as soon as a box as corners // get out of the cycle as soon as a box as corners
// both inside and out of the frustum // both inside and out of the frustum
for (int y = 0; y < 8 && (boxIn == 0 || boxOut == 0); y++) for (u8 y = 0; y < 8 && (boxIn == 0 || boxOut == 0); y++)
{ {
boxCalcTemp.set( boxCalcTemp.set(
boundingBox[y].x + position.x, boundingBox[y].x + position.x,
+15 -15
View File
@@ -18,7 +18,7 @@
const u8 WATER_TILES_COUNT = 64; const u8 WATER_TILES_COUNT = 64;
Ari::Ari(const Engine &t_engine) : engine(t_engine), camera(&engine.screen) Ari::Ari(Engine *t_engine) : engine(t_engine), camera(&engine->screen)
{ {
waterFloors = new Mesh[WATER_TILES_COUNT]; waterFloors = new Mesh[WATER_TILES_COUNT];
spirals = new Point[WATER_TILES_COUNT]; spirals = new Point[WATER_TILES_COUNT];
@@ -33,13 +33,13 @@ Ari::~Ari() {}
void Ari::onInit() void Ari::onInit()
{ {
engine.renderer->setCameraDefinitions(&camera.worldView, &camera.unitCirclePosition, camera.planes); engine->renderer->setCameraDefinitions(&camera.worldView, &camera.unitCirclePosition, camera.planes);
engine.audio.init(0); engine->audio.init(0);
engine.audio.setVolume(40); engine->audio.setVolume(40);
engine.audio.loadSong("MOV-CIRC.WAV"); engine->audio.loadSong("MOV-CIRC.WAV");
engine.audio.play(); engine->audio.play();
texRepo = engine.renderer->getTextureRepository(); texRepo = engine->renderer->getTextureRepository();
island.loadDff("sunnyisl/", "sunnyisl", 0.1F, false); island.loadDff("sunnyisl/", "sunnyisl", 0.1F, false);
island.rotation.x = -1.6F; island.rotation.x = -1.6F;
@@ -86,15 +86,15 @@ void Ari::initBulb()
void Ari::onUpdate() void Ari::onUpdate()
{ {
if (engine.pad.isCrossClicked) if (engine->pad.isCrossClicked)
printf("FPS:%f\n", engine.fps); printf("FPS:%f\n", engine->fps);
camera.update(engine.pad, player.mesh); camera.update(engine->pad, player.mesh);
engine.renderer->draw(skybox); engine->renderer->draw(skybox);
engine.renderer->draw(island); engine->renderer->draw(island);
engine.renderer->draw(islandAddons); engine->renderer->draw(islandAddons);
engine.renderer->draw(player.mesh); engine->renderer->draw(player.mesh);
for (u8 i = 0; i < WATER_TILES_COUNT; i++) for (u8 i = 0; i < WATER_TILES_COUNT; i++)
engine.renderer->draw(waterFloors[i]); engine->renderer->draw(waterFloors[i]);
} }
void Ari::calcSpiral(int X, int Y) void Ari::calcSpiral(int X, int Y)
+2 -2
View File
@@ -23,13 +23,13 @@ class Ari : public Game
{ {
public: public:
Ari(const Engine &t_engine); Ari(Engine *t_engine);
~Ari(); ~Ari();
void onInit(); void onInit();
void onUpdate(); void onUpdate();
Engine engine; Engine *engine;
private: private:
void initBulb(); void initBulb();
+2 -2
View File
@@ -13,8 +13,8 @@
int main() int main()
{ {
Engine engine = Engine(); Engine engine = Engine();
Ari game = Ari(engine); Ari game = Ari(&engine);
game.engine.init(&game, 40000); game.engine->init(&game, 40000);
SleepThread(); SleepThread();
return 0; return 0;
} }
+16 -15
View File
@@ -16,8 +16,8 @@
const u8 FLOORS_COUNT = 64; // Temp change it also in floor_manager.hpp const u8 FLOORS_COUNT = 64; // Temp change it also in floor_manager.hpp
Floors::Floors(const Engine &t_engine) Floors::Floors(Engine *t_engine)
: engine(t_engine), floorManager(FLOORS_COUNT), camera(&engine.screen) {} : engine(t_engine), floorManager(FLOORS_COUNT), camera(&t_engine->screen) {}
Floors::~Floors() {} Floors::~Floors() {}
@@ -27,13 +27,14 @@ Floors::~Floors() {}
void Floors::onInit() void Floors::onInit()
{ {
engine.renderer->setCameraDefinitions(&camera.worldView, &camera.unitCirclePosition, camera.planes); engine->renderer->setCameraDefinitions(&camera.worldView, &camera.unitCirclePosition, camera.planes);
engine.audio.init(2); engine->audio.init(2);
engine.audio.addListener(&floorManager); engine->audio.addListener(&floorManager);
engine.audio.addListener(&lightManager); engine->audio.addListener(&lightManager);
engine.audio.loadSong("NF-CHILL.WAV"); engine->audio.loadSong("NF-CHILL.WAV");
engine.audio.play(); engine->audio.setVolume(1);
texRepo = engine.renderer->getTextureRepository(); engine->audio.play();
texRepo = engine->renderer->getTextureRepository();
texRepo->addByMesh("warrior/", player.mesh); texRepo->addByMesh("warrior/", player.mesh);
texRepo->addByMesh("floor/", floorManager.floors[0].mesh); texRepo->addByMesh("floor/", floorManager.floors[0].mesh);
for (u32 i = 1; i < floorManager.floorAmount; i++) for (u32 i = 1; i < floorManager.floorAmount; i++)
@@ -43,13 +44,13 @@ void Floors::onInit()
void Floors::onUpdate() void Floors::onUpdate()
{ {
if (engine.pad.isCrossClicked) if (engine->pad.isCrossClicked)
printf("FPS:%f\n", engine.fps); printf("FPS:%f\n", engine->fps);
lightManager.update(); lightManager.update();
camera.update(engine.pad, player.mesh); camera.update(engine->pad, player.mesh);
floorManager.update(player); floorManager.update(player);
player.update(engine.pad, camera, floorManager); player.update(engine->pad, camera, floorManager);
engine.renderer->draw(player.mesh); engine->renderer->draw(player.mesh);
for (u8 i = 0; i < FLOORS_COUNT; i++) for (u8 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);
} }
+2 -2
View File
@@ -24,13 +24,13 @@ class Floors : public Game
{ {
public: public:
Floors(const Engine &t_engine); Floors(Engine *t_engine);
~Floors(); ~Floors();
void onInit(); void onInit();
void onUpdate(); void onUpdate();
Engine engine; Engine *engine;
private: private:
TextureRepository *texRepo; TextureRepository *texRepo;
+2 -2
View File
@@ -13,8 +13,8 @@
int main() int main()
{ {
Engine engine = Engine(); Engine engine = Engine();
Floors game = Floors(engine); Floors game = Floors(&engine);
game.engine.init(&game, 40000); game.engine->init(&game, 40000);
SleepThread(); SleepThread();
return 0; return 0;
} }
@@ -106,7 +106,6 @@ void FloorManager::update(Player &t_player)
/** Called by audio thread */ /** Called by audio thread */
void FloorManager::onAudioTick() void FloorManager::onAudioTick()
{ {
printf("Hit!\n");
if (audioTick++ > 16) if (audioTick++ > 16)
{ {
if (audioMode == 0) if (audioMode == 0)