Skip to content

Commit

Permalink
Finished all rendering aspects
Browse files Browse the repository at this point in the history
  • Loading branch information
Zang3th committed Jul 27, 2024
1 parent 1f8136a commit aa39dc8
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 62 deletions.
74 changes: 40 additions & 34 deletions Apps/Liquefied/LiquefiedApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,56 @@ namespace Liq

void LiquefiedApp::AddTurbine() const
{
//Init with object (pixel perfect) with a subsampled texture
_gridRenderer->AddTextureBufferSubsampled
(
"TurbineTexBuf",
_turbinePos,
_turbineSize
);

//Get vector with empty cells (corresponding pixel with alpha channel value == 0)
auto emptyCellVec = _gridRenderer->GetEmptyCells();

for(Engine::uint32 x = 0; x < _turbineSize; x++)
{
for(Engine::uint32 y = 0; y < _turbineSize; y++)
{
_fluidSimulator->AddBorderCell(_turbinePos.x + x, _turbinePos.y + y);
_gridRenderer->Set(_turbinePos.x + x, _turbinePos.y + y, _defaultColor);
Engine::GridPos gridPos = {_turbinePos.x + x, _turbinePos.y + y};

//If cell is not empty (aka not in the vector), add a border cell to the simulation
if(std::find(emptyCellVec.begin(), emptyCellVec.end(), gridPos) == emptyCellVec.end())
{
_fluidSimulator->AddBorderCell(_turbinePos.x + x, _turbinePos.y + y);
}
}
}
}

void LiquefiedApp::AddObstacle() const
{
//Init with object (pixel perfect) with a subsampled texture
_gridRenderer->AddTextureBufferSubsampled
(
"BoxTexBuf",
_obstaclePos,
_obstacleSize
);

//Get vector with empty cells (corresponding pixel with alpha channel value == 0)
auto emptyCellVec = _gridRenderer->GetEmptyCells();

for(Engine::uint32 x = 0; x < _obstacleSize; x++)
{
for(Engine::uint32 y = 0; y < _obstacleSize; y++)
{
_fluidSimulator->AddBorderCell(_obstaclePos.x + x, _obstaclePos.y + y);
_gridRenderer->Set(_obstaclePos.x + x, _obstaclePos.y + y, _defaultColor);
Engine::GridPos gridPos = {_obstaclePos.x + x, _obstaclePos.y + y};

//If cell is not empty (aka not in the vector), add a border cell to the simulation
if(std::find(emptyCellVec.begin(), emptyCellVec.end(), gridPos) == emptyCellVec.end())
{
_fluidSimulator->AddBorderCell(_obstaclePos.x + x, _obstaclePos.y + y);
}
}
}
}
Expand Down Expand Up @@ -110,22 +142,6 @@ namespace Liq
AddTurbine();
AddObstacle();

//Add sprites/textures to the config of the grid renderer
_gridRenderer->AddTextureBufferSubsampled
(
"TurbineTexBuf",
_turbinePos,
_turbineSize
);
_gridRenderer->AddTextureBufferSubsampled
(
"BoxTexBuf",
_obstaclePos,
_obstacleSize
);

_gridRenderer->SetConfigAsDefault();

return true;
}

Expand All @@ -144,19 +160,8 @@ namespace Liq
{
for(Engine::uint32 y = 0; y < Engine::LiquefiedParams::SIMULATION_HEIGHT; y++)
{
//Check for border cell (could be any type of object/obstacle)
if(_fluidSimulator->GetBorder(x, y) == 0.0f)
{
if(Engine::LiquefiedParams::renderObjects)
{
_gridRenderer->Reset(x, y);
continue;
}

//Overwrite objects/sprites/textures with default gray
color = {0.0f, 0.0f, 0.0f};
}
else
//Check for non border cell
if(_fluidSimulator->GetBorder(x, y) != 0.0f)
{
const float val = _fluidSimulator->GetDensity(x, y);

Expand All @@ -172,9 +177,10 @@ namespace Liq
{
color = Engine::Utility::GetColor_ParaView(val);
}

_gridRenderer->Set(x, y, color);
}

_gridRenderer->Set(x, y, color);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Apps/Liquefied/LiquefiedApp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Liq
Engine::Scope<Engine::Timer> _physicsTimer, _inputTimer;

const glm::vec3 _defaultColor = {0.5f, 0.5f, 0.5f};
const glm::uvec2 _turbinePos = {1, 35};
const glm::uvec2 _turbinePos = {0, 35};
const glm::uint32 _turbineSize = 32;
const glm::uvec2 _turbineOutlet =
{
Expand Down
5 changes: 0 additions & 5 deletions Apps/Liquefied/LiquefiedInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ namespace Liq
ImGui::NewLine();
ImGui::RadioButton("Forward Euler", (int*)&Engine::LiquefiedParams::integratorChoice, Engine::Integrator::ForwardEuler);
ImGui::RadioButton("Runge Kutta 2", (int*)&Engine::LiquefiedParams::integratorChoice, Engine::Integrator::RungeKutta2);
ImGui::RadioButton("Runge Kutta 3", (int*)&Engine::LiquefiedParams::integratorChoice, Engine::Integrator::RungeKutta3);
ImGui::EndTabItem();
}

Expand All @@ -62,10 +61,6 @@ namespace Liq
ImGui::RadioButton("Greyscale", (int*)&Engine::LiquefiedParams::visualizationChoice, Engine::Visualization::Greyscale);
ImGui::RadioButton("BlackBody", (int*)&Engine::LiquefiedParams::visualizationChoice, Engine::Visualization::BlackBody);
ImGui::RadioButton("ParaView", (int*)&Engine::LiquefiedParams::visualizationChoice, Engine::Visualization::ParaView);
ImGui::NewLine();
ImGui::Separator();
ImGui::NewLine();
ImGui::Checkbox("Render objects", &Engine::LiquefiedParams::renderObjects);
ImGui::EndTabItem();
}

Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
cmake_minimum_required(VERSION 3.20)

set(SALINITYGL_ENGINE_VERSION "0.2.0")
set(SALINITYGL_ENGINE_VERSION_SUFFIX "alpha")
set(SALINITYGL_ENGINE_VERSION_SUFFIX "release")
add_compile_definitions(SALINITYGL_ENGINE_VERSION="${SALINITYGL_ENGINE_VERSION}")
add_compile_definitions(SALINITYGL_ENGINE_VERSION_SUFFIX="${SALINITYGL_ENGINE_VERSION_SUFFIX}")
set(CMAKE_CXX_STANDARD 17)
Expand Down
16 changes: 16 additions & 0 deletions Engine/Core/Types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,21 @@ namespace Engine
unsigned char r;
unsigned char g;
unsigned char b;
unsigned char a;
};

struct GridPos
{
uint32 x;
uint32 y;

GridPos(uint32 xpos, uint32 ypos)
: x(xpos), y(ypos)
{}

inline bool operator==(const GridPos& comp) const
{
return (x == comp.x) && (y == comp.y);
}
};
}
21 changes: 9 additions & 12 deletions Engine/Rendering/Renderer/GridRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ namespace Engine
for(uint32 i = 0; i < _quadAmountTotal; i++)
{
_colorStorage.emplace_back(0.0f);
_backupStorage.emplace_back(0.0f);
}

//Create and bind vao
Expand Down Expand Up @@ -62,16 +61,10 @@ namespace Engine
Logger::Info("Created", "Renderer", __func__);

_colorStorage.reserve(_quadAmountTotal);
_backupStorage.reserve(_quadAmountTotal);

InitGpuStorage();
}

void GridRenderer::SetConfigAsDefault()
{
_backupStorage = _colorStorage;
}

void GridRenderer::SetDefaultColor(const glm::vec3& color)
{
_defaultColor = color;
Expand Down Expand Up @@ -140,11 +133,6 @@ namespace Engine
}
}

void GridRenderer::Reset(const uint32 x, const uint32 y)
{
_colorStorage.at(x * _gridHeight + y) = _backupStorage.at(x * _gridHeight + y);
}

void GridRenderer::AddTextureBufferSubsampled(const std::string& texBuffer, const glm::uvec2& pos, const uint32 size)
{
auto* textureBuffer = ResourceManager::GetTextureBuffer(texBuffer);
Expand Down Expand Up @@ -181,6 +169,10 @@ namespace Engine
{
Set(gridPos.x, gridPos.y, Utility::TransformVec3uTo3f(subsampledColor));
}
else
{
_emptyCells.emplace_back(gridPos.x, gridPos.y);
}

gridPos.y++;
}
Expand All @@ -189,4 +181,9 @@ namespace Engine
gridPos.y = pos.y; //Reset y-position
}
}

std::vector<GridPos> GridRenderer::GetEmptyCells() const
{
return _emptyCells;
}
}
9 changes: 6 additions & 3 deletions Engine/Rendering/Renderer/GridRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "GlobalParams.hpp"
#include "ResourceManager.hpp"

#include <vector>

namespace Engine
{
class GridRenderer final : public Renderer
Expand All @@ -21,20 +23,21 @@ namespace Engine
Scope<VertexBuffer> _vboVert, _vboColor;
glm::mat4 _orthoProj, _model;

std::vector<glm::vec3> _colorStorage, _backupStorage;
std::vector<glm::vec3> _colorStorage;
std::vector<GridPos> _emptyCells;

void InitGpuStorage();

public:
GridRenderer(uint32 width, uint32 height, uint32 quadSize, const std::string& shader);

void SetConfigAsDefault();
void SetDefaultColor(const glm::vec3& color);
void Flush(Renderer* renderer) override;
void UpdateGpuStorage() const;
void Set(uint32 x, uint32 y, const glm::vec3& color);
void SetArea(const glm::uvec2& pos, uint32 size, const glm::vec3& color);
void Reset(uint32 x, uint32 y);
void AddTextureBufferSubsampled(const std::string& texBuffer, const glm::uvec2& pos, uint32 size);

[[nodiscard]] std::vector<GridPos> GetEmptyCells() const;
};
} // namespace Engine
23 changes: 19 additions & 4 deletions Engine/Rendering/Resources/TextureBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ namespace Engine
colorOut->r = *(_pxBuffer + (y * _width * _channels) + (x * _channels) + 0);
colorOut->g = *(_pxBuffer + (y * _width * _channels) + (x * _channels) + 1);
colorOut->b = *(_pxBuffer + (y * _width * _channels) + (x * _channels) + 2);
colorOut->a = *(_pxBuffer + (y * _width * _channels) + (x * _channels) + 3);

return true;
}

Expand All @@ -140,6 +142,8 @@ namespace Engine
*(_pxBuffer + (y * _width * _channels) + (x * _channels) + 0) = colorIn.r;
*(_pxBuffer + (y * _width * _channels) + (x * _channels) + 1) = colorIn.g;
*(_pxBuffer + (y * _width * _channels) + (x * _channels) + 2) = colorIn.b;
*(_pxBuffer + (y * _width * _channels) + (x * _channels) + 3) = colorIn.a;

return true;
}

Expand All @@ -154,7 +158,8 @@ namespace Engine
const PxColor color = {
*(_backupBuffer + (y * _width * _channels) + (x * _channels) + 0),
*(_backupBuffer + (y * _width * _channels) + (x * _channels) + 1),
*(_backupBuffer + (y * _width * _channels) + (x * _channels) + 2)
*(_backupBuffer + (y * _width * _channels) + (x * _channels) + 2),
*(_backupBuffer + (y * _width * _channels) + (x * _channels) + 3),
};

if(SetPxColor(x, y, color))
Expand All @@ -174,7 +179,16 @@ namespace Engine
{
bool success = true;
uint32 pxAmount = sampleAmount * sampleAmount;
PxColor pxColor= {0, 0, 0};
PxColor pxColor= {0, 0, 0, 0};

//Check for empty pixel
if(GetPxColor(xpos, ypos, &pxColor))
{
if(pxColor.a == 0)
{
return false;
}
}

for(uint32 x = 0; x < sampleAmount; x++)
{
Expand Down Expand Up @@ -206,14 +220,15 @@ namespace Engine
{
for(uint32 y = 0; y < _height; y++)
{
PxColor color = {0, 0, 0};
PxColor color = {0, 0, 0, 0};
if(GetPxColor(x, y, &color))
{
Logger::Print("Color (" + std::to_string(x) + ", "
+ std::to_string(y) + ") : "
+ std::to_string(color.r) + ", "
+ std::to_string(color.g) + ", "
+ std::to_string(color.b));
+ std::to_string(color.b) + ", "
+ std::to_string(color.a));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ Nature scene with water rendering, normal mapped objects, and a particle system

2D Eulerian Fluid Simulation on the CPU.

![Liquefied_color1](Res/Screenshots/Liquefied/Screenshot_Liq_007-1.png)
![Liquefied_color2](Res/Screenshots/Liquefied/Screenshot_Liq_007-2.png)
![Liquefied_color1](Res/Screenshots/Liquefied/Screenshot_Liq_008-1.png)
![Liquefied_color2](Res/Screenshots/Liquefied/Screenshot_Liq_008-2.png)

## Building and compiling

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit aa39dc8

Please sign in to comment.