Skip to content

Commit

Permalink
Work on the grid renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
Zang3th committed Jul 22, 2024
1 parent 2aa06c8 commit 1f8136a
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 84 deletions.
74 changes: 31 additions & 43 deletions Apps/Liquefied/LiquefiedApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ namespace Liq
Engine::ResourceManager::LoadShader("SpriteShader", "../Res/Shader/GreenWorld/Sprite_VS.glsl", "../Res/Shader/GreenWorld/Sprite_FS.glsl");

//Textures
// Engine::ResourceManager::LoadTextureBuffer("TurbineTexBuf", "../Res/Assets/Textures/Liquefied/Turbine_512.png");
// Engine::ResourceManager::LoadTextureBuffer("ObstacleTexBuf", "../Res/Assets/Textures/Liquefied/Box_512.png");
Engine::ResourceManager::LoadTextureBuffer("TestTexBuf", "../Res/Assets/Textures/Liquefied/Test_1C_4Px.png");
Engine::ResourceManager::LoadTextureBuffer("TurbineTexBuf", "../Res/Assets/Textures/Liquefied/Turbine_512.png");
Engine::ResourceManager::LoadTextureBuffer("BoxTexBuf", "../Res/Assets/Textures/Liquefied/Box_512.png");
}

void LiquefiedApp::AddBorderCells() const
Expand All @@ -30,29 +29,32 @@ namespace Liq
{
//Add solid cell to simulation grid
_fluidSimulator->AddBorderCell(x, y);
_gridRenderer->Set(x, y, _defaultColor);
}
}
}
}

void LiquefiedApp::AddTurbine() const
{
for(Engine::uint32 x = 0; x < turbineSize; x++)
for(Engine::uint32 x = 0; x < _turbineSize; x++)
{
for(Engine::uint32 y = 0; y < turbineSize; y++)
for(Engine::uint32 y = 0; y < _turbineSize; y++)
{
_fluidSimulator->AddBorderCell(turbinePos.x + x, turbinePos.y + y);
_fluidSimulator->AddBorderCell(_turbinePos.x + x, _turbinePos.y + y);
_gridRenderer->Set(_turbinePos.x + x, _turbinePos.y + y, _defaultColor);
}
}
}

void LiquefiedApp::AddObstacles() const
void LiquefiedApp::AddObstacle() const
{
for(Engine::uint32 x = 0; x < obstacleSize; x++)
for(Engine::uint32 x = 0; x < _obstacleSize; x++)
{
for(Engine::uint32 y = 0; y < obstacleSize; y++)
for(Engine::uint32 y = 0; y < _obstacleSize; y++)
{
_fluidSimulator->AddBorderCell(obstaclePos.x + x, obstaclePos.y + y);
_fluidSimulator->AddBorderCell(_obstaclePos.x + x, _obstaclePos.y + y);
_gridRenderer->Set(_obstaclePos.x + x, _obstaclePos.y + y, _defaultColor);
}
}
}
Expand All @@ -61,8 +63,8 @@ namespace Liq
{
_fluidSimulator->AddHorizonalTurbine
(
turbineOutlet.x,
turbineOutlet.y,
_turbineOutlet.x,
_turbineOutlet.y,
(float)Engine::LiquefiedParams::turbinePower,
dt
);
Expand All @@ -89,6 +91,7 @@ namespace Liq
10,
"GridShader"
);
_gridRenderer->SetDefaultColor(_defaultColor);
Engine::RenderManager::Submit(_gridRenderer);

//Create UI
Expand All @@ -105,39 +108,24 @@ namespace Liq
//Add border cells, turbine and physical obstacles to the simulation
AddBorderCells();
AddTurbine();
AddObstacles();
AddObstacle();

_spriteRenderer = new Engine::SpriteRenderer();
Engine::RenderManager::Submit(_spriteRenderer);

auto* tex = Engine::ResourceManager::CreateGLTextureFromBuffer("TestTexture", Engine::ResourceManager::GetTextureBuffer("TestTexBuf"));
tex->Bind();
tex->AddMinFilterNearest();
tex->AddMaxFilterNearest();
tex->Unbind();

_spriteRenderer->AddSprite
//Add sprites/textures to the config of the grid renderer
_gridRenderer->AddTextureBufferSubsampled
(
glm::vec2(64.0f, 64.0f),
glm::vec2(10.0f, 700.0f),
Engine::ResourceManager::GetGLTexture("TestTexture"),
Engine::ResourceManager::GetShader("SpriteShader")
"TurbineTexBuf",
_turbinePos,
_turbineSize
);

//Add sprites/textures to the config of the renderer
// _gridRenderer->AddTextureSubsampled
// (
// "TurbineTexture",
// turbinePos,
// turbineSize
// );
_gridRenderer->AddTextureBufferSubsampled
(
"TestTexBuf",
obstaclePos,
obstacleSize
"BoxTexBuf",
_obstaclePos,
_obstacleSize
);

_gridRenderer->SetConfigAsDefault();

return true;
}

Expand All @@ -161,11 +149,12 @@ namespace Liq
{
if(Engine::LiquefiedParams::renderObjects)
{
_gridRenderer->Reset(x, y);
continue;
}

//Else overwrite objects/sprites/textures with default gray
// color = {0.5f, 0.5f, 0.5f};
//Overwrite objects/sprites/textures with default gray
color = {0.0f, 0.0f, 0.0f};
}
else
{
Expand Down Expand Up @@ -231,7 +220,7 @@ namespace Liq
_fluidSimulator->Reset();
AddBorderCells();
AddTurbine();
AddObstacles();
AddObstacle();
Engine::LiquefiedParams::resetSimulation = false;
}

Expand All @@ -250,10 +239,9 @@ namespace Liq
{
Engine::PROFILE_SCOPE("Visualize grid");

// RenderSmoke();
RenderSmoke();
_gridRenderer->UpdateGpuStorage();
_gridRenderer->Flush(nullptr);
_spriteRenderer->Flush(nullptr);
}

{
Expand Down
20 changes: 11 additions & 9 deletions Apps/Liquefied/LiquefiedApp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,26 @@ namespace Liq
{
private:
Engine::GridRenderer* _gridRenderer = nullptr;
Engine::SpriteRenderer* _spriteRenderer = nullptr;

Engine::Scope<LiquefiedInterface> _interface;
Engine::Scope<Engine::FluidSimulator> _fluidSimulator;
Engine::Scope<Engine::Timer> _physicsTimer, _inputTimer;
const glm::uvec2 turbinePos = {1, 50};
const glm::uint32 turbineSize = 8;
const glm::uvec2 turbineOutlet =

const glm::vec3 _defaultColor = {0.5f, 0.5f, 0.5f};
const glm::uvec2 _turbinePos = {1, 35};
const glm::uint32 _turbineSize = 32;
const glm::uvec2 _turbineOutlet =
{
turbinePos.x + turbineSize,
turbinePos.y + turbineSize / 2
_turbinePos.x + _turbineSize,
_turbinePos.y + _turbineSize / 2
};
const glm::uvec2 obstaclePos = {50, 50};
const glm::uint32 obstacleSize = 4;
const glm::uvec2 _obstaclePos = {70, 45};
const glm::uint32 _obstacleSize = 16;

void LoadResources() override;
void AddBorderCells() const;
void AddTurbine() const;
void AddObstacles() const;
void AddObstacle() const;
void TurbinePushVelocity(float dt) const;
void UpdateTimer() const;
void RenderSmoke() const;
Expand Down
45 changes: 33 additions & 12 deletions Engine/Rendering/Renderer/GridRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ namespace Engine
1.0f, 0.0f
};

//Init color storage
//Init storage
for(uint32 i = 0; i < _quadAmountTotal; i++)
{
_colorStorage.emplace_back(_defaultColor);
_colorStorage.emplace_back(0.0f);
_backupStorage.emplace_back(0.0f);
}

//Create and bind vao
Expand Down Expand Up @@ -54,15 +55,28 @@ namespace Engine
const std::string& shader
)
: _gridWidth(width), _gridHeight(height), _quadSize(quadSize), _quadAmountTotal(_gridWidth * _gridHeight),
_defaultColor(glm::vec3(0.1f, 0.1f, 0.1f)), _shader(ResourceManager::GetShader(shader)),
_shader(ResourceManager::GetShader(shader)), _defaultColor(0.0f),
_orthoProj(glm::ortho(0.0f, (float)WindowParams::WIDTH, 0.0f, (float)WindowParams::HEIGHT, -1.0f, 1.0f)),
_model(glm::scale(glm::mat4(1.0f), glm::vec3(glm::vec2((float)_quadSize), 0.0f)))
{
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;
}

void GridRenderer::Flush(Renderer* renderer)
{
//Check for Wireframe-Mode
Expand All @@ -84,7 +98,6 @@ namespace Engine
_vboColor->Bind();

//Set uniforms
_shader->SetUniformVec3f("color", _defaultColor);
_shader->SetUniformMat4f("projection", _orthoProj);
_shader->SetUniformMat4f("model", _model);

Expand Down Expand Up @@ -116,9 +129,20 @@ namespace Engine
_colorStorage.at(x * _gridHeight + y) = glm::vec3(color);
}

void GridRenderer::SetArea(const glm::uvec2& pos, uint32 size, const glm::vec3& color)
{
for(uint32 x = pos.x; x < pos.x + size; x++)
{
for(uint32 y = pos.y; y < pos.y + size; y++)
{
Set(x, y, color);
}
}
}

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

void GridRenderer::AddTextureBufferSubsampled(const std::string& texBuffer, const glm::uvec2& pos, const uint32 size)
Expand All @@ -133,6 +157,9 @@ namespace Engine
return;
}

//Set texture area to default color
SetArea(pos, size, _defaultColor);

//sampleAmount stands for the amount of pixels that needs to be sampled in one direction.
//F.E. the original image is 512x512 and we want to reduce it to 8x8.
//That results in 64x64 pixels that will get sampled for 1 pixel in the new image.
Expand All @@ -152,13 +179,7 @@ namespace Engine

if(success)
{
glm::vec3 color = Utility::TransformVec3uTo3f(subsampledColor);
Logger::Print("Color (" + std::to_string(gridPos.x) + ", "
+ std::to_string(gridPos.y) + ") : "
+ std::to_string(color.x) + ", "
+ std::to_string(color.y) + ", "
+ std::to_string(color.z));
Set(gridPos.x, gridPos.y, color);
Set(gridPos.x, gridPos.y, Utility::TransformVec3uTo3f(subsampledColor));
}

gridPos.y++;
Expand Down
7 changes: 5 additions & 2 deletions Engine/Rendering/Renderer/GridRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,26 @@ namespace Engine
{
private:
uint32 _gridWidth, _gridHeight, _quadSize, _quadAmountTotal;
glm::vec3 _defaultColor;
Shader* _shader;
glm::vec3 _defaultColor;

Scope<VertexArray> _vao;
Scope<VertexBuffer> _vboVert, _vboColor;
glm::mat4 _orthoProj, _model;

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

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);
};
Expand Down
5 changes: 5 additions & 0 deletions Engine/Rendering/Renderer/RenderManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ namespace Engine
{
_rendererStorage.clear();
_rendererStorage.reserve(10);

//OpenGL-Rendersettings
GLRenderSettings::EnableBlending();
GLRenderSettings::SetBlendFunc(GL_ONE_MINUS_SRC_ALPHA);

_initialized = true;
}

Expand Down
9 changes: 0 additions & 9 deletions Engine/Rendering/Resources/TextureBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,6 @@ namespace Engine
uint32 pxAmount = sampleAmount * sampleAmount;
PxColor pxColor= {0, 0, 0};

// For debug purposes:
if(GetPxColor(xpos, ypos, &pxColor))
{
colorOut->x = pxColor.r;
colorOut->y = pxColor.g;
colorOut->z = pxColor.b;
return success;
}

for(uint32 x = 0; x < sampleAmount; x++)
{
for(uint32 y = 0; y < sampleAmount; y++)
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ The engine and the demo applications are compiled separately. The engine is comp

## Releases

| **Version** | **Date** | **Commit [Count / ID]** | **Lines of code [Apps / Engine]** | **Notes** |
|--------------------------------------------------------------------|------------|-------------------------|-----------------------------------|-----------------------------------------|
| [0.1.1](https://github.com/Zang3th/GameEngine/releases/tag/v0.1.1) | 14.01.2023 | [255 / d425a33] | [480 / 4250] | Completion of the *GreenWorld* demo app |
| [0.1.0](https://github.com/Zang3th/GameEngine/releases/tag/v0.1.0) | 11.06.2022 | [229 / 218a55e] | [575 / 4110] | First official release |
| **Version** | **Date** | **Commit** <br> [Count / ID] | **Lines of code** <br> [Apps / Engine] | **Notes** |
|:------------------------------------------------------------------:|:----------:|:----------------------------:|:--------------------------------------:|:---------------------------------------:|
| [0.1.1](https://github.com/Zang3th/GameEngine/releases/tag/v0.1.1) | 14.01.2023 | [255 / d425a33] | [480 / 4250] | Completion of the *GreenWorld* demo app |
| [0.1.0](https://github.com/Zang3th/GameEngine/releases/tag/v0.1.0) | 11.06.2022 | [229 / 218a55e] | [575 / 4110] | First official release |

- 07/2021 - 09/2021: Rewrite of the engine core
- 01/2020 - 09/2020: Some very early projects (still under Windows)
Expand All @@ -157,7 +157,7 @@ I always work *on and off* on this project, but I try to make more regular relea
**Thanks to all the creators and contributors of these projects!**

| **Library** | **Version** | **Commit** | **Updated in Engine** | **Functionality** |
|--------------------------------------------------------------------- |-------------|------------|-----------------------|-----------------------------|
|:--------------------------------------------------------------------:|:-----------:|:----------:|:---------------------:|:---------------------------:|
| [GLFW](https://github.com/glfw/glfw) | 3.4.0 | 7b6aead | 13.04.2024 | Window and input management |
| [glad](https://github.com/Dav1dde/glad) | 2.0.6 | 658f48e | 13.04.2024 | OpenGL function loading |
| [imgui](https://github.com/ocornut/imgui) | 1.90.4 | 277ae93 | 13.04.2024 | GUI |
Expand Down
Binary file added Res/Assets/Textures/Liquefied/Test_2C_8Px.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions Res/Shader/GreenWorld/Sprite_FS.glsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#version 450 core

in vec2 texCoords;

out vec4 fragColor;
Expand All @@ -10,4 +10,4 @@ uniform vec3 color;
void main()
{
fragColor = vec4(color, 1.0f) * texture(image, texCoords);
}
}
Loading

0 comments on commit 1f8136a

Please sign in to comment.