Improved Texture class code
Removed some code duplication Former-commit-id: affa0bf18ea317b3276a7e8d0a6351f28037dfba
This commit is contained in:
parent
ad0fa61cdf
commit
2a29dcf6e9
|
|
@ -661,122 +661,50 @@ bool NzTexture::LoadFromStream(NzInputStream& stream, const NzImageParams& param
|
||||||
|
|
||||||
bool NzTexture::LoadCubemapFromFile(const NzString& filePath, const NzImageParams& imageParams, bool generateMipmaps, const NzCubemapParams& cubemapParams)
|
bool NzTexture::LoadCubemapFromFile(const NzString& filePath, const NzImageParams& imageParams, bool generateMipmaps, const NzCubemapParams& cubemapParams)
|
||||||
{
|
{
|
||||||
NzImage image;
|
NzImage cubemap;
|
||||||
if (!image.LoadFromFile(filePath, imageParams))
|
if (!cubemap.LoadCubemapFromFile(filePath, imageParams, cubemapParams))
|
||||||
{
|
{
|
||||||
NazaraError("Failed to load image");
|
NazaraError("Failed to load cubemap");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return LoadCubemapFromImage(image, generateMipmaps, cubemapParams);
|
return LoadFromImage(cubemap, generateMipmaps);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NzTexture::LoadCubemapFromImage(const NzImage& image, bool generateMipmaps, const NzCubemapParams& params)
|
bool NzTexture::LoadCubemapFromImage(const NzImage& image, bool generateMipmaps, const NzCubemapParams& params)
|
||||||
{
|
{
|
||||||
// Implémentation presque identique à celle de Image::LoadCubemapFromImage
|
NzImage cubemap;
|
||||||
#if NAZARA_UTILITY_SAFE
|
if (!cubemap.LoadCubemapFromImage(image, params))
|
||||||
if (!image.IsValid())
|
|
||||||
{
|
{
|
||||||
NazaraError("Image must be valid");
|
NazaraError("Failed to load cubemap");
|
||||||
return false;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
unsigned int width = image.GetWidth();
|
|
||||||
unsigned int height = image.GetHeight();
|
|
||||||
unsigned int faceSize = (params.faceSize == 0) ? std::max(width, height)/4 : params.faceSize;
|
|
||||||
|
|
||||||
// Sans cette vérification, celles des rectangles pourrait réussir via un overflow
|
|
||||||
if (width < faceSize || height < faceSize)
|
|
||||||
{
|
|
||||||
NazaraError("Image is too small for this face size");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calcul et vérification des surfaces
|
return LoadFromImage(cubemap, generateMipmaps);
|
||||||
unsigned limitX = width - faceSize;
|
|
||||||
unsigned limitY = height - faceSize;
|
|
||||||
|
|
||||||
NzVector2ui backPos = params.backPosition * faceSize;
|
|
||||||
if (backPos.x > limitX || backPos.y > limitY)
|
|
||||||
{
|
|
||||||
NazaraError("Back rectangle is out of image");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
NzVector2ui downPos = params.downPosition * faceSize;
|
|
||||||
if (downPos.x > limitX || downPos.y > limitY)
|
|
||||||
{
|
|
||||||
NazaraError("Down rectangle is out of image");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
NzVector2ui forwardPos = params.forwardPosition * faceSize;
|
|
||||||
if (forwardPos.x > limitX || forwardPos.y > limitY)
|
|
||||||
{
|
|
||||||
NazaraError("Forward rectangle is out of image");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
NzVector2ui leftPos = params.leftPosition * faceSize;
|
|
||||||
if (leftPos.x > limitX || leftPos.y > limitY)
|
|
||||||
{
|
|
||||||
NazaraError("Left rectangle is out of image");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
NzVector2ui rightPos = params.rightPosition * faceSize;
|
|
||||||
if (rightPos.x > limitX || rightPos.y > limitY)
|
|
||||||
{
|
|
||||||
NazaraError("Right rectangle is out of image");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
NzVector2ui upPos = params.upPosition * faceSize;
|
|
||||||
if (upPos.x > limitX || upPos.y > limitY)
|
|
||||||
{
|
|
||||||
NazaraError("Up rectangle is out of image");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!Create(nzImageType_Cubemap, image.GetFormat(), faceSize, faceSize, 1, (generateMipmaps) ? 0xFF : 1))
|
|
||||||
{
|
|
||||||
NazaraError("Failed to create texture");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
UpdateFace(nzCubemapFace_NegativeX, image.GetConstPixels(leftPos.x, leftPos.y), width, height);
|
|
||||||
UpdateFace(nzCubemapFace_NegativeY, image.GetConstPixels(downPos.x, downPos.y), width, height);
|
|
||||||
UpdateFace(nzCubemapFace_NegativeZ, image.GetConstPixels(backPos.x, backPos.y), width, height);
|
|
||||||
UpdateFace(nzCubemapFace_PositiveX, image.GetConstPixels(rightPos.x, rightPos.y), width, height);
|
|
||||||
UpdateFace(nzCubemapFace_PositiveY, image.GetConstPixels(upPos.x, upPos.y), width, height);
|
|
||||||
UpdateFace(nzCubemapFace_PositiveZ, image.GetConstPixels(forwardPos.x, forwardPos.y), width, height);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NzTexture::LoadCubemapFromMemory(const void* data, std::size_t size, const NzImageParams& imageParams, bool generateMipmaps, const NzCubemapParams& cubemapParams)
|
bool NzTexture::LoadCubemapFromMemory(const void* data, std::size_t size, const NzImageParams& imageParams, bool generateMipmaps, const NzCubemapParams& cubemapParams)
|
||||||
{
|
{
|
||||||
NzImage image;
|
NzImage cubemap;
|
||||||
if (!image.LoadFromMemory(data, size, imageParams))
|
if (!cubemap.LoadCubemapFromMemory(data, size, imageParams, cubemapParams))
|
||||||
{
|
{
|
||||||
NazaraError("Failed to load image");
|
NazaraError("Failed to load cubemap");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return LoadCubemapFromImage(image, generateMipmaps, cubemapParams);
|
return LoadFromImage(cubemap, generateMipmaps);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NzTexture::LoadCubemapFromStream(NzInputStream& stream, const NzImageParams& imageParams, bool generateMipmaps, const NzCubemapParams& cubemapParams)
|
bool NzTexture::LoadCubemapFromStream(NzInputStream& stream, const NzImageParams& imageParams, bool generateMipmaps, const NzCubemapParams& cubemapParams)
|
||||||
{
|
{
|
||||||
NzImage image;
|
NzImage cubemap;
|
||||||
if (!image.LoadFromStream(stream, imageParams))
|
if (!cubemap.LoadCubemapFromStream(stream, imageParams, cubemapParams))
|
||||||
{
|
{
|
||||||
NazaraError("Failed to load image");
|
NazaraError("Failed to load cubemap");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return LoadCubemapFromImage(image, generateMipmaps, cubemapParams);
|
return LoadFromImage(cubemap, generateMipmaps);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NzTexture::LoadFaceFromFile(nzCubemapFace face, const NzString& filePath, const NzImageParams& params)
|
bool NzTexture::LoadFaceFromFile(nzCubemapFace face, const NzString& filePath, const NzImageParams& params)
|
||||||
|
|
@ -809,7 +737,6 @@ bool NzTexture::LoadFaceFromFile(nzCubemapFace face, const NzString& filePath, c
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int faceSize = m_impl->width;
|
unsigned int faceSize = m_impl->width;
|
||||||
|
|
||||||
if (image.GetWidth() != faceSize || image.GetHeight() != faceSize)
|
if (image.GetWidth() != faceSize || image.GetHeight() != faceSize)
|
||||||
{
|
{
|
||||||
NazaraError("Image size must match texture face size");
|
NazaraError("Image size must match texture face size");
|
||||||
|
|
@ -849,7 +776,6 @@ bool NzTexture::LoadFaceFromMemory(nzCubemapFace face, const void* data, std::si
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int faceSize = m_impl->width;
|
unsigned int faceSize = m_impl->width;
|
||||||
|
|
||||||
if (image.GetWidth() != faceSize || image.GetHeight() != faceSize)
|
if (image.GetWidth() != faceSize || image.GetHeight() != faceSize)
|
||||||
{
|
{
|
||||||
NazaraError("Image size must match texture face size");
|
NazaraError("Image size must match texture face size");
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue