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 | |
077a220c |
18 | #include <Graphic3d_CubeMap.hxx> |
ba00aab7 |
19 | #include <OpenGl_TextureFormat.hxx> |
cc8cbabe |
20 | #include <OpenGl_NamedResource.hxx> |
21 | #include <OpenGl_Sampler.hxx> |
cc8cbabe |
22 | #include <Graphic3d_TextureUnit.hxx> |
077a220c |
23 | #include <Graphic3d_TypeOfTexture.hxx> |
bf75be98 |
24 | |
c04c30b3 |
25 | class Graphic3d_TextureParams; |
bf75be98 |
26 | class Image_PixMap; |
27 | |
28 | //! Texture resource. |
cc8cbabe |
29 | class OpenGl_Texture : public OpenGl_NamedResource |
bf75be98 |
30 | { |
cc8cbabe |
31 | DEFINE_STANDARD_RTTIEXT(OpenGl_Texture, OpenGl_NamedResource) |
bf75be98 |
32 | public: |
33 | |
34 | //! Helpful constants |
35 | static const GLuint NO_TEXTURE = 0; |
36 | |
15669413 |
37 | //! Return pixel size of pixel format in bytes. |
38 | //! Note that this method considers that OpenGL natively supports this pixel format, |
39 | //! which might be not the case - in the latter case, actual pixel size might differ! |
40 | Standard_EXPORT static Standard_Size PixelSizeOfPixelFormat (Standard_Integer theInternalFormat); |
41 | |
bf75be98 |
42 | public: |
43 | |
cc8cbabe |
44 | //! Create uninitialized texture. |
45 | Standard_EXPORT OpenGl_Texture (const TCollection_AsciiString& theResourceId = TCollection_AsciiString(), |
46 | const Handle(Graphic3d_TextureParams)& theParams = Handle(Graphic3d_TextureParams)()); |
bf75be98 |
47 | |
48 | //! Destroy object. |
49 | Standard_EXPORT virtual ~OpenGl_Texture(); |
50 | |
51 | //! @return true if current object was initialized |
ba00aab7 |
52 | bool IsValid() const { return myTextureId != NO_TEXTURE; } |
bf75be98 |
53 | |
54 | //! @return target to which the texture is bound (GL_TEXTURE_1D, GL_TEXTURE_2D) |
ba00aab7 |
55 | GLenum GetTarget() const { return myTarget; } |
bf75be98 |
56 | |
a174a3c5 |
57 | //! @return texture width (0 LOD) |
ba00aab7 |
58 | GLsizei SizeX() const { return mySizeX; } |
a174a3c5 |
59 | |
60 | //! @return texture height (0 LOD) |
ba00aab7 |
61 | GLsizei SizeY() const { return mySizeY; } |
a174a3c5 |
62 | |
63 | //! @return texture ID |
ba00aab7 |
64 | GLuint TextureId() const { return myTextureId; } |
a174a3c5 |
65 | |
8625ef7e |
66 | //! @return texture format (not sized) |
ba00aab7 |
67 | GLenum GetFormat() const { return myTextFormat; } |
4bcd0738 |
68 | |
69 | //! @return texture format (sized) |
ba00aab7 |
70 | GLint SizedFormat() const { return mySizedFormat; } |
a577aaab |
71 | |
4e1523ef |
72 | //! Return true for GL_RED and GL_ALPHA formats. |
ba00aab7 |
73 | bool IsAlpha() const { return myIsAlpha; } |
4e1523ef |
74 | |
75 | //! Setup to interprete the format as Alpha by Shader Manager |
76 | //! (should be GL_ALPHA within compatible context or GL_RED otherwise). |
ba00aab7 |
77 | void SetAlpha (const bool theValue) { myIsAlpha = theValue; } |
4e1523ef |
78 | |
bf75be98 |
79 | //! Creates Texture id if not yet generated. |
80 | //! Data should be initialized by another method. |
81 | Standard_EXPORT bool Create (const Handle(OpenGl_Context)& theCtx); |
82 | |
83 | //! Destroy object - will release GPU memory if any. |
79104795 |
84 | Standard_EXPORT virtual void Release (OpenGl_Context* theCtx) Standard_OVERRIDE; |
bf75be98 |
85 | |
cc8cbabe |
86 | //! Return texture sampler. |
87 | const Handle(OpenGl_Sampler)& Sampler() const { return mySampler; } |
88 | |
89 | //! Set texture sampler. |
90 | void SetSampler (const Handle(OpenGl_Sampler)& theSampler) { mySampler = theSampler; } |
91 | |
92 | //! Initialize the Sampler Object (as OpenGL object). |
93 | //! @param theCtx currently bound OpenGL context |
94 | Standard_EXPORT bool InitSamplerObject (const Handle(OpenGl_Context)& theCtx); |
95 | |
96 | //! Bind this Texture to the unit specified in sampler parameters. |
97 | //! Also binds Sampler Object if it is allocated. |
98 | void Bind (const Handle(OpenGl_Context)& theCtx) const |
99 | { |
100 | Bind (theCtx, mySampler->Parameters()->TextureUnit()); |
101 | } |
102 | |
103 | //! Unbind texture from the unit specified in sampler parameters. |
104 | //! Also unbinds Sampler Object if it is allocated. |
105 | void Unbind (const Handle(OpenGl_Context)& theCtx) const |
106 | { |
107 | Unbind (theCtx, mySampler->Parameters()->TextureUnit()); |
108 | } |
109 | |
bf75be98 |
110 | //! Bind this Texture to specified unit. |
cc8cbabe |
111 | //! Also binds Sampler Object if it is allocated. |
bf75be98 |
112 | Standard_EXPORT void Bind (const Handle(OpenGl_Context)& theCtx, |
cc8cbabe |
113 | const Graphic3d_TextureUnit theTextureUnit) const; |
bf75be98 |
114 | |
115 | //! Unbind texture from specified unit. |
cc8cbabe |
116 | //! Also unbinds Sampler Object if it is allocated. |
bf75be98 |
117 | Standard_EXPORT void Unbind (const Handle(OpenGl_Context)& theCtx, |
cc8cbabe |
118 | const Graphic3d_TextureUnit theTextureUnit) const; |
bf75be98 |
119 | |
d2edda76 |
120 | //! Revision of associated data source. |
121 | Standard_Size Revision() const { return myRevision; } |
122 | |
123 | //! Set revision of associated data source. |
124 | void SetRevision (const Standard_Size theRevision) { myRevision = theRevision; } |
125 | |
bf75be98 |
126 | //! Notice that texture will be unbound after this call. |
127 | Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theCtx, |
128 | const Image_PixMap& theImage, |
ba00aab7 |
129 | const Graphic3d_TypeOfTexture theType, |
130 | const Standard_Boolean theIsColorMap); |
bf75be98 |
131 | |
18f4e8e2 |
132 | //! Initialize the texture with specified format, size and texture type. |
133 | //! If theImage is empty the texture data will contain trash. |
134 | //! Notice that texture will be unbound after this call. |
135 | Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theCtx, |
ba00aab7 |
136 | const OpenGl_TextureFormat& theFormat, |
137 | const Graphic3d_Vec2i& theSizeXY, |
18f4e8e2 |
138 | const Graphic3d_TypeOfTexture theType, |
139 | const Image_PixMap* theImage = NULL); |
140 | |
077a220c |
141 | //! Initialize the texture with Graphic3d_TextureMap. |
142 | //! It is an universal way to initialize. |
143 | //! Sitable initialization method will be chosen. |
144 | Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theCtx, |
145 | const Handle(Graphic3d_TextureMap)& theTextureMap); |
146 | |
3c4b62a4 |
147 | //! Initialize the 2D multisampling texture using glTexImage2DMultisample(). |
148 | Standard_EXPORT bool Init2DMultisample (const Handle(OpenGl_Context)& theCtx, |
149 | const GLsizei theNbSamples, |
150 | const GLint theTextFormat, |
151 | const GLsizei theSizeX, |
152 | const GLsizei theSizeY); |
153 | |
68333c8f |
154 | //! Allocates texture rectangle with specified format and size. |
155 | //! \note Texture data is not initialized (will contain trash). |
156 | Standard_EXPORT bool InitRectangle (const Handle(OpenGl_Context)& theCtx, |
157 | const Standard_Integer theSizeX, |
158 | const Standard_Integer theSizeY, |
159 | const OpenGl_TextureFormat& theFormat); |
160 | |
74fb257d |
161 | //! Initializes 3D texture rectangle with specified format and size. |
162 | Standard_EXPORT bool Init3D (const Handle(OpenGl_Context)& theCtx, |
ba00aab7 |
163 | const OpenGl_TextureFormat& theFormat, |
164 | const Graphic3d_Vec3i& theSizeXYZ, |
74fb257d |
165 | const void* thePixels); |
166 | |
bf75be98 |
167 | //! @return true if texture was generated within mipmaps |
cc8cbabe |
168 | Standard_Boolean HasMipmaps() const { return myHasMipmaps; } |
bf75be98 |
169 | |
ba00aab7 |
170 | //! Returns estimated GPU memory usage for holding data without considering overheads and allocation alignment rules. |
171 | Standard_EXPORT virtual Standard_Size EstimatedDataSize() const Standard_OVERRIDE; |
172 | |
173 | //! Returns TRUE for point sprite texture. |
174 | virtual bool IsPointSprite() const { return false; } |
18f4e8e2 |
175 | |
ba00aab7 |
176 | public: |
177 | |
178 | Standard_DEPRECATED("Deprecated method, OpenGl_TextureFormat::FindFormat() should be used instead") |
179 | static bool GetDataFormat (const Handle(OpenGl_Context)& theCtx, |
180 | const Image_Format theFormat, |
181 | GLint& theTextFormat, |
182 | GLenum& thePixelFormat, |
183 | GLenum& theDataType) |
184 | { |
185 | OpenGl_TextureFormat aFormat = OpenGl_TextureFormat::FindFormat (theCtx, theFormat, false); |
186 | theTextFormat = aFormat.InternalFormat(); |
187 | thePixelFormat = aFormat.PixelFormat(); |
188 | theDataType = aFormat.DataType(); |
189 | return aFormat.IsValid(); |
190 | } |
191 | |
192 | Standard_DEPRECATED("Deprecated method, OpenGl_TextureFormat::FindFormat() should be used instead") |
077a220c |
193 | static bool GetDataFormat (const Handle(OpenGl_Context)& theCtx, |
194 | const Image_PixMap& theData, |
195 | GLint& theTextFormat, |
196 | GLenum& thePixelFormat, |
197 | GLenum& theDataType) |
198 | { |
ba00aab7 |
199 | OpenGl_TextureFormat aFormat = OpenGl_TextureFormat::FindFormat (theCtx, theData.Format(), false); |
200 | theTextFormat = aFormat.InternalFormat(); |
201 | thePixelFormat = aFormat.PixelFormat(); |
202 | theDataType = aFormat.DataType(); |
203 | return aFormat.IsValid(); |
077a220c |
204 | } |
205 | |
ba00aab7 |
206 | Standard_DEPRECATED("Deprecated method, OpenGl_TextureFormat should be passed instead of separate parameters") |
207 | bool Init (const Handle(OpenGl_Context)& theCtx, |
208 | const GLint theTextFormat, |
209 | const GLenum thePixelFormat, |
210 | const GLenum theDataType, |
211 | const GLsizei theSizeX, |
212 | const GLsizei theSizeY, |
213 | const Graphic3d_TypeOfTexture theType, |
214 | const Image_PixMap* theImage = NULL) |
215 | { |
216 | OpenGl_TextureFormat aFormat; |
217 | aFormat.SetInternalFormat (theTextFormat); |
218 | aFormat.SetPixelFormat (thePixelFormat); |
219 | aFormat.SetDataType (theDataType); |
220 | return Init (theCtx, aFormat, Graphic3d_Vec2i (theSizeX, theSizeY), theType, theImage); |
221 | } |
15669413 |
222 | |
ba00aab7 |
223 | Standard_DEPRECATED("Deprecated method, theIsColorMap parameter should be explicitly specified") |
224 | bool Init (const Handle(OpenGl_Context)& theCtx, |
225 | const Image_PixMap& theImage, |
226 | const Graphic3d_TypeOfTexture theType) |
227 | { |
228 | return Init (theCtx, theImage, theType, true); |
229 | } |
230 | |
231 | Standard_DEPRECATED("Deprecated method, OpenGl_TextureFormat should be passed instead of separate parameters") |
232 | bool Init3D (const Handle(OpenGl_Context)& theCtx, |
233 | const GLint theTextFormat, |
234 | const GLenum thePixelFormat, |
235 | const GLenum theDataType, |
236 | const Standard_Integer theSizeX, |
237 | const Standard_Integer theSizeY, |
238 | const Standard_Integer theSizeZ, |
239 | const void* thePixels) |
240 | { |
241 | OpenGl_TextureFormat aFormat; |
242 | aFormat.SetInternalFormat (theTextFormat); |
243 | aFormat.SetPixelFormat (thePixelFormat); |
244 | aFormat.SetDataType (theDataType); |
245 | return Init3D (theCtx, aFormat, Graphic3d_Vec3i (theSizeX, theSizeY, theSizeZ), thePixels); |
246 | } |
737e9a8d |
247 | |
ba00aab7 |
248 | //! Initializes 6 sides of cubemap. |
249 | //! If theCubeMap is not NULL then size and format will be taken from it and corresponding arguments will be ignored. |
250 | //! Otherwise this parametres will be taken from arguments. |
251 | //! @param theCtx [in] active OpenGL context |
252 | //! @param theCubeMap [in] cubemap definition, can be NULL |
253 | //! @param theSize [in] cubemap dimensions |
254 | //! @param theFormat [in] image format |
255 | //! @param theToGenMipmap [in] flag to generate mipmaped cubemap |
256 | //! @param theIsColorMap [in] flag indicating cubemap storing color values |
67312b79 |
257 | Standard_EXPORT bool InitCubeMap (const Handle(OpenGl_Context)& theCtx, |
ba00aab7 |
258 | const Handle(Graphic3d_CubeMap)& theCubeMap, |
259 | Standard_Size theSize, |
260 | Image_Format theFormat, |
261 | Standard_Boolean theToGenMipmap, |
262 | Standard_Boolean theIsColorMap); |
263 | |
67312b79 |
264 | protected: |
265 | |
266 | //! Apply default sampler parameters after texture creation. |
267 | Standard_EXPORT void applyDefaultSamplerParams (const Handle(OpenGl_Context)& theCtx); |
268 | |
cc8cbabe |
269 | protected: |
270 | |
271 | Handle(OpenGl_Sampler) mySampler; //!< texture sampler |
d2edda76 |
272 | Standard_Size myRevision; //!< revision of associated data source |
bf75be98 |
273 | GLuint myTextureId; //!< GL resource ID |
74fb257d |
274 | GLenum myTarget; //!< GL_TEXTURE_1D/GL_TEXTURE_2D/GL_TEXTURE_3D |
bf75be98 |
275 | GLsizei mySizeX; //!< texture width |
276 | GLsizei mySizeY; //!< texture height |
74fb257d |
277 | GLsizei mySizeZ; //!< texture depth |
8625ef7e |
278 | GLenum myTextFormat; //!< texture format - GL_RGB, GL_RGBA,... |
15669413 |
279 | GLint mySizedFormat;//!< internal (sized) texture format |
280 | Standard_Integer myNbSamples; //!< number of MSAA samples |
bf75be98 |
281 | Standard_Boolean myHasMipmaps; //!< flag indicates that texture was uploaded with mipmaps |
4e1523ef |
282 | bool myIsAlpha; //!< indicates alpha format |
bf75be98 |
283 | |
bf75be98 |
284 | }; |
285 | |
cc8cbabe |
286 | DEFINE_STANDARD_HANDLE(OpenGl_Texture, OpenGl_NamedResource) |
287 | |
bf75be98 |
288 | #endif // _OpenGl_Texture_H__ |