Skip to content

Commit

Permalink
Fixed several small texture bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Zang3th committed Jul 22, 2024
1 parent 2a8182c commit c9451c6
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 51 deletions.
12 changes: 8 additions & 4 deletions Apps/Liquefied/LiquefiedApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Liq
//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_1G_8Px.png");
Engine::ResourceManager::LoadTextureBuffer("TestTexBuf", "../Res/Assets/Textures/Liquefied/Test_1C_4Px.png");
}

void LiquefiedApp::AddBorderCells() const
Expand Down Expand Up @@ -110,12 +110,16 @@ namespace Liq
_spriteRenderer = new Engine::SpriteRenderer();
Engine::RenderManager::Submit(_spriteRenderer);

Engine::ResourceManager::CreateGLTextureFromBuffer("TestTexture", Engine::ResourceManager::GetTextureBuffer("TestTexBuf"));
auto* tex = Engine::ResourceManager::CreateGLTextureFromBuffer("TestTexture", Engine::ResourceManager::GetTextureBuffer("TestTexBuf"));
tex->Bind();
tex->AddMinFilterNearest();
tex->AddMaxFilterNearest();
tex->Unbind();

_spriteRenderer->AddSprite
(
glm::vec2(200.0f, 200.0f),
glm::vec2(10.0f, 800.0f),
glm::vec2(64.0f, 64.0f),
glm::vec2(10.0f, 700.0f),
Engine::ResourceManager::GetGLTexture("TestTexture"),
Engine::ResourceManager::GetShader("SpriteShader")
);
Expand Down
8 changes: 4 additions & 4 deletions Engine/Debug/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,28 @@ namespace Engine
void Logger::Info(const std::string& action, const std::string& obj, const std::string& params)
{
LOG(INFO) << " | " << FileManager::PadString(action, 8)
<< " | " << FileManager::PadString(obj, 12)
<< " | " << FileManager::PadString(obj, 13)
<< " | " << params;
}

void Logger::Warn(const std::string& action, const std::string&obj, const std::string& params)
{
LOG(WARNING) << " | " << FileManager::PadString(action, 8)
<< " | " << FileManager::PadString(obj, 12)
<< " | " << FileManager::PadString(obj, 13)
<< " | " << params;
}

void Logger::Error(const std::string& action, const std::string& obj, const std::string& params)
{
LOG(ERROR) << "| " << FileManager::PadString(action, 8)
<< " | " << FileManager::PadString(obj, 12)
<< " | " << FileManager::PadString(obj, 13)
<< " | " << params;
}

void Logger::Trace(const std::string& action, const std::string& obj, const std::string& params)
{
LOG(TRACE) << "| " << FileManager::PadString(action, 8)
<< " | " << FileManager::PadString(obj, 12)
<< " | " << FileManager::PadString(obj, 13)
<< " | " << params;
}

Expand Down
2 changes: 1 addition & 1 deletion Engine/Rendering/Renderer/GridRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ namespace Engine

void GridRenderer::Set(const uint32 x, const uint32 y, const glm::vec3& color)
{
_colorStorage.at(x * _gridHeight + y) = glm::vec4(color, 0.0f);
_colorStorage.at(x * _gridHeight + y) = glm::vec3(color);
}

void GridRenderer::Reset(const uint32 x, const uint32 y)
Expand Down
42 changes: 22 additions & 20 deletions Engine/Rendering/Resources/GLTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,14 @@ namespace Engine
_format,
GL_UNSIGNED_BYTE,
_textureBuffer->GetRawData()))

// Generate mip map
GLCall(glGenerateMipmap(GL_TEXTURE_2D))

//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", "ID: " + std::to_string(_textureID));
Logger::Info("Created", "GLTexture", _name + " (Texture-ID: " + std::to_string(_textureID) + ")");
initStatus = EXIT_SUCCESS;
}
else
{
Logger::Error("Failed", "TextureBuffer", "ID: " + std::to_string(_textureID));
Logger::Error("Failed", "GLTexture", _name + " (Texture-ID: " + std::to_string(_textureID) + ")");
}

return initStatus;
Expand All @@ -56,33 +48,33 @@ namespace Engine
GLCall(glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, _width, _height, 0, _format, type, nullptr))
Unbind();

std::string texInfo = "( X: " + std::to_string(_width) +
std::string texInfo = " (X: " + std::to_string(_width) +
", Y: " + std::to_string(_height) +
", Channels: " + std::to_string(_channels) + " )";
Logger::Info("Created", "GLTexture", texInfo);
", Channels: " + std::to_string(_channels) + ")";
Logger::Info("Created", "GLTexture", _name + texInfo);

return EXIT_SUCCESS;
}

// ----- Public -----

GLTexture::GLTexture(const TextureBuffer* texBuffer)
: _initStatus(EXIT_FAILURE), _width(0), _height(0), _channels(0), _textureID(0),
GLTexture::GLTexture(std::string name, const TextureBuffer* texBuffer)
: _name(std::move(name)), _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),
GLTexture::GLTexture(std::string name, const std::string& filepath, bool saveBackup, uint32 numberOfRows)
: _name(std::move(name)), _initStatus(EXIT_FAILURE), _width(0), _height(0), _channels(0), _textureID(0),
_numberOfRows(numberOfRows), _format(0), _ownsTexBufPointer(true), _textureBuffer(nullptr)
{
_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),
GLTexture::GLTexture(std::string name, uint32 width, uint32 height, GLint internalFormat, GLenum format, GLenum type)
: _name(std::move(name)), _initStatus(EXIT_FAILURE), _width(width), _height(height), _channels(0), _textureID(0),
_numberOfRows(0), _format(format), _ownsTexBufPointer(false), _textureBuffer(nullptr)
{
_initStatus = Create(internalFormat, type);
Expand Down Expand Up @@ -115,9 +107,19 @@ namespace Engine
GLCall(glBindTexture(GL_TEXTURE_2D, 0))
}

void GLTexture::AddMinFilterMipmapLinear() const
void GLTexture::AddMipmapLinear() const
{
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, 0))
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR))
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR))
GLCall(glGenerateMipmap(GL_TEXTURE_2D))

Logger::Info("Added", "Mipmap", "Texture-ID: " + std::to_string(_textureID));
}

void GLTexture::AddAnisotropicFilter(const float strength) const
{
GLCall(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, strength))
}

void GLTexture::AddMinFilterNearest() const
Expand Down
18 changes: 14 additions & 4 deletions Engine/Rendering/Resources/GLTexture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Engine
class GLTexture
{
private:
const std::string _name;
uint32 _initStatus, _width, _height, _channels, _textureID, _numberOfRows;
GLenum _format;
bool _ownsTexBufPointer;
Expand All @@ -20,16 +21,25 @@ namespace Engine
[[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);
explicit GLTexture(std::string name, const TextureBuffer* texBuffer);
explicit GLTexture(std::string name,
const std::string& filepath,
bool saveBackup,
uint32 numberOfRows = 0);
GLTexture(std::string name,
uint32 width,
uint32 height,
GLint internalFormat,
GLenum format,
GLenum type);
~GLTexture();

void Bind() const;
void BindToSlot(uint32 slot) const;
void Unbind() const;

void AddMinFilterMipmapLinear() const;
void AddMipmapLinear() const;
void AddAnisotropicFilter(float strength) const;
void AddMinFilterNearest() const;
void AddMaxFilterNearest() const;
void AddMinFilterLinear() const;
Expand Down
18 changes: 13 additions & 5 deletions Engine/Rendering/Resources/ResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,43 @@ namespace Engine

GLTexture* ResourceManager::LoadGLTexture(const std::string& name, const std::string& filepath)
{
auto* glTexture = new GLTexture(filepath, false);
auto* glTexture = new GLTexture(name, filepath, false);
glTexture->Bind();
glTexture->AddMipmapLinear();
glTexture->AddAnisotropicFilter(4.0f);
glTexture->Unbind();
_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);
auto* glTexture = new GLTexture(name, filepath, false, numberOfRows);
glTexture->Bind();
glTexture->AddMipmapLinear();
glTexture->AddAnisotropicFilter(4.0f);
glTexture->Unbind();
_glTextureStorage[name] = glTexture;
return glTexture ;
}

GLTexture* ResourceManager::LoadGLTextureWithBackup(const std::string& name, const std::string& filepath)
{
auto* glTexture = new GLTexture(filepath, true);
auto* glTexture = new GLTexture(name, filepath, true);
_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);
auto* glTexture = new GLTexture(name, width, height, internalFormat, format, type);
_glTextureStorage[name] = glTexture;
return glTexture;
}

GLTexture* ResourceManager::CreateGLTextureFromBuffer(const std::string& name, const TextureBuffer* texBuffer)
{
auto* glTexture = new GLTexture(texBuffer);
auto* glTexture = new GLTexture(name, texBuffer);
_glTextureStorage[name] = glTexture;
return glTexture;
}
Expand Down
46 changes: 33 additions & 13 deletions Engine/Rendering/Resources/TextureBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace Engine
}
else
{
Logger::Error("Failed", "malloc", filepath);
Logger::Error("Failed", "Texturebuffer", "malloc (" + filepath + ")");
}
}

Expand All @@ -52,19 +52,20 @@ namespace Engine
else
{
_format = 0;
Logger::Error("Failed", "Texture-Format", filepath);
Logger::Error("Failed", "Texturebuffer", "Format (" + filepath + ")");
}

Logger::Info("Loaded", "Texture", filepath);
std::string texInfo = "( X: " + std::to_string(_width) +
Logger::Info("Loaded", "Texturebuffer", filepath);
std::string texInfo = "(X: " + std::to_string(_width) +
", Y: " + std::to_string(_height) +
", Channels: " + std::to_string(_channels) + " )";
", Channels: " + std::to_string(_channels) +
", Format: " + std::to_string(_format) + ")";
Logger::Info("", "", texInfo);
initStatus = EXIT_SUCCESS;
}
else
{
Logger::Error("Failed", "Texture-Load", filepath);
Logger::Error("Failed", "Texturebuffer", "Loading (" + filepath + ")");
}

return initStatus;
Expand Down Expand Up @@ -122,9 +123,9 @@ namespace Engine
{
if(x < _width && y < _height)
{
colorOut->r = *(_pxBuffer + (y * _width * 3) + (x * 3) + 0);
colorOut->g = *(_pxBuffer + (y * _width * 3) + (x * 3) + 1);
colorOut->b = *(_pxBuffer + (y * _width * 3) + (x * 3) + 2);
colorOut->r = *(_pxBuffer + (y * _width * _channels) + (x * _channels) + 0);
colorOut->g = *(_pxBuffer + (y * _width * _channels) + (x * _channels) + 1);
colorOut->b = *(_pxBuffer + (y * _width * _channels) + (x * _channels) + 2);
return true;
}

Expand All @@ -136,9 +137,9 @@ namespace Engine
{
if(x < _width && y < _height)
{
*(_pxBuffer + (y * _width * 3) + (x * 3) + 0) = color.r;
*(_pxBuffer + (y * _width * 3) + (x * 3) + 1) = color.g;
*(_pxBuffer + (y * _width * 3) + (x * 3) + 2) = color.b;
*(_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;
return true;
}

Expand Down Expand Up @@ -210,4 +211,23 @@ namespace Engine

return success;
}
}

void TextureBuffer::PrintAllValues() const
{
for(uint32 x = 0; x < _width; x++)
{
for(uint32 y = 0; y < _height; y++)
{
PxColor color = {0, 0, 0};
if(GetPxColor(x, y, &color))
{
Logger::Print("Color (" + std::to_string(x) + ", "
+ std::to_string(y) + ") : "
+ std::to_string(color.r) + ", "
+ std::to_string(color.g) + ", "
+ std::to_string(color.b));
}
}
}
}
} // namespace Engine
2 changes: 2 additions & 0 deletions Engine/Rendering/Resources/TextureBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,7 @@ namespace Engine
[[nodiscard]] bool SetPxColor(uint32 x, uint32 y, const PxColor& color) const;
[[nodiscard]] bool ResetPxColor(uint32 x, uint32 y) const;
[[nodiscard]] bool SubsampleArea(uint32 xpos, uint32 ypos, uint32 sampleAmount, glm::uvec3* colorOut) const;

void PrintAllValues() const;
};
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c9451c6

Please sign in to comment.