241e681e678ad0d422e13aea4c904a0d3b420226
[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_Header
17 #define _OpenGl_SceneGeometry_Header
18
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>
24
25 class  OpenGl_Element;
26 struct OpenGl_ElementNode;
27 class  OpenGl_Group;
28 class  OpenGl_Structure;
29 class  OpenGl_PrimitiveArray;
30
31 namespace OpenGl_Raytrace
32 {
33   //! Checks to see if the group contains ray-trace geometry.
34   Standard_Boolean IsRaytracedGroup (const OpenGl_Group* theGroup);
35
36   //! Checks to see if the element contains ray-trace geometry.
37   Standard_Boolean IsRaytracedElement (const OpenGl_ElementNode* theNode);
38
39   //! Checks to see if the element contains ray-trace geometry.
40   Standard_Boolean IsRaytracedElement (const OpenGl_Element* theElement);
41
42   //! Checks to see if the structure contains ray-trace geometry.
43   Standard_Boolean IsRaytracedStructure (const OpenGl_Structure* theStructure);
44 }
45
46 //! Stores properties of surface material.
47 class OpenGl_RaytraceMaterial
48 {
49 public:
50
51   //! Ambient reflection coefficient.
52   BVH_Vec4f Ambient;
53
54   //! Diffuse reflection coefficient.
55   BVH_Vec4f Diffuse;
56
57   //! Glossy reflection coefficient.
58   BVH_Vec4f Specular;
59
60   //! Material emission.
61   BVH_Vec4f Emission;
62
63   //! Specular reflection coefficient.
64   BVH_Vec4f Reflection;
65
66   //! Specular refraction coefficient.
67   BVH_Vec4f Refraction;
68
69   //! Material transparency.
70   BVH_Vec4f Transparency;
71
72   //! Texture transformation matrix.
73   BVH_Mat4f TextureTransform;
74
75 public:
76
77   //! Creates new default material.
78   OpenGl_RaytraceMaterial();
79
80   //! Creates new material with specified properties.
81   OpenGl_RaytraceMaterial (const BVH_Vec4f& theAmbient,
82                            const BVH_Vec4f& theDiffuse,
83                            const BVH_Vec4f& theSpecular);
84
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);
91
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);
100
101   //! Returns packed (serialized) representation of material.
102   const Standard_ShortReal* Packed()
103   {
104     return reinterpret_cast<Standard_ShortReal*> (this);
105   }
106 };
107
108 //! Stores properties of OpenGL light source.
109 class OpenGl_RaytraceLight
110 {
111 public:
112
113   //! Diffuse intensity (in terms of OpenGL).
114   BVH_Vec4f Diffuse;
115
116   //! Position of light source (in terms of OpenGL).
117   BVH_Vec4f Position;
118
119 public:
120
121   //! Creates new light source.
122   OpenGl_RaytraceLight (const BVH_Vec4f& theDiffuse,
123                         const BVH_Vec4f& thePosition);
124
125   //! Returns packed (serialized) representation of light source.
126   const Standard_ShortReal* Packed()
127   {
128     return reinterpret_cast<Standard_ShortReal*> (this);
129   }
130 };
131
132 //! Triangulation of single OpenGL primitive array.
133 class OpenGl_TriangleSet : public BVH_Triangulation<Standard_ShortReal, 3>
134 {
135 public:
136
137   //! Value of invalid material index to return in case of errors.
138   static const Standard_Integer INVALID_MATERIAL = -1;
139
140 public:
141
142   //! Creates new OpenGL element triangulation.
143   OpenGl_TriangleSet (const Standard_Size theArrayID)
144   : BVH_Triangulation<Standard_ShortReal, 3> (),
145     myArrayID (theArrayID)
146   {
147     //
148   }
149
150   //! Releases resources of OpenGL element triangulation.
151   ~OpenGl_TriangleSet()
152   {
153     //
154   }
155
156   //! Returns Id of associated primitive array.
157   const Standard_Size AssociatedPArrayID() const
158   {
159     return myArrayID;
160   }
161
162   //! Returns material index of triangle set.
163   Standard_Integer MaterialIndex() const
164   {
165     if (Elements.size() == 0)
166     {
167       return INVALID_MATERIAL;
168     }
169
170     return Elements.front().w();
171   }
172
173   //! Sets material index for entire triangle set.
174   void SetMaterialIndex (Standard_Integer theMatID)
175   {
176     for (Standard_Size anIdx = 0; anIdx < Elements.size(); ++anIdx)
177     {
178       Elements[anIdx].w() = theMatID;
179     }
180   }
181
182   //! Returns AABB of primitive set.
183   BVH_BoxNt Box() const;
184
185 public:
186
187   BVH_Array3f Normals; //!< Array of vertex normals.
188
189   BVH_Array2f TexCrds; //!< Array of vertex UV coords.
190
191 private:
192
193   Standard_Size myArrayID; //!< Id of associated primitive array.
194
195 };
196
197 //! Stores geometry of ray-tracing scene.
198 class OpenGl_RaytraceGeometry : public BVH_Geometry<Standard_ShortReal, 3>
199 {
200 public:
201
202   //! Value of invalid offset to return in case of errors.
203   static const Standard_Integer INVALID_OFFSET = -1;
204
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;
210
211 public:
212
213   //! Array of properties of light sources.
214   std::vector<OpenGl_RaytraceLight,
215     NCollection_StdAllocator<OpenGl_RaytraceLight> > Sources;
216
217   //! Array of 'front' material properties.
218   std::vector<OpenGl_RaytraceMaterial,
219     NCollection_StdAllocator<OpenGl_RaytraceMaterial> > Materials;
220
221   //! Global ambient from all light sources.
222   BVH_Vec4f Ambient;
223
224 public:
225
226   //! Creates uninitialized ray-tracing geometry.
227   OpenGl_RaytraceGeometry()
228   : BVH_Geometry<Standard_ShortReal, 3>(),
229     myHighLevelTreeDepth (0),
230     myBottomLevelTreeDepth (0)
231   {
232     //
233   }
234
235   //! Releases resources of ray-tracing geometry.
236   ~OpenGl_RaytraceGeometry()
237   {
238     //
239   }
240
241   //! Clears only ray-tracing materials.
242   void ClearMaterials()
243   {
244     std::vector<OpenGl_RaytraceMaterial,
245       NCollection_StdAllocator<OpenGl_RaytraceMaterial> > anEmptyMaterials;
246
247     Materials.swap (anEmptyMaterials);
248
249     myTextures.Clear();
250   }
251
252   //! Clears ray-tracing geometry.
253   void Clear();
254
255 public: //! @name methods related to acceleration structure
256
257   //! Performs post-processing of high-level scene BVH.
258   Standard_Boolean ProcessAcceleration();
259
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);
264
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);
269
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);
274
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);
279
280 public: //! @name methods related to texture management
281
282   //! Adds new OpenGL texture to the scene and returns its index.
283   Standard_Integer AddTexture (const Handle(OpenGl_Texture)& theTexture);
284
285   //! Updates unique 64-bit texture handles to use in shaders.
286   Standard_Boolean UpdateTextureHandles (const Handle(OpenGl_Context)& theContext);
287
288   //! Makes the OpenGL texture handles resident (must be called before using).
289   Standard_Boolean AcquireTextures (const Handle(OpenGl_Context)& theContext) const;
290
291   //! Makes the OpenGL texture handles non-resident (must be called after using).
292   Standard_Boolean ReleaseTextures (const Handle(OpenGl_Context)& theContext) const;
293
294   //! Returns array of texture handles.
295   const std::vector<GLuint64>& TextureHandles() const
296   {
297     return myTextureHandles;
298   }
299
300   //! Checks if scene contains textured objects.
301   Standard_Integer HasTextures() const
302   {
303     return !myTextures.IsEmpty();
304   }
305
306 public: //! @name auxiliary methods
307
308   //! Returns depth of high-level scene BVH from last build.
309   Standard_Integer HighLevelTreeDepth() const
310   {
311     return myHighLevelTreeDepth;
312   }
313
314   //! Returns maximum depth of bottom-level scene BVHs from last build.
315   Standard_Integer BottomLevelTreeDepth() const
316   {
317     return myBottomLevelTreeDepth;
318   }
319
320 protected:
321
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
326
327 };
328
329 #endif