0024572: Improvement performance of BRepExtrema_DistShapeShape
[occt.git] / src / OpenGl / OpenGl_Workspace_Raytrace.cxx
CommitLineData
e276548b 1// Created on: 2013-08-27
2// Created by: Denis BOGOLEPOV
3// Copyright (c) 2013 OPEN CASCADE SAS
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
fc73a202 16#include <NCollection_Mat4.hxx>
17#include <OpenGl_ArbFBO.hxx>
18#include <OpenGl_FrameBuffer.hxx>
e276548b 19#include <OpenGl_Texture.hxx>
fc73a202 20#include <OpenGl_VertexBuffer.hxx>
e276548b 21#include <OpenGl_View.hxx>
22#include <OpenGl_Workspace.hxx>
fc73a202 23#include <OSD_File.hxx>
24#include <OSD_Protection.hxx>
265d4508 25#include <Standard_Assert.hxx>
e276548b 26
27using namespace OpenGl_Raytrace;
28
29//! Use this macro to output ray-tracing debug info
fc73a202 30// #define RAY_TRACE_PRINT_INFO
e276548b 31
32#ifdef RAY_TRACE_PRINT_INFO
33 #include <OSD_Timer.hxx>
34#endif
35
e276548b 36// =======================================================================
37// function : MatVecMult
38// purpose : Multiples 4x4 matrix by 4D vector
39// =======================================================================
265d4508 40template<typename T>
41BVH_Vec4f MatVecMult (const T m[16], const BVH_Vec4f& v)
e276548b 42{
265d4508 43 return BVH_Vec4f (
e276548b 44 static_cast<float> (m[ 0] * v.x() + m[ 4] * v.y() +
45 m[ 8] * v.z() + m[12] * v.w()),
46 static_cast<float> (m[ 1] * v.x() + m[ 5] * v.y() +
47 m[ 9] * v.z() + m[13] * v.w()),
48 static_cast<float> (m[ 2] * v.x() + m[ 6] * v.y() +
49 m[10] * v.z() + m[14] * v.w()),
50 static_cast<float> (m[ 3] * v.x() + m[ 7] * v.y() +
51 m[11] * v.z() + m[15] * v.w()));
52}
53
54// =======================================================================
e276548b 55// function : UpdateRaytraceGeometry
56// purpose : Updates 3D scene geometry for ray tracing
57// =======================================================================
58Standard_Boolean OpenGl_Workspace::UpdateRaytraceGeometry (Standard_Boolean theCheck)
59{
60 if (myView.IsNull())
61 return Standard_False;
62
63 // Note: In 'check' mode the scene geometry is analyzed for modifications
64 // This is light-weight procedure performed for each frame
65
66 if (!theCheck)
67 {
265d4508 68 myRaytraceGeometry.Clear();
e276548b 69
70 myIsRaytraceDataValid = Standard_False;
71 }
72 else
73 {
74 if (myLayersModificationStatus != myView->LayerList().ModificationState())
75 {
76 return UpdateRaytraceGeometry (Standard_False);
77 }
78 }
79
265d4508 80 Standard_ShortReal* aTransform (NULL);
e276548b 81
82 // The set of processed structures (reflected to ray-tracing)
83 // This set is used to remove out-of-date records from the
84 // hash map of structures
85 std::set<const OpenGl_Structure*> anElements;
86
87 const OpenGl_LayerList& aList = myView->LayerList();
88
89 for (OpenGl_SequenceOfLayers::Iterator anLayerIt (aList.Layers()); anLayerIt.More(); anLayerIt.Next())
90 {
91 const OpenGl_PriorityList& aPriorityList = anLayerIt.Value();
92
93 if (aPriorityList.NbStructures() == 0)
94 continue;
95
96 const OpenGl_ArrayOfStructure& aStructArray = aPriorityList.ArrayOfStructures();
97
68333c8f 98 for (Standard_Integer anIndex = 0; anIndex < aStructArray.Length(); ++anIndex)
e276548b 99 {
100 OpenGl_SequenceOfStructure::Iterator aStructIt;
101
102 for (aStructIt.Init (aStructArray (anIndex)); aStructIt.More(); aStructIt.Next())
103 {
104 const OpenGl_Structure* aStructure = aStructIt.Value();
105
106 if (theCheck)
107 {
108 if (CheckRaytraceStructure (aStructure))
109 {
110 return UpdateRaytraceGeometry (Standard_False);
111 }
112 }
113 else
114 {
115 if (!aStructure->IsRaytracable())
116 continue;
117
118 if (aStructure->Transformation()->mat != NULL)
119 {
120 if (aTransform == NULL)
265d4508 121 aTransform = new Standard_ShortReal[16];
e276548b 122
68333c8f 123 for (Standard_Integer i = 0; i < 4; ++i)
124 for (Standard_Integer j = 0; j < 4; ++j)
e276548b 125 {
126 aTransform[j * 4 + i] = aStructure->Transformation()->mat[i][j];
127 }
128 }
129
130 AddRaytraceStructure (aStructure, aTransform, anElements);
131 }
132 }
133 }
134 }
135
136 if (!theCheck)
137 {
138 // Actualize the hash map of structures -- remove out-of-date records
139 std::map<const OpenGl_Structure*, Standard_Size>::iterator anIter = myStructureStates.begin();
140
141 while (anIter != myStructureStates.end())
142 {
143 if (anElements.find (anIter->first) == anElements.end())
144 {
145 myStructureStates.erase (anIter++);
146 }
147 else
148 {
149 ++anIter;
150 }
151 }
152
153 // Actualize OpenGL layer list state
154 myLayersModificationStatus = myView->LayerList().ModificationState();
155
265d4508 156 // Rebuild bottom-level and high-level BVHs
157 myRaytraceGeometry.ProcessAcceleration();
e276548b 158
265d4508 159 const Standard_ShortReal aMinRadius = Max (fabs (myRaytraceGeometry.Box().CornerMin().x()), Max (
160 fabs (myRaytraceGeometry.Box().CornerMin().y()), fabs (myRaytraceGeometry.Box().CornerMin().z())));
161 const Standard_ShortReal aMaxRadius = Max (fabs (myRaytraceGeometry.Box().CornerMax().x()), Max (
162 fabs (myRaytraceGeometry.Box().CornerMax().y()), fabs (myRaytraceGeometry.Box().CornerMax().z())));
e276548b 163
265d4508 164 myRaytraceSceneRadius = 2.f /* scale factor */ * Max (aMinRadius, aMaxRadius);
e276548b 165
fc73a202 166 const BVH_Vec4f aSize = myRaytraceGeometry.Box().Size();
e276548b 167
fc73a202 168 myRaytraceSceneEpsilon = Max (1e-4f, 1e-4f * sqrtf (
169 aSize.x() * aSize.x() + aSize.y() * aSize.y() + aSize.z() * aSize.z()));
170
171 return UploadRaytraceData();
e276548b 172 }
173
174 delete [] aTransform;
175
176 return Standard_True;
177}
178
179// =======================================================================
180// function : CheckRaytraceStructure
265d4508 181// purpose : Checks to see if the structure is modified
e276548b 182// =======================================================================
183Standard_Boolean OpenGl_Workspace::CheckRaytraceStructure (const OpenGl_Structure* theStructure)
184{
185 if (!theStructure->IsRaytracable())
186 {
187 // Checks to see if all ray-tracable elements were
188 // removed from the structure
189 if (theStructure->ModificationState() > 0)
190 {
191 theStructure->ResetModificationState();
192 return Standard_True;
193 }
194
195 return Standard_False;
196 }
197
198 std::map<const OpenGl_Structure*, Standard_Size>::iterator aStructState = myStructureStates.find (theStructure);
199
200 if (aStructState != myStructureStates.end())
201 return aStructState->second != theStructure->ModificationState();
202
203 return Standard_True;
204}
205
206// =======================================================================
207// function : CreateMaterial
208// purpose : Creates ray-tracing material properties
209// =======================================================================
265d4508 210void CreateMaterial (const OPENGL_SURF_PROP& theProp, OpenGl_RaytraceMaterial& theMaterial)
e276548b 211{
64c759f8 212 const float* aSrcAmb = theProp.isphysic ? theProp.ambcol.rgb : theProp.matcol.rgb;
265d4508 213 theMaterial.Ambient = BVH_Vec4f (aSrcAmb[0] * theProp.amb,
214 aSrcAmb[1] * theProp.amb,
215 aSrcAmb[2] * theProp.amb,
216 1.0f);
64c759f8 217
218 const float* aSrcDif = theProp.isphysic ? theProp.difcol.rgb : theProp.matcol.rgb;
265d4508 219 theMaterial.Diffuse = BVH_Vec4f (aSrcDif[0] * theProp.diff,
220 aSrcDif[1] * theProp.diff,
221 aSrcDif[2] * theProp.diff,
222 1.0f);
64c759f8 223
224 const float aDefSpecCol[4] = {1.0f, 1.0f, 1.0f, 1.0f};
225 const float* aSrcSpe = theProp.isphysic ? theProp.speccol.rgb : aDefSpecCol;
265d4508 226 theMaterial.Specular = BVH_Vec4f (aSrcSpe[0] * theProp.spec,
227 aSrcSpe[1] * theProp.spec,
228 aSrcSpe[2] * theProp.spec,
229 theProp.shine);
e276548b 230
64c759f8 231 const float* aSrcEms = theProp.isphysic ? theProp.emscol.rgb : theProp.matcol.rgb;
265d4508 232 theMaterial.Emission = BVH_Vec4f (aSrcEms[0] * theProp.emsv,
233 aSrcEms[1] * theProp.emsv,
234 aSrcEms[2] * theProp.emsv,
235 1.0f);
e276548b 236
237 // Note: Here we use sub-linear transparency function
238 // to produce realistic-looking transparency effect
265d4508 239 theMaterial.Transparency = BVH_Vec4f (powf (theProp.trans, 0.75f),
240 1.f - theProp.trans,
241 1.f,
242 1.f);
e276548b 243
244 const float aMaxRefl = Max (theMaterial.Diffuse.x() + theMaterial.Specular.x(),
245 Max (theMaterial.Diffuse.y() + theMaterial.Specular.y(),
246 theMaterial.Diffuse.z() + theMaterial.Specular.z()));
247
248 const float aReflectionScale = 0.75f / aMaxRefl;
249
fc73a202 250 theMaterial.Reflection = BVH_Vec4f (theProp.speccol.rgb[0] * theProp.spec * aReflectionScale,
251 theProp.speccol.rgb[1] * theProp.spec * aReflectionScale,
252 theProp.speccol.rgb[2] * theProp.spec * aReflectionScale,
253 0.f);
e276548b 254}
255
256// =======================================================================
257// function : AddRaytraceStructure
258// purpose : Adds OpenGL structure to ray-traced scene geometry
259// =======================================================================
265d4508 260Standard_Boolean OpenGl_Workspace::AddRaytraceStructure (const OpenGl_Structure* theStructure,
261 const Standard_ShortReal* theTransform, std::set<const OpenGl_Structure*>& theElements)
e276548b 262{
e276548b 263 theElements.insert (theStructure);
264
265 if (!theStructure->IsVisible())
266 {
267 myStructureStates[theStructure] = theStructure->ModificationState();
268 return Standard_True;
269 }
270
271 // Get structure material
68333c8f 272 Standard_Integer aStructMatID = -1;
e276548b 273
274 if (theStructure->AspectFace() != NULL)
275 {
265d4508 276 aStructMatID = static_cast<Standard_Integer> (myRaytraceGeometry.Materials.size());
e276548b 277
278 OpenGl_RaytraceMaterial aStructMaterial;
279 CreateMaterial (theStructure->AspectFace()->IntFront(), aStructMaterial);
280
265d4508 281 myRaytraceGeometry.Materials.push_back (aStructMaterial);
e276548b 282 }
283
b64d84be 284 for (OpenGl_Structure::GroupIterator aGroupIter (theStructure->DrawGroups()); aGroupIter.More(); aGroupIter.Next())
e276548b 285 {
286 // Get group material
68333c8f 287 Standard_Integer aGroupMatID = -1;
b64d84be 288 if (aGroupIter.Value()->AspectFace() != NULL)
e276548b 289 {
265d4508 290 aGroupMatID = static_cast<Standard_Integer> (myRaytraceGeometry.Materials.size());
e276548b 291
292 OpenGl_RaytraceMaterial aGroupMaterial;
b64d84be 293 CreateMaterial (aGroupIter.Value()->AspectFace()->IntFront(), aGroupMaterial);
e276548b 294
265d4508 295 myRaytraceGeometry.Materials.push_back (aGroupMaterial);
e276548b 296 }
297
68333c8f 298 Standard_Integer aMatID = aGroupMatID < 0 ? aStructMatID : aGroupMatID;
e276548b 299
265d4508 300 if (aMatID < 0)
e276548b 301 {
265d4508 302 aMatID = static_cast<Standard_Integer> (myRaytraceGeometry.Materials.size());
e276548b 303
265d4508 304 myRaytraceGeometry.Materials.push_back (OpenGl_RaytraceMaterial());
e276548b 305 }
306
265d4508 307 // Add OpenGL elements from group (extract primitives arrays and aspects)
b64d84be 308 for (const OpenGl_ElementNode* aNode = aGroupIter.Value()->FirstNode(); aNode != NULL; aNode = aNode->next)
e276548b 309 {
5322131b 310 OpenGl_AspectFace* anAspect = dynamic_cast<OpenGl_AspectFace*> (aNode->elem);
311 if (anAspect != NULL)
e276548b 312 {
5322131b 313 aMatID = static_cast<Standard_Integer> (myRaytraceGeometry.Materials.size());
e276548b 314
5322131b 315 OpenGl_RaytraceMaterial aMaterial;
316 CreateMaterial (anAspect->IntFront(), aMaterial);
e276548b 317
5322131b 318 myRaytraceGeometry.Materials.push_back (aMaterial);
e276548b 319 }
5322131b 320 else
e276548b 321 {
322 OpenGl_PrimitiveArray* aPrimArray = dynamic_cast<OpenGl_PrimitiveArray*> (aNode->elem);
e276548b 323 if (aPrimArray != NULL)
324 {
265d4508 325 NCollection_Handle<BVH_Object<Standard_ShortReal, 4> > aSet =
326 AddRaytracePrimitiveArray (aPrimArray->PArray(), aMatID, theTransform);
327
328 if (!aSet.IsNull())
329 myRaytraceGeometry.Objects().Append (aSet);
e276548b 330 }
331 }
332 }
e276548b 333 }
334
265d4508 335 Standard_ShortReal* aTransform (NULL);
e276548b 336
337 // Process all connected OpenGL structures
265d4508 338 for (OpenGl_ListOfStructure::Iterator anIts (theStructure->ConnectedStructures()); anIts.More(); anIts.Next())
e276548b 339 {
340 if (anIts.Value()->Transformation()->mat != NULL)
341 {
265d4508 342 Standard_ShortReal* aTransform = new Standard_ShortReal[16];
e276548b 343
68333c8f 344 for (Standard_Integer i = 0; i < 4; ++i)
345 for (Standard_Integer j = 0; j < 4; ++j)
e276548b 346 {
347 aTransform[j * 4 + i] =
348 anIts.Value()->Transformation()->mat[i][j];
349 }
350 }
351
352 if (anIts.Value()->IsRaytracable())
353 AddRaytraceStructure (anIts.Value(), aTransform != NULL ? aTransform : theTransform, theElements);
e276548b 354 }
355
356 delete[] aTransform;
357
358 myStructureStates[theStructure] = theStructure->ModificationState();
359
360 return Standard_True;
361}
362
363// =======================================================================
364// function : AddRaytracePrimitiveArray
365// purpose : Adds OpenGL primitive array to ray-traced scene geometry
366// =======================================================================
265d4508 367OpenGl_TriangleSet* OpenGl_Workspace::AddRaytracePrimitiveArray (
368 const CALL_DEF_PARRAY* theArray, Standard_Integer theMatID, const Standard_ShortReal* theTransform)
e276548b 369{
370 if (theArray->type != TelPolygonsArrayType &&
371 theArray->type != TelTrianglesArrayType &&
372 theArray->type != TelQuadranglesArrayType &&
373 theArray->type != TelTriangleFansArrayType &&
374 theArray->type != TelTriangleStripsArrayType &&
375 theArray->type != TelQuadrangleStripsArrayType)
376 {
265d4508 377 return NULL;
e276548b 378 }
379
380 if (theArray->vertices == NULL)
265d4508 381 return NULL;
e276548b 382
383#ifdef RAY_TRACE_PRINT_INFO
384 switch (theArray->type)
385 {
386 case TelPolygonsArrayType:
265d4508 387 std::cout << "\tAdding TelPolygonsArrayType" << std::endl; break;
e276548b 388 case TelTrianglesArrayType:
265d4508 389 std::cout << "\tAdding TelTrianglesArrayType" << std::endl; break;
e276548b 390 case TelQuadranglesArrayType:
265d4508 391 std::cout << "\tAdding TelQuadranglesArrayType" << std::endl; break;
e276548b 392 case TelTriangleFansArrayType:
265d4508 393 std::cout << "\tAdding TelTriangleFansArrayType" << std::endl; break;
e276548b 394 case TelTriangleStripsArrayType:
265d4508 395 std::cout << "\tAdding TelTriangleStripsArrayType" << std::endl; break;
e276548b 396 case TelQuadrangleStripsArrayType:
265d4508 397 std::cout << "\tAdding TelQuadrangleStripsArrayType" << std::endl; break;
e276548b 398 }
399#endif
400
265d4508 401 OpenGl_TriangleSet* aSet = new OpenGl_TriangleSet;
e276548b 402
e276548b 403 {
265d4508 404 aSet->Vertices.reserve (theArray->num_vertexs);
e276548b 405
265d4508 406 for (Standard_Integer aVert = 0; aVert < theArray->num_vertexs; ++aVert)
407 {
408 BVH_Vec4f aVertex (theArray->vertices[aVert].xyz[0],
409 theArray->vertices[aVert].xyz[1],
410 theArray->vertices[aVert].xyz[2],
411 1.f);
412 if (theTransform)
413 aVertex = MatVecMult (theTransform, aVertex);
e276548b 414
265d4508 415 aSet->Vertices.push_back (aVertex);
416 }
e276548b 417
265d4508 418 aSet->Normals.reserve (theArray->num_vertexs);
e276548b 419
265d4508 420 for (Standard_Integer aNorm = 0; aNorm < theArray->num_vertexs; ++aNorm)
421 {
422 BVH_Vec4f aNormal;
e276548b 423
265d4508 424 // Note: In case of absence of normals, the
425 // renderer uses generated geometric normals
e276548b 426
265d4508 427 if (theArray->vnormals != NULL)
428 {
429 aNormal = BVH_Vec4f (theArray->vnormals[aNorm].xyz[0],
430 theArray->vnormals[aNorm].xyz[1],
431 theArray->vnormals[aNorm].xyz[2],
432 0.f);
e276548b 433
265d4508 434 if (theTransform)
435 aNormal = MatVecMult (theTransform, aNormal);
436 }
e276548b 437
265d4508 438 aSet->Normals.push_back (aNormal);
e276548b 439 }
440
265d4508 441 if (theArray->num_bounds > 0)
442 {
443 #ifdef RAY_TRACE_PRINT_INFO
444 std::cout << "\tNumber of bounds = " << theArray->num_bounds << std::endl;
445 #endif
e276548b 446
265d4508 447 Standard_Integer aBoundStart = 0;
e276548b 448
265d4508 449 for (Standard_Integer aBound = 0; aBound < theArray->num_bounds; ++aBound)
450 {
451 const Standard_Integer aVertNum = theArray->bounds[aBound];
e276548b 452
265d4508 453 #ifdef RAY_TRACE_PRINT_INFO
454 std::cout << "\tAdding indices from bound " << aBound << ": " <<
455 aBoundStart << " .. " << aVertNum << std::endl;
456 #endif
457
458 if (!AddRaytraceVertexIndices (aSet, theArray, aBoundStart, aVertNum, theMatID))
459 {
460 delete aSet;
461 return NULL;
462 }
463
464 aBoundStart += aVertNum;
465 }
466 }
467 else
e276548b 468 {
265d4508 469 const Standard_Integer aVertNum = theArray->num_edges > 0 ? theArray->num_edges : theArray->num_vertexs;
e276548b 470
265d4508 471 #ifdef RAY_TRACE_PRINT_INFO
472 std::cout << "\tAdding indices from array: " << aVertNum << std::endl;
473 #endif
e276548b 474
265d4508 475 if (!AddRaytraceVertexIndices (aSet, theArray, 0, aVertNum, theMatID))
e276548b 476 {
265d4508 477 delete aSet;
478 return NULL;
e276548b 479 }
e276548b 480 }
481 }
e276548b 482
265d4508 483 if (aSet->Size() != 0)
484 aSet->MarkDirty();
e276548b 485
265d4508 486 return aSet;
e276548b 487}
488
489// =======================================================================
490// function : AddRaytraceVertexIndices
491// purpose : Adds vertex indices to ray-traced scene geometry
492// =======================================================================
265d4508 493Standard_Boolean OpenGl_Workspace::AddRaytraceVertexIndices (OpenGl_TriangleSet* theSet,
494 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID)
e276548b 495{
e276548b 496 switch (theArray->type)
497 {
265d4508 498 case TelTrianglesArrayType:
499 return AddRaytraceTriangleArray (theSet, theArray, theOffset, theCount, theMatID);
500
501 case TelQuadranglesArrayType:
502 return AddRaytraceQuadrangleArray (theSet, theArray, theOffset, theCount, theMatID);
503
504 case TelTriangleFansArrayType:
505 return AddRaytraceTriangleFanArray (theSet, theArray, theOffset, theCount, theMatID);
506
5322131b 507 case TelTriangleStripsArrayType:
265d4508 508 return AddRaytraceTriangleStripArray (theSet, theArray, theOffset, theCount, theMatID);
509
510 case TelQuadrangleStripsArrayType:
511 return AddRaytraceQuadrangleStripArray (theSet, theArray, theOffset, theCount, theMatID);
512
513 default:
514 return AddRaytracePolygonArray (theSet, theArray, theOffset, theCount, theMatID);
e276548b 515 }
e276548b 516}
517
518// =======================================================================
519// function : AddRaytraceTriangleArray
520// purpose : Adds OpenGL triangle array to ray-traced scene geometry
521// =======================================================================
265d4508 522Standard_Boolean OpenGl_Workspace::AddRaytraceTriangleArray (OpenGl_TriangleSet* theSet,
523 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID)
e276548b 524{
265d4508 525 if (theCount < 3)
e276548b 526 return Standard_True;
527
265d4508 528 theSet->Elements.reserve (theSet->Elements.size() + theCount / 3);
529
e276548b 530 if (theArray->num_edges > 0)
531 {
265d4508 532 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; aVert += 3)
e276548b 533 {
265d4508 534 theSet->Elements.push_back (BVH_Vec4i (theArray->edges[aVert + 0],
535 theArray->edges[aVert + 1],
536 theArray->edges[aVert + 2],
537 theMatID));
e276548b 538 }
539 }
540 else
541 {
265d4508 542 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; aVert += 3)
e276548b 543 {
265d4508 544 theSet->Elements.push_back (BVH_Vec4i (aVert + 0,
545 aVert + 1,
546 aVert + 2,
547 theMatID));
e276548b 548 }
549 }
550
551 return Standard_True;
552}
553
554// =======================================================================
555// function : AddRaytraceTriangleFanArray
556// purpose : Adds OpenGL triangle fan array to ray-traced scene geometry
557// =======================================================================
265d4508 558Standard_Boolean OpenGl_Workspace::AddRaytraceTriangleFanArray (OpenGl_TriangleSet* theSet,
559 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID)
e276548b 560{
265d4508 561 if (theCount < 3)
e276548b 562 return Standard_True;
563
265d4508 564 theSet->Elements.reserve (theSet->Elements.size() + theCount - 2);
565
e276548b 566 if (theArray->num_edges > 0)
567 {
265d4508 568 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; ++aVert)
e276548b 569 {
265d4508 570 theSet->Elements.push_back (BVH_Vec4i (theArray->edges[theOffset],
571 theArray->edges[aVert + 1],
572 theArray->edges[aVert + 2],
573 theMatID));
e276548b 574 }
575 }
576 else
577 {
265d4508 578 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; ++aVert)
e276548b 579 {
265d4508 580 theSet->Elements.push_back (BVH_Vec4i (theOffset,
581 aVert + 1,
582 aVert + 2,
583 theMatID));
e276548b 584 }
585 }
586
587 return Standard_True;
588}
589
590// =======================================================================
591// function : AddRaytraceTriangleStripArray
592// purpose : Adds OpenGL triangle strip array to ray-traced scene geometry
593// =======================================================================
265d4508 594Standard_Boolean OpenGl_Workspace::AddRaytraceTriangleStripArray (OpenGl_TriangleSet* theSet,
595 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID)
e276548b 596{
265d4508 597 if (theCount < 3)
e276548b 598 return Standard_True;
599
265d4508 600 theSet->Elements.reserve (theSet->Elements.size() + theCount - 2);
601
e276548b 602 if (theArray->num_edges > 0)
603 {
265d4508 604 for (Standard_Integer aVert = theOffset, aCW = 0; aVert < theOffset + theCount - 2; ++aVert, aCW = (aCW + 1) % 2)
e276548b 605 {
265d4508 606 theSet->Elements.push_back (BVH_Vec4i (theArray->edges[aVert + aCW ? 1 : 0],
607 theArray->edges[aVert + aCW ? 0 : 1],
608 theArray->edges[aVert + 2],
609 theMatID));
e276548b 610 }
611 }
612 else
613 {
265d4508 614 for (Standard_Integer aVert = theOffset, aCW = 0; aVert < theOffset + theCount - 2; ++aVert, aCW = (aCW + 1) % 2)
e276548b 615 {
265d4508 616 theSet->Elements.push_back (BVH_Vec4i (aVert + aCW ? 1 : 0,
617 aVert + aCW ? 0 : 1,
618 aVert + 2,
619 theMatID));
e276548b 620 }
621 }
622
623 return Standard_True;
624}
625
626// =======================================================================
627// function : AddRaytraceQuadrangleArray
628// purpose : Adds OpenGL quad array to ray-traced scene geometry
629// =======================================================================
265d4508 630Standard_Boolean OpenGl_Workspace::AddRaytraceQuadrangleArray (OpenGl_TriangleSet* theSet,
631 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID)
e276548b 632{
265d4508 633 if (theCount < 4)
e276548b 634 return Standard_True;
635
265d4508 636 theSet->Elements.reserve (theSet->Elements.size() + theCount / 2);
637
e276548b 638 if (theArray->num_edges > 0)
639 {
265d4508 640 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 3; aVert += 4)
e276548b 641 {
265d4508 642 theSet->Elements.push_back (BVH_Vec4i (theArray->edges[aVert + 0],
643 theArray->edges[aVert + 1],
644 theArray->edges[aVert + 2],
645 theMatID));
646
647 theSet->Elements.push_back (BVH_Vec4i (theArray->edges[aVert + 0],
648 theArray->edges[aVert + 2],
649 theArray->edges[aVert + 3],
650 theMatID));
e276548b 651 }
652 }
653 else
654 {
265d4508 655 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 3; aVert += 4)
e276548b 656 {
265d4508 657 theSet->Elements.push_back (BVH_Vec4i (aVert + 0,
658 aVert + 1,
659 aVert + 2,
660 theMatID));
661
662 theSet->Elements.push_back (BVH_Vec4i (aVert + 0,
663 aVert + 2,
664 aVert + 3,
665 theMatID));
e276548b 666 }
667 }
668
669 return Standard_True;
670}
671
672// =======================================================================
673// function : AddRaytraceQuadrangleStripArray
674// purpose : Adds OpenGL quad strip array to ray-traced scene geometry
675// =======================================================================
265d4508 676Standard_Boolean OpenGl_Workspace::AddRaytraceQuadrangleStripArray (OpenGl_TriangleSet* theSet,
677 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID)
e276548b 678{
265d4508 679 if (theCount < 4)
e276548b 680 return Standard_True;
681
265d4508 682 theSet->Elements.reserve (theSet->Elements.size() + 2 * theCount - 6);
683
e276548b 684 if (theArray->num_edges > 0)
685 {
265d4508 686 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 3; aVert += 2)
e276548b 687 {
265d4508 688 theSet->Elements.push_back (BVH_Vec4i (theArray->edges[aVert + 0],
689 theArray->edges[aVert + 1],
690 theArray->edges[aVert + 2],
691 theMatID));
692
693 theSet->Elements.push_back (BVH_Vec4i (theArray->edges[aVert + 1],
694 theArray->edges[aVert + 3],
695 theArray->edges[aVert + 2],
696 theMatID));
e276548b 697 }
698 }
699 else
700 {
265d4508 701 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 3; aVert += 2)
e276548b 702 {
265d4508 703 theSet->Elements.push_back (BVH_Vec4i (aVert + 0,
704 aVert + 1,
705 aVert + 2,
706 theMatID));
707
708 theSet->Elements.push_back (BVH_Vec4i (aVert + 1,
709 aVert + 3,
710 aVert + 2,
711 theMatID));
e276548b 712 }
713 }
714
715 return Standard_True;
716}
717
718// =======================================================================
719// function : AddRaytracePolygonArray
720// purpose : Adds OpenGL polygon array to ray-traced scene geometry
721// =======================================================================
265d4508 722Standard_Boolean OpenGl_Workspace::AddRaytracePolygonArray (OpenGl_TriangleSet* theSet,
723 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID)
e276548b 724{
265d4508 725 if (theCount < 3)
e276548b 726 return Standard_True;
727
265d4508 728 theSet->Elements.reserve (theSet->Elements.size() + theCount - 2);
729
730 if (theArray->num_edges > 0)
e276548b 731 {
265d4508 732 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; ++aVert)
e276548b 733 {
265d4508 734 theSet->Elements.push_back (BVH_Vec4i (theArray->edges[theOffset],
735 theArray->edges[aVert + 1],
736 theArray->edges[aVert + 2],
737 theMatID));
e276548b 738 }
739 }
740 else
741 {
265d4508 742 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; ++aVert)
e276548b 743 {
265d4508 744 theSet->Elements.push_back (BVH_Vec4i (theOffset,
745 aVert + 1,
746 aVert + 2,
747 theMatID));
e276548b 748 }
749 }
750
751 return Standard_True;
752}
753
754// =======================================================================
755// function : UpdateRaytraceLightSources
756// purpose : Updates 3D scene light sources for ray-tracing
757// =======================================================================
758Standard_Boolean OpenGl_Workspace::UpdateRaytraceLightSources (const GLdouble theInvModelView[16])
759{
265d4508 760 myRaytraceGeometry.Sources.clear();
e276548b 761
fc73a202 762 myRaytraceGeometry.Ambient = BVH_Vec4f (0.0f, 0.0f, 0.0f, 0.0f);
265d4508 763
764 for (OpenGl_ListOfLight::Iterator anItl (myView->LightList()); anItl.More(); anItl.Next())
e276548b 765 {
12381341 766 const OpenGl_Light& aLight = anItl.Value();
265d4508 767
12381341 768 if (aLight.Type == Visual3d_TOLS_AMBIENT)
e276548b 769 {
fc73a202 770 myRaytraceGeometry.Ambient += BVH_Vec4f (aLight.Color.r(),
771 aLight.Color.g(),
772 aLight.Color.b(),
773 0.0f);
e276548b 774 continue;
775 }
776
265d4508 777 BVH_Vec4f aDiffuse (aLight.Color.r(),
778 aLight.Color.g(),
779 aLight.Color.b(),
780 1.0f);
781
782 BVH_Vec4f aPosition (-aLight.Direction.x(),
783 -aLight.Direction.y(),
784 -aLight.Direction.z(),
785 0.0f);
786
12381341 787 if (aLight.Type != Visual3d_TOLS_DIRECTIONAL)
e276548b 788 {
265d4508 789 aPosition = BVH_Vec4f (aLight.Position.x(),
790 aLight.Position.y(),
791 aLight.Position.z(),
792 1.0f);
e276548b 793 }
265d4508 794
12381341 795 if (aLight.IsHeadlight)
e276548b 796 aPosition = MatVecMult (theInvModelView, aPosition);
e276548b 797
fc73a202 798
265d4508 799 myRaytraceGeometry.Sources.push_back (OpenGl_RaytraceLight (aDiffuse, aPosition));
e276548b 800 }
801
fc73a202 802 if (myRaytraceLightSrcTexture.IsNull()) // create light source buffer
e276548b 803 {
fc73a202 804 myRaytraceLightSrcTexture = new OpenGl_TextureBufferArb;
e276548b 805
fc73a202 806 if (!myRaytraceLightSrcTexture->Create (myGlContext))
807 {
e276548b 808#ifdef RAY_TRACE_PRINT_INFO
fc73a202 809 std::cout << "Error: Failed to create light source buffer" << std::endl;
e276548b 810#endif
fc73a202 811 return Standard_False;
812 }
e276548b 813 }
fc73a202 814
815 if (myRaytraceGeometry.Sources.size() != 0)
e276548b 816 {
fc73a202 817 const GLfloat* aDataPtr = myRaytraceGeometry.Sources.front().Packed();
818
819 bool aResult = myRaytraceLightSrcTexture->Init (
820 myGlContext, 4, myRaytraceGeometry.Sources.size() * 2, aDataPtr);
e276548b 821
fc73a202 822 if (!aResult)
823 {
824#ifdef RAY_TRACE_PRINT_INFO
825 std::cout << "Error: Failed to upload light source buffer" << std::endl;
e276548b 826#endif
fc73a202 827 return Standard_False;
828 }
829 }
e276548b 830
831 return Standard_True;
832}
833
834// =======================================================================
fc73a202 835// function : UpdateRaytraceEnvironmentMap
836// purpose : Updates environment map for ray-tracing
e276548b 837// =======================================================================
fc73a202 838Standard_Boolean OpenGl_Workspace::UpdateRaytraceEnvironmentMap()
e276548b 839{
fc73a202 840 if (myView.IsNull())
e276548b 841 return Standard_False;
e276548b 842
fc73a202 843 if (myViewModificationStatus == myView->ModificationState())
844 return Standard_True;
e276548b 845
fc73a202 846 for (Standard_Integer anIdx = 0; anIdx < 2; ++anIdx)
e276548b 847 {
fc73a202 848 const Handle(OpenGl_ShaderProgram)& aProgram =
849 anIdx == 0 ? myRaytraceProgram : myPostFSAAProgram;
e276548b 850
fc73a202 851 if (!aProgram.IsNull())
e276548b 852 {
fc73a202 853 aProgram->Bind (myGlContext);
e276548b 854
fc73a202 855 if (!myView->TextureEnv().IsNull() && myView->SurfaceDetail() != Visual3d_TOD_NONE)
e276548b 856 {
fc73a202 857 myView->TextureEnv()->Bind (
858 myGlContext, GL_TEXTURE0 + OpenGl_RT_EnvironmentMapTexture);
859
860 aProgram->SetUniform (myGlContext, "uEnvironmentEnable", 1);
e276548b 861 }
862 else
863 {
fc73a202 864 aProgram->SetUniform (myGlContext, "uEnvironmentEnable", 0);
e276548b 865 }
e276548b 866
fc73a202 867 aProgram->SetSampler (myGlContext,
868 "uEnvironmentMapTexture", OpenGl_RT_EnvironmentMapTexture);
869 }
e276548b 870 }
871
fc73a202 872 OpenGl_ShaderProgram::Unbind (myGlContext);
e276548b 873
fc73a202 874 myViewModificationStatus = myView->ModificationState();
e276548b 875
e276548b 876 return Standard_True;
877}
878
879// =======================================================================
fc73a202 880// function : Source
881// purpose : Returns shader source combined with prefix
e276548b 882// =======================================================================
fc73a202 883TCollection_AsciiString OpenGl_Workspace::ShaderSource::Source() const
e276548b 884{
fc73a202 885 static const TCollection_AsciiString aVersion = "#version 140";
e276548b 886
fc73a202 887 if (myPrefix.IsEmpty())
e276548b 888 {
fc73a202 889 return aVersion + "\n" + mySource;
e276548b 890 }
891
fc73a202 892 return aVersion + "\n" + myPrefix + "\n" + mySource;
e276548b 893}
894
895// =======================================================================
fc73a202 896// function : Load
897// purpose : Loads shader source from specified files
e276548b 898// =======================================================================
fc73a202 899void OpenGl_Workspace::ShaderSource::Load (
900 const TCollection_AsciiString* theFileNames, const Standard_Integer theCount)
e276548b 901{
fc73a202 902 mySource.Clear();
e276548b 903
fc73a202 904 for (Standard_Integer anIndex = 0; anIndex < theCount; ++anIndex)
905 {
906 OSD_File aFile (theFileNames[anIndex]);
e276548b 907
fc73a202 908 Standard_ASSERT_RETURN (aFile.Exists(),
909 "Error: Failed to find shader source file", /* none */);
e276548b 910
fc73a202 911 aFile.Open (OSD_ReadOnly, OSD_Protection());
265d4508 912
fc73a202 913 TCollection_AsciiString aSource;
265d4508 914
fc73a202 915 Standard_ASSERT_RETURN (aFile.IsOpen(),
916 "Error: Failed to open shader source file", /* none */);
e276548b 917
fc73a202 918 aFile.Read (aSource, (Standard_Integer) aFile.Size());
e276548b 919
fc73a202 920 if (!aSource.IsEmpty())
921 {
922 mySource += TCollection_AsciiString ("\n") + aSource;
923 }
68333c8f 924
fc73a202 925 aFile.Close();
68333c8f 926 }
e276548b 927}
928
929// =======================================================================
fc73a202 930// function : LoadShader
931// purpose : Creates new shader object with specified source
e276548b 932// =======================================================================
fc73a202 933Handle(OpenGl_ShaderObject) OpenGl_Workspace::LoadShader (const ShaderSource& theSource, GLenum theType)
e276548b 934{
fc73a202 935 Handle(OpenGl_ShaderObject) aShader = new OpenGl_ShaderObject (theType);
e276548b 936
fc73a202 937 if (!aShader->Create (myGlContext))
e276548b 938 {
fc73a202 939 const TCollection_ExtendedString aMessage = "Error: Failed to create shader object";
940
941 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
942 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
e276548b 943
fc73a202 944 aShader->Release (myGlContext.operator->());
e276548b 945
fc73a202 946 return Handle(OpenGl_ShaderObject)();
e276548b 947 }
5322131b 948
fc73a202 949 if (!aShader->LoadSource (myGlContext, theSource.Source()))
950 {
951 const TCollection_ExtendedString aMessage = "Error: Failed to set shader source";
952
953 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
954 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
e276548b 955
fc73a202 956 aShader->Release (myGlContext.operator->());
e276548b 957
fc73a202 958 return Handle(OpenGl_ShaderObject)();
959 }
e276548b 960
fc73a202 961 TCollection_AsciiString aBuildLog;
e276548b 962
fc73a202 963 if (!aShader->Compile (myGlContext))
e276548b 964 {
fc73a202 965 if (aShader->FetchInfoLog (myGlContext, aBuildLog))
966 {
967 const TCollection_ExtendedString aMessage =
968 TCollection_ExtendedString ("Error: Failed to compile shader object:\n") + aBuildLog;
969
e276548b 970#ifdef RAY_TRACE_PRINT_INFO
fc73a202 971 std::cout << aBuildLog << std::endl;
e276548b 972#endif
e276548b 973
fc73a202 974 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
975 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
976 }
977
978 aShader->Release (myGlContext.operator->());
979
980 return Handle(OpenGl_ShaderObject)();
981 }
68333c8f 982
e276548b 983#ifdef RAY_TRACE_PRINT_INFO
fc73a202 984 if (aShader->FetchInfoLog (myGlContext, aBuildLog))
985 {
986 if (!aBuildLog.IsEmpty())
987 {
988 std::cout << aBuildLog << std::endl;
989 }
990 else
991 {
992 std::cout << "Info: shader build log is empty" << std::endl;
993 }
994 }
e276548b 995#endif
e276548b 996
fc73a202 997 return aShader;
e276548b 998}
999
1000// =======================================================================
fc73a202 1001// function : SafeFailBack
1002// purpose : Performs safe exit when shaders initialization fails
e276548b 1003// =======================================================================
fc73a202 1004Standard_Boolean OpenGl_Workspace::SafeFailBack (const TCollection_ExtendedString& theMessage)
e276548b 1005{
fc73a202 1006 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1007 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, theMessage);
265d4508 1008
fc73a202 1009 myComputeInitStatus = OpenGl_RT_FAIL;
265d4508 1010
fc73a202 1011 ReleaseRaytraceResources();
1012
1013 return Standard_False;
1014}
e276548b 1015
fc73a202 1016// =======================================================================
1017// function : InitRaytraceResources
1018// purpose : Initializes OpenGL/GLSL shader programs
1019// =======================================================================
1020Standard_Boolean OpenGl_Workspace::InitRaytraceResources()
1021{
1022 Standard_Boolean aToRebuildShaders = Standard_False;
e276548b 1023
fc73a202 1024 if (myComputeInitStatus == OpenGl_RT_INIT)
1025 {
1026 if (!myIsRaytraceDataValid)
1027 return Standard_True;
e276548b 1028
fc73a202 1029 const Standard_Integer aRequiredStackSize =
1030 myRaytraceGeometry.HighLevelTreeDepth() + myRaytraceGeometry.BottomLevelTreeDepth();
e276548b 1031
fc73a202 1032 if (myTraversalStackSize < aRequiredStackSize)
1033 {
1034 myTraversalStackSize = Max (aRequiredStackSize, THE_DEFAULT_STACK_SIZE);
e276548b 1035
fc73a202 1036 aToRebuildShaders = Standard_True;
1037 }
1038 else
1039 {
1040 if (aRequiredStackSize < myTraversalStackSize)
1041 {
1042 if (myTraversalStackSize > THE_DEFAULT_STACK_SIZE)
1043 {
1044 myTraversalStackSize = Max (aRequiredStackSize, THE_DEFAULT_STACK_SIZE);
e276548b 1045
fc73a202 1046 aToRebuildShaders = Standard_True;
1047 }
1048 }
1049 }
e276548b 1050
fc73a202 1051 if (aToRebuildShaders)
1052 {
e276548b 1053#ifdef RAY_TRACE_PRINT_INFO
fc73a202 1054 std::cout << "Info: Rebuild shaders with stack size: " << myTraversalStackSize << std::endl;
e276548b 1055#endif
e276548b 1056
fc73a202 1057 TCollection_AsciiString aStackSizeStr =
1058 TCollection_AsciiString ("#define STACK_SIZE ") + TCollection_AsciiString (myTraversalStackSize);
265d4508 1059
fc73a202 1060 myRaytraceShaderSource.SetPrefix (aStackSizeStr);
1061 myPostFSAAShaderSource.SetPrefix (aStackSizeStr);
265d4508 1062
fc73a202 1063 if (!myRaytraceShader->LoadSource (myGlContext, myRaytraceShaderSource.Source())
1064 || !myPostFSAAShader->LoadSource (myGlContext, myPostFSAAShaderSource.Source()))
1065 {
1066 return Standard_False;
1067 }
265d4508 1068
fc73a202 1069 if (!myRaytraceShader->Compile (myGlContext)
1070 || !myPostFSAAShader->Compile (myGlContext))
1071 {
1072 return Standard_False;
1073 }
1074
1075 if (!myRaytraceProgram->Link (myGlContext)
1076 || !myPostFSAAProgram->Link (myGlContext))
1077 {
1078 return Standard_False;
1079 }
1080 }
e276548b 1081 }
1082
fc73a202 1083 if (myComputeInitStatus == OpenGl_RT_NONE)
1084 {
1085 if (!myGlContext->IsGlGreaterEqual (3, 1))
1086 {
1087 const TCollection_ExtendedString aMessage = "Ray-tracing requires OpenGL 3.1 and higher";
265d4508 1088
fc73a202 1089 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1090 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
265d4508 1091
fc73a202 1092 return Standard_False;
1093 }
5322131b 1094
fc73a202 1095 TCollection_AsciiString aFolder = Graphic3d_ShaderProgram::ShadersFolder();
265d4508 1096
fc73a202 1097 if (aFolder.IsEmpty())
1098 {
1099 const TCollection_ExtendedString aMessage = "Failed to locate shaders directory";
1100
1101 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1102 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
1103
1104 return Standard_False;
1105 }
265d4508 1106
fc73a202 1107 if (myIsRaytraceDataValid)
1108 {
1109 myTraversalStackSize = Max (THE_DEFAULT_STACK_SIZE,
1110 myRaytraceGeometry.HighLevelTreeDepth() + myRaytraceGeometry.BottomLevelTreeDepth());
1111 }
265d4508 1112
fc73a202 1113 {
1114 Handle(OpenGl_ShaderObject) aBasicVertShader = LoadShader (
1115 ShaderSource (aFolder + "/RaytraceBase.vs"), GL_VERTEX_SHADER);
265d4508 1116
fc73a202 1117 if (aBasicVertShader.IsNull())
1118 {
1119 return SafeFailBack ("Failed to set vertex shader source");
1120 }
265d4508 1121
fc73a202 1122 TCollection_AsciiString aFiles[] = { aFolder + "/RaytraceBase.fs", aFolder + "/RaytraceRender.fs" };
265d4508 1123
fc73a202 1124 myRaytraceShaderSource.Load (aFiles, 2);
e276548b 1125
fc73a202 1126 TCollection_AsciiString aStackSizeStr =
1127 TCollection_AsciiString ("#define STACK_SIZE ") + TCollection_AsciiString (myTraversalStackSize);
265d4508 1128
fc73a202 1129 myRaytraceShaderSource.SetPrefix (aStackSizeStr);
265d4508 1130
fc73a202 1131 myRaytraceShader = LoadShader (myRaytraceShaderSource, GL_FRAGMENT_SHADER);
265d4508 1132
fc73a202 1133 if (myRaytraceShader.IsNull())
1134 {
1135 aBasicVertShader->Release (myGlContext.operator->());
265d4508 1136
fc73a202 1137 return SafeFailBack ("Failed to set ray-trace fragment shader source");
1138 }
265d4508 1139
fc73a202 1140 myRaytraceProgram = new OpenGl_ShaderProgram;
5322131b 1141
fc73a202 1142 if (!myRaytraceProgram->Create (myGlContext))
1143 {
1144 aBasicVertShader->Release (myGlContext.operator->());
265d4508 1145
fc73a202 1146 return SafeFailBack ("Failed to create ray-trace shader program");
1147 }
265d4508 1148
fc73a202 1149 if (!myRaytraceProgram->AttachShader (myGlContext, aBasicVertShader)
1150 || !myRaytraceProgram->AttachShader (myGlContext, myRaytraceShader))
1151 {
1152 aBasicVertShader->Release (myGlContext.operator->());
265d4508 1153
fc73a202 1154 return SafeFailBack ("Failed to attach ray-trace shader objects");
1155 }
265d4508 1156
fc73a202 1157 if (!myRaytraceProgram->Link (myGlContext))
1158 {
1159 TCollection_AsciiString aLinkLog;
e276548b 1160
fc73a202 1161 if (myRaytraceProgram->FetchInfoLog (myGlContext, aLinkLog))
1162 {
1163 #ifdef RAY_TRACE_PRINT_INFO
1164 std::cout << aLinkLog << std::endl;
1165 #endif
1166 }
265d4508 1167
fc73a202 1168 return SafeFailBack ("Failed to link ray-trace shader program");
1169 }
1170 }
265d4508 1171
fc73a202 1172 {
1173 Handle(OpenGl_ShaderObject) aBasicVertShader = LoadShader (
1174 ShaderSource (aFolder + "/RaytraceBase.vs"), GL_VERTEX_SHADER);
265d4508 1175
fc73a202 1176 if (aBasicVertShader.IsNull())
1177 {
1178 return SafeFailBack ("Failed to set vertex shader source");
1179 }
265d4508 1180
fc73a202 1181 TCollection_AsciiString aFiles[] = { aFolder + "/RaytraceBase.fs", aFolder + "/RaytraceSmooth.fs" };
265d4508 1182
fc73a202 1183 myPostFSAAShaderSource.Load (aFiles, 2);
265d4508 1184
fc73a202 1185 TCollection_AsciiString aStackSizeStr =
1186 TCollection_AsciiString ("#define STACK_SIZE ") + TCollection_AsciiString (myTraversalStackSize);
265d4508 1187
fc73a202 1188 myPostFSAAShaderSource.SetPrefix (aStackSizeStr);
1189
1190 myPostFSAAShader = LoadShader (myPostFSAAShaderSource, GL_FRAGMENT_SHADER);
265d4508 1191
fc73a202 1192 if (myPostFSAAShader.IsNull())
1193 {
1194 aBasicVertShader->Release (myGlContext.operator->());
265d4508 1195
fc73a202 1196 return SafeFailBack ("Failed to set FSAA fragment shader source");
1197 }
265d4508 1198
fc73a202 1199 myPostFSAAProgram = new OpenGl_ShaderProgram;
265d4508 1200
fc73a202 1201 if (!myPostFSAAProgram->Create (myGlContext))
1202 {
1203 aBasicVertShader->Release (myGlContext.operator->());
265d4508 1204
fc73a202 1205 return SafeFailBack ("Failed to create FSAA shader program");
1206 }
1207
1208 if (!myPostFSAAProgram->AttachShader (myGlContext, aBasicVertShader)
1209 || !myPostFSAAProgram->AttachShader (myGlContext, myPostFSAAShader))
1210 {
1211 aBasicVertShader->Release (myGlContext.operator->());
1212
1213 return SafeFailBack ("Failed to attach FSAA shader objects");
1214 }
1215
1216 if (!myPostFSAAProgram->Link (myGlContext))
1217 {
1218 TCollection_AsciiString aLinkLog;
1219
1220 if (myPostFSAAProgram->FetchInfoLog (myGlContext, aLinkLog))
1221 {
1222 #ifdef RAY_TRACE_PRINT_INFO
1223 std::cout << aLinkLog << std::endl;
1224 #endif
1225 }
1226
1227 return SafeFailBack ("Failed to link FSAA shader program");
1228 }
e276548b 1229 }
fc73a202 1230 }
e276548b 1231
fc73a202 1232 if (myComputeInitStatus == OpenGl_RT_NONE || aToRebuildShaders)
1233 {
1234 for (Standard_Integer anIndex = 0; anIndex < 2; ++anIndex)
e276548b 1235 {
fc73a202 1236 Handle(OpenGl_ShaderProgram)& aShaderProgram =
1237 (anIndex == 0) ? myRaytraceProgram : myPostFSAAProgram;
1238
1239 aShaderProgram->Bind (myGlContext);
1240
1241 aShaderProgram->SetSampler (myGlContext,
1242 "uSceneMinPointTexture", OpenGl_RT_SceneMinPointTexture);
1243 aShaderProgram->SetSampler (myGlContext,
1244 "uSceneMaxPointTexture", OpenGl_RT_SceneMaxPointTexture);
1245 aShaderProgram->SetSampler (myGlContext,
1246 "uSceneNodeInfoTexture", OpenGl_RT_SceneNodeInfoTexture);
1247 aShaderProgram->SetSampler (myGlContext,
1248 "uObjectMinPointTexture", OpenGl_RT_ObjectMinPointTexture);
1249 aShaderProgram->SetSampler (myGlContext,
1250 "uObjectMaxPointTexture", OpenGl_RT_ObjectMaxPointTexture);
1251 aShaderProgram->SetSampler (myGlContext,
1252 "uObjectNodeInfoTexture", OpenGl_RT_ObjectNodeInfoTexture);
1253 aShaderProgram->SetSampler (myGlContext,
1254 "uGeometryVertexTexture", OpenGl_RT_GeometryVertexTexture);
1255 aShaderProgram->SetSampler (myGlContext,
1256 "uGeometryNormalTexture", OpenGl_RT_GeometryNormalTexture);
1257 aShaderProgram->SetSampler (myGlContext,
1258 "uGeometryTriangTexture", OpenGl_RT_GeometryTriangTexture);
1259 aShaderProgram->SetSampler (myGlContext,
1260 "uRaytraceMaterialTexture", OpenGl_RT_RaytraceMaterialTexture);
1261 aShaderProgram->SetSampler (myGlContext,
1262 "uRaytraceLightSrcTexture", OpenGl_RT_RaytraceLightSrcTexture);
1263
1264 if (anIndex == 1)
1265 {
1266 aShaderProgram->SetSampler (myGlContext,
1267 "uFSAAInputTexture", OpenGl_RT_FSAAInputTexture);
1268 }
265d4508 1269
fc73a202 1270 myUniformLocations[anIndex][OpenGl_RT_aPosition] =
1271 aShaderProgram->GetAttributeLocation (myGlContext, "aPosition");
1272
1273 myUniformLocations[anIndex][OpenGl_RT_uOriginLB] =
1274 aShaderProgram->GetUniformLocation (myGlContext, "uOriginLB");
1275 myUniformLocations[anIndex][OpenGl_RT_uOriginRB] =
1276 aShaderProgram->GetUniformLocation (myGlContext, "uOriginRB");
1277 myUniformLocations[anIndex][OpenGl_RT_uOriginLT] =
1278 aShaderProgram->GetUniformLocation (myGlContext, "uOriginLT");
1279 myUniformLocations[anIndex][OpenGl_RT_uOriginRT] =
1280 aShaderProgram->GetUniformLocation (myGlContext, "uOriginRT");
1281 myUniformLocations[anIndex][OpenGl_RT_uDirectLB] =
1282 aShaderProgram->GetUniformLocation (myGlContext, "uDirectLB");
1283 myUniformLocations[anIndex][OpenGl_RT_uDirectRB] =
1284 aShaderProgram->GetUniformLocation (myGlContext, "uDirectRB");
1285 myUniformLocations[anIndex][OpenGl_RT_uDirectLT] =
1286 aShaderProgram->GetUniformLocation (myGlContext, "uDirectLT");
1287 myUniformLocations[anIndex][OpenGl_RT_uDirectRT] =
1288 aShaderProgram->GetUniformLocation (myGlContext, "uDirectRT");
1289
1290 myUniformLocations[anIndex][OpenGl_RT_uLightCount] =
1291 aShaderProgram->GetUniformLocation (myGlContext, "uLightCount");
1292 myUniformLocations[anIndex][OpenGl_RT_uLightAmbnt] =
1293 aShaderProgram->GetUniformLocation (myGlContext, "uGlobalAmbient");
1294
1295 myUniformLocations[anIndex][OpenGl_RT_uSceneRad] =
1296 aShaderProgram->GetUniformLocation (myGlContext, "uSceneRadius");
1297 myUniformLocations[anIndex][OpenGl_RT_uSceneEps] =
1298 aShaderProgram->GetUniformLocation (myGlContext, "uSceneEpsilon");
1299
1300 myUniformLocations[anIndex][OpenGl_RT_uShadEnabled] =
1301 aShaderProgram->GetUniformLocation (myGlContext, "uShadowsEnable");
1302 myUniformLocations[anIndex][OpenGl_RT_uReflEnabled] =
1303 aShaderProgram->GetUniformLocation (myGlContext, "uReflectionsEnable");
1304
1305 myUniformLocations[anIndex][OpenGl_RT_uOffsetX] =
1306 aShaderProgram->GetUniformLocation (myGlContext, "uOffsetX");
1307 myUniformLocations[anIndex][OpenGl_RT_uOffsetY] =
1308 aShaderProgram->GetUniformLocation (myGlContext, "uOffsetY");
1309 myUniformLocations[anIndex][OpenGl_RT_uSamples] =
1310 aShaderProgram->GetUniformLocation (myGlContext, "uSamples");
1311 }
265d4508 1312
fc73a202 1313 OpenGl_ShaderProgram::Unbind (myGlContext);
1314 }
265d4508 1315
fc73a202 1316 if (myComputeInitStatus != OpenGl_RT_NONE)
1317 {
1318 return myComputeInitStatus == OpenGl_RT_INIT;
1319 }
265d4508 1320
fc73a202 1321 if (myRaytraceFBO1.IsNull())
1322 {
1323 myRaytraceFBO1 = new OpenGl_FrameBuffer;
1324 }
265d4508 1325
fc73a202 1326 if (myRaytraceFBO2.IsNull())
1327 {
1328 myRaytraceFBO2 = new OpenGl_FrameBuffer;
1329 }
265d4508 1330
fc73a202 1331 const GLfloat aVertices[] = { -1.f, -1.f, 0.f,
1332 -1.f, 1.f, 0.f,
1333 1.f, 1.f, 0.f,
1334 1.f, 1.f, 0.f,
1335 1.f, -1.f, 0.f,
1336 -1.f, -1.f, 0.f };
265d4508 1337
fc73a202 1338 myRaytraceScreenQuad.Init (myGlContext, 3, 6, aVertices);
265d4508 1339
fc73a202 1340 myComputeInitStatus = OpenGl_RT_INIT; // initialized in normal way
1341
1342 return Standard_True;
1343}
265d4508 1344
fc73a202 1345// =======================================================================
1346// function : NullifyResource
1347// purpose :
1348// =======================================================================
1349inline void NullifyResource (const Handle(OpenGl_Context)& theContext,
1350 Handle(OpenGl_Resource)& theResource)
1351{
1352 if (!theResource.IsNull())
1353 {
1354 theResource->Release (theContext.operator->());
1355 theResource.Nullify();
1356 }
1357}
265d4508 1358
fc73a202 1359// =======================================================================
1360// function : ReleaseRaytraceResources
1361// purpose : Releases OpenGL/GLSL shader programs
1362// =======================================================================
1363void OpenGl_Workspace::ReleaseRaytraceResources()
1364{
1365 NullifyResource (myGlContext, myRaytraceFBO1);
1366 NullifyResource (myGlContext, myRaytraceFBO2);
265d4508 1367
fc73a202 1368 NullifyResource (myGlContext, myRaytraceShader);
1369 NullifyResource (myGlContext, myPostFSAAShader);
265d4508 1370
fc73a202 1371 NullifyResource (myGlContext, myRaytraceProgram);
1372 NullifyResource (myGlContext, myPostFSAAProgram);
265d4508 1373
fc73a202 1374 NullifyResource (myGlContext, mySceneNodeInfoTexture);
1375 NullifyResource (myGlContext, mySceneMinPointTexture);
1376 NullifyResource (myGlContext, mySceneMaxPointTexture);
265d4508 1377
fc73a202 1378 NullifyResource (myGlContext, myObjectNodeInfoTexture);
1379 NullifyResource (myGlContext, myObjectMinPointTexture);
1380 NullifyResource (myGlContext, myObjectMaxPointTexture);
265d4508 1381
fc73a202 1382 NullifyResource (myGlContext, myGeometryVertexTexture);
1383 NullifyResource (myGlContext, myGeometryNormalTexture);
1384 NullifyResource (myGlContext, myGeometryTriangTexture);
265d4508 1385
fc73a202 1386 NullifyResource (myGlContext, myRaytraceLightSrcTexture);
1387 NullifyResource (myGlContext, myRaytraceMaterialTexture);
265d4508 1388
fc73a202 1389 if (myRaytraceScreenQuad.IsValid())
1390 myRaytraceScreenQuad.Release (myGlContext.operator->());
1391}
265d4508 1392
fc73a202 1393// =======================================================================
1394// function : UploadRaytraceData
1395// purpose : Uploads ray-trace data to the GPU
1396// =======================================================================
1397Standard_Boolean OpenGl_Workspace::UploadRaytraceData()
1398{
1399 if (!myGlContext->IsGlGreaterEqual (3, 1))
1400 {
265d4508 1401#ifdef RAY_TRACE_PRINT_INFO
fc73a202 1402 std::cout << "Error: OpenGL version is less than 3.1" << std::endl;
265d4508 1403#endif
fc73a202 1404 return Standard_False;
e276548b 1405 }
1406
265d4508 1407 /////////////////////////////////////////////////////////////////////////////
fc73a202 1408 // Create OpenGL texture buffers
265d4508 1409
fc73a202 1410 if (mySceneNodeInfoTexture.IsNull()) // create hight-level BVH buffers
e276548b 1411 {
fc73a202 1412 mySceneNodeInfoTexture = new OpenGl_TextureBufferArb;
1413 mySceneMinPointTexture = new OpenGl_TextureBufferArb;
1414 mySceneMaxPointTexture = new OpenGl_TextureBufferArb;
265d4508 1415
fc73a202 1416 if (!mySceneNodeInfoTexture->Create (myGlContext)
1417 || !mySceneMinPointTexture->Create (myGlContext)
1418 || !mySceneMaxPointTexture->Create (myGlContext))
e276548b 1419 {
fc73a202 1420#ifdef RAY_TRACE_PRINT_INFO
1421 std::cout << "Error: Failed to create buffers for high-level scene BVH" << std::endl;
1422#endif
e276548b 1423 return Standard_False;
1424 }
1425 }
1426
fc73a202 1427 if (myObjectNodeInfoTexture.IsNull()) // create bottom-level BVH buffers
265d4508 1428 {
fc73a202 1429 myObjectNodeInfoTexture = new OpenGl_TextureBufferArb;
1430 myObjectMinPointTexture = new OpenGl_TextureBufferArb;
1431 myObjectMaxPointTexture = new OpenGl_TextureBufferArb;
e276548b 1432
fc73a202 1433 if (!myObjectNodeInfoTexture->Create (myGlContext)
1434 || !myObjectMinPointTexture->Create (myGlContext)
1435 || !myObjectMaxPointTexture->Create (myGlContext))
1436 {
e276548b 1437#ifdef RAY_TRACE_PRINT_INFO
fc73a202 1438 std::cout << "Error: Failed to create buffers for bottom-level scene BVH" << std::endl;
1439#endif
1440 return Standard_False;
1441 }
1442 }
5322131b 1443
fc73a202 1444 if (myGeometryVertexTexture.IsNull()) // create geometry buffers
265d4508 1445 {
fc73a202 1446 myGeometryVertexTexture = new OpenGl_TextureBufferArb;
1447 myGeometryNormalTexture = new OpenGl_TextureBufferArb;
1448 myGeometryTriangTexture = new OpenGl_TextureBufferArb;
e276548b 1449
fc73a202 1450 if (!myGeometryVertexTexture->Create (myGlContext)
1451 || !myGeometryNormalTexture->Create (myGlContext)
1452 || !myGeometryTriangTexture->Create (myGlContext))
1453 {
1454#ifdef RAY_TRACE_PRINT_INFO
1455 std::cout << "Error: Failed to create buffers for triangulation data" << std::endl;
1456#endif
1457 return Standard_False;
1458 }
265d4508 1459 }
5322131b 1460
fc73a202 1461 if (myRaytraceMaterialTexture.IsNull()) // create material buffer
1462 {
1463 myRaytraceMaterialTexture = new OpenGl_TextureBufferArb;
e276548b 1464
fc73a202 1465 if (!myRaytraceMaterialTexture->Create (myGlContext))
1466 {
1467#ifdef RAY_TRACE_PRINT_INFO
1468 std::cout << "Error: Failed to create buffers for material data" << std::endl;
e276548b 1469#endif
fc73a202 1470 return Standard_False;
1471 }
1472 }
e276548b 1473
fc73a202 1474 /////////////////////////////////////////////////////////////////////////////
1475 // Write OpenGL texture buffers
e276548b 1476
fc73a202 1477 const NCollection_Handle<BVH_Tree<Standard_ShortReal, 4> >& aBVH = myRaytraceGeometry.BVH();
e276548b 1478
fc73a202 1479 bool aResult = true;
e276548b 1480
fc73a202 1481 if (!aBVH->NodeInfoBuffer().empty())
1482 {
1483 aResult &= mySceneNodeInfoTexture->Init (myGlContext, 4,
1484 aBVH->NodeInfoBuffer().size(), reinterpret_cast<const GLuint*> (&aBVH->NodeInfoBuffer().front()));
5322131b 1485
fc73a202 1486 aResult &= mySceneMinPointTexture->Init (myGlContext, 4,
1487 aBVH->MinPointBuffer().size(), reinterpret_cast<const GLfloat*> (&aBVH->MinPointBuffer().front()));
1488
1489 aResult &= mySceneMaxPointTexture->Init (myGlContext, 4,
1490 aBVH->MaxPointBuffer().size(), reinterpret_cast<const GLfloat*> (&aBVH->MaxPointBuffer().front()));
1491 }
265d4508 1492
fc73a202 1493 if (!aResult)
1494 {
1495#ifdef RAY_TRACE_PRINT_INFO
1496 std::cout << "Error: Failed to upload buffers for high-level scene BVH" << std::endl;
1497#endif
e276548b 1498 return Standard_False;
1499 }
1500
fc73a202 1501 Standard_Size aTotalVerticesNb = 0;
1502 Standard_Size aTotalElementsNb = 0;
1503 Standard_Size aTotalBVHNodesNb = 0;
265d4508 1504
fc73a202 1505 for (Standard_Integer anElemIndex = 0; anElemIndex < myRaytraceGeometry.Size(); ++anElemIndex)
1506 {
1507 OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> (
1508 myRaytraceGeometry.Objects().ChangeValue (anElemIndex).operator->());
265d4508 1509
fc73a202 1510 Standard_ASSERT_RETURN (aTriangleSet != NULL,
1511 "Error: Failed to get triangulation of OpenGL element", Standard_False);
1512
1513 aTotalVerticesNb += aTriangleSet->Vertices.size();
1514 aTotalElementsNb += aTriangleSet->Elements.size();
e276548b 1515
fc73a202 1516 Standard_ASSERT_RETURN (!aTriangleSet->BVH().IsNull(),
1517 "Error: Failed to get bottom-level BVH of OpenGL element", Standard_False);
265d4508 1518
fc73a202 1519 aTotalBVHNodesNb += aTriangleSet->BVH()->NodeInfoBuffer().size();
1520 }
e276548b 1521
fc73a202 1522 if (aTotalBVHNodesNb != 0)
e276548b 1523 {
fc73a202 1524 aResult &= myObjectNodeInfoTexture->Init (
1525 myGlContext, 4, aTotalBVHNodesNb, static_cast<const GLuint*> (NULL));
e276548b 1526
fc73a202 1527 aResult &= myObjectMinPointTexture->Init (
1528 myGlContext, 4, aTotalBVHNodesNb, static_cast<const GLfloat*> (NULL));
e276548b 1529
fc73a202 1530 aResult &= myObjectMaxPointTexture->Init (
1531 myGlContext, 4, aTotalBVHNodesNb, static_cast<const GLfloat*> (NULL));
1532 }
265d4508 1533
fc73a202 1534 if (!aResult)
1535 {
1536#ifdef RAY_TRACE_PRINT_INFO
1537 std::cout << "Error: Failed to upload buffers for bottom-level scene BVH" << std::endl;
1538#endif
1539 return Standard_False;
1540 }
e276548b 1541
fc73a202 1542 if (aTotalElementsNb != 0)
1543 {
1544 aResult &= myGeometryTriangTexture->Init (
1545 myGlContext, 4, aTotalElementsNb, static_cast<const GLuint*> (NULL));
1546 }
265d4508 1547
fc73a202 1548 if (aTotalVerticesNb != 0)
1549 {
1550 aResult &= myGeometryVertexTexture->Init (
1551 myGlContext, 4, aTotalVerticesNb, static_cast<const GLfloat*> (NULL));
e276548b 1552
fc73a202 1553 aResult &= myGeometryNormalTexture->Init (
1554 myGlContext, 4, aTotalVerticesNb, static_cast<const GLfloat*> (NULL));
1555 }
265d4508 1556
fc73a202 1557 if (!aResult)
1558 {
1559#ifdef RAY_TRACE_PRINT_INFO
1560 std::cout << "Error: Failed to upload buffers for scene geometry" << std::endl;
1561#endif
1562 return Standard_False;
1563 }
265d4508 1564
fc73a202 1565 for (Standard_Integer aNodeIdx = 0; aNodeIdx < aBVH->Length(); ++aNodeIdx)
1566 {
1567 if (!aBVH->IsOuter (aNodeIdx))
1568 continue;
265d4508 1569
fc73a202 1570 OpenGl_TriangleSet* aTriangleSet = myRaytraceGeometry.TriangleSet (aNodeIdx);
e276548b 1571
fc73a202 1572 Standard_ASSERT_RETURN (aTriangleSet != NULL,
1573 "Error: Failed to get triangulation of OpenGL element", Standard_False);
e276548b 1574
fc73a202 1575 const Standard_Integer aBVHOffset = myRaytraceGeometry.AccelerationOffset (aNodeIdx);
e276548b 1576
fc73a202 1577 Standard_ASSERT_RETURN (aBVHOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET,
1578 "Error: Failed to get offset for bottom-level BVH", Standard_False);
e276548b 1579
fc73a202 1580 const size_t aBVHBuffserSize = aTriangleSet->BVH()->NodeInfoBuffer().size();
265d4508 1581
fc73a202 1582 if (aBVHBuffserSize != 0)
1583 {
1584 aResult &= myObjectNodeInfoTexture->SubData (myGlContext, aBVHOffset,
1585 aBVHBuffserSize, reinterpret_cast<const GLuint*> (&aTriangleSet->BVH()->NodeInfoBuffer().front()));
265d4508 1586
fc73a202 1587 aResult &= myObjectMinPointTexture->SubData (myGlContext, aBVHOffset,
1588 aBVHBuffserSize, reinterpret_cast<const GLfloat*> (&aTriangleSet->BVH()->MinPointBuffer().front()));
e276548b 1589
fc73a202 1590 aResult &= myObjectMaxPointTexture->SubData (myGlContext, aBVHOffset,
1591 aBVHBuffserSize, reinterpret_cast<const GLfloat*> (&aTriangleSet->BVH()->MaxPointBuffer().front()));
e276548b 1592
fc73a202 1593 if (!aResult)
e276548b 1594 {
fc73a202 1595#ifdef RAY_TRACE_PRINT_INFO
1596 std::cout << "Error: Failed to upload buffers for bottom-level scene BVHs" << std::endl;
1597#endif
e276548b 1598 return Standard_False;
1599 }
1600 }
1601
fc73a202 1602 const Standard_Integer aVerticesOffset = myRaytraceGeometry.VerticesOffset (aNodeIdx);
265d4508 1603
fc73a202 1604 Standard_ASSERT_RETURN (aVerticesOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET,
1605 "Error: Failed to get offset for triangulation vertices of OpenGL element", Standard_False);
265d4508 1606
fc73a202 1607 if (!aTriangleSet->Vertices.empty())
1608 {
1609 aResult &= myGeometryNormalTexture->SubData (myGlContext, aVerticesOffset,
1610 aTriangleSet->Normals.size(), reinterpret_cast<const GLfloat*> (&aTriangleSet->Normals.front()));
e276548b 1611
fc73a202 1612 aResult &= myGeometryVertexTexture->SubData (myGlContext, aVerticesOffset,
1613 aTriangleSet->Vertices.size(), reinterpret_cast<const GLfloat*> (&aTriangleSet->Vertices.front()));
1614 }
e276548b 1615
fc73a202 1616 const Standard_Integer anElementsOffset = myRaytraceGeometry.ElementsOffset (aNodeIdx);
265d4508 1617
fc73a202 1618 Standard_ASSERT_RETURN (anElementsOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET,
1619 "Error: Failed to get offset for triangulation elements of OpenGL element", Standard_False);
e276548b 1620
fc73a202 1621 if (!aTriangleSet->Elements.empty())
e276548b 1622 {
fc73a202 1623 aResult &= myGeometryTriangTexture->SubData (myGlContext, anElementsOffset,
1624 aTriangleSet->Elements.size(), reinterpret_cast<const GLuint*> (&aTriangleSet->Elements.front()));
e276548b 1625 }
265d4508 1626
fc73a202 1627 if (!aResult)
1628 {
1629#ifdef RAY_TRACE_PRINT_INFO
1630 std::cout << "Error: Failed to upload triangulation buffers for OpenGL element" << std::endl;
e276548b 1631#endif
fc73a202 1632 return Standard_False;
1633 }
e276548b 1634 }
e276548b 1635
fc73a202 1636 if (myRaytraceGeometry.Materials.size() != 0)
1637 {
1638 const GLfloat* aDataPtr = myRaytraceGeometry.Materials.front().Packed();
e276548b 1639
fc73a202 1640 aResult &= myRaytraceMaterialTexture->Init (
1641 myGlContext, 4, myRaytraceGeometry.Materials.size() * 7, aDataPtr);
e276548b 1642
fc73a202 1643 if (!aResult)
1644 {
1645#ifdef RAY_TRACE_PRINT_INFO
1646 std::cout << "Error: Failed to upload material buffer" << std::endl;
1647#endif
1648 return Standard_False;
1649 }
1650 }
e276548b 1651
fc73a202 1652 myIsRaytraceDataValid = myRaytraceGeometry.Objects().Size() != 0;
e276548b 1653
fc73a202 1654#ifdef RAY_TRACE_PRINT_INFO
e276548b 1655
fc73a202 1656 Standard_ShortReal aMemUsed = 0.f;
e276548b 1657
fc73a202 1658 for (Standard_Integer anElemIdx = 0; anElemIdx < myRaytraceGeometry.Size(); ++anElemIdx)
1659 {
1660 OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> (
1661 myRaytraceGeometry.Objects().ChangeValue (anElemIdx).operator->());
e276548b 1662
fc73a202 1663 aMemUsed += static_cast<Standard_ShortReal> (
1664 aTriangleSet->Vertices.size() * sizeof (BVH_Vec4f));
1665 aMemUsed += static_cast<Standard_ShortReal> (
1666 aTriangleSet->Normals.size() * sizeof (BVH_Vec4f));
1667 aMemUsed += static_cast<Standard_ShortReal> (
1668 aTriangleSet->Elements.size() * sizeof (BVH_Vec4i));
e276548b 1669
fc73a202 1670 aMemUsed += static_cast<Standard_ShortReal> (
1671 aTriangleSet->BVH()->NodeInfoBuffer().size() * sizeof (BVH_Vec4i));
1672 aMemUsed += static_cast<Standard_ShortReal> (
1673 aTriangleSet->BVH()->MinPointBuffer().size() * sizeof (BVH_Vec4f));
1674 aMemUsed += static_cast<Standard_ShortReal> (
1675 aTriangleSet->BVH()->MaxPointBuffer().size() * sizeof (BVH_Vec4f));
1676 }
e276548b 1677
fc73a202 1678 aMemUsed += static_cast<Standard_ShortReal> (
1679 myRaytraceGeometry.BVH()->NodeInfoBuffer().size() * sizeof (BVH_Vec4i));
1680 aMemUsed += static_cast<Standard_ShortReal> (
1681 myRaytraceGeometry.BVH()->MinPointBuffer().size() * sizeof (BVH_Vec4f));
1682 aMemUsed += static_cast<Standard_ShortReal> (
1683 myRaytraceGeometry.BVH()->MaxPointBuffer().size() * sizeof (BVH_Vec4f));
e276548b 1684
fc73a202 1685 std::cout << "GPU Memory Used (MB): ~" << aMemUsed / 1048576 << std::endl;
e276548b 1686
fc73a202 1687#endif
e276548b 1688
fc73a202 1689 return aResult;
1690}
e276548b 1691
fc73a202 1692// =======================================================================
1693// function : ResizeRaytraceBuffers
1694// purpose : Resizes OpenGL frame buffers
1695// =======================================================================
1696Standard_Boolean OpenGl_Workspace::ResizeRaytraceBuffers (const Standard_Integer theSizeX,
1697 const Standard_Integer theSizeY)
1698{
1699 if (myRaytraceFBO1->GetVPSizeX() != theSizeX
1700 || myRaytraceFBO1->GetVPSizeY() != theSizeY)
1701 {
1702 myRaytraceFBO1->Init (myGlContext, theSizeX, theSizeY);
1703 myRaytraceFBO2->Init (myGlContext, theSizeX, theSizeY);
1704 }
e276548b 1705
fc73a202 1706 return Standard_True;
e276548b 1707}
1708
1709// =======================================================================
fc73a202 1710// function : UpdateCamera
1711// purpose : Generates viewing rays for corners of screen quad
e276548b 1712// =======================================================================
fc73a202 1713void OpenGl_Workspace::UpdateCamera (const NCollection_Mat4<GLdouble>& theOrientation,
1714 const NCollection_Mat4<GLdouble>& theViewMapping,
1715 OpenGl_Vec3 theOrigins[4],
1716 OpenGl_Vec3 theDirects[4])
e276548b 1717{
fc73a202 1718 NCollection_Mat4<GLdouble> aInvModelProj;
1719
1720 // compute invserse model-view-projection matrix
1721 (theViewMapping * theOrientation).Inverted (aInvModelProj);
1722
68333c8f 1723 Standard_Integer aOriginIndex = 0;
1724 Standard_Integer aDirectIndex = 0;
e276548b 1725
fc73a202 1726 for (Standard_Integer aY = -1; aY <= 1; aY += 2)
e276548b 1727 {
fc73a202 1728 for (Standard_Integer aX = -1; aX <= 1; aX += 2)
e276548b 1729 {
fc73a202 1730 OpenGl_Vec4d aOrigin (GLdouble(aX),
1731 GLdouble(aY),
1732 -1.0,
1733 1.0);
1734
1735 aOrigin = aInvModelProj * aOrigin;
e276548b 1736
b5ac8292 1737 aOrigin.x() = aOrigin.x() / aOrigin.w();
1738 aOrigin.y() = aOrigin.y() / aOrigin.w();
1739 aOrigin.z() = aOrigin.z() / aOrigin.w();
e276548b 1740
fc73a202 1741 OpenGl_Vec4d aDirect (GLdouble(aX),
1742 GLdouble(aY),
1743 1.0,
1744 1.0);
1745
1746 aDirect = aInvModelProj * aDirect;
e276548b 1747
b5ac8292 1748 aDirect.x() = aDirect.x() / aDirect.w();
1749 aDirect.y() = aDirect.y() / aDirect.w();
1750 aDirect.z() = aDirect.z() / aDirect.w();
b5ac8292 1751
1752 aDirect = aDirect - aOrigin;
e276548b 1753
fc73a202 1754 GLdouble aInvLen = 1.0 / sqrt (aDirect.x() * aDirect.x() +
e276548b 1755 aDirect.y() * aDirect.y() +
1756 aDirect.z() * aDirect.z());
1757
fc73a202 1758 theOrigins[aOriginIndex++] = OpenGl_Vec3 (static_cast<GLfloat> (aOrigin.x()),
1759 static_cast<GLfloat> (aOrigin.y()),
1760 static_cast<GLfloat> (aOrigin.z()));
1761
1762 theDirects[aDirectIndex++] = OpenGl_Vec3 (static_cast<GLfloat> (aDirect.x() * aInvLen),
1763 static_cast<GLfloat> (aDirect.y() * aInvLen),
1764 static_cast<GLfloat> (aDirect.z() * aInvLen));
1765 }
1766 }
1767}
1768
1769// =======================================================================
1770// function : RunRaytraceShaders
1771// purpose : Runs ray-tracing shader programs
1772// =======================================================================
1773Standard_Boolean OpenGl_Workspace::RunRaytraceShaders (const Graphic3d_CView& theCView,
1774 const Standard_Integer theSizeX,
1775 const Standard_Integer theSizeY,
1776 const OpenGl_Vec3 theOrigins[4],
1777 const OpenGl_Vec3 theDirects[4],
1778 OpenGl_FrameBuffer* theFrameBuffer)
1779{
1780 mySceneMinPointTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_SceneMinPointTexture);
1781 mySceneMaxPointTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_SceneMaxPointTexture);
1782 mySceneNodeInfoTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_SceneNodeInfoTexture);
1783 myObjectMinPointTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_ObjectMinPointTexture);
1784 myObjectMaxPointTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_ObjectMaxPointTexture);
1785 myObjectNodeInfoTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_ObjectNodeInfoTexture);
1786 myGeometryVertexTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryVertexTexture);
1787 myGeometryNormalTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryNormalTexture);
1788 myGeometryTriangTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryTriangTexture);
1789 myRaytraceMaterialTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceMaterialTexture);
1790 myRaytraceLightSrcTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceLightSrcTexture);
1791
1792 if (theCView.IsAntialiasingEnabled) // render source image to FBO
1793 {
1794 myRaytraceFBO1->BindBuffer (myGlContext);
1795
1796 glDisable (GL_BLEND);
1797 }
1798
1799 myRaytraceProgram->Bind (myGlContext);
1800
1801 Standard_Integer aLightSourceBufferSize =
1802 static_cast<Standard_Integer> (myRaytraceGeometry.Sources.size());
1803
1804 myRaytraceProgram->SetUniform (myGlContext,
1805 myUniformLocations[0][OpenGl_RT_uOriginLB], theOrigins[0]);
1806 myRaytraceProgram->SetUniform (myGlContext,
1807 myUniformLocations[0][OpenGl_RT_uOriginRB], theOrigins[1]);
1808 myRaytraceProgram->SetUniform (myGlContext,
1809 myUniformLocations[0][OpenGl_RT_uOriginLT], theOrigins[2]);
1810 myRaytraceProgram->SetUniform (myGlContext,
1811 myUniformLocations[0][OpenGl_RT_uOriginRT], theOrigins[3]);
1812 myRaytraceProgram->SetUniform (myGlContext,
1813 myUniformLocations[0][OpenGl_RT_uDirectLB], theDirects[0]);
1814 myRaytraceProgram->SetUniform (myGlContext,
1815 myUniformLocations[0][OpenGl_RT_uDirectRB], theDirects[1]);
1816 myRaytraceProgram->SetUniform (myGlContext,
1817 myUniformLocations[0][OpenGl_RT_uDirectLT], theDirects[2]);
1818 myRaytraceProgram->SetUniform (myGlContext,
1819 myUniformLocations[0][OpenGl_RT_uDirectRT], theDirects[3]);
1820 myRaytraceProgram->SetUniform (myGlContext,
1821 myUniformLocations[0][OpenGl_RT_uSceneRad], myRaytraceSceneRadius);
1822 myRaytraceProgram->SetUniform (myGlContext,
1823 myUniformLocations[0][OpenGl_RT_uSceneEps], myRaytraceSceneEpsilon);
1824 myRaytraceProgram->SetUniform (myGlContext,
1825 myUniformLocations[0][OpenGl_RT_uLightCount], aLightSourceBufferSize);
1826 myRaytraceProgram->SetUniform (myGlContext,
1827 myUniformLocations[0][OpenGl_RT_uLightAmbnt], myRaytraceGeometry.Ambient);
1828 myRaytraceProgram->SetUniform (myGlContext,
1829 myUniformLocations[0][OpenGl_RT_uShadEnabled], theCView.IsShadowsEnabled);
1830 myRaytraceProgram->SetUniform (myGlContext,
1831 myUniformLocations[0][OpenGl_RT_uReflEnabled], theCView.IsReflectionsEnabled);
1832
1833 myGlContext->core20fwd->glEnableVertexAttribArray (
1834 myUniformLocations[0][OpenGl_RT_aPosition]);
1835 {
1836 myGlContext->core20fwd->glVertexAttribPointer (
1837 myUniformLocations[0][OpenGl_RT_aPosition], 3, GL_FLOAT, GL_FALSE, 0, NULL);
1838
1839 myGlContext->core15fwd->glDrawArrays (GL_TRIANGLES, 0, 6);
1840 }
1841 myGlContext->core20fwd->glDisableVertexAttribArray (
1842 myUniformLocations[0][OpenGl_RT_aPosition]);
1843
1844 if (!theCView.IsAntialiasingEnabled)
1845 {
1846 myRaytraceProgram->Unbind (myGlContext);
1847
1848 return Standard_True;
1849 }
1850
1851 myGlContext->core20fwd->glActiveTexture (
1852 GL_TEXTURE0 + OpenGl_RT_FSAAInputTexture); // texture unit for FBO texture
1853
1854 myRaytraceFBO1->BindTexture (myGlContext);
1855
1856 myPostFSAAProgram->Bind (myGlContext);
1857
1858 myPostFSAAProgram->SetUniform (myGlContext,
1859 myUniformLocations[1][OpenGl_RT_uOriginLB], theOrigins[0]);
1860 myPostFSAAProgram->SetUniform (myGlContext,
1861 myUniformLocations[1][OpenGl_RT_uOriginRB], theOrigins[1]);
1862 myPostFSAAProgram->SetUniform (myGlContext,
1863 myUniformLocations[1][OpenGl_RT_uOriginLT], theOrigins[2]);
1864 myPostFSAAProgram->SetUniform (myGlContext,
1865 myUniformLocations[1][OpenGl_RT_uOriginRT], theOrigins[3]);
1866 myPostFSAAProgram->SetUniform (myGlContext,
1867 myUniformLocations[1][OpenGl_RT_uDirectLB], theDirects[0]);
1868 myPostFSAAProgram->SetUniform (myGlContext,
1869 myUniformLocations[1][OpenGl_RT_uDirectRB], theDirects[1]);
1870 myPostFSAAProgram->SetUniform (myGlContext,
1871 myUniformLocations[1][OpenGl_RT_uDirectLT], theDirects[2]);
1872 myPostFSAAProgram->SetUniform (myGlContext,
1873 myUniformLocations[1][OpenGl_RT_uDirectRT], theDirects[3]);
1874 myPostFSAAProgram->SetUniform (myGlContext,
1875 myUniformLocations[1][OpenGl_RT_uSceneRad], myRaytraceSceneRadius);
1876 myPostFSAAProgram->SetUniform (myGlContext,
1877 myUniformLocations[1][OpenGl_RT_uSceneEps], myRaytraceSceneEpsilon);
1878 myPostFSAAProgram->SetUniform (myGlContext,
1879 myUniformLocations[1][OpenGl_RT_uLightCount], aLightSourceBufferSize);
1880 myPostFSAAProgram->SetUniform (myGlContext,
1881 myUniformLocations[1][OpenGl_RT_uLightAmbnt], myRaytraceGeometry.Ambient);
1882 myPostFSAAProgram->SetUniform (myGlContext,
1883 myUniformLocations[1][OpenGl_RT_uShadEnabled], theCView.IsShadowsEnabled);
1884 myPostFSAAProgram->SetUniform (myGlContext,
1885 myUniformLocations[1][OpenGl_RT_uReflEnabled], theCView.IsReflectionsEnabled);
1886
1887 const Standard_ShortReal aMaxOffset = 0.559017f;
1888 const Standard_ShortReal aMinOffset = 0.186339f;
1889
1890 myGlContext->core20fwd->glEnableVertexAttribArray (
1891 myUniformLocations[1][OpenGl_RT_aPosition]);
1892
1893 myGlContext->core20fwd->glVertexAttribPointer (
1894 myUniformLocations[1][OpenGl_RT_aPosition], 3, GL_FLOAT, GL_FALSE, 0, NULL);
1895
1896 // Perform multi-pass adaptive FSAA using ping-pong technique
1897 for (Standard_Integer anIt = 0; anIt < 4; ++anIt)
1898 {
1899 GLfloat aOffsetX = 1.f / theSizeX;
1900 GLfloat aOffsetY = 1.f / theSizeY;
1901
1902 if (anIt < 2)
1903 {
1904 aOffsetX *= anIt < 1 ? aMinOffset : -aMaxOffset;
1905 aOffsetY *= anIt < 1 ? aMaxOffset : aMinOffset;
1906 }
1907 else
1908 {
1909 aOffsetX *= anIt > 2 ? aMaxOffset : -aMinOffset;
1910 aOffsetY *= anIt > 2 ? -aMinOffset : -aMaxOffset;
1911 }
1912
1913 myPostFSAAProgram->SetUniform (myGlContext,
1914 myUniformLocations[1][OpenGl_RT_uSamples], anIt + 2);
1915 myPostFSAAProgram->SetUniform (myGlContext,
1916 myUniformLocations[1][OpenGl_RT_uOffsetX], aOffsetX);
1917 myPostFSAAProgram->SetUniform (myGlContext,
1918 myUniformLocations[1][OpenGl_RT_uOffsetY], aOffsetY);
1919
1920 Handle(OpenGl_FrameBuffer)& aFramebuffer = anIt % 2 ? myRaytraceFBO1 : myRaytraceFBO2;
1921
1922 if (anIt == 3) // disable FBO on last iteration
1923 {
1924 glEnable (GL_BLEND);
1925
1926 if (theFrameBuffer != NULL)
1927 theFrameBuffer->BindBuffer (myGlContext);
1928 }
1929 else
1930 {
1931 aFramebuffer->BindBuffer (myGlContext);
1932 }
1933
1934 myGlContext->core15fwd->glDrawArrays (GL_TRIANGLES, 0, 6);
e276548b 1935
fc73a202 1936 if (anIt != 3) // set input for the next pass
1937 {
1938 aFramebuffer->BindTexture (myGlContext);
1939 aFramebuffer->UnbindBuffer (myGlContext);
e276548b 1940 }
1941 }
fc73a202 1942
1943 myGlContext->core20fwd->glDisableVertexAttribArray (
1944 myUniformLocations[1][OpenGl_RT_aPosition]);
1945
1946 myPostFSAAProgram->Unbind (myGlContext);
1947
1948 return Standard_True;
e276548b 1949}
1950
1951// =======================================================================
1952// function : Raytrace
fc73a202 1953// purpose : Redraws the window using OpenGL/GLSL ray-tracing
e276548b 1954// =======================================================================
1955Standard_Boolean OpenGl_Workspace::Raytrace (const Graphic3d_CView& theCView,
68333c8f 1956 const Standard_Integer theSizeX,
1957 const Standard_Integer theSizeY,
fc73a202 1958 const Standard_Boolean theToSwap,
1959 OpenGl_FrameBuffer* theFrameBuffer)
e276548b 1960{
fc73a202 1961 if (!UpdateRaytraceGeometry (Standard_True))
e276548b 1962 return Standard_False;
1963
fc73a202 1964 if (!InitRaytraceResources())
e276548b 1965 return Standard_False;
1966
fc73a202 1967 if (!ResizeRaytraceBuffers (theSizeX, theSizeY))
e276548b 1968 return Standard_False;
1969
fc73a202 1970 if (!UpdateRaytraceEnvironmentMap())
e276548b 1971 return Standard_False;
1972
1973 // Get model-view and projection matrices
1974 TColStd_Array2OfReal theOrientation (0, 3, 0, 3);
1975 TColStd_Array2OfReal theViewMapping (0, 3, 0, 3);
1976
b5ac8292 1977 myView->GetMatrices (theOrientation, theViewMapping);
e276548b 1978
fc73a202 1979 NCollection_Mat4<GLdouble> aOrientationMatrix;
1980 NCollection_Mat4<GLdouble> aViewMappingMatrix;
e276548b 1981
68333c8f 1982 for (Standard_Integer j = 0; j < 4; ++j)
fc73a202 1983 {
68333c8f 1984 for (Standard_Integer i = 0; i < 4; ++i)
e276548b 1985 {
1986 aOrientationMatrix [4 * j + i] = theOrientation (i, j);
1987 aViewMappingMatrix [4 * j + i] = theViewMapping (i, j);
1988 }
fc73a202 1989 }
1990
1991 NCollection_Mat4<GLdouble> aInvOrientationMatrix;
1992 aOrientationMatrix.Inverted (aInvOrientationMatrix);
e276548b 1993
fc73a202 1994 if (!UpdateRaytraceLightSources (aInvOrientationMatrix))
e276548b 1995 return Standard_False;
1996
fc73a202 1997 OpenGl_Vec3 aOrigins[4];
1998 OpenGl_Vec3 aDirects[4];
e276548b 1999
fc73a202 2000 UpdateCamera (aOrientationMatrix,
2001 aViewMappingMatrix,
2002 aOrigins,
2003 aDirects);
e276548b 2004
2005 // Draw background
2006 glPushAttrib (GL_ENABLE_BIT |
2007 GL_CURRENT_BIT |
2008 GL_COLOR_BUFFER_BIT |
2009 GL_DEPTH_BUFFER_BIT);
2010
2011 glDisable (GL_DEPTH_TEST);
2012
2013 if (NamedStatus & OPENGL_NS_WHITEBACK)
2014 {
2015 glClearColor (1.0f, 1.0f, 1.0f, 1.0f);
2016 }
2017 else
2018 {
2019 glClearColor (myBgColor.rgb[0],
2020 myBgColor.rgb[1],
2021 myBgColor.rgb[2],
2022 1.0f);
2023 }
2024
2025 glClear (GL_COLOR_BUFFER_BIT);
2026
fc73a202 2027 if (theFrameBuffer != NULL)
2028 theFrameBuffer->BindBuffer (myGlContext);
e276548b 2029
fc73a202 2030 myView->DrawBackground (*this);
e276548b 2031
fc73a202 2032 // Generate ray-traced image
e276548b 2033 glMatrixMode (GL_PROJECTION);
2034 glLoadIdentity();
2035
2036 glMatrixMode (GL_MODELVIEW);
2037 glLoadIdentity();
2038
fc73a202 2039 glEnable (GL_BLEND);
2040 glBlendFunc (GL_ONE, GL_SRC_ALPHA);
e276548b 2041
2042 if (myIsRaytraceDataValid)
2043 {
fc73a202 2044 myRaytraceScreenQuad.Bind (myGlContext);
2045
2046 RunRaytraceShaders (theCView,
2047 theSizeX,
2048 theSizeY,
2049 aOrigins,
2050 aDirects,
2051 theFrameBuffer);
2052
2053 myRaytraceScreenQuad.Unbind (myGlContext);
e276548b 2054 }
2055
fc73a202 2056 if (theFrameBuffer != NULL)
2057 theFrameBuffer->UnbindBuffer (myGlContext);
2058
e276548b 2059 glPopAttrib();
2060
2061 // Swap the buffers
2062 if (theToSwap)
2063 {
2064 GetGlContext()->SwapBuffers();
2065 myBackBufferRestored = Standard_False;
2066 }
2067 else
fc73a202 2068 {
e276548b 2069 glFlush();
fc73a202 2070 }
e276548b 2071
2072 return Standard_True;
2073}