From 1f8136ad7d772ad6e48e3850d9ea91e03a75c3a6 Mon Sep 17 00:00:00 2001 From: Zang3th Date: Mon, 22 Jul 2024 15:49:47 +0200 Subject: [PATCH] Work on the grid renderer --- Apps/Liquefied/LiquefiedApp.cpp | 74 ++++++++---------- Apps/Liquefied/LiquefiedApp.hpp | 20 ++--- Engine/Rendering/Renderer/GridRenderer.cpp | 45 ++++++++--- Engine/Rendering/Renderer/GridRenderer.hpp | 7 +- Engine/Rendering/Renderer/RenderManager.cpp | 5 ++ Engine/Rendering/Resources/TextureBuffer.cpp | 9 --- README.md | 10 +-- Res/Assets/Textures/Liquefied/Test_2C_8Px.png | Bin 0 -> 4250 bytes Res/Shader/GreenWorld/Sprite_FS.glsl | 4 +- Res/Shader/GreenWorld/Sprite_VS.glsl | 4 +- 10 files changed, 94 insertions(+), 84 deletions(-) create mode 100644 Res/Assets/Textures/Liquefied/Test_2C_8Px.png diff --git a/Apps/Liquefied/LiquefiedApp.cpp b/Apps/Liquefied/LiquefiedApp.cpp index c2524a88..6b8464d0 100644 --- a/Apps/Liquefied/LiquefiedApp.cpp +++ b/Apps/Liquefied/LiquefiedApp.cpp @@ -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 @@ -30,6 +29,7 @@ namespace Liq { //Add solid cell to simulation grid _fluidSimulator->AddBorderCell(x, y); + _gridRenderer->Set(x, y, _defaultColor); } } } @@ -37,22 +37,24 @@ namespace Liq 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); } } } @@ -61,8 +63,8 @@ namespace Liq { _fluidSimulator->AddHorizonalTurbine ( - turbineOutlet.x, - turbineOutlet.y, + _turbineOutlet.x, + _turbineOutlet.y, (float)Engine::LiquefiedParams::turbinePower, dt ); @@ -89,6 +91,7 @@ namespace Liq 10, "GridShader" ); + _gridRenderer->SetDefaultColor(_defaultColor); Engine::RenderManager::Submit(_gridRenderer); //Create UI @@ -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; } @@ -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 { @@ -231,7 +220,7 @@ namespace Liq _fluidSimulator->Reset(); AddBorderCells(); AddTurbine(); - AddObstacles(); + AddObstacle(); Engine::LiquefiedParams::resetSimulation = false; } @@ -250,10 +239,9 @@ namespace Liq { Engine::PROFILE_SCOPE("Visualize grid"); - // RenderSmoke(); + RenderSmoke(); _gridRenderer->UpdateGpuStorage(); _gridRenderer->Flush(nullptr); - _spriteRenderer->Flush(nullptr); } { diff --git a/Apps/Liquefied/LiquefiedApp.hpp b/Apps/Liquefied/LiquefiedApp.hpp index 37b7a8c0..1705253a 100644 --- a/Apps/Liquefied/LiquefiedApp.hpp +++ b/Apps/Liquefied/LiquefiedApp.hpp @@ -9,24 +9,26 @@ namespace Liq { private: Engine::GridRenderer* _gridRenderer = nullptr; - Engine::SpriteRenderer* _spriteRenderer = nullptr; + Engine::Scope _interface; Engine::Scope _fluidSimulator; Engine::Scope _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; diff --git a/Engine/Rendering/Renderer/GridRenderer.cpp b/Engine/Rendering/Renderer/GridRenderer.cpp index 0a42557e..b0c6e3e7 100644 --- a/Engine/Rendering/Renderer/GridRenderer.cpp +++ b/Engine/Rendering/Renderer/GridRenderer.cpp @@ -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 @@ -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 @@ -84,7 +98,6 @@ namespace Engine _vboColor->Bind(); //Set uniforms - _shader->SetUniformVec3f("color", _defaultColor); _shader->SetUniformMat4f("projection", _orthoProj); _shader->SetUniformMat4f("model", _model); @@ -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) @@ -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. @@ -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++; diff --git a/Engine/Rendering/Renderer/GridRenderer.hpp b/Engine/Rendering/Renderer/GridRenderer.hpp index 5f53e000..d3df2488 100644 --- a/Engine/Rendering/Renderer/GridRenderer.hpp +++ b/Engine/Rendering/Renderer/GridRenderer.hpp @@ -14,23 +14,26 @@ namespace Engine { private: uint32 _gridWidth, _gridHeight, _quadSize, _quadAmountTotal; - glm::vec3 _defaultColor; Shader* _shader; + glm::vec3 _defaultColor; Scope _vao; Scope _vboVert, _vboColor; glm::mat4 _orthoProj, _model; - std::vector _colorStorage; + std::vector _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); }; diff --git a/Engine/Rendering/Renderer/RenderManager.cpp b/Engine/Rendering/Renderer/RenderManager.cpp index 4f883136..b04b683e 100644 --- a/Engine/Rendering/Renderer/RenderManager.cpp +++ b/Engine/Rendering/Renderer/RenderManager.cpp @@ -8,6 +8,11 @@ namespace Engine { _rendererStorage.clear(); _rendererStorage.reserve(10); + + //OpenGL-Rendersettings + GLRenderSettings::EnableBlending(); + GLRenderSettings::SetBlendFunc(GL_ONE_MINUS_SRC_ALPHA); + _initialized = true; } diff --git a/Engine/Rendering/Resources/TextureBuffer.cpp b/Engine/Rendering/Resources/TextureBuffer.cpp index 082eeeb9..497e3728 100644 --- a/Engine/Rendering/Resources/TextureBuffer.cpp +++ b/Engine/Rendering/Resources/TextureBuffer.cpp @@ -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++) diff --git a/README.md b/README.md index f8c38b93..0050e2a8 100644 --- a/README.md +++ b/README.md @@ -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**
[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 | - 07/2021 - 09/2021: Rewrite of the engine core - 01/2020 - 09/2020: Some very early projects (still under Windows) @@ -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 | diff --git a/Res/Assets/Textures/Liquefied/Test_2C_8Px.png b/Res/Assets/Textures/Liquefied/Test_2C_8Px.png new file mode 100644 index 0000000000000000000000000000000000000000..22ce4bcc9288a942a7b71dd11f58f6bff6db9a01 GIT binary patch literal 4250 zcmeHKe{2&~9B&6>Y_M$Lfb!GhfC!B1U9Z=*y{yC0j25sw{?-kERp|q&FlN# zd!O&;z4v+F_if)=f4Mt1XHeKR^et71Ug&70%&ZC6e-zW?Ot^b4;q z+Hc6uU%Tlv6~0=3wb^&M{m9%g+^~g{N?zDW?Jd3PZYdmhtj^dv?XS~on!i6Y#J%Q3 zY((Lb(^Eg1S?n7Vud48d*NHQW_MN%3;?9WGO%Lx3y|8zehu1f)$7Ym{op)~O zg!0z%b*rZB*t7ic?v_{ZEi+oC8tm6T)#rY8GwaElS3W;5E3oP13R~vAHrtt#hWtyJ zi*ipsC~4S!K0AN;fR=mjuH2nHvBWDY`!5cm4(2XioM2Gi!C&<=cMkq#^O7l^NtJGJsdJyW zUxgS58W(qa0t06fB6UrHBK+-FghiB*B3%u=prn z)QM^&&$1W_EGw_%m(}ui0iRxyQ=DXhAdD0aONK*{IGc3f8ZQgB+Dzh@22pAqcooEv zQ;s2wF)~JqC`(EWG+vT}6~_dTt#HjuQNWu6uThjJOOlC1!k91{MUym5 z0D_1&MieedMB)=viVhAJit{lksz`DKQ#rYyT(3BA9Qv`&`GuoCUl)BOo>BqyAd_5_ zG#M!}946B};!0TqkfbK`RF8ONV-%4UC@$B>cvRMaBFcnx3W4wPkJiUR+HwS*L?ILg zXdFg0^%zn;aa|s&1l3YFs(FFfJ&=kdc8k?xZfcLVob-u+dlzpHbm!VNFra)s)+O`x z>h!!W2d<9K3NkMVtY(_cMP`Ac5y1pePgt35!MG2*!eF!frPU1SME$o}!Q~NQ-F{b7K-jB^OGcl}aT5m6-{O zCXVL_I%qc&3}fO6juu6NHVKSfunD|q<1{LPXJ^W>FbCTyg}G`(MkCc)hblN*>i0Tu z+DLV`_(Pl`f`bE}BSq?y-5r%u7zGqgRnugpO+^;ULRl$>u~BK>^HD4gk*IQNxl?mj zw}pkpfUukzr$C^!!&+Ep3~`DatCZ!C1MkYZUEMx7P6DTJE>1x}+LebZJM(ZMMYE|1 zsB}JAki^FSLaT=dD+WAlxkrk__>Edq$B7D{x{kMw*N~(gB@ELJ1!xd%1Y35r(o8UU}S2+q?$82S6oc~OOs*^ zkd|b?ucHl;7vw^+D;cIVQ`62fd{TSy3`YR!zCn7WZ=YO!a`j4qUV;0ft52?8DbOo$ zUv&L%a^-Yir%(j`1ts9JG}7Aq1zfZS1ZTTl+LcT9z}dPItn8>~VO*yh^qgAsx^3GH z(3quoePvlUvW5@N%lbXBMZL0Hy{^(q!%r*TE6vei`5EpfPpVs8{SP(Hb60MpJQ;WE vHjT&hqi>17?T%c0DRR;3-?qmv@NQuE7F$8~pM?dN)j_>w<*t?)OW*hhK?Is8 literal 0 HcmV?d00001 diff --git a/Res/Shader/GreenWorld/Sprite_FS.glsl b/Res/Shader/GreenWorld/Sprite_FS.glsl index aa0288b4..f4deac07 100644 --- a/Res/Shader/GreenWorld/Sprite_FS.glsl +++ b/Res/Shader/GreenWorld/Sprite_FS.glsl @@ -1,5 +1,5 @@ #version 450 core - + in vec2 texCoords; out vec4 fragColor; @@ -10,4 +10,4 @@ uniform vec3 color; void main() { fragColor = vec4(color, 1.0f) * texture(image, texCoords); -} \ No newline at end of file +} diff --git a/Res/Shader/GreenWorld/Sprite_VS.glsl b/Res/Shader/GreenWorld/Sprite_VS.glsl index 85354983..0eaa0f67 100644 --- a/Res/Shader/GreenWorld/Sprite_VS.glsl +++ b/Res/Shader/GreenWorld/Sprite_VS.glsl @@ -1,5 +1,5 @@ #version 450 core - + layout(location = 0) in vec4 vertexIn; out vec2 texCoords; @@ -11,4 +11,4 @@ void main() { texCoords = vec2(vertexIn.zw); gl_Position = projection * model * vec4(vertexIn.xy, 0.0f, 1.0f); -} \ No newline at end of file +}