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