Skip to content

Commit

Permalink
Implemented texture modification
Browse files Browse the repository at this point in the history
  • Loading branch information
Zang3th committed Apr 19, 2024
1 parent 9aa5735 commit d0bf6c9
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 65 deletions.
10 changes: 5 additions & 5 deletions Apps/GreenWorld/GreenWorldApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ namespace GW
Engine::ResourceManager::LoadShader("SpriteShaderGreyscale", "../Res/Shader/GreenWorld/Sprite_VS.glsl", "../Res/Shader/GreenWorld/SpriteGreyscale_FS.glsl");

//Textures
Engine::ResourceManager::LoadTextureAtlasFromFile("ParticleTextureAtlas", "../Res/Assets/Textures/GreenWorld/SmokeAtlas.png", 8);
Engine::ResourceManager::LoadTextureFromFile("TerrainGrassTexture", "../Res/Assets/Textures/GreenWorld/Grass.jpg");
Engine::ResourceManager::LoadTextureFromFile("TerrainColormap", "../Res/Assets/Textures/GreenWorld/Colormap128.png");
Engine::ResourceManager::LoadTextureFromFile("WaterDuDvMap", "../Res/Assets/Textures/GreenWorld/DuDvMap.png");
Engine::ResourceManager::LoadTextureFromFile("WaterNormalMap", "../Res/Assets/Textures/GreenWorld/WaterNormalMap.png");
Engine::ResourceManager::LoadTextureAtlas("ParticleTextureAtlas", "../Res/Assets/Textures/GreenWorld/SmokeAtlas.png", 8);
Engine::ResourceManager::LoadTexture("TerrainGrassTexture", "../Res/Assets/Textures/GreenWorld/Grass.jpg");
Engine::ResourceManager::LoadTexture("TerrainColormap", "../Res/Assets/Textures/GreenWorld/Colormap128.png");
Engine::ResourceManager::LoadTexture("WaterDuDvMap", "../Res/Assets/Textures/GreenWorld/DuDvMap.png");
Engine::ResourceManager::LoadTexture("WaterNormalMap", "../Res/Assets/Textures/GreenWorld/WaterNormalMap.png");

//Heightmap
Engine::ResourceManager::LoadHeightmap("TerrainHeightmap", "../Res/Assets/Textures/GreenWorld/Heightmap128.bmp");
Expand Down
21 changes: 18 additions & 3 deletions Apps/Liquefied/LiquefiedApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Liq
Engine::ResourceManager::LoadShader("SpriteShader", "../Res/Shader/GreenWorld/Sprite_VS.glsl", "../Res/Shader/GreenWorld/Sprite_FS.glsl");

//Texture
Engine::ResourceManager::LoadTextureFromFile("BG_Texture", "../Res/Assets/Textures/Liquefied/Dark_Squares.jpg");
Engine::ResourceManager::LoadTextureToBuffer("BG_Texture", "../Res/Assets/Textures/Liquefied/Dark_Squares.jpg");
}

Engine::uint32 LiquefiedApp::InitModules()
Expand Down Expand Up @@ -77,13 +77,28 @@ namespace Liq
Engine::PROFILE_SCOPE("Render pixels");

Engine::RenderManager::RenderPixels();
}

//Test of the pixel renderer
if(Engine::Window::GetFrameCounter() == 75)
{
_pixelRenderer->Set(0, 0, COLOR_WHITE);
_pixelRenderer->Set(1919, 0, COLOR_RED);
_pixelRenderer->Set(0, 1079, COLOR_BLUE);
_pixelRenderer->Set(1919, 1079, COLOR_GREEN);
}
else if(Engine::Window::GetFrameCounter() == 0)
{
_pixelRenderer->Reset(0, 0);
_pixelRenderer->Reset(1919, 0);
_pixelRenderer->Reset(0, 1079);
_pixelRenderer->Reset(1919, 1079);
}
}

{
Engine::PROFILE_SCOPE("Render UI");

_interface->AddElements();
//_interface->AddElements();
_interface->Render();
}

Expand Down
4 changes: 2 additions & 2 deletions Engine/Rendering/Buffers/FrameBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace Engine
Texture* FrameBuffer::CreateTextureAttachment(const std::string& name, uint32 width, uint32 height)
{
//Create and configure texture
_texture = ResourceManager::LoadTexture(name, width, height, GL_RGB, GL_RGB, GL_UNSIGNED_BYTE);
_texture = ResourceManager::CreateTexture(name, width, height, GL_RGB, GL_RGB, GL_UNSIGNED_BYTE);

//Attach texture
GLCall(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _texture->GetTextureID(), 0))
Expand All @@ -55,7 +55,7 @@ namespace Engine
Texture* FrameBuffer::CreateDepthTextureAttachment(const std::string& name, const uint32 width, const uint32 height)
{
//Create and configure depth texture
_depthTexture = ResourceManager::LoadTexture(name, width, height, GL_DEPTH_COMPONENT32, GL_DEPTH_COMPONENT, GL_FLOAT);
_depthTexture = ResourceManager::CreateTexture(name, width, height, GL_DEPTH_COMPONENT32, GL_DEPTH_COMPONENT, GL_FLOAT);

//Attach depth texture
GLCall(glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, _depthTexture->GetTextureID(), 0))
Expand Down
4 changes: 2 additions & 2 deletions Engine/Rendering/Renderables/ObjLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ namespace Engine
//Load diffuse texture
std::string textureFilepath = baseFilepath + "/" + materials[i].diffuse_texname;
std::string textureName = filename + "DiffuseTexture";
ResourceManager::LoadTextureFromFile(textureName, textureFilepath);
ResourceManager::LoadTexture(textureName, textureFilepath);
mesh.textures.push_back(ResourceManager::GetTexture(textureName));
mesh.gotDiffuseTex = 1;
}
Expand All @@ -83,7 +83,7 @@ namespace Engine
//Load bump map
std::string textureFilepath = baseFilepath + "/" + materials[i].bump_texname;
std::string textureName = filename + "NormalTexture";
ResourceManager::LoadTextureFromFile(textureName, textureFilepath);
ResourceManager::LoadTexture(textureName, textureFilepath);
mesh.textures.push_back(ResourceManager::GetTexture(textureName));
mesh.gotNormalMap = 1;
}
Expand Down
5 changes: 5 additions & 0 deletions Engine/Rendering/Renderables/Sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,9 @@ namespace Engine
{
_texture = texture;
}

Texture* Sprite::GetTexture() const
{
return _texture;
}
}
2 changes: 2 additions & 0 deletions Engine/Rendering/Renderables/Sprite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,7 @@ namespace Engine
void ChangeRotation(float rotation);
void ChangeSize(const glm::vec2& size);
void SetTexture(Texture* texture);

[[nodiscard]] Texture* GetTexture() const;
};
}
15 changes: 5 additions & 10 deletions Engine/Rendering/Renderer/PixelRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Engine
GLRenderSettings::DisableCulling();

//Render sprite
RenderStatistics::drawnVertices +=_canvasSprite.Draw();
RenderStatistics::drawnVertices += _canvasSprite.Draw();
RenderStatistics::drawCalls++;

//Increase render pass counter
Expand All @@ -32,18 +32,13 @@ namespace Engine
}


void Set(uint32 x, uint32 y, const glm::vec3& color)
void PixelRenderer::Set(uint32 x, uint32 y, const glm::vec3& color) const
{

}

void Reset(uint32 x, uint32 y)
{

_canvasSprite.GetTexture()->ModifyTexture(x, y, color);
}

void AddBGTexture(const std::string& bgTexFilepath)
void PixelRenderer::Reset(uint32 x, uint32 y) const
{

_canvasSprite.GetTexture()->ResetTextureModification(x, y);
}
}
4 changes: 2 additions & 2 deletions Engine/Rendering/Renderer/PixelRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Engine

public:
void Flush(Renderer* renderer) final;
void Set(uint32 x, uint32 y, const glm::vec3& color);
void Reset(uint32 x, uint32 y);
void Set(uint32 x, uint32 y, const glm::vec3& color) const;
void Reset(uint32 x, uint32 y) const;
};
}
17 changes: 12 additions & 5 deletions Engine/Rendering/Resources/ResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,28 @@ namespace Engine
{
// ----- Public -----

Texture* ResourceManager::LoadTexture(const std::string& name, const uint32 width, const uint32 height, GLint internalFormat, GLenum format, GLenum type)
Texture* ResourceManager::CreateTexture(const std::string& name, const uint32 width, const uint32 height, GLint internalFormat, GLenum format, GLenum type)
{
auto texture = new Texture(width, height, internalFormat, format, type);
auto texture = new Texture((int32)width, (int32)height, internalFormat, format, type);
_textureStorage[name] = texture;
return texture;
}

Texture* ResourceManager::LoadTextureFromFile(const std::string& name, const std::string& filepath)
Texture* ResourceManager::LoadTexture(const std::string& name, const std::string& filepath)
{
auto texture = new Texture(filepath);
auto texture = new Texture(filepath, false);
_textureStorage[name] = texture;
return texture;
}

Texture* ResourceManager::LoadTextureAtlasFromFile(const std::string& name, const std::string& filepath, uint32 numberOfRows)
Texture* ResourceManager::LoadTextureToBuffer(const std::string& name, const std::string& filepath)
{
auto texture = new Texture(filepath, 0, true);
_textureStorage[name] = texture;
return texture;
}

Texture* ResourceManager::LoadTextureAtlas(const std::string& name, const std::string& filepath, uint32 numberOfRows)
{
auto texture = new Texture(filepath, numberOfRows);
_textureStorage[name] = texture;
Expand Down
7 changes: 4 additions & 3 deletions Engine/Rendering/Resources/ResourceManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ namespace Engine

ResourceManager() = delete;

static Texture* LoadTexture(const std::string& name, uint32 width, uint32 height, GLint internalFormat, GLenum format, GLenum type);
static Texture* LoadTextureFromFile(const std::string& name, const std::string& filepath);
static Texture* LoadTextureAtlasFromFile(const std::string& name, const std::string& filepath, uint32 numberOfRows);
static Texture* CreateTexture(const std::string& name, uint32 width, uint32 height, GLint internalFormat, GLenum format, GLenum type);
static Texture* LoadTexture(const std::string& name, const std::string& filepath);
static Texture* LoadTextureToBuffer(const std::string& name, const std::string& filepath);
static Texture* LoadTextureAtlas(const std::string& name, const std::string& filepath, uint32 numberOfRows);
static Texture* GetTexture(const std::string& name);
static std::string OutputTextureStorage();

Expand Down
113 changes: 85 additions & 28 deletions Engine/Rendering/Resources/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,69 @@ namespace Engine

void Texture::InitFromFile(const std::string &filepath)
{
int32 width, height, nrChannels;

ActivateTextureFlipOnLoad();
unsigned char* localBuffer = stbi_load(filepath.c_str(), &width, &height, &nrChannels, 0);
_imgBuffer = stbi_load(filepath.c_str(), &_width, &_height, &_nrChannels, 0);
DeactivateTextureFlipOnLoad();

if(localBuffer)
if(_saveToBuffer)
{
_backupBuffer = (unsigned char*)malloc(_width * _height * 3);
std::memcpy(_backupBuffer, _imgBuffer, _width * _height * 3);
}

if(_imgBuffer != nullptr)
{
GLenum format = 0;

if(nrChannels == 1)
format = GL_RED;
else if(nrChannels == 3)
format = GL_RGB;
else if(nrChannels == 4)
format = GL_RGBA;
if(_nrChannels == 1)
{
_format = GL_RED;
}
else if(_nrChannels == 3)
{
_format = GL_RGB;
}
else if(_nrChannels == 4)
{
_format = GL_RGBA;
}
else
{
_format = 0;
Logger::Error("Failed", "Image-Texture-Format", filepath);
}

if(format != 0)

if(_format != 0)
{
//Create texture
GLCall(glGenTextures(1, &_textureID))
Bind();
GLCall(glTexImage2D(GL_TEXTURE_2D, 0, _format, _width, _height, 0, _format, GL_UNSIGNED_BYTE, _imgBuffer))

//Texture parameters
GLCall(glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, localBuffer))
GLCall(glGenerateMipmap(GL_TEXTURE_2D))
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();

std::string texInfo = "(X: " + std::to_string(width) + ", Y: " + std::to_string(height) + ", Channels: " + std::to_string(nrChannels) + ")";
std::string texInfo = "(X: " + std::to_string(_width) + ", Y: " + std::to_string(_height) + ", Channels: " + std::to_string(_nrChannels) + ")";
Logger::Info("Loaded", "Texture", filepath);
Logger::Info("", "", texInfo);
}
}
else
Logger::Error("Failed", "Texture-Load", filepath);

stbi_image_free(localBuffer);
if(!_saveToBuffer)
{
stbi_image_free(_imgBuffer);
}
}

void Texture::Init(uint32 width, uint32 height, GLint internalFormat, GLenum format, GLenum type)
void Texture::Create(uint32 width, uint32 height, GLint internalFormat, GLenum format, GLenum type)
{
//Create texture
GLCall(glGenTextures(1, &_textureID))
Expand All @@ -64,27 +81,27 @@ namespace Engine

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

Texture::Texture(const std::string &filepath)
: _textureID(0), _numberOfRows(0)
Texture::Texture(const std::string &filepath, uint32 numberOfRows, bool saveToBuffer)
: _width(0), _height(0), _nrChannels(0), _format(0), _textureID(0), _numberOfRows(numberOfRows), _saveToBuffer(saveToBuffer), _imgBuffer(nullptr), _backupBuffer(nullptr)
{
InitFromFile(filepath);
}

Texture::Texture(const std::string &filepath, uint32 numberOfRows)
: _textureID(0), _numberOfRows(numberOfRows)
Texture::Texture(int32 width, int32 height, GLint internalFormat, GLenum format, GLenum type)
: _width(width), _height(height), _nrChannels(0), _format(format), _textureID(0), _numberOfRows(0), _saveToBuffer(false), _imgBuffer(nullptr), _backupBuffer(nullptr)
{
InitFromFile(filepath);
}

Texture::Texture(uint32 width, uint32 height, GLint internalFormat, GLenum format, GLenum type)
: _textureID(0), _numberOfRows(0)
{
Init(width, height, internalFormat, format, type);
Create(width, height, internalFormat, format, type);
}

Texture::~Texture()
{
GLCall(glDeleteTextures(1, &_textureID))

if(_saveToBuffer)
{
stbi_image_free(_imgBuffer);
free(_backupBuffer);
}
}

void Texture::Bind() const
Expand Down Expand Up @@ -149,6 +166,46 @@ namespace Engine
GLCall(glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor))
}

void Texture::ModifyTexture(uint32 x, uint32 y, const glm::vec3& color)
{
if(_saveToBuffer)
{
if((int32)x < _width && (int32)y < _height)
{
*(_imgBuffer + (y * _width * 3) + (x * 3) + 0) = (unsigned char)(color.x * 255);
*(_imgBuffer + (y * _width * 3) + (x * 3) + 1) = (unsigned char)(color.y * 255);
*(_imgBuffer + (y * _width * 3) + (x * 3) + 2) = (unsigned char)(color.z * 255);

Bind();
GLCall(glTexImage2D(GL_TEXTURE_2D, 0, _format, _width, _height, 0, _format, GL_UNSIGNED_BYTE, _imgBuffer));
}
}
else
{
Logger::Error("Failed", "Modification", "Texture wasn't saved");
}
}

void Texture::ResetTextureModification(uint32 x, uint32 y)
{
if(_saveToBuffer)
{
if((int32)x < _width && (int32)y < _height)
{
*(_imgBuffer + (y * _width * 3) + (x * 3) + 0) = *(_backupBuffer + (y * _width * 3) + (x * 3) + 0);
*(_imgBuffer + (y * _width * 3) + (x * 3) + 1) = *(_backupBuffer + (y * _width * 3) + (x * 3) + 1);
*(_imgBuffer + (y * _width * 3) + (x * 3) + 2) = *(_backupBuffer + (y * _width * 3) + (x * 3) + 2);

Bind();
GLCall(glTexImage2D(GL_TEXTURE_2D, 0, _format, _width, _height, 0, _format, GL_UNSIGNED_BYTE, _imgBuffer));
}
}
else
{
Logger::Error("Failed", "Modification", "Texture wasn't saved");
}
}

uint32 Texture::GetTextureID() const
{
return _textureID;
Expand Down
Loading

0 comments on commit d0bf6c9

Please sign in to comment.