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