Skip to content

Commit

Permalink
Some quality of life changes and increased control over the simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
Zang3th committed Apr 29, 2024
1 parent 97f942c commit c812c02
Show file tree
Hide file tree
Showing 22 changed files with 187 additions and 61 deletions.
10 changes: 6 additions & 4 deletions Apps/CellSim/CellSimApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ namespace CS
//Create UI
_interface = Engine::MakeScope<CellSimInterface>();

//Create timer
_physicsTimer = Engine::MakeScope<Engine::Timer>(10); //10ms

return EXIT_SUCCESS;
}

Expand Down Expand Up @@ -145,7 +148,7 @@ namespace CS
Engine::Window::PollEvents();
Engine::Window::ProcessEvents();
Engine::CameraController3D::ProcessInput();
_timeElapsed += Engine::Window::GetDeltaTime();
_physicsTimer->Update(Engine::Window::GetDeltaTime_msec());

if(Engine::UIParams::resetCamera)
{
Expand Down Expand Up @@ -212,10 +215,9 @@ namespace CS
if(_calcPhysicsThisTurn)
{
//Check if 10ms have elapsed
if(_timeElapsed >= 0.01)
if(_physicsTimer->CheckElapsedAndReset())
{
//Reset time and increase tick counter
_timeElapsed = 0;
//Increase tick counter
_tickCounter++;

//Do physics
Expand Down
5 changes: 4 additions & 1 deletion Apps/CellSim/CellSimApp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace CS
class CellSimApp final : public Engine::App
{
private:
double _timeElapsed = 0.0;
Engine::Scope<Engine::Timer> _physicsTimer;
Engine::uint8 _tickCounter = 0;
bool _calcPhysicsThisTurn = true;
Engine::SceneRenderer* _sceneRenderer = nullptr;
Expand All @@ -26,5 +26,8 @@ namespace CS
CellSimApp();
~CellSimApp() override;
void Update() override;

//Not needed in this app because we only use a general 3D camera controller to handle input.
void ProcessInput() override {}
};
}
4 changes: 2 additions & 2 deletions Apps/CellSim/CellSimInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ namespace CS
// --- Stats
{
// --- Application stats
ImGui::Text("Application average %.2f ms/frame (%.1f FPS)", Engine::Window::GetDeltaTime() * 1000.0f, Engine::Window::GetFps());
ImGui::PlotVar("", (float)Engine::Window::GetDeltaTime() * 1000.0f, 0.0f, 30.0f);
ImGui::Text("Application average %.2f ms/frame (%.1f FPS)", Engine::Window::GetDeltaTime_msec(), Engine::Window::GetFps());
ImGui::PlotVar("", (float)Engine::Window::GetDeltaTime_msec() * 1000.0f, 0.0f, 30.0f);

// --- Render stats
ImGui::Separator();
Expand Down
3 changes: 3 additions & 0 deletions Apps/GreenWorld/GreenWorldApp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@ namespace GW
GreenWorldApp();
~GreenWorldApp() override;
void Update() override;

//Not needed in this app because we only use a general 3D camera controller to handle input.
void ProcessInput() override {}
};
}
4 changes: 2 additions & 2 deletions Apps/GreenWorld/GreenWorldInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ namespace GW
if(ImGui::Begin("Sidebar", nullptr, _windowFlags))
{
// --- Application stats
ImGui::Text("Application average %.2f ms/frame (%.1f FPS)", Engine::Window::GetDeltaTime() * 1000.0f, Engine::Window::GetFps());
ImGui::PlotVar("", (float)Engine::Window::GetDeltaTime() * 1000.0f, 0.0f, 30.0f);
ImGui::Text("Application average %.2f ms/frame (%.1f FPS)", Engine::Window::GetDeltaTime_msec(), Engine::Window::GetFps());
ImGui::PlotVar("", (float)Engine::Window::GetDeltaTime_msec(), 0.0f, 30.0f);

// --- Render stats
ImGui::NewLine();
Expand Down
63 changes: 52 additions & 11 deletions Apps/Liquefied/LiquefiedApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ namespace Liq
void LiquefiedApp::AddBorderCells() const
{
Engine::StaggeredGrid* grid = _fluidSimulator->GetGrid();
const float cellValue = 0.0f;

for(Engine::uint32 x = 0; x < grid->width; x++)
{
Expand All @@ -22,7 +21,7 @@ namespace Liq
if((x == 0) || (y == 0) || (x == grid->width-1) || (y == grid->height-1))
{
//Add solid cell to simulation grid
grid->s_At(x, y) = cellValue;
grid->s_At(x, y) = 0.0f;

//Add cell to renderer
_gridRenderer->Set(x, y, glm::vec3(1.0f, 1.0f, 1.0f));
Expand Down Expand Up @@ -59,13 +58,24 @@ namespace Liq
//Create fluid simulator
_fluidSimulator = Engine::MakeScope<Engine::FluidSimulator>();

//Create timer
_physicsTimer = Engine::MakeScope<Engine::Timer>(4); //4ms
_inputTimer = Engine::MakeScope<Engine::Timer>(100); //100ms

//Add border cells to simulation and renderer
AddBorderCells();
_gridRenderer->SetConfigAsDefault();

return EXIT_SUCCESS;
}

void LiquefiedApp::UpdateTimer() const
{
const double dt_msec = Engine::Window::GetDeltaTime_msec();
_physicsTimer->Update(dt_msec);
_inputTimer->Update(dt_msec);
}

void LiquefiedApp::VisualizeSmoke() const
{
Engine::StaggeredGrid* grid = _fluidSimulator->GetGrid();
Expand Down Expand Up @@ -115,7 +125,13 @@ namespace Liq

Engine::Window::PollEvents();
Engine::Window::ProcessEvents();
_timeElapsed += Engine::Window::GetDeltaTime();
UpdateTimer();

//Handle input only if timer elapsed
if(_inputTimer->CheckElapsedAndReset())
{
ProcessInput();
}
}

{
Expand All @@ -129,17 +145,24 @@ namespace Liq
{
Engine::PROFILE_SCOPE("Simulate liquid");

//Check if 4ms have elapsed
if(_timeElapsed >= 0.004)
if(Engine::LiquiefiedParams::resetSimulation)
{
//Add a horizontal turbine (initial velocity)
_fluidSimulator->AddHorizonalTurbine(1, 50, 50.0f);
_fluidSimulator->Reset();
AddBorderCells();
Engine::LiquiefiedParams::resetSimulation = false;
}

//Reset time
_timeElapsed = 0;
if(!Engine::LiquiefiedParams::pauseSimulation)
{
//Check if timer elapsed
if(_physicsTimer->CheckElapsedAndReset())
{
//Add a horizontal turbine (initial velocity)
_fluidSimulator->AddHorizonalTurbine(1, 50, 50.0f);

//Run simulation timestep and visualize result
_fluidSimulator->TimeStep();
//Run simulation timestep and visualize result
_fluidSimulator->TimeStep();
}
}
}

Expand Down Expand Up @@ -172,4 +195,22 @@ namespace Liq
Engine::Window::SwapBuffers();
}
}

void LiquefiedApp::ProcessInput()
{
if(glfwGetKey(Engine::Window::GetWindow(), GLFW_KEY_W) == GLFW_PRESS)
Engine::UIParams::wireframeRendering = !Engine::UIParams::wireframeRendering;

else if(glfwGetKey(Engine::Window::GetWindow(), GLFW_KEY_SPACE) == GLFW_PRESS)
Engine::LiquiefiedParams::pauseSimulation = !Engine::LiquiefiedParams::pauseSimulation;

else if(glfwGetKey(Engine::Window::GetWindow(), GLFW_KEY_S) == GLFW_PRESS)
Engine::LiquiefiedParams::visualizeSmoke = !Engine::LiquiefiedParams::visualizeSmoke;

else if(glfwGetKey(Engine::Window::GetWindow(), GLFW_KEY_C) == GLFW_PRESS)
Engine::LiquiefiedParams::scientificColorScheme = !Engine::LiquiefiedParams::scientificColorScheme;

else if(glfwGetKey(Engine::Window::GetWindow(), GLFW_KEY_R) == GLFW_PRESS)
Engine::LiquiefiedParams::resetSimulation = true;
}
}
10 changes: 6 additions & 4 deletions Apps/Liquefied/LiquefiedApp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@ namespace Liq
class LiquefiedApp final : public Engine::App
{
private:
double _timeElapsed = 0.0;
Engine::GridRenderer* _gridRenderer = nullptr;
Engine::GridRenderer* _gridRenderer = nullptr;
Engine::Scope<LiquefiedInterface> _interface;
Engine::Scope<Engine::FluidSimulator> _fluidSimulator;
Engine::Scope<Engine::Timer> _physicsTimer, _inputTimer;

void LoadResources() override;
void AddBorderCells() const;
Engine::uint32 InitModules() override;
void UpdateTimer() const;
void VisualizeSmoke() const;

public:
LiquefiedApp();
~LiquefiedApp() override;
void Update() override;
~LiquefiedApp() override;
void Update() override;
void ProcessInput() override;
};
}
37 changes: 26 additions & 11 deletions Apps/Liquefied/LiquefiedInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,24 @@ namespace Liq
if(ImGui::Begin("Sidebar", nullptr, _windowFlags))
{
// --- Application stats
ImGui::Text("Application average %.2f ms/frame (%.1f FPS)", Engine::Window::GetDeltaTime() * 1000.0f, Engine::Window::GetFps());
ImGui::PlotVar("", (float)Engine::Window::GetDeltaTime() * 1000.0f, 0.0f, 30.0f);
ImGui::Text("Application average %.2f ms/frame (%.1f FPS)", Engine::Window::GetDeltaTime_msec(), Engine::Window::GetFps());
ImGui::PlotVar("", (float)Engine::Window::GetDeltaTime_msec(), 0.0f, 30.0f);

// --- Render stats
ImGui::NewLine();
ImGui::Separator();
ImGui::Text("Draw calls: %d", Engine::RenderStatistics::drawCalls);
ImGui::Text("Drawn vertices: %d", Engine::RenderStatistics::drawnVertices);
ImGui::Separator();

// --- Profiling/Timing-Results
ImGui::NewLine();
ImGui::Separator();
for(auto const& entry : Engine::Profiler::_results)
{
ImGui::Text("%.3fms - %s", entry.second, entry.first);
}
ImGui::Separator();
}
ImGui::End();
}
Expand All @@ -39,15 +43,26 @@ namespace Liq

if(ImGui::Begin("Bufferbar", nullptr, _windowFlags))
{
ImGui::SetCursorPosX(25.0f);
ImGui::SetCursorPosY(25.0f);
ImGui::Checkbox("Wireframe Rendering", &Engine::UIParams::wireframeRendering);
ImGui::SetCursorPosY(25.0f);
ImGui::SetCursorPosX(250.0f);
ImGui::Checkbox("Visualize Smoke", &Engine::LiquiefiedParams::visualizeSmoke);
ImGui::SetCursorPosY(25.0f);
ImGui::SetCursorPosX(440.0f);
ImGui::Checkbox("Scientific Colors", &Engine::LiquiefiedParams::scientificColorScheme);
ImGui::SetCursorPosX(10.0f);
ImGui::SetCursorPosY(10.0f);
ImGui::Checkbox("Wireframe Rendering (W) |", &Engine::UIParams::wireframeRendering);
ImGui::SetCursorPosY(45.0f);
ImGui::SetCursorPosX(10.0f);
ImGui::Checkbox("Pause Simulation (SPACE) |", &Engine::LiquiefiedParams::pauseSimulation);

ImGui::SetCursorPosY(10.0f);
ImGui::SetCursorPosX(275.0f);
ImGui::Checkbox("Visualize Smoke (S) |", &Engine::LiquiefiedParams::visualizeSmoke);
ImGui::SetCursorPosY(45.0f);
ImGui::SetCursorPosX(275.0f);
ImGui::Checkbox("Scientific Colors (C) |", &Engine::LiquiefiedParams::scientificColorScheme);

ImGui::SetCursorPosY(10.0f);
ImGui::SetCursorPosX(515.0f);
if(ImGui::Button("Reset Simulation (R)"))
{
Engine::LiquiefiedParams::resetSimulation = true;
}
}
ImGui::End();
}
Expand Down
5 changes: 3 additions & 2 deletions Engine/Application/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ namespace Engine
virtual uint32 InitModules() = 0;

public:
virtual ~App() = 0;
virtual void Update() = 0;
virtual ~App() = 0;
virtual void Update() = 0;
virtual void ProcessInput() = 0;

[[nodiscard]] bool GetInitSuccess() const;
[[nodiscard]] bool IsRunning() const;
Expand Down
14 changes: 8 additions & 6 deletions Engine/Application/GlobalParams.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,14 @@ namespace Engine

struct LiquiefiedParams
{
inline static constexpr uint32 SIMULATION_WIDTH = 150;
inline static constexpr uint32 SIMULATION_HEIGHT = 100;
inline static constexpr uint32 LIQUID_NUM_CELLS = SIMULATION_WIDTH * SIMULATION_HEIGHT;
inline static constexpr uint32 GAUSS_SEIDEL_ITERATIONS = 20;
inline static constexpr uint32 SIMULATION_WIDTH = 150;
inline static constexpr uint32 SIMULATION_HEIGHT = 100;
inline static constexpr uint32 LIQUID_NUM_CELLS = SIMULATION_WIDTH * SIMULATION_HEIGHT;
inline static constexpr uint32 GAUSS_SEIDEL_ITERATIONS = 20;
inline static constexpr float GAUSS_SEIDEL_OVERRELAXATION = 1.9f;
inline static bool scientificColorScheme = false;
inline static bool visualizeSmoke = false;
inline static bool visualizeSmoke = true;
inline static bool scientificColorScheme = false;
inline static bool pauseSimulation = true;
inline static bool resetSimulation = false;
};
}
28 changes: 28 additions & 0 deletions Engine/Core/Timer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "Timer.hpp"

namespace Engine
{
// ----- Public -----

Timer::Timer(double time_msec)
: _start(time_msec), _current(time_msec)
{

}

void Timer::Update(const double dt_msec)
{
_current -= dt_msec;
}

bool Timer::CheckElapsedAndReset()
{
if(_current <= 0.0f)
{
_current = _start;
return true;
}

return false;
}
}
16 changes: 16 additions & 0 deletions Engine/Core/Timer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

namespace Engine
{
class Timer
{
private:
double _start, _current;

public:
explicit Timer(double time_msec);
~Timer() = default;
void Update(double dt_msec);
[[nodiscard]] bool CheckElapsedAndReset();
};
}
Loading

0 comments on commit c812c02

Please sign in to comment.