Added Image::LoadArrayFrom* (convert an atlas to an array)

Also added Texture::LoadArrayFrom* shortcuts


Former-commit-id: f3cde12d5b634c75de8745fa800c30749e0fe662
This commit is contained in:
Lynix 2014-08-18 18:13:30 +02:00
parent 8640f86f44
commit 0d69267992
4 changed files with 157 additions and 0 deletions

View File

@ -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());

View File

@ -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());

View File

@ -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;

View File

@ -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;