From 2a8182c504213fd0fa938e3c6f41ed52d1c7e59d Mon Sep 17 00:00:00 2001 From: Zang3th Date: Fri, 19 Jul 2024 13:49:51 +0200 Subject: [PATCH] Refactoring of texture functionalities finished (5/5). Back to debugging the texture subsampling in the grid renderer --- Apps/GreenWorld/GreenWorldApp.cpp | 3 + Apps/Liquefied/LiquefiedApp.cpp | 25 ++++-- Apps/Liquefied/LiquefiedApp.hpp | 3 +- Engine/Rendering/Renderer/GridRenderer.cpp | 83 ++++++++++--------- Engine/Rendering/Renderer/RenderManager.cpp | 4 +- Engine/Rendering/Renderer/SceneRenderer.cpp | 1 - Engine/Rendering/Renderer/SpriteRenderer.cpp | 6 -- Engine/Rendering/Resources/GLTexture.cpp | 34 ++++---- Engine/Rendering/Resources/GLTexture.hpp | 20 +++-- .../Rendering/Resources/ResourceManager.cpp | 51 +++++++++--- .../Rendering/Resources/ResourceManager.hpp | 1 + Engine/Rendering/Resources/TextureBuffer.cpp | 14 ++-- 12 files changed, 149 insertions(+), 96 deletions(-) diff --git a/Apps/GreenWorld/GreenWorldApp.cpp b/Apps/GreenWorld/GreenWorldApp.cpp index 2454086..817eb2f 100644 --- a/Apps/GreenWorld/GreenWorldApp.cpp +++ b/Apps/GreenWorld/GreenWorldApp.cpp @@ -1,4 +1,5 @@ #include "GreenWorldApp.hpp" +#include "GLRenderSettings.hpp" namespace GW { @@ -261,7 +262,9 @@ namespace GW if(Engine::UIParams::debugSprites) { + Engine::GLRenderSettings::DisableCulling(); _spriteRenderer->Flush(nullptr); + Engine::GLRenderSettings::EnableCulling(); } } diff --git a/Apps/Liquefied/LiquefiedApp.cpp b/Apps/Liquefied/LiquefiedApp.cpp index e9f00c4..91b7b91 100644 --- a/Apps/Liquefied/LiquefiedApp.cpp +++ b/Apps/Liquefied/LiquefiedApp.cpp @@ -1,5 +1,6 @@ #include "LiquefiedApp.hpp" #include "GlobalParams.hpp" +#include "ResourceManager.hpp" namespace Liq { @@ -9,11 +10,12 @@ namespace Liq { //Shader Engine::ResourceManager::LoadShader("GridShader", "../Res/Shader/Liquefied/Grid_VS.glsl", "../Res/Shader/Liquefied/Grid_FS.glsl"); + Engine::ResourceManager::LoadShader("SpriteShader", "../Res/Shader/GreenWorld/Sprite_VS.glsl", "../Res/Shader/GreenWorld/Sprite_FS.glsl"); //Textures - Engine::ResourceManager::LoadTextureBuffer("TurbineTexture", "../Res/Assets/Textures/Liquefied/Turbine_512.png"); - Engine::ResourceManager::LoadTextureBuffer("ObstacleTexture", "../Res/Assets/Textures/Liquefied/Box_512.png"); - Engine::ResourceManager::LoadTextureBuffer("TestTexture", "../Res/Assets/Textures/Liquefied/Test_1C_1G_8Px.png"); + // 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_1G_8Px.png"); } void LiquefiedApp::AddBorderCells() const @@ -105,6 +107,19 @@ namespace Liq AddTurbine(); AddObstacles(); + _spriteRenderer = new Engine::SpriteRenderer(); + Engine::RenderManager::Submit(_spriteRenderer); + + Engine::ResourceManager::CreateGLTextureFromBuffer("TestTexture", Engine::ResourceManager::GetTextureBuffer("TestTexBuf")); + + _spriteRenderer->AddSprite + ( + glm::vec2(200.0f, 200.0f), + glm::vec2(10.0f, 800.0f), + Engine::ResourceManager::GetGLTexture("TestTexture"), + Engine::ResourceManager::GetShader("SpriteShader") + ); + //Add sprites/textures to the config of the renderer // _gridRenderer->AddTextureSubsampled // ( @@ -114,8 +129,7 @@ namespace Liq // ); _gridRenderer->AddTextureBufferSubsampled ( - // "ObstacleTexture", - "TestTexture", + "TestTexBuf", obstaclePos, obstacleSize ); @@ -235,6 +249,7 @@ namespace Liq // RenderSmoke(); _gridRenderer->UpdateGpuStorage(); _gridRenderer->Flush(nullptr); + _spriteRenderer->Flush(nullptr); } { diff --git a/Apps/Liquefied/LiquefiedApp.hpp b/Apps/Liquefied/LiquefiedApp.hpp index d483e72..37b7a8c 100644 --- a/Apps/Liquefied/LiquefiedApp.hpp +++ b/Apps/Liquefied/LiquefiedApp.hpp @@ -8,7 +8,8 @@ namespace Liq class LiquefiedApp final : public Engine::App { private: - Engine::GridRenderer* _gridRenderer = nullptr; + Engine::GridRenderer* _gridRenderer = nullptr; + Engine::SpriteRenderer* _spriteRenderer = nullptr; Engine::Scope _interface; Engine::Scope _fluidSimulator; Engine::Scope _physicsTimer, _inputTimer; diff --git a/Engine/Rendering/Renderer/GridRenderer.cpp b/Engine/Rendering/Renderer/GridRenderer.cpp index 551d179..d7ed31f 100644 --- a/Engine/Rendering/Renderer/GridRenderer.cpp +++ b/Engine/Rendering/Renderer/GridRenderer.cpp @@ -124,45 +124,48 @@ namespace Engine void GridRenderer::AddTextureBufferSubsampled(const std::string& texBuffer, const glm::uvec2& pos, const uint32 size) { auto* textureBuffer = ResourceManager::GetTextureBuffer(texBuffer); - // uint32 width = tex->GetWidth(); - // uint32 height = tex->GetHeight(); - // - // if(width != height || width % size != 0 || height % size != 0) - // { - // Logger::Error("Failed", "Subsampling", "Dimensions or format unsupported"); - // return; - // } - // - // //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. - // //In our case these pixels are cells in the grid. - // uint32 sampleAmount = width / size; - // - // glm::uvec3 subsampledColor = {0, 0, 0}; - // glm::uvec2 gridPos = pos; - // bool success = false; - // - // //Go over the image in sampleAmount steps - // for(uint32 x = 0; x < width; x += sampleAmount) - // { - // for(uint32 y = 0; y < height; y += sampleAmount) - // { - // success = tex->Subsample(x, y, sampleAmount, &subsampledColor); - // 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); - // } - // gridPos.y++; - // } - // gridPos.x++; - // gridPos.y = pos.y; //Reset y-position - // } + uint32 width = textureBuffer->GetWidth(); + uint32 height = textureBuffer->GetHeight(); + + if(width != height || width % size != 0 || height % size != 0) + { + Logger::Error("Failed", "Subsampling", "Dimensions or format unsupported"); + return; + } + + //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. + //In our case these pixels are cells in the grid. + uint32 sampleAmount = width / size; + + glm::uvec2 gridPos = pos; + bool success = false; + + //Go over the image in sampleAmount steps + for(uint32 x = 0; x < width; x += sampleAmount) + { + for(uint32 y = 0; y < height; y += sampleAmount) + { + glm::uvec3 subsampledColor = {0, 0, 0}; + success = textureBuffer->SubsampleArea(x, y, sampleAmount, &subsampledColor); + + 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); + } + + gridPos.y++; + } + + gridPos.x++; + gridPos.y = pos.y; //Reset y-position + } } } diff --git a/Engine/Rendering/Renderer/RenderManager.cpp b/Engine/Rendering/Renderer/RenderManager.cpp index d87adce..4f88313 100644 --- a/Engine/Rendering/Renderer/RenderManager.cpp +++ b/Engine/Rendering/Renderer/RenderManager.cpp @@ -31,8 +31,8 @@ namespace Engine void RenderManager::CleanUp() { for(auto const& renderer : _rendererStorage) - { - delete renderer; + { + delete renderer; } } diff --git a/Engine/Rendering/Renderer/SceneRenderer.cpp b/Engine/Rendering/Renderer/SceneRenderer.cpp index 01f77bb..ba48bad 100644 --- a/Engine/Rendering/Renderer/SceneRenderer.cpp +++ b/Engine/Rendering/Renderer/SceneRenderer.cpp @@ -175,7 +175,6 @@ namespace Engine void SceneRenderer::FlushCubemap() { GLRenderSettings::SetCullFace(GL_FRONT); - GLRenderSettings::DisableWireframe(); RenderStatistics::drawnVertices += _cubemap->Draw(_perspProj, Camera3D::GetViewMatrix()); RenderStatistics::drawCalls++; RenderStatistics::cubemapPasses++; diff --git a/Engine/Rendering/Renderer/SpriteRenderer.cpp b/Engine/Rendering/Renderer/SpriteRenderer.cpp index e3db61c..8ac8773 100644 --- a/Engine/Rendering/Renderer/SpriteRenderer.cpp +++ b/Engine/Rendering/Renderer/SpriteRenderer.cpp @@ -11,10 +11,6 @@ namespace Engine void SpriteRenderer::Flush(Renderer* renderer) { - GLRenderSettings::DisableWireframe(); - GLRenderSettings::DisableCulling(); - - //Render sprites for(const auto& sprite : _spriteStorage) { RenderStatistics::drawnVertices += sprite->Draw(); @@ -23,8 +19,6 @@ namespace Engine //Increase render pass counter RenderStatistics::spritePasses++; - - GLRenderSettings::EnableCulling(); } void SpriteRenderer::AddSprite(const glm::vec2& size, const glm::vec2& pos, GLTexture* glTexture, Shader* shader) diff --git a/Engine/Rendering/Resources/GLTexture.cpp b/Engine/Rendering/Resources/GLTexture.cpp index d16ff11..8d4f560 100644 --- a/Engine/Rendering/Resources/GLTexture.cpp +++ b/Engine/Rendering/Resources/GLTexture.cpp @@ -4,10 +4,9 @@ namespace Engine { // ----- Private ----- - uint32 GLTexture::Init(const std::string &filepath, bool saveBackup) + uint32 GLTexture::Init() { uint32 initStatus = EXIT_FAILURE; - _textureBuffer = new TextureBuffer(filepath, saveBackup); if(_textureBuffer->GetInitStatus() == EXIT_SUCCESS) { @@ -33,21 +32,18 @@ namespace Engine // Generate mip map GLCall(glGenerateMipmap(GL_TEXTURE_2D)) - //Old defaults - // GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)) - // GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)) - //Activate anisotropic filtering GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, 0)) GLCall(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 4.0f)) + Unbind(); - Logger::Info("Created", "GLTexture", filepath); + Logger::Info("Created", "GLTexture", "ID: " + std::to_string(_textureID)); initStatus = EXIT_SUCCESS; } else { - Logger::Error("Failed", "TextureBuffer", filepath); + Logger::Error("Failed", "TextureBuffer", "ID: " + std::to_string(_textureID)); } return initStatus; @@ -70,16 +66,24 @@ namespace Engine // ----- Public ----- + GLTexture::GLTexture(const TextureBuffer* texBuffer) + : _initStatus(EXIT_FAILURE), _width(0), _height(0), _channels(0), _textureID(0), + _numberOfRows(0), _format(0), _ownsTexBufPointer(false), _textureBuffer(texBuffer) + { + _initStatus = Init(); + } + GLTexture::GLTexture(const std::string& filepath, bool saveBackup, uint32 numberOfRows) - : _initStatus(EXIT_FAILURE), _width(0), _height(0), _channels(0), - _textureID(0), _numberOfRows(numberOfRows), _format(0), _textureBuffer(nullptr) + : _initStatus(EXIT_FAILURE), _width(0), _height(0), _channels(0), _textureID(0), + _numberOfRows(numberOfRows), _format(0), _ownsTexBufPointer(true), _textureBuffer(nullptr) { - _initStatus = Init(filepath, saveBackup); + _textureBuffer = new TextureBuffer(filepath, saveBackup); + _initStatus = Init(); } GLTexture::GLTexture(uint32 width, uint32 height, GLint internalFormat, GLenum format, GLenum type) - : _initStatus(EXIT_FAILURE), _width(width), _height(height), _channels(0), - _textureID(0), _numberOfRows(0), _format(format), _textureBuffer(nullptr) + : _initStatus(EXIT_FAILURE), _width(width), _height(height), _channels(0), _textureID(0), + _numberOfRows(0), _format(format), _ownsTexBufPointer(false), _textureBuffer(nullptr) { _initStatus = Create(internalFormat, type); } @@ -88,7 +92,7 @@ namespace Engine { GLCall(glDeleteTextures(1, &_textureID)) - if(_textureBuffer != nullptr) + if(_ownsTexBufPointer == true) { delete _textureBuffer; } @@ -173,7 +177,7 @@ namespace Engine return _numberOfRows; } - TextureBuffer* GLTexture::GetTextureBuffer() const + const TextureBuffer* GLTexture::GetTextureBuffer() const { return _textureBuffer; } diff --git a/Engine/Rendering/Resources/GLTexture.hpp b/Engine/Rendering/Resources/GLTexture.hpp index 0a78591..2772476 100644 --- a/Engine/Rendering/Resources/GLTexture.hpp +++ b/Engine/Rendering/Resources/GLTexture.hpp @@ -11,14 +11,16 @@ namespace Engine class GLTexture { private: - uint32 _initStatus, _width, _height, _channels, _textureID, _numberOfRows; - GLenum _format; - TextureBuffer* _textureBuffer; + uint32 _initStatus, _width, _height, _channels, _textureID, _numberOfRows; + GLenum _format; + bool _ownsTexBufPointer; + const TextureBuffer* _textureBuffer; - [[nodiscard]] uint32 Init(const std::string& filepath, bool saveBackup); + [[nodiscard]] uint32 Init(); [[nodiscard]] uint32 Create(GLint internalFormat, GLenum type); public: + explicit GLTexture(const TextureBuffer* texBuffer); explicit GLTexture(const std::string& filepath, bool saveBackup, uint32 numberOfRows = 0); GLTexture(uint32 width, uint32 height, GLint internalFormat, GLenum format, GLenum type); ~GLTexture(); @@ -36,10 +38,10 @@ namespace Engine void AddClampToEdge() const; void AddBorderColor(const glm::vec4& color) const; - [[nodiscard]] uint32 GetWidth() const; - [[nodiscard]] uint32 GetHeight() const; - [[nodiscard]] uint32 GetTextureID() const; - [[nodiscard]] uint32 GetNumberOfRows() const; - [[nodiscard]] TextureBuffer* GetTextureBuffer() const; + [[nodiscard]] uint32 GetWidth() const; + [[nodiscard]] uint32 GetHeight() const; + [[nodiscard]] uint32 GetTextureID() const; + [[nodiscard]] uint32 GetNumberOfRows() const; + [[nodiscard]] const TextureBuffer* GetTextureBuffer() const; }; } diff --git a/Engine/Rendering/Resources/ResourceManager.cpp b/Engine/Rendering/Resources/ResourceManager.cpp index 89c03cb..c05694c 100644 --- a/Engine/Rendering/Resources/ResourceManager.cpp +++ b/Engine/Rendering/Resources/ResourceManager.cpp @@ -7,34 +7,47 @@ namespace Engine GLTexture* ResourceManager::LoadGLTexture(const std::string& name, const std::string& filepath) { auto* glTexture = new GLTexture(filepath, false); - _glTextureStorage[name] = glTexture ; + _glTextureStorage[name] = glTexture; return glTexture ; } GLTexture* ResourceManager::LoadGLTextureAtlas(const std::string& name, const std::string& filepath, uint32 numberOfRows) { auto* glTexture = new GLTexture(filepath, false, numberOfRows); - _glTextureStorage[name] = glTexture ; + _glTextureStorage[name] = glTexture; return glTexture ; } GLTexture* ResourceManager::LoadGLTextureWithBackup(const std::string& name, const std::string& filepath) { auto* glTexture = new GLTexture(filepath, true); - _glTextureStorage[name] = glTexture ; + _glTextureStorage[name] = glTexture; return glTexture ; } GLTexture* ResourceManager::CreateGLTexture(const std::string& name, const uint32 width, const uint32 height, GLint internalFormat, GLenum format, GLenum type) { auto* glTexture = new GLTexture(width, height, internalFormat, format, type); - _glTextureStorage[name] = glTexture ; + _glTextureStorage[name] = glTexture; + return glTexture; + } + + GLTexture* ResourceManager::CreateGLTextureFromBuffer(const std::string& name, const TextureBuffer* texBuffer) + { + auto* glTexture = new GLTexture(texBuffer); + _glTextureStorage[name] = glTexture; return glTexture; } GLTexture* ResourceManager::GetGLTexture(const std::string& name) { - return _glTextureStorage[name]; + auto iter = _glTextureStorage.find(name); + if(iter == _glTextureStorage.end()) + { + Logger::Error("Failed", "GetGLTexture", name); + return nullptr; + } + return iter->second; } std::string ResourceManager::OutputGLTextureStorage() @@ -43,7 +56,7 @@ namespace Engine for(auto const& glTex : _glTextureStorage) { - output += (glTex .first + "\n"); + output += (glTex.first + "\n"); } return output; @@ -65,7 +78,13 @@ namespace Engine TextureBuffer* ResourceManager::GetTextureBuffer(const std::string& name) { - return _texBufferStorage[name]; + auto iter = _texBufferStorage.find(name); + if(iter == _texBufferStorage.end()) + { + Logger::Error("Failed", "GetTextureBuffer", name); + return nullptr; + } + return iter->second; } Shader* ResourceManager::LoadShader(const std::string& name, const std::string& vsFilepath, const std::string& fsFilepath) @@ -77,7 +96,13 @@ namespace Engine Shader* ResourceManager::GetShader(const std::string& name) { - return _shaderStorage[name]; + auto iter = _shaderStorage.find(name); + if(iter == _shaderStorage.end()) + { + Logger::Error("Failed", "GetShader", name); + return nullptr; + } + return iter->second; } std::string ResourceManager::OutputShaderStorage() @@ -101,7 +126,13 @@ namespace Engine Heightmap* ResourceManager::GetHeightmap(const std::string& name) { - return _heightmapStorage[name]; + auto iter = _heightmapStorage.find(name); + if(iter == _heightmapStorage.end()) + { + Logger::Error("Failed", "GetHeightmap", name); + return nullptr; + } + return iter->second; } void ResourceManager::CleanUp() @@ -113,7 +144,7 @@ namespace Engine for(auto const& texBuffer : _texBufferStorage) { - delete texBuffer .second; + delete texBuffer.second; } for(auto const& shader : _shaderStorage) diff --git a/Engine/Rendering/Resources/ResourceManager.hpp b/Engine/Rendering/Resources/ResourceManager.hpp index 0a5cc38..ef35c3d 100644 --- a/Engine/Rendering/Resources/ResourceManager.hpp +++ b/Engine/Rendering/Resources/ResourceManager.hpp @@ -24,6 +24,7 @@ namespace Engine static GLTexture* LoadGLTextureAtlas(const std::string& name, const std::string& filepath, uint32 numberOfRows); static GLTexture* LoadGLTextureWithBackup(const std::string& name, const std::string& filepath); static GLTexture* CreateGLTexture(const std::string& name, uint32 width, uint32 height, GLint internalFormat, GLenum format, GLenum type); + static GLTexture* CreateGLTextureFromBuffer(const std::string& name, const TextureBuffer* texBuffer); static GLTexture* GetGLTexture(const std::string& name); static std::string OutputGLTextureStorage(); diff --git a/Engine/Rendering/Resources/TextureBuffer.cpp b/Engine/Rendering/Resources/TextureBuffer.cpp index 094a45a..fefdc16 100644 --- a/Engine/Rendering/Resources/TextureBuffer.cpp +++ b/Engine/Rendering/Resources/TextureBuffer.cpp @@ -179,13 +179,13 @@ namespace Engine 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; - // } + 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++) {