0026292: Visualization - Parallelize queue-based BVH builders (subclasses of BVH_Queu...
[occt.git] / src / OpenGl / OpenGl_SceneGeometry.hxx
CommitLineData
e276548b 1// Created on: 2013-08-27
2// Created by: Denis BOGOLEPOV
d5f74e42 3// Copyright (c) 2013-2014 OPEN CASCADE SAS
e276548b 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
e276548b 6//
d5f74e42 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
973c2be1 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.
e276548b 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
e276548b 15
16#ifndef _OpenGl_SceneGeometry_Header
17#define _OpenGl_SceneGeometry_Header
18
265d4508 19#include <BVH_Geometry.hxx>
20#include <BVH_Triangulation.hxx>
65578e1c 21#include <BVH_BinnedBuilder.hxx>
265d4508 22#include <NCollection_StdAllocator.hxx>
25ef750e 23#include <OpenGl_TextureBufferArb.hxx>
24#include <OpenGl_Texture.hxx>
b7cd4ba7 25
a89742cf 26class OpenGl_Element;
b7cd4ba7 27struct OpenGl_ElementNode;
28class OpenGl_Group;
29class OpenGl_Structure;
30class OpenGl_PrimitiveArray;
e276548b 31
32namespace OpenGl_Raytrace
33{
34 //! Checks to see if the group contains ray-trace geometry.
35 Standard_Boolean IsRaytracedGroup (const OpenGl_Group* theGroup);
36
37 //! Checks to see if the element contains ray-trace geometry.
38 Standard_Boolean IsRaytracedElement (const OpenGl_ElementNode* theNode);
39
a89742cf 40 //! Checks to see if the element contains ray-trace geometry.
41 Standard_Boolean IsRaytracedElement (const OpenGl_Element* theElement);
42
e276548b 43 //! Checks to see if the structure contains ray-trace geometry.
44 Standard_Boolean IsRaytracedStructure (const OpenGl_Structure* theStructure);
45}
46
47//! Stores properties of surface material.
48class OpenGl_RaytraceMaterial
49{
50public:
51
52 //! Ambient reflection coefficient.
265d4508 53 BVH_Vec4f Ambient;
e276548b 54
55 //! Diffuse reflection coefficient.
265d4508 56 BVH_Vec4f Diffuse;
e276548b 57
58 //! Glossy reflection coefficient.
265d4508 59 BVH_Vec4f Specular;
e276548b 60
61 //! Material emission.
265d4508 62 BVH_Vec4f Emission;
e276548b 63
64 //! Specular reflection coefficient.
265d4508 65 BVH_Vec4f Reflection;
e276548b 66
67 //! Specular refraction coefficient.
265d4508 68 BVH_Vec4f Refraction;
e276548b 69
70 //! Material transparency.
265d4508 71 BVH_Vec4f Transparency;
e276548b 72
25ef750e 73 //! Texture transformation matrix.
74 BVH_Mat4f TextureTransform;
75
189f85a3 76 //! Physically-based material properties (used in path tracing engine).
77 struct Physical
78 {
79 //! Weight of the diffuse BRDF.
80 BVH_Vec4f Kd;
81
82 //! Weight of the reflection BRDF.
83 BVH_Vec4f Kr;
84
85 //! Weight of the transmission BTDF.
86 BVH_Vec4f Kt;
87
88 //! Weight of the Blinn's glossy BRDF.
89 BVH_Vec4f Ks;
90
91 //! Self-emitted radiance.
92 BVH_Vec4f Le;
93
94 //! Fresnel coefficients.
95 BVH_Vec4f Fresnel;
96
97 //! Absorption color for the transmission BSDF.
98 BVH_Vec4f Absorption;
99
100 } BSDF;
101
e276548b 102public:
103
104 //! Creates new default material.
105 OpenGl_RaytraceMaterial();
106
107 //! Creates new material with specified properties.
265d4508 108 OpenGl_RaytraceMaterial (const BVH_Vec4f& theAmbient,
109 const BVH_Vec4f& theDiffuse,
110 const BVH_Vec4f& theSpecular);
e276548b 111
112 //! Creates new material with specified properties.
265d4508 113 OpenGl_RaytraceMaterial (const BVH_Vec4f& theAmbient,
114 const BVH_Vec4f& theDiffuse,
115 const BVH_Vec4f& theSpecular,
116 const BVH_Vec4f& theEmission,
117 const BVH_Vec4f& theTranspar);
e276548b 118
119 //! Creates new material with specified properties.
265d4508 120 OpenGl_RaytraceMaterial (const BVH_Vec4f& theAmbient,
121 const BVH_Vec4f& theDiffuse,
122 const BVH_Vec4f& theSpecular,
123 const BVH_Vec4f& theEmission,
124 const BVH_Vec4f& theTranspar,
125 const BVH_Vec4f& theReflection,
126 const BVH_Vec4f& theRefraction);
e276548b 127
128 //! Returns packed (serialized) representation of material.
265d4508 129 const Standard_ShortReal* Packed()
130 {
131 return reinterpret_cast<Standard_ShortReal*> (this);
132 }
e276548b 133};
134
135//! Stores properties of OpenGL light source.
136class OpenGl_RaytraceLight
137{
138public:
139
265d4508 140 //! Diffuse intensity (in terms of OpenGL).
141 BVH_Vec4f Diffuse;
e276548b 142
143 //! Position of light source (in terms of OpenGL).
265d4508 144 BVH_Vec4f Position;
e276548b 145
146public:
147
148 //! Creates new light source.
265d4508 149 OpenGl_RaytraceLight (const BVH_Vec4f& theDiffuse,
150 const BVH_Vec4f& thePosition);
e276548b 151
152 //! Returns packed (serialized) representation of light source.
265d4508 153 const Standard_ShortReal* Packed()
154 {
155 return reinterpret_cast<Standard_ShortReal*> (this);
156 }
e276548b 157};
158
265d4508 159//! Triangulation of single OpenGL primitive array.
25ef750e 160class OpenGl_TriangleSet : public BVH_Triangulation<Standard_ShortReal, 3>
e276548b 161{
265d4508 162public:
e276548b 163
84c71f29 164 //! Value of invalid material index to return in case of errors.
165 static const Standard_Integer INVALID_MATERIAL = -1;
e276548b 166
167public:
168
265d4508 169 //! Creates new OpenGL element triangulation.
65578e1c 170 OpenGl_TriangleSet (const Standard_Size theArrayID);
265d4508 171
172 //! Releases resources of OpenGL element triangulation.
173 ~OpenGl_TriangleSet()
174 {
175 //
176 }
84c71f29 177
8d3f219f 178 //! Returns Id of associated primitive array.
179 const Standard_Size AssociatedPArrayID() const
84c71f29 180 {
8d3f219f 181 return myArrayID;
84c71f29 182 }
183
184 //! Returns material index of triangle set.
185 Standard_Integer MaterialIndex() const
186 {
187 if (Elements.size() == 0)
25ef750e 188 {
84c71f29 189 return INVALID_MATERIAL;
25ef750e 190 }
84c71f29 191
192 return Elements.front().w();
193 }
194
195 //! Sets material index for entire triangle set.
25ef750e 196 void SetMaterialIndex (Standard_Integer theMatID)
84c71f29 197 {
198 for (Standard_Size anIdx = 0; anIdx < Elements.size(); ++anIdx)
84c71f29 199 {
25ef750e 200 Elements[anIdx].w() = theMatID;
84c71f29 201 }
84c71f29 202 }
203
5e6a855a 204 //! Returns AABB of the given object.
205 using BVH_Triangulation<Standard_ShortReal, 3>::Box;
206
25ef750e 207 //! Returns AABB of primitive set.
208 BVH_BoxNt Box() const;
209
50d0e1ce 210 //! Returns centroid position along the given axis.
211 Standard_ShortReal Center (const Standard_Integer theIndex, const Standard_Integer theAxis) const;
212
84c71f29 213public:
214
25ef750e 215 BVH_Array3f Normals; //!< Array of vertex normals.
216
217 BVH_Array2f TexCrds; //!< Array of vertex UV coords.
218
84c71f29 219private:
220
50d0e1ce 221 Standard_Size myArrayID; //!< ID of associated primitive array.
84c71f29 222
e276548b 223};
224
265d4508 225//! Stores geometry of ray-tracing scene.
25ef750e 226class OpenGl_RaytraceGeometry : public BVH_Geometry<Standard_ShortReal, 3>
e276548b 227{
228public:
229
265d4508 230 //! Value of invalid offset to return in case of errors.
231 static const Standard_Integer INVALID_OFFSET = -1;
e276548b 232
25ef750e 233 //! Maximum number of textures used in ray-tracing shaders.
234 //! This is not restriction of the solution implemented, but
235 //! rather the reasonable limit of the number of textures in
236 //! various applications (can be increased if needed).
237 static const Standard_Integer MAX_TEX_NUMBER = 32;
238
e276548b 239public:
240
265d4508 241 //! Array of properties of light sources.
242 std::vector<OpenGl_RaytraceLight,
243 NCollection_StdAllocator<OpenGl_RaytraceLight> > Sources;
e276548b 244
265d4508 245 //! Array of 'front' material properties.
246 std::vector<OpenGl_RaytraceMaterial,
247 NCollection_StdAllocator<OpenGl_RaytraceMaterial> > Materials;
e276548b 248
265d4508 249 //! Global ambient from all light sources.
fc73a202 250 BVH_Vec4f Ambient;
e276548b 251
265d4508 252public:
e276548b 253
265d4508 254 //! Creates uninitialized ray-tracing geometry.
255 OpenGl_RaytraceGeometry()
25ef750e 256 : BVH_Geometry<Standard_ShortReal, 3>(),
fc73a202 257 myHighLevelTreeDepth (0),
258 myBottomLevelTreeDepth (0)
265d4508 259 {
260 //
261 }
e276548b 262
265d4508 263 //! Releases resources of ray-tracing geometry.
264 ~OpenGl_RaytraceGeometry()
265 {
266 //
267 }
e276548b 268
84c71f29 269 //! Clears only ray-tracing materials.
270 void ClearMaterials()
271 {
272 std::vector<OpenGl_RaytraceMaterial,
273 NCollection_StdAllocator<OpenGl_RaytraceMaterial> > anEmptyMaterials;
25ef750e 274
84c71f29 275 Materials.swap (anEmptyMaterials);
25ef750e 276
277 myTextures.Clear();
84c71f29 278 }
279
25ef750e 280 //! Clears ray-tracing geometry.
281 void Clear();
282
283public: //! @name methods related to acceleration structure
e276548b 284
265d4508 285 //! Performs post-processing of high-level scene BVH.
286 Standard_Boolean ProcessAcceleration();
e276548b 287
265d4508 288 //! Returns offset of bottom-level BVH for given leaf node.
289 //! If the node index is not valid the function returns -1.
290 //! @note Can be used after processing acceleration structure.
291 Standard_Integer AccelerationOffset (Standard_Integer theNodeIdx);
e276548b 292
265d4508 293 //! Returns offset of triangulation vertices for given leaf node.
294 //! If the node index is not valid the function returns -1.
295 //! @note Can be used after processing acceleration structure.
296 Standard_Integer VerticesOffset (Standard_Integer theNodeIdx);
e276548b 297
265d4508 298 //! Returns offset of triangulation elements for given leaf node.
299 //! If the node index is not valid the function returns -1.
300 //! @note Can be used after processing acceleration structure.
301 Standard_Integer ElementsOffset (Standard_Integer theNodeIdx);
e276548b 302
265d4508 303 //! Returns triangulation data for given leaf node.
304 //! If the node index is not valid the function returns NULL.
305 //! @note Can be used after processing acceleration structure.
306 OpenGl_TriangleSet* TriangleSet (Standard_Integer theNodeIdx);
fc73a202 307
25ef750e 308public: //! @name methods related to texture management
309
310 //! Adds new OpenGL texture to the scene and returns its index.
311 Standard_Integer AddTexture (const Handle(OpenGl_Texture)& theTexture);
312
313 //! Updates unique 64-bit texture handles to use in shaders.
314 Standard_Boolean UpdateTextureHandles (const Handle(OpenGl_Context)& theContext);
315
316 //! Makes the OpenGL texture handles resident (must be called before using).
317 Standard_Boolean AcquireTextures (const Handle(OpenGl_Context)& theContext) const;
318
319 //! Makes the OpenGL texture handles non-resident (must be called after using).
320 Standard_Boolean ReleaseTextures (const Handle(OpenGl_Context)& theContext) const;
321
322 //! Returns array of texture handles.
323 const std::vector<GLuint64>& TextureHandles() const
324 {
325 return myTextureHandles;
326 }
327
328 //! Checks if scene contains textured objects.
f483f2ed 329 Standard_Boolean HasTextures() const
25ef750e 330 {
331 return !myTextures.IsEmpty();
332 }
333
334public: //! @name auxiliary methods
335
fc73a202 336 //! Returns depth of high-level scene BVH from last build.
337 Standard_Integer HighLevelTreeDepth() const
338 {
339 return myHighLevelTreeDepth;
340 }
341
342 //! Returns maximum depth of bottom-level scene BVHs from last build.
343 Standard_Integer BottomLevelTreeDepth() const
344 {
345 return myBottomLevelTreeDepth;
346 }
347
348protected:
349
25ef750e 350 NCollection_Vector<Handle(OpenGl_Texture)> myTextures; //!< Array of texture maps shared between rendered objects
351 std::vector<GLuint64> myTextureHandles; //!< Array of unique 64-bit texture handles obtained from OpenGL
352 Standard_Integer myHighLevelTreeDepth; //!< Depth of high-level scene BVH from last build
353 Standard_Integer myBottomLevelTreeDepth; //!< Maximum depth of bottom-level scene BVHs from last build
fc73a202 354
e276548b 355};
356
357#endif