From 2aa06c865a211b32ca7bc3e8f6013e71e002a302 Mon Sep 17 00:00:00 2001 From: Zang3th Date: Mon, 22 Jul 2024 13:50:45 +0200 Subject: [PATCH] Modified PixelRenderer to work with new Texture <=> Buffer interface --- Engine/Rendering/Renderer/PixelRenderer.cpp | 164 ++++++++++--------- Engine/Rendering/Renderer/PixelRenderer.hpp | 74 ++++----- Engine/Rendering/Resources/GLTexture.cpp | 21 ++- Engine/Rendering/Resources/GLTexture.hpp | 1 + Engine/Rendering/Resources/TextureBuffer.cpp | 29 ++-- Engine/Rendering/Resources/TextureBuffer.hpp | 2 +- 6 files changed, 156 insertions(+), 135 deletions(-) diff --git a/Engine/Rendering/Renderer/PixelRenderer.cpp b/Engine/Rendering/Renderer/PixelRenderer.cpp index 9534611..aed8fe5 100644 --- a/Engine/Rendering/Renderer/PixelRenderer.cpp +++ b/Engine/Rendering/Renderer/PixelRenderer.cpp @@ -1,78 +1,86 @@ -// #include "PixelRenderer.hpp" -// -// namespace Engine -// { -// // ----- Public ----- -// -// PixelRenderer::PixelRenderer -// ( -// const uint32 width, -// const uint32 height, -// const uint32 pxSize, -// const std::string& bgTexture, -// const std::string& shader -// ) -// : _width(width), _height(height), _pxSize(pxSize), -// _canvasSprite(ResourceManager::GetTexture(bgTexture), -// ResourceManager::GetShader(shader), -// COLOR_WHITE, -// glm::vec2(_width * _pxSize, _height * _pxSize)) -// { -// Logger::Info("Created", "Renderer", __func__); -// } -// -// void PixelRenderer::Flush(Renderer* renderer) -// { -// //Commit texture changes -// _canvasSprite.GetTexture()->CommitModifications(); -// -// //Render sprite -// RenderStatistics::drawnVertices += _canvasSprite.Draw(); -// RenderStatistics::drawCalls++; -// -// //Increase render pass counter -// RenderStatistics::spritePasses++; -// } -// -// void PixelRenderer::Set(const uint32 x, const uint32 y, const glm::vec3& color) const -// { -// const uint32 x_end = x * _pxSize; -// const uint32 y_end = y * _pxSize; -// const uint32 x_start = x_end - _pxSize; -// const uint32 y_start = y_end - _pxSize; -// -// SetArea(x_start, x_end, y_start, y_end, color); -// } -// -// void PixelRenderer::SetArea(const uint32 x_start, const uint32 x_end, const uint32 y_start, const uint32 y_end, const glm::vec3& color) const -// { -// for(uint32 x = x_start; x < x_end; x++) -// { -// for(uint32 y = y_start; y < y_end; y++) -// { -// _canvasSprite.GetTexture()->ModifyTexture(x, y, color); -// } -// } -// } -// -// void PixelRenderer::ResetArea(const uint32 x_start, const uint32 x_end, const uint32 y_start, const uint32 y_end) const -// { -// for(uint32 x = x_start; x < x_end; x++) -// { -// for(uint32 y = y_start; y < y_end; y++) -// { -// _canvasSprite.GetTexture()->ResetTextureModification(x, y); -// } -// } -// } -// -// void PixelRenderer::SetScreen(const glm::vec3& color) const -// { -// SetArea(0, _width * _pxSize, 0, _height * _pxSize, color); -// } -// -// void PixelRenderer::ClearScreen() const -// { -// ResetArea(0, _width * _pxSize, 0, _height * _pxSize); -// } -// } +#include "PixelRenderer.hpp" + +namespace Engine +{ + // ----- Public ----- + + PixelRenderer::PixelRenderer + ( + const uint32 width, + const uint32 height, + const uint32 pxSize, + const std::string& bgTexture, + const std::string& shader + ) + : _width(width), _height(height), _pxSize(pxSize), + _canvasSprite(ResourceManager::GetGLTexture(bgTexture), + ResourceManager::GetShader(shader), + COLOR_WHITE, + glm::vec2(_width * _pxSize, _height * _pxSize)) + { + Logger::Info("Created", "Renderer", __func__); + } + + void PixelRenderer::Flush(Renderer* renderer) + { + //Commit texture changes + _canvasSprite.GetGLTexture()->UploadModifiedBuffer(); + + //Render sprite + RenderStatistics::drawnVertices += _canvasSprite.Draw(); + RenderStatistics::drawCalls++; + + //Increase render pass counter + RenderStatistics::spritePasses++; + } + + void PixelRenderer::Set(const uint32 x, const uint32 y, const PxColor& colorIn) const + { + const uint32 x_end = x * _pxSize; + const uint32 y_end = y * _pxSize; + const uint32 x_start = x_end - _pxSize; + const uint32 y_start = y_end - _pxSize; + + SetArea(x_start, x_end, y_start, y_end, colorIn); + } + + void PixelRenderer::SetArea(const uint32 x_start, const uint32 x_end, const uint32 y_start, const uint32 y_end, const PxColor& colorIn) const + { + for(uint32 x = x_start; x < x_end; x++) + { + for(uint32 y = y_start; y < y_end; y++) + { + //Modify texture buffer and break/quit if something goes wrong + if(_canvasSprite.GetGLTexture()->GetTextureBuffer()->SetPxColor(x, y, colorIn) == false) + { + break; + } + } + } + } + + void PixelRenderer::ResetArea(const uint32 x_start, const uint32 x_end, const uint32 y_start, const uint32 y_end) const + { + for(uint32 x = x_start; x < x_end; x++) + { + for(uint32 y = y_start; y < y_end; y++) + { + //Reset texture buffer back to the default values and break/quit if something goes wrong + if(_canvasSprite.GetGLTexture()->GetTextureBuffer()->ResetPxColor(x, y)) + { + break; + } + } + } + } + + void PixelRenderer::SetScreen(const PxColor& colorIn) const + { + SetArea(0, _width * _pxSize, 0, _height * _pxSize, colorIn); + } + + void PixelRenderer::ClearScreen() const + { + ResetArea(0, _width * _pxSize, 0, _height * _pxSize); + } +} diff --git a/Engine/Rendering/Renderer/PixelRenderer.hpp b/Engine/Rendering/Renderer/PixelRenderer.hpp index 1b6c3b5..c16092c 100644 --- a/Engine/Rendering/Renderer/PixelRenderer.hpp +++ b/Engine/Rendering/Renderer/PixelRenderer.hpp @@ -1,37 +1,37 @@ -// #pragma once -// -// #include "Renderer.hpp" -// #include "glm.hpp" -// #include "Sprite.hpp" -// #include "Texture.hpp" -// #include "Shader.hpp" -// #include "GLRenderSettings.hpp" -// #include "GlobalParams.hpp" -// #include "ResourceManager.hpp" -// -// namespace Engine -// { -// class PixelRenderer final : public Renderer -// { -// private: -// uint32 _width, _height, _pxSize; -// Sprite _canvasSprite; -// -// public: -// PixelRenderer -// ( -// uint32 width, -// uint32 height, -// uint32 pxSize, -// const std::string& bgTexture, -// const std::string& shader -// ); -// -// void Flush(Renderer* renderer) override; -// void Set(uint32 x, uint32 y, const glm::vec3& color) const; -// void SetArea(uint32 x_start, uint32 x_end, uint32 y_start, uint32 y_end, const glm::vec3& color) const; -// void ResetArea(uint32 x_start, uint32 x_end, uint32 y_start, uint32 y_end) const; -// void SetScreen(const glm::vec3& color) const; -// void ClearScreen() const; -// }; -// } +#pragma once + +#include "Renderer.hpp" +#include "glm.hpp" +#include "Sprite.hpp" +#include "GLTexture.hpp" +#include "Shader.hpp" +#include "GLRenderSettings.hpp" +#include "GlobalParams.hpp" +#include "ResourceManager.hpp" + +namespace Engine +{ + class PixelRenderer final : public Renderer + { + private: + uint32 _width, _height, _pxSize; + Sprite _canvasSprite; + + public: + PixelRenderer + ( + uint32 width, + uint32 height, + uint32 pxSize, + const std::string& bgTexture, + const std::string& shader + ); + + void Flush(Renderer* renderer) override; + void Set(uint32 x, uint32 y, const PxColor& colorIn) const; + void SetArea(uint32 x_start, uint32 x_end, uint32 y_start, uint32 y_end, const PxColor& colorIn) const; + void ResetArea(uint32 x_start, uint32 x_end, uint32 y_start, uint32 y_end) const; + void SetScreen(const PxColor& colorIn) const; + void ClearScreen() const; + }; +} diff --git a/Engine/Rendering/Resources/GLTexture.cpp b/Engine/Rendering/Resources/GLTexture.cpp index b48f273..250887c 100644 --- a/Engine/Rendering/Resources/GLTexture.cpp +++ b/Engine/Rendering/Resources/GLTexture.cpp @@ -11,10 +11,10 @@ namespace Engine if(_textureBuffer->GetInitStatus() == EXIT_SUCCESS) { //Save internal parameters - _width = _textureBuffer->GetWidth(); - _height = _textureBuffer->GetHeight(); + _width = _textureBuffer->GetWidth(); + _height = _textureBuffer->GetHeight(); _channels = _textureBuffer->GetChannels(); - _format = _textureBuffer->GetFormat(); + _format = _textureBuffer->GetFormat(); //Create texture from buffer GLCall(glGenTextures(1, &_textureID)) @@ -107,6 +107,21 @@ namespace Engine GLCall(glBindTexture(GL_TEXTURE_2D, 0)) } + void GLTexture::UploadModifiedBuffer() const + { + Bind(); + GLCall(glTexSubImage2D(GL_TEXTURE_2D, + 0, + 0, + 0, + _width, + _height, + _format, + GL_UNSIGNED_BYTE, + _textureBuffer->GetRawData())); + Unbind(); + } + void GLTexture::AddMipmapLinear() const { GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, 0)) diff --git a/Engine/Rendering/Resources/GLTexture.hpp b/Engine/Rendering/Resources/GLTexture.hpp index 16635d2..9dff742 100644 --- a/Engine/Rendering/Resources/GLTexture.hpp +++ b/Engine/Rendering/Resources/GLTexture.hpp @@ -37,6 +37,7 @@ namespace Engine void Bind() const; void BindToSlot(uint32 slot) const; void Unbind() const; + void UploadModifiedBuffer() const; void AddMipmapLinear() const; void AddAnisotropicFilter(float strength) const; diff --git a/Engine/Rendering/Resources/TextureBuffer.cpp b/Engine/Rendering/Resources/TextureBuffer.cpp index 678f21d..082eeeb 100644 --- a/Engine/Rendering/Resources/TextureBuffer.cpp +++ b/Engine/Rendering/Resources/TextureBuffer.cpp @@ -133,13 +133,13 @@ namespace Engine return false; } - bool TextureBuffer::SetPxColor(uint32 x, uint32 y, const PxColor& color) const + bool TextureBuffer::SetPxColor(uint32 x, uint32 y, const PxColor& colorIn) const { if(x < _width && y < _height) { - *(_pxBuffer + (y * _width * _channels) + (x * _channels) + 0) = color.r; - *(_pxBuffer + (y * _width * _channels) + (x * _channels) + 1) = color.g; - *(_pxBuffer + (y * _width * _channels) + (x * _channels) + 2) = color.b; + *(_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; return true; } @@ -149,20 +149,17 @@ namespace Engine bool TextureBuffer::ResetPxColor(uint32 x, uint32 y) const { - bool success = false; - if(_saveBackup) { - if(x < _width && y < _height) - { - *(_pxBuffer + (y * _width * 3) + (x * 3) + 0) = *(_backupBuffer + (y * _width * 3) + (x * 3) + 0); - *(_pxBuffer + (y * _width * 3) + (x * 3) + 1) = *(_backupBuffer + (y * _width * 3) + (x * 3) + 1); - *(_pxBuffer + (y * _width * 3) + (x * 3) + 2) = *(_backupBuffer + (y * _width * 3) + (x * 3) + 2); - success = true; - } - else + const PxColor color = { + *(_backupBuffer + (y * _width * _channels) + (x * _channels) + 0), + *(_backupBuffer + (y * _width * _channels) + (x * _channels) + 1), + *(_backupBuffer + (y * _width * _channels) + (x * _channels) + 2) + }; + + if(SetPxColor(x, y, color)) { - Logger::Error("Failed", "ResetPxColor", "Array out of bounds"); + return true; } } else @@ -170,7 +167,7 @@ namespace Engine Logger::Error("Failed", "ResetPxColor", "No backup buffer"); } - return success; + return false; } bool TextureBuffer::SubsampleArea(uint32 xpos, uint32 ypos, uint32 sampleAmount, glm::uvec3* colorOut) const diff --git a/Engine/Rendering/Resources/TextureBuffer.hpp b/Engine/Rendering/Resources/TextureBuffer.hpp index 3efa60a..9d6356a 100644 --- a/Engine/Rendering/Resources/TextureBuffer.hpp +++ b/Engine/Rendering/Resources/TextureBuffer.hpp @@ -33,7 +33,7 @@ namespace Engine [[nodiscard]] GLenum GetFormat() const; [[nodiscard]] unsigned char* GetRawData() const; [[nodiscard]] bool GetPxColor(uint32 x, uint32 y, PxColor* colorOut) const; - [[nodiscard]] bool SetPxColor(uint32 x, uint32 y, const PxColor& color) const; + [[nodiscard]] bool SetPxColor(uint32 x, uint32 y, const PxColor& colorIn) const; [[nodiscard]] bool ResetPxColor(uint32 x, uint32 y) const; [[nodiscard]] bool SubsampleArea(uint32 xpos, uint32 ypos, uint32 sampleAmount, glm::uvec3* colorOut) const;