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