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_TextureBufferArb.hxx>
23 #include <OpenGl_Texture.hxx>
26 struct OpenGl_ElementNode;
28 class OpenGl_Structure;
29 class OpenGl_PrimitiveArray;
31 namespace OpenGl_Raytrace
33 //! Checks to see if the group contains ray-trace geometry.
34 Standard_Boolean IsRaytracedGroup (const OpenGl_Group* theGroup);
36 //! Checks to see if the element contains ray-trace geometry.
37 Standard_Boolean IsRaytracedElement (const OpenGl_ElementNode* theNode);
39 //! Checks to see if the element contains ray-trace geometry.
40 Standard_Boolean IsRaytracedElement (const OpenGl_Element* theElement);
42 //! Checks to see if the structure contains ray-trace geometry.
43 Standard_Boolean IsRaytracedStructure (const OpenGl_Structure* theStructure);
46 //! Stores properties of surface material.
47 class OpenGl_RaytraceMaterial
51 //! Ambient reflection coefficient.
54 //! Diffuse reflection coefficient.
57 //! Glossy reflection coefficient.
60 //! Material emission.
63 //! Specular reflection coefficient.
66 //! Specular refraction coefficient.
69 //! Material transparency.
70 BVH_Vec4f Transparency;
72 //! Texture transformation matrix.
73 BVH_Mat4f TextureTransform;
77 //! Creates new default material.
78 OpenGl_RaytraceMaterial();
80 //! Creates new material with specified properties.
81 OpenGl_RaytraceMaterial (const BVH_Vec4f& theAmbient,
82 const BVH_Vec4f& theDiffuse,
83 const BVH_Vec4f& theSpecular);
85 //! Creates new material with specified properties.
86 OpenGl_RaytraceMaterial (const BVH_Vec4f& theAmbient,
87 const BVH_Vec4f& theDiffuse,
88 const BVH_Vec4f& theSpecular,
89 const BVH_Vec4f& theEmission,
90 const BVH_Vec4f& theTranspar);
92 //! Creates new material with specified properties.
93 OpenGl_RaytraceMaterial (const BVH_Vec4f& theAmbient,
94 const BVH_Vec4f& theDiffuse,
95 const BVH_Vec4f& theSpecular,
96 const BVH_Vec4f& theEmission,
97 const BVH_Vec4f& theTranspar,
98 const BVH_Vec4f& theReflection,
99 const BVH_Vec4f& theRefraction);
101 //! Returns packed (serialized) representation of material.
102 const Standard_ShortReal* Packed()
104 return reinterpret_cast<Standard_ShortReal*> (this);
108 //! Stores properties of OpenGL light source.
109 class OpenGl_RaytraceLight
113 //! Diffuse intensity (in terms of OpenGL).
116 //! Position of light source (in terms of OpenGL).
121 //! Creates new light source.
122 OpenGl_RaytraceLight (const BVH_Vec4f& theDiffuse,
123 const BVH_Vec4f& thePosition);
125 //! Returns packed (serialized) representation of light source.
126 const Standard_ShortReal* Packed()
128 return reinterpret_cast<Standard_ShortReal*> (this);
132 //! Triangulation of single OpenGL primitive array.
133 class OpenGl_TriangleSet : public BVH_Triangulation<Standard_ShortReal, 3>
137 //! Value of invalid material index to return in case of errors.
138 static const Standard_Integer INVALID_MATERIAL = -1;
142 //! Creates new OpenGL element triangulation.
143 OpenGl_TriangleSet (const Standard_Size theArrayID)
144 : BVH_Triangulation<Standard_ShortReal, 3>(),
145 myArrayID (theArrayID)
150 //! Releases resources of OpenGL element triangulation.
151 ~OpenGl_TriangleSet()
156 //! Returns Id of associated primitive array.
157 const Standard_Size AssociatedPArrayID() const
162 //! Returns material index of triangle set.
163 Standard_Integer MaterialIndex() const
165 if (Elements.size() == 0)
167 return INVALID_MATERIAL;
170 return Elements.front().w();
173 //! Sets material index for entire triangle set.
174 void SetMaterialIndex (Standard_Integer theMatID)
176 for (Standard_Size anIdx = 0; anIdx < Elements.size(); ++anIdx)
178 Elements[anIdx].w() = theMatID;
182 //! Returns AABB of the given object.
183 using BVH_Triangulation<Standard_ShortReal, 3>::Box;
185 //! Returns AABB of primitive set.
186 BVH_BoxNt Box() const;
188 //! Returns centroid position along the given axis.
189 Standard_ShortReal Center (const Standard_Integer theIndex, const Standard_Integer theAxis) const;
193 BVH_Array3f Normals; //!< Array of vertex normals.
195 BVH_Array2f TexCrds; //!< Array of vertex UV coords.
199 Standard_Size myArrayID; //!< ID of associated primitive array.
203 //! Stores geometry of ray-tracing scene.
204 class OpenGl_RaytraceGeometry : public BVH_Geometry<Standard_ShortReal, 3>
208 //! Value of invalid offset to return in case of errors.
209 static const Standard_Integer INVALID_OFFSET = -1;
211 //! Maximum number of textures used in ray-tracing shaders.
212 //! This is not restriction of the solution implemented, but
213 //! rather the reasonable limit of the number of textures in
214 //! various applications (can be increased if needed).
215 static const Standard_Integer MAX_TEX_NUMBER = 32;
219 //! Array of properties of light sources.
220 std::vector<OpenGl_RaytraceLight,
221 NCollection_StdAllocator<OpenGl_RaytraceLight> > Sources;
223 //! Array of 'front' material properties.
224 std::vector<OpenGl_RaytraceMaterial,
225 NCollection_StdAllocator<OpenGl_RaytraceMaterial> > Materials;
227 //! Global ambient from all light sources.
232 //! Creates uninitialized ray-tracing geometry.
233 OpenGl_RaytraceGeometry()
234 : BVH_Geometry<Standard_ShortReal, 3>(),
235 myHighLevelTreeDepth (0),
236 myBottomLevelTreeDepth (0)
241 //! Releases resources of ray-tracing geometry.
242 ~OpenGl_RaytraceGeometry()
247 //! Clears only ray-tracing materials.
248 void ClearMaterials()
250 std::vector<OpenGl_RaytraceMaterial,
251 NCollection_StdAllocator<OpenGl_RaytraceMaterial> > anEmptyMaterials;
253 Materials.swap (anEmptyMaterials);
258 //! Clears ray-tracing geometry.
261 public: //! @name methods related to acceleration structure
263 //! Performs post-processing of high-level scene BVH.
264 Standard_Boolean ProcessAcceleration();
266 //! Returns offset of bottom-level BVH for given leaf node.
267 //! If the node index is not valid the function returns -1.
268 //! @note Can be used after processing acceleration structure.
269 Standard_Integer AccelerationOffset (Standard_Integer theNodeIdx);
271 //! Returns offset of triangulation vertices for given leaf node.
272 //! If the node index is not valid the function returns -1.
273 //! @note Can be used after processing acceleration structure.
274 Standard_Integer VerticesOffset (Standard_Integer theNodeIdx);
276 //! Returns offset of triangulation elements for given leaf node.
277 //! If the node index is not valid the function returns -1.
278 //! @note Can be used after processing acceleration structure.
279 Standard_Integer ElementsOffset (Standard_Integer theNodeIdx);
281 //! Returns triangulation data for given leaf node.
282 //! If the node index is not valid the function returns NULL.
283 //! @note Can be used after processing acceleration structure.
284 OpenGl_TriangleSet* TriangleSet (Standard_Integer theNodeIdx);
286 public: //! @name methods related to texture management
288 //! Adds new OpenGL texture to the scene and returns its index.
289 Standard_Integer AddTexture (const Handle(OpenGl_Texture)& theTexture);
291 //! Updates unique 64-bit texture handles to use in shaders.
292 Standard_Boolean UpdateTextureHandles (const Handle(OpenGl_Context)& theContext);
294 //! Makes the OpenGL texture handles resident (must be called before using).
295 Standard_Boolean AcquireTextures (const Handle(OpenGl_Context)& theContext) const;
297 //! Makes the OpenGL texture handles non-resident (must be called after using).
298 Standard_Boolean ReleaseTextures (const Handle(OpenGl_Context)& theContext) const;
300 //! Returns array of texture handles.
301 const std::vector<GLuint64>& TextureHandles() const
303 return myTextureHandles;
306 //! Checks if scene contains textured objects.
307 Standard_Boolean HasTextures() const
309 return !myTextures.IsEmpty();
312 public: //! @name auxiliary methods
314 //! Returns depth of high-level scene BVH from last build.
315 Standard_Integer HighLevelTreeDepth() const
317 return myHighLevelTreeDepth;
320 //! Returns maximum depth of bottom-level scene BVHs from last build.
321 Standard_Integer BottomLevelTreeDepth() const
323 return myBottomLevelTreeDepth;
328 NCollection_Vector<Handle(OpenGl_Texture)> myTextures; //!< Array of texture maps shared between rendered objects
329 std::vector<GLuint64> myTextureHandles; //!< Array of unique 64-bit texture handles obtained from OpenGL
330 Standard_Integer myHighLevelTreeDepth; //!< Depth of high-level scene BVH from last build
331 Standard_Integer myBottomLevelTreeDepth; //!< Maximum depth of bottom-level scene BVHs from last build