Skip to content

Commit

Permalink
Refactoring of texture functionalities finished (5/5). Back to debugg…
Browse files Browse the repository at this point in the history
…ing the

texture subsampling in the grid renderer
  • Loading branch information
Zang3th committed Jul 19, 2024
1 parent 5047a68 commit 2a8182c
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 96 deletions.
3 changes: 3 additions & 0 deletions Apps/GreenWorld/GreenWorldApp.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "GreenWorldApp.hpp"
#include "GLRenderSettings.hpp"

namespace GW
{
Expand Down Expand Up @@ -261,7 +262,9 @@ namespace GW

if(Engine::UIParams::debugSprites)
{
Engine::GLRenderSettings::DisableCulling();
_spriteRenderer->Flush(nullptr);
Engine::GLRenderSettings::EnableCulling();
}
}

Expand Down
25 changes: 20 additions & 5 deletions Apps/Liquefied/LiquefiedApp.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "LiquefiedApp.hpp"
#include "GlobalParams.hpp"
#include "ResourceManager.hpp"

namespace Liq
{
Expand All @@ -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
Expand Down Expand Up @@ -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
// (
Expand All @@ -114,8 +129,7 @@ namespace Liq
// );
_gridRenderer->AddTextureBufferSubsampled
(
// "ObstacleTexture",
"TestTexture",
"TestTexBuf",
obstaclePos,
obstacleSize
);
Expand Down Expand Up @@ -235,6 +249,7 @@ namespace Liq
// RenderSmoke();
_gridRenderer->UpdateGpuStorage();
_gridRenderer->Flush(nullptr);
_spriteRenderer->Flush(nullptr);
}

{
Expand Down
3 changes: 2 additions & 1 deletion Apps/Liquefied/LiquefiedApp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<LiquefiedInterface> _interface;
Engine::Scope<Engine::FluidSimulator> _fluidSimulator;
Engine::Scope<Engine::Timer> _physicsTimer, _inputTimer;
Expand Down
83 changes: 43 additions & 40 deletions Engine/Rendering/Renderer/GridRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
4 changes: 2 additions & 2 deletions Engine/Rendering/Renderer/RenderManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ namespace Engine
void RenderManager::CleanUp()
{
for(auto const& renderer : _rendererStorage)
{
delete renderer;
{
delete renderer;
}
}

Expand Down
1 change: 0 additions & 1 deletion Engine/Rendering/Renderer/SceneRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down
6 changes: 0 additions & 6 deletions Engine/Rendering/Renderer/SpriteRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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)
Expand Down
34 changes: 19 additions & 15 deletions Engine/Rendering/Resources/GLTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
Expand All @@ -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);
}
Expand All @@ -88,7 +92,7 @@ namespace Engine
{
GLCall(glDeleteTextures(1, &_textureID))

if(_textureBuffer != nullptr)
if(_ownsTexBufPointer == true)
{
delete _textureBuffer;
}
Expand Down Expand Up @@ -173,7 +177,7 @@ namespace Engine
return _numberOfRows;
}

TextureBuffer* GLTexture::GetTextureBuffer() const
const TextureBuffer* GLTexture::GetTextureBuffer() const
{
return _textureBuffer;
}
Expand Down
20 changes: 11 additions & 9 deletions Engine/Rendering/Resources/GLTexture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
};
}
Loading

0 comments on commit 2a8182c

Please sign in to comment.