0027232: Configuration - fix mblen missing building issue on Android
[occt.git] / src / OpenGl / OpenGl_SceneGeometry.hxx
1 // Created on: 2013-08-27
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifndef _OpenGl_SceneGeometry_Header
17 #define _OpenGl_SceneGeometry_Header
18
19 #include <BVH_Geometry.hxx>
20 #include <BVH_Triangulation.hxx>
21 #include <BVH_BinnedBuilder.hxx>
22 #include <NCollection_StdAllocator.hxx>
23 #include <OpenGl_TextureBufferArb.hxx>
24 #include <OpenGl_Texture.hxx>
25 #include <OpenGl_Sampler.hxx>
26
27 class  OpenGl_Element;
28 struct OpenGl_ElementNode;
29 class  OpenGl_Group;
30 class  OpenGl_Structure;
31 class  OpenGl_PrimitiveArray;
32
33 namespace OpenGl_Raytrace
34 {
35   //! Checks to see if the group contains ray-trace geometry.
36   Standard_Boolean IsRaytracedGroup (const OpenGl_Group* theGroup);
37
38   //! Checks to see if the element contains ray-trace geometry.
39   Standard_Boolean IsRaytracedElement (const OpenGl_ElementNode* theNode);
40
41   //! Checks to see if the element contains ray-trace geometry.
42   Standard_Boolean IsRaytracedElement (const OpenGl_Element* theElement);
43
44   //! Checks to see if the structure contains ray-trace geometry.
45   Standard_Boolean IsRaytracedStructure (const OpenGl_Structure* theStructure);
46 }
47
48 //! Stores properties of surface material.
49 class OpenGl_RaytraceMaterial
50 {
51 public:
52
53   //! Ambient reflection coefficient.
54   BVH_Vec4f Ambient;
55
56   //! Diffuse reflection coefficient.
57   BVH_Vec4f Diffuse;
58
59   //! Glossy reflection coefficient.
60   BVH_Vec4f Specular;
61
62   //! Material emission.
63   BVH_Vec4f Emission;
64
65   //! Specular reflection coefficient.
66   BVH_Vec4f Reflection;
67
68   //! Specular refraction coefficient.
69   BVH_Vec4f Refraction;
70
71   //! Material transparency.
72   BVH_Vec4f Transparency;
73
74   //! Texture transformation matrix.
75   BVH_Mat4f TextureTransform;
76
77   //! Physically-based material properties (used in path tracing engine).
78   struct Physical
79   {
80     //! Weight of the diffuse BRDF.
81     BVH_Vec4f Kd;
82
83     //! Weight of the reflection BRDF.
84     BVH_Vec4f Kr;
85
86     //! Weight of the transmission BTDF.
87     BVH_Vec4f Kt;
88
89     //! Weight of the Blinn's glossy BRDF.
90     BVH_Vec4f Ks;
91
92     //! Self-emitted radiance.
93     BVH_Vec4f Le;
94
95     //! Fresnel coefficients.
96     BVH_Vec4f Fresnel;
97
98     //! Absorption color for the transmission BSDF.
99     BVH_Vec4f Absorption;
100
101   } BSDF;
102
103 public:
104
105   //! Creates new default material.
106   OpenGl_RaytraceMaterial();
107
108   //! Creates new material with specified properties.
109   OpenGl_RaytraceMaterial (const BVH_Vec4f& theAmbient,
110                            const BVH_Vec4f& theDiffuse,
111                            const BVH_Vec4f& theSpecular);
112
113   //! Creates new material with specified properties.
114   OpenGl_RaytraceMaterial (const BVH_Vec4f& theAmbient,
115                            const BVH_Vec4f& theDiffuse,
116                            const BVH_Vec4f& theSpecular,
117                            const BVH_Vec4f& theEmission,
118                            const BVH_Vec4f& theTranspar);
119
120   //! Creates new material with specified properties.
121   OpenGl_RaytraceMaterial (const BVH_Vec4f& theAmbient,
122                            const BVH_Vec4f& theDiffuse,
123                            const BVH_Vec4f& theSpecular,
124                            const BVH_Vec4f& theEmission,
125                            const BVH_Vec4f& theTranspar,
126                            const BVH_Vec4f& theReflection,
127                            const BVH_Vec4f& theRefraction);
128
129   //! Returns packed (serialized) representation of material.
130   const Standard_ShortReal* Packed()
131   {
132     return reinterpret_cast<Standard_ShortReal*> (this);
133   }
134 };
135
136 //! Stores properties of OpenGL light source.
137 class OpenGl_RaytraceLight
138 {
139 public:
140
141   //! Diffuse intensity (in terms of OpenGL).
142   BVH_Vec4f Diffuse;
143
144   //! Position of light source (in terms of OpenGL).
145   BVH_Vec4f Position;
146
147 public:
148
149   //! Creates new light source.
150   OpenGl_RaytraceLight (const BVH_Vec4f& theDiffuse,
151                         const BVH_Vec4f& thePosition);
152
153   //! Returns packed (serialized) representation of light source.
154   const Standard_ShortReal* Packed()
155   {
156     return reinterpret_cast<Standard_ShortReal*> (this);
157   }
158 };
159
160 //! Triangulation of single OpenGL primitive array.
161 class OpenGl_TriangleSet : public BVH_Triangulation<Standard_ShortReal, 3>
162 {
163 public:
164
165   //! Value of invalid material index to return in case of errors.
166   static const Standard_Integer INVALID_MATERIAL = -1;
167
168 public:
169
170   //! Creates new OpenGL element triangulation.
171   OpenGl_TriangleSet (const Standard_Size theArrayID);
172
173   //! Releases resources of OpenGL element triangulation.
174   ~OpenGl_TriangleSet()
175   {
176     //
177   }
178
179   //! Returns Id of associated primitive array.
180   Standard_Size AssociatedPArrayID() const
181   {
182     return myArrayID;
183   }
184
185   //! Returns material index of triangle set.
186   Standard_Integer MaterialIndex() const
187   {
188     if (Elements.size() == 0)
189     {
190       return INVALID_MATERIAL;
191     }
192
193     return Elements.front().w();
194   }
195
196   //! Sets material index for entire triangle set.
197   void SetMaterialIndex (Standard_Integer theMatID)
198   {
199     for (Standard_Size anIdx = 0; anIdx < Elements.size(); ++anIdx)
200     {
201       Elements[anIdx].w() = theMatID;
202     }
203   }
204
205   //! Returns AABB of the given object.
206   using BVH_Triangulation<Standard_ShortReal, 3>::Box;
207
208   //! Returns AABB of primitive set.
209   BVH_BoxNt Box() const;
210
211   //! Returns centroid position along the given axis.
212   Standard_ShortReal Center (const Standard_Integer theIndex, const Standard_Integer theAxis) const;
213
214 public:
215
216   BVH_Array3f Normals; //!< Array of vertex normals.
217
218   BVH_Array2f TexCrds; //!< Array of vertex UV coords.
219
220 private:
221
222   Standard_Size myArrayID; //!< ID of associated primitive array.
223
224 };
225
226 //! Stores geometry of ray-tracing scene.
227 class OpenGl_RaytraceGeometry : public BVH_Geometry<Standard_ShortReal, 3>
228 {
229 public:
230
231   //! Value of invalid offset to return in case of errors.
232   static const Standard_Integer INVALID_OFFSET = -1;
233
234   //! Maximum number of textures used in ray-tracing shaders.
235   //! This is not restriction of the solution implemented, but
236   //! rather the reasonable limit of the number of textures in
237   //! various applications (can be increased if needed).
238   static const Standard_Integer MAX_TEX_NUMBER = 32;
239
240 public:
241
242   //! Array of properties of light sources.
243   std::vector<OpenGl_RaytraceLight,
244     NCollection_StdAllocator<OpenGl_RaytraceLight> > Sources;
245
246   //! Array of 'front' material properties.
247   std::vector<OpenGl_RaytraceMaterial,
248     NCollection_StdAllocator<OpenGl_RaytraceMaterial> > Materials;
249
250   //! Global ambient from all light sources.
251   BVH_Vec4f Ambient;
252
253 public:
254
255   //! Creates uninitialized ray-tracing geometry.
256   OpenGl_RaytraceGeometry()
257   : BVH_Geometry<Standard_ShortReal, 3>(),
258     myHighLevelTreeDepth (0),
259     myBottomLevelTreeDepth (0)
260   {
261     //
262   }
263
264   //! Releases resources of ray-tracing geometry.
265   ~OpenGl_RaytraceGeometry()
266   {
267     //
268   }
269
270   //! Clears only ray-tracing materials.
271   void ClearMaterials()
272   {
273     std::vector<OpenGl_RaytraceMaterial,
274       NCollection_StdAllocator<OpenGl_RaytraceMaterial> > anEmptyMaterials;
275
276     Materials.swap (anEmptyMaterials);
277
278     myTextures.Clear();
279   }
280
281   //! Clears ray-tracing geometry.
282   void Clear();
283
284 public: //! @name methods related to acceleration structure
285
286   //! Performs post-processing of high-level scene BVH.
287   Standard_Boolean ProcessAcceleration();
288
289   //! Returns offset of bottom-level BVH for given leaf node.
290   //! If the node index is not valid the function returns -1.
291   //! @note Can be used after processing acceleration structure.
292   Standard_Integer AccelerationOffset (Standard_Integer theNodeIdx);
293
294   //! Returns offset of triangulation vertices for given leaf node.
295   //! If the node index is not valid the function returns -1.
296   //! @note Can be used after processing acceleration structure.
297   Standard_Integer VerticesOffset (Standard_Integer theNodeIdx);
298
299   //! Returns offset of triangulation elements for given leaf node.
300   //! If the node index is not valid the function returns -1.
301   //! @note Can be used after processing acceleration structure.
302   Standard_Integer ElementsOffset (Standard_Integer theNodeIdx);
303
304   //! Returns triangulation data for given leaf node.
305   //! If the node index is not valid the function returns NULL.
306   //! @note Can be used after processing acceleration structure.
307   OpenGl_TriangleSet* TriangleSet (Standard_Integer theNodeIdx);
308
309 public: //! @name methods related to texture management
310
311   //! Adds new OpenGL texture to the scene and returns its index.
312   Standard_Integer AddTexture (const Handle(OpenGl_Texture)& theTexture);
313
314   //! Updates unique 64-bit texture handles to use in shaders.
315   Standard_Boolean UpdateTextureHandles (const Handle(OpenGl_Context)& theContext);
316
317   //! Makes the OpenGL texture handles resident (must be called before using).
318   Standard_Boolean AcquireTextures (const Handle(OpenGl_Context)& theContext) const;
319
320   //! Makes the OpenGL texture handles non-resident (must be called after using).
321   Standard_Boolean ReleaseTextures (const Handle(OpenGl_Context)& theContext) const;
322
323   //! Returns array of texture handles.
324   const std::vector<GLuint64>& TextureHandles() const
325   {
326     return myTextureHandles;
327   }
328
329   //! Checks if scene contains textured objects.
330   Standard_Boolean HasTextures() const
331   {
332     return !myTextures.IsEmpty();
333   }
334
335   //! Releases OpenGL resources.
336   void ReleaseResources (const Handle(OpenGl_Context)& theContext)
337   {
338     if (!myTextureSampler.IsNull())
339     {
340       myTextureSampler->Release (theContext.operator->());
341       myTextureSampler.Nullify();
342     }
343   }
344
345 public: //! @name auxiliary methods
346
347   //! Returns depth of high-level scene BVH from last build.
348   Standard_Integer HighLevelTreeDepth() const
349   {
350     return myHighLevelTreeDepth;
351   }
352
353   //! Returns maximum depth of bottom-level scene BVHs from last build.
354   Standard_Integer BottomLevelTreeDepth() const
355   {
356     return myBottomLevelTreeDepth;
357   }
358
359 protected:
360
361   NCollection_Vector<Handle(OpenGl_Texture)> myTextures;             //!< Array of texture maps shared between rendered objects
362   Handle(OpenGl_Sampler)                     myTextureSampler;       //!< Sampler object providing fixed sampling params for texures
363   std::vector<GLuint64>                      myTextureHandles;       //!< Array of unique 64-bit texture handles obtained from OpenGL
364   Standard_Integer                           myHighLevelTreeDepth;   //!< Depth of high-level scene BVH from last build
365   Standard_Integer                           myBottomLevelTreeDepth; //!< Maximum depth of bottom-level scene BVHs from last build
366
367 };
368
369 #endif