0024001: Stereographic rendering support
[occt.git] / src / OpenGl / OpenGl_Texture.hxx
... / ...
CommitLineData
1// Created by: Kirill GAVRILOV
2// Copyright (c) 2014 OPEN CASCADE SAS
3//
4// This file is part of Open CASCADE Technology software library.
5//
6// This library is free software; you can redistribute it and / or modify it
7// under the terms of the GNU Lesser General Public 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.
11//
12// Alternatively, this file may be used under the terms of Open CASCADE
13// commercial license or contractual agreement.
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
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.
139 OpenGl_TextureFormat (const GLint theChannels, const GLint theInternal)
140 : myChannels (theChannels),
141 myInternal (theInternal)
142 {
143 //
144 }
145
146private:
147
148 //! OpenGL internal format of the pixel data.
149 GLint myInternal;
150
151 //! Number of channels for each pixel (from 1 to 4).
152 GLint myChannels;
153};
154
155//! Texture resource.
156class OpenGl_Texture : public OpenGl_Resource
157{
158
159public:
160
161 //! Helpful constants
162 static const GLuint NO_TEXTURE = 0;
163
164public:
165
166 //! Create uninitialized VBO.
167 Standard_EXPORT OpenGl_Texture (const Handle(Graphic3d_TextureParams)& theParams = NULL);
168
169 //! Destroy object.
170 Standard_EXPORT virtual ~OpenGl_Texture();
171
172 //! @return true if current object was initialized
173 inline bool IsValid() const
174 {
175 return myTextureId != NO_TEXTURE;
176 }
177
178 //! @return target to which the texture is bound (GL_TEXTURE_1D, GL_TEXTURE_2D)
179 inline GLenum GetTarget() const
180 {
181 return myTarget;
182 }
183
184 //! @return texture width (0 LOD)
185 inline GLsizei SizeX() const
186 {
187 return mySizeX;
188 }
189
190 //! @return texture height (0 LOD)
191 inline GLsizei SizeY() const
192 {
193 return mySizeY;
194 }
195
196 //! @return texture ID
197 inline GLuint TextureId() const
198 {
199 return myTextureId;
200 }
201
202 //! @return texture format
203 inline GLint GetFormat() const
204 {
205 return myTextFormat;
206 }
207
208 //! Creates Texture id if not yet generated.
209 //! Data should be initialized by another method.
210 Standard_EXPORT bool Create (const Handle(OpenGl_Context)& theCtx);
211
212 //! Destroy object - will release GPU memory if any.
213 Standard_EXPORT virtual void Release (const OpenGl_Context* theCtx);
214
215 //! Bind this Texture to specified unit.
216 Standard_EXPORT void Bind (const Handle(OpenGl_Context)& theCtx,
217 const GLenum theTextureUnit = GL_TEXTURE0) const;
218
219 //! Unbind texture from specified unit.
220 Standard_EXPORT void Unbind (const Handle(OpenGl_Context)& theCtx,
221 const GLenum theTextureUnit = GL_TEXTURE0) const;
222
223 //! Notice that texture will be unbound after this call.
224 Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theCtx,
225 const Image_PixMap& theImage,
226 const Graphic3d_TypeOfTexture theType);
227
228 //! Allocates texture rectangle with specified format and size.
229 //! \note Texture data is not initialized (will contain trash).
230 Standard_EXPORT bool InitRectangle (const Handle(OpenGl_Context)& theCtx,
231 const Standard_Integer theSizeX,
232 const Standard_Integer theSizeY,
233 const OpenGl_TextureFormat& theFormat);
234
235 //! @return true if texture was generated within mipmaps
236 Standard_EXPORT const Standard_Boolean HasMipmaps() const;
237
238 //! @return assigned texture parameters (not necessary applied)
239 Standard_EXPORT const Handle(Graphic3d_TextureParams)& GetParams() const;
240
241 //! @param texture parameters
242 Standard_EXPORT void SetParams (const Handle(Graphic3d_TextureParams)& theParams);
243
244protected:
245
246 GLuint myTextureId; //!< GL resource ID
247 GLenum myTarget; //!< GL_TEXTURE_1D/GL_TEXTURE_2D
248 GLsizei mySizeX; //!< texture width
249 GLsizei mySizeY; //!< texture height
250 GLint myTextFormat; //!< texture format - GL_RGB, GL_RGBA,...
251 Standard_Boolean myHasMipmaps; //!< flag indicates that texture was uploaded with mipmaps
252
253 Handle(Graphic3d_TextureParams) myParams; //!< texture parameters
254
255public:
256
257 DEFINE_STANDARD_RTTI(OpenGl_Texture) // Type definition
258
259};
260
261#endif // _OpenGl_Texture_H__