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 primitive set.
183 BVH_BoxNt Box() const;
187 BVH_Array3f Normals; //!< Array of vertex normals.
189 BVH_Array2f TexCrds; //!< Array of vertex UV coords.
193 Standard_Size myArrayID; //!< Id of associated primitive array.
197 //! Stores geometry of ray-tracing scene.
198 class OpenGl_RaytraceGeometry : public BVH_Geometry<Standard_ShortReal, 3>
202 //! Value of invalid offset to return in case of errors.
203 static const Standard_Integer INVALID_OFFSET = -1;
205 //! Maximum number of textures used in ray-tracing shaders.
206 //! This is not restriction of the solution implemented, but
207 //! rather the reasonable limit of the number of textures in
208 //! various applications (can be increased if needed).
209 static const Standard_Integer MAX_TEX_NUMBER = 32;
213 //! Array of properties of light sources.
214 std::vector<OpenGl_RaytraceLight,
215 NCollection_StdAllocator<OpenGl_RaytraceLight> > Sources;
217 //! Array of 'front' material properties.
218 std::vector<OpenGl_RaytraceMaterial,
219 NCollection_StdAllocator<OpenGl_RaytraceMaterial> > Materials;
221 //! Global ambient from all light sources.
226 //! Creates uninitialized ray-tracing geometry.
227 OpenGl_RaytraceGeometry()
228 : BVH_Geometry<Standard_ShortReal, 3>(),
229 myHighLevelTreeDepth (0),
230 myBottomLevelTreeDepth (0)
235 //! Releases resources of ray-tracing geometry.
236 ~OpenGl_RaytraceGeometry()
241 //! Clears only ray-tracing materials.
242 void ClearMaterials()
244 std::vector<OpenGl_RaytraceMaterial,
245 NCollection_StdAllocator<OpenGl_RaytraceMaterial> > anEmptyMaterials;
247 Materials.swap (anEmptyMaterials);
252 //! Clears ray-tracing geometry.
255 public: //! @name methods related to acceleration structure
257 //! Performs post-processing of high-level scene BVH.
258 Standard_Boolean ProcessAcceleration();
260 //! Returns offset of bottom-level BVH 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 AccelerationOffset (Standard_Integer theNodeIdx);
265 //! Returns offset of triangulation vertices for given leaf node.
266 //! If the node index is not valid the function returns -1.
267 //! @note Can be used after processing acceleration structure.
268 Standard_Integer VerticesOffset (Standard_Integer theNodeIdx);
270 //! Returns offset of triangulation elements for given leaf node.
271 //! If the node index is not valid the function returns -1.
272 //! @note Can be used after processing acceleration structure.
273 Standard_Integer ElementsOffset (Standard_Integer theNodeIdx);
275 //! Returns triangulation data for given leaf node.
276 //! If the node index is not valid the function returns NULL.
277 //! @note Can be used after processing acceleration structure.
278 OpenGl_TriangleSet* TriangleSet (Standard_Integer theNodeIdx);
280 public: //! @name methods related to texture management
282 //! Adds new OpenGL texture to the scene and returns its index.
283 Standard_Integer AddTexture (const Handle(OpenGl_Texture)& theTexture);
285 //! Updates unique 64-bit texture handles to use in shaders.
286 Standard_Boolean UpdateTextureHandles (const Handle(OpenGl_Context)& theContext);
288 //! Makes the OpenGL texture handles resident (must be called before using).
289 Standard_Boolean AcquireTextures (const Handle(OpenGl_Context)& theContext) const;
291 //! Makes the OpenGL texture handles non-resident (must be called after using).
292 Standard_Boolean ReleaseTextures (const Handle(OpenGl_Context)& theContext) const;
294 //! Returns array of texture handles.
295 const std::vector<GLuint64>& TextureHandles() const
297 return myTextureHandles;
300 //! Checks if scene contains textured objects.
301 Standard_Integer HasTextures() const
303 return !myTextures.IsEmpty();
306 public: //! @name auxiliary methods
308 //! Returns depth of high-level scene BVH from last build.
309 Standard_Integer HighLevelTreeDepth() const
311 return myHighLevelTreeDepth;
314 //! Returns maximum depth of bottom-level scene BVHs from last build.
315 Standard_Integer BottomLevelTreeDepth() const
317 return myBottomLevelTreeDepth;
322 NCollection_Vector<Handle(OpenGl_Texture)> myTextures; //!< Array of texture maps shared between rendered objects
323 std::vector<GLuint64> myTextureHandles; //!< Array of unique 64-bit texture handles obtained from OpenGL
324 Standard_Integer myHighLevelTreeDepth; //!< Depth of high-level scene BVH from last build
325 Standard_Integer myBottomLevelTreeDepth; //!< Maximum depth of bottom-level scene BVHs from last build