From 904319ee90e6e562535143b648086839b7349e3e Mon Sep 17 00:00:00 2001 From: Lynix Date: Wed, 7 Jan 2015 21:03:29 +0100 Subject: [PATCH] Improved/Fixed Texture class Allowed move constructor/operator Fixed Texture::Download/Texture::Update Made InvalidateMipmaps public Former-commit-id: b8b6a54bc5d6250f640fed6582dc694df6405f73 --- include/Nazara/Renderer/Texture.hpp | 7 +++- src/Nazara/Renderer/Texture.cpp | 59 +++++++++++++++-------------- 2 files changed, 35 insertions(+), 31 deletions(-) diff --git a/include/Nazara/Renderer/Texture.hpp b/include/Nazara/Renderer/Texture.hpp index 52abbe3fc..5c082cc2e 100644 --- a/include/Nazara/Renderer/Texture.hpp +++ b/include/Nazara/Renderer/Texture.hpp @@ -26,11 +26,12 @@ struct NzTextureImpl; class NAZARA_API NzTexture : public NzAbstractImage, public NzResource, NzNonCopyable { friend class NzRenderer; - friend class NzRenderTexture; public: NzTexture() = default; + NzTexture(nzImageType type, nzPixelFormat format, unsigned int width, unsigned int height, unsigned int depth = 1, nzUInt8 levelCount = 1); explicit NzTexture(const NzImage& image); + NzTexture(NzTexture&& texture); ~NzTexture(); bool Create(nzImageType type, nzPixelFormat format, unsigned int width, unsigned int height, unsigned int depth = 1, nzUInt8 levelCount = 1); @@ -55,6 +56,7 @@ class NAZARA_API NzTexture : public NzAbstractImage, public NzResource, NzNonCop bool HasMipmaps() const; + void InvalidateMipmaps(); bool IsValid() const; // Load @@ -92,6 +94,8 @@ class NAZARA_API NzTexture : public NzAbstractImage, public NzResource, NzNonCop // Fonctions OpenGL unsigned int GetOpenGLID() const; + NzTexture& operator=(NzTexture&& texture); + static unsigned int GetValidSize(unsigned int size); static bool IsFormatSupported(nzPixelFormat format); static bool IsMipmappingSupported(); @@ -99,7 +103,6 @@ class NAZARA_API NzTexture : public NzAbstractImage, public NzResource, NzNonCop private: bool CreateTexture(bool proxy); - void InvalidateMipmaps(); NzTextureImpl* m_impl = nullptr; }; diff --git a/src/Nazara/Renderer/Texture.cpp b/src/Nazara/Renderer/Texture.cpp index 855a70e2f..6861a8e69 100644 --- a/src/Nazara/Renderer/Texture.cpp +++ b/src/Nazara/Renderer/Texture.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -47,17 +48,22 @@ namespace } } +NzTexture::NzTexture(nzImageType type, nzPixelFormat format, unsigned int width, unsigned int height, unsigned int depth, nzUInt8 levelCount) +{ + NzErrorFlags flags(nzErrorFlag_ThrowException); + Create(type, format, width, height, depth, levelCount); +} + NzTexture::NzTexture(const NzImage& image) { + NzErrorFlags flags(nzErrorFlag_ThrowException); LoadFromImage(image); +} - #ifdef NAZARA_DEBUG - if (!m_impl) - { - NazaraError("Failed to create texture"); - throw std::runtime_error("Constructor failed"); - } - #endif +NzTexture::NzTexture(NzTexture&& texture) : +m_impl(texture.m_impl) +{ + texture.m_impl = nullptr; } NzTexture::~NzTexture() @@ -248,30 +254,11 @@ bool NzTexture::Download(NzImage* image) const return false; } - unsigned int width = m_impl->width; - unsigned int height = m_impl->height; - unsigned int depth = m_impl->depth; - // Téléchargement... NzOpenGL::BindTexture(m_impl->type, m_impl->id); for (nzUInt8 level = 0; level < m_impl->levelCount; ++level) - { glGetTexImage(NzOpenGL::TextureTarget[m_impl->type], level, format.dataFormat, format.dataType, image->GetPixels(level)); - if (width > 1) - width >>= 1; - - if (height > 1) - height >>= 1; - - if (depth > 1) - depth >>= 1; - } - - // Inversion de la texture pour le repère d'OpenGL - if (!image->FlipVertically()) - NazaraWarning("Failed to flip image"); - return true; } @@ -292,7 +279,11 @@ bool NzTexture::EnableMipmapping(bool enable) } if (m_impl->levelCount == 1) // Transformation d'une texture sans mipmaps vers une texture avec mipmaps + { + ///FIXME: Est-ce que cette opération est seulement possible ? m_impl->levelCount = NzImage::GetMaxLevel(m_impl->width, m_impl->height, m_impl->depth); + SetMipmapRange(0, m_impl->levelCount-1); + } if (!m_impl->mipmapping && enable) m_impl->mipmapsUpdated = false; @@ -1006,16 +997,16 @@ bool NzTexture::Update(const nzUInt8* pixels, const NzBoxui& box, unsigned int s case nzImageType_1D_Array: case nzImageType_2D: - glTexSubImage2D(NzOpenGL::TextureTarget[m_impl->type], level, box.x, height-box.height-box.y, box.width, box.height, format.dataFormat, format.dataType, pixels); + glTexSubImage2D(NzOpenGL::TextureTarget[m_impl->type], level, box.x, box.y, box.width, box.height, format.dataFormat, format.dataType, pixels); break; case nzImageType_2D_Array: case nzImageType_3D: - glTexSubImage3D(NzOpenGL::TextureTarget[m_impl->type], level, box.x, height-box.height-box.y, box.z, box.width, box.height, box.depth, format.dataFormat, format.dataType, pixels); + glTexSubImage3D(NzOpenGL::TextureTarget[m_impl->type], level, box.x, box.y, box.z, box.width, box.height, box.depth, format.dataFormat, format.dataType, pixels); break; case nzImageType_Cubemap: - glTexSubImage2D(NzOpenGL::CubemapFace[box.z], level, box.x, height-box.height-box.y, box.width, box.height, format.dataFormat, format.dataType, pixels); + glTexSubImage2D(NzOpenGL::CubemapFace[box.z], level, box.x, box.y, box.width, box.height, format.dataFormat, format.dataType, pixels); break; } @@ -1040,6 +1031,16 @@ unsigned int NzTexture::GetOpenGLID() const return m_impl->id; } +NzTexture& NzTexture::operator=(NzTexture&& texture) +{ + Destroy(); + + m_impl = texture.m_impl; + texture.m_impl = nullptr; + + return *this; +} + unsigned int NzTexture::GetValidSize(unsigned int size) { if (NzRenderer::HasCapability(nzRendererCap_TextureNPOT))