From 0d692679922034080858d845f00cb47b3f86a9c3 Mon Sep 17 00:00:00 2001 From: Lynix Date: Mon, 18 Aug 2014 18:13:30 +0200 Subject: [PATCH] Added Image::LoadArrayFrom* (convert an atlas to an array) Also added Texture::LoadArrayFrom* shortcuts Former-commit-id: f3cde12d5b634c75de8745fa800c30749e0fe662 --- include/Nazara/Renderer/Texture.hpp | 6 ++ include/Nazara/Utility/Image.hpp | 6 ++ src/Nazara/Renderer/Texture.cpp | 48 ++++++++++++++ src/Nazara/Utility/Image.cpp | 97 +++++++++++++++++++++++++++++ 4 files changed, 157 insertions(+) diff --git a/include/Nazara/Renderer/Texture.hpp b/include/Nazara/Renderer/Texture.hpp index 53cb9e442..024216d70 100644 --- a/include/Nazara/Renderer/Texture.hpp +++ b/include/Nazara/Renderer/Texture.hpp @@ -62,6 +62,12 @@ class NAZARA_API NzTexture : public NzResource, NzNonCopyable bool LoadFromMemory(const void* data, std::size_t size, const NzImageParams& params = NzImageParams(), bool generateMipmaps = true); bool LoadFromStream(NzInputStream& stream, const NzImageParams& params = NzImageParams(), bool generateMipmaps = true); + // LoadArray + bool LoadArrayFromFile(const NzString& filePath, const NzImageParams& imageParams = NzImageParams(), bool generateMipmaps = true, const NzVector2ui& atlasSize = NzVector2ui(2, 2)); + bool LoadArrayFromImage(const NzImage& image, bool generateMipmaps = true, const NzVector2ui& atlasSize = NzVector2ui(2, 2)); + bool LoadArrayFromMemory(const void* data, std::size_t size, const NzImageParams& imageParams = NzImageParams(), bool generateMipmaps = true, const NzVector2ui& atlasSize = NzVector2ui(2, 2)); + bool LoadArrayFromStream(NzInputStream& stream, const NzImageParams& imageParams = NzImageParams(), bool generateMipmaps = true, const NzVector2ui& atlasSize = NzVector2ui(2, 2)); + // LoadCubemap bool LoadCubemapFromFile(const NzString& filePath, const NzImageParams& imageParams = NzImageParams(), bool generateMipmaps = true, const NzCubemapParams& cubemapParams = NzCubemapParams()); bool LoadCubemapFromImage(const NzImage& image, bool generateMipmaps = true, const NzCubemapParams& params = NzCubemapParams()); diff --git a/include/Nazara/Utility/Image.hpp b/include/Nazara/Utility/Image.hpp index 0b83022bc..21cefa6b3 100644 --- a/include/Nazara/Utility/Image.hpp +++ b/include/Nazara/Utility/Image.hpp @@ -91,6 +91,12 @@ class NAZARA_API NzImage : public NzResource bool LoadFromMemory(const void* data, std::size_t size, const NzImageParams& params = NzImageParams()); bool LoadFromStream(NzInputStream& stream, const NzImageParams& params = NzImageParams()); + // LoadArray + bool LoadArrayFromFile(const NzString& filePath, const NzImageParams& imageParams = NzImageParams(), const NzVector2ui& atlasSize = NzVector2ui(2, 2)); + bool LoadArrayFromImage(const NzImage& image, const NzVector2ui& atlasSize = NzVector2ui(2, 2)); + bool LoadArrayFromMemory(const void* data, std::size_t size, const NzImageParams& imageParams = NzImageParams(), const NzVector2ui& atlasSize = NzVector2ui(2, 2)); + bool LoadArrayFromStream(NzInputStream& stream, const NzImageParams& imageParams = NzImageParams(), const NzVector2ui& atlasSize = NzVector2ui(2, 2)); + // LoadCubemap bool LoadCubemapFromFile(const NzString& filePath, const NzImageParams& imageParams = NzImageParams(), const NzCubemapParams& cubemapParams = NzCubemapParams()); bool LoadCubemapFromImage(const NzImage& image, const NzCubemapParams& params = NzCubemapParams()); diff --git a/src/Nazara/Renderer/Texture.cpp b/src/Nazara/Renderer/Texture.cpp index 5038ccab8..4a5996e4e 100644 --- a/src/Nazara/Renderer/Texture.cpp +++ b/src/Nazara/Renderer/Texture.cpp @@ -661,6 +661,54 @@ bool NzTexture::LoadFromStream(NzInputStream& stream, const NzImageParams& param return LoadFromImage(image, generateMipmaps); } +bool NzTexture::LoadArrayFromFile(const NzString& filePath, const NzImageParams& imageParams, bool generateMipmaps, const NzVector2ui& atlasSize) +{ + NzImage cubemap; + if (!cubemap.LoadArrayFromFile(filePath, imageParams, atlasSize)) + { + NazaraError("Failed to load cubemap"); + return false; + } + + return LoadFromImage(cubemap, generateMipmaps); +} + +bool NzTexture::LoadArrayFromImage(const NzImage& image, bool generateMipmaps, const NzVector2ui& atlasSize) +{ + NzImage cubemap; + if (!cubemap.LoadArrayFromImage(image, atlasSize)) + { + NazaraError("Failed to load cubemap"); + return false; + } + + return LoadFromImage(cubemap, generateMipmaps); +} + +bool NzTexture::LoadArrayFromMemory(const void* data, std::size_t size, const NzImageParams& imageParams, bool generateMipmaps, const NzVector2ui& atlasSize) +{ + NzImage cubemap; + if (!cubemap.LoadArrayFromMemory(data, size, imageParams, atlasSize)) + { + NazaraError("Failed to load cubemap"); + return false; + } + + return LoadFromImage(cubemap, generateMipmaps); +} + +bool NzTexture::LoadArrayFromStream(NzInputStream& stream, const NzImageParams& imageParams, bool generateMipmaps, const NzVector2ui& atlasSize) +{ + NzImage cubemap; + if (!cubemap.LoadArrayFromStream(stream, imageParams, atlasSize)) + { + NazaraError("Failed to load cubemap"); + return false; + } + + return LoadFromImage(cubemap, generateMipmaps); +} + bool NzTexture::LoadCubemapFromFile(const NzString& filePath, const NzImageParams& imageParams, bool generateMipmaps, const NzCubemapParams& cubemapParams) { NzImage cubemap; diff --git a/src/Nazara/Utility/Image.cpp b/src/Nazara/Utility/Image.cpp index d9ba58cb7..7b963e30d 100644 --- a/src/Nazara/Utility/Image.cpp +++ b/src/Nazara/Utility/Image.cpp @@ -863,6 +863,103 @@ bool NzImage::LoadFromStream(NzInputStream& stream, const NzImageParams& params) return NzImageLoader::LoadFromStream(this, stream, params); } +// LoadArray +bool NzImage::LoadArrayFromFile(const NzString& filePath, const NzImageParams& imageParams, const NzVector2ui& atlasSize) +{ + NzImage image; + if (!image.LoadFromFile(filePath, imageParams)) + { + NazaraError("Failed to load image"); + return false; + } + + return LoadArrayFromImage(image, atlasSize); +} + +bool NzImage::LoadArrayFromImage(const NzImage& image, const NzVector2ui& atlasSize) +{ + #if NAZARA_UTILITY_SAFE + if (!image.IsValid()) + { + NazaraError("Image must be valid"); + return false; + } + + if (atlasSize.x == 0) + { + NazaraError("Atlas width must be over zero"); + return false; + } + + if (atlasSize.y == 0) + { + NazaraError("Atlas height must be over zero"); + return false; + } + #endif + + nzImageType type = image.GetType(); + + #if NAZARA_UTILITY_SAFE + if (type != nzImageType_1D && type != nzImageType_2D) + { + NazaraError("Image type not handled (0x" + NzString::Number(type, 16) + ')'); + return false; + } + #endif + + NzVector2ui imageSize(image.GetWidth(), image.GetHeight()); + + if (imageSize.x % atlasSize.x != 0) + { + NazaraWarning("Image width is not divisible by atlas width (" + NzString::Number(imageSize.x) + " mod " + NzString::Number(atlasSize.x) + " != 0)"); + } + + if (imageSize.y % atlasSize.y != 0) + { + NazaraWarning("Image height is not divisible by atlas height (" + NzString::Number(imageSize.y) + " mod " + NzString::Number(atlasSize.y) + " != 0)"); + } + + NzVector2ui faceSize = imageSize/atlasSize; + + // Selon le type de l'image de base, on va créer un array d'images 2D ou 1D + if (type == nzImageType_2D) + Create(nzImageType_2D_Array, image.GetFormat(), faceSize.x, faceSize.y); + else + Create(nzImageType_1D_Array, image.GetFormat(), faceSize.x, 1); + + unsigned int layer = 0; + for (unsigned int i = 0; i < atlasSize.x; ++i) + for (unsigned int j = 0; j < atlasSize.y; ++j) + Copy(image, NzRectui(i*faceSize.x, j*faceSize.y, faceSize.x, faceSize.y), NzVector3ui(0, 0, layer++)); + + return true; +} + +bool NzImage::LoadArrayFromMemory(const void* data, std::size_t size, const NzImageParams& imageParams, const NzVector2ui& atlasSize) +{ + NzImage image; + if (!image.LoadFromMemory(data, size, imageParams)) + { + NazaraError("Failed to load image"); + return false; + } + + return LoadArrayFromImage(image, atlasSize); +} + +bool NzImage::LoadArrayFromStream(NzInputStream& stream, const NzImageParams& imageParams, const NzVector2ui& atlasSize) +{ + NzImage image; + if (!image.LoadFromStream(stream, imageParams)) + { + NazaraError("Failed to load image"); + return false; + } + + return LoadArrayFromImage(image, atlasSize); +} + bool NzImage::LoadCubemapFromFile(const NzString& filePath, const NzImageParams& imageParams, const NzCubemapParams& cubemapParams) { NzImage image;