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> |
bf75be98 |
20 | #include <Graphic3d_TypeOfTexture.hxx> |
bf75be98 |
21 | |
bf75be98 |
22 | class OpenGl_Context; |
c04c30b3 |
23 | class Graphic3d_TextureParams; |
bf75be98 |
24 | class Image_PixMap; |
25 | |
68333c8f |
26 | //! Selects preferable texture format for specified parameters. |
27 | template<class T> |
28 | struct OpenGl_TextureFormatSelector |
29 | { |
30 | // Not implemented |
31 | }; |
32 | |
33 | template<> |
34 | struct OpenGl_TextureFormatSelector<GLubyte> |
35 | { |
36 | static GLint Internal (GLuint theChannels) |
37 | { |
38 | switch (theChannels) |
39 | { |
40 | case 1: |
41 | return GL_R8; |
42 | case 2: |
43 | return GL_RG8; |
44 | case 3: |
45 | return GL_RGB8; |
46 | case 4: |
47 | return GL_RGBA8; |
48 | default: |
49 | return GL_NONE; |
50 | } |
51 | } |
fbef84f9 |
52 | |
53 | static GLint DataType() |
54 | { |
55 | return GL_UNSIGNED_BYTE; |
56 | } |
68333c8f |
57 | }; |
58 | |
59 | template<> |
60 | struct OpenGl_TextureFormatSelector<GLushort> |
61 | { |
62 | static GLint Internal (GLuint theChannels) |
63 | { |
64 | switch (theChannels) |
65 | { |
66 | case 1: |
67 | return GL_R16; |
68 | case 2: |
69 | return GL_RG16; |
70 | case 3: |
71 | return GL_RGB16; |
72 | case 4: |
73 | return GL_RGBA16; |
74 | default: |
75 | return GL_NONE; |
76 | } |
77 | } |
fbef84f9 |
78 | |
79 | static GLint DataType() |
80 | { |
81 | return GL_UNSIGNED_SHORT; |
82 | } |
68333c8f |
83 | }; |
84 | |
85 | template<> |
86 | struct OpenGl_TextureFormatSelector<GLfloat> |
87 | { |
88 | static GLint Internal (GLuint theChannels) |
89 | { |
90 | switch (theChannels) |
91 | { |
92 | case 1: |
93 | return GL_R32F; |
94 | case 2: |
95 | return GL_RG32F; |
96 | case 3: |
97 | return GL_RGB32F; |
98 | case 4: |
99 | return GL_RGBA32F; |
100 | default: |
101 | return GL_NONE; |
102 | } |
103 | } |
fbef84f9 |
104 | |
105 | static GLint DataType() |
106 | { |
107 | return GL_FLOAT; |
108 | } |
109 | }; |
110 | |
111 | template<> |
112 | struct OpenGl_TextureFormatSelector<GLuint> |
113 | { |
114 | static GLint Internal (GLuint theChannels) |
115 | { |
116 | switch (theChannels) |
117 | { |
118 | case 1: |
119 | return GL_RED; |
120 | case 2: |
121 | return GL_RG; |
122 | case 3: |
123 | return GL_RGB; |
124 | case 4: |
125 | return GL_RGBA; |
126 | default: |
127 | return GL_NONE; |
128 | } |
129 | } |
130 | |
131 | static GLint DataType() |
132 | { |
133 | return GL_UNSIGNED_INT; |
134 | } |
135 | }; |
136 | |
137 | //! Only unsigned formats are available in OpenGL ES 2.0 |
138 | #if !defined(GL_ES_VERSION_2_0) |
139 | template<> |
140 | struct OpenGl_TextureFormatSelector<GLbyte> |
141 | { |
142 | static GLint Internal (GLuint theChannels) |
143 | { |
144 | switch (theChannels) |
145 | { |
146 | case 1: |
147 | return GL_R8_SNORM; |
148 | case 2: |
149 | return GL_RG8_SNORM; |
150 | case 3: |
151 | return GL_RGB8_SNORM; |
152 | case 4: |
153 | return GL_RGBA8_SNORM; |
154 | default: |
155 | return GL_NONE; |
156 | } |
157 | } |
158 | |
159 | static GLint DataType() |
160 | { |
161 | return GL_BYTE; |
162 | } |
163 | }; |
164 | |
165 | template<> |
166 | struct OpenGl_TextureFormatSelector<GLshort> |
167 | { |
168 | static GLint Internal (GLuint theChannels) |
169 | { |
170 | switch (theChannels) |
171 | { |
172 | case 1: |
173 | return GL_R16_SNORM; |
174 | case 2: |
175 | return GL_RG16_SNORM; |
176 | case 3: |
177 | return GL_RGB16_SNORM; |
178 | case 4: |
179 | return GL_RGBA16_SNORM; |
180 | default: |
181 | return GL_NONE; |
182 | } |
183 | } |
184 | |
185 | static GLint DataType() |
186 | { |
187 | return GL_SHORT; |
188 | } |
189 | }; |
190 | |
191 | template<> |
192 | struct OpenGl_TextureFormatSelector<GLint> |
193 | { |
194 | static GLint Internal (GLuint theChannels) |
195 | { |
196 | switch (theChannels) |
197 | { |
198 | case 1: |
199 | return GL_RED_SNORM; |
200 | case 2: |
201 | return GL_RG_SNORM; |
202 | case 3: |
203 | return GL_RGB_SNORM; |
204 | case 4: |
205 | return GL_RGBA_SNORM; |
206 | default: |
207 | return GL_NONE; |
208 | } |
209 | } |
210 | |
211 | static GLint DataType() |
212 | { |
213 | return GL_INT; |
214 | } |
68333c8f |
215 | }; |
fbef84f9 |
216 | #endif |
68333c8f |
217 | |
218 | //! Stores parameters of OpenGL texture format. |
219 | class OpenGl_TextureFormat |
220 | { |
221 | friend class OpenGl_Texture; |
222 | |
223 | public: |
224 | |
225 | //! Returns OpenGL format of the pixel data. |
226 | inline GLenum Format() const |
227 | { |
228 | switch (myChannels) |
229 | { |
230 | case 1: |
231 | return GL_RED; |
232 | case 2: |
233 | return GL_RG; |
234 | case 3: |
235 | return GL_RGB; |
236 | case 4: |
237 | return GL_RGBA; |
238 | default: |
239 | return GL_NONE; |
240 | } |
241 | } |
242 | |
243 | //! Returns OpenGL internal format of the pixel data. |
244 | inline GLint Internal() const |
245 | { |
246 | return myInternal; |
247 | } |
248 | |
fbef84f9 |
249 | //! Returns OpenGL data type of the pixel data. |
250 | inline GLint DataType() const |
251 | { |
252 | return myDataType; |
253 | } |
254 | |
68333c8f |
255 | //! Returns texture format for specified type and number of channels. |
256 | template<class T, int N> |
257 | static OpenGl_TextureFormat Create() |
258 | { |
fbef84f9 |
259 | return OpenGl_TextureFormat (N, |
260 | OpenGl_TextureFormatSelector<T>::Internal(N), |
261 | OpenGl_TextureFormatSelector<T>::DataType()); |
68333c8f |
262 | } |
263 | |
264 | private: |
265 | |
266 | //! Creates new texture format. |
5322131b |
267 | OpenGl_TextureFormat (const GLint theChannels, |
fbef84f9 |
268 | const GLint theInternal, |
269 | const GLint theDataType) |
5322131b |
270 | : myInternal (theInternal), |
fbef84f9 |
271 | myChannels (theChannels), |
272 | myDataType (theDataType) {} |
68333c8f |
273 | |
274 | private: |
275 | |
5322131b |
276 | GLint myInternal; //!< OpenGL internal format of the pixel data |
277 | GLint myChannels; //!< Number of channels for each pixel (from 1 to 4) |
fbef84f9 |
278 | GLint myDataType; //!< OpenGL data type of input pixel data |
68333c8f |
279 | |
68333c8f |
280 | }; |
281 | |
c04c30b3 |
282 | class OpenGl_Texture; |
283 | DEFINE_STANDARD_HANDLE(OpenGl_Texture, OpenGl_Resource) |
284 | |
bf75be98 |
285 | //! Texture resource. |
286 | class OpenGl_Texture : public OpenGl_Resource |
287 | { |
288 | |
289 | public: |
290 | |
291 | //! Helpful constants |
292 | static const GLuint NO_TEXTURE = 0; |
293 | |
294 | public: |
295 | |
296 | //! Create uninitialized VBO. |
297 | Standard_EXPORT OpenGl_Texture (const Handle(Graphic3d_TextureParams)& theParams = NULL); |
298 | |
299 | //! Destroy object. |
300 | Standard_EXPORT virtual ~OpenGl_Texture(); |
301 | |
302 | //! @return true if current object was initialized |
303 | inline bool IsValid() const |
304 | { |
305 | return myTextureId != NO_TEXTURE; |
306 | } |
307 | |
308 | //! @return target to which the texture is bound (GL_TEXTURE_1D, GL_TEXTURE_2D) |
309 | inline GLenum GetTarget() const |
310 | { |
311 | return myTarget; |
312 | } |
313 | |
a174a3c5 |
314 | //! @return texture width (0 LOD) |
315 | inline GLsizei SizeX() const |
316 | { |
317 | return mySizeX; |
318 | } |
319 | |
320 | //! @return texture height (0 LOD) |
321 | inline GLsizei SizeY() const |
322 | { |
323 | return mySizeY; |
324 | } |
325 | |
326 | //! @return texture ID |
327 | inline GLuint TextureId() const |
328 | { |
329 | return myTextureId; |
330 | } |
331 | |
8625ef7e |
332 | //! @return texture format (not sized) |
333 | inline GLenum GetFormat() const |
a577aaab |
334 | { |
335 | return myTextFormat; |
336 | } |
337 | |
4e1523ef |
338 | //! Return true for GL_RED and GL_ALPHA formats. |
339 | bool IsAlpha() const |
340 | { |
341 | return myIsAlpha; |
342 | } |
343 | |
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) |
347 | { |
348 | myIsAlpha = theValue; |
349 | } |
350 | |
bf75be98 |
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); |
354 | |
355 | //! Destroy object - will release GPU memory if any. |
79104795 |
356 | Standard_EXPORT virtual void Release (OpenGl_Context* theCtx) Standard_OVERRIDE; |
bf75be98 |
357 | |
358 | //! Bind this Texture to specified unit. |
359 | Standard_EXPORT void Bind (const Handle(OpenGl_Context)& theCtx, |
360 | const GLenum theTextureUnit = GL_TEXTURE0) const; |
361 | |
362 | //! Unbind texture from specified unit. |
363 | Standard_EXPORT void Unbind (const Handle(OpenGl_Context)& theCtx, |
364 | const GLenum theTextureUnit = GL_TEXTURE0) const; |
365 | |
366 | //! Notice that texture will be unbound after this call. |
367 | Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theCtx, |
368 | const Image_PixMap& theImage, |
369 | const Graphic3d_TypeOfTexture theType); |
370 | |
18f4e8e2 |
371 | //! Initialize the texture with specified format, size and texture type. |
372 | //! If theImage is empty the texture data will contain trash. |
373 | //! Notice that texture will be unbound after this call. |
374 | Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theCtx, |
375 | const GLint theTextFormat, |
376 | const GLenum thePixelFormat, |
377 | const GLenum theDataType, |
378 | const GLsizei theSizeX, |
379 | const GLsizei theSizeY, |
380 | const Graphic3d_TypeOfTexture theType, |
381 | const Image_PixMap* theImage = NULL); |
382 | |
3c4b62a4 |
383 | //! Initialize the 2D multisampling texture using glTexImage2DMultisample(). |
384 | Standard_EXPORT bool Init2DMultisample (const Handle(OpenGl_Context)& theCtx, |
385 | const GLsizei theNbSamples, |
386 | const GLint theTextFormat, |
387 | const GLsizei theSizeX, |
388 | const GLsizei theSizeY); |
389 | |
68333c8f |
390 | //! Allocates texture rectangle with specified format and size. |
391 | //! \note Texture data is not initialized (will contain trash). |
392 | Standard_EXPORT bool InitRectangle (const Handle(OpenGl_Context)& theCtx, |
393 | const Standard_Integer theSizeX, |
394 | const Standard_Integer theSizeY, |
395 | const OpenGl_TextureFormat& theFormat); |
396 | |
bf75be98 |
397 | //! @return true if texture was generated within mipmaps |
487bf1ce |
398 | Standard_EXPORT Standard_Boolean HasMipmaps() const; |
bf75be98 |
399 | |
400 | //! @return assigned texture parameters (not necessary applied) |
401 | Standard_EXPORT const Handle(Graphic3d_TextureParams)& GetParams() const; |
402 | |
403 | //! @param texture parameters |
404 | Standard_EXPORT void SetParams (const Handle(Graphic3d_TextureParams)& theParams); |
405 | |
18f4e8e2 |
406 | //! Return texture type and format by Image_PixMap data format. |
407 | Standard_EXPORT static bool GetDataFormat (const Handle(OpenGl_Context)& theCtx, |
408 | const Image_PixMap& theData, |
409 | GLint& theTextFormat, |
410 | GLenum& thePixelFormat, |
411 | GLenum& theDataType); |
412 | |
bf75be98 |
413 | protected: |
414 | |
415 | GLuint myTextureId; //!< GL resource ID |
416 | GLenum myTarget; //!< GL_TEXTURE_1D/GL_TEXTURE_2D |
417 | GLsizei mySizeX; //!< texture width |
418 | GLsizei mySizeY; //!< texture height |
8625ef7e |
419 | GLenum myTextFormat; //!< texture format - GL_RGB, GL_RGBA,... |
bf75be98 |
420 | Standard_Boolean myHasMipmaps; //!< flag indicates that texture was uploaded with mipmaps |
4e1523ef |
421 | bool myIsAlpha; //!< indicates alpha format |
bf75be98 |
422 | |
423 | Handle(Graphic3d_TextureParams) myParams; //!< texture parameters |
424 | |
425 | public: |
426 | |
ec357c5c |
427 | DEFINE_STANDARD_RTTI(OpenGl_Texture, OpenGl_Resource) // Type definition |
bf75be98 |
428 | |
429 | }; |
430 | |
431 | #endif // _OpenGl_Texture_H__ |