1 // Created on: 2013-08-27
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2013 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef _OpenGl_SceneGeometry_Header
17 #define _OpenGl_SceneGeometry_Header
21 #include <OpenGl_AABB.hxx>
22 #include <OpenGl_Structure.hxx>
23 #include <OpenGl_PrimitiveArray.hxx>
25 namespace OpenGl_Raytrace
27 //! Checks to see if the group contains ray-trace geometry.
28 Standard_Boolean IsRaytracedGroup (const OpenGl_Group* theGroup);
30 //! Checks to see if the element contains ray-trace geometry.
31 Standard_Boolean IsRaytracedElement (const OpenGl_ElementNode* theNode);
33 //! Checks to see if the structure contains ray-trace geometry.
34 Standard_Boolean IsRaytracedStructure (const OpenGl_Structure* theStructure);
37 //! Stores properties of surface material.
38 class OpenGl_RaytraceMaterial
42 //! Ambient reflection coefficient.
43 OpenGl_RTVec4f Ambient;
45 //! Diffuse reflection coefficient.
46 OpenGl_RTVec4f Diffuse;
48 //! Glossy reflection coefficient.
49 OpenGl_RTVec4f Specular;
51 //! Material emission.
52 OpenGl_RTVec4f Emission;
54 //! Specular reflection coefficient.
55 OpenGl_RTVec4f Reflection;
57 //! Specular refraction coefficient.
58 OpenGl_RTVec4f Refraction;
60 //! Material transparency.
61 OpenGl_RTVec4f Transparency;
65 //! Creates new default material.
66 OpenGl_RaytraceMaterial();
68 //! Creates new material with specified properties.
69 OpenGl_RaytraceMaterial (const OpenGl_RTVec4f& theAmbient,
70 const OpenGl_RTVec4f& theDiffuse,
71 const OpenGl_RTVec4f& theSpecular);
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);
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);
89 //! Returns packed (serialized) representation of material.
90 const float* Packed() { return reinterpret_cast<float*> (this); }
93 //! Stores properties of OpenGL light source.
94 class OpenGl_RaytraceLight
98 //! 'Ambient' intensity.
99 OpenGl_RTVec4f Ambient;
101 //! 'Diffuse' intensity.
102 OpenGl_RTVec4f Diffuse;
104 //! Position of light source (in terms of OpenGL).
105 OpenGl_RTVec4f Position;
110 //! Creates new light source.
111 OpenGl_RaytraceLight (const OpenGl_RTVec4f& theAmbient);
113 //! Creates new light source.
114 OpenGl_RaytraceLight (const OpenGl_RTVec4f& theDiffuse,
115 const OpenGl_RTVec4f& thePosition);
117 //! Returns packed (serialized) representation of light source.
118 const float* Packed() { return reinterpret_cast<float*> (this); }
121 //! Stores scene geometry data.
122 struct OpenGl_RaytraceScene
124 //! AABB of 3D scene.
127 //! Array of vertex normals.
128 OpenGl_RTArray4f Normals;
130 //! Array of vertex coordinates.
131 OpenGl_RTArray4f Vertices;
133 //! Array of scene triangles.
134 OpenGl_RTArray4i Triangles;
136 //! Array of 'front' material properties.
137 std::vector<OpenGl_RaytraceMaterial,
138 NCollection_StdAllocator<OpenGl_RaytraceMaterial> > Materials;
140 //! Array of properties of light sources.
141 std::vector<OpenGl_RaytraceLight,
142 NCollection_StdAllocator<OpenGl_RaytraceLight> > LightSources;
144 //! Clears all scene geometry and material data.
147 //! Returns AABB of specified triangle.
148 OpenGl_AABB Box (const int theTriangle) const;
150 //! Returns centroid of specified triangle.
151 OpenGl_RTVec4f Center (const int theTriangle) const;
153 //! Returns centroid coordinate for specified axis.
154 float CenterAxis (const int theTriangle, const int theAxis) const;
157 //! Stores parameters of BVH tree node.
160 friend class OpenGl_BVH;
164 //! Creates new empty BVH node.
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);
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);
178 //! Creates new leaf BVH node with specified data.
179 OpenGl_BVHNode (const OpenGl_AABB& theAABB,
180 const int theBegTriangle,
181 const int theEndTriangle);
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; }
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; }
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; }
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; }
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; }
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; }
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; }
218 //! Is node a leaf (outer)?
219 bool IsOuter() const { return myDataRcrd.x() == 1; }
221 //! Sets node type to 'outer'.
222 void SetOuter() { myDataRcrd.x() = 1; }
223 //! Sets node type to 'inner'.
224 void SetInner() { myDataRcrd.x() = 0; }
228 //! Minimum point of node's bounding box.
229 OpenGl_RTVec4f myMinPoint;
230 //! Maximum point of node's bounding box.
231 OpenGl_RTVec4f myMaxPoint;
233 //! Data vector (stores data fields of the node).
234 OpenGl_RTVec4i myDataRcrd;
237 //! Stores parameters of BVH tree.
242 //! Removes all tree nodes.
245 //! Adds new node to the tree.
246 int PushNode (const OpenGl_BVHNode& theNode);
248 //! Returns node with specified index.
249 OpenGl_BVHNode Node (const int theIndex) const;
251 //! Replaces node with specified index by the new one.
252 void SetNode (const int theIndex, const OpenGl_BVHNode& theNode);
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; }
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;
271 //! Stores parameters of single node bin (slice of AABB).
274 //! Creates new node bin.
275 OpenGl_BVHBin(): Count (0) { }
277 //! Number of primitives in the bin.
284 //! Node building task.
285 struct OpenGl_BVHNodeTask
287 //! Creates new node building task.
288 OpenGl_BVHNodeTask();
290 //! Creates new node building task.
291 OpenGl_BVHNodeTask (const int theNodeToBuild,
292 const int theBegTriangle,
293 const int theEndTriangle);
295 //! Index of building tree node.
297 //! Index of start node triangle.
299 //! Index of final node triangle.
303 //! The array of bins of BVH tree node.
304 typedef std::vector<OpenGl_BVHBin,
305 NCollection_StdAllocator<OpenGl_BVHBin> > OpenGl_BinVector;
307 //! Binned SAH-based BVH builder.
308 class OpenGl_BinnedBVHBuilder
312 //! Creates new binned BVH builder.
313 OpenGl_BinnedBVHBuilder();
315 //! Releases binned BVH builder.
316 ~OpenGl_BinnedBVHBuilder();
318 //! Builds BVH tree using binned SAH algorithm.
319 void Build (OpenGl_RaytraceScene& theGeometry, const float theEpsilon = 1e-3f);
321 //! Sets maximum tree depth.
322 void SetMaxDepth (const int theMaxDepth);
324 //! Clears previously constructed BVH tree.
327 //! Return constructed BVH tree.
328 OpenGl_BVH& Tree() { return myTree; }
332 //! Builds node using task info.
333 void BuildNode (OpenGl_RaytraceScene& theGeometry, const int theTask);
335 //! Arranges node triangles into bins.
336 void GetSubVolumes (OpenGl_RaytraceScene& theGeometry, const OpenGl_BVHNode& theNode,
337 OpenGl_BinVector& theBins, const int theAxis);
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);
345 //! Queue of node building tasks.
346 std::vector<OpenGl_BVHNodeTask> myNodeTasksQueue;
348 //! Builded BVH tree.
351 //! Maximum depth of BVH tree.