0025040: Visualization - capping plane is drawn at wrong position when created by...
[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>
b7cd4ba7 22
23struct OpenGl_ElementNode;
24class OpenGl_Group;
25class OpenGl_Structure;
26class OpenGl_PrimitiveArray;
e276548b 27
28namespace OpenGl_Raytrace
29{
30 //! Checks to see if the group contains ray-trace geometry.
31 Standard_Boolean IsRaytracedGroup (const OpenGl_Group* theGroup);
32
33 //! Checks to see if the element contains ray-trace geometry.
34 Standard_Boolean IsRaytracedElement (const OpenGl_ElementNode* theNode);
35
36 //! Checks to see if the structure contains ray-trace geometry.
37 Standard_Boolean IsRaytracedStructure (const OpenGl_Structure* theStructure);
38}
39
40//! Stores properties of surface material.
41class OpenGl_RaytraceMaterial
42{
43public:
44
45 //! Ambient reflection coefficient.
265d4508 46 BVH_Vec4f Ambient;
e276548b 47
48 //! Diffuse reflection coefficient.
265d4508 49 BVH_Vec4f Diffuse;
e276548b 50
51 //! Glossy reflection coefficient.
265d4508 52 BVH_Vec4f Specular;
e276548b 53
54 //! Material emission.
265d4508 55 BVH_Vec4f Emission;
e276548b 56
57 //! Specular reflection coefficient.
265d4508 58 BVH_Vec4f Reflection;
e276548b 59
60 //! Specular refraction coefficient.
265d4508 61 BVH_Vec4f Refraction;
e276548b 62
63 //! Material transparency.
265d4508 64 BVH_Vec4f Transparency;
e276548b 65
66public:
67
68 //! Creates new default material.
69 OpenGl_RaytraceMaterial();
70
71 //! Creates new material with specified properties.
265d4508 72 OpenGl_RaytraceMaterial (const BVH_Vec4f& theAmbient,
73 const BVH_Vec4f& theDiffuse,
74 const BVH_Vec4f& theSpecular);
e276548b 75
76 //! Creates new material with specified properties.
265d4508 77 OpenGl_RaytraceMaterial (const BVH_Vec4f& theAmbient,
78 const BVH_Vec4f& theDiffuse,
79 const BVH_Vec4f& theSpecular,
80 const BVH_Vec4f& theEmission,
81 const BVH_Vec4f& theTranspar);
e276548b 82
83 //! Creates new material with specified properties.
265d4508 84 OpenGl_RaytraceMaterial (const BVH_Vec4f& theAmbient,
85 const BVH_Vec4f& theDiffuse,
86 const BVH_Vec4f& theSpecular,
87 const BVH_Vec4f& theEmission,
88 const BVH_Vec4f& theTranspar,
89 const BVH_Vec4f& theReflection,
90 const BVH_Vec4f& theRefraction);
e276548b 91
92 //! Returns packed (serialized) representation of material.
265d4508 93 const Standard_ShortReal* Packed()
94 {
95 return reinterpret_cast<Standard_ShortReal*> (this);
96 }
e276548b 97};
98
99//! Stores properties of OpenGL light source.
100class OpenGl_RaytraceLight
101{
102public:
103
265d4508 104 //! Diffuse intensity (in terms of OpenGL).
105 BVH_Vec4f Diffuse;
e276548b 106
107 //! Position of light source (in terms of OpenGL).
265d4508 108 BVH_Vec4f Position;
e276548b 109
110public:
111
112 //! Creates new light source.
265d4508 113 OpenGl_RaytraceLight (const BVH_Vec4f& theDiffuse,
114 const BVH_Vec4f& thePosition);
e276548b 115
116 //! Returns packed (serialized) representation of light source.
265d4508 117 const Standard_ShortReal* Packed()
118 {
119 return reinterpret_cast<Standard_ShortReal*> (this);
120 }
e276548b 121};
122
265d4508 123//! Triangulation of single OpenGL primitive array.
124class OpenGl_TriangleSet : public BVH_Triangulation<Standard_ShortReal, 4>
e276548b 125{
265d4508 126public:
e276548b 127
84c71f29 128 //! Value of invalid material index to return in case of errors.
129 static const Standard_Integer INVALID_MATERIAL = -1;
e276548b 130
131public:
132
265d4508 133 //! Creates new OpenGL element triangulation.
84c71f29 134 OpenGl_TriangleSet (const OpenGl_PrimitiveArray* theArray = NULL)
135 : BVH_Triangulation<Standard_ShortReal, 4>(),
136 myArray (theArray)
137 {
138 //
139 }
265d4508 140
141 //! Releases resources of OpenGL element triangulation.
142 ~OpenGl_TriangleSet()
143 {
144 //
145 }
84c71f29 146
147 //! Returns associated OpenGl structure.
148 const OpenGl_PrimitiveArray* AssociatedPArray() const
149 {
150 return myArray;
151 }
152
153 //! Returns material index of triangle set.
154 Standard_Integer MaterialIndex() const
155 {
156 if (Elements.size() == 0)
157 return INVALID_MATERIAL;
158
159 return Elements.front().w();
160 }
161
162 //! Sets material index for entire triangle set.
163 void SetMaterialIndex (Standard_Integer aMatID)
164 {
165 for (Standard_Size anIdx = 0; anIdx < Elements.size(); ++anIdx)
166 Elements[anIdx].w() = aMatID;
167 }
168
169 //! Returns AABB of primitive set.
170 BVH_BoxNt Box() const
171 {
172 const BVH_Transform<Standard_ShortReal, 4>* aTransform =
173 dynamic_cast<const BVH_Transform<Standard_ShortReal, 4>* > (Properties().operator->());
174
175 BVH_BoxNt aBox = BVH_PrimitiveSet<Standard_ShortReal, 4>::Box();
176
177 if (aTransform)
178 {
179 return aTransform->Apply (aBox);
180 }
181
182 return aBox;
183 }
184
185public:
186
187 BVH_Array4f Normals; //!< Array of vertex normals.
188
189private:
190
191 const OpenGl_PrimitiveArray* myArray; //!< Reference to associated OpenGl structure.
192
e276548b 193};
194
265d4508 195//! Stores geometry of ray-tracing scene.
196class OpenGl_RaytraceGeometry : public BVH_Geometry<Standard_ShortReal, 4>
e276548b 197{
198public:
199
265d4508 200 //! Value of invalid offset to return in case of errors.
201 static const Standard_Integer INVALID_OFFSET = -1;
e276548b 202
e276548b 203public:
204
265d4508 205 //! Array of properties of light sources.
206 std::vector<OpenGl_RaytraceLight,
207 NCollection_StdAllocator<OpenGl_RaytraceLight> > Sources;
e276548b 208
265d4508 209 //! Array of 'front' material properties.
210 std::vector<OpenGl_RaytraceMaterial,
211 NCollection_StdAllocator<OpenGl_RaytraceMaterial> > Materials;
e276548b 212
265d4508 213 //! Global ambient from all light sources.
fc73a202 214 BVH_Vec4f Ambient;
e276548b 215
265d4508 216public:
e276548b 217
265d4508 218 //! Creates uninitialized ray-tracing geometry.
219 OpenGl_RaytraceGeometry()
fc73a202 220 : BVH_Geometry<Standard_ShortReal, 4>(),
221 myHighLevelTreeDepth (0),
222 myBottomLevelTreeDepth (0)
265d4508 223 {
224 //
225 }
e276548b 226
265d4508 227 //! Releases resources of ray-tracing geometry.
228 ~OpenGl_RaytraceGeometry()
229 {
230 //
231 }
e276548b 232
265d4508 233 //! Clears ray-tracing geometry.
234 void Clear();
e276548b 235
84c71f29 236 //! Clears only ray-tracing materials.
237 void ClearMaterials()
238 {
239 std::vector<OpenGl_RaytraceMaterial,
240 NCollection_StdAllocator<OpenGl_RaytraceMaterial> > anEmptyMaterials;
241
242 Materials.swap (anEmptyMaterials);
243 }
244
265d4508 245public:
e276548b 246
265d4508 247 //! Performs post-processing of high-level scene BVH.
248 Standard_Boolean ProcessAcceleration();
e276548b 249
265d4508 250 //! Returns offset of bottom-level BVH for given leaf node.
251 //! If the node index is not valid the function returns -1.
252 //! @note Can be used after processing acceleration structure.
253 Standard_Integer AccelerationOffset (Standard_Integer theNodeIdx);
e276548b 254
265d4508 255 //! Returns offset of triangulation vertices for given leaf node.
256 //! If the node index is not valid the function returns -1.
257 //! @note Can be used after processing acceleration structure.
258 Standard_Integer VerticesOffset (Standard_Integer theNodeIdx);
e276548b 259
265d4508 260 //! Returns offset of triangulation elements 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 ElementsOffset (Standard_Integer theNodeIdx);
e276548b 264
265d4508 265 //! Returns triangulation data for given leaf node.
266 //! If the node index is not valid the function returns NULL.
267 //! @note Can be used after processing acceleration structure.
268 OpenGl_TriangleSet* TriangleSet (Standard_Integer theNodeIdx);
fc73a202 269
270 //! Returns depth of high-level scene BVH from last build.
271 Standard_Integer HighLevelTreeDepth() const
272 {
273 return myHighLevelTreeDepth;
274 }
275
276 //! Returns maximum depth of bottom-level scene BVHs from last build.
277 Standard_Integer BottomLevelTreeDepth() const
278 {
279 return myBottomLevelTreeDepth;
280 }
281
282protected:
283
284 Standard_Integer myHighLevelTreeDepth; //!< Depth of high-level scene BVH from last build
285 Standard_Integer myBottomLevelTreeDepth; //!< Maximum depth of bottom-level scene BVHs from last build
286
e276548b 287};
288
289#endif