Files
Seele/src/Engine/Asset/TextureLoader.cpp
T

49 lines
1.7 KiB
C++
Raw Normal View History

2020-06-02 11:46:18 +02:00
#include "TextureLoader.h"
#include "TextureAsset.h"
#include "Graphics/Graphics.h"
#include "AssetRegistry.h"
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
2020-06-08 01:44:47 +02:00
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb_image_write.h>
2020-06-02 11:46:18 +02:00
using namespace Seele;
TextureLoader::TextureLoader(Gfx::PGraphics graphics)
: graphics(graphics)
{
2020-08-11 21:23:20 +02:00
import("textures/placeholder.png");
placeholderTexture = AssetRegistry::findTexture("textures/placeholder.png");
2020-06-02 11:46:18 +02:00
}
TextureLoader::~TextureLoader()
{
}
2020-06-08 01:44:47 +02:00
void TextureLoader::importAsset(const std::filesystem::path& filePath)
2020-06-02 11:46:18 +02:00
{
futures.add(std::async(std::launch::async, &TextureLoader::import, this, filePath));
}
2020-06-08 01:44:47 +02:00
void TextureLoader::import(const std::filesystem::path& path)
2020-06-02 11:46:18 +02:00
{
2020-08-11 21:23:20 +02:00
auto assetFileName = path;
std::filesystem::path assetPath = AssetRegistry::get().rootFolder.append(assetFileName.replace_extension("asset").filename().string());
2020-06-08 01:44:47 +02:00
PTextureAsset asset = new TextureAsset(assetPath);
asset->setStatus(Asset::Status::Loading);
2020-06-02 11:46:18 +02:00
int x, y, n;
2020-06-08 01:44:47 +02:00
unsigned char* data = stbi_load(path.string().c_str(), &x, &y, &n, 4);
2020-06-02 11:46:18 +02:00
TextureCreateInfo createInfo;
2020-06-08 01:44:47 +02:00
createInfo.format = Gfx::SE_FORMAT_R8G8B8A8_UINT;
2020-06-02 11:46:18 +02:00
createInfo.resourceData.data = data;
2020-06-08 01:44:47 +02:00
createInfo.resourceData.size = x * y * 4 * sizeof(unsigned char);
createInfo.resourceData.owner = Gfx::QueueType::DEDICATED_TRANSFER;
2020-06-02 11:46:18 +02:00
createInfo.width = x;
createInfo.height = y;
Gfx::PTexture2D texture = graphics->createTexture2D(createInfo);
2020-06-08 01:44:47 +02:00
stbi_image_free(data);
2020-06-02 11:46:18 +02:00
texture->transferOwnership(Gfx::QueueType::GRAPHICS);
asset->setTexture(texture);
asset->setStatus(Asset::Status::Ready);
2020-08-11 21:23:20 +02:00
AssetRegistry::get().textures[path.string()] = asset;
2020-06-02 11:46:18 +02:00
}