From 52971935abbc565b79849da5a721c2d19bbec21b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 19 Feb 2012 21:10:02 +0100 Subject: [PATCH] Using strongly-typed enum for plain OpenGL types. Mesh and IndexedMesh is not completely ported, as it will need more work. --- src/AbstractTexture.h | 28 ++++++++++++++-------------- src/CubeMapTexture.h | 4 ++-- src/IndexedMesh.cpp | 2 +- src/IndexedMesh.h | 13 ++++++------- src/Mesh.h | 2 +- src/MeshBuilder.h | 2 +- src/Trade/Image.h | 4 ++-- src/TypeTraits.h | 38 +++++++++++++++++++++++++------------- 8 files changed, 52 insertions(+), 41 deletions(-) diff --git a/src/AbstractTexture.h b/src/AbstractTexture.h index 69e930316..2ec484b76 100644 --- a/src/AbstractTexture.h +++ b/src/AbstractTexture.h @@ -228,7 +228,7 @@ class MAGNUM_EXPORT AbstractTexture { * Calls @c glTexImage1D, @c glTexImage2D, @c glTexImage3D depending * on dimension count. */ - inline static void set(GLenum target, GLint mipLevel, InternalFormat internalFormat, const Math::Vector& dimensions, ColorFormat colorFormat, GLenum type, const void* data); + inline static void set(GLenum target, GLint mipLevel, InternalFormat internalFormat, const Math::Vector& dimensions, ColorFormat colorFormat, Type type, const void* data); /** * @brief Set texture subdata @@ -243,7 +243,7 @@ class MAGNUM_EXPORT AbstractTexture { * Calls @c glTexSubImage1D, @c glTexSubImage2D, @c glTexSubImage3D * depending on dimension count. */ - inline static void setSub(GLenum target, GLint mipLevel, const Math::Vector& offset, const Math::Vector& dimensions, ColorFormat colorFormat, GLenum type, const void* data); + inline static void setSub(GLenum target, GLint mipLevel, const Math::Vector& offset, const Math::Vector& dimensions, ColorFormat colorFormat, Type type, const void* data); #endif }; @@ -256,34 +256,34 @@ class MAGNUM_EXPORT AbstractTexture { template<> struct AbstractTexture::DataHelper<1> { inline constexpr static GLenum target() { return GL_TEXTURE_1D; } - inline static void set(GLenum target, GLint mipLevel, InternalFormat internalFormat, const Math::Vector& dimensions, ColorFormat colorFormat, GLenum type, const void* data) { - glTexImage1D(target, mipLevel, static_cast(internalFormat), dimensions.at(0), 0, static_cast(colorFormat), type, data); + inline static void set(GLenum target, GLint mipLevel, InternalFormat internalFormat, const Math::Vector& dimensions, ColorFormat colorFormat, Type type, const void* data) { + glTexImage1D(target, mipLevel, static_cast(internalFormat), dimensions.at(0), 0, static_cast(colorFormat), static_cast(type), data); } - inline static void setSub(GLenum target, GLint mipLevel, const Math::Vector& offset, const Math::Vector& dimensions, ColorFormat colorFormat, GLenum type, const void* data) { - glTexSubImage1D(target, mipLevel, offset.at(0), dimensions.at(0), static_cast(colorFormat), type, data); + inline static void setSub(GLenum target, GLint mipLevel, const Math::Vector& offset, const Math::Vector& dimensions, ColorFormat colorFormat, Type type, const void* data) { + glTexSubImage1D(target, mipLevel, offset.at(0), dimensions.at(0), static_cast(colorFormat), static_cast(type), data); } }; template<> struct AbstractTexture::DataHelper<2> { inline constexpr static GLenum target() { return GL_TEXTURE_2D; } - inline static void set(GLenum target, GLint mipLevel, InternalFormat internalFormat, const Math::Vector& dimensions, ColorFormat colorFormat, GLenum type, const void* data) { - glTexImage2D(target, mipLevel, static_cast(internalFormat), dimensions.at(0), dimensions.at(1), 0, static_cast(colorFormat), type, data); + inline static void set(GLenum target, GLint mipLevel, InternalFormat internalFormat, const Math::Vector& dimensions, ColorFormat colorFormat, Type type, const void* data) { + glTexImage2D(target, mipLevel, static_cast(internalFormat), dimensions.at(0), dimensions.at(1), 0, static_cast(colorFormat), static_cast(type), data); } - inline static void setSub(GLenum target, GLint mipLevel, const Math::Vector& offset, const Math::Vector& dimensions, ColorFormat colorFormat, GLenum type, const void* data) { - glTexSubImage2D(target, mipLevel, offset.at(0), offset.at(1), dimensions.at(0), dimensions.at(1), static_cast(colorFormat), type, data); + inline static void setSub(GLenum target, GLint mipLevel, const Math::Vector& offset, const Math::Vector& 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(colorFormat), static_cast(type), data); } }; template<> struct AbstractTexture::DataHelper<3> { inline constexpr static GLenum target() { return GL_TEXTURE_3D; } - inline static void set(GLenum target, GLint mipLevel, InternalFormat internalFormat, const Math::Vector& dimensions, ColorFormat colorFormat, GLenum type, const void* data) { - glTexImage3D(target, mipLevel, static_cast(internalFormat), dimensions.at(0), dimensions.at(1), dimensions.at(2), 0, static_cast(colorFormat), type, data); + inline static void set(GLenum target, GLint mipLevel, InternalFormat internalFormat, const Math::Vector& dimensions, ColorFormat colorFormat, Type type, const void* data) { + glTexImage3D(target, mipLevel, static_cast(internalFormat), dimensions.at(0), dimensions.at(1), dimensions.at(2), 0, static_cast(colorFormat), static_cast(type), data); } - inline static void setSub(GLenum target, GLint mipLevel, const Math::Vector& offset, const Math::Vector& dimensions, ColorFormat colorFormat, GLenum 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(colorFormat), type, data); + inline static void setSub(GLenum target, GLint mipLevel, const Math::Vector& offset, const Math::Vector& 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(colorFormat), static_cast(type), data); } }; #endif diff --git a/src/CubeMapTexture.h b/src/CubeMapTexture.h index 63a863df0..c10b41d73 100644 --- a/src/CubeMapTexture.h +++ b/src/CubeMapTexture.h @@ -109,12 +109,12 @@ class CubeMapTexture: public Texture2D { } private: - void setData(GLenum target, GLint mipLevel, InternalFormat internalFormat, const Math::Vector& _dimensions, ColorFormat colorFormat, GLenum type, const void* data) { + void setData(GLenum target, GLint mipLevel, InternalFormat internalFormat, const Math::Vector& _dimensions, ColorFormat colorFormat, Type type, const void* data) { bind(); DataHelper::set(target, mipLevel, internalFormat, _dimensions, colorFormat, type, data); } - void setSubData(GLenum target, GLint mipLevel, const Math::Vector& offset, const Math::Vector& dimensions, ColorFormat colorFormat, GLenum type, const void* data) { + void setSubData(GLenum target, GLint mipLevel, const Math::Vector& offset, const Math::Vector& dimensions, ColorFormat colorFormat, Type type, const void* data) { bind(); DataHelper::setSub(target, mipLevel, offset, dimensions, colorFormat, type, data); } diff --git a/src/IndexedMesh.cpp b/src/IndexedMesh.cpp index 98692e83a..039fab3eb 100644 --- a/src/IndexedMesh.cpp +++ b/src/IndexedMesh.cpp @@ -50,7 +50,7 @@ void IndexedMesh::draw() { /* Bind index array, draw the elements and unbind */ _indexBuffer.bind(); - glDrawElements(primitive(), _indexCount, _indexType, nullptr); + glDrawElements(primitive(), _indexCount, static_cast(_indexType), nullptr); /* Disable vertex arrays for all attributes */ for(set::const_iterator it = attributes().begin(); it != attributes().end(); ++it) diff --git a/src/IndexedMesh.h b/src/IndexedMesh.h index 4741aa73b..098424bc9 100644 --- a/src/IndexedMesh.h +++ b/src/IndexedMesh.h @@ -37,17 +37,16 @@ class MAGNUM_EXPORT IndexedMesh: public Mesh { * setIndexCount() and setIndexType() manually for mesh to draw * properly. */ - inline IndexedMesh(): _indexBuffer(Buffer::ElementArrayBuffer), _indexCount(0), _indexType(GL_UNSIGNED_SHORT) {} + inline IndexedMesh(): _indexBuffer(Buffer::ElementArrayBuffer), _indexCount(0), _indexType(Type::UnsignedShort) {} /** * @brief Constructor * @param primitive Primitive type * @param vertexCount Count of unique vertices * @param indexCount Count of indices - * @param indexType Type of indices (GL_UNSIGNED_BYTE, - * GL_UNSIGNED_SHORT or GL_UNSIGNED_INT) + * @param indexType Type of indices (indexable, see TypeTraits) */ - 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 */ inline GLsizei indexCount() const { return _indexCount; } @@ -56,10 +55,10 @@ class MAGNUM_EXPORT IndexedMesh: public Mesh { inline void setIndexCount(GLsizei count) { _indexCount = count; } /** @brief Index type */ - inline GLenum indexType() const { return _indexType; } + inline Type indexType() const { return _indexType; } /** @brief Set index type */ - inline void setIndexType(GLsizei type) { _indexType = type; } + inline void setIndexType(Type type) { _indexType = type; } /** * @brief Index buffer @@ -80,7 +79,7 @@ class MAGNUM_EXPORT IndexedMesh: public Mesh { private: Buffer _indexBuffer; GLsizei _indexCount; - GLenum _indexType; + Type _indexType; }; } diff --git a/src/Mesh.h b/src/Mesh.h index 47e8dbde1..19e48a0fe 100644 --- a/src/Mesh.h +++ b/src/Mesh.h @@ -157,7 +157,7 @@ class MAGNUM_EXPORT Mesh { * function does nothing. */ template inline void bindAttribute(Buffer* buffer, GLuint attribute) { - bindAttribute(buffer, attribute, TypeTraits::count(), TypeTraits::glType()); + bindAttribute(buffer, attribute, TypeTraits::count(), static_cast(TypeTraits::glType())); } /** diff --git a/src/MeshBuilder.h b/src/MeshBuilder.h index a8fe42832..b74919c49 100644 --- a/src/MeshBuilder.h +++ b/src/MeshBuilder.h @@ -150,7 +150,7 @@ template class MeshBuilder { * @see build(IndexedMesh*, Buffer*, Buffer::Usage, Buffer::Usage) */ 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); build(mesh, vertexBuffer, vertexBufferUsage, indexBufferUsage); diff --git a/src/Trade/Image.h b/src/Trade/Image.h index e6da645cd..c3ccf2edc 100644 --- a/src/Trade/Image.h +++ b/src/Trade/Image.h @@ -60,7 +60,7 @@ template class Image { inline AbstractTexture::ColorFormat colorFormat() const { return _colorFormat; } /** @brief Data type */ - GLenum type() const { return _type; } + Type type() const { return _type; } /** @brief Pointer to raw data */ inline const void* data() const { return _data; } @@ -68,7 +68,7 @@ template class Image { private: Math::Vector _dimensions; AbstractTexture::ColorFormat _colorFormat; - GLenum _type; + Type _type; const char* _data; }; diff --git a/src/TypeTraits.h b/src/TypeTraits.h index 53c71e0d3..9c8eadff2 100644 --- a/src/TypeTraits.h +++ b/src/TypeTraits.h @@ -16,7 +16,7 @@ */ /** @file - * @brief Class Magnum::TypeTraits + * @brief Enum Magnum::Type, class Magnum::TypeTraits */ #include "Magnum.h" @@ -54,9 +54,9 @@ template struct TypeTraits: public Math::TypeTraits { /** * @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 @@ -77,6 +77,18 @@ template struct TypeTraits: public Math::TypeTraits { template struct TypeTraits {}; #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 Using Vector3 for textures? */ @@ -85,7 +97,7 @@ template<> struct TypeTraits: public Math::TypeTraits { typedef GLubyte IndexType; 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 count() { return 1; } }; @@ -94,7 +106,7 @@ template<> struct TypeTraits: public Math::TypeTraits { /* Can not be used for indices */ 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 count() { return 1; } }; @@ -103,7 +115,7 @@ template<> struct TypeTraits: public Math::TypeTraits typedef GLushort IndexType; 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 count() { return 1; } }; @@ -112,7 +124,7 @@ template<> struct TypeTraits: public Math::TypeTraits { /* Can not be used for indices */ 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 count() { return 1; } }; @@ -121,7 +133,7 @@ template<> struct TypeTraits: public Math::TypeTraits { typedef GLuint IndexType; 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 count() { return 1; } }; @@ -130,7 +142,7 @@ template<> struct TypeTraits: public Math::TypeTraits { /* Can not be used for indices */ 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 count() { return 1; } }; @@ -139,7 +151,7 @@ template<> struct TypeTraits: public Math::TypeTraits { /* Can not be used for indices */ 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 count() { return 1; } }; @@ -148,7 +160,7 @@ template<> struct TypeTraits: public Math::TypeTraits { /* Can not be used for indices */ /* 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 count() { return 1; } }; @@ -157,7 +169,7 @@ template struct TypeTraits::glType(); } + inline constexpr static Type glType() { return TypeTraits::glType(); } inline constexpr static size_t size() { return sizeof(T); } inline constexpr static size_t count() { return vectorSize; } }; @@ -170,7 +182,7 @@ template struct TypeTraits::glType(); } + inline constexpr static Type glType() { return TypeTraits::glType(); } inline constexpr static size_t size() { return sizeof(T); } inline constexpr static size_t count() { return matrixSize*matrixSize; } };