Browse Source

Using strongly-typed enum for plain OpenGL types.

Mesh and IndexedMesh is not completely ported, as it will need more
work.
vectorfields
Vladimír Vondruš 15 years ago
parent
commit
52971935ab
  1. 28
      src/AbstractTexture.h
  2. 4
      src/CubeMapTexture.h
  3. 2
      src/IndexedMesh.cpp
  4. 13
      src/IndexedMesh.h
  5. 2
      src/Mesh.h
  6. 2
      src/MeshBuilder.h
  7. 4
      src/Trade/Image.h
  8. 38
      src/TypeTraits.h

28
src/AbstractTexture.h

@ -228,7 +228,7 @@ class MAGNUM_EXPORT AbstractTexture {
* Calls @c glTexImage1D, @c glTexImage2D, @c glTexImage3D depending * Calls @c glTexImage1D, @c glTexImage2D, @c glTexImage3D depending
* on dimension count. * on dimension count.
*/ */
inline static void set(GLenum target, GLint mipLevel, InternalFormat internalFormat, const Math::Vector<GLsizei, textureDimensions>& dimensions, ColorFormat colorFormat, GLenum type, const void* data); inline static void set(GLenum target, GLint mipLevel, InternalFormat internalFormat, const Math::Vector<GLsizei, textureDimensions>& dimensions, ColorFormat colorFormat, Type type, const void* data);
/** /**
* @brief Set texture subdata * @brief Set texture subdata
@ -243,7 +243,7 @@ class MAGNUM_EXPORT AbstractTexture {
* Calls @c glTexSubImage1D, @c glTexSubImage2D, @c glTexSubImage3D * Calls @c glTexSubImage1D, @c glTexSubImage2D, @c glTexSubImage3D
* depending on dimension count. * depending on dimension count.
*/ */
inline static void setSub(GLenum target, GLint mipLevel, const Math::Vector<GLint, textureDimensions>& offset, const Math::Vector<GLsizei, textureDimensions>& dimensions, ColorFormat colorFormat, GLenum type, const void* data); inline static void setSub(GLenum target, GLint mipLevel, const Math::Vector<GLint, textureDimensions>& offset, const Math::Vector<GLsizei, textureDimensions>& dimensions, ColorFormat colorFormat, Type type, const void* data);
#endif #endif
}; };
@ -256,34 +256,34 @@ class MAGNUM_EXPORT AbstractTexture {
template<> struct AbstractTexture::DataHelper<1> { template<> struct AbstractTexture::DataHelper<1> {
inline constexpr static GLenum target() { return GL_TEXTURE_1D; } inline constexpr static GLenum target() { return GL_TEXTURE_1D; }
inline static void set(GLenum target, GLint mipLevel, InternalFormat internalFormat, const Math::Vector<GLsizei, 1>& dimensions, ColorFormat colorFormat, GLenum type, const void* data) { inline static void set(GLenum target, GLint mipLevel, InternalFormat internalFormat, const Math::Vector<GLsizei, 1>& dimensions, ColorFormat colorFormat, Type type, const void* data) {
glTexImage1D(target, mipLevel, static_cast<GLint>(internalFormat), dimensions.at(0), 0, static_cast<GLenum>(colorFormat), type, data); glTexImage1D(target, mipLevel, static_cast<GLint>(internalFormat), dimensions.at(0), 0, static_cast<GLenum>(colorFormat), static_cast<GLenum>(type), data);
} }
inline static void setSub(GLenum target, GLint mipLevel, const Math::Vector<GLint, 1>& offset, const Math::Vector<GLsizei, 1>& dimensions, ColorFormat colorFormat, GLenum type, const void* data) { inline static void setSub(GLenum target, GLint mipLevel, const Math::Vector<GLint, 1>& offset, const Math::Vector<GLsizei, 1>& dimensions, ColorFormat colorFormat, Type type, const void* data) {
glTexSubImage1D(target, mipLevel, offset.at(0), dimensions.at(0), static_cast<GLenum>(colorFormat), type, data); glTexSubImage1D(target, mipLevel, offset.at(0), dimensions.at(0), static_cast<GLenum>(colorFormat), static_cast<GLenum>(type), data);
} }
}; };
template<> struct AbstractTexture::DataHelper<2> { template<> struct AbstractTexture::DataHelper<2> {
inline constexpr static GLenum target() { return GL_TEXTURE_2D; } inline constexpr static GLenum target() { return GL_TEXTURE_2D; }
inline static void set(GLenum target, GLint mipLevel, InternalFormat internalFormat, const Math::Vector<GLsizei, 2>& dimensions, ColorFormat colorFormat, GLenum type, const void* data) { inline static void set(GLenum target, GLint mipLevel, InternalFormat internalFormat, const Math::Vector<GLsizei, 2>& dimensions, ColorFormat colorFormat, Type type, const void* data) {
glTexImage2D(target, mipLevel, static_cast<GLint>(internalFormat), dimensions.at(0), dimensions.at(1), 0, static_cast<GLenum>(colorFormat), type, data); glTexImage2D(target, mipLevel, static_cast<GLint>(internalFormat), dimensions.at(0), dimensions.at(1), 0, static_cast<GLenum>(colorFormat), static_cast<GLenum>(type), data);
} }
inline static void setSub(GLenum target, GLint mipLevel, const Math::Vector<GLint, 2>& offset, const Math::Vector<GLsizei, 2>& dimensions, ColorFormat colorFormat, GLenum type, const void* data) { inline static void setSub(GLenum target, GLint mipLevel, const Math::Vector<GLint, 2>& offset, const Math::Vector<GLsizei, 2>& dimensions, ColorFormat colorFormat, Type type, const void* data) {
glTexSubImage2D(target, mipLevel, offset.at(0), offset.at(1), dimensions.at(0), dimensions.at(1), static_cast<GLenum>(colorFormat), type, data); glTexSubImage2D(target, mipLevel, offset.at(0), offset.at(1), dimensions.at(0), dimensions.at(1), static_cast<GLenum>(colorFormat), static_cast<GLenum>(type), data);
} }
}; };
template<> struct AbstractTexture::DataHelper<3> { template<> struct AbstractTexture::DataHelper<3> {
inline constexpr static GLenum target() { return GL_TEXTURE_3D; } inline constexpr static GLenum target() { return GL_TEXTURE_3D; }
inline static void set(GLenum target, GLint mipLevel, InternalFormat internalFormat, const Math::Vector<GLsizei, 3>& dimensions, ColorFormat colorFormat, GLenum type, const void* data) { inline static void set(GLenum target, GLint mipLevel, InternalFormat internalFormat, const Math::Vector<GLsizei, 3>& dimensions, ColorFormat colorFormat, Type type, const void* data) {
glTexImage3D(target, mipLevel, static_cast<GLint>(internalFormat), dimensions.at(0), dimensions.at(1), dimensions.at(2), 0, static_cast<GLenum>(colorFormat), type, data); glTexImage3D(target, mipLevel, static_cast<GLint>(internalFormat), dimensions.at(0), dimensions.at(1), dimensions.at(2), 0, static_cast<GLenum>(colorFormat), static_cast<GLenum>(type), data);
} }
inline static void setSub(GLenum target, GLint mipLevel, const Math::Vector<GLint, 3>& offset, const Math::Vector<GLsizei, 3>& dimensions, ColorFormat colorFormat, GLenum type, const void* data) { inline static void setSub(GLenum target, GLint mipLevel, const Math::Vector<GLint, 3>& offset, const Math::Vector<GLsizei, 3>& dimensions, ColorFormat colorFormat, Type type, const void* data) {
glTexSubImage3D(target, mipLevel, offset.at(0), offset.at(1), offset.at(2), dimensions.at(0), dimensions.at(1), dimensions.at(2), static_cast<GLenum>(colorFormat), type, data); glTexSubImage3D(target, mipLevel, offset.at(0), offset.at(1), offset.at(2), dimensions.at(0), dimensions.at(1), dimensions.at(2), static_cast<GLenum>(colorFormat), static_cast<GLenum>(type), data);
} }
}; };
#endif #endif

4
src/CubeMapTexture.h

@ -109,12 +109,12 @@ class CubeMapTexture: public Texture2D {
} }
private: private:
void setData(GLenum target, GLint mipLevel, InternalFormat internalFormat, const Math::Vector<GLsizei, Dimensions>& _dimensions, ColorFormat colorFormat, GLenum type, const void* data) { void setData(GLenum target, GLint mipLevel, InternalFormat internalFormat, const Math::Vector<GLsizei, Dimensions>& _dimensions, ColorFormat colorFormat, Type type, const void* data) {
bind(); bind();
DataHelper<Dimensions>::set(target, mipLevel, internalFormat, _dimensions, colorFormat, type, data); DataHelper<Dimensions>::set(target, mipLevel, internalFormat, _dimensions, colorFormat, type, data);
} }
void setSubData(GLenum target, GLint mipLevel, const Math::Vector<GLint, Dimensions>& offset, const Math::Vector<GLsizei, Dimensions>& dimensions, ColorFormat colorFormat, GLenum type, const void* data) { void setSubData(GLenum target, GLint mipLevel, const Math::Vector<GLint, Dimensions>& offset, const Math::Vector<GLsizei, Dimensions>& dimensions, ColorFormat colorFormat, Type type, const void* data) {
bind(); bind();
DataHelper<Dimensions>::setSub(target, mipLevel, offset, dimensions, colorFormat, type, data); DataHelper<Dimensions>::setSub(target, mipLevel, offset, dimensions, colorFormat, type, data);
} }

2
src/IndexedMesh.cpp

@ -50,7 +50,7 @@ void IndexedMesh::draw() {
/* Bind index array, draw the elements and unbind */ /* Bind index array, draw the elements and unbind */
_indexBuffer.bind(); _indexBuffer.bind();
glDrawElements(primitive(), _indexCount, _indexType, nullptr); glDrawElements(primitive(), _indexCount, static_cast<GLenum>(_indexType), nullptr);
/* Disable vertex arrays for all attributes */ /* Disable vertex arrays for all attributes */
for(set<GLuint>::const_iterator it = attributes().begin(); it != attributes().end(); ++it) for(set<GLuint>::const_iterator it = attributes().begin(); it != attributes().end(); ++it)

13
src/IndexedMesh.h

@ -37,17 +37,16 @@ class MAGNUM_EXPORT IndexedMesh: public Mesh {
* setIndexCount() and setIndexType() manually for mesh to draw * setIndexCount() and setIndexType() manually for mesh to draw
* properly. * properly.
*/ */
inline IndexedMesh(): _indexBuffer(Buffer::ElementArrayBuffer), _indexCount(0), _indexType(GL_UNSIGNED_SHORT) {} inline IndexedMesh(): _indexBuffer(Buffer::ElementArrayBuffer), _indexCount(0), _indexType(Type::UnsignedShort) {}
/** /**
* @brief Constructor * @brief Constructor
* @param primitive Primitive type * @param primitive Primitive type
* @param vertexCount Count of unique vertices * @param vertexCount Count of unique vertices
* @param indexCount Count of indices * @param indexCount Count of indices
* @param indexType Type of indices (GL_UNSIGNED_BYTE, * @param indexType Type of indices (indexable, see TypeTraits)
* GL_UNSIGNED_SHORT or GL_UNSIGNED_INT)
*/ */
inline IndexedMesh(Primitive primitive, GLsizei vertexCount, GLsizei indexCount, GLenum indexType = GL_UNSIGNED_SHORT): Mesh(primitive, vertexCount), _indexBuffer(Buffer::ElementArrayBuffer), _indexCount(indexCount), _indexType(indexType) {} inline IndexedMesh(Primitive primitive, GLsizei vertexCount, GLsizei indexCount, Type indexType = Type::UnsignedShort): Mesh(primitive, vertexCount), _indexBuffer(Buffer::ElementArrayBuffer), _indexCount(indexCount), _indexType(indexType) {}
/** @brief Index count */ /** @brief Index count */
inline GLsizei indexCount() const { return _indexCount; } inline GLsizei indexCount() const { return _indexCount; }
@ -56,10 +55,10 @@ class MAGNUM_EXPORT IndexedMesh: public Mesh {
inline void setIndexCount(GLsizei count) { _indexCount = count; } inline void setIndexCount(GLsizei count) { _indexCount = count; }
/** @brief Index type */ /** @brief Index type */
inline GLenum indexType() const { return _indexType; } inline Type indexType() const { return _indexType; }
/** @brief Set index type */ /** @brief Set index type */
inline void setIndexType(GLsizei type) { _indexType = type; } inline void setIndexType(Type type) { _indexType = type; }
/** /**
* @brief Index buffer * @brief Index buffer
@ -80,7 +79,7 @@ class MAGNUM_EXPORT IndexedMesh: public Mesh {
private: private:
Buffer _indexBuffer; Buffer _indexBuffer;
GLsizei _indexCount; GLsizei _indexCount;
GLenum _indexType; Type _indexType;
}; };
} }

2
src/Mesh.h

@ -157,7 +157,7 @@ class MAGNUM_EXPORT Mesh {
* function does nothing. * function does nothing.
*/ */
template<class T> inline void bindAttribute(Buffer* buffer, GLuint attribute) { template<class T> inline void bindAttribute(Buffer* buffer, GLuint attribute) {
bindAttribute(buffer, attribute, TypeTraits<T>::count(), TypeTraits<T>::glType()); bindAttribute(buffer, attribute, TypeTraits<T>::count(), static_cast<GLenum>(TypeTraits<T>::glType()));
} }
/** /**

2
src/MeshBuilder.h

@ -150,7 +150,7 @@ template<class Vertex> class MeshBuilder {
* @see build(IndexedMesh*, Buffer*, Buffer::Usage, Buffer::Usage) * @see build(IndexedMesh*, Buffer*, Buffer::Usage, Buffer::Usage)
*/ */
IndexedMesh* build(Buffer::Usage vertexBufferUsage, Buffer::Usage indexBufferUsage) { IndexedMesh* build(Buffer::Usage vertexBufferUsage, Buffer::Usage indexBufferUsage) {
IndexedMesh mesh = new IndexedMesh(Mesh::Triangles, 0, 0, GL_UNSIGNED_BYTE); IndexedMesh mesh = new IndexedMesh(Mesh::Triangles, 0, 0, Type::UnsignedByte);
Buffer* vertexBuffer = mesh.addBuffer(true); Buffer* vertexBuffer = mesh.addBuffer(true);
build(mesh, vertexBuffer, vertexBufferUsage, indexBufferUsage); build(mesh, vertexBuffer, vertexBufferUsage, indexBufferUsage);

4
src/Trade/Image.h

@ -60,7 +60,7 @@ template<size_t imageDimensions> class Image {
inline AbstractTexture::ColorFormat colorFormat() const { return _colorFormat; } inline AbstractTexture::ColorFormat colorFormat() const { return _colorFormat; }
/** @brief Data type */ /** @brief Data type */
GLenum type() const { return _type; } Type type() const { return _type; }
/** @brief Pointer to raw data */ /** @brief Pointer to raw data */
inline const void* data() const { return _data; } inline const void* data() const { return _data; }
@ -68,7 +68,7 @@ template<size_t imageDimensions> class Image {
private: private:
Math::Vector<GLsizei, Dimensions> _dimensions; Math::Vector<GLsizei, Dimensions> _dimensions;
AbstractTexture::ColorFormat _colorFormat; AbstractTexture::ColorFormat _colorFormat;
GLenum _type; Type _type;
const char* _data; const char* _data;
}; };

38
src/TypeTraits.h

@ -16,7 +16,7 @@
*/ */
/** @file /** @file
* @brief Class Magnum::TypeTraits * @brief Enum Magnum::Type, class Magnum::TypeTraits
*/ */
#include "Magnum.h" #include "Magnum.h"
@ -54,9 +54,9 @@ template<class T> struct TypeTraits: public Math::TypeTraits<T> {
/** /**
* @brief OpenGL plain type ID * @brief OpenGL plain type ID
* *
* Returns e.g. GL_UNSIGNED_INT for GLuint. * Returns e.g. Type::UnsignedInt for GLuint.
*/ */
constexpr inline static GLenum glType(); constexpr inline static Type glType();
/** /**
* @brief Size of plain OpenGL type * @brief Size of plain OpenGL type
@ -77,6 +77,18 @@ template<class T> struct TypeTraits: public Math::TypeTraits<T> {
template<class T> struct TypeTraits {}; template<class T> struct TypeTraits {};
#endif #endif
/** @brief OpenGL plain types */
enum class Type: GLenum {
UnsignedByte = GL_UNSIGNED_BYTE, /**< Unsigned byte (char) */
Byte = GL_BYTE, /**< Byte (char) */
UnsignedShort = GL_UNSIGNED_SHORT, /**< Unsigned short */
Short = GL_SHORT, /**< Short */
UnsignedInt = GL_UNSIGNED_INT, /**< Unsigned int */
Int = GL_INT, /**< Int */
Float = GL_FLOAT, /**< Float */
Double = GL_DOUBLE /**< Double */
};
/** @todo Other texture types, referenced in glTexImage2D function manual */ /** @todo Other texture types, referenced in glTexImage2D function manual */
/** @todo Using Vector3 for textures? */ /** @todo Using Vector3 for textures? */
@ -85,7 +97,7 @@ template<> struct TypeTraits<GLubyte>: public Math::TypeTraits<unsigned char> {
typedef GLubyte IndexType; typedef GLubyte IndexType;
typedef GLubyte TextureType; typedef GLubyte TextureType;
inline constexpr static GLenum glType() { return GL_UNSIGNED_BYTE; } inline constexpr static Type glType() { return Type::UnsignedByte; }
inline constexpr static size_t size() { return sizeof(GLubyte); } inline constexpr static size_t size() { return sizeof(GLubyte); }
inline constexpr static size_t count() { return 1; } inline constexpr static size_t count() { return 1; }
}; };
@ -94,7 +106,7 @@ template<> struct TypeTraits<GLbyte>: public Math::TypeTraits<char> {
/* Can not be used for indices */ /* Can not be used for indices */
typedef GLbyte TextureType; typedef GLbyte TextureType;
inline constexpr static GLenum glType() { return GL_BYTE; } inline constexpr static Type glType() { return Type::Byte; }
inline constexpr static size_t size() { return sizeof(GLbyte); } inline constexpr static size_t size() { return sizeof(GLbyte); }
inline constexpr static size_t count() { return 1; } inline constexpr static size_t count() { return 1; }
}; };
@ -103,7 +115,7 @@ template<> struct TypeTraits<GLushort>: public Math::TypeTraits<unsigned short>
typedef GLushort IndexType; typedef GLushort IndexType;
typedef GLushort TextureType; typedef GLushort TextureType;
inline constexpr static GLenum glType() { return GL_UNSIGNED_SHORT; } inline constexpr static Type glType() { return Type::UnsignedShort; }
inline constexpr static size_t size() { return sizeof(GLushort); } inline constexpr static size_t size() { return sizeof(GLushort); }
inline constexpr static size_t count() { return 1; } inline constexpr static size_t count() { return 1; }
}; };
@ -112,7 +124,7 @@ template<> struct TypeTraits<GLshort>: public Math::TypeTraits<short> {
/* Can not be used for indices */ /* Can not be used for indices */
typedef GLshort TextureType; typedef GLshort TextureType;
inline constexpr static GLenum glType() { return GL_SHORT; } inline constexpr static Type glType() { return Type::Short; }
inline constexpr static size_t size() { return sizeof(GLshort); } inline constexpr static size_t size() { return sizeof(GLshort); }
inline constexpr static size_t count() { return 1; } inline constexpr static size_t count() { return 1; }
}; };
@ -121,7 +133,7 @@ template<> struct TypeTraits<GLuint>: public Math::TypeTraits<unsigned int> {
typedef GLuint IndexType; typedef GLuint IndexType;
typedef GLuint TextureType; typedef GLuint TextureType;
inline constexpr static GLenum glType() { return GL_UNSIGNED_INT; } inline constexpr static Type glType() { return Type::UnsignedInt; }
inline constexpr static size_t size() { return sizeof(GLuint); } inline constexpr static size_t size() { return sizeof(GLuint); }
inline constexpr static size_t count() { return 1; } inline constexpr static size_t count() { return 1; }
}; };
@ -130,7 +142,7 @@ template<> struct TypeTraits<GLint>: public Math::TypeTraits<int> {
/* Can not be used for indices */ /* Can not be used for indices */
typedef GLint TextureType; typedef GLint TextureType;
inline constexpr static GLenum glType() { return GL_INT; } inline constexpr static Type glType() { return Type::Int; }
inline constexpr static size_t size() { return sizeof(GLint); } inline constexpr static size_t size() { return sizeof(GLint); }
inline constexpr static size_t count() { return 1; } inline constexpr static size_t count() { return 1; }
}; };
@ -139,7 +151,7 @@ template<> struct TypeTraits<GLfloat>: public Math::TypeTraits<float> {
/* Can not be used for indices */ /* Can not be used for indices */
typedef GLfloat TextureType; typedef GLfloat TextureType;
inline constexpr static GLenum glType() { return GL_FLOAT; } inline constexpr static Type glType() { return Type::Float; }
inline constexpr static size_t size() { return sizeof(GLfloat); } inline constexpr static size_t size() { return sizeof(GLfloat); }
inline constexpr static size_t count() { return 1; } inline constexpr static size_t count() { return 1; }
}; };
@ -148,7 +160,7 @@ template<> struct TypeTraits<GLdouble>: public Math::TypeTraits<double> {
/* Can not be used for indices */ /* Can not be used for indices */
/* Can not be used for textures */ /* Can not be used for textures */
inline constexpr static GLenum glType() { return GL_DOUBLE; } inline constexpr static Type glType() { return Type::Double; }
inline constexpr static size_t size() { return sizeof(GLdouble); } inline constexpr static size_t size() { return sizeof(GLdouble); }
inline constexpr static size_t count() { return 1; } inline constexpr static size_t count() { return 1; }
}; };
@ -157,7 +169,7 @@ template<class T, size_t vectorSize> struct TypeTraits<Math::Vector<T, vectorSiz
/* Can not be used for indices */ /* Can not be used for indices */
/* Can not be used for textures */ /* Can not be used for textures */
inline constexpr static GLenum glType() { return TypeTraits<T>::glType(); } inline constexpr static Type glType() { return TypeTraits<T>::glType(); }
inline constexpr static size_t size() { return sizeof(T); } inline constexpr static size_t size() { return sizeof(T); }
inline constexpr static size_t count() { return vectorSize; } inline constexpr static size_t count() { return vectorSize; }
}; };
@ -170,7 +182,7 @@ template<class T, size_t matrixSize> struct TypeTraits<Math::Matrix<T, matrixSiz
/* Can not be used for indices */ /* Can not be used for indices */
/* Can not be used for textures, obviously */ /* Can not be used for textures, obviously */
inline constexpr static GLenum glType() { return TypeTraits<T>::glType(); } inline constexpr static Type glType() { return TypeTraits<T>::glType(); }
inline constexpr static size_t size() { return sizeof(T); } inline constexpr static size_t size() { return sizeof(T); }
inline constexpr static size_t count() { return matrixSize*matrixSize; } inline constexpr static size_t count() { return matrixSize*matrixSize; }
}; };

Loading…
Cancel
Save