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 <BVH_BinnedBuilder.hxx>
22 #include <NCollection_StdAllocator.hxx>
23 #include <OpenGl_TextureBufferArb.hxx>
24 #include <OpenGl_Texture.hxx>
25 #include <OpenGl_Sampler.hxx>
28 struct OpenGl_ElementNode;
30 class OpenGl_Structure;
31 class OpenGl_PrimitiveArray;
33 namespace OpenGl_Raytrace
35 //! Checks to see if the group contains ray-trace geometry.
36 Standard_Boolean IsRaytracedGroup (const OpenGl_Group* theGroup);
38 //! Checks to see if the element contains ray-trace geometry.
39 Standard_Boolean IsRaytracedElement (const OpenGl_ElementNode* theNode);
41 //! Checks to see if the element contains ray-trace geometry.
42 Standard_Boolean IsRaytracedElement (const OpenGl_Element* theElement);
45 //! Stores properties of surface material.
46 class OpenGl_RaytraceMaterial
50 //! Ambient reflection coefficient.
53 //! Diffuse reflection coefficient.
56 //! Glossy reflection coefficient.
59 //! Material emission.
62 //! Specular reflection coefficient.
65 //! Specular refraction coefficient.
68 //! Material transparency.
69 BVH_Vec4f Transparency;
71 //! Texture transformation matrix.
72 BVH_Mat4f TextureTransform;
74 //! Physically-based material properties (used in path tracing engine).
77 //! Weight of the diffuse BRDF.
80 //! Weight of the reflection BRDF.
83 //! Weight of the transmission BTDF.
86 //! Weight of the Blinn's glossy BRDF.
89 //! Self-emitted radiance.
92 //! Fresnel coefficients.
95 //! Absorption color for the transmission BSDF.
102 //! Creates new default material.
103 OpenGl_RaytraceMaterial();
105 //! Creates new material with specified properties.
106 OpenGl_RaytraceMaterial (const BVH_Vec4f& theAmbient,
107 const BVH_Vec4f& theDiffuse,
108 const BVH_Vec4f& theSpecular);
110 //! Creates new material with specified properties.
111 OpenGl_RaytraceMaterial (const BVH_Vec4f& theAmbient,
112 const BVH_Vec4f& theDiffuse,
113 const BVH_Vec4f& theSpecular,
114 const BVH_Vec4f& theEmission,
115 const BVH_Vec4f& theTranspar);
117 //! Creates new material with specified properties.
118 OpenGl_RaytraceMaterial (const BVH_Vec4f& theAmbient,
119 const BVH_Vec4f& theDiffuse,
120 const BVH_Vec4f& theSpecular,
121 const BVH_Vec4f& theEmission,
122 const BVH_Vec4f& theTranspar,
123 const BVH_Vec4f& theReflection,
124 const BVH_Vec4f& theRefraction);
126 //! Returns packed (serialized) representation of material.
127 const Standard_ShortReal* Packed()
129 return reinterpret_cast<Standard_ShortReal*> (this);
133 //! Stores properties of OpenGL light source.
134 class OpenGl_RaytraceLight
138 //! Diffuse intensity (in terms of OpenGL).
141 //! Position of light source (in terms of OpenGL).
146 //! Creates new light source.
147 OpenGl_RaytraceLight() { }
149 //! Creates new light source.
150 OpenGl_RaytraceLight (const BVH_Vec4f& theEmission,
151 const BVH_Vec4f& thePosition);
153 //! Returns packed (serialized) representation of light source.
154 const Standard_ShortReal* Packed()
156 return reinterpret_cast<Standard_ShortReal*> (this);
160 //! Shared pointer to quad BVH (QBVH) tree.
161 typedef NCollection_Handle<BVH_Tree<Standard_ShortReal, 3, BVH_QuadTree> > QuadBvhHandle;
163 //! Triangulation of single OpenGL primitive array.
164 class OpenGl_TriangleSet : public BVH_Triangulation<Standard_ShortReal, 3>
168 //! Value of invalid material index to return in case of errors.
169 static const Standard_Integer INVALID_MATERIAL = -1;
173 //! Creates new OpenGL element triangulation.
174 OpenGl_TriangleSet (const Standard_Size theArrayID);
176 //! Returns ID of associated primitive array.
177 Standard_Size AssociatedPArrayID() const
182 //! Returns material index of triangle set.
183 Standard_Integer MaterialIndex() const
185 if (Elements.size() == 0)
187 return INVALID_MATERIAL;
190 return Elements.front().w();
193 //! Sets material index for entire triangle set.
194 void SetMaterialIndex (Standard_Integer theMatID)
196 for (Standard_Size anIdx = 0; anIdx < Elements.size(); ++anIdx)
198 Elements[anIdx].w() = theMatID;
202 //! Returns AABB of primitive set.
203 BVH_BoxNt Box() const;
205 //! Returns AABB of the given object.
206 using BVH_Triangulation<Standard_ShortReal, 3>::Box;
208 //! Returns centroid position along the given axis.
209 Standard_ShortReal Center (const Standard_Integer theIndex, const Standard_Integer theAxis) const;
211 //! Returns quad BVH (QBVH) tree produced from binary BVH.
212 const QuadBvhHandle& QuadBVH();
216 BVH_Array3f Normals; //!< Array of vertex normals.
217 BVH_Array2f TexCrds; //!< Array of texture coords.
221 Standard_Size myArrayID; //!< ID of associated primitive array.
223 QuadBvhHandle myQuadBVH; //!< QBVH produced from binary BVH tree.
227 //! Stores geometry of ray-tracing scene.
228 class OpenGl_RaytraceGeometry : public BVH_Geometry<Standard_ShortReal, 3>
232 //! Value of invalid offset to return in case of errors.
233 static const Standard_Integer INVALID_OFFSET = -1;
235 //! Maximum number of textures used in ray-tracing shaders.
236 //! This is not restriction of the solution implemented, but
237 //! rather the reasonable limit of the number of textures in
238 //! various applications (can be increased if needed).
239 static const Standard_Integer MAX_TEX_NUMBER = 32;
243 //! Array of properties of light sources.
244 std::vector<OpenGl_RaytraceLight,
245 NCollection_StdAllocator<OpenGl_RaytraceLight> > Sources;
247 //! Array of 'front' material properties.
248 std::vector<OpenGl_RaytraceMaterial,
249 NCollection_StdAllocator<OpenGl_RaytraceMaterial> > Materials;
251 //! Global ambient from all light sources.
256 //! Creates uninitialized ray-tracing geometry.
257 OpenGl_RaytraceGeometry()
258 : BVH_Geometry<Standard_ShortReal, 3>(),
259 myTopLevelTreeDepth (0),
260 myBotLevelTreeDepth (0)
265 //! Releases resources of ray-tracing geometry.
266 ~OpenGl_RaytraceGeometry()
271 //! Clears only ray-tracing materials.
272 void ClearMaterials()
274 std::vector<OpenGl_RaytraceMaterial,
275 NCollection_StdAllocator<OpenGl_RaytraceMaterial> > anEmptyMaterials;
277 Materials.swap (anEmptyMaterials);
282 //! Clears ray-tracing geometry.
285 public: //! @name methods related to acceleration structure
287 //! Performs post-processing of high-level scene BVH.
288 Standard_Boolean ProcessAcceleration();
290 //! Returns offset of bottom-level BVH for given leaf node.
291 //! If the node index is not valid the function returns -1.
292 //! @note Can be used after processing acceleration structure.
293 Standard_Integer AccelerationOffset (Standard_Integer theNodeIdx);
295 //! Returns offset of triangulation vertices for given leaf node.
296 //! If the node index is not valid the function returns -1.
297 //! @note Can be used after processing acceleration structure.
298 Standard_Integer VerticesOffset (Standard_Integer theNodeIdx);
300 //! Returns offset of triangulation elements for given leaf node.
301 //! If the node index is not valid the function returns -1.
302 //! @note Can be used after processing acceleration structure.
303 Standard_Integer ElementsOffset (Standard_Integer theNodeIdx);
305 //! Returns triangulation data for given leaf node.
306 //! If the node index is not valid the function returns NULL.
307 //! @note Can be used after processing acceleration structure.
308 OpenGl_TriangleSet* TriangleSet (Standard_Integer theNodeIdx);
310 //! Returns quad BVH (QBVH) tree produced from binary BVH.
311 const QuadBvhHandle& QuadBVH();
313 public: //! @name methods related to texture management
315 //! Checks if scene contains textured objects.
316 Standard_Boolean HasTextures() const
318 return !myTextures.IsEmpty();
321 //! Adds new OpenGL texture to the scene and returns its index.
322 Standard_Integer AddTexture (const Handle(OpenGl_Texture)& theTexture);
324 //! Updates unique 64-bit texture handles to use in shaders.
325 Standard_Boolean UpdateTextureHandles (const Handle(OpenGl_Context)& theContext);
327 //! Makes the OpenGL texture handles resident (must be called before using).
328 Standard_Boolean AcquireTextures (const Handle(OpenGl_Context)& theContext) const;
330 //! Makes the OpenGL texture handles non-resident (must be called after using).
331 Standard_Boolean ReleaseTextures (const Handle(OpenGl_Context)& theContext) const;
333 //! Returns array of texture handles.
334 const std::vector<GLuint64>& TextureHandles() const
336 return myTextureHandles;
339 //! Releases OpenGL resources.
340 void ReleaseResources (const Handle(OpenGl_Context)& theContext)
342 if (!myTextureSampler.IsNull())
344 myTextureSampler->Release (theContext.operator->());
345 myTextureSampler.Nullify();
349 public: //! @name auxiliary methods
351 //! Returns depth of top-level scene BVH from last build.
352 Standard_Integer TopLevelTreeDepth() const
354 return myTopLevelTreeDepth;
357 //! Returns maximum depth of bottom-level scene BVHs from last build.
358 Standard_Integer BotLevelTreeDepth() const
360 return myBotLevelTreeDepth;
365 NCollection_Vector<Handle(OpenGl_Texture)> myTextures; //!< Array of texture maps shared between rendered objects
366 Handle(OpenGl_Sampler) myTextureSampler; //!< Sampler object providing fixed sampling params for texures
367 std::vector<GLuint64> myTextureHandles; //!< Array of unique 64-bit texture handles obtained from OpenGL
368 Standard_Integer myTopLevelTreeDepth; //!< Depth of high-level scene BVH from last build
369 Standard_Integer myBotLevelTreeDepth; //!< Maximum depth of bottom-level scene BVHs from last build
371 QuadBvhHandle myQuadBVH; //!< QBVH produced from binary BVH tree.