Browse Source

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<GLsizei> 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.
vectorfields
Vladimír Vondruš 14 years ago
parent
commit
d49ee5c76e
  1. 2
      src/BufferedImage.cpp
  2. 9
      src/BufferedImage.h
  3. 2
      src/CubeMapTexture.h
  4. 2
      src/Image.cpp
  5. 13
      src/Image.h
  6. 11
      src/ImageWrapper.h
  7. 3
      src/Texture.h
  8. 9
      src/Trade/ImageData.h

2
src/BufferedImage.cpp

@ -17,7 +17,7 @@
namespace Magnum {
template<size_t dimensions> void BufferedImage<dimensions>::setData(const Math::Vector<Dimensions, GLsizei>& size, Components components, ComponentType type, const GLvoid* data, Buffer::Usage usage) {
template<size_t dimensions> void BufferedImage<dimensions>::setData(const typename DimensionTraits<Dimensions, GLsizei>::VectorType& size, Components components, ComponentType type, const GLvoid* data, Buffer::Usage usage) {
_components = components;
_type = type;
_size = size;

9
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<size_t dimensions> class BufferedImage: public AbstractImage {
inline BufferedImage(Components components, ComponentType type): AbstractImage(components, type), _buffer(Buffer::Target::PixelPack) {}
/** @brief %Image size */
inline constexpr Math::Vector<Dimensions, GLsizei> size() const { return _size; }
inline constexpr typename DimensionTraits<Dimensions, GLsizei>::VectorType size() const { return _size; }
/**
* @brief Data
@ -82,7 +83,7 @@ template<size_t dimensions> class BufferedImage: public AbstractImage {
*
* @see setData(const Math::Vector<Dimensions, GLsizei>&, Components, ComponentType, const GLvoid*, Buffer::Usage)
*/
template<class T> inline void setData(const Math::Vector<Dimensions, GLsizei>& size, Components components, const T* data, Buffer::Usage usage) {
template<class T> inline void setData(const typename DimensionTraits<Dimensions, GLsizei>::VectorType& size, Components components, const T* data, Buffer::Usage usage) {
setData(size, components, TypeTraits<T>::imageType(), data, usage);
}
@ -99,7 +100,7 @@ template<size_t dimensions> class BufferedImage: public AbstractImage {
*
* @see Buffer::setData()
*/
void setData(const Math::Vector<Dimensions, GLsizei>& size, Components components, ComponentType type, const GLvoid* data, Buffer::Usage usage);
void setData(const typename DimensionTraits<Dimensions, GLsizei>::VectorType& size, Components components, ComponentType type, const GLvoid* data, Buffer::Usage usage);
protected:
Math::Vector<Dimensions, GLsizei> _size; /**< @brief %Image size */

2
src/CubeMapTexture.h

@ -103,7 +103,7 @@ class CubeMapTexture: public AbstractTexture {
}
/**
* @copydoc Texture::setSubData(GLint, const Math::Vector<Dimensions, GLint>&, Image*)
* @copydoc Texture::setSubData(GLint, const typename DimensionTraits<Dimensions, GLint>::VectorType&, Image*)
* @param coordinate Coordinate
* @return Pointer to self (for method chaining)
*/

2
src/Image.cpp

@ -17,7 +17,7 @@
namespace Magnum {
template<size_t dimensions> void Image<dimensions>::setData(const Math::Vector<Dimensions, GLsizei>& size, Components components, ComponentType type, GLvoid* data) {
template<size_t dimensions> void Image<dimensions>::setData(const typename DimensionTraits<Dimensions, GLsizei>::VectorType& size, Components components, ComponentType type, GLvoid* data) {
delete[] _data;
_components = components;
_type = type;

13
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<size_t dimensions> class Image: public AbstractImage {
* Note that the image data are not copied on construction, but they
* are deleted on class destruction.
*/
template<class T> inline Image(const Math::Vector<Dimensions, GLsizei>& size, Components components, T* data): AbstractImage(components, TypeTraits<T>::imageType()), _size(size), _data(data) {}
template<class T> inline Image(const typename DimensionTraits<Dimensions, GLsizei>::VectorType& size, Components components, T* data): AbstractImage(components, TypeTraits<T>::imageType()), _size(size), _data(data) {}
/**
* @brief Constructor
@ -59,7 +60,7 @@ template<size_t dimensions> 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<Dimensions, GLsizei>& size, Components components, ComponentType type, GLvoid* data): AbstractImage(components, type), _size(size), _data(reinterpret_cast<char*>(data)) {}
inline Image(const typename DimensionTraits<Dimensions, GLsizei>::VectorType& size, Components components, ComponentType type, GLvoid* data): AbstractImage(components, type), _size(size), _data(reinterpret_cast<char*>(data)) {}
/**
* @brief Constructor
@ -75,7 +76,7 @@ template<size_t dimensions> class Image: public AbstractImage {
inline ~Image() { delete[] _data; }
/** @brief %Image size */
inline constexpr const Math::Vector<Dimensions, GLsizei>& size() const { return _size; }
inline constexpr typename DimensionTraits<Dimensions, GLsizei>::VectorType size() const { return _size; }
/** @brief Pointer to raw data */
inline void* data() { return _data; }
@ -91,7 +92,7 @@ template<size_t dimensions> 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<class T> inline void setData(const Math::Vector<Dimensions, GLsizei>& size, Components components, T* data) {
template<class T> inline void setData(const typename DimensionTraits<Dimensions, GLsizei>::VectorType& size, Components components, T* data) {
setData(size, components, TypeTraits<T>::imageType(), data);
}
@ -105,7 +106,7 @@ template<size_t dimensions> 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<Dimensions, GLsizei>& size, Components components, ComponentType type, GLvoid* data);
void setData(const typename DimensionTraits<Dimensions, GLsizei>::VectorType& size, Components components, ComponentType type, GLvoid* data);
protected:
Math::Vector<Dimensions, GLsizei> _size; /**< @brief %Image size */

11
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<size_t dimensions> class ImageWrapper: public AbstractImage {
* Note that the image data are not copied on construction, but they
* are deleted on class destruction.
*/
template<class T> inline ImageWrapper(const Math::Vector<Dimensions, GLsizei>& size, Components components, T* data): AbstractImage(components, TypeTraits<T>::imageType()), _size(size), _data(data) {}
template<class T> inline ImageWrapper(const typename DimensionTraits<Dimensions, GLsizei>::VectorType& size, Components components, T* data): AbstractImage(components, TypeTraits<T>::imageType()), _size(size), _data(data) {}
/**
* @brief Constructor
@ -65,7 +66,7 @@ template<size_t dimensions> 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<Dimensions, GLsizei>& size, Components components, ComponentType type, GLvoid* data): AbstractImage(components, type), _size(size), _data(reinterpret_cast<char*>(data)) {}
inline ImageWrapper(const typename DimensionTraits<Dimensions, GLsizei>::VectorType& size, Components components, ComponentType type, GLvoid* data): AbstractImage(components, type), _size(size), _data(reinterpret_cast<char*>(data)) {}
/**
* @brief Constructor
@ -76,10 +77,10 @@ template<size_t dimensions> 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<Dimensions, GLsizei>& size, Components components, ComponentType type): AbstractImage(components, type), _size(size), _data(nullptr) {}
inline ImageWrapper(const typename DimensionTraits<Dimensions, GLsizei>::VectorType& size, Components components, ComponentType type): AbstractImage(components, type), _size(size), _data(nullptr) {}
/** @brief %Image size */
inline constexpr const Math::Vector<Dimensions, GLsizei>& size() const { return _size; }
inline constexpr typename DimensionTraits<Dimensions, GLsizei>::VectorType size() const { return _size; }
/** @brief Pointer to raw data */
inline void* data() { return _data; }

3
src/Texture.h

@ -20,6 +20,7 @@
*/
#include "AbstractTexture.h"
#include "DimensionTraits.h"
namespace Magnum {
@ -167,7 +168,7 @@ template<size_t dimensions> 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<class Image> inline Texture<Dimensions>* setSubData(GLint mipLevel, const Math::Vector<Dimensions, GLint>& offset, Image* image) {
template<class Image> inline Texture<Dimensions>* setSubData(GLint mipLevel, const typename DimensionTraits<Dimensions, GLint>::VectorType& offset, Image* image) {
bind();
DataHelper<Dimensions>::setSub(_target, mipLevel, offset, image);
return this;

9
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<size_t dimensions> class ImageData: public AbstractImage {
* Note that the image data are not copied on construction, but they
* are deleted on class destruction.
*/
template<class T> inline ImageData(const std::string& name, const Math::Vector<Dimensions, GLsizei>& size, Components components, T* data): AbstractImage(components, TypeTraits<T>::imageType()), _name(name), _size(size), _data(reinterpret_cast<char*>(data)) {}
template<class T> inline ImageData(const std::string& name, const typename DimensionTraits<Dimensions, GLsizei>::VectorType& size, Components components, T* data): AbstractImage(components, TypeTraits<T>::imageType()), _name(name), _size(size), _data(reinterpret_cast<char*>(data)) {}
/**
* @brief Constructor
@ -59,7 +60,7 @@ template<size_t dimensions> 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<Dimensions, GLsizei>& size, Components components, ComponentType type, GLvoid* data): AbstractImage(components, type), _name(name), _size(size), _data(reinterpret_cast<char*>(data)) {}
inline ImageData(const std::string& name, const typename DimensionTraits<Dimensions, GLsizei>::VectorType& size, Components components, ComponentType type, GLvoid* data): AbstractImage(components, type), _name(name), _size(size), _data(reinterpret_cast<char*>(data)) {}
/** @brief Destructor */
inline ~ImageData() { delete[] _data; }
@ -68,7 +69,7 @@ template<size_t dimensions> class ImageData: public AbstractImage {
inline std::string name() const { return _name; }
/** @brief %Image size */
inline constexpr const Math::Vector<Dimensions, GLsizei>& size() const { return _size; }
inline constexpr typename DimensionTraits<Dimensions, GLsizei>::VectorType& size() const { return _size; }
/** @brief Pointer to raw data */
inline void* data() { return _data; }

Loading…
Cancel
Save