From d49ee5c76ef784c9af30e98b4ecec32895f4c119 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 5 Oct 2012 16:20:01 +0200 Subject: [PATCH] Using specialized Vector type in images and textures. Thanks to DimensionTraits it is now possible to e.g. conveniently access components by name or pass size as combination of vector and scalar: GLsizei width = image.size().x(); image.setData({xy, 1}, ...); Instead of previous inconvenient ways: GLsizei width = image.size()[0]; Math::Vector2 size(xy, 1); image.setData(size, ...); Not using the specialized type for internal functions and storage, as it wouldn't cause any other improvements. This way it is virtually possible to forward-declare the specialized types without including them in the headers. --- src/BufferedImage.cpp | 2 +- src/BufferedImage.h | 9 +++++---- src/CubeMapTexture.h | 2 +- src/Image.cpp | 2 +- src/Image.h | 13 +++++++------ src/ImageWrapper.h | 11 ++++++----- src/Texture.h | 3 ++- src/Trade/ImageData.h | 9 +++++---- 8 files changed, 28 insertions(+), 23 deletions(-) diff --git a/src/BufferedImage.cpp b/src/BufferedImage.cpp index e40a9ab3f..43c43227b 100644 --- a/src/BufferedImage.cpp +++ b/src/BufferedImage.cpp @@ -17,7 +17,7 @@ namespace Magnum { -template void BufferedImage::setData(const Math::Vector& size, Components components, ComponentType type, const GLvoid* data, Buffer::Usage usage) { +template void BufferedImage::setData(const typename DimensionTraits::VectorType& size, Components components, ComponentType type, const GLvoid* data, Buffer::Usage usage) { _components = components; _type = type; _size = size; diff --git a/src/BufferedImage.h b/src/BufferedImage.h index 7cd0eedab..99062b105 100644 --- a/src/BufferedImage.h +++ b/src/BufferedImage.h @@ -19,9 +19,10 @@ * @brief Class Magnum::BufferedImage, typedef Magnum::BufferedImage1D, Magnum::BufferedImage2D, Magnum::BufferedImage3D */ -#include "Math/Vector.h" +#include "Math/Vector3.h" #include "AbstractImage.h" #include "Buffer.h" +#include "DimensionTraits.h" #include "TypeTraits.h" namespace Magnum { @@ -50,7 +51,7 @@ template class BufferedImage: public AbstractImage { inline BufferedImage(Components components, ComponentType type): AbstractImage(components, type), _buffer(Buffer::Target::PixelPack) {} /** @brief %Image size */ - inline constexpr Math::Vector size() const { return _size; } + inline constexpr typename DimensionTraits::VectorType size() const { return _size; } /** * @brief Data @@ -82,7 +83,7 @@ template class BufferedImage: public AbstractImage { * * @see setData(const Math::Vector&, Components, ComponentType, const GLvoid*, Buffer::Usage) */ - template inline void setData(const Math::Vector& size, Components components, const T* data, Buffer::Usage usage) { + template inline void setData(const typename DimensionTraits::VectorType& size, Components components, const T* data, Buffer::Usage usage) { setData(size, components, TypeTraits::imageType(), data, usage); } @@ -99,7 +100,7 @@ template class BufferedImage: public AbstractImage { * * @see Buffer::setData() */ - void setData(const Math::Vector& size, Components components, ComponentType type, const GLvoid* data, Buffer::Usage usage); + void setData(const typename DimensionTraits::VectorType& size, Components components, ComponentType type, const GLvoid* data, Buffer::Usage usage); protected: Math::Vector _size; /**< @brief %Image size */ diff --git a/src/CubeMapTexture.h b/src/CubeMapTexture.h index edf64830a..a537eaec8 100644 --- a/src/CubeMapTexture.h +++ b/src/CubeMapTexture.h @@ -103,7 +103,7 @@ class CubeMapTexture: public AbstractTexture { } /** - * @copydoc Texture::setSubData(GLint, const Math::Vector&, Image*) + * @copydoc Texture::setSubData(GLint, const typename DimensionTraits::VectorType&, Image*) * @param coordinate Coordinate * @return Pointer to self (for method chaining) */ diff --git a/src/Image.cpp b/src/Image.cpp index f12b2caa7..a5fa6c4d1 100644 --- a/src/Image.cpp +++ b/src/Image.cpp @@ -17,7 +17,7 @@ namespace Magnum { -template void Image::setData(const Math::Vector& size, Components components, ComponentType type, GLvoid* data) { +template void Image::setData(const typename DimensionTraits::VectorType& size, Components components, ComponentType type, GLvoid* data) { delete[] _data; _components = components; _type = type; diff --git a/src/Image.h b/src/Image.h index aa2b6fcf8..83a1187a2 100644 --- a/src/Image.h +++ b/src/Image.h @@ -19,8 +19,9 @@ * @brief Class Magnum::Image, typedef Magnum::Image1D, Magnum::Image2D, Magnum::Image3D */ -#include "Math/Vector.h" +#include "Math/Vector3.h" #include "AbstractImage.h" +#include "DimensionTraits.h" #include "TypeTraits.h" namespace Magnum { @@ -47,7 +48,7 @@ template class Image: public AbstractImage { * Note that the image data are not copied on construction, but they * are deleted on class destruction. */ - template inline Image(const Math::Vector& size, Components components, T* data): AbstractImage(components, TypeTraits::imageType()), _size(size), _data(data) {} + template inline Image(const typename DimensionTraits::VectorType& size, Components components, T* data): AbstractImage(components, TypeTraits::imageType()), _size(size), _data(data) {} /** * @brief Constructor @@ -59,7 +60,7 @@ template class Image: public AbstractImage { * Note that the image data are not copied on construction, but they * are deleted on class destruction. */ - inline Image(const Math::Vector& size, Components components, ComponentType type, GLvoid* data): AbstractImage(components, type), _size(size), _data(reinterpret_cast(data)) {} + inline Image(const typename DimensionTraits::VectorType& size, Components components, ComponentType type, GLvoid* data): AbstractImage(components, type), _size(size), _data(reinterpret_cast(data)) {} /** * @brief Constructor @@ -75,7 +76,7 @@ template class Image: public AbstractImage { inline ~Image() { delete[] _data; } /** @brief %Image size */ - inline constexpr const Math::Vector& size() const { return _size; } + inline constexpr typename DimensionTraits::VectorType size() const { return _size; } /** @brief Pointer to raw data */ inline void* data() { return _data; } @@ -91,7 +92,7 @@ template class Image: public AbstractImage { * Deletes previous data and replaces them with new. Note that the * data are not copied, but they are deleted on destruction. */ - template inline void setData(const Math::Vector& size, Components components, T* data) { + template inline void setData(const typename DimensionTraits::VectorType& size, Components components, T* data) { setData(size, components, TypeTraits::imageType(), data); } @@ -105,7 +106,7 @@ template class Image: public AbstractImage { * Deletes previous data and replaces them with new. Note that the * data are not copied, but they are deleted on destruction. */ - void setData(const Math::Vector& size, Components components, ComponentType type, GLvoid* data); + void setData(const typename DimensionTraits::VectorType& size, Components components, ComponentType type, GLvoid* data); protected: Math::Vector _size; /**< @brief %Image size */ diff --git a/src/ImageWrapper.h b/src/ImageWrapper.h index e1237e329..6699d680e 100644 --- a/src/ImageWrapper.h +++ b/src/ImageWrapper.h @@ -19,8 +19,9 @@ * @brief Class Magnum::ImageWrapper */ -#include "Math/Vector.h" +#include "Math/Vector3.h" #include "AbstractImage.h" +#include "DimensionTraits.h" #include "TypeTraits.h" namespace Magnum { @@ -53,7 +54,7 @@ template class ImageWrapper: public AbstractImage { * Note that the image data are not copied on construction, but they * are deleted on class destruction. */ - template inline ImageWrapper(const Math::Vector& size, Components components, T* data): AbstractImage(components, TypeTraits::imageType()), _size(size), _data(data) {} + template inline ImageWrapper(const typename DimensionTraits::VectorType& size, Components components, T* data): AbstractImage(components, TypeTraits::imageType()), _size(size), _data(data) {} /** * @brief Constructor @@ -65,7 +66,7 @@ template class ImageWrapper: public AbstractImage { * Note that the image data are not copied on construction, but they * are deleted on class destruction. */ - inline ImageWrapper(const Math::Vector& size, Components components, ComponentType type, GLvoid* data): AbstractImage(components, type), _size(size), _data(reinterpret_cast(data)) {} + inline ImageWrapper(const typename DimensionTraits::VectorType& size, Components components, ComponentType type, GLvoid* data): AbstractImage(components, type), _size(size), _data(reinterpret_cast(data)) {} /** * @brief Constructor @@ -76,10 +77,10 @@ template class ImageWrapper: public AbstractImage { * Dimensions and data pointer are set to zero, call setData() to fill * the image with data. */ - inline ImageWrapper(const Math::Vector& size, Components components, ComponentType type): AbstractImage(components, type), _size(size), _data(nullptr) {} + inline ImageWrapper(const typename DimensionTraits::VectorType& size, Components components, ComponentType type): AbstractImage(components, type), _size(size), _data(nullptr) {} /** @brief %Image size */ - inline constexpr const Math::Vector& size() const { return _size; } + inline constexpr typename DimensionTraits::VectorType size() const { return _size; } /** @brief Pointer to raw data */ inline void* data() { return _data; } diff --git a/src/Texture.h b/src/Texture.h index 015f18cdb..01560b586 100644 --- a/src/Texture.h +++ b/src/Texture.h @@ -20,6 +20,7 @@ */ #include "AbstractTexture.h" +#include "DimensionTraits.h" namespace Magnum { @@ -167,7 +168,7 @@ template class Texture: public AbstractTexture { * 1D texture array (which is two-dimensional) with 1D images. * @see bind(), @fn_gl{TexSubImage1D}, @fn_gl{TexSubImage2D}, @fn_gl{TexSubImage3D} */ - template inline Texture* setSubData(GLint mipLevel, const Math::Vector& offset, Image* image) { + template inline Texture* setSubData(GLint mipLevel, const typename DimensionTraits::VectorType& offset, Image* image) { bind(); DataHelper::setSub(_target, mipLevel, offset, image); return this; diff --git a/src/Trade/ImageData.h b/src/Trade/ImageData.h index 260fa8d8a..bde311640 100644 --- a/src/Trade/ImageData.h +++ b/src/Trade/ImageData.h @@ -19,8 +19,9 @@ * @brief Class Magnum::Trade::ImageData */ -#include "Math/Vector.h" +#include "Math/Vector3.h" #include "AbstractImage.h" +#include "DimensionTraits.h" #include "TypeTraits.h" namespace Magnum { namespace Trade { @@ -46,7 +47,7 @@ template class ImageData: public AbstractImage { * Note that the image data are not copied on construction, but they * are deleted on class destruction. */ - template inline ImageData(const std::string& name, const Math::Vector& size, Components components, T* data): AbstractImage(components, TypeTraits::imageType()), _name(name), _size(size), _data(reinterpret_cast(data)) {} + template inline ImageData(const std::string& name, const typename DimensionTraits::VectorType& size, Components components, T* data): AbstractImage(components, TypeTraits::imageType()), _name(name), _size(size), _data(reinterpret_cast(data)) {} /** * @brief Constructor @@ -59,7 +60,7 @@ template class ImageData: public AbstractImage { * Note that the image data are not copied on construction, but they * are deleted on class destruction. */ - inline ImageData(const std::string& name, const Math::Vector& size, Components components, ComponentType type, GLvoid* data): AbstractImage(components, type), _name(name), _size(size), _data(reinterpret_cast(data)) {} + inline ImageData(const std::string& name, const typename DimensionTraits::VectorType& size, Components components, ComponentType type, GLvoid* data): AbstractImage(components, type), _name(name), _size(size), _data(reinterpret_cast(data)) {} /** @brief Destructor */ inline ~ImageData() { delete[] _data; } @@ -68,7 +69,7 @@ template class ImageData: public AbstractImage { inline std::string name() const { return _name; } /** @brief %Image size */ - inline constexpr const Math::Vector& size() const { return _size; } + inline constexpr typename DimensionTraits::VectorType& size() const { return _size; } /** @brief Pointer to raw data */ inline void* data() { return _data; }