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)
53 static GLint DataType()
55 return GL_UNSIGNED_BYTE;
60 struct OpenGl_TextureFormatSelector<GLushort>
62 static GLint Internal (GLuint theChannels)
79 static GLint DataType()
81 return GL_UNSIGNED_SHORT;
86 struct OpenGl_TextureFormatSelector<GLfloat>
88 static GLint Internal (GLuint theChannels)
105 static GLint DataType()
112 struct OpenGl_TextureFormatSelector<GLuint>
114 static GLint Internal (GLuint theChannels)
131 static GLint DataType()
133 return GL_UNSIGNED_INT;
137 //! Only unsigned formats are available in OpenGL ES 2.0
138 #if !defined(GL_ES_VERSION_2_0)
140 struct OpenGl_TextureFormatSelector<GLbyte>
142 static GLint Internal (GLuint theChannels)
151 return GL_RGB8_SNORM;
153 return GL_RGBA8_SNORM;
159 static GLint DataType()
166 struct OpenGl_TextureFormatSelector<GLshort>
168 static GLint Internal (GLuint theChannels)
175 return GL_RG16_SNORM;
177 return GL_RGB16_SNORM;
179 return GL_RGBA16_SNORM;
185 static GLint DataType()
192 struct OpenGl_TextureFormatSelector<GLint>
194 static GLint Internal (GLuint theChannels)
205 return GL_RGBA_SNORM;
211 static GLint DataType()
218 //! Stores parameters of OpenGL texture format.
219 class OpenGl_TextureFormat
221 friend class OpenGl_Texture;
225 //! Returns OpenGL format of the pixel data.
226 inline GLenum Format() const
243 //! Returns OpenGL internal format of the pixel data.
244 inline GLint Internal() const
249 //! Returns OpenGL data type of the pixel data.
250 inline GLint DataType() const
255 //! Returns texture format for specified type and number of channels.
256 template<class T, int N>
257 static OpenGl_TextureFormat Create()
259 return OpenGl_TextureFormat (N,
260 OpenGl_TextureFormatSelector<T>::Internal(N),
261 OpenGl_TextureFormatSelector<T>::DataType());
266 //! Creates new texture format.
267 OpenGl_TextureFormat (const GLint theChannels,
268 const GLint theInternal,
269 const GLint theDataType)
270 : myInternal (theInternal),
271 myChannels (theChannels),
272 myDataType (theDataType) {}
276 GLint myInternal; //!< OpenGL internal format of the pixel data
277 GLint myChannels; //!< Number of channels for each pixel (from 1 to 4)
278 GLint myDataType; //!< OpenGL data type of input pixel data
282 class OpenGl_Texture;
283 DEFINE_STANDARD_HANDLE(OpenGl_Texture, OpenGl_Resource)
285 //! Texture resource.
286 class OpenGl_Texture : public OpenGl_Resource
291 //! Helpful constants
292 static const GLuint NO_TEXTURE = 0;
296 //! Create uninitialized VBO.
297 Standard_EXPORT OpenGl_Texture (const Handle(Graphic3d_TextureParams)& theParams = NULL);
300 Standard_EXPORT virtual ~OpenGl_Texture();
302 //! @return true if current object was initialized
303 inline bool IsValid() const
305 return myTextureId != NO_TEXTURE;
308 //! @return target to which the texture is bound (GL_TEXTURE_1D, GL_TEXTURE_2D)
309 inline GLenum GetTarget() const
314 //! @return texture width (0 LOD)
315 inline GLsizei SizeX() const
320 //! @return texture height (0 LOD)
321 inline GLsizei SizeY() const
326 //! @return texture ID
327 inline GLuint TextureId() const
332 //! @return texture format (not sized)
333 inline GLenum GetFormat() const
338 //! Return true for GL_RED and GL_ALPHA formats.
344 //! Setup to interprete the format as Alpha by Shader Manager
345 //! (should be GL_ALPHA within compatible context or GL_RED otherwise).
346 void SetAlpha (const bool theValue)
348 myIsAlpha = theValue;
351 //! Creates Texture id if not yet generated.
352 //! Data should be initialized by another method.
353 Standard_EXPORT bool Create (const Handle(OpenGl_Context)& theCtx);
355 //! Destroy object - will release GPU memory if any.
356 Standard_EXPORT virtual void Release (OpenGl_Context* theCtx) Standard_OVERRIDE;
358 //! Bind this Texture to specified unit.
359 Standard_EXPORT void Bind (const Handle(OpenGl_Context)& theCtx,
360 const GLenum theTextureUnit = GL_TEXTURE0) const;
362 //! Unbind texture from specified unit.
363 Standard_EXPORT void Unbind (const Handle(OpenGl_Context)& theCtx,
364 const GLenum theTextureUnit = GL_TEXTURE0) const;
366 //! Revision of associated data source.
367 Standard_Size Revision() const { return myRevision; }
369 //! Set revision of associated data source.
370 void SetRevision (const Standard_Size theRevision) { myRevision = theRevision; }
372 //! Notice that texture will be unbound after this call.
373 Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theCtx,
374 const Image_PixMap& theImage,
375 const Graphic3d_TypeOfTexture theType);
377 //! Initialize the texture with specified format, size and texture type.
378 //! If theImage is empty the texture data will contain trash.
379 //! Notice that texture will be unbound after this call.
380 Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theCtx,
381 const GLint theTextFormat,
382 const GLenum thePixelFormat,
383 const GLenum theDataType,
384 const GLsizei theSizeX,
385 const GLsizei theSizeY,
386 const Graphic3d_TypeOfTexture theType,
387 const Image_PixMap* theImage = NULL);
389 //! Initialize the 2D multisampling texture using glTexImage2DMultisample().
390 Standard_EXPORT bool Init2DMultisample (const Handle(OpenGl_Context)& theCtx,
391 const GLsizei theNbSamples,
392 const GLint theTextFormat,
393 const GLsizei theSizeX,
394 const GLsizei theSizeY);
396 //! Allocates texture rectangle with specified format and size.
397 //! \note Texture data is not initialized (will contain trash).
398 Standard_EXPORT bool InitRectangle (const Handle(OpenGl_Context)& theCtx,
399 const Standard_Integer theSizeX,
400 const Standard_Integer theSizeY,
401 const OpenGl_TextureFormat& theFormat);
403 //! Initializes 3D texture rectangle with specified format and size.
404 Standard_EXPORT bool Init3D (const Handle(OpenGl_Context)& theCtx,
405 const GLint theTextFormat,
406 const GLenum thePixelFormat,
407 const GLenum theDataType,
408 const Standard_Integer theSizeX,
409 const Standard_Integer theSizeY,
410 const Standard_Integer theSizeZ,
411 const void* thePixels);
413 //! @return true if texture was generated within mipmaps
414 Standard_EXPORT Standard_Boolean HasMipmaps() const;
416 //! @return assigned texture parameters (not necessary applied)
417 Standard_EXPORT const Handle(Graphic3d_TextureParams)& GetParams() const;
419 //! @param texture parameters
420 Standard_EXPORT void SetParams (const Handle(Graphic3d_TextureParams)& theParams);
422 //! Return texture type and format by Image_PixMap data format.
423 Standard_EXPORT static bool GetDataFormat (const Handle(OpenGl_Context)& theCtx,
424 const Image_PixMap& theData,
425 GLint& theTextFormat,
426 GLenum& thePixelFormat,
427 GLenum& theDataType);
431 Standard_Size myRevision; //!< revision of associated data source
432 GLuint myTextureId; //!< GL resource ID
433 GLenum myTarget; //!< GL_TEXTURE_1D/GL_TEXTURE_2D/GL_TEXTURE_3D
434 GLsizei mySizeX; //!< texture width
435 GLsizei mySizeY; //!< texture height
436 GLsizei mySizeZ; //!< texture depth
437 GLenum myTextFormat; //!< texture format - GL_RGB, GL_RGBA,...
438 Standard_Boolean myHasMipmaps; //!< flag indicates that texture was uploaded with mipmaps
439 bool myIsAlpha; //!< indicates alpha format
441 Handle(Graphic3d_TextureParams) myParams; //!< texture parameters
445 DEFINE_STANDARD_RTTIEXT(OpenGl_Texture,OpenGl_Resource) // Type definition
449 #endif // _OpenGl_Texture_H__