0026799: NCollection_Array2's RowLength and ColLength return wrong value.
[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>
bf75be98 20#include <Graphic3d_TypeOfTexture.hxx>
bf75be98 21
bf75be98 22class OpenGl_Context;
c04c30b3 23class Graphic3d_TextureParams;
bf75be98 24class Image_PixMap;
25
68333c8f 26//! Selects preferable texture format for specified parameters.
27template<class T>
28struct OpenGl_TextureFormatSelector
29{
30 // Not implemented
31};
32
33template<>
34struct 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 }
52};
53
54template<>
55struct OpenGl_TextureFormatSelector<GLushort>
56{
57 static GLint Internal (GLuint theChannels)
58 {
59 switch (theChannels)
60 {
61 case 1:
62 return GL_R16;
63 case 2:
64 return GL_RG16;
65 case 3:
66 return GL_RGB16;
67 case 4:
68 return GL_RGBA16;
69 default:
70 return GL_NONE;
71 }
72 }
73};
74
75template<>
76struct OpenGl_TextureFormatSelector<GLfloat>
77{
78 static GLint Internal (GLuint theChannels)
79 {
80 switch (theChannels)
81 {
82 case 1:
83 return GL_R32F;
84 case 2:
85 return GL_RG32F;
86 case 3:
87 return GL_RGB32F;
88 case 4:
89 return GL_RGBA32F;
90 default:
91 return GL_NONE;
92 }
93 }
94};
95
96//! Stores parameters of OpenGL texture format.
97class OpenGl_TextureFormat
98{
99 friend class OpenGl_Texture;
100
101public:
102
103 //! Returns OpenGL format of the pixel data.
104 inline GLenum Format() const
105 {
106 switch (myChannels)
107 {
108 case 1:
109 return GL_RED;
110 case 2:
111 return GL_RG;
112 case 3:
113 return GL_RGB;
114 case 4:
115 return GL_RGBA;
116 default:
117 return GL_NONE;
118 }
119 }
120
121 //! Returns OpenGL internal format of the pixel data.
122 inline GLint Internal() const
123 {
124 return myInternal;
125 }
126
127 //! Returns texture format for specified type and number of channels.
128 template<class T, int N>
129 static OpenGl_TextureFormat Create()
130 {
131 return OpenGl_TextureFormat (N, OpenGl_TextureFormatSelector<T>::Internal (N));
132 }
133
134private:
135
136 //! Creates new texture format.
5322131b 137 OpenGl_TextureFormat (const GLint theChannels,
138 const GLint theInternal)
139 : myInternal (theInternal),
140 myChannels (theChannels) {}
68333c8f 141
142private:
143
5322131b 144 GLint myInternal; //!< OpenGL internal format of the pixel data
145 GLint myChannels; //!< Number of channels for each pixel (from 1 to 4)
68333c8f 146
68333c8f 147};
148
c04c30b3 149class OpenGl_Texture;
150DEFINE_STANDARD_HANDLE(OpenGl_Texture, OpenGl_Resource)
151
bf75be98 152//! Texture resource.
153class OpenGl_Texture : public OpenGl_Resource
154{
155
156public:
157
158 //! Helpful constants
159 static const GLuint NO_TEXTURE = 0;
160
161public:
162
163 //! Create uninitialized VBO.
164 Standard_EXPORT OpenGl_Texture (const Handle(Graphic3d_TextureParams)& theParams = NULL);
165
166 //! Destroy object.
167 Standard_EXPORT virtual ~OpenGl_Texture();
168
169 //! @return true if current object was initialized
170 inline bool IsValid() const
171 {
172 return myTextureId != NO_TEXTURE;
173 }
174
175 //! @return target to which the texture is bound (GL_TEXTURE_1D, GL_TEXTURE_2D)
176 inline GLenum GetTarget() const
177 {
178 return myTarget;
179 }
180
a174a3c5 181 //! @return texture width (0 LOD)
182 inline GLsizei SizeX() const
183 {
184 return mySizeX;
185 }
186
187 //! @return texture height (0 LOD)
188 inline GLsizei SizeY() const
189 {
190 return mySizeY;
191 }
192
193 //! @return texture ID
194 inline GLuint TextureId() const
195 {
196 return myTextureId;
197 }
198
8625ef7e 199 //! @return texture format (not sized)
200 inline GLenum GetFormat() const
a577aaab 201 {
202 return myTextFormat;
203 }
204
4e1523ef 205 //! Return true for GL_RED and GL_ALPHA formats.
206 bool IsAlpha() const
207 {
208 return myIsAlpha;
209 }
210
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)
214 {
215 myIsAlpha = theValue;
216 }
217
bf75be98 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);
221
222 //! Destroy object - will release GPU memory if any.
10b9c7df 223 Standard_EXPORT virtual void Release (OpenGl_Context* theCtx);
bf75be98 224
225 //! Bind this Texture to specified unit.
226 Standard_EXPORT void Bind (const Handle(OpenGl_Context)& theCtx,
227 const GLenum theTextureUnit = GL_TEXTURE0) const;
228
229 //! Unbind texture from specified unit.
230 Standard_EXPORT void Unbind (const Handle(OpenGl_Context)& theCtx,
231 const GLenum theTextureUnit = GL_TEXTURE0) const;
232
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);
237
18f4e8e2 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);
249
68333c8f 250 //! Allocates texture rectangle with specified format and size.
251 //! \note Texture data is not initialized (will contain trash).
252 Standard_EXPORT bool InitRectangle (const Handle(OpenGl_Context)& theCtx,
253 const Standard_Integer theSizeX,
254 const Standard_Integer theSizeY,
255 const OpenGl_TextureFormat& theFormat);
256
bf75be98 257 //! @return true if texture was generated within mipmaps
258 Standard_EXPORT const Standard_Boolean HasMipmaps() const;
259
260 //! @return assigned texture parameters (not necessary applied)
261 Standard_EXPORT const Handle(Graphic3d_TextureParams)& GetParams() const;
262
263 //! @param texture parameters
264 Standard_EXPORT void SetParams (const Handle(Graphic3d_TextureParams)& theParams);
265
18f4e8e2 266 //! Return texture type and format by Image_PixMap data format.
267 Standard_EXPORT static bool GetDataFormat (const Handle(OpenGl_Context)& theCtx,
268 const Image_PixMap& theData,
269 GLint& theTextFormat,
270 GLenum& thePixelFormat,
271 GLenum& theDataType);
272
bf75be98 273protected:
274
275 GLuint myTextureId; //!< GL resource ID
276 GLenum myTarget; //!< GL_TEXTURE_1D/GL_TEXTURE_2D
277 GLsizei mySizeX; //!< texture width
278 GLsizei mySizeY; //!< texture height
8625ef7e 279 GLenum myTextFormat; //!< texture format - GL_RGB, GL_RGBA,...
bf75be98 280 Standard_Boolean myHasMipmaps; //!< flag indicates that texture was uploaded with mipmaps
4e1523ef 281 bool myIsAlpha; //!< indicates alpha format
bf75be98 282
283 Handle(Graphic3d_TextureParams) myParams; //!< texture parameters
284
285public:
286
ec357c5c 287 DEFINE_STANDARD_RTTI(OpenGl_Texture, OpenGl_Resource) // Type definition
bf75be98 288
289};
290
291#endif // _OpenGl_Texture_H__