Skip to content

Commit

Permalink
Modified PixelRenderer to work with new Texture <=> Buffer interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Zang3th committed Jul 22, 2024
1 parent c9451c6 commit 2aa06c8
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 135 deletions.
164 changes: 86 additions & 78 deletions Engine/Rendering/Renderer/PixelRenderer.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
}
74 changes: 37 additions & 37 deletions Engine/Rendering/Renderer/PixelRenderer.hpp
Original file line number Diff line number Diff line change
@@ -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;
};
}
21 changes: 18 additions & 3 deletions Engine/Rendering/Resources/GLTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down
1 change: 1 addition & 0 deletions Engine/Rendering/Resources/GLTexture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 13 additions & 16 deletions Engine/Rendering/Resources/TextureBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -149,28 +149,25 @@ 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
{
Logger::Error("Failed", "ResetPxColor", "No backup buffer");
}

return success;
return false;
}

bool TextureBuffer::SubsampleArea(uint32 xpos, uint32 ypos, uint32 sampleAmount, glm::uvec3* colorOut) const
Expand Down
2 changes: 1 addition & 1 deletion Engine/Rendering/Resources/TextureBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 2aa06c8

Please sign in to comment.