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 |
25 | class OpenGl_Element; |
b7cd4ba7 |
26 | struct OpenGl_ElementNode; |
27 | class OpenGl_Group; |
28 | class OpenGl_Structure; |
29 | class OpenGl_PrimitiveArray; |
e276548b |
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 | |
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. |
47 | class OpenGl_RaytraceMaterial |
48 | { |
49 | public: |
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 |
75 | public: |
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. |
109 | class OpenGl_RaytraceLight |
110 | { |
111 | public: |
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 | |
119 | public: |
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 |
133 | class OpenGl_TriangleSet : public BVH_Triangulation<Standard_ShortReal, 3> |
e276548b |
134 | { |
265d4508 |
135 | public: |
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 | |
140 | public: |
141 | |
265d4508 |
142 | //! Creates new OpenGL element triangulation. |
8d3f219f |
143 | OpenGl_TriangleSet (const Standard_Size theArrayID) |
25ef750e |
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) |
7c58a2ab |
177 | Elements[anIdx].w() = aMatID; |
178 | } |
179 | |
180 | //! Returns AABB of the given object. |
181 | using BVH_Triangulation<Standard_ShortReal, 4>::Box; |
182 | |
183 | //! Returns AABB of primitive set. |
184 | BVH_BoxNt Box() const |
185 | { |
186 | const BVH_Transform<Standard_ShortReal, 4>* aTransform = |
187 | dynamic_cast<const BVH_Transform<Standard_ShortReal, 4>* > (Properties().operator->()); |
188 | |
189 | BVH_BoxNt aBox = BVH_PrimitiveSet<Standard_ShortReal, 4>::Box(); |
190 | |
191 | if (aTransform) |
84c71f29 |
192 | { |
25ef750e |
193 | Elements[anIdx].w() = theMatID; |
84c71f29 |
194 | } |
84c71f29 |
195 | } |
196 | |
25ef750e |
197 | //! Returns AABB of primitive set. |
198 | BVH_BoxNt Box() const; |
199 | |
84c71f29 |
200 | public: |
201 | |
25ef750e |
202 | BVH_Array3f Normals; //!< Array of vertex normals. |
203 | |
204 | BVH_Array2f TexCrds; //!< Array of vertex UV coords. |
205 | |
84c71f29 |
206 | private: |
207 | |
8d3f219f |
208 | Standard_Size myArrayID; //!< Id of associated primitive array. |
84c71f29 |
209 | |
e276548b |
210 | }; |
211 | |
265d4508 |
212 | //! Stores geometry of ray-tracing scene. |
25ef750e |
213 | class OpenGl_RaytraceGeometry : public BVH_Geometry<Standard_ShortReal, 3> |
e276548b |
214 | { |
215 | public: |
216 | |
265d4508 |
217 | //! Value of invalid offset to return in case of errors. |
218 | static const Standard_Integer INVALID_OFFSET = -1; |
e276548b |
219 | |
25ef750e |
220 | //! Maximum number of textures used in ray-tracing shaders. |
221 | //! This is not restriction of the solution implemented, but |
222 | //! rather the reasonable limit of the number of textures in |
223 | //! various applications (can be increased if needed). |
224 | static const Standard_Integer MAX_TEX_NUMBER = 32; |
225 | |
e276548b |
226 | public: |
227 | |
265d4508 |
228 | //! Array of properties of light sources. |
229 | std::vector<OpenGl_RaytraceLight, |
230 | NCollection_StdAllocator<OpenGl_RaytraceLight> > Sources; |
e276548b |
231 | |
265d4508 |
232 | //! Array of 'front' material properties. |
233 | std::vector<OpenGl_RaytraceMaterial, |
234 | NCollection_StdAllocator<OpenGl_RaytraceMaterial> > Materials; |
e276548b |
235 | |
265d4508 |
236 | //! Global ambient from all light sources. |
fc73a202 |
237 | BVH_Vec4f Ambient; |
e276548b |
238 | |
265d4508 |
239 | public: |
e276548b |
240 | |
265d4508 |
241 | //! Creates uninitialized ray-tracing geometry. |
242 | OpenGl_RaytraceGeometry() |
25ef750e |
243 | : BVH_Geometry<Standard_ShortReal, 3>(), |
fc73a202 |
244 | myHighLevelTreeDepth (0), |
245 | myBottomLevelTreeDepth (0) |
265d4508 |
246 | { |
247 | // |
248 | } |
e276548b |
249 | |
265d4508 |
250 | //! Releases resources of ray-tracing geometry. |
251 | ~OpenGl_RaytraceGeometry() |
252 | { |
253 | // |
254 | } |
e276548b |
255 | |
84c71f29 |
256 | //! Clears only ray-tracing materials. |
257 | void ClearMaterials() |
258 | { |
259 | std::vector<OpenGl_RaytraceMaterial, |
260 | NCollection_StdAllocator<OpenGl_RaytraceMaterial> > anEmptyMaterials; |
25ef750e |
261 | |
84c71f29 |
262 | Materials.swap (anEmptyMaterials); |
25ef750e |
263 | |
264 | myTextures.Clear(); |
84c71f29 |
265 | } |
266 | |
25ef750e |
267 | //! Clears ray-tracing geometry. |
268 | void Clear(); |
269 | |
270 | public: //! @name methods related to acceleration structure |
e276548b |
271 | |
265d4508 |
272 | //! Performs post-processing of high-level scene BVH. |
273 | Standard_Boolean ProcessAcceleration(); |
e276548b |
274 | |
265d4508 |
275 | //! Returns offset of bottom-level BVH for given leaf node. |
276 | //! If the node index is not valid the function returns -1. |
277 | //! @note Can be used after processing acceleration structure. |
278 | Standard_Integer AccelerationOffset (Standard_Integer theNodeIdx); |
e276548b |
279 | |
265d4508 |
280 | //! Returns offset of triangulation vertices for given leaf node. |
281 | //! If the node index is not valid the function returns -1. |
282 | //! @note Can be used after processing acceleration structure. |
283 | Standard_Integer VerticesOffset (Standard_Integer theNodeIdx); |
e276548b |
284 | |
265d4508 |
285 | //! Returns offset of triangulation elements for given leaf node. |
286 | //! If the node index is not valid the function returns -1. |
287 | //! @note Can be used after processing acceleration structure. |
288 | Standard_Integer ElementsOffset (Standard_Integer theNodeIdx); |
e276548b |
289 | |
265d4508 |
290 | //! Returns triangulation data for given leaf node. |
291 | //! If the node index is not valid the function returns NULL. |
292 | //! @note Can be used after processing acceleration structure. |
293 | OpenGl_TriangleSet* TriangleSet (Standard_Integer theNodeIdx); |
fc73a202 |
294 | |
25ef750e |
295 | public: //! @name methods related to texture management |
296 | |
297 | //! Adds new OpenGL texture to the scene and returns its index. |
298 | Standard_Integer AddTexture (const Handle(OpenGl_Texture)& theTexture); |
299 | |
300 | //! Updates unique 64-bit texture handles to use in shaders. |
301 | Standard_Boolean UpdateTextureHandles (const Handle(OpenGl_Context)& theContext); |
302 | |
303 | //! Makes the OpenGL texture handles resident (must be called before using). |
304 | Standard_Boolean AcquireTextures (const Handle(OpenGl_Context)& theContext) const; |
305 | |
306 | //! Makes the OpenGL texture handles non-resident (must be called after using). |
307 | Standard_Boolean ReleaseTextures (const Handle(OpenGl_Context)& theContext) const; |
308 | |
309 | //! Returns array of texture handles. |
310 | const std::vector<GLuint64>& TextureHandles() const |
311 | { |
312 | return myTextureHandles; |
313 | } |
314 | |
315 | //! Checks if scene contains textured objects. |
316 | Standard_Integer HasTextures() const |
317 | { |
318 | return !myTextures.IsEmpty(); |
319 | } |
320 | |
321 | public: //! @name auxiliary methods |
322 | |
fc73a202 |
323 | //! Returns depth of high-level scene BVH from last build. |
324 | Standard_Integer HighLevelTreeDepth() const |
325 | { |
326 | return myHighLevelTreeDepth; |
327 | } |
328 | |
329 | //! Returns maximum depth of bottom-level scene BVHs from last build. |
330 | Standard_Integer BottomLevelTreeDepth() const |
331 | { |
332 | return myBottomLevelTreeDepth; |
333 | } |
334 | |
335 | protected: |
336 | |
25ef750e |
337 | NCollection_Vector<Handle(OpenGl_Texture)> myTextures; //!< Array of texture maps shared between rendered objects |
338 | std::vector<GLuint64> myTextureHandles; //!< Array of unique 64-bit texture handles obtained from OpenGL |
339 | Standard_Integer myHighLevelTreeDepth; //!< Depth of high-level scene BVH from last build |
340 | Standard_Integer myBottomLevelTreeDepth; //!< Maximum depth of bottom-level scene BVHs from last build |
fc73a202 |
341 | |
e276548b |
342 | }; |
343 | |
344 | #endif |