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 <Graphic3d_TypeOfTexture.hxx>
23 class Graphic3d_TextureParams;
26 //! Selects preferable texture format for specified parameters.
28 struct OpenGl_TextureFormatSelector
34 struct OpenGl_TextureFormatSelector<GLubyte>
36 static GLint Internal (GLuint theChannels)
55 struct OpenGl_TextureFormatSelector<GLushort>
57 static GLint Internal (GLuint theChannels)
76 struct OpenGl_TextureFormatSelector<GLfloat>
78 static GLint Internal (GLuint theChannels)
96 //! Stores parameters of OpenGL texture format.
97 class OpenGl_TextureFormat
99 friend class OpenGl_Texture;
103 //! Returns OpenGL format of the pixel data.
104 inline GLenum Format() const
121 //! Returns OpenGL internal format of the pixel data.
122 inline GLint Internal() const
127 //! Returns texture format for specified type and number of channels.
128 template<class T, int N>
129 static OpenGl_TextureFormat Create()
131 return OpenGl_TextureFormat (N, OpenGl_TextureFormatSelector<T>::Internal (N));
136 //! Creates new texture format.
137 OpenGl_TextureFormat (const GLint theChannels,
138 const GLint theInternal)
139 : myInternal (theInternal),
140 myChannels (theChannels) {}
144 GLint myInternal; //!< OpenGL internal format of the pixel data
145 GLint myChannels; //!< Number of channels for each pixel (from 1 to 4)
149 class OpenGl_Texture;
150 DEFINE_STANDARD_HANDLE(OpenGl_Texture, OpenGl_Resource)
152 //! Texture resource.
153 class OpenGl_Texture : public OpenGl_Resource
158 //! Helpful constants
159 static const GLuint NO_TEXTURE = 0;
163 //! Create uninitialized VBO.
164 Standard_EXPORT OpenGl_Texture (const Handle(Graphic3d_TextureParams)& theParams = NULL);
167 Standard_EXPORT virtual ~OpenGl_Texture();
169 //! @return true if current object was initialized
170 inline bool IsValid() const
172 return myTextureId != NO_TEXTURE;
175 //! @return target to which the texture is bound (GL_TEXTURE_1D, GL_TEXTURE_2D)
176 inline GLenum GetTarget() const
181 //! @return texture width (0 LOD)
182 inline GLsizei SizeX() const
187 //! @return texture height (0 LOD)
188 inline GLsizei SizeY() const
193 //! @return texture ID
194 inline GLuint TextureId() const
199 //! @return texture format (not sized)
200 inline GLenum GetFormat() const
205 //! Return true for GL_RED and GL_ALPHA formats.
211 //! Setup to interprete the format as Alpha by Shader Manager
212 //! (should be GL_ALPHA within compatible context or GL_RED otherwise).
213 void SetAlpha (const bool theValue)
215 myIsAlpha = theValue;
218 //! Creates Texture id if not yet generated.
219 //! Data should be initialized by another method.
220 Standard_EXPORT bool Create (const Handle(OpenGl_Context)& theCtx);
222 //! Destroy object - will release GPU memory if any.
223 Standard_EXPORT virtual void Release (OpenGl_Context* theCtx);
225 //! Bind this Texture to specified unit.
226 Standard_EXPORT void Bind (const Handle(OpenGl_Context)& theCtx,
227 const GLenum theTextureUnit = GL_TEXTURE0) const;
229 //! Unbind texture from specified unit.
230 Standard_EXPORT void Unbind (const Handle(OpenGl_Context)& theCtx,
231 const GLenum theTextureUnit = GL_TEXTURE0) const;
233 //! Notice that texture will be unbound after this call.
234 Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theCtx,
235 const Image_PixMap& theImage,
236 const Graphic3d_TypeOfTexture theType);
238 //! Initialize the texture with specified format, size and texture type.
239 //! If theImage is empty the texture data will contain trash.
240 //! Notice that texture will be unbound after this call.
241 Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theCtx,
242 const GLint theTextFormat,
243 const GLenum thePixelFormat,
244 const GLenum theDataType,
245 const GLsizei theSizeX,
246 const GLsizei theSizeY,
247 const Graphic3d_TypeOfTexture theType,
248 const Image_PixMap* theImage = NULL);
250 //! Initialize the 2D multisampling texture using glTexImage2DMultisample().
251 Standard_EXPORT bool Init2DMultisample (const Handle(OpenGl_Context)& theCtx,
252 const GLsizei theNbSamples,
253 const GLint theTextFormat,
254 const GLsizei theSizeX,
255 const GLsizei theSizeY);
257 //! Allocates texture rectangle with specified format and size.
258 //! \note Texture data is not initialized (will contain trash).
259 Standard_EXPORT bool InitRectangle (const Handle(OpenGl_Context)& theCtx,
260 const Standard_Integer theSizeX,
261 const Standard_Integer theSizeY,
262 const OpenGl_TextureFormat& theFormat);
264 //! @return true if texture was generated within mipmaps
265 Standard_EXPORT const Standard_Boolean HasMipmaps() const;
267 //! @return assigned texture parameters (not necessary applied)
268 Standard_EXPORT const Handle(Graphic3d_TextureParams)& GetParams() const;
270 //! @param texture parameters
271 Standard_EXPORT void SetParams (const Handle(Graphic3d_TextureParams)& theParams);
273 //! Return texture type and format by Image_PixMap data format.
274 Standard_EXPORT static bool GetDataFormat (const Handle(OpenGl_Context)& theCtx,
275 const Image_PixMap& theData,
276 GLint& theTextFormat,
277 GLenum& thePixelFormat,
278 GLenum& theDataType);
282 GLuint myTextureId; //!< GL resource ID
283 GLenum myTarget; //!< GL_TEXTURE_1D/GL_TEXTURE_2D
284 GLsizei mySizeX; //!< texture width
285 GLsizei mySizeY; //!< texture height
286 GLenum myTextFormat; //!< texture format - GL_RGB, GL_RGBA,...
287 Standard_Boolean myHasMipmaps; //!< flag indicates that texture was uploaded with mipmaps
288 bool myIsAlpha; //!< indicates alpha format
290 Handle(Graphic3d_TextureParams) myParams; //!< texture parameters
294 DEFINE_STANDARD_RTTI(OpenGl_Texture, OpenGl_Resource) // Type definition
298 #endif // _OpenGl_Texture_H__