//////////////////////////////////////////////////////////////////////////////// // Copyright (c) 2011, Computer Graphics Group RWTH Aachen University // // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// #ifndef ACGL_RESOURCE_TEXTURE_HH #define ACGL_RESOURCE_TEXTURE_HH #include #include #include #include #include #include namespace ACGL{ namespace OpenGL{ class Texture { // ======================================================================================================== \/ // ============================================================================================ GPU CONTEXT \/ // ======================================================================================================== \/ private: static GLuint sTextureContext; // ========================================================================================================= \/ // ============================================================================================ CONSTRUCTORS \/ // ========================================================================================================= \/ public: Texture( GLenum _internalFormat = GL_RGBA, GLenum _target = GL_TEXTURE_2D, GLenum _format = GL_RGBA, GLenum _type = GL_UNSIGNED_BYTE) : mContext(0), mWidth(0), mHeight(0), mDepth(0), mInternalFormat(_internalFormat), mTarget(_target), mFormat(_format), mType(_type) { glGenTextures(1, &mContext); if (openGLCriticalErrorOccured() ) { ACGL::Utils::error() << "could not generate texture!" << std::endl; } } virtual ~Texture(void) { // context 0 will get ignored by OpenGL glDeleteTextures(1, &mContext); } // ==================================================================================================== \/ // ============================================================================================ GETTERS \/ // ==================================================================================================== \/ public: inline GLuint getContext (void) const { return mContext; } inline GLsizei getWidth (void) const { return mWidth; } inline GLsizei getHeight (void) const { return mHeight; } inline GLsizei getDepth (void) const { return mDepth; } inline GLenum getInternalFormat (void) const { return mInternalFormat; } inline GLenum getTarget (void) const { return mTarget; } inline GLenum getFormat (void) const { return mFormat; } inline GLenum getType (void) const { return mType; } // ==================================================================================================== \/ // ============================================================================================ SETTERS \/ // ==================================================================================================== \/ public: inline void setInternalFormat (GLenum _internalFormat) { mInternalFormat = _internalFormat; } inline void setTarget (GLenum _target) { mTarget = _target; } inline void setFormat (GLenum _format) { mFormat = _format; } inline void setType (GLenum _type) { mType = _type; } inline void setSize( GLsizei _width = 0, GLsizei _height = 0, GLsizei _depth = 0) { mWidth = _width; mHeight = _height; mDepth = _depth; } // ===================================================================================================== \/ // ============================================================================================ WRAPPERS \/ // ===================================================================================================== \/ public: //! Activate texture unit and bind this texture. inline void bind(GLenum _textureUnit) const { if(sTextureContext == mContext) return; glActiveTexture(GL_TEXTURE0 + _textureUnit); glBindTexture(mTarget, mContext); openGLRareError(); sTextureContext = mContext; } //! Bind this texture. inline void bind(void) const { if(sTextureContext == mContext) return; glBindTexture(mTarget, mContext); openGLRareError(); sTextureContext = mContext; } inline void setParameter(GLenum _parameter, GLint _value) { bind(); glTexParameteri(mTarget, _parameter, _value); openGLRareError(); } inline void setParameter(GLenum _parameter, GLfloat _value) { bind(); glTexParameterf(mTarget, _parameter, _value); openGLRareError(); } //! Set texture size for 1D, 2D or 3D textures and NULL data inline void setRenderTarget( GLsizei _width, GLsizei _height = 0, GLsizei _depth = 0) { bind(); mWidth = _width; mHeight = _height; mDepth = _depth; if(mWidth > 0 && mHeight > 0 && mDepth > 0) { //3D } else if(mWidth > 0 && mHeight > 0) { glTexImage2D( mTarget, 0, mInternalFormat, mWidth, mHeight, 0,//no border mFormat, mType, NULL); } else if(mWidth > 0) { //1D } } //! Set texture size and NULL data inline void setRenderTarget2D( GLsizei _width, GLsizei _height) { bind(); mWidth = _width; mHeight = _height; glTexImage2D( mTarget, 0, mInternalFormat, mWidth, mHeight, 0,//no border mFormat, mType, NULL); } //! Set texture data for some mipmap level. /*! If you upload a texture for a higher mipmap level, the function will automatically determine its size calculating (w,h)/2^level where w and h are the width and height of the base texture. */ inline void setTexImage2D( const GLvoid* _pData) { bind(); glTexImage2D( mTarget, 0, mInternalFormat, mWidth, mHeight, 0,//no border mFormat, mType, _pData); } //! Set texture data for some mipmap level. /*! If you upload a texture for a higher mipmap level, the function will automatically determine its size calculating (w,h)/2^level where w and h are the width and height of the base texture. */ inline void setTexImage2D( const GLvoid* _pData, GLint _mipmapLevel) { bind(); glTexImage2D( mTarget, _mipmapLevel, mInternalFormat, mWidth / GLsizei(Math::Functions::pow(2.0f, float(_mipmapLevel))), mHeight / GLsizei(Math::Functions::pow(2.0f, float(_mipmapLevel))), 0,//no border mFormat, mType, _pData); } //! Generate mipmaps from the current base texture (i.e. the texture from level 0). void generateMipmaps(void) const { bind(); glEnable(mTarget); glGenerateMipmap(mTarget); openGLRareError(); } // =================================================================================================== \/ // ============================================================================================ FIELDS \/ // =================================================================================================== \/ private: GLuint mContext; GLsizei mWidth; GLsizei mHeight; GLsizei mDepth; GLenum mInternalFormat; GLenum mTarget; GLenum mFormat; GLenum mType; }; typedef std::tr1::shared_ptr SharedTexture; } // OpenGL } // ACGL #endif // ACGL_RESOURCE_TEXTURE_HH