Skip to content

Commit

Permalink
Fixed Texture2D::Create(...) (JeanPhilippeKernel#403)
Browse files Browse the repository at this point in the history
* texture2d

* update

* update
  • Loading branch information
jnyfah authored Jan 7, 2025
1 parent 37b98aa commit 09456d4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
41 changes: 27 additions & 14 deletions ZEngine/ZEngine/Rendering/Renderers/GraphicRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,28 +292,41 @@ namespace ZEngine::Rendering::Renderers

Helpers::Ref<Textures::Texture> GraphicRenderer::CreateTexture(uint32_t width, uint32_t height)
{
unsigned char image_data[] = {255, 255, 255, 255, '\0'};

Specifications::TextureSpecification spec = {
.Width = width,
.Height = height,
.BytePerPixel = Specifications::BytePerChannelMap[VALUE_FROM_SPEC_MAP(Specifications::ImageFormat::R8G8B8A8_SRGB)],
.Format = Specifications::ImageFormat::R8G8B8A8_SRGB,
.Data = image_data,
uint32_t BytePerPixel = Specifications::BytePerChannelMap[VALUE_FROM_SPEC_MAP(Specifications::ImageFormat::R8G8B8A8_SRGB)];
size_t data_size = width * height * BytePerPixel;
std::vector<unsigned char> image_data(data_size, 255);

Specifications::TextureSpecification spec = {
.Width = width,
.Height = height,
.BytePerPixel = BytePerPixel,
.Format = Specifications::ImageFormat::R8G8B8A8_SRGB,
.Data = image_data.data(),
};

return CreateTexture(spec);
}

Helpers::Ref<Textures::Texture> GraphicRenderer::CreateTexture(uint32_t width, uint32_t height, float r, float g, float b, float a)
{
unsigned char image_data[] = {0, 0, 0, 0, '\0'};
image_data[0] = static_cast<unsigned char>(std::clamp(r, .0f, 255.0f));
image_data[1] = static_cast<unsigned char>(std::clamp(g, .0f, 255.0f));
image_data[2] = static_cast<unsigned char>(std::clamp(b, .0f, 255.0f));
image_data[3] = static_cast<unsigned char>(std::clamp(a, .0f, 255.0f));
uint32_t BytePerPixel = Specifications::BytePerChannelMap[VALUE_FROM_SPEC_MAP(Specifications::ImageFormat::R8G8B8A8_SRGB)];
size_t data_size = width * height * BytePerPixel;
std::vector<unsigned char> image_data(data_size);

Specifications::TextureSpecification spec = {.Width = width, .Height = height, .BytePerPixel = Specifications::BytePerChannelMap[VALUE_FROM_SPEC_MAP(Specifications::ImageFormat::R8G8B8A8_SRGB)], .Format = Specifications::ImageFormat::R8G8B8A8_SRGB, .Data = image_data};
unsigned char r_byte = static_cast<unsigned char>(std::clamp(r * 255.0f, 0.0f, 255.0f));
unsigned char g_byte = static_cast<unsigned char>(std::clamp(g * 255.0f, 0.0f, 255.0f));
unsigned char b_byte = static_cast<unsigned char>(std::clamp(b * 255.0f, 0.0f, 255.0f));
unsigned char a_byte = static_cast<unsigned char>(std::clamp(a * 255.0f, 0.0f, 255.0f));

for (size_t i = 0; i < data_size; i += BytePerPixel)
{
image_data[i] = r_byte;
image_data[i + 1] = g_byte;
image_data[i + 2] = b_byte;
image_data[i + 3] = a_byte;
}

Specifications::TextureSpecification spec = {.Width = width, .Height = height, .BytePerPixel = BytePerPixel, .Format = Specifications::ImageFormat::R8G8B8A8_SRGB, .Data = image_data.data()};
return CreateTexture(spec);
}

Expand Down
2 changes: 1 addition & 1 deletion ZEngine/ZEngine/Rendering/Textures/Texture2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,4 @@ namespace ZEngine::Rendering::Textures
Dispose();
}

} // namespace ZEngine::Rendering::Textures
} // namespace ZEngine::Rendering::Textures

0 comments on commit 09456d4

Please sign in to comment.