Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
TextureLoader.cpp
Go to the documentation of this file.
2
3#include <stb_image.h>
4#include <vulkan/vulkan_core.h>
5#include <iostream>
6
8
9namespace EngineCore {
10 std::unique_ptr<TextureLoadData> TextureLoader::loadImage(const std::filesystem::path &path) {
11 int width, height, channels;
12
13 // Load image with STBI_rgb_alpha to ensure 4 channels (RGBA)
14 stbi_uc* pixels = stbi_load(
15 std::filesystem::absolute(path).generic_string().c_str(),
16 &width,
17 &height,
18 &channels,
19 STBI_rgb_alpha // Force 4 channels
20 );
21
22 if (!pixels) {
23 std::cerr << "Failed to load image at path: "
24 << std::filesystem::absolute(path).generic_string()
25 << std::endl;
26 throw std::runtime_error("Failed to load texture image!");
27 }
28
29 // Calculate the image size based on forced 4 channels
30 size_t imageSize = static_cast<size_t>(width) * height * 4;
31
32 // Copy pixel data into a vector
33 std::vector<unsigned char> imageData(pixels, pixels + imageSize);
34
35 // Free the memory allocated by stbi_load
36 stbi_image_free(pixels);
37
38 // Create and return the TextureLoadData struct
39 return std::make_unique<TextureLoadData>(std::move(imageData), width, height, 4);
40 }
41}
std::unique_ptr< TextureLoadData > loadImage(const std::filesystem::path &path)
Log category system implementation.