1 // Created on: 2013-08-27
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2013 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
20 #ifndef _OpenGl_SceneGeometry_Header
21 #define _OpenGl_SceneGeometry_Header
25 #include <OpenGl_AABB.hxx>
26 #include <OpenGl_Structure.hxx>
27 #include <OpenGl_PrimitiveArray.hxx>
29 namespace OpenGl_Raytrace
31 //! Checks to see if the group contains ray-trace geometry.
32 Standard_Boolean IsRaytracedGroup (const OpenGl_Group* theGroup);
34 //! Checks to see if the element contains ray-trace geometry.
35 Standard_Boolean IsRaytracedElement (const OpenGl_ElementNode* theNode);
37 //! Checks to see if the structure contains ray-trace geometry.
38 Standard_Boolean IsRaytracedStructure (const OpenGl_Structure* theStructure);
41 //! Stores properties of surface material.
42 class OpenGl_RaytraceMaterial
46 //! Ambient reflection coefficient.
47 OpenGl_RTVec4f Ambient;
49 //! Diffuse reflection coefficient.
50 OpenGl_RTVec4f Diffuse;
52 //! Glossy reflection coefficient.
53 OpenGl_RTVec4f Specular;
55 //! Material emission.
56 OpenGl_RTVec4f Emission;
58 //! Specular reflection coefficient.
59 OpenGl_RTVec4f Reflection;
61 //! Specular refraction coefficient.
62 OpenGl_RTVec4f Refraction;
64 //! Material transparency.
65 OpenGl_RTVec4f Transparency;
69 //! Creates new default material.
70 OpenGl_RaytraceMaterial();
72 //! Creates new material with specified properties.
73 OpenGl_RaytraceMaterial (const OpenGl_RTVec4f& theAmbient,
74 const OpenGl_RTVec4f& theDiffuse,
75 const OpenGl_RTVec4f& theSpecular);
77 //! Creates new material with specified properties.
78 OpenGl_RaytraceMaterial (const OpenGl_RTVec4f& theAmbient,
79 const OpenGl_RTVec4f& theDiffuse,
80 const OpenGl_RTVec4f& theSpecular,
81 const OpenGl_RTVec4f& theEmission,
82 const OpenGl_RTVec4f& theTranspar);
84 //! Creates new material with specified properties.
85 OpenGl_RaytraceMaterial (const OpenGl_RTVec4f& theAmbient,
86 const OpenGl_RTVec4f& theDiffuse,
87 const OpenGl_RTVec4f& theSpecular,
88 const OpenGl_RTVec4f& theEmission,
89 const OpenGl_RTVec4f& theTranspar,
90 const OpenGl_RTVec4f& theReflection,
91 const OpenGl_RTVec4f& theRefraction);
93 //! Returns packed (serialized) representation of material.
94 const float* Packed() { return reinterpret_cast<float*> (this); }
97 //! Stores properties of OpenGL light source.
98 class OpenGl_RaytraceLight
102 //! 'Ambient' intensity.
103 OpenGl_RTVec4f Ambient;
105 //! 'Diffuse' intensity.
106 OpenGl_RTVec4f Diffuse;
108 //! Position of light source (in terms of OpenGL).
109 OpenGl_RTVec4f Position;
114 //! Creates new light source.
115 OpenGl_RaytraceLight (const OpenGl_RTVec4f& theAmbient);
117 //! Creates new light source.
118 OpenGl_RaytraceLight (const OpenGl_RTVec4f& theDiffuse,
119 const OpenGl_RTVec4f& thePosition);
121 //! Returns packed (serialized) representation of light source.
122 const float* Packed() { return reinterpret_cast<float*> (this); }
125 //! Stores scene geometry data.
126 struct OpenGl_RaytraceScene
128 //! AABB of 3D scene.
131 //! Array of vertex normals.
132 OpenGl_RTArray4f Normals;
134 //! Array of vertex coordinates.
135 OpenGl_RTArray4f Vertices;
137 //! Array of scene triangles.
138 OpenGl_RTArray4i Triangles;
140 //! Array of 'front' material properties.
141 std::vector<OpenGl_RaytraceMaterial,
142 NCollection_StdAllocator<OpenGl_RaytraceMaterial> > Materials;
144 //! Array of properties of light sources.
145 std::vector<OpenGl_RaytraceLight,
146 NCollection_StdAllocator<OpenGl_RaytraceLight> > LightSources;
148 //! Clears all scene geometry and material data.
151 //! Returns AABB of specified triangle.
152 OpenGl_AABB Box (const int theTriangle) const;
154 //! Returns centroid of specified triangle.
155 OpenGl_RTVec4f Center (const int theTriangle) const;
157 //! Returns centroid coordinate for specified axis.
158 float CenterAxis (const int theTriangle, const int theAxis) const;
161 //! Stores parameters of BVH tree node.
164 friend class OpenGl_BVH;
168 //! Creates new empty BVH node.
171 //! Creates new BVH node with specified data.
172 OpenGl_BVHNode (const OpenGl_RTVec4f& theMinPoint,
173 const OpenGl_RTVec4f& theMaxPoint,
174 const OpenGl_RTVec4i& theDataRcrd);
176 //! Creates new leaf BVH node with specified data.
177 OpenGl_BVHNode (const OpenGl_RTVec4f& theMinPoint,
178 const OpenGl_RTVec4f& theMaxPoint,
179 const int theBegTriangle,
180 const int theEndTriangle);
182 //! Creates new leaf BVH node with specified data.
183 OpenGl_BVHNode (const OpenGl_AABB& theAABB,
184 const int theBegTriangle,
185 const int theEndTriangle);
187 //! Returns minimum point of node's AABB.
188 OpenGl_RTVec4f& MinPoint() { return myMinPoint; }
189 //! Returns maximum point of node's AABB.
190 OpenGl_RTVec4f& MaxPoint() { return myMaxPoint; }
192 //! Returns minimum point of node's AABB.
193 const OpenGl_RTVec4f& MinPoint() const { return myMinPoint; }
194 //! Returns maximum point of node's AABB.
195 const OpenGl_RTVec4f& MaxPoint() const { return myMaxPoint; }
197 //! Returns index of left child of inner node.
198 int LeftChild() const { return myDataRcrd.y(); }
199 //! Sets index of left child of inner node.
200 void SetLeftChild (int theChild) { myDataRcrd.y() = theChild; }
202 //! Returns index of right child of inner node.
203 int RightChild() const { return myDataRcrd.z(); }
204 //! Sets index of right child of inner node.
205 void SetRightChild (int theChild) { myDataRcrd.z() = theChild; }
207 //! Returns index of begin triangle of leaf node.
208 int BegTriangle() const { return myDataRcrd.y(); }
209 //! Sets index of begin triangle of leaf node.
210 void SetBegTriangle (int theIndex) { myDataRcrd.y() = theIndex; }
212 //! Returns index of end triangle of leaf node.
213 int EndTriangle() const { return myDataRcrd.z(); }
214 //! Sets index of end triangle of leaf node.
215 void SetEndTriangle (int theIndex) { myDataRcrd.z() = theIndex; }
217 //! Returns level of the node in BVH tree.
218 int Level() const { return myDataRcrd.w(); }
219 //! Sets level of the node in BVH tree.
220 void SetLevel (int theLevel) { myDataRcrd.w() = theLevel; }
222 //! Is node a leaf (outer)?
223 bool IsOuter() const { return myDataRcrd.x() == 1; }
225 //! Sets node type to 'outer'.
226 void SetOuter() { myDataRcrd.x() = 1; }
227 //! Sets node type to 'inner'.
228 void SetInner() { myDataRcrd.x() = 0; }
232 //! Minimum point of node's bounding box.
233 OpenGl_RTVec4f myMinPoint;
234 //! Maximum point of node's bounding box.
235 OpenGl_RTVec4f myMaxPoint;
237 //! Data vector (stores data fields of the node).
238 OpenGl_RTVec4i myDataRcrd;
241 //! Stores parameters of BVH tree.
246 //! Removes all tree nodes.
249 //! Adds new node to the tree.
250 int PushNode (const OpenGl_BVHNode& theNode);
252 //! Returns node with specified index.
253 OpenGl_BVHNode Node (const int theIndex) const;
255 //! Replaces node with specified index by the new one.
256 void SetNode (const int theIndex, const OpenGl_BVHNode& theNode);
258 //! Returns array of node min points.
259 OpenGl_RTArray4f& MinPointBuffer() { return myMinPointBuffer; }
260 //! Returns array of node max points.
261 OpenGl_RTArray4f& MaxPointBuffer() { return myMaxPointBuffer; }
262 //! Returns array of node data records.
263 OpenGl_RTArray4i& DataRcrdBuffer() { return myDataRcrdBuffer; }
267 //! Array of min points of BVH nodes.
268 OpenGl_RTArray4f myMinPointBuffer;
269 //! Array of max points of BVH nodes.
270 OpenGl_RTArray4f myMaxPointBuffer;
271 //! Array of data vectors of BVH nodes.
272 OpenGl_RTArray4i myDataRcrdBuffer;
275 //! Stores parameters of single node bin (slice of AABB).
278 //! Creates new node bin.
279 OpenGl_BVHBin(): Count (0) { }
281 //! Number of primitives in the bin.
288 //! Node building task.
289 struct OpenGl_BVHNodeTask
291 //! Creates new node building task.
292 OpenGl_BVHNodeTask();
294 //! Creates new node building task.
295 OpenGl_BVHNodeTask (const int theNodeToBuild,
296 const int theBegTriangle,
297 const int theEndTriangle);
299 //! Index of building tree node.
301 //! Index of start node triangle.
303 //! Index of final node triangle.
307 //! The array of bins of BVH tree node.
308 typedef std::vector<OpenGl_BVHBin,
309 NCollection_StdAllocator<OpenGl_BVHBin> > OpenGl_BinVector;
311 //! Binned SAH-based BVH builder.
312 class OpenGl_BinnedBVHBuilder
316 //! Creates new binned BVH builder.
317 OpenGl_BinnedBVHBuilder();
319 //! Releases binned BVH builder.
320 ~OpenGl_BinnedBVHBuilder();
322 //! Builds BVH tree using binned SAH algorithm.
323 void Build (OpenGl_RaytraceScene& theGeometry, const float theEpsilon = 1e-3f);
325 //! Sets maximum tree depth.
326 void SetMaxDepth (const int theMaxDepth);
328 //! Clears previously constructed BVH tree.
331 //! Return constructed BVH tree.
332 OpenGl_BVH& Tree() { return myTree; }
336 //! Builds node using task info.
337 void BuildNode (OpenGl_RaytraceScene& theGeometry, const int theTask);
339 //! Arranges node triangles into bins.
340 void GetSubVolumes (OpenGl_RaytraceScene& theGeometry, const OpenGl_BVHNode& theNode,
341 OpenGl_BinVector& theBins, const int theAxis);
343 //! Splits node triangles into two intervals for child nodes.
344 int SplitTriangles (OpenGl_RaytraceScene& theGeometry, const int theFirst, const int theLast,
345 OpenGl_BVHNode& theNode, int theBin, const int theAxis);
349 //! Queue of node building tasks.
350 std::vector<OpenGl_BVHNodeTask> myNodeTasksQueue;
352 //! Builded BVH tree.
355 //! Maximum depth of BVH tree.