0024428: Implementation of LGPL license
[occt.git] / src / OpenGl / OpenGl_SceneGeometry.hxx
1 // Created on: 2013-08-27
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2013 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
8 // under the terms of the GNU Lesser General Public 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 #ifdef HAVE_OPENCL
20
21 #include <OpenGl_AABB.hxx>
22 #include <OpenGl_Structure.hxx>
23 #include <OpenGl_PrimitiveArray.hxx>
24
25 namespace OpenGl_Raytrace
26 {
27   //! Checks to see if the group contains ray-trace geometry.
28   Standard_Boolean IsRaytracedGroup (const OpenGl_Group* theGroup);
29
30   //! Checks to see if the element contains ray-trace geometry.
31   Standard_Boolean IsRaytracedElement (const OpenGl_ElementNode* theNode);
32
33   //! Checks to see if the structure contains ray-trace geometry.
34   Standard_Boolean IsRaytracedStructure (const OpenGl_Structure* theStructure);
35 }
36
37 //! Stores properties of surface material.
38 class OpenGl_RaytraceMaterial
39 {
40 public:
41
42   //! Ambient reflection coefficient.
43   OpenGl_RTVec4f Ambient;
44
45   //! Diffuse reflection coefficient.
46   OpenGl_RTVec4f Diffuse;
47
48   //! Glossy reflection coefficient.
49   OpenGl_RTVec4f Specular;
50
51   //! Material emission.
52   OpenGl_RTVec4f Emission;
53
54   //! Specular reflection coefficient.
55   OpenGl_RTVec4f Reflection;
56
57   //! Specular refraction coefficient.
58   OpenGl_RTVec4f Refraction;
59
60   //! Material transparency.
61   OpenGl_RTVec4f Transparency;
62
63 public:
64
65   //! Creates new default material.
66   OpenGl_RaytraceMaterial();
67
68   //! Creates new material with specified properties.
69   OpenGl_RaytraceMaterial (const OpenGl_RTVec4f& theAmbient,
70                            const OpenGl_RTVec4f& theDiffuse,
71                            const OpenGl_RTVec4f& theSpecular);
72
73   //! Creates new material with specified properties.
74   OpenGl_RaytraceMaterial (const OpenGl_RTVec4f& theAmbient,
75                            const OpenGl_RTVec4f& theDiffuse,
76                            const OpenGl_RTVec4f& theSpecular,
77                            const OpenGl_RTVec4f& theEmission,
78                            const OpenGl_RTVec4f& theTranspar);
79
80   //! Creates new material with specified properties.
81   OpenGl_RaytraceMaterial (const OpenGl_RTVec4f& theAmbient,
82                            const OpenGl_RTVec4f& theDiffuse,
83                            const OpenGl_RTVec4f& theSpecular,
84                            const OpenGl_RTVec4f& theEmission,
85                            const OpenGl_RTVec4f& theTranspar,
86                            const OpenGl_RTVec4f& theReflection,
87                            const OpenGl_RTVec4f& theRefraction);
88
89   //! Returns packed (serialized) representation of material.
90   const float* Packed() { return reinterpret_cast<float*> (this); }
91 };
92
93 //! Stores properties of OpenGL light source.
94 class OpenGl_RaytraceLight
95 {
96 public:
97
98   //! 'Ambient' intensity.
99   OpenGl_RTVec4f Ambient;
100
101   //! 'Diffuse' intensity.
102   OpenGl_RTVec4f Diffuse;
103
104   //! Position of light source (in terms of OpenGL).
105   OpenGl_RTVec4f Position;
106
107
108 public:
109
110   //! Creates new light source.
111   OpenGl_RaytraceLight (const OpenGl_RTVec4f& theAmbient);
112
113   //! Creates new light source.
114   OpenGl_RaytraceLight (const OpenGl_RTVec4f& theDiffuse,
115                         const OpenGl_RTVec4f& thePosition);
116
117   //! Returns packed (serialized) representation of light source.
118   const float* Packed() { return reinterpret_cast<float*> (this); }
119 };
120
121 //! Stores scene geometry data.
122 struct OpenGl_RaytraceScene
123 {
124   //! AABB of 3D scene.
125   OpenGl_AABB AABB;
126
127   //! Array of vertex normals.
128   OpenGl_RTArray4f Normals;
129
130   //! Array of vertex coordinates.
131   OpenGl_RTArray4f Vertices;
132
133   //! Array of scene triangles.
134   OpenGl_RTArray4i Triangles;
135
136   //! Array of 'front' material properties.
137   std::vector<OpenGl_RaytraceMaterial,
138               NCollection_StdAllocator<OpenGl_RaytraceMaterial> > Materials;
139
140   //! Array of properties of light sources.
141   std::vector<OpenGl_RaytraceLight,
142               NCollection_StdAllocator<OpenGl_RaytraceLight> > LightSources;
143
144   //! Clears all scene geometry and material data.
145   void Clear();
146
147   //! Returns AABB of specified triangle.
148   OpenGl_AABB Box (const int theTriangle) const;
149
150   //! Returns centroid of specified triangle.
151   OpenGl_RTVec4f Center (const int theTriangle) const;
152
153   //! Returns centroid coordinate for specified axis.
154   float CenterAxis (const int theTriangle, const int theAxis) const;
155 };
156
157 //! Stores parameters of BVH tree node.
158 class OpenGl_BVHNode
159 {
160   friend class OpenGl_BVH;
161
162 public:
163
164   //! Creates new empty BVH node.
165   OpenGl_BVHNode();
166
167   //! Creates new BVH node with specified data.
168   OpenGl_BVHNode (const OpenGl_RTVec4f& theMinPoint,
169                   const OpenGl_RTVec4f& theMaxPoint,
170                   const OpenGl_RTVec4i& theDataRcrd);
171
172   //! Creates new leaf BVH node with specified data.
173   OpenGl_BVHNode (const OpenGl_RTVec4f& theMinPoint,
174                   const OpenGl_RTVec4f& theMaxPoint,
175                   const int theBegTriangle,
176                   const int theEndTriangle);
177
178   //! Creates new leaf BVH node with specified data.
179   OpenGl_BVHNode (const OpenGl_AABB& theAABB,
180                   const int theBegTriangle,
181                   const int theEndTriangle);
182
183   //! Returns minimum point of node's AABB.
184   OpenGl_RTVec4f& MinPoint() { return myMinPoint; }
185   //! Returns maximum point of node's AABB.
186   OpenGl_RTVec4f& MaxPoint() { return myMaxPoint; }
187
188   //! Returns minimum point of node's AABB.
189   const OpenGl_RTVec4f& MinPoint() const { return myMinPoint; }
190   //! Returns maximum point of node's AABB.
191   const OpenGl_RTVec4f& MaxPoint() const { return myMaxPoint; }
192
193   //! Returns index of left child of inner node.
194   int LeftChild() const { return myDataRcrd.y(); }
195   //! Sets index of left child of inner node.
196   void SetLeftChild (int theChild) { myDataRcrd.y() = theChild; }
197
198   //! Returns index of right child of inner node.
199   int RightChild() const { return myDataRcrd.z(); }
200   //! Sets index of right child of inner node.
201   void SetRightChild (int theChild) { myDataRcrd.z() = theChild; }
202
203   //! Returns index of begin triangle of leaf node.
204   int BegTriangle() const { return myDataRcrd.y(); }
205   //! Sets index of begin triangle of leaf node.
206   void SetBegTriangle (int theIndex) { myDataRcrd.y() = theIndex; }
207
208   //! Returns index of end triangle of leaf node.
209   int EndTriangle() const { return myDataRcrd.z(); }
210   //! Sets index of end triangle of leaf node.
211   void SetEndTriangle (int theIndex) { myDataRcrd.z() = theIndex; }
212
213   //! Returns level of the node in BVH tree.
214   int Level() const { return myDataRcrd.w(); }
215   //! Sets level of the node in BVH tree.
216   void SetLevel (int theLevel) { myDataRcrd.w() = theLevel; }
217
218   //! Is node a leaf (outer)?
219   bool IsOuter() const { return myDataRcrd.x() == 1; }
220
221   //! Sets node type to 'outer'.
222   void SetOuter() { myDataRcrd.x() = 1; }
223   //! Sets node type to 'inner'.
224   void SetInner() { myDataRcrd.x() = 0; }
225
226 private:
227
228   //! Minimum point of node's bounding box.
229   OpenGl_RTVec4f myMinPoint;
230   //! Maximum point of node's bounding box.
231   OpenGl_RTVec4f myMaxPoint;
232
233   //! Data vector (stores data fields of the node).
234   OpenGl_RTVec4i myDataRcrd;
235 };
236
237 //! Stores parameters of BVH tree.
238 class OpenGl_BVH
239 {
240 public:
241
242   //! Removes all tree nodes.
243   void CleanUp();
244
245   //! Adds new node to the tree.
246   int PushNode (const OpenGl_BVHNode& theNode);
247
248   //! Returns node with specified index.
249   OpenGl_BVHNode Node (const int theIndex) const;
250
251   //! Replaces node with specified index by the new one.
252   void SetNode (const int theIndex, const OpenGl_BVHNode& theNode);
253
254   //! Returns array of node min points.
255   OpenGl_RTArray4f& MinPointBuffer() { return myMinPointBuffer; }
256   //! Returns array of node max points.
257   OpenGl_RTArray4f& MaxPointBuffer() { return myMaxPointBuffer; }
258   //! Returns array of node data records.
259   OpenGl_RTArray4i& DataRcrdBuffer() { return myDataRcrdBuffer; }
260
261 private:
262
263   //! Array of min points of BVH nodes.
264   OpenGl_RTArray4f myMinPointBuffer;
265   //! Array of max points of BVH nodes.
266   OpenGl_RTArray4f myMaxPointBuffer;
267   //! Array of data vectors of BVH nodes.
268   OpenGl_RTArray4i myDataRcrdBuffer;
269 };
270
271 //! Stores parameters of single node bin (slice of AABB).
272 struct OpenGl_BVHBin
273 {
274   //! Creates new node bin.
275   OpenGl_BVHBin(): Count (0) { }
276
277   //! Number of primitives in the bin.
278   int Count;
279
280   //! AABB of the bin.
281   OpenGl_AABB Volume;
282 };
283
284 //! Node building task.
285 struct OpenGl_BVHNodeTask
286 {
287   //! Creates new node building task.
288   OpenGl_BVHNodeTask();
289
290   //! Creates new node building task.
291   OpenGl_BVHNodeTask (const int theNodeToBuild,
292                       const int theBegTriangle,
293                       const int theEndTriangle);
294
295   //! Index of building tree node.
296   int NodeToBuild;
297   //! Index of start node triangle.
298   int BegTriangle;
299   //! Index of final node triangle.
300   int EndTriangle;
301 };
302
303 //! The array of bins of BVH tree node.
304 typedef std::vector<OpenGl_BVHBin,
305                     NCollection_StdAllocator<OpenGl_BVHBin> > OpenGl_BinVector;
306
307 //! Binned SAH-based BVH builder.
308 class OpenGl_BinnedBVHBuilder
309 {
310 public:
311
312   //! Creates new binned BVH builder.
313   OpenGl_BinnedBVHBuilder();
314
315   //! Releases binned BVH builder.
316   ~OpenGl_BinnedBVHBuilder();
317
318   //! Builds BVH tree using binned SAH algorithm.
319   void Build (OpenGl_RaytraceScene& theGeometry, const float theEpsilon = 1e-3f);
320
321   //! Sets maximum tree depth.
322   void SetMaxDepth (const int theMaxDepth);
323
324   //! Clears previously constructed BVH tree.
325   void CleanUp();
326
327   //! Return constructed BVH tree.
328   OpenGl_BVH& Tree() { return myTree; }
329
330 private:
331
332   //! Builds node using task info.
333   void BuildNode (OpenGl_RaytraceScene& theGeometry, const int theTask);
334
335   //! Arranges node triangles into bins.
336   void GetSubVolumes (OpenGl_RaytraceScene& theGeometry, const OpenGl_BVHNode& theNode,
337                                                 OpenGl_BinVector& theBins, const int theAxis);
338
339   //! Splits node triangles into two intervals for child nodes.
340   int SplitTriangles (OpenGl_RaytraceScene& theGeometry, const int theFirst, const int theLast,
341                                                   OpenGl_BVHNode& theNode, int theBin, const int theAxis);
342
343 private:
344
345   //! Queue of node building tasks.
346   std::vector<OpenGl_BVHNodeTask> myNodeTasksQueue;
347
348   //! Builded BVH tree.
349   OpenGl_BVH myTree;
350
351   //! Maximum depth of BVH tree.
352   int myMaxDepth;
353 };
354
355 #endif
356 #endif