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>
22 #include <OpenGl_PrimitiveArray.hxx>
23 #include <OpenGl_Structure.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.
45 //! Diffuse reflection coefficient.
48 //! Glossy reflection coefficient.
51 //! Material emission.
54 //! Specular reflection coefficient.
57 //! Specular refraction coefficient.
60 //! Material transparency.
61 BVH_Vec4f Transparency;
65 //! Creates new default material.
66 OpenGl_RaytraceMaterial();
68 //! Creates new material with specified properties.
69 OpenGl_RaytraceMaterial (const BVH_Vec4f& theAmbient,
70 const BVH_Vec4f& theDiffuse,
71 const BVH_Vec4f& theSpecular);
73 //! Creates new material with specified properties.
74 OpenGl_RaytraceMaterial (const BVH_Vec4f& theAmbient,
75 const BVH_Vec4f& theDiffuse,
76 const BVH_Vec4f& theSpecular,
77 const BVH_Vec4f& theEmission,
78 const BVH_Vec4f& theTranspar);
80 //! Creates new material with specified properties.
81 OpenGl_RaytraceMaterial (const BVH_Vec4f& theAmbient,
82 const BVH_Vec4f& theDiffuse,
83 const BVH_Vec4f& theSpecular,
84 const BVH_Vec4f& theEmission,
85 const BVH_Vec4f& theTranspar,
86 const BVH_Vec4f& theReflection,
87 const BVH_Vec4f& theRefraction);
89 //! Returns packed (serialized) representation of material.
90 const Standard_ShortReal* Packed()
92 return reinterpret_cast<Standard_ShortReal*> (this);
96 //! Stores properties of OpenGL light source.
97 class OpenGl_RaytraceLight
101 //! Diffuse intensity (in terms of OpenGL).
104 //! Position of light source (in terms of OpenGL).
109 //! Creates new light source.
110 OpenGl_RaytraceLight (const BVH_Vec4f& theDiffuse,
111 const BVH_Vec4f& thePosition);
113 //! Returns packed (serialized) representation of light source.
114 const Standard_ShortReal* Packed()
116 return reinterpret_cast<Standard_ShortReal*> (this);
120 //! Triangulation of single OpenGL primitive array.
121 class OpenGl_TriangleSet : public BVH_Triangulation<Standard_ShortReal, 4>
125 //! Value of invalid material index to return in case of errors.
126 static const Standard_Integer INVALID_MATERIAL = -1;
130 //! Creates new OpenGL element triangulation.
131 OpenGl_TriangleSet (const OpenGl_PrimitiveArray* theArray = NULL)
132 : BVH_Triangulation<Standard_ShortReal, 4>(),
138 //! Releases resources of OpenGL element triangulation.
139 ~OpenGl_TriangleSet()
144 //! Returns associated OpenGl structure.
145 const OpenGl_PrimitiveArray* AssociatedPArray() const
150 //! Returns material index of triangle set.
151 Standard_Integer MaterialIndex() const
153 if (Elements.size() == 0)
154 return INVALID_MATERIAL;
156 return Elements.front().w();
159 //! Sets material index for entire triangle set.
160 void SetMaterialIndex (Standard_Integer aMatID)
162 for (Standard_Size anIdx = 0; anIdx < Elements.size(); ++anIdx)
163 Elements[anIdx].w() = aMatID;
166 //! Returns AABB of primitive set.
167 BVH_BoxNt Box() const
169 const BVH_Transform<Standard_ShortReal, 4>* aTransform =
170 dynamic_cast<const BVH_Transform<Standard_ShortReal, 4>* > (Properties().operator->());
172 BVH_BoxNt aBox = BVH_PrimitiveSet<Standard_ShortReal, 4>::Box();
176 return aTransform->Apply (aBox);
184 BVH_Array4f Normals; //!< Array of vertex normals.
188 const OpenGl_PrimitiveArray* myArray; //!< Reference to associated OpenGl structure.
192 //! Stores geometry of ray-tracing scene.
193 class OpenGl_RaytraceGeometry : public BVH_Geometry<Standard_ShortReal, 4>
197 //! Value of invalid offset to return in case of errors.
198 static const Standard_Integer INVALID_OFFSET = -1;
202 //! Array of properties of light sources.
203 std::vector<OpenGl_RaytraceLight,
204 NCollection_StdAllocator<OpenGl_RaytraceLight> > Sources;
206 //! Array of 'front' material properties.
207 std::vector<OpenGl_RaytraceMaterial,
208 NCollection_StdAllocator<OpenGl_RaytraceMaterial> > Materials;
210 //! Global ambient from all light sources.
215 //! Creates uninitialized ray-tracing geometry.
216 OpenGl_RaytraceGeometry()
217 : BVH_Geometry<Standard_ShortReal, 4>(),
218 myHighLevelTreeDepth (0),
219 myBottomLevelTreeDepth (0)
224 //! Releases resources of ray-tracing geometry.
225 ~OpenGl_RaytraceGeometry()
230 //! Clears ray-tracing geometry.
233 //! Clears only ray-tracing materials.
234 void ClearMaterials()
236 std::vector<OpenGl_RaytraceMaterial,
237 NCollection_StdAllocator<OpenGl_RaytraceMaterial> > anEmptyMaterials;
239 Materials.swap (anEmptyMaterials);
244 //! Performs post-processing of high-level scene BVH.
245 Standard_Boolean ProcessAcceleration();
247 //! Returns offset of bottom-level BVH for given leaf node.
248 //! If the node index is not valid the function returns -1.
249 //! @note Can be used after processing acceleration structure.
250 Standard_Integer AccelerationOffset (Standard_Integer theNodeIdx);
252 //! Returns offset of triangulation vertices for given leaf node.
253 //! If the node index is not valid the function returns -1.
254 //! @note Can be used after processing acceleration structure.
255 Standard_Integer VerticesOffset (Standard_Integer theNodeIdx);
257 //! Returns offset of triangulation elements for given leaf node.
258 //! If the node index is not valid the function returns -1.
259 //! @note Can be used after processing acceleration structure.
260 Standard_Integer ElementsOffset (Standard_Integer theNodeIdx);
262 //! Returns triangulation data for given leaf node.
263 //! If the node index is not valid the function returns NULL.
264 //! @note Can be used after processing acceleration structure.
265 OpenGl_TriangleSet* TriangleSet (Standard_Integer theNodeIdx);
267 //! Returns depth of high-level scene BVH from last build.
268 Standard_Integer HighLevelTreeDepth() const
270 return myHighLevelTreeDepth;
273 //! Returns maximum depth of bottom-level scene BVHs from last build.
274 Standard_Integer BottomLevelTreeDepth() const
276 return myBottomLevelTreeDepth;
281 Standard_Integer myHighLevelTreeDepth; //!< Depth of high-level scene BVH from last build
282 Standard_Integer myBottomLevelTreeDepth; //!< Maximum depth of bottom-level scene BVHs from last build