| 1 | // Created by: Kirill GAVRILOV |
| 2 | // Copyright (c) 2013-2014 OPEN CASCADE SAS |
| 3 | // |
| 4 | // This file is part of Open CASCADE Technology software library. |
| 5 | // |
| 6 | // This library is free software; you can redistribute it and/or modify it under |
| 7 | // the terms of the GNU Lesser General Public License version 2.1 as published |
| 8 | // by the Free Software Foundation, with special exception defined in the file |
| 9 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
| 10 | // distribution for complete text of the license and disclaimer of any warranty. |
| 11 | // |
| 12 | // Alternatively, this file may be used under the terms of Open CASCADE |
| 13 | // commercial license or contractual agreement. |
| 14 | |
| 15 | #ifndef _OpenGl_Texture_H__ |
| 16 | #define _OpenGl_Texture_H__ |
| 17 | |
| 18 | #include <OpenGl_GlCore13.hxx> |
| 19 | #include <OpenGl_Resource.hxx> |
| 20 | #include <Handle_OpenGl_Texture.hxx> |
| 21 | #include <Graphic3d_TypeOfTexture.hxx> |
| 22 | #include <Handle_Graphic3d_TextureParams.hxx> |
| 23 | |
| 24 | class Handle(OpenGl_Context); |
| 25 | class OpenGl_Context; |
| 26 | class Image_PixMap; |
| 27 | |
| 28 | //! Selects preferable texture format for specified parameters. |
| 29 | template<class T> |
| 30 | struct OpenGl_TextureFormatSelector |
| 31 | { |
| 32 | // Not implemented |
| 33 | }; |
| 34 | |
| 35 | template<> |
| 36 | struct OpenGl_TextureFormatSelector<GLubyte> |
| 37 | { |
| 38 | static GLint Internal (GLuint theChannels) |
| 39 | { |
| 40 | switch (theChannels) |
| 41 | { |
| 42 | case 1: |
| 43 | return GL_R8; |
| 44 | case 2: |
| 45 | return GL_RG8; |
| 46 | case 3: |
| 47 | return GL_RGB8; |
| 48 | case 4: |
| 49 | return GL_RGBA8; |
| 50 | default: |
| 51 | return GL_NONE; |
| 52 | } |
| 53 | } |
| 54 | }; |
| 55 | |
| 56 | template<> |
| 57 | struct OpenGl_TextureFormatSelector<GLushort> |
| 58 | { |
| 59 | static GLint Internal (GLuint theChannels) |
| 60 | { |
| 61 | switch (theChannels) |
| 62 | { |
| 63 | case 1: |
| 64 | return GL_R16; |
| 65 | case 2: |
| 66 | return GL_RG16; |
| 67 | case 3: |
| 68 | return GL_RGB16; |
| 69 | case 4: |
| 70 | return GL_RGBA16; |
| 71 | default: |
| 72 | return GL_NONE; |
| 73 | } |
| 74 | } |
| 75 | }; |
| 76 | |
| 77 | template<> |
| 78 | struct OpenGl_TextureFormatSelector<GLfloat> |
| 79 | { |
| 80 | static GLint Internal (GLuint theChannels) |
| 81 | { |
| 82 | switch (theChannels) |
| 83 | { |
| 84 | case 1: |
| 85 | return GL_R32F; |
| 86 | case 2: |
| 87 | return GL_RG32F; |
| 88 | case 3: |
| 89 | return GL_RGB32F; |
| 90 | case 4: |
| 91 | return GL_RGBA32F; |
| 92 | default: |
| 93 | return GL_NONE; |
| 94 | } |
| 95 | } |
| 96 | }; |
| 97 | |
| 98 | //! Stores parameters of OpenGL texture format. |
| 99 | class OpenGl_TextureFormat |
| 100 | { |
| 101 | friend class OpenGl_Texture; |
| 102 | |
| 103 | public: |
| 104 | |
| 105 | //! Returns OpenGL format of the pixel data. |
| 106 | inline GLenum Format() const |
| 107 | { |
| 108 | switch (myChannels) |
| 109 | { |
| 110 | case 1: |
| 111 | return GL_RED; |
| 112 | case 2: |
| 113 | return GL_RG; |
| 114 | case 3: |
| 115 | return GL_RGB; |
| 116 | case 4: |
| 117 | return GL_RGBA; |
| 118 | default: |
| 119 | return GL_NONE; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | //! Returns OpenGL internal format of the pixel data. |
| 124 | inline GLint Internal() const |
| 125 | { |
| 126 | return myInternal; |
| 127 | } |
| 128 | |
| 129 | //! Returns texture format for specified type and number of channels. |
| 130 | template<class T, int N> |
| 131 | static OpenGl_TextureFormat Create() |
| 132 | { |
| 133 | return OpenGl_TextureFormat (N, OpenGl_TextureFormatSelector<T>::Internal (N)); |
| 134 | } |
| 135 | |
| 136 | private: |
| 137 | |
| 138 | //! Creates new texture format. |
| 139 | OpenGl_TextureFormat (const GLint theChannels, |
| 140 | const GLint theInternal) |
| 141 | : myInternal (theInternal), |
| 142 | myChannels (theChannels) {} |
| 143 | |
| 144 | private: |
| 145 | |
| 146 | GLint myInternal; //!< OpenGL internal format of the pixel data |
| 147 | GLint myChannels; //!< Number of channels for each pixel (from 1 to 4) |
| 148 | |
| 149 | }; |
| 150 | |
| 151 | //! Texture resource. |
| 152 | class OpenGl_Texture : public OpenGl_Resource |
| 153 | { |
| 154 | |
| 155 | public: |
| 156 | |
| 157 | //! Helpful constants |
| 158 | static const GLuint NO_TEXTURE = 0; |
| 159 | |
| 160 | public: |
| 161 | |
| 162 | //! Create uninitialized VBO. |
| 163 | Standard_EXPORT OpenGl_Texture (const Handle(Graphic3d_TextureParams)& theParams = NULL); |
| 164 | |
| 165 | //! Destroy object. |
| 166 | Standard_EXPORT virtual ~OpenGl_Texture(); |
| 167 | |
| 168 | //! @return true if current object was initialized |
| 169 | inline bool IsValid() const |
| 170 | { |
| 171 | return myTextureId != NO_TEXTURE; |
| 172 | } |
| 173 | |
| 174 | //! @return target to which the texture is bound (GL_TEXTURE_1D, GL_TEXTURE_2D) |
| 175 | inline GLenum GetTarget() const |
| 176 | { |
| 177 | return myTarget; |
| 178 | } |
| 179 | |
| 180 | //! @return texture width (0 LOD) |
| 181 | inline GLsizei SizeX() const |
| 182 | { |
| 183 | return mySizeX; |
| 184 | } |
| 185 | |
| 186 | //! @return texture height (0 LOD) |
| 187 | inline GLsizei SizeY() const |
| 188 | { |
| 189 | return mySizeY; |
| 190 | } |
| 191 | |
| 192 | //! @return texture ID |
| 193 | inline GLuint TextureId() const |
| 194 | { |
| 195 | return myTextureId; |
| 196 | } |
| 197 | |
| 198 | //! @return texture format |
| 199 | inline GLint GetFormat() const |
| 200 | { |
| 201 | return myTextFormat; |
| 202 | } |
| 203 | |
| 204 | //! Creates Texture id if not yet generated. |
| 205 | //! Data should be initialized by another method. |
| 206 | Standard_EXPORT bool Create (const Handle(OpenGl_Context)& theCtx); |
| 207 | |
| 208 | //! Destroy object - will release GPU memory if any. |
| 209 | Standard_EXPORT virtual void Release (OpenGl_Context* theCtx); |
| 210 | |
| 211 | //! Bind this Texture to specified unit. |
| 212 | Standard_EXPORT void Bind (const Handle(OpenGl_Context)& theCtx, |
| 213 | const GLenum theTextureUnit = GL_TEXTURE0) const; |
| 214 | |
| 215 | //! Unbind texture from specified unit. |
| 216 | Standard_EXPORT void Unbind (const Handle(OpenGl_Context)& theCtx, |
| 217 | const GLenum theTextureUnit = GL_TEXTURE0) const; |
| 218 | |
| 219 | //! Notice that texture will be unbound after this call. |
| 220 | Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theCtx, |
| 221 | const Image_PixMap& theImage, |
| 222 | const Graphic3d_TypeOfTexture theType); |
| 223 | |
| 224 | //! Initialize the texture with specified format, size and texture type. |
| 225 | //! If theImage is empty the texture data will contain trash. |
| 226 | //! Notice that texture will be unbound after this call. |
| 227 | Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theCtx, |
| 228 | const GLint theTextFormat, |
| 229 | const GLenum thePixelFormat, |
| 230 | const GLenum theDataType, |
| 231 | const GLsizei theSizeX, |
| 232 | const GLsizei theSizeY, |
| 233 | const Graphic3d_TypeOfTexture theType, |
| 234 | const Image_PixMap* theImage = NULL); |
| 235 | |
| 236 | //! Allocates texture rectangle with specified format and size. |
| 237 | //! \note Texture data is not initialized (will contain trash). |
| 238 | Standard_EXPORT bool InitRectangle (const Handle(OpenGl_Context)& theCtx, |
| 239 | const Standard_Integer theSizeX, |
| 240 | const Standard_Integer theSizeY, |
| 241 | const OpenGl_TextureFormat& theFormat); |
| 242 | |
| 243 | //! @return true if texture was generated within mipmaps |
| 244 | Standard_EXPORT const Standard_Boolean HasMipmaps() const; |
| 245 | |
| 246 | //! @return assigned texture parameters (not necessary applied) |
| 247 | Standard_EXPORT const Handle(Graphic3d_TextureParams)& GetParams() const; |
| 248 | |
| 249 | //! @param texture parameters |
| 250 | Standard_EXPORT void SetParams (const Handle(Graphic3d_TextureParams)& theParams); |
| 251 | |
| 252 | //! Return texture type and format by Image_PixMap data format. |
| 253 | Standard_EXPORT static bool GetDataFormat (const Handle(OpenGl_Context)& theCtx, |
| 254 | const Image_PixMap& theData, |
| 255 | GLint& theTextFormat, |
| 256 | GLenum& thePixelFormat, |
| 257 | GLenum& theDataType); |
| 258 | |
| 259 | protected: |
| 260 | |
| 261 | GLuint myTextureId; //!< GL resource ID |
| 262 | GLenum myTarget; //!< GL_TEXTURE_1D/GL_TEXTURE_2D |
| 263 | GLsizei mySizeX; //!< texture width |
| 264 | GLsizei mySizeY; //!< texture height |
| 265 | GLint myTextFormat; //!< texture format - GL_RGB, GL_RGBA,... |
| 266 | Standard_Boolean myHasMipmaps; //!< flag indicates that texture was uploaded with mipmaps |
| 267 | |
| 268 | Handle(Graphic3d_TextureParams) myParams; //!< texture parameters |
| 269 | |
| 270 | public: |
| 271 | |
| 272 | DEFINE_STANDARD_RTTI(OpenGl_Texture) // Type definition |
| 273 | |
| 274 | }; |
| 275 | |
| 276 | #endif // _OpenGl_Texture_H__ |