Skip to content

Commit 09456d4

Browse files
authored
Fixed Texture2D::Create(...) (#403)
* texture2d * update * update
1 parent 37b98aa commit 09456d4

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

ZEngine/ZEngine/Rendering/Renderers/GraphicRenderer.cpp

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -292,28 +292,41 @@ namespace ZEngine::Rendering::Renderers
292292

293293
Helpers::Ref<Textures::Texture> GraphicRenderer::CreateTexture(uint32_t width, uint32_t height)
294294
{
295-
unsigned char image_data[] = {255, 255, 255, 255, '\0'};
296-
297-
Specifications::TextureSpecification spec = {
298-
.Width = width,
299-
.Height = height,
300-
.BytePerPixel = Specifications::BytePerChannelMap[VALUE_FROM_SPEC_MAP(Specifications::ImageFormat::R8G8B8A8_SRGB)],
301-
.Format = Specifications::ImageFormat::R8G8B8A8_SRGB,
302-
.Data = image_data,
295+
uint32_t BytePerPixel = Specifications::BytePerChannelMap[VALUE_FROM_SPEC_MAP(Specifications::ImageFormat::R8G8B8A8_SRGB)];
296+
size_t data_size = width * height * BytePerPixel;
297+
std::vector<unsigned char> image_data(data_size, 255);
298+
299+
Specifications::TextureSpecification spec = {
300+
.Width = width,
301+
.Height = height,
302+
.BytePerPixel = BytePerPixel,
303+
.Format = Specifications::ImageFormat::R8G8B8A8_SRGB,
304+
.Data = image_data.data(),
303305
};
304306

305307
return CreateTexture(spec);
306308
}
307309

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

316-
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};
316+
unsigned char r_byte = static_cast<unsigned char>(std::clamp(r * 255.0f, 0.0f, 255.0f));
317+
unsigned char g_byte = static_cast<unsigned char>(std::clamp(g * 255.0f, 0.0f, 255.0f));
318+
unsigned char b_byte = static_cast<unsigned char>(std::clamp(b * 255.0f, 0.0f, 255.0f));
319+
unsigned char a_byte = static_cast<unsigned char>(std::clamp(a * 255.0f, 0.0f, 255.0f));
320+
321+
for (size_t i = 0; i < data_size; i += BytePerPixel)
322+
{
323+
image_data[i] = r_byte;
324+
image_data[i + 1] = g_byte;
325+
image_data[i + 2] = b_byte;
326+
image_data[i + 3] = a_byte;
327+
}
328+
329+
Specifications::TextureSpecification spec = {.Width = width, .Height = height, .BytePerPixel = BytePerPixel, .Format = Specifications::ImageFormat::R8G8B8A8_SRGB, .Data = image_data.data()};
317330
return CreateTexture(spec);
318331
}
319332

ZEngine/ZEngine/Rendering/Textures/Texture2D.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,4 @@ namespace ZEngine::Rendering::Textures
244244
Dispose();
245245
}
246246

247-
} // namespace ZEngine::Rendering::Textures
247+
} // namespace ZEngine::Rendering::Textures

0 commit comments

Comments
 (0)