0025442: Visualization, TKOpenGl - prevent inclusion of system header glxext.h
[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>
21#include <NCollection_StdAllocator.hxx>
25ef750e 22#include <OpenGl_TextureBufferArb.hxx>
23#include <OpenGl_Texture.hxx>
b7cd4ba7 24
a89742cf 25class OpenGl_Element;
b7cd4ba7 26struct OpenGl_ElementNode;
27class OpenGl_Group;
28class OpenGl_Structure;
29class OpenGl_PrimitiveArray;
e276548b 30
31namespace 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
a89742cf 39 //! Checks to see if the element contains ray-trace geometry.
40 Standard_Boolean IsRaytracedElement (const OpenGl_Element* theElement);
41
e276548b 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.
47class OpenGl_RaytraceMaterial
48{
49public:
50
51 //! Ambient reflection coefficient.
265d4508 52 BVH_Vec4f Ambient;
e276548b 53
54 //! Diffuse reflection coefficient.
265d4508 55 BVH_Vec4f Diffuse;
e276548b 56
57 //! Glossy reflection coefficient.
265d4508 58 BVH_Vec4f Specular;
e276548b 59
60 //! Material emission.
265d4508 61 BVH_Vec4f Emission;
e276548b 62
63 //! Specular reflection coefficient.
265d4508 64 BVH_Vec4f Reflection;
e276548b 65
66 //! Specular refraction coefficient.
265d4508 67 BVH_Vec4f Refraction;
e276548b 68
69 //! Material transparency.
265d4508 70 BVH_Vec4f Transparency;
e276548b 71
25ef750e 72 //! Texture transformation matrix.
73 BVH_Mat4f TextureTransform;
74
e276548b 75public:
76
77 //! Creates new default material.
78 OpenGl_RaytraceMaterial();
79
80 //! Creates new material with specified properties.
265d4508 81 OpenGl_RaytraceMaterial (const BVH_Vec4f& theAmbient,
82 const BVH_Vec4f& theDiffuse,
83 const BVH_Vec4f& theSpecular);
e276548b 84
85 //! Creates new material with specified properties.
265d4508 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);
e276548b 91
92 //! Creates new material with specified properties.
265d4508 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);
e276548b 100
101 //! Returns packed (serialized) representation of material.
265d4508 102 const Standard_ShortReal* Packed()
103 {
104 return reinterpret_cast<Standard_ShortReal*> (this);
105 }
e276548b 106};
107
108//! Stores properties of OpenGL light source.
109class OpenGl_RaytraceLight
110{
111public:
112
265d4508 113 //! Diffuse intensity (in terms of OpenGL).
114 BVH_Vec4f Diffuse;
e276548b 115
116 //! Position of light source (in terms of OpenGL).
265d4508 117 BVH_Vec4f Position;
e276548b 118
119public:
120
121 //! Creates new light source.
265d4508 122 OpenGl_RaytraceLight (const BVH_Vec4f& theDiffuse,
123 const BVH_Vec4f& thePosition);
e276548b 124
125 //! Returns packed (serialized) representation of light source.
265d4508 126 const Standard_ShortReal* Packed()
127 {
128 return reinterpret_cast<Standard_ShortReal*> (this);
129 }
e276548b 130};
131
265d4508 132//! Triangulation of single OpenGL primitive array.
25ef750e 133class OpenGl_TriangleSet : public BVH_Triangulation<Standard_ShortReal, 3>
e276548b 134{
265d4508 135public:
e276548b 136
84c71f29 137 //! Value of invalid material index to return in case of errors.
138 static const Standard_Integer INVALID_MATERIAL = -1;
e276548b 139
140public:
141
265d4508 142 //! Creates new OpenGL element triangulation.
8d3f219f 143 OpenGl_TriangleSet (const Standard_Size theArrayID)
50d0e1ce 144 : BVH_Triangulation<Standard_ShortReal, 3>(),
8d3f219f 145 myArrayID (theArrayID)
25ef750e 146 {
147 //
148 }
265d4508 149
150 //! Releases resources of OpenGL element triangulation.
151 ~OpenGl_TriangleSet()
152 {
153 //
154 }
84c71f29 155
8d3f219f 156 //! Returns Id of associated primitive array.
157 const Standard_Size AssociatedPArrayID() const
84c71f29 158 {
8d3f219f 159 return myArrayID;
84c71f29 160 }
161
162 //! Returns material index of triangle set.
163 Standard_Integer MaterialIndex() const
164 {
165 if (Elements.size() == 0)
25ef750e 166 {
84c71f29 167 return INVALID_MATERIAL;
25ef750e 168 }
84c71f29 169
170 return Elements.front().w();
171 }
172
173 //! Sets material index for entire triangle set.
25ef750e 174 void SetMaterialIndex (Standard_Integer theMatID)
84c71f29 175 {
176 for (Standard_Size anIdx = 0; anIdx < Elements.size(); ++anIdx)
84c71f29 177 {
25ef750e 178 Elements[anIdx].w() = theMatID;
84c71f29 179 }
84c71f29 180 }
181
5e6a855a 182 //! Returns AABB of the given object.
183 using BVH_Triangulation<Standard_ShortReal, 3>::Box;
184
25ef750e 185 //! Returns AABB of primitive set.
186 BVH_BoxNt Box() const;
187
50d0e1ce 188 //! Returns centroid position along the given axis.
189 Standard_ShortReal Center (const Standard_Integer theIndex, const Standard_Integer theAxis) const;
190
84c71f29 191public:
192
25ef750e 193 BVH_Array3f Normals; //!< Array of vertex normals.
194
195 BVH_Array2f TexCrds; //!< Array of vertex UV coords.
196
84c71f29 197private:
198
50d0e1ce 199 Standard_Size myArrayID; //!< ID of associated primitive array.
84c71f29 200
e276548b 201};
202
265d4508 203//! Stores geometry of ray-tracing scene.
25ef750e 204class OpenGl_RaytraceGeometry : public BVH_Geometry<Standard_ShortReal, 3>
e276548b 205{
206public:
207
265d4508 208 //! Value of invalid offset to return in case of errors.
209 static const Standard_Integer INVALID_OFFSET = -1;
e276548b 210
25ef750e 211 //! Maximum number of textures used in ray-tracing shaders.
212 //! This is not restriction of the solution implemented, but
213 //! rather the reasonable limit of the number of textures in
214 //! various applications (can be increased if needed).
215 static const Standard_Integer MAX_TEX_NUMBER = 32;
216
e276548b 217public:
218
265d4508 219 //! Array of properties of light sources.
220 std::vector<OpenGl_RaytraceLight,
221 NCollection_StdAllocator<OpenGl_RaytraceLight> > Sources;
e276548b 222
265d4508 223 //! Array of 'front' material properties.
224 std::vector<OpenGl_RaytraceMaterial,
225 NCollection_StdAllocator<OpenGl_RaytraceMaterial> > Materials;
e276548b 226
265d4508 227 //! Global ambient from all light sources.
fc73a202 228 BVH_Vec4f Ambient;
e276548b 229
265d4508 230public:
e276548b 231
265d4508 232 //! Creates uninitialized ray-tracing geometry.
233 OpenGl_RaytraceGeometry()
25ef750e 234 : BVH_Geometry<Standard_ShortReal, 3>(),
fc73a202 235 myHighLevelTreeDepth (0),
236 myBottomLevelTreeDepth (0)
265d4508 237 {
238 //
239 }
e276548b 240
265d4508 241 //! Releases resources of ray-tracing geometry.
242 ~OpenGl_RaytraceGeometry()
243 {
244 //
245 }
e276548b 246
84c71f29 247 //! Clears only ray-tracing materials.
248 void ClearMaterials()
249 {
250 std::vector<OpenGl_RaytraceMaterial,
251 NCollection_StdAllocator<OpenGl_RaytraceMaterial> > anEmptyMaterials;
25ef750e 252
84c71f29 253 Materials.swap (anEmptyMaterials);
25ef750e 254
255 myTextures.Clear();
84c71f29 256 }
257
25ef750e 258 //! Clears ray-tracing geometry.
259 void Clear();
260
261public: //! @name methods related to acceleration structure
e276548b 262
265d4508 263 //! Performs post-processing of high-level scene BVH.
264 Standard_Boolean ProcessAcceleration();
e276548b 265
265d4508 266 //! Returns offset of bottom-level BVH for given leaf node.
267 //! If the node index is not valid the function returns -1.
268 //! @note Can be used after processing acceleration structure.
269 Standard_Integer AccelerationOffset (Standard_Integer theNodeIdx);
e276548b 270
265d4508 271 //! Returns offset of triangulation vertices for given leaf node.
272 //! If the node index is not valid the function returns -1.
273 //! @note Can be used after processing acceleration structure.
274 Standard_Integer VerticesOffset (Standard_Integer theNodeIdx);
e276548b 275
265d4508 276 //! Returns offset of triangulation elements for given leaf node.
277 //! If the node index is not valid the function returns -1.
278 //! @note Can be used after processing acceleration structure.
279 Standard_Integer ElementsOffset (Standard_Integer theNodeIdx);
e276548b 280
265d4508 281 //! Returns triangulation data for given leaf node.
282 //! If the node index is not valid the function returns NULL.
283 //! @note Can be used after processing acceleration structure.
284 OpenGl_TriangleSet* TriangleSet (Standard_Integer theNodeIdx);
fc73a202 285
25ef750e 286public: //! @name methods related to texture management
287
288 //! Adds new OpenGL texture to the scene and returns its index.
289 Standard_Integer AddTexture (const Handle(OpenGl_Texture)& theTexture);
290
291 //! Updates unique 64-bit texture handles to use in shaders.
292 Standard_Boolean UpdateTextureHandles (const Handle(OpenGl_Context)& theContext);
293
294 //! Makes the OpenGL texture handles resident (must be called before using).
295 Standard_Boolean AcquireTextures (const Handle(OpenGl_Context)& theContext) const;
296
297 //! Makes the OpenGL texture handles non-resident (must be called after using).
298 Standard_Boolean ReleaseTextures (const Handle(OpenGl_Context)& theContext) const;
299
300 //! Returns array of texture handles.
301 const std::vector<GLuint64>& TextureHandles() const
302 {
303 return myTextureHandles;
304 }
305
306 //! Checks if scene contains textured objects.
307 Standard_Integer HasTextures() const
308 {
309 return !myTextures.IsEmpty();
310 }
311
312public: //! @name auxiliary methods
313
fc73a202 314 //! Returns depth of high-level scene BVH from last build.
315 Standard_Integer HighLevelTreeDepth() const
316 {
317 return myHighLevelTreeDepth;
318 }
319
320 //! Returns maximum depth of bottom-level scene BVHs from last build.
321 Standard_Integer BottomLevelTreeDepth() const
322 {
323 return myBottomLevelTreeDepth;
324 }
325
326protected:
327
25ef750e 328 NCollection_Vector<Handle(OpenGl_Texture)> myTextures; //!< Array of texture maps shared between rendered objects
329 std::vector<GLuint64> myTextureHandles; //!< Array of unique 64-bit texture handles obtained from OpenGL
330 Standard_Integer myHighLevelTreeDepth; //!< Depth of high-level scene BVH from last build
331 Standard_Integer myBottomLevelTreeDepth; //!< Maximum depth of bottom-level scene BVHs from last build
fc73a202 332
e276548b 333};
334
335#endif