0024797: TKOpenGl, OpenGl_PointSprite - the assert is absent during incorrect object...
[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();
5cff985a 818 if (!myRaytraceLightSrcTexture->Init (myGlContext, 4, GLsizei (myRaytraceGeometry.Sources.size() * 2), aDataPtr))
fc73a202 819 {
820#ifdef RAY_TRACE_PRINT_INFO
821 std::cout << "Error: Failed to upload light source buffer" << std::endl;
e276548b 822#endif
fc73a202 823 return Standard_False;
824 }
825 }
e276548b 826
827 return Standard_True;
828}
829
830// =======================================================================
fc73a202 831// function : UpdateRaytraceEnvironmentMap
832// purpose : Updates environment map for ray-tracing
e276548b 833// =======================================================================
fc73a202 834Standard_Boolean OpenGl_Workspace::UpdateRaytraceEnvironmentMap()
e276548b 835{
fc73a202 836 if (myView.IsNull())
e276548b 837 return Standard_False;
e276548b 838
fc73a202 839 if (myViewModificationStatus == myView->ModificationState())
840 return Standard_True;
e276548b 841
fc73a202 842 for (Standard_Integer anIdx = 0; anIdx < 2; ++anIdx)
e276548b 843 {
fc73a202 844 const Handle(OpenGl_ShaderProgram)& aProgram =
845 anIdx == 0 ? myRaytraceProgram : myPostFSAAProgram;
e276548b 846
fc73a202 847 if (!aProgram.IsNull())
e276548b 848 {
fc73a202 849 aProgram->Bind (myGlContext);
e276548b 850
fc73a202 851 if (!myView->TextureEnv().IsNull() && myView->SurfaceDetail() != Visual3d_TOD_NONE)
e276548b 852 {
fc73a202 853 myView->TextureEnv()->Bind (
854 myGlContext, GL_TEXTURE0 + OpenGl_RT_EnvironmentMapTexture);
855
856 aProgram->SetUniform (myGlContext, "uEnvironmentEnable", 1);
e276548b 857 }
858 else
859 {
fc73a202 860 aProgram->SetUniform (myGlContext, "uEnvironmentEnable", 0);
e276548b 861 }
e276548b 862
fc73a202 863 aProgram->SetSampler (myGlContext,
864 "uEnvironmentMapTexture", OpenGl_RT_EnvironmentMapTexture);
865 }
e276548b 866 }
867
fc73a202 868 OpenGl_ShaderProgram::Unbind (myGlContext);
e276548b 869
fc73a202 870 myViewModificationStatus = myView->ModificationState();
e276548b 871
e276548b 872 return Standard_True;
873}
874
875// =======================================================================
fc73a202 876// function : Source
877// purpose : Returns shader source combined with prefix
e276548b 878// =======================================================================
fc73a202 879TCollection_AsciiString OpenGl_Workspace::ShaderSource::Source() const
e276548b 880{
fc73a202 881 static const TCollection_AsciiString aVersion = "#version 140";
e276548b 882
fc73a202 883 if (myPrefix.IsEmpty())
e276548b 884 {
fc73a202 885 return aVersion + "\n" + mySource;
e276548b 886 }
887
fc73a202 888 return aVersion + "\n" + myPrefix + "\n" + mySource;
e276548b 889}
890
891// =======================================================================
fc73a202 892// function : Load
893// purpose : Loads shader source from specified files
e276548b 894// =======================================================================
fc73a202 895void OpenGl_Workspace::ShaderSource::Load (
896 const TCollection_AsciiString* theFileNames, const Standard_Integer theCount)
e276548b 897{
fc73a202 898 mySource.Clear();
e276548b 899
fc73a202 900 for (Standard_Integer anIndex = 0; anIndex < theCount; ++anIndex)
901 {
902 OSD_File aFile (theFileNames[anIndex]);
e276548b 903
fc73a202 904 Standard_ASSERT_RETURN (aFile.Exists(),
905 "Error: Failed to find shader source file", /* none */);
e276548b 906
fc73a202 907 aFile.Open (OSD_ReadOnly, OSD_Protection());
265d4508 908
fc73a202 909 TCollection_AsciiString aSource;
265d4508 910
fc73a202 911 Standard_ASSERT_RETURN (aFile.IsOpen(),
912 "Error: Failed to open shader source file", /* none */);
e276548b 913
fc73a202 914 aFile.Read (aSource, (Standard_Integer) aFile.Size());
e276548b 915
fc73a202 916 if (!aSource.IsEmpty())
917 {
918 mySource += TCollection_AsciiString ("\n") + aSource;
919 }
68333c8f 920
fc73a202 921 aFile.Close();
68333c8f 922 }
e276548b 923}
924
925// =======================================================================
fc73a202 926// function : LoadShader
927// purpose : Creates new shader object with specified source
e276548b 928// =======================================================================
fc73a202 929Handle(OpenGl_ShaderObject) OpenGl_Workspace::LoadShader (const ShaderSource& theSource, GLenum theType)
e276548b 930{
fc73a202 931 Handle(OpenGl_ShaderObject) aShader = new OpenGl_ShaderObject (theType);
e276548b 932
fc73a202 933 if (!aShader->Create (myGlContext))
e276548b 934 {
fc73a202 935 const TCollection_ExtendedString aMessage = "Error: Failed to create shader object";
936
937 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
938 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
e276548b 939
fc73a202 940 aShader->Release (myGlContext.operator->());
e276548b 941
fc73a202 942 return Handle(OpenGl_ShaderObject)();
e276548b 943 }
5322131b 944
fc73a202 945 if (!aShader->LoadSource (myGlContext, theSource.Source()))
946 {
947 const TCollection_ExtendedString aMessage = "Error: Failed to set shader source";
948
949 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
950 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
e276548b 951
fc73a202 952 aShader->Release (myGlContext.operator->());
e276548b 953
fc73a202 954 return Handle(OpenGl_ShaderObject)();
955 }
e276548b 956
fc73a202 957 TCollection_AsciiString aBuildLog;
e276548b 958
fc73a202 959 if (!aShader->Compile (myGlContext))
e276548b 960 {
fc73a202 961 if (aShader->FetchInfoLog (myGlContext, aBuildLog))
962 {
963 const TCollection_ExtendedString aMessage =
964 TCollection_ExtendedString ("Error: Failed to compile shader object:\n") + aBuildLog;
965
e276548b 966#ifdef RAY_TRACE_PRINT_INFO
fc73a202 967 std::cout << aBuildLog << std::endl;
e276548b 968#endif
e276548b 969
fc73a202 970 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
971 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
972 }
973
974 aShader->Release (myGlContext.operator->());
975
976 return Handle(OpenGl_ShaderObject)();
977 }
68333c8f 978
e276548b 979#ifdef RAY_TRACE_PRINT_INFO
fc73a202 980 if (aShader->FetchInfoLog (myGlContext, aBuildLog))
981 {
982 if (!aBuildLog.IsEmpty())
983 {
984 std::cout << aBuildLog << std::endl;
985 }
986 else
987 {
988 std::cout << "Info: shader build log is empty" << std::endl;
989 }
990 }
e276548b 991#endif
e276548b 992
fc73a202 993 return aShader;
e276548b 994}
995
996// =======================================================================
fc73a202 997// function : SafeFailBack
998// purpose : Performs safe exit when shaders initialization fails
e276548b 999// =======================================================================
fc73a202 1000Standard_Boolean OpenGl_Workspace::SafeFailBack (const TCollection_ExtendedString& theMessage)
e276548b 1001{
fc73a202 1002 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1003 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, theMessage);
265d4508 1004
fc73a202 1005 myComputeInitStatus = OpenGl_RT_FAIL;
265d4508 1006
fc73a202 1007 ReleaseRaytraceResources();
1008
1009 return Standard_False;
1010}
e276548b 1011
fc73a202 1012// =======================================================================
1013// function : InitRaytraceResources
1014// purpose : Initializes OpenGL/GLSL shader programs
1015// =======================================================================
1016Standard_Boolean OpenGl_Workspace::InitRaytraceResources()
1017{
1018 Standard_Boolean aToRebuildShaders = Standard_False;
e276548b 1019
fc73a202 1020 if (myComputeInitStatus == OpenGl_RT_INIT)
1021 {
1022 if (!myIsRaytraceDataValid)
1023 return Standard_True;
e276548b 1024
fc73a202 1025 const Standard_Integer aRequiredStackSize =
1026 myRaytraceGeometry.HighLevelTreeDepth() + myRaytraceGeometry.BottomLevelTreeDepth();
e276548b 1027
fc73a202 1028 if (myTraversalStackSize < aRequiredStackSize)
1029 {
1030 myTraversalStackSize = Max (aRequiredStackSize, THE_DEFAULT_STACK_SIZE);
e276548b 1031
fc73a202 1032 aToRebuildShaders = Standard_True;
1033 }
1034 else
1035 {
1036 if (aRequiredStackSize < myTraversalStackSize)
1037 {
1038 if (myTraversalStackSize > THE_DEFAULT_STACK_SIZE)
1039 {
1040 myTraversalStackSize = Max (aRequiredStackSize, THE_DEFAULT_STACK_SIZE);
e276548b 1041
fc73a202 1042 aToRebuildShaders = Standard_True;
1043 }
1044 }
1045 }
e276548b 1046
fc73a202 1047 if (aToRebuildShaders)
1048 {
e276548b 1049#ifdef RAY_TRACE_PRINT_INFO
fc73a202 1050 std::cout << "Info: Rebuild shaders with stack size: " << myTraversalStackSize << std::endl;
e276548b 1051#endif
e276548b 1052
fc73a202 1053 TCollection_AsciiString aStackSizeStr =
1054 TCollection_AsciiString ("#define STACK_SIZE ") + TCollection_AsciiString (myTraversalStackSize);
265d4508 1055
fc73a202 1056 myRaytraceShaderSource.SetPrefix (aStackSizeStr);
1057 myPostFSAAShaderSource.SetPrefix (aStackSizeStr);
265d4508 1058
fc73a202 1059 if (!myRaytraceShader->LoadSource (myGlContext, myRaytraceShaderSource.Source())
1060 || !myPostFSAAShader->LoadSource (myGlContext, myPostFSAAShaderSource.Source()))
1061 {
1062 return Standard_False;
1063 }
265d4508 1064
fc73a202 1065 if (!myRaytraceShader->Compile (myGlContext)
1066 || !myPostFSAAShader->Compile (myGlContext))
1067 {
1068 return Standard_False;
1069 }
1070
1071 if (!myRaytraceProgram->Link (myGlContext)
1072 || !myPostFSAAProgram->Link (myGlContext))
1073 {
1074 return Standard_False;
1075 }
1076 }
e276548b 1077 }
1078
fc73a202 1079 if (myComputeInitStatus == OpenGl_RT_NONE)
1080 {
1081 if (!myGlContext->IsGlGreaterEqual (3, 1))
1082 {
1083 const TCollection_ExtendedString aMessage = "Ray-tracing requires OpenGL 3.1 and higher";
265d4508 1084
fc73a202 1085 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1086 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
265d4508 1087
fc73a202 1088 return Standard_False;
1089 }
5322131b 1090
fc73a202 1091 TCollection_AsciiString aFolder = Graphic3d_ShaderProgram::ShadersFolder();
265d4508 1092
fc73a202 1093 if (aFolder.IsEmpty())
1094 {
1095 const TCollection_ExtendedString aMessage = "Failed to locate shaders directory";
1096
1097 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1098 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
1099
1100 return Standard_False;
1101 }
265d4508 1102
fc73a202 1103 if (myIsRaytraceDataValid)
1104 {
1105 myTraversalStackSize = Max (THE_DEFAULT_STACK_SIZE,
1106 myRaytraceGeometry.HighLevelTreeDepth() + myRaytraceGeometry.BottomLevelTreeDepth());
1107 }
265d4508 1108
fc73a202 1109 {
1110 Handle(OpenGl_ShaderObject) aBasicVertShader = LoadShader (
1111 ShaderSource (aFolder + "/RaytraceBase.vs"), GL_VERTEX_SHADER);
265d4508 1112
fc73a202 1113 if (aBasicVertShader.IsNull())
1114 {
1115 return SafeFailBack ("Failed to set vertex shader source");
1116 }
265d4508 1117
fc73a202 1118 TCollection_AsciiString aFiles[] = { aFolder + "/RaytraceBase.fs", aFolder + "/RaytraceRender.fs" };
265d4508 1119
fc73a202 1120 myRaytraceShaderSource.Load (aFiles, 2);
e276548b 1121
fc73a202 1122 TCollection_AsciiString aStackSizeStr =
1123 TCollection_AsciiString ("#define STACK_SIZE ") + TCollection_AsciiString (myTraversalStackSize);
265d4508 1124
fc73a202 1125 myRaytraceShaderSource.SetPrefix (aStackSizeStr);
265d4508 1126
fc73a202 1127 myRaytraceShader = LoadShader (myRaytraceShaderSource, GL_FRAGMENT_SHADER);
265d4508 1128
fc73a202 1129 if (myRaytraceShader.IsNull())
1130 {
1131 aBasicVertShader->Release (myGlContext.operator->());
265d4508 1132
fc73a202 1133 return SafeFailBack ("Failed to set ray-trace fragment shader source");
1134 }
265d4508 1135
fc73a202 1136 myRaytraceProgram = new OpenGl_ShaderProgram;
5322131b 1137
fc73a202 1138 if (!myRaytraceProgram->Create (myGlContext))
1139 {
1140 aBasicVertShader->Release (myGlContext.operator->());
265d4508 1141
fc73a202 1142 return SafeFailBack ("Failed to create ray-trace shader program");
1143 }
265d4508 1144
fc73a202 1145 if (!myRaytraceProgram->AttachShader (myGlContext, aBasicVertShader)
1146 || !myRaytraceProgram->AttachShader (myGlContext, myRaytraceShader))
1147 {
1148 aBasicVertShader->Release (myGlContext.operator->());
265d4508 1149
fc73a202 1150 return SafeFailBack ("Failed to attach ray-trace shader objects");
1151 }
265d4508 1152
fc73a202 1153 if (!myRaytraceProgram->Link (myGlContext))
1154 {
1155 TCollection_AsciiString aLinkLog;
e276548b 1156
fc73a202 1157 if (myRaytraceProgram->FetchInfoLog (myGlContext, aLinkLog))
1158 {
1159 #ifdef RAY_TRACE_PRINT_INFO
1160 std::cout << aLinkLog << std::endl;
1161 #endif
1162 }
265d4508 1163
fc73a202 1164 return SafeFailBack ("Failed to link ray-trace shader program");
1165 }
1166 }
265d4508 1167
fc73a202 1168 {
1169 Handle(OpenGl_ShaderObject) aBasicVertShader = LoadShader (
1170 ShaderSource (aFolder + "/RaytraceBase.vs"), GL_VERTEX_SHADER);
265d4508 1171
fc73a202 1172 if (aBasicVertShader.IsNull())
1173 {
1174 return SafeFailBack ("Failed to set vertex shader source");
1175 }
265d4508 1176
fc73a202 1177 TCollection_AsciiString aFiles[] = { aFolder + "/RaytraceBase.fs", aFolder + "/RaytraceSmooth.fs" };
265d4508 1178
fc73a202 1179 myPostFSAAShaderSource.Load (aFiles, 2);
265d4508 1180
fc73a202 1181 TCollection_AsciiString aStackSizeStr =
1182 TCollection_AsciiString ("#define STACK_SIZE ") + TCollection_AsciiString (myTraversalStackSize);
265d4508 1183
fc73a202 1184 myPostFSAAShaderSource.SetPrefix (aStackSizeStr);
1185
1186 myPostFSAAShader = LoadShader (myPostFSAAShaderSource, GL_FRAGMENT_SHADER);
265d4508 1187
fc73a202 1188 if (myPostFSAAShader.IsNull())
1189 {
1190 aBasicVertShader->Release (myGlContext.operator->());
265d4508 1191
fc73a202 1192 return SafeFailBack ("Failed to set FSAA fragment shader source");
1193 }
265d4508 1194
fc73a202 1195 myPostFSAAProgram = new OpenGl_ShaderProgram;
265d4508 1196
fc73a202 1197 if (!myPostFSAAProgram->Create (myGlContext))
1198 {
1199 aBasicVertShader->Release (myGlContext.operator->());
265d4508 1200
fc73a202 1201 return SafeFailBack ("Failed to create FSAA shader program");
1202 }
1203
1204 if (!myPostFSAAProgram->AttachShader (myGlContext, aBasicVertShader)
1205 || !myPostFSAAProgram->AttachShader (myGlContext, myPostFSAAShader))
1206 {
1207 aBasicVertShader->Release (myGlContext.operator->());
1208
1209 return SafeFailBack ("Failed to attach FSAA shader objects");
1210 }
1211
1212 if (!myPostFSAAProgram->Link (myGlContext))
1213 {
1214 TCollection_AsciiString aLinkLog;
1215
1216 if (myPostFSAAProgram->FetchInfoLog (myGlContext, aLinkLog))
1217 {
1218 #ifdef RAY_TRACE_PRINT_INFO
1219 std::cout << aLinkLog << std::endl;
1220 #endif
1221 }
1222
1223 return SafeFailBack ("Failed to link FSAA shader program");
1224 }
e276548b 1225 }
fc73a202 1226 }
e276548b 1227
fc73a202 1228 if (myComputeInitStatus == OpenGl_RT_NONE || aToRebuildShaders)
1229 {
1230 for (Standard_Integer anIndex = 0; anIndex < 2; ++anIndex)
e276548b 1231 {
fc73a202 1232 Handle(OpenGl_ShaderProgram)& aShaderProgram =
1233 (anIndex == 0) ? myRaytraceProgram : myPostFSAAProgram;
1234
1235 aShaderProgram->Bind (myGlContext);
1236
1237 aShaderProgram->SetSampler (myGlContext,
1238 "uSceneMinPointTexture", OpenGl_RT_SceneMinPointTexture);
1239 aShaderProgram->SetSampler (myGlContext,
1240 "uSceneMaxPointTexture", OpenGl_RT_SceneMaxPointTexture);
1241 aShaderProgram->SetSampler (myGlContext,
1242 "uSceneNodeInfoTexture", OpenGl_RT_SceneNodeInfoTexture);
1243 aShaderProgram->SetSampler (myGlContext,
1244 "uObjectMinPointTexture", OpenGl_RT_ObjectMinPointTexture);
1245 aShaderProgram->SetSampler (myGlContext,
1246 "uObjectMaxPointTexture", OpenGl_RT_ObjectMaxPointTexture);
1247 aShaderProgram->SetSampler (myGlContext,
1248 "uObjectNodeInfoTexture", OpenGl_RT_ObjectNodeInfoTexture);
1249 aShaderProgram->SetSampler (myGlContext,
1250 "uGeometryVertexTexture", OpenGl_RT_GeometryVertexTexture);
1251 aShaderProgram->SetSampler (myGlContext,
1252 "uGeometryNormalTexture", OpenGl_RT_GeometryNormalTexture);
1253 aShaderProgram->SetSampler (myGlContext,
1254 "uGeometryTriangTexture", OpenGl_RT_GeometryTriangTexture);
1255 aShaderProgram->SetSampler (myGlContext,
1256 "uRaytraceMaterialTexture", OpenGl_RT_RaytraceMaterialTexture);
1257 aShaderProgram->SetSampler (myGlContext,
1258 "uRaytraceLightSrcTexture", OpenGl_RT_RaytraceLightSrcTexture);
1259
1260 if (anIndex == 1)
1261 {
1262 aShaderProgram->SetSampler (myGlContext,
1263 "uFSAAInputTexture", OpenGl_RT_FSAAInputTexture);
1264 }
265d4508 1265
fc73a202 1266 myUniformLocations[anIndex][OpenGl_RT_aPosition] =
1267 aShaderProgram->GetAttributeLocation (myGlContext, "aPosition");
1268
1269 myUniformLocations[anIndex][OpenGl_RT_uOriginLB] =
1270 aShaderProgram->GetUniformLocation (myGlContext, "uOriginLB");
1271 myUniformLocations[anIndex][OpenGl_RT_uOriginRB] =
1272 aShaderProgram->GetUniformLocation (myGlContext, "uOriginRB");
1273 myUniformLocations[anIndex][OpenGl_RT_uOriginLT] =
1274 aShaderProgram->GetUniformLocation (myGlContext, "uOriginLT");
1275 myUniformLocations[anIndex][OpenGl_RT_uOriginRT] =
1276 aShaderProgram->GetUniformLocation (myGlContext, "uOriginRT");
1277 myUniformLocations[anIndex][OpenGl_RT_uDirectLB] =
1278 aShaderProgram->GetUniformLocation (myGlContext, "uDirectLB");
1279 myUniformLocations[anIndex][OpenGl_RT_uDirectRB] =
1280 aShaderProgram->GetUniformLocation (myGlContext, "uDirectRB");
1281 myUniformLocations[anIndex][OpenGl_RT_uDirectLT] =
1282 aShaderProgram->GetUniformLocation (myGlContext, "uDirectLT");
1283 myUniformLocations[anIndex][OpenGl_RT_uDirectRT] =
1284 aShaderProgram->GetUniformLocation (myGlContext, "uDirectRT");
1285
1286 myUniformLocations[anIndex][OpenGl_RT_uLightCount] =
1287 aShaderProgram->GetUniformLocation (myGlContext, "uLightCount");
1288 myUniformLocations[anIndex][OpenGl_RT_uLightAmbnt] =
1289 aShaderProgram->GetUniformLocation (myGlContext, "uGlobalAmbient");
1290
1291 myUniformLocations[anIndex][OpenGl_RT_uSceneRad] =
1292 aShaderProgram->GetUniformLocation (myGlContext, "uSceneRadius");
1293 myUniformLocations[anIndex][OpenGl_RT_uSceneEps] =
1294 aShaderProgram->GetUniformLocation (myGlContext, "uSceneEpsilon");
1295
1296 myUniformLocations[anIndex][OpenGl_RT_uShadEnabled] =
1297 aShaderProgram->GetUniformLocation (myGlContext, "uShadowsEnable");
1298 myUniformLocations[anIndex][OpenGl_RT_uReflEnabled] =
1299 aShaderProgram->GetUniformLocation (myGlContext, "uReflectionsEnable");
1300
1301 myUniformLocations[anIndex][OpenGl_RT_uOffsetX] =
1302 aShaderProgram->GetUniformLocation (myGlContext, "uOffsetX");
1303 myUniformLocations[anIndex][OpenGl_RT_uOffsetY] =
1304 aShaderProgram->GetUniformLocation (myGlContext, "uOffsetY");
1305 myUniformLocations[anIndex][OpenGl_RT_uSamples] =
1306 aShaderProgram->GetUniformLocation (myGlContext, "uSamples");
1307 }
265d4508 1308
fc73a202 1309 OpenGl_ShaderProgram::Unbind (myGlContext);
1310 }
265d4508 1311
fc73a202 1312 if (myComputeInitStatus != OpenGl_RT_NONE)
1313 {
1314 return myComputeInitStatus == OpenGl_RT_INIT;
1315 }
265d4508 1316
fc73a202 1317 if (myRaytraceFBO1.IsNull())
1318 {
1319 myRaytraceFBO1 = new OpenGl_FrameBuffer;
1320 }
265d4508 1321
fc73a202 1322 if (myRaytraceFBO2.IsNull())
1323 {
1324 myRaytraceFBO2 = new OpenGl_FrameBuffer;
1325 }
265d4508 1326
fc73a202 1327 const GLfloat aVertices[] = { -1.f, -1.f, 0.f,
1328 -1.f, 1.f, 0.f,
1329 1.f, 1.f, 0.f,
1330 1.f, 1.f, 0.f,
1331 1.f, -1.f, 0.f,
1332 -1.f, -1.f, 0.f };
265d4508 1333
fc73a202 1334 myRaytraceScreenQuad.Init (myGlContext, 3, 6, aVertices);
265d4508 1335
fc73a202 1336 myComputeInitStatus = OpenGl_RT_INIT; // initialized in normal way
1337
1338 return Standard_True;
1339}
265d4508 1340
fc73a202 1341// =======================================================================
1342// function : NullifyResource
1343// purpose :
1344// =======================================================================
1345inline void NullifyResource (const Handle(OpenGl_Context)& theContext,
1346 Handle(OpenGl_Resource)& theResource)
1347{
1348 if (!theResource.IsNull())
1349 {
1350 theResource->Release (theContext.operator->());
1351 theResource.Nullify();
1352 }
1353}
265d4508 1354
fc73a202 1355// =======================================================================
1356// function : ReleaseRaytraceResources
1357// purpose : Releases OpenGL/GLSL shader programs
1358// =======================================================================
1359void OpenGl_Workspace::ReleaseRaytraceResources()
1360{
1361 NullifyResource (myGlContext, myRaytraceFBO1);
1362 NullifyResource (myGlContext, myRaytraceFBO2);
265d4508 1363
fc73a202 1364 NullifyResource (myGlContext, myRaytraceShader);
1365 NullifyResource (myGlContext, myPostFSAAShader);
265d4508 1366
fc73a202 1367 NullifyResource (myGlContext, myRaytraceProgram);
1368 NullifyResource (myGlContext, myPostFSAAProgram);
265d4508 1369
fc73a202 1370 NullifyResource (myGlContext, mySceneNodeInfoTexture);
1371 NullifyResource (myGlContext, mySceneMinPointTexture);
1372 NullifyResource (myGlContext, mySceneMaxPointTexture);
265d4508 1373
fc73a202 1374 NullifyResource (myGlContext, myObjectNodeInfoTexture);
1375 NullifyResource (myGlContext, myObjectMinPointTexture);
1376 NullifyResource (myGlContext, myObjectMaxPointTexture);
265d4508 1377
fc73a202 1378 NullifyResource (myGlContext, myGeometryVertexTexture);
1379 NullifyResource (myGlContext, myGeometryNormalTexture);
1380 NullifyResource (myGlContext, myGeometryTriangTexture);
265d4508 1381
fc73a202 1382 NullifyResource (myGlContext, myRaytraceLightSrcTexture);
1383 NullifyResource (myGlContext, myRaytraceMaterialTexture);
265d4508 1384
fc73a202 1385 if (myRaytraceScreenQuad.IsValid())
1386 myRaytraceScreenQuad.Release (myGlContext.operator->());
1387}
265d4508 1388
fc73a202 1389// =======================================================================
1390// function : UploadRaytraceData
1391// purpose : Uploads ray-trace data to the GPU
1392// =======================================================================
1393Standard_Boolean OpenGl_Workspace::UploadRaytraceData()
1394{
1395 if (!myGlContext->IsGlGreaterEqual (3, 1))
1396 {
265d4508 1397#ifdef RAY_TRACE_PRINT_INFO
fc73a202 1398 std::cout << "Error: OpenGL version is less than 3.1" << std::endl;
265d4508 1399#endif
fc73a202 1400 return Standard_False;
e276548b 1401 }
1402
265d4508 1403 /////////////////////////////////////////////////////////////////////////////
fc73a202 1404 // Create OpenGL texture buffers
265d4508 1405
fc73a202 1406 if (mySceneNodeInfoTexture.IsNull()) // create hight-level BVH buffers
e276548b 1407 {
fc73a202 1408 mySceneNodeInfoTexture = new OpenGl_TextureBufferArb;
1409 mySceneMinPointTexture = new OpenGl_TextureBufferArb;
1410 mySceneMaxPointTexture = new OpenGl_TextureBufferArb;
265d4508 1411
fc73a202 1412 if (!mySceneNodeInfoTexture->Create (myGlContext)
1413 || !mySceneMinPointTexture->Create (myGlContext)
1414 || !mySceneMaxPointTexture->Create (myGlContext))
e276548b 1415 {
fc73a202 1416#ifdef RAY_TRACE_PRINT_INFO
1417 std::cout << "Error: Failed to create buffers for high-level scene BVH" << std::endl;
1418#endif
e276548b 1419 return Standard_False;
1420 }
1421 }
1422
fc73a202 1423 if (myObjectNodeInfoTexture.IsNull()) // create bottom-level BVH buffers
265d4508 1424 {
fc73a202 1425 myObjectNodeInfoTexture = new OpenGl_TextureBufferArb;
1426 myObjectMinPointTexture = new OpenGl_TextureBufferArb;
1427 myObjectMaxPointTexture = new OpenGl_TextureBufferArb;
e276548b 1428
fc73a202 1429 if (!myObjectNodeInfoTexture->Create (myGlContext)
1430 || !myObjectMinPointTexture->Create (myGlContext)
1431 || !myObjectMaxPointTexture->Create (myGlContext))
1432 {
e276548b 1433#ifdef RAY_TRACE_PRINT_INFO
fc73a202 1434 std::cout << "Error: Failed to create buffers for bottom-level scene BVH" << std::endl;
1435#endif
1436 return Standard_False;
1437 }
1438 }
5322131b 1439
fc73a202 1440 if (myGeometryVertexTexture.IsNull()) // create geometry buffers
265d4508 1441 {
fc73a202 1442 myGeometryVertexTexture = new OpenGl_TextureBufferArb;
1443 myGeometryNormalTexture = new OpenGl_TextureBufferArb;
1444 myGeometryTriangTexture = new OpenGl_TextureBufferArb;
e276548b 1445
fc73a202 1446 if (!myGeometryVertexTexture->Create (myGlContext)
1447 || !myGeometryNormalTexture->Create (myGlContext)
1448 || !myGeometryTriangTexture->Create (myGlContext))
1449 {
1450#ifdef RAY_TRACE_PRINT_INFO
1451 std::cout << "Error: Failed to create buffers for triangulation data" << std::endl;
1452#endif
1453 return Standard_False;
1454 }
265d4508 1455 }
5322131b 1456
fc73a202 1457 if (myRaytraceMaterialTexture.IsNull()) // create material buffer
1458 {
1459 myRaytraceMaterialTexture = new OpenGl_TextureBufferArb;
e276548b 1460
fc73a202 1461 if (!myRaytraceMaterialTexture->Create (myGlContext))
1462 {
1463#ifdef RAY_TRACE_PRINT_INFO
1464 std::cout << "Error: Failed to create buffers for material data" << std::endl;
e276548b 1465#endif
fc73a202 1466 return Standard_False;
1467 }
1468 }
e276548b 1469
fc73a202 1470 /////////////////////////////////////////////////////////////////////////////
1471 // Write OpenGL texture buffers
e276548b 1472
fc73a202 1473 const NCollection_Handle<BVH_Tree<Standard_ShortReal, 4> >& aBVH = myRaytraceGeometry.BVH();
e276548b 1474
fc73a202 1475 bool aResult = true;
fc73a202 1476 if (!aBVH->NodeInfoBuffer().empty())
1477 {
5cff985a 1478 aResult &= mySceneNodeInfoTexture->Init (myGlContext, 4, GLsizei (aBVH->NodeInfoBuffer().size()),
1479 reinterpret_cast<const GLuint*> (&aBVH->NodeInfoBuffer().front()));
1480 aResult &= mySceneMinPointTexture->Init (myGlContext, 4, GLsizei (aBVH->MinPointBuffer().size()),
1481 reinterpret_cast<const GLfloat*> (&aBVH->MinPointBuffer().front()));
1482 aResult &= mySceneMaxPointTexture->Init (myGlContext, 4, GLsizei (aBVH->MaxPointBuffer().size()),
1483 reinterpret_cast<const GLfloat*> (&aBVH->MaxPointBuffer().front()));
fc73a202 1484 }
fc73a202 1485 if (!aResult)
1486 {
1487#ifdef RAY_TRACE_PRINT_INFO
1488 std::cout << "Error: Failed to upload buffers for high-level scene BVH" << std::endl;
1489#endif
e276548b 1490 return Standard_False;
1491 }
1492
fc73a202 1493 Standard_Size aTotalVerticesNb = 0;
1494 Standard_Size aTotalElementsNb = 0;
1495 Standard_Size aTotalBVHNodesNb = 0;
265d4508 1496
fc73a202 1497 for (Standard_Integer anElemIndex = 0; anElemIndex < myRaytraceGeometry.Size(); ++anElemIndex)
1498 {
1499 OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> (
1500 myRaytraceGeometry.Objects().ChangeValue (anElemIndex).operator->());
265d4508 1501
fc73a202 1502 Standard_ASSERT_RETURN (aTriangleSet != NULL,
1503 "Error: Failed to get triangulation of OpenGL element", Standard_False);
1504
1505 aTotalVerticesNb += aTriangleSet->Vertices.size();
1506 aTotalElementsNb += aTriangleSet->Elements.size();
e276548b 1507
fc73a202 1508 Standard_ASSERT_RETURN (!aTriangleSet->BVH().IsNull(),
1509 "Error: Failed to get bottom-level BVH of OpenGL element", Standard_False);
265d4508 1510
fc73a202 1511 aTotalBVHNodesNb += aTriangleSet->BVH()->NodeInfoBuffer().size();
1512 }
e276548b 1513
fc73a202 1514 if (aTotalBVHNodesNb != 0)
e276548b 1515 {
5cff985a 1516 aResult &= myObjectNodeInfoTexture->Init (myGlContext, 4, GLsizei (aTotalBVHNodesNb), static_cast<const GLuint*> (NULL));
1517 aResult &= myObjectMinPointTexture->Init (myGlContext, 4, GLsizei (aTotalBVHNodesNb), static_cast<const GLfloat*> (NULL));
1518 aResult &= myObjectMaxPointTexture->Init (myGlContext, 4, GLsizei (aTotalBVHNodesNb), static_cast<const GLfloat*> (NULL));
fc73a202 1519 }
265d4508 1520
fc73a202 1521 if (!aResult)
1522 {
1523#ifdef RAY_TRACE_PRINT_INFO
1524 std::cout << "Error: Failed to upload buffers for bottom-level scene BVH" << std::endl;
1525#endif
1526 return Standard_False;
1527 }
e276548b 1528
fc73a202 1529 if (aTotalElementsNb != 0)
1530 {
5cff985a 1531 aResult &= myGeometryTriangTexture->Init (myGlContext, 4, GLsizei (aTotalElementsNb), static_cast<const GLuint*> (NULL));
fc73a202 1532 }
265d4508 1533
fc73a202 1534 if (aTotalVerticesNb != 0)
1535 {
5cff985a 1536 aResult &= myGeometryVertexTexture->Init (myGlContext, 4, GLsizei (aTotalVerticesNb), static_cast<const GLfloat*> (NULL));
1537 aResult &= myGeometryNormalTexture->Init (myGlContext, 4, GLsizei (aTotalVerticesNb), static_cast<const GLfloat*> (NULL));
fc73a202 1538 }
265d4508 1539
fc73a202 1540 if (!aResult)
1541 {
1542#ifdef RAY_TRACE_PRINT_INFO
1543 std::cout << "Error: Failed to upload buffers for scene geometry" << std::endl;
1544#endif
1545 return Standard_False;
1546 }
265d4508 1547
fc73a202 1548 for (Standard_Integer aNodeIdx = 0; aNodeIdx < aBVH->Length(); ++aNodeIdx)
1549 {
1550 if (!aBVH->IsOuter (aNodeIdx))
1551 continue;
265d4508 1552
fc73a202 1553 OpenGl_TriangleSet* aTriangleSet = myRaytraceGeometry.TriangleSet (aNodeIdx);
e276548b 1554
fc73a202 1555 Standard_ASSERT_RETURN (aTriangleSet != NULL,
1556 "Error: Failed to get triangulation of OpenGL element", Standard_False);
e276548b 1557
fc73a202 1558 const Standard_Integer aBVHOffset = myRaytraceGeometry.AccelerationOffset (aNodeIdx);
e276548b 1559
fc73a202 1560 Standard_ASSERT_RETURN (aBVHOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET,
1561 "Error: Failed to get offset for bottom-level BVH", Standard_False);
e276548b 1562
fc73a202 1563 const size_t aBVHBuffserSize = aTriangleSet->BVH()->NodeInfoBuffer().size();
265d4508 1564
fc73a202 1565 if (aBVHBuffserSize != 0)
1566 {
5cff985a 1567 aResult &= myObjectNodeInfoTexture->SubData (myGlContext, aBVHOffset, GLsizei (aBVHBuffserSize),
1568 reinterpret_cast<const GLuint*> (&aTriangleSet->BVH()->NodeInfoBuffer().front()));
1569 aResult &= myObjectMinPointTexture->SubData (myGlContext, aBVHOffset, GLsizei (aBVHBuffserSize),
1570 reinterpret_cast<const GLfloat*> (&aTriangleSet->BVH()->MinPointBuffer().front()));
1571 aResult &= myObjectMaxPointTexture->SubData (myGlContext, aBVHOffset, GLsizei (aBVHBuffserSize),
1572 reinterpret_cast<const GLfloat*> (&aTriangleSet->BVH()->MaxPointBuffer().front()));
fc73a202 1573 if (!aResult)
e276548b 1574 {
fc73a202 1575#ifdef RAY_TRACE_PRINT_INFO
1576 std::cout << "Error: Failed to upload buffers for bottom-level scene BVHs" << std::endl;
1577#endif
e276548b 1578 return Standard_False;
1579 }
1580 }
1581
fc73a202 1582 const Standard_Integer aVerticesOffset = myRaytraceGeometry.VerticesOffset (aNodeIdx);
265d4508 1583
fc73a202 1584 Standard_ASSERT_RETURN (aVerticesOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET,
1585 "Error: Failed to get offset for triangulation vertices of OpenGL element", Standard_False);
265d4508 1586
fc73a202 1587 if (!aTriangleSet->Vertices.empty())
1588 {
5cff985a 1589 aResult &= myGeometryNormalTexture->SubData (myGlContext, aVerticesOffset, GLsizei (aTriangleSet->Normals.size()),
1590 reinterpret_cast<const GLfloat*> (&aTriangleSet->Normals.front()));
1591 aResult &= myGeometryVertexTexture->SubData (myGlContext, aVerticesOffset, GLsizei (aTriangleSet->Vertices.size()),
1592 reinterpret_cast<const GLfloat*> (&aTriangleSet->Vertices.front()));
fc73a202 1593 }
e276548b 1594
fc73a202 1595 const Standard_Integer anElementsOffset = myRaytraceGeometry.ElementsOffset (aNodeIdx);
265d4508 1596
fc73a202 1597 Standard_ASSERT_RETURN (anElementsOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET,
1598 "Error: Failed to get offset for triangulation elements of OpenGL element", Standard_False);
e276548b 1599
fc73a202 1600 if (!aTriangleSet->Elements.empty())
e276548b 1601 {
5cff985a 1602 aResult &= myGeometryTriangTexture->SubData (myGlContext, anElementsOffset, GLsizei (aTriangleSet->Elements.size()),
1603 reinterpret_cast<const GLuint*> (&aTriangleSet->Elements.front()));
e276548b 1604 }
265d4508 1605
fc73a202 1606 if (!aResult)
1607 {
1608#ifdef RAY_TRACE_PRINT_INFO
1609 std::cout << "Error: Failed to upload triangulation buffers for OpenGL element" << std::endl;
e276548b 1610#endif
fc73a202 1611 return Standard_False;
1612 }
e276548b 1613 }
e276548b 1614
fc73a202 1615 if (myRaytraceGeometry.Materials.size() != 0)
1616 {
1617 const GLfloat* aDataPtr = myRaytraceGeometry.Materials.front().Packed();
5cff985a 1618 aResult &= myRaytraceMaterialTexture->Init (myGlContext, 4, GLsizei (myRaytraceGeometry.Materials.size() * 7), aDataPtr);
fc73a202 1619 if (!aResult)
1620 {
1621#ifdef RAY_TRACE_PRINT_INFO
1622 std::cout << "Error: Failed to upload material buffer" << std::endl;
1623#endif
1624 return Standard_False;
1625 }
1626 }
e276548b 1627
fc73a202 1628 myIsRaytraceDataValid = myRaytraceGeometry.Objects().Size() != 0;
e276548b 1629
fc73a202 1630#ifdef RAY_TRACE_PRINT_INFO
e276548b 1631
fc73a202 1632 Standard_ShortReal aMemUsed = 0.f;
e276548b 1633
fc73a202 1634 for (Standard_Integer anElemIdx = 0; anElemIdx < myRaytraceGeometry.Size(); ++anElemIdx)
1635 {
1636 OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> (
1637 myRaytraceGeometry.Objects().ChangeValue (anElemIdx).operator->());
e276548b 1638
fc73a202 1639 aMemUsed += static_cast<Standard_ShortReal> (
1640 aTriangleSet->Vertices.size() * sizeof (BVH_Vec4f));
1641 aMemUsed += static_cast<Standard_ShortReal> (
1642 aTriangleSet->Normals.size() * sizeof (BVH_Vec4f));
1643 aMemUsed += static_cast<Standard_ShortReal> (
1644 aTriangleSet->Elements.size() * sizeof (BVH_Vec4i));
e276548b 1645
fc73a202 1646 aMemUsed += static_cast<Standard_ShortReal> (
1647 aTriangleSet->BVH()->NodeInfoBuffer().size() * sizeof (BVH_Vec4i));
1648 aMemUsed += static_cast<Standard_ShortReal> (
1649 aTriangleSet->BVH()->MinPointBuffer().size() * sizeof (BVH_Vec4f));
1650 aMemUsed += static_cast<Standard_ShortReal> (
1651 aTriangleSet->BVH()->MaxPointBuffer().size() * sizeof (BVH_Vec4f));
1652 }
e276548b 1653
fc73a202 1654 aMemUsed += static_cast<Standard_ShortReal> (
1655 myRaytraceGeometry.BVH()->NodeInfoBuffer().size() * sizeof (BVH_Vec4i));
1656 aMemUsed += static_cast<Standard_ShortReal> (
1657 myRaytraceGeometry.BVH()->MinPointBuffer().size() * sizeof (BVH_Vec4f));
1658 aMemUsed += static_cast<Standard_ShortReal> (
1659 myRaytraceGeometry.BVH()->MaxPointBuffer().size() * sizeof (BVH_Vec4f));
e276548b 1660
fc73a202 1661 std::cout << "GPU Memory Used (MB): ~" << aMemUsed / 1048576 << std::endl;
e276548b 1662
fc73a202 1663#endif
e276548b 1664
fc73a202 1665 return aResult;
1666}
e276548b 1667
fc73a202 1668// =======================================================================
1669// function : ResizeRaytraceBuffers
1670// purpose : Resizes OpenGL frame buffers
1671// =======================================================================
1672Standard_Boolean OpenGl_Workspace::ResizeRaytraceBuffers (const Standard_Integer theSizeX,
1673 const Standard_Integer theSizeY)
1674{
1675 if (myRaytraceFBO1->GetVPSizeX() != theSizeX
1676 || myRaytraceFBO1->GetVPSizeY() != theSizeY)
1677 {
1678 myRaytraceFBO1->Init (myGlContext, theSizeX, theSizeY);
1679 myRaytraceFBO2->Init (myGlContext, theSizeX, theSizeY);
1680 }
e276548b 1681
fc73a202 1682 return Standard_True;
e276548b 1683}
1684
1685// =======================================================================
fc73a202 1686// function : UpdateCamera
1687// purpose : Generates viewing rays for corners of screen quad
e276548b 1688// =======================================================================
fc73a202 1689void OpenGl_Workspace::UpdateCamera (const NCollection_Mat4<GLdouble>& theOrientation,
1690 const NCollection_Mat4<GLdouble>& theViewMapping,
1691 OpenGl_Vec3 theOrigins[4],
1692 OpenGl_Vec3 theDirects[4])
e276548b 1693{
fc73a202 1694 NCollection_Mat4<GLdouble> aInvModelProj;
1695
1696 // compute invserse model-view-projection matrix
1697 (theViewMapping * theOrientation).Inverted (aInvModelProj);
1698
68333c8f 1699 Standard_Integer aOriginIndex = 0;
1700 Standard_Integer aDirectIndex = 0;
e276548b 1701
fc73a202 1702 for (Standard_Integer aY = -1; aY <= 1; aY += 2)
e276548b 1703 {
fc73a202 1704 for (Standard_Integer aX = -1; aX <= 1; aX += 2)
e276548b 1705 {
fc73a202 1706 OpenGl_Vec4d aOrigin (GLdouble(aX),
1707 GLdouble(aY),
1708 -1.0,
1709 1.0);
1710
1711 aOrigin = aInvModelProj * aOrigin;
e276548b 1712
b5ac8292 1713 aOrigin.x() = aOrigin.x() / aOrigin.w();
1714 aOrigin.y() = aOrigin.y() / aOrigin.w();
1715 aOrigin.z() = aOrigin.z() / aOrigin.w();
e276548b 1716
fc73a202 1717 OpenGl_Vec4d aDirect (GLdouble(aX),
1718 GLdouble(aY),
1719 1.0,
1720 1.0);
1721
1722 aDirect = aInvModelProj * aDirect;
e276548b 1723
b5ac8292 1724 aDirect.x() = aDirect.x() / aDirect.w();
1725 aDirect.y() = aDirect.y() / aDirect.w();
1726 aDirect.z() = aDirect.z() / aDirect.w();
b5ac8292 1727
1728 aDirect = aDirect - aOrigin;
e276548b 1729
fc73a202 1730 GLdouble aInvLen = 1.0 / sqrt (aDirect.x() * aDirect.x() +
e276548b 1731 aDirect.y() * aDirect.y() +
1732 aDirect.z() * aDirect.z());
1733
fc73a202 1734 theOrigins[aOriginIndex++] = OpenGl_Vec3 (static_cast<GLfloat> (aOrigin.x()),
1735 static_cast<GLfloat> (aOrigin.y()),
1736 static_cast<GLfloat> (aOrigin.z()));
1737
1738 theDirects[aDirectIndex++] = OpenGl_Vec3 (static_cast<GLfloat> (aDirect.x() * aInvLen),
1739 static_cast<GLfloat> (aDirect.y() * aInvLen),
1740 static_cast<GLfloat> (aDirect.z() * aInvLen));
1741 }
1742 }
1743}
1744
1745// =======================================================================
1746// function : RunRaytraceShaders
1747// purpose : Runs ray-tracing shader programs
1748// =======================================================================
1749Standard_Boolean OpenGl_Workspace::RunRaytraceShaders (const Graphic3d_CView& theCView,
1750 const Standard_Integer theSizeX,
1751 const Standard_Integer theSizeY,
1752 const OpenGl_Vec3 theOrigins[4],
1753 const OpenGl_Vec3 theDirects[4],
1754 OpenGl_FrameBuffer* theFrameBuffer)
1755{
1756 mySceneMinPointTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_SceneMinPointTexture);
1757 mySceneMaxPointTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_SceneMaxPointTexture);
1758 mySceneNodeInfoTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_SceneNodeInfoTexture);
1759 myObjectMinPointTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_ObjectMinPointTexture);
1760 myObjectMaxPointTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_ObjectMaxPointTexture);
1761 myObjectNodeInfoTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_ObjectNodeInfoTexture);
1762 myGeometryVertexTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryVertexTexture);
1763 myGeometryNormalTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryNormalTexture);
1764 myGeometryTriangTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryTriangTexture);
1765 myRaytraceMaterialTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceMaterialTexture);
1766 myRaytraceLightSrcTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceLightSrcTexture);
1767
1768 if (theCView.IsAntialiasingEnabled) // render source image to FBO
1769 {
1770 myRaytraceFBO1->BindBuffer (myGlContext);
1771
1772 glDisable (GL_BLEND);
1773 }
1774
1775 myRaytraceProgram->Bind (myGlContext);
1776
1777 Standard_Integer aLightSourceBufferSize =
1778 static_cast<Standard_Integer> (myRaytraceGeometry.Sources.size());
1779
1780 myRaytraceProgram->SetUniform (myGlContext,
1781 myUniformLocations[0][OpenGl_RT_uOriginLB], theOrigins[0]);
1782 myRaytraceProgram->SetUniform (myGlContext,
1783 myUniformLocations[0][OpenGl_RT_uOriginRB], theOrigins[1]);
1784 myRaytraceProgram->SetUniform (myGlContext,
1785 myUniformLocations[0][OpenGl_RT_uOriginLT], theOrigins[2]);
1786 myRaytraceProgram->SetUniform (myGlContext,
1787 myUniformLocations[0][OpenGl_RT_uOriginRT], theOrigins[3]);
1788 myRaytraceProgram->SetUniform (myGlContext,
1789 myUniformLocations[0][OpenGl_RT_uDirectLB], theDirects[0]);
1790 myRaytraceProgram->SetUniform (myGlContext,
1791 myUniformLocations[0][OpenGl_RT_uDirectRB], theDirects[1]);
1792 myRaytraceProgram->SetUniform (myGlContext,
1793 myUniformLocations[0][OpenGl_RT_uDirectLT], theDirects[2]);
1794 myRaytraceProgram->SetUniform (myGlContext,
1795 myUniformLocations[0][OpenGl_RT_uDirectRT], theDirects[3]);
1796 myRaytraceProgram->SetUniform (myGlContext,
1797 myUniformLocations[0][OpenGl_RT_uSceneRad], myRaytraceSceneRadius);
1798 myRaytraceProgram->SetUniform (myGlContext,
1799 myUniformLocations[0][OpenGl_RT_uSceneEps], myRaytraceSceneEpsilon);
1800 myRaytraceProgram->SetUniform (myGlContext,
1801 myUniformLocations[0][OpenGl_RT_uLightCount], aLightSourceBufferSize);
1802 myRaytraceProgram->SetUniform (myGlContext,
1803 myUniformLocations[0][OpenGl_RT_uLightAmbnt], myRaytraceGeometry.Ambient);
1804 myRaytraceProgram->SetUniform (myGlContext,
1805 myUniformLocations[0][OpenGl_RT_uShadEnabled], theCView.IsShadowsEnabled);
1806 myRaytraceProgram->SetUniform (myGlContext,
1807 myUniformLocations[0][OpenGl_RT_uReflEnabled], theCView.IsReflectionsEnabled);
1808
1809 myGlContext->core20fwd->glEnableVertexAttribArray (
1810 myUniformLocations[0][OpenGl_RT_aPosition]);
1811 {
1812 myGlContext->core20fwd->glVertexAttribPointer (
1813 myUniformLocations[0][OpenGl_RT_aPosition], 3, GL_FLOAT, GL_FALSE, 0, NULL);
1814
1815 myGlContext->core15fwd->glDrawArrays (GL_TRIANGLES, 0, 6);
1816 }
1817 myGlContext->core20fwd->glDisableVertexAttribArray (
1818 myUniformLocations[0][OpenGl_RT_aPosition]);
1819
1820 if (!theCView.IsAntialiasingEnabled)
1821 {
1822 myRaytraceProgram->Unbind (myGlContext);
1823
1824 return Standard_True;
1825 }
1826
1827 myGlContext->core20fwd->glActiveTexture (
1828 GL_TEXTURE0 + OpenGl_RT_FSAAInputTexture); // texture unit for FBO texture
1829
1830 myRaytraceFBO1->BindTexture (myGlContext);
1831
1832 myPostFSAAProgram->Bind (myGlContext);
1833
1834 myPostFSAAProgram->SetUniform (myGlContext,
1835 myUniformLocations[1][OpenGl_RT_uOriginLB], theOrigins[0]);
1836 myPostFSAAProgram->SetUniform (myGlContext,
1837 myUniformLocations[1][OpenGl_RT_uOriginRB], theOrigins[1]);
1838 myPostFSAAProgram->SetUniform (myGlContext,
1839 myUniformLocations[1][OpenGl_RT_uOriginLT], theOrigins[2]);
1840 myPostFSAAProgram->SetUniform (myGlContext,
1841 myUniformLocations[1][OpenGl_RT_uOriginRT], theOrigins[3]);
1842 myPostFSAAProgram->SetUniform (myGlContext,
1843 myUniformLocations[1][OpenGl_RT_uDirectLB], theDirects[0]);
1844 myPostFSAAProgram->SetUniform (myGlContext,
1845 myUniformLocations[1][OpenGl_RT_uDirectRB], theDirects[1]);
1846 myPostFSAAProgram->SetUniform (myGlContext,
1847 myUniformLocations[1][OpenGl_RT_uDirectLT], theDirects[2]);
1848 myPostFSAAProgram->SetUniform (myGlContext,
1849 myUniformLocations[1][OpenGl_RT_uDirectRT], theDirects[3]);
1850 myPostFSAAProgram->SetUniform (myGlContext,
1851 myUniformLocations[1][OpenGl_RT_uSceneRad], myRaytraceSceneRadius);
1852 myPostFSAAProgram->SetUniform (myGlContext,
1853 myUniformLocations[1][OpenGl_RT_uSceneEps], myRaytraceSceneEpsilon);
1854 myPostFSAAProgram->SetUniform (myGlContext,
1855 myUniformLocations[1][OpenGl_RT_uLightCount], aLightSourceBufferSize);
1856 myPostFSAAProgram->SetUniform (myGlContext,
1857 myUniformLocations[1][OpenGl_RT_uLightAmbnt], myRaytraceGeometry.Ambient);
1858 myPostFSAAProgram->SetUniform (myGlContext,
1859 myUniformLocations[1][OpenGl_RT_uShadEnabled], theCView.IsShadowsEnabled);
1860 myPostFSAAProgram->SetUniform (myGlContext,
1861 myUniformLocations[1][OpenGl_RT_uReflEnabled], theCView.IsReflectionsEnabled);
1862
1863 const Standard_ShortReal aMaxOffset = 0.559017f;
1864 const Standard_ShortReal aMinOffset = 0.186339f;
1865
1866 myGlContext->core20fwd->glEnableVertexAttribArray (
1867 myUniformLocations[1][OpenGl_RT_aPosition]);
1868
1869 myGlContext->core20fwd->glVertexAttribPointer (
1870 myUniformLocations[1][OpenGl_RT_aPosition], 3, GL_FLOAT, GL_FALSE, 0, NULL);
1871
1872 // Perform multi-pass adaptive FSAA using ping-pong technique
1873 for (Standard_Integer anIt = 0; anIt < 4; ++anIt)
1874 {
1875 GLfloat aOffsetX = 1.f / theSizeX;
1876 GLfloat aOffsetY = 1.f / theSizeY;
1877
1878 if (anIt < 2)
1879 {
1880 aOffsetX *= anIt < 1 ? aMinOffset : -aMaxOffset;
1881 aOffsetY *= anIt < 1 ? aMaxOffset : aMinOffset;
1882 }
1883 else
1884 {
1885 aOffsetX *= anIt > 2 ? aMaxOffset : -aMinOffset;
1886 aOffsetY *= anIt > 2 ? -aMinOffset : -aMaxOffset;
1887 }
1888
1889 myPostFSAAProgram->SetUniform (myGlContext,
1890 myUniformLocations[1][OpenGl_RT_uSamples], anIt + 2);
1891 myPostFSAAProgram->SetUniform (myGlContext,
1892 myUniformLocations[1][OpenGl_RT_uOffsetX], aOffsetX);
1893 myPostFSAAProgram->SetUniform (myGlContext,
1894 myUniformLocations[1][OpenGl_RT_uOffsetY], aOffsetY);
1895
1896 Handle(OpenGl_FrameBuffer)& aFramebuffer = anIt % 2 ? myRaytraceFBO1 : myRaytraceFBO2;
1897
1898 if (anIt == 3) // disable FBO on last iteration
1899 {
1900 glEnable (GL_BLEND);
1901
1902 if (theFrameBuffer != NULL)
1903 theFrameBuffer->BindBuffer (myGlContext);
1904 }
1905 else
1906 {
1907 aFramebuffer->BindBuffer (myGlContext);
1908 }
1909
1910 myGlContext->core15fwd->glDrawArrays (GL_TRIANGLES, 0, 6);
e276548b 1911
fc73a202 1912 if (anIt != 3) // set input for the next pass
1913 {
1914 aFramebuffer->BindTexture (myGlContext);
1915 aFramebuffer->UnbindBuffer (myGlContext);
e276548b 1916 }
1917 }
fc73a202 1918
1919 myGlContext->core20fwd->glDisableVertexAttribArray (
1920 myUniformLocations[1][OpenGl_RT_aPosition]);
1921
1922 myPostFSAAProgram->Unbind (myGlContext);
1923
1924 return Standard_True;
e276548b 1925}
1926
1927// =======================================================================
1928// function : Raytrace
fc73a202 1929// purpose : Redraws the window using OpenGL/GLSL ray-tracing
e276548b 1930// =======================================================================
1931Standard_Boolean OpenGl_Workspace::Raytrace (const Graphic3d_CView& theCView,
68333c8f 1932 const Standard_Integer theSizeX,
1933 const Standard_Integer theSizeY,
fc73a202 1934 const Standard_Boolean theToSwap,
1935 OpenGl_FrameBuffer* theFrameBuffer)
e276548b 1936{
fc73a202 1937 if (!UpdateRaytraceGeometry (Standard_True))
e276548b 1938 return Standard_False;
1939
fc73a202 1940 if (!InitRaytraceResources())
e276548b 1941 return Standard_False;
1942
fc73a202 1943 if (!ResizeRaytraceBuffers (theSizeX, theSizeY))
e276548b 1944 return Standard_False;
1945
fc73a202 1946 if (!UpdateRaytraceEnvironmentMap())
e276548b 1947 return Standard_False;
1948
1949 // Get model-view and projection matrices
1950 TColStd_Array2OfReal theOrientation (0, 3, 0, 3);
1951 TColStd_Array2OfReal theViewMapping (0, 3, 0, 3);
1952
b5ac8292 1953 myView->GetMatrices (theOrientation, theViewMapping);
e276548b 1954
fc73a202 1955 NCollection_Mat4<GLdouble> aOrientationMatrix;
1956 NCollection_Mat4<GLdouble> aViewMappingMatrix;
e276548b 1957
68333c8f 1958 for (Standard_Integer j = 0; j < 4; ++j)
fc73a202 1959 {
68333c8f 1960 for (Standard_Integer i = 0; i < 4; ++i)
e276548b 1961 {
1962 aOrientationMatrix [4 * j + i] = theOrientation (i, j);
1963 aViewMappingMatrix [4 * j + i] = theViewMapping (i, j);
1964 }
fc73a202 1965 }
1966
1967 NCollection_Mat4<GLdouble> aInvOrientationMatrix;
1968 aOrientationMatrix.Inverted (aInvOrientationMatrix);
e276548b 1969
fc73a202 1970 if (!UpdateRaytraceLightSources (aInvOrientationMatrix))
e276548b 1971 return Standard_False;
1972
fc73a202 1973 OpenGl_Vec3 aOrigins[4];
1974 OpenGl_Vec3 aDirects[4];
e276548b 1975
fc73a202 1976 UpdateCamera (aOrientationMatrix,
1977 aViewMappingMatrix,
1978 aOrigins,
1979 aDirects);
e276548b 1980
1981 // Draw background
1982 glPushAttrib (GL_ENABLE_BIT |
1983 GL_CURRENT_BIT |
1984 GL_COLOR_BUFFER_BIT |
1985 GL_DEPTH_BUFFER_BIT);
1986
1987 glDisable (GL_DEPTH_TEST);
1988
1989 if (NamedStatus & OPENGL_NS_WHITEBACK)
1990 {
1991 glClearColor (1.0f, 1.0f, 1.0f, 1.0f);
1992 }
1993 else
1994 {
1995 glClearColor (myBgColor.rgb[0],
1996 myBgColor.rgb[1],
1997 myBgColor.rgb[2],
1998 1.0f);
1999 }
2000
2001 glClear (GL_COLOR_BUFFER_BIT);
2002
fc73a202 2003 if (theFrameBuffer != NULL)
2004 theFrameBuffer->BindBuffer (myGlContext);
e276548b 2005
fc73a202 2006 myView->DrawBackground (*this);
e276548b 2007
fc73a202 2008 // Generate ray-traced image
e276548b 2009 glMatrixMode (GL_PROJECTION);
2010 glLoadIdentity();
2011
2012 glMatrixMode (GL_MODELVIEW);
2013 glLoadIdentity();
2014
fc73a202 2015 glEnable (GL_BLEND);
2016 glBlendFunc (GL_ONE, GL_SRC_ALPHA);
e276548b 2017
2018 if (myIsRaytraceDataValid)
2019 {
fc73a202 2020 myRaytraceScreenQuad.Bind (myGlContext);
2021
2022 RunRaytraceShaders (theCView,
2023 theSizeX,
2024 theSizeY,
2025 aOrigins,
2026 aDirects,
2027 theFrameBuffer);
2028
2029 myRaytraceScreenQuad.Unbind (myGlContext);
e276548b 2030 }
2031
fc73a202 2032 if (theFrameBuffer != NULL)
2033 theFrameBuffer->UnbindBuffer (myGlContext);
2034
e276548b 2035 glPopAttrib();
2036
2037 // Swap the buffers
2038 if (theToSwap)
2039 {
2040 GetGlContext()->SwapBuffers();
2041 myBackBufferRestored = Standard_False;
2042 }
2043 else
fc73a202 2044 {
e276548b 2045 glFlush();
fc73a202 2046 }
e276548b 2047
2048 return Standard_True;
2049}