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