1 // Created by: Kirill GAVRILOV
2 // Copyright (c) 2013-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
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.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #ifndef _OpenGl_Texture_H__
16 #define _OpenGl_Texture_H__
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>
24 class Handle(OpenGl_Context);
28 //! Selects preferable texture format for specified parameters.
30 struct OpenGl_TextureFormatSelector
36 struct OpenGl_TextureFormatSelector<GLubyte>
38 static GLint Internal (GLuint theChannels)
57 struct OpenGl_TextureFormatSelector<GLushort>
59 static GLint Internal (GLuint theChannels)
78 struct OpenGl_TextureFormatSelector<GLfloat>
80 static GLint Internal (GLuint theChannels)
98 //! Stores parameters of OpenGL texture format.
99 class OpenGl_TextureFormat
101 friend class OpenGl_Texture;
105 //! Returns OpenGL format of the pixel data.
106 inline GLenum Format() const
123 //! Returns OpenGL internal format of the pixel data.
124 inline GLint Internal() const
129 //! Returns texture format for specified type and number of channels.
130 template<class T, int N>
131 static OpenGl_TextureFormat Create()
133 return OpenGl_TextureFormat (N, OpenGl_TextureFormatSelector<T>::Internal (N));
138 //! Creates new texture format.
139 OpenGl_TextureFormat (const GLint theChannels,
140 const GLint theInternal)
141 : myInternal (theInternal),
142 myChannels (theChannels) {}
146 GLint myInternal; //!< OpenGL internal format of the pixel data
147 GLint myChannels; //!< Number of channels for each pixel (from 1 to 4)
151 //! Texture resource.
152 class OpenGl_Texture : public OpenGl_Resource
157 //! Helpful constants
158 static const GLuint NO_TEXTURE = 0;
162 //! Create uninitialized VBO.
163 Standard_EXPORT OpenGl_Texture (const Handle(Graphic3d_TextureParams)& theParams = NULL);
166 Standard_EXPORT virtual ~OpenGl_Texture();
168 //! @return true if current object was initialized
169 inline bool IsValid() const
171 return myTextureId != NO_TEXTURE;
174 //! @return target to which the texture is bound (GL_TEXTURE_1D, GL_TEXTURE_2D)
175 inline GLenum GetTarget() const
180 //! @return texture width (0 LOD)
181 inline GLsizei SizeX() const
186 //! @return texture height (0 LOD)
187 inline GLsizei SizeY() const
192 //! @return texture ID
193 inline GLuint TextureId() const
198 //! @return texture format (not sized)
199 inline GLenum GetFormat() const
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);
208 //! Destroy object - will release GPU memory if any.
209 Standard_EXPORT virtual void Release (OpenGl_Context* theCtx);
211 //! Bind this Texture to specified unit.
212 Standard_EXPORT void Bind (const Handle(OpenGl_Context)& theCtx,
213 const GLenum theTextureUnit = GL_TEXTURE0) const;
215 //! Unbind texture from specified unit.
216 Standard_EXPORT void Unbind (const Handle(OpenGl_Context)& theCtx,
217 const GLenum theTextureUnit = GL_TEXTURE0) const;
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);
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);
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);
243 //! @return true if texture was generated within mipmaps
244 Standard_EXPORT const Standard_Boolean HasMipmaps() const;
246 //! @return assigned texture parameters (not necessary applied)
247 Standard_EXPORT const Handle(Graphic3d_TextureParams)& GetParams() const;
249 //! @param texture parameters
250 Standard_EXPORT void SetParams (const Handle(Graphic3d_TextureParams)& theParams);
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);
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 GLenum myTextFormat; //!< texture format - GL_RGB, GL_RGBA,...
266 Standard_Boolean myHasMipmaps; //!< flag indicates that texture was uploaded with mipmaps
268 Handle(Graphic3d_TextureParams) myParams; //!< texture parameters
272 DEFINE_STANDARD_RTTI(OpenGl_Texture) // Type definition
276 #endif // _OpenGl_Texture_H__