0024307: TKOpenGl - efficient culling of large number of presentations
[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 <NCollection_StdAllocator.hxx>
22
23 struct OpenGl_ElementNode;
24 class  OpenGl_Group;
25 class  OpenGl_Structure;
26 class  OpenGl_PrimitiveArray;
27
28 namespace OpenGl_Raytrace
29 {
30   //! Checks to see if the group contains ray-trace geometry.
31   Standard_Boolean IsRaytracedGroup (const OpenGl_Group* theGroup);
32
33   //! Checks to see if the element contains ray-trace geometry.
34   Standard_Boolean IsRaytracedElement (const OpenGl_ElementNode* theNode);
35
36   //! Checks to see if the structure contains ray-trace geometry.
37   Standard_Boolean IsRaytracedStructure (const OpenGl_Structure* theStructure);
38 }
39
40 //! Stores properties of surface material.
41 class OpenGl_RaytraceMaterial
42 {
43 public:
44
45   //! Ambient reflection coefficient.
46   BVH_Vec4f Ambient;
47
48   //! Diffuse reflection coefficient.
49   BVH_Vec4f Diffuse;
50
51   //! Glossy reflection coefficient.
52   BVH_Vec4f Specular;
53
54   //! Material emission.
55   BVH_Vec4f Emission;
56
57   //! Specular reflection coefficient.
58   BVH_Vec4f Reflection;
59
60   //! Specular refraction coefficient.
61   BVH_Vec4f Refraction;
62
63   //! Material transparency.
64   BVH_Vec4f Transparency;
65
66 public:
67
68   //! Creates new default material.
69   OpenGl_RaytraceMaterial();
70
71   //! Creates new material with specified properties.
72   OpenGl_RaytraceMaterial (const BVH_Vec4f& theAmbient,
73                            const BVH_Vec4f& theDiffuse,
74                            const BVH_Vec4f& theSpecular);
75
76   //! Creates new material with specified properties.
77   OpenGl_RaytraceMaterial (const BVH_Vec4f& theAmbient,
78                            const BVH_Vec4f& theDiffuse,
79                            const BVH_Vec4f& theSpecular,
80                            const BVH_Vec4f& theEmission,
81                            const BVH_Vec4f& theTranspar);
82
83   //! Creates new material with specified properties.
84   OpenGl_RaytraceMaterial (const BVH_Vec4f& theAmbient,
85                            const BVH_Vec4f& theDiffuse,
86                            const BVH_Vec4f& theSpecular,
87                            const BVH_Vec4f& theEmission,
88                            const BVH_Vec4f& theTranspar,
89                            const BVH_Vec4f& theReflection,
90                            const BVH_Vec4f& theRefraction);
91
92   //! Returns packed (serialized) representation of material.
93   const Standard_ShortReal* Packed()
94   {
95     return reinterpret_cast<Standard_ShortReal*> (this);
96   }
97 };
98
99 //! Stores properties of OpenGL light source.
100 class OpenGl_RaytraceLight
101 {
102 public:
103
104   //! Diffuse intensity (in terms of OpenGL).
105   BVH_Vec4f Diffuse;
106
107   //! Position of light source (in terms of OpenGL).
108   BVH_Vec4f Position;
109
110 public:
111
112   //! Creates new light source.
113   OpenGl_RaytraceLight (const BVH_Vec4f& theDiffuse,
114                         const BVH_Vec4f& thePosition);
115
116   //! Returns packed (serialized) representation of light source.
117   const Standard_ShortReal* Packed()
118   {
119     return reinterpret_cast<Standard_ShortReal*> (this);
120   }
121 };
122
123 //! Triangulation of single OpenGL primitive array.
124 class OpenGl_TriangleSet : public BVH_Triangulation<Standard_ShortReal, 4>
125 {
126 public:
127
128   //! Value of invalid material index to return in case of errors.
129   static const Standard_Integer INVALID_MATERIAL = -1;
130
131 public:
132
133   //! Creates new OpenGL element triangulation.
134   OpenGl_TriangleSet (const OpenGl_PrimitiveArray* theArray = NULL)
135   : BVH_Triangulation<Standard_ShortReal, 4>(),
136     myArray (theArray)
137    {
138      //
139    }
140
141   //! Releases resources of OpenGL element triangulation.
142   ~OpenGl_TriangleSet()
143   {
144     //
145   }
146
147   //! Returns associated OpenGl structure.
148   const OpenGl_PrimitiveArray* AssociatedPArray() const
149   {
150     return myArray;
151   }
152
153   //! Returns material index of triangle set.
154   Standard_Integer MaterialIndex() const
155   {
156     if (Elements.size() == 0)
157       return INVALID_MATERIAL;
158
159     return Elements.front().w();
160   }
161
162   //! Sets material index for entire triangle set.
163   void SetMaterialIndex (Standard_Integer aMatID)
164   {
165     for (Standard_Size anIdx = 0; anIdx < Elements.size(); ++anIdx)
166       Elements[anIdx].w() = aMatID;
167   }
168
169   //! Returns AABB of primitive set.
170   BVH_BoxNt Box() const
171   {
172     const BVH_Transform<Standard_ShortReal, 4>* aTransform = 
173       dynamic_cast<const BVH_Transform<Standard_ShortReal, 4>* > (Properties().operator->());
174  
175     BVH_BoxNt aBox = BVH_PrimitiveSet<Standard_ShortReal, 4>::Box(); 
176  
177     if (aTransform)
178     {
179       return aTransform->Apply (aBox);
180     }
181  
182     return aBox;
183   }
184
185 public:
186
187   BVH_Array4f Normals; //!< Array of vertex normals.
188  
189 private:
190
191   const OpenGl_PrimitiveArray* myArray; //!< Reference to associated OpenGl structure.
192
193 };
194
195 //! Stores geometry of ray-tracing scene.
196 class OpenGl_RaytraceGeometry : public BVH_Geometry<Standard_ShortReal, 4>
197 {
198 public:
199
200   //! Value of invalid offset to return in case of errors.
201   static const Standard_Integer INVALID_OFFSET = -1;
202
203 public:
204
205   //! Array of properties of light sources.
206   std::vector<OpenGl_RaytraceLight,
207     NCollection_StdAllocator<OpenGl_RaytraceLight> > Sources;
208
209   //! Array of 'front' material properties.
210   std::vector<OpenGl_RaytraceMaterial,
211     NCollection_StdAllocator<OpenGl_RaytraceMaterial> > Materials;
212
213   //! Global ambient from all light sources.
214   BVH_Vec4f Ambient;
215
216 public:
217
218   //! Creates uninitialized ray-tracing geometry.
219   OpenGl_RaytraceGeometry()
220   : BVH_Geometry<Standard_ShortReal, 4>(),
221     myHighLevelTreeDepth (0),
222     myBottomLevelTreeDepth (0)
223   {
224     //
225   }
226
227   //! Releases resources of ray-tracing geometry.
228   ~OpenGl_RaytraceGeometry()
229   {
230     //
231   }
232
233   //! Clears ray-tracing geometry.
234   void Clear();
235
236   //! Clears only ray-tracing materials.
237   void ClearMaterials()
238   {
239     std::vector<OpenGl_RaytraceMaterial,
240       NCollection_StdAllocator<OpenGl_RaytraceMaterial> > anEmptyMaterials;
241  
242     Materials.swap (anEmptyMaterials);
243   }
244
245 public:
246
247   //! Performs post-processing of high-level scene BVH.
248   Standard_Boolean ProcessAcceleration();
249
250   //! Returns offset of bottom-level BVH for given leaf node.
251   //! If the node index is not valid the function returns -1.
252   //! @note Can be used after processing acceleration structure.
253   Standard_Integer AccelerationOffset (Standard_Integer theNodeIdx);
254
255   //! Returns offset of triangulation vertices for given leaf node.
256   //! If the node index is not valid the function returns -1.
257   //! @note Can be used after processing acceleration structure.
258   Standard_Integer VerticesOffset (Standard_Integer theNodeIdx);
259
260   //! Returns offset of triangulation elements for given leaf node.
261   //! If the node index is not valid the function returns -1.
262   //! @note Can be used after processing acceleration structure.
263   Standard_Integer ElementsOffset (Standard_Integer theNodeIdx);
264
265   //! Returns triangulation data for given leaf node.
266   //! If the node index is not valid the function returns NULL.
267   //! @note Can be used after processing acceleration structure.
268   OpenGl_TriangleSet* TriangleSet (Standard_Integer theNodeIdx);
269
270   //! Returns depth of high-level scene BVH from last build.
271   Standard_Integer HighLevelTreeDepth() const
272   {
273     return myHighLevelTreeDepth;
274   }
275
276   //! Returns maximum depth of bottom-level scene BVHs from last build.
277   Standard_Integer BottomLevelTreeDepth() const
278   {
279     return myBottomLevelTreeDepth;
280   }
281
282 protected:
283
284   Standard_Integer myHighLevelTreeDepth;   //!< Depth of high-level scene BVH from last build
285   Standard_Integer myBottomLevelTreeDepth; //!< Maximum depth of bottom-level scene BVHs from last build
286
287 };
288
289 #endif