2020-06-02 11:46:18 +02:00
|
|
|
#include "TextureLoader.h"
|
|
|
|
|
#include "TextureAsset.h"
|
|
|
|
|
#include "Graphics/Graphics.h"
|
|
|
|
|
#include "AssetRegistry.h"
|
2021-08-22 23:07:38 +02:00
|
|
|
#include "Graphics/Vulkan/VulkanGraphicsEnums.h"
|
2020-06-02 11:46:18 +02:00
|
|
|
#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>
|
2022-04-15 11:19:30 +02:00
|
|
|
#include "ktx.h"
|
2020-06-02 11:46:18 +02:00
|
|
|
|
|
|
|
|
using namespace Seele;
|
|
|
|
|
|
|
|
|
|
TextureLoader::TextureLoader(Gfx::PGraphics graphics)
|
|
|
|
|
: graphics(graphics)
|
|
|
|
|
{
|
2021-08-22 23:07:38 +02:00
|
|
|
placeholderAsset = new TextureAsset(std::filesystem::absolute("./textures/placeholder.ktx"));
|
2023-01-29 18:58:59 +01:00
|
|
|
placeholderAsset->load(graphics);
|
2023-01-21 18:43:21 +01:00
|
|
|
AssetRegistry::get().assetRoot.textures[""] = placeholderAsset;
|
2020-06-02 11:46:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextureLoader::~TextureLoader()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 18:43:21 +01:00
|
|
|
void TextureLoader::importAsset(const std::filesystem::path& path, const std::string& importPath)
|
2020-06-02 11:46:18 +02:00
|
|
|
{
|
2021-11-11 20:12:50 +01:00
|
|
|
std::filesystem::path assetPath = path.filename();
|
|
|
|
|
assetPath.replace_extension("asset");
|
|
|
|
|
PTextureAsset asset = new TextureAsset(assetPath.generic_string());
|
2020-09-19 14:36:50 +02:00
|
|
|
asset->setStatus(Asset::Status::Loading);
|
2021-08-22 23:07:38 +02:00
|
|
|
asset->setTexture(placeholderAsset->getTexture());
|
2023-01-21 18:43:21 +01:00
|
|
|
AssetRegistry::get().registerTexture(asset, importPath);
|
2021-11-11 20:12:50 +01:00
|
|
|
import(path, asset);
|
2020-10-03 11:00:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PTextureAsset TextureLoader::getPlaceholderTexture()
|
|
|
|
|
{
|
|
|
|
|
return placeholderAsset;
|
2020-06-02 11:46:18 +02:00
|
|
|
}
|
|
|
|
|
|
2022-02-24 22:38:26 +01:00
|
|
|
void TextureLoader::import(std::filesystem::path path, PTextureAsset textureAsset)
|
2020-06-02 11:46:18 +02:00
|
|
|
{
|
2023-01-29 18:58:59 +01:00
|
|
|
int totalWidth, totalHeight, n;
|
|
|
|
|
unsigned char* data = stbi_load(path.string().c_str(), &totalWidth, &totalHeight, &n, 4);
|
2021-08-22 23:07:38 +02:00
|
|
|
ktxTexture2* kTexture;
|
|
|
|
|
ktxTextureCreateInfo createInfo;
|
|
|
|
|
|
2023-01-29 18:58:59 +01:00
|
|
|
if (totalWidth / 4 == totalHeight / 3)
|
|
|
|
|
{
|
|
|
|
|
uint32 faceWidth = totalWidth / 4;
|
|
|
|
|
uint32 faceHeight = totalHeight / 3;
|
|
|
|
|
// Cube map
|
|
|
|
|
createInfo.vkFormat = VK_FORMAT_R8G8B8A8_SRGB;
|
|
|
|
|
createInfo.baseWidth = totalWidth / 4;
|
|
|
|
|
createInfo.baseHeight = totalHeight / 3;
|
|
|
|
|
createInfo.baseDepth = 1;
|
|
|
|
|
createInfo.numFaces = 6;
|
|
|
|
|
createInfo.numDimensions = 3;
|
|
|
|
|
createInfo.numLevels = 1;
|
|
|
|
|
createInfo.numLayers = 1;
|
|
|
|
|
createInfo.isArray = false;
|
|
|
|
|
createInfo.generateMipmaps = true;
|
2021-08-22 23:07:38 +02:00
|
|
|
|
2023-01-29 18:58:59 +01:00
|
|
|
ktxTexture2_Create(&createInfo,
|
|
|
|
|
KTX_TEXTURE_CREATE_ALLOC_STORAGE,
|
|
|
|
|
&kTexture);
|
2021-08-22 23:07:38 +02:00
|
|
|
|
2023-01-29 18:58:59 +01:00
|
|
|
auto loadCubeFace = [kTexture, faceWidth, totalWidth, data](int xPos, int yPos, int faceName)
|
|
|
|
|
{
|
|
|
|
|
std::vector<unsigned char> vec(faceWidth * faceWidth * 4);
|
|
|
|
|
for (int y = 0; y < faceWidth; ++y)
|
|
|
|
|
{
|
|
|
|
|
for (int x = 0; x < faceWidth; ++x)
|
|
|
|
|
{
|
|
|
|
|
int imgX = x + (xPos * faceWidth);
|
|
|
|
|
int imgY = y + (yPos * faceWidth);
|
|
|
|
|
std::memcpy(&vec[(x + (faceWidth * y)) * 4], &data[(imgX + (totalWidth * imgY)) * 4], 4);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ktxTexture_SetImageFromMemory(ktxTexture(kTexture),
|
|
|
|
|
0, 0, faceName, vec.data(), vec.size());
|
|
|
|
|
};
|
|
|
|
|
loadCubeFace(1, 0, 0);
|
|
|
|
|
loadCubeFace(0, 1, 1);
|
|
|
|
|
loadCubeFace(1, 1, 2);
|
|
|
|
|
loadCubeFace(2, 1, 3);
|
|
|
|
|
loadCubeFace(3, 1, 4);
|
|
|
|
|
loadCubeFace(1, 2, 5);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
createInfo.vkFormat = VK_FORMAT_R8G8B8A8_SRGB;
|
|
|
|
|
createInfo.baseWidth = totalWidth;
|
|
|
|
|
createInfo.baseHeight = totalHeight;
|
|
|
|
|
createInfo.baseDepth = 1;
|
|
|
|
|
createInfo.numDimensions = 1 + (totalHeight > 1);
|
|
|
|
|
createInfo.numLevels = 1;
|
|
|
|
|
createInfo.numLayers = 1;
|
|
|
|
|
createInfo.numFaces = 1;
|
|
|
|
|
createInfo.isArray = false;
|
|
|
|
|
createInfo.generateMipmaps = true;
|
|
|
|
|
|
|
|
|
|
ktxTexture2_Create(&createInfo,
|
|
|
|
|
KTX_TEXTURE_CREATE_ALLOC_STORAGE,
|
|
|
|
|
&kTexture);
|
|
|
|
|
|
|
|
|
|
ktxTexture_SetImageFromMemory(ktxTexture(kTexture),
|
|
|
|
|
0, 0, 0, data, totalWidth * totalHeight * 4 * sizeof(unsigned char));
|
|
|
|
|
}
|
2021-08-22 23:07:38 +02:00
|
|
|
|
2020-06-08 01:44:47 +02:00
|
|
|
stbi_image_free(data);
|
2021-08-22 23:07:38 +02:00
|
|
|
|
2021-10-15 23:12:29 +02:00
|
|
|
ktxTexture_WriteToNamedFile(ktxTexture(kTexture), textureAsset->getFullPath().c_str());
|
2021-08-22 23:07:38 +02:00
|
|
|
ktxTexture_Destroy(ktxTexture(kTexture));
|
2021-11-11 20:12:50 +01:00
|
|
|
|
2023-01-29 18:58:59 +01:00
|
|
|
textureAsset->load(graphics);
|
2021-11-11 20:12:50 +01:00
|
|
|
textureAsset->setStatus(Asset::Status::Ready);
|
2022-04-15 23:45:44 +02:00
|
|
|
////co_return;
|
2020-06-02 11:46:18 +02:00
|
|
|
}
|