1 // Created on: 2013-08-27
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2013-2014 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 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.
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
19 #include <BVH_Geometry.hxx>
20 #include <BVH_Triangulation.hxx>
21 #include <NCollection_StdAllocator.hxx>
23 struct OpenGl_ElementNode;
25 class OpenGl_Structure;
26 class OpenGl_PrimitiveArray;
28 namespace OpenGl_Raytrace
30 //! Checks to see if the group contains ray-trace geometry.
31 Standard_Boolean IsRaytracedGroup (const OpenGl_Group* theGroup);
33 //! Checks to see if the element contains ray-trace geometry.
34 Standard_Boolean IsRaytracedElement (const OpenGl_ElementNode* theNode);
36 //! Checks to see if the structure contains ray-trace geometry.
37 Standard_Boolean IsRaytracedStructure (const OpenGl_Structure* theStructure);
40 //! Stores properties of surface material.
41 class OpenGl_RaytraceMaterial
45 //! Ambient reflection coefficient.
48 //! Diffuse reflection coefficient.
51 //! Glossy reflection coefficient.
54 //! Material emission.
57 //! Specular reflection coefficient.
60 //! Specular refraction coefficient.
63 //! Material transparency.
64 BVH_Vec4f Transparency;
68 //! Creates new default material.
69 OpenGl_RaytraceMaterial();
71 //! Creates new material with specified properties.
72 OpenGl_RaytraceMaterial (const BVH_Vec4f& theAmbient,
73 const BVH_Vec4f& theDiffuse,
74 const BVH_Vec4f& theSpecular);
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);
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);
92 //! Returns packed (serialized) representation of material.
93 const Standard_ShortReal* Packed()
95 return reinterpret_cast<Standard_ShortReal*> (this);
99 //! Stores properties of OpenGL light source.
100 class OpenGl_RaytraceLight
104 //! Diffuse intensity (in terms of OpenGL).
107 //! Position of light source (in terms of OpenGL).
112 //! Creates new light source.
113 OpenGl_RaytraceLight (const BVH_Vec4f& theDiffuse,
114 const BVH_Vec4f& thePosition);
116 //! Returns packed (serialized) representation of light source.
117 const Standard_ShortReal* Packed()
119 return reinterpret_cast<Standard_ShortReal*> (this);
123 //! Triangulation of single OpenGL primitive array.
124 class OpenGl_TriangleSet : public BVH_Triangulation<Standard_ShortReal, 4>
128 //! Value of invalid material index to return in case of errors.
129 static const Standard_Integer INVALID_MATERIAL = -1;
133 //! Creates new OpenGL element triangulation.
134 OpenGl_TriangleSet (const OpenGl_PrimitiveArray* theArray = NULL)
135 : BVH_Triangulation<Standard_ShortReal, 4>(),
141 //! Releases resources of OpenGL element triangulation.
142 ~OpenGl_TriangleSet()
147 //! Returns associated OpenGl structure.
148 const OpenGl_PrimitiveArray* AssociatedPArray() const
153 //! Returns material index of triangle set.
154 Standard_Integer MaterialIndex() const
156 if (Elements.size() == 0)
157 return INVALID_MATERIAL;
159 return Elements.front().w();
162 //! Sets material index for entire triangle set.
163 void SetMaterialIndex (Standard_Integer aMatID)
165 for (Standard_Size anIdx = 0; anIdx < Elements.size(); ++anIdx)
166 Elements[anIdx].w() = aMatID;
169 //! Returns AABB of primitive set.
170 BVH_BoxNt Box() const
172 const BVH_Transform<Standard_ShortReal, 4>* aTransform =
173 dynamic_cast<const BVH_Transform<Standard_ShortReal, 4>* > (Properties().operator->());
175 BVH_BoxNt aBox = BVH_PrimitiveSet<Standard_ShortReal, 4>::Box();
179 return aTransform->Apply (aBox);
187 BVH_Array4f Normals; //!< Array of vertex normals.
191 const OpenGl_PrimitiveArray* myArray; //!< Reference to associated OpenGl structure.
195 //! Stores geometry of ray-tracing scene.
196 class OpenGl_RaytraceGeometry : public BVH_Geometry<Standard_ShortReal, 4>
200 //! Value of invalid offset to return in case of errors.
201 static const Standard_Integer INVALID_OFFSET = -1;
205 //! Array of properties of light sources.
206 std::vector<OpenGl_RaytraceLight,
207 NCollection_StdAllocator<OpenGl_RaytraceLight> > Sources;
209 //! Array of 'front' material properties.
210 std::vector<OpenGl_RaytraceMaterial,
211 NCollection_StdAllocator<OpenGl_RaytraceMaterial> > Materials;
213 //! Global ambient from all light sources.
218 //! Creates uninitialized ray-tracing geometry.
219 OpenGl_RaytraceGeometry()
220 : BVH_Geometry<Standard_ShortReal, 4>(),
221 myHighLevelTreeDepth (0),
222 myBottomLevelTreeDepth (0)
227 //! Releases resources of ray-tracing geometry.
228 ~OpenGl_RaytraceGeometry()
233 //! Clears ray-tracing geometry.
236 //! Clears only ray-tracing materials.
237 void ClearMaterials()
239 std::vector<OpenGl_RaytraceMaterial,
240 NCollection_StdAllocator<OpenGl_RaytraceMaterial> > anEmptyMaterials;
242 Materials.swap (anEmptyMaterials);
247 //! Performs post-processing of high-level scene BVH.
248 Standard_Boolean ProcessAcceleration();
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);
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);
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);
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);
270 //! Returns depth of high-level scene BVH from last build.
271 Standard_Integer HighLevelTreeDepth() const
273 return myHighLevelTreeDepth;
276 //! Returns maximum depth of bottom-level scene BVHs from last build.
277 Standard_Integer BottomLevelTreeDepth() const
279 return myBottomLevelTreeDepth;
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