0025133: TKOpenGl - Crash on closing a view containing presentations with capping
[occt.git] / src / OpenGl / OpenGl_Texture.hxx
CommitLineData
bf75be98 1// Created by: Kirill GAVRILOV
d5f74e42 2// Copyright (c) 2013-2014 OPEN CASCADE SAS
bf75be98 3//
973c2be1 4// This file is part of Open CASCADE Technology software library.
bf75be98 5//
d5f74e42 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
973c2be1 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.
bf75be98 11//
973c2be1 12// Alternatively, this file may be used under the terms of Open CASCADE
13// commercial license or contractual agreement.
bf75be98 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
24class Handle(OpenGl_Context);
25class OpenGl_Context;
26class Image_PixMap;
27
68333c8f 28//! Selects preferable texture format for specified parameters.
29template<class T>
30struct OpenGl_TextureFormatSelector
31{
32 // Not implemented
33};
34
35template<>
36struct 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
56template<>
57struct 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
77template<>
78struct 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.
99class OpenGl_TextureFormat
100{
101 friend class OpenGl_Texture;
102
103public:
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
136private:
137
138 //! Creates new texture format.
5322131b 139 OpenGl_TextureFormat (const GLint theChannels,
140 const GLint theInternal)
141 : myInternal (theInternal),
142 myChannels (theChannels) {}
68333c8f 143
144private:
145
5322131b 146 GLint myInternal; //!< OpenGL internal format of the pixel data
147 GLint myChannels; //!< Number of channels for each pixel (from 1 to 4)
68333c8f 148
68333c8f 149};
150
bf75be98 151//! Texture resource.
152class OpenGl_Texture : public OpenGl_Resource
153{
154
155public:
156
157 //! Helpful constants
158 static const GLuint NO_TEXTURE = 0;
159
160public:
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
a174a3c5 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
a577aaab 198 //! @return texture format
199 inline GLint GetFormat() const
200 {
201 return myTextFormat;
202 }
203
bf75be98 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.
10b9c7df 209 Standard_EXPORT virtual void Release (OpenGl_Context* theCtx);
bf75be98 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
18f4e8e2 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
68333c8f 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
bf75be98 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
18f4e8e2 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
bf75be98 259protected:
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
270public:
271
272 DEFINE_STANDARD_RTTI(OpenGl_Texture) // Type definition
273
274};
275
276#endif // _OpenGl_Texture_H__