1 // Created on: 2013-08-27
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2013 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <NCollection_Mat4.hxx>
17 #include <OpenGl_ArbFBO.hxx>
18 #include <OpenGl_FrameBuffer.hxx>
19 #include <OpenGl_Texture.hxx>
20 #include <OpenGl_VertexBuffer.hxx>
21 #include <OpenGl_View.hxx>
22 #include <OpenGl_Workspace.hxx>
23 #include <OSD_File.hxx>
24 #include <OSD_Protection.hxx>
25 #include <Standard_Assert.hxx>
27 using namespace OpenGl_Raytrace;
29 //! Use this macro to output ray-tracing debug info
30 // #define RAY_TRACE_PRINT_INFO
32 #ifdef RAY_TRACE_PRINT_INFO
33 #include <OSD_Timer.hxx>
36 // =======================================================================
37 // function : MatVecMult
38 // purpose : Multiples 4x4 matrix by 4D vector
39 // =======================================================================
41 BVH_Vec4f MatVecMult (const T m[16], const BVH_Vec4f& v)
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()));
54 // =======================================================================
55 // function : UpdateRaytraceGeometry
56 // purpose : Updates 3D scene geometry for ray tracing
57 // =======================================================================
58 Standard_Boolean OpenGl_Workspace::UpdateRaytraceGeometry (Standard_Boolean theCheck)
61 return Standard_False;
63 // Note: In 'check' mode the scene geometry is analyzed for modifications
64 // This is light-weight procedure performed for each frame
68 myRaytraceGeometry.Clear();
70 myIsRaytraceDataValid = Standard_False;
74 if (myLayersModificationStatus != myView->LayerList().ModificationState())
76 return UpdateRaytraceGeometry (Standard_False);
80 Standard_ShortReal* aTransform (NULL);
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;
87 const OpenGl_LayerList& aList = myView->LayerList();
89 for (OpenGl_SequenceOfLayers::Iterator anLayerIt (aList.Layers()); anLayerIt.More(); anLayerIt.Next())
91 const OpenGl_PriorityList& aPriorityList = anLayerIt.Value();
93 if (aPriorityList.NbStructures() == 0)
96 const OpenGl_ArrayOfStructure& aStructArray = aPriorityList.ArrayOfStructures();
98 for (Standard_Integer anIndex = 0; anIndex < aStructArray.Length(); ++anIndex)
100 OpenGl_SequenceOfStructure::Iterator aStructIt;
102 for (aStructIt.Init (aStructArray (anIndex)); aStructIt.More(); aStructIt.Next())
104 const OpenGl_Structure* aStructure = aStructIt.Value();
108 if (CheckRaytraceStructure (aStructure))
110 return UpdateRaytraceGeometry (Standard_False);
115 if (!aStructure->IsRaytracable())
118 if (aStructure->Transformation()->mat != NULL)
120 if (aTransform == NULL)
121 aTransform = new Standard_ShortReal[16];
123 for (Standard_Integer i = 0; i < 4; ++i)
124 for (Standard_Integer j = 0; j < 4; ++j)
126 aTransform[j * 4 + i] = aStructure->Transformation()->mat[i][j];
130 AddRaytraceStructure (aStructure, aTransform, anElements);
138 // Actualize the hash map of structures -- remove out-of-date records
139 std::map<const OpenGl_Structure*, Standard_Size>::iterator anIter = myStructureStates.begin();
141 while (anIter != myStructureStates.end())
143 if (anElements.find (anIter->first) == anElements.end())
145 myStructureStates.erase (anIter++);
153 // Actualize OpenGL layer list state
154 myLayersModificationStatus = myView->LayerList().ModificationState();
156 // Rebuild bottom-level and high-level BVHs
157 myRaytraceGeometry.ProcessAcceleration();
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())));
164 myRaytraceSceneRadius = 2.f /* scale factor */ * Max (aMinRadius, aMaxRadius);
166 const BVH_Vec4f aSize = myRaytraceGeometry.Box().Size();
168 myRaytraceSceneEpsilon = Max (1e-4f, 1e-4f * sqrtf (
169 aSize.x() * aSize.x() + aSize.y() * aSize.y() + aSize.z() * aSize.z()));
171 return UploadRaytraceData();
174 delete [] aTransform;
176 return Standard_True;
179 // =======================================================================
180 // function : CheckRaytraceStructure
181 // purpose : Checks to see if the structure is modified
182 // =======================================================================
183 Standard_Boolean OpenGl_Workspace::CheckRaytraceStructure (const OpenGl_Structure* theStructure)
185 if (!theStructure->IsRaytracable())
187 // Checks to see if all ray-tracable elements were
188 // removed from the structure
189 if (theStructure->ModificationState() > 0)
191 theStructure->ResetModificationState();
192 return Standard_True;
195 return Standard_False;
198 std::map<const OpenGl_Structure*, Standard_Size>::iterator aStructState = myStructureStates.find (theStructure);
200 if (aStructState != myStructureStates.end())
201 return aStructState->second != theStructure->ModificationState();
203 return Standard_True;
206 // =======================================================================
207 // function : CreateMaterial
208 // purpose : Creates ray-tracing material properties
209 // =======================================================================
210 void CreateMaterial (const OPENGL_SURF_PROP& theProp, OpenGl_RaytraceMaterial& theMaterial)
212 const float* aSrcAmb = theProp.isphysic ? theProp.ambcol.rgb : theProp.matcol.rgb;
213 theMaterial.Ambient = BVH_Vec4f (aSrcAmb[0] * theProp.amb,
214 aSrcAmb[1] * theProp.amb,
215 aSrcAmb[2] * theProp.amb,
218 const float* aSrcDif = theProp.isphysic ? theProp.difcol.rgb : theProp.matcol.rgb;
219 theMaterial.Diffuse = BVH_Vec4f (aSrcDif[0] * theProp.diff,
220 aSrcDif[1] * theProp.diff,
221 aSrcDif[2] * theProp.diff,
224 const float aDefSpecCol[4] = {1.0f, 1.0f, 1.0f, 1.0f};
225 const float* aSrcSpe = theProp.isphysic ? theProp.speccol.rgb : aDefSpecCol;
226 theMaterial.Specular = BVH_Vec4f (aSrcSpe[0] * theProp.spec,
227 aSrcSpe[1] * theProp.spec,
228 aSrcSpe[2] * theProp.spec,
231 const float* aSrcEms = theProp.isphysic ? theProp.emscol.rgb : theProp.matcol.rgb;
232 theMaterial.Emission = BVH_Vec4f (aSrcEms[0] * theProp.emsv,
233 aSrcEms[1] * theProp.emsv,
234 aSrcEms[2] * theProp.emsv,
237 // Note: Here we use sub-linear transparency function
238 // to produce realistic-looking transparency effect
239 theMaterial.Transparency = BVH_Vec4f (powf (theProp.trans, 0.75f),
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()));
248 const float aReflectionScale = 0.75f / aMaxRefl;
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,
256 // =======================================================================
257 // function : AddRaytraceStructure
258 // purpose : Adds OpenGL structure to ray-traced scene geometry
259 // =======================================================================
260 Standard_Boolean OpenGl_Workspace::AddRaytraceStructure (const OpenGl_Structure* theStructure,
261 const Standard_ShortReal* theTransform, std::set<const OpenGl_Structure*>& theElements)
263 theElements.insert (theStructure);
265 if (!theStructure->IsVisible())
267 myStructureStates[theStructure] = theStructure->ModificationState();
268 return Standard_True;
271 // Get structure material
272 Standard_Integer aStructMatID = -1;
274 if (theStructure->AspectFace() != NULL)
276 aStructMatID = static_cast<Standard_Integer> (myRaytraceGeometry.Materials.size());
278 OpenGl_RaytraceMaterial aStructMaterial;
279 CreateMaterial (theStructure->AspectFace()->IntFront(), aStructMaterial);
281 myRaytraceGeometry.Materials.push_back (aStructMaterial);
284 for (OpenGl_Structure::GroupIterator aGroupIter (theStructure->DrawGroups()); aGroupIter.More(); aGroupIter.Next())
286 // Get group material
287 Standard_Integer aGroupMatID = -1;
288 if (aGroupIter.Value()->AspectFace() != NULL)
290 aGroupMatID = static_cast<Standard_Integer> (myRaytraceGeometry.Materials.size());
292 OpenGl_RaytraceMaterial aGroupMaterial;
293 CreateMaterial (aGroupIter.Value()->AspectFace()->IntFront(), aGroupMaterial);
295 myRaytraceGeometry.Materials.push_back (aGroupMaterial);
298 Standard_Integer aMatID = aGroupMatID < 0 ? aStructMatID : aGroupMatID;
302 aMatID = static_cast<Standard_Integer> (myRaytraceGeometry.Materials.size());
304 myRaytraceGeometry.Materials.push_back (OpenGl_RaytraceMaterial());
307 // Add OpenGL elements from group (extract primitives arrays and aspects)
308 for (const OpenGl_ElementNode* aNode = aGroupIter.Value()->FirstNode(); aNode != NULL; aNode = aNode->next)
310 OpenGl_AspectFace* anAspect = dynamic_cast<OpenGl_AspectFace*> (aNode->elem);
311 if (anAspect != NULL)
313 aMatID = static_cast<Standard_Integer> (myRaytraceGeometry.Materials.size());
315 OpenGl_RaytraceMaterial aMaterial;
316 CreateMaterial (anAspect->IntFront(), aMaterial);
318 myRaytraceGeometry.Materials.push_back (aMaterial);
322 OpenGl_PrimitiveArray* aPrimArray = dynamic_cast<OpenGl_PrimitiveArray*> (aNode->elem);
323 if (aPrimArray != NULL)
325 NCollection_Handle<BVH_Object<Standard_ShortReal, 4> > aSet =
326 AddRaytracePrimitiveArray (aPrimArray->PArray(), aMatID, theTransform);
329 myRaytraceGeometry.Objects().Append (aSet);
335 Standard_ShortReal* aTransform (NULL);
337 // Process all connected OpenGL structures
338 for (OpenGl_ListOfStructure::Iterator anIts (theStructure->ConnectedStructures()); anIts.More(); anIts.Next())
340 if (anIts.Value()->Transformation()->mat != NULL)
342 Standard_ShortReal* aTransform = new Standard_ShortReal[16];
344 for (Standard_Integer i = 0; i < 4; ++i)
345 for (Standard_Integer j = 0; j < 4; ++j)
347 aTransform[j * 4 + i] =
348 anIts.Value()->Transformation()->mat[i][j];
352 if (anIts.Value()->IsRaytracable())
353 AddRaytraceStructure (anIts.Value(), aTransform != NULL ? aTransform : theTransform, theElements);
358 myStructureStates[theStructure] = theStructure->ModificationState();
360 return Standard_True;
363 // =======================================================================
364 // function : AddRaytracePrimitiveArray
365 // purpose : Adds OpenGL primitive array to ray-traced scene geometry
366 // =======================================================================
367 OpenGl_TriangleSet* OpenGl_Workspace::AddRaytracePrimitiveArray (
368 const CALL_DEF_PARRAY* theArray, Standard_Integer theMatID, const Standard_ShortReal* theTransform)
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)
380 if (theArray->vertices == NULL)
383 #ifdef RAY_TRACE_PRINT_INFO
384 switch (theArray->type)
386 case TelPolygonsArrayType:
387 std::cout << "\tAdding TelPolygonsArrayType" << std::endl; break;
388 case TelTrianglesArrayType:
389 std::cout << "\tAdding TelTrianglesArrayType" << std::endl; break;
390 case TelQuadranglesArrayType:
391 std::cout << "\tAdding TelQuadranglesArrayType" << std::endl; break;
392 case TelTriangleFansArrayType:
393 std::cout << "\tAdding TelTriangleFansArrayType" << std::endl; break;
394 case TelTriangleStripsArrayType:
395 std::cout << "\tAdding TelTriangleStripsArrayType" << std::endl; break;
396 case TelQuadrangleStripsArrayType:
397 std::cout << "\tAdding TelQuadrangleStripsArrayType" << std::endl; break;
401 OpenGl_TriangleSet* aSet = new OpenGl_TriangleSet;
404 aSet->Vertices.reserve (theArray->num_vertexs);
406 for (Standard_Integer aVert = 0; aVert < theArray->num_vertexs; ++aVert)
408 BVH_Vec4f aVertex (theArray->vertices[aVert].xyz[0],
409 theArray->vertices[aVert].xyz[1],
410 theArray->vertices[aVert].xyz[2],
413 aVertex = MatVecMult (theTransform, aVertex);
415 aSet->Vertices.push_back (aVertex);
418 aSet->Normals.reserve (theArray->num_vertexs);
420 for (Standard_Integer aNorm = 0; aNorm < theArray->num_vertexs; ++aNorm)
424 // Note: In case of absence of normals, the
425 // renderer uses generated geometric normals
427 if (theArray->vnormals != NULL)
429 aNormal = BVH_Vec4f (theArray->vnormals[aNorm].xyz[0],
430 theArray->vnormals[aNorm].xyz[1],
431 theArray->vnormals[aNorm].xyz[2],
435 aNormal = MatVecMult (theTransform, aNormal);
438 aSet->Normals.push_back (aNormal);
441 if (theArray->num_bounds > 0)
443 #ifdef RAY_TRACE_PRINT_INFO
444 std::cout << "\tNumber of bounds = " << theArray->num_bounds << std::endl;
447 Standard_Integer aBoundStart = 0;
449 for (Standard_Integer aBound = 0; aBound < theArray->num_bounds; ++aBound)
451 const Standard_Integer aVertNum = theArray->bounds[aBound];
453 #ifdef RAY_TRACE_PRINT_INFO
454 std::cout << "\tAdding indices from bound " << aBound << ": " <<
455 aBoundStart << " .. " << aVertNum << std::endl;
458 if (!AddRaytraceVertexIndices (aSet, theArray, aBoundStart, aVertNum, theMatID))
464 aBoundStart += aVertNum;
469 const Standard_Integer aVertNum = theArray->num_edges > 0 ? theArray->num_edges : theArray->num_vertexs;
471 #ifdef RAY_TRACE_PRINT_INFO
472 std::cout << "\tAdding indices from array: " << aVertNum << std::endl;
475 if (!AddRaytraceVertexIndices (aSet, theArray, 0, aVertNum, theMatID))
483 if (aSet->Size() != 0)
489 // =======================================================================
490 // function : AddRaytraceVertexIndices
491 // purpose : Adds vertex indices to ray-traced scene geometry
492 // =======================================================================
493 Standard_Boolean OpenGl_Workspace::AddRaytraceVertexIndices (OpenGl_TriangleSet* theSet,
494 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID)
496 switch (theArray->type)
498 case TelTrianglesArrayType:
499 return AddRaytraceTriangleArray (theSet, theArray, theOffset, theCount, theMatID);
501 case TelQuadranglesArrayType:
502 return AddRaytraceQuadrangleArray (theSet, theArray, theOffset, theCount, theMatID);
504 case TelTriangleFansArrayType:
505 return AddRaytraceTriangleFanArray (theSet, theArray, theOffset, theCount, theMatID);
507 case TelTriangleStripsArrayType:
508 return AddRaytraceTriangleStripArray (theSet, theArray, theOffset, theCount, theMatID);
510 case TelQuadrangleStripsArrayType:
511 return AddRaytraceQuadrangleStripArray (theSet, theArray, theOffset, theCount, theMatID);
514 return AddRaytracePolygonArray (theSet, theArray, theOffset, theCount, theMatID);
518 // =======================================================================
519 // function : AddRaytraceTriangleArray
520 // purpose : Adds OpenGL triangle array to ray-traced scene geometry
521 // =======================================================================
522 Standard_Boolean OpenGl_Workspace::AddRaytraceTriangleArray (OpenGl_TriangleSet* theSet,
523 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID)
526 return Standard_True;
528 theSet->Elements.reserve (theSet->Elements.size() + theCount / 3);
530 if (theArray->num_edges > 0)
532 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; aVert += 3)
534 theSet->Elements.push_back (BVH_Vec4i (theArray->edges[aVert + 0],
535 theArray->edges[aVert + 1],
536 theArray->edges[aVert + 2],
542 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; aVert += 3)
544 theSet->Elements.push_back (BVH_Vec4i (aVert + 0,
551 return Standard_True;
554 // =======================================================================
555 // function : AddRaytraceTriangleFanArray
556 // purpose : Adds OpenGL triangle fan array to ray-traced scene geometry
557 // =======================================================================
558 Standard_Boolean OpenGl_Workspace::AddRaytraceTriangleFanArray (OpenGl_TriangleSet* theSet,
559 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID)
562 return Standard_True;
564 theSet->Elements.reserve (theSet->Elements.size() + theCount - 2);
566 if (theArray->num_edges > 0)
568 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; ++aVert)
570 theSet->Elements.push_back (BVH_Vec4i (theArray->edges[theOffset],
571 theArray->edges[aVert + 1],
572 theArray->edges[aVert + 2],
578 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; ++aVert)
580 theSet->Elements.push_back (BVH_Vec4i (theOffset,
587 return Standard_True;
590 // =======================================================================
591 // function : AddRaytraceTriangleStripArray
592 // purpose : Adds OpenGL triangle strip array to ray-traced scene geometry
593 // =======================================================================
594 Standard_Boolean OpenGl_Workspace::AddRaytraceTriangleStripArray (OpenGl_TriangleSet* theSet,
595 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID)
598 return Standard_True;
600 theSet->Elements.reserve (theSet->Elements.size() + theCount - 2);
602 if (theArray->num_edges > 0)
604 for (Standard_Integer aVert = theOffset, aCW = 0; aVert < theOffset + theCount - 2; ++aVert, aCW = (aCW + 1) % 2)
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],
614 for (Standard_Integer aVert = theOffset, aCW = 0; aVert < theOffset + theCount - 2; ++aVert, aCW = (aCW + 1) % 2)
616 theSet->Elements.push_back (BVH_Vec4i (aVert + aCW ? 1 : 0,
623 return Standard_True;
626 // =======================================================================
627 // function : AddRaytraceQuadrangleArray
628 // purpose : Adds OpenGL quad array to ray-traced scene geometry
629 // =======================================================================
630 Standard_Boolean OpenGl_Workspace::AddRaytraceQuadrangleArray (OpenGl_TriangleSet* theSet,
631 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID)
634 return Standard_True;
636 theSet->Elements.reserve (theSet->Elements.size() + theCount / 2);
638 if (theArray->num_edges > 0)
640 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 3; aVert += 4)
642 theSet->Elements.push_back (BVH_Vec4i (theArray->edges[aVert + 0],
643 theArray->edges[aVert + 1],
644 theArray->edges[aVert + 2],
647 theSet->Elements.push_back (BVH_Vec4i (theArray->edges[aVert + 0],
648 theArray->edges[aVert + 2],
649 theArray->edges[aVert + 3],
655 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 3; aVert += 4)
657 theSet->Elements.push_back (BVH_Vec4i (aVert + 0,
662 theSet->Elements.push_back (BVH_Vec4i (aVert + 0,
669 return Standard_True;
672 // =======================================================================
673 // function : AddRaytraceQuadrangleStripArray
674 // purpose : Adds OpenGL quad strip array to ray-traced scene geometry
675 // =======================================================================
676 Standard_Boolean OpenGl_Workspace::AddRaytraceQuadrangleStripArray (OpenGl_TriangleSet* theSet,
677 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID)
680 return Standard_True;
682 theSet->Elements.reserve (theSet->Elements.size() + 2 * theCount - 6);
684 if (theArray->num_edges > 0)
686 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 3; aVert += 2)
688 theSet->Elements.push_back (BVH_Vec4i (theArray->edges[aVert + 0],
689 theArray->edges[aVert + 1],
690 theArray->edges[aVert + 2],
693 theSet->Elements.push_back (BVH_Vec4i (theArray->edges[aVert + 1],
694 theArray->edges[aVert + 3],
695 theArray->edges[aVert + 2],
701 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 3; aVert += 2)
703 theSet->Elements.push_back (BVH_Vec4i (aVert + 0,
708 theSet->Elements.push_back (BVH_Vec4i (aVert + 1,
715 return Standard_True;
718 // =======================================================================
719 // function : AddRaytracePolygonArray
720 // purpose : Adds OpenGL polygon array to ray-traced scene geometry
721 // =======================================================================
722 Standard_Boolean OpenGl_Workspace::AddRaytracePolygonArray (OpenGl_TriangleSet* theSet,
723 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID)
726 return Standard_True;
728 theSet->Elements.reserve (theSet->Elements.size() + theCount - 2);
730 if (theArray->num_edges > 0)
732 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; ++aVert)
734 theSet->Elements.push_back (BVH_Vec4i (theArray->edges[theOffset],
735 theArray->edges[aVert + 1],
736 theArray->edges[aVert + 2],
742 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; ++aVert)
744 theSet->Elements.push_back (BVH_Vec4i (theOffset,
751 return Standard_True;
754 // =======================================================================
755 // function : UpdateRaytraceLightSources
756 // purpose : Updates 3D scene light sources for ray-tracing
757 // =======================================================================
758 Standard_Boolean OpenGl_Workspace::UpdateRaytraceLightSources (const GLdouble theInvModelView[16])
760 myRaytraceGeometry.Sources.clear();
762 myRaytraceGeometry.Ambient = BVH_Vec4f (0.0f, 0.0f, 0.0f, 0.0f);
764 for (OpenGl_ListOfLight::Iterator anItl (myView->LightList()); anItl.More(); anItl.Next())
766 const OpenGl_Light& aLight = anItl.Value();
768 if (aLight.Type == Visual3d_TOLS_AMBIENT)
770 myRaytraceGeometry.Ambient += BVH_Vec4f (aLight.Color.r(),
777 BVH_Vec4f aDiffuse (aLight.Color.r(),
782 BVH_Vec4f aPosition (-aLight.Direction.x(),
783 -aLight.Direction.y(),
784 -aLight.Direction.z(),
787 if (aLight.Type != Visual3d_TOLS_DIRECTIONAL)
789 aPosition = BVH_Vec4f (aLight.Position.x(),
795 if (aLight.IsHeadlight)
796 aPosition = MatVecMult (theInvModelView, aPosition);
799 myRaytraceGeometry.Sources.push_back (OpenGl_RaytraceLight (aDiffuse, aPosition));
802 if (myRaytraceLightSrcTexture.IsNull()) // create light source buffer
804 myRaytraceLightSrcTexture = new OpenGl_TextureBufferArb;
806 if (!myRaytraceLightSrcTexture->Create (myGlContext))
808 #ifdef RAY_TRACE_PRINT_INFO
809 std::cout << "Error: Failed to create light source buffer" << std::endl;
811 return Standard_False;
815 if (myRaytraceGeometry.Sources.size() != 0)
817 const GLfloat* aDataPtr = myRaytraceGeometry.Sources.front().Packed();
818 if (!myRaytraceLightSrcTexture->Init (myGlContext, 4, GLsizei (myRaytraceGeometry.Sources.size() * 2), aDataPtr))
820 #ifdef RAY_TRACE_PRINT_INFO
821 std::cout << "Error: Failed to upload light source buffer" << std::endl;
823 return Standard_False;
827 return Standard_True;
830 // =======================================================================
831 // function : UpdateRaytraceEnvironmentMap
832 // purpose : Updates environment map for ray-tracing
833 // =======================================================================
834 Standard_Boolean OpenGl_Workspace::UpdateRaytraceEnvironmentMap()
837 return Standard_False;
839 if (myViewModificationStatus == myView->ModificationState())
840 return Standard_True;
842 for (Standard_Integer anIdx = 0; anIdx < 2; ++anIdx)
844 const Handle(OpenGl_ShaderProgram)& aProgram =
845 anIdx == 0 ? myRaytraceProgram : myPostFSAAProgram;
847 if (!aProgram.IsNull())
849 aProgram->Bind (myGlContext);
851 if (!myView->TextureEnv().IsNull() && myView->SurfaceDetail() != Visual3d_TOD_NONE)
853 myView->TextureEnv()->Bind (
854 myGlContext, GL_TEXTURE0 + OpenGl_RT_EnvironmentMapTexture);
856 aProgram->SetUniform (myGlContext, "uEnvironmentEnable", 1);
860 aProgram->SetUniform (myGlContext, "uEnvironmentEnable", 0);
863 aProgram->SetSampler (myGlContext,
864 "uEnvironmentMapTexture", OpenGl_RT_EnvironmentMapTexture);
868 OpenGl_ShaderProgram::Unbind (myGlContext);
870 myViewModificationStatus = myView->ModificationState();
872 return Standard_True;
875 // =======================================================================
877 // purpose : Returns shader source combined with prefix
878 // =======================================================================
879 TCollection_AsciiString OpenGl_Workspace::ShaderSource::Source() const
881 static const TCollection_AsciiString aVersion = "#version 140";
883 if (myPrefix.IsEmpty())
885 return aVersion + "\n" + mySource;
888 return aVersion + "\n" + myPrefix + "\n" + mySource;
891 // =======================================================================
893 // purpose : Loads shader source from specified files
894 // =======================================================================
895 void OpenGl_Workspace::ShaderSource::Load (
896 const TCollection_AsciiString* theFileNames, const Standard_Integer theCount)
900 for (Standard_Integer anIndex = 0; anIndex < theCount; ++anIndex)
902 OSD_File aFile (theFileNames[anIndex]);
904 Standard_ASSERT_RETURN (aFile.Exists(),
905 "Error: Failed to find shader source file", /* none */);
907 aFile.Open (OSD_ReadOnly, OSD_Protection());
909 TCollection_AsciiString aSource;
911 Standard_ASSERT_RETURN (aFile.IsOpen(),
912 "Error: Failed to open shader source file", /* none */);
914 aFile.Read (aSource, (Standard_Integer) aFile.Size());
916 if (!aSource.IsEmpty())
918 mySource += TCollection_AsciiString ("\n") + aSource;
925 // =======================================================================
926 // function : LoadShader
927 // purpose : Creates new shader object with specified source
928 // =======================================================================
929 Handle(OpenGl_ShaderObject) OpenGl_Workspace::LoadShader (const ShaderSource& theSource, GLenum theType)
931 Handle(OpenGl_ShaderObject) aShader = new OpenGl_ShaderObject (theType);
933 if (!aShader->Create (myGlContext))
935 const TCollection_ExtendedString aMessage = "Error: Failed to create shader object";
937 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
938 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
940 aShader->Release (myGlContext.operator->());
942 return Handle(OpenGl_ShaderObject)();
945 if (!aShader->LoadSource (myGlContext, theSource.Source()))
947 const TCollection_ExtendedString aMessage = "Error: Failed to set shader source";
949 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
950 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
952 aShader->Release (myGlContext.operator->());
954 return Handle(OpenGl_ShaderObject)();
957 TCollection_AsciiString aBuildLog;
959 if (!aShader->Compile (myGlContext))
961 if (aShader->FetchInfoLog (myGlContext, aBuildLog))
963 const TCollection_ExtendedString aMessage =
964 TCollection_ExtendedString ("Error: Failed to compile shader object:\n") + aBuildLog;
966 #ifdef RAY_TRACE_PRINT_INFO
967 std::cout << aBuildLog << std::endl;
970 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
971 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
974 aShader->Release (myGlContext.operator->());
976 return Handle(OpenGl_ShaderObject)();
979 #ifdef RAY_TRACE_PRINT_INFO
980 if (aShader->FetchInfoLog (myGlContext, aBuildLog))
982 if (!aBuildLog.IsEmpty())
984 std::cout << aBuildLog << std::endl;
988 std::cout << "Info: shader build log is empty" << std::endl;
996 // =======================================================================
997 // function : SafeFailBack
998 // purpose : Performs safe exit when shaders initialization fails
999 // =======================================================================
1000 Standard_Boolean OpenGl_Workspace::SafeFailBack (const TCollection_ExtendedString& theMessage)
1002 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1003 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, theMessage);
1005 myComputeInitStatus = OpenGl_RT_FAIL;
1007 ReleaseRaytraceResources();
1009 return Standard_False;
1012 // =======================================================================
1013 // function : InitRaytraceResources
1014 // purpose : Initializes OpenGL/GLSL shader programs
1015 // =======================================================================
1016 Standard_Boolean OpenGl_Workspace::InitRaytraceResources()
1018 Standard_Boolean aToRebuildShaders = Standard_False;
1020 if (myComputeInitStatus == OpenGl_RT_INIT)
1022 if (!myIsRaytraceDataValid)
1023 return Standard_True;
1025 const Standard_Integer aRequiredStackSize =
1026 myRaytraceGeometry.HighLevelTreeDepth() + myRaytraceGeometry.BottomLevelTreeDepth();
1028 if (myTraversalStackSize < aRequiredStackSize)
1030 myTraversalStackSize = Max (aRequiredStackSize, THE_DEFAULT_STACK_SIZE);
1032 aToRebuildShaders = Standard_True;
1036 if (aRequiredStackSize < myTraversalStackSize)
1038 if (myTraversalStackSize > THE_DEFAULT_STACK_SIZE)
1040 myTraversalStackSize = Max (aRequiredStackSize, THE_DEFAULT_STACK_SIZE);
1042 aToRebuildShaders = Standard_True;
1047 if (aToRebuildShaders)
1049 #ifdef RAY_TRACE_PRINT_INFO
1050 std::cout << "Info: Rebuild shaders with stack size: " << myTraversalStackSize << std::endl;
1053 TCollection_AsciiString aStackSizeStr =
1054 TCollection_AsciiString ("#define STACK_SIZE ") + TCollection_AsciiString (myTraversalStackSize);
1056 myRaytraceShaderSource.SetPrefix (aStackSizeStr);
1057 myPostFSAAShaderSource.SetPrefix (aStackSizeStr);
1059 if (!myRaytraceShader->LoadSource (myGlContext, myRaytraceShaderSource.Source())
1060 || !myPostFSAAShader->LoadSource (myGlContext, myPostFSAAShaderSource.Source()))
1062 return Standard_False;
1065 if (!myRaytraceShader->Compile (myGlContext)
1066 || !myPostFSAAShader->Compile (myGlContext))
1068 return Standard_False;
1071 if (!myRaytraceProgram->Link (myGlContext)
1072 || !myPostFSAAProgram->Link (myGlContext))
1074 return Standard_False;
1079 if (myComputeInitStatus == OpenGl_RT_NONE)
1081 if (!myGlContext->IsGlGreaterEqual (3, 1))
1083 const TCollection_ExtendedString aMessage = "Ray-tracing requires OpenGL 3.1 and higher";
1085 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1086 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
1088 return Standard_False;
1091 TCollection_AsciiString aFolder = Graphic3d_ShaderProgram::ShadersFolder();
1093 if (aFolder.IsEmpty())
1095 const TCollection_ExtendedString aMessage = "Failed to locate shaders directory";
1097 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1098 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
1100 return Standard_False;
1103 if (myIsRaytraceDataValid)
1105 myTraversalStackSize = Max (THE_DEFAULT_STACK_SIZE,
1106 myRaytraceGeometry.HighLevelTreeDepth() + myRaytraceGeometry.BottomLevelTreeDepth());
1110 Handle(OpenGl_ShaderObject) aBasicVertShader = LoadShader (
1111 ShaderSource (aFolder + "/RaytraceBase.vs"), GL_VERTEX_SHADER);
1113 if (aBasicVertShader.IsNull())
1115 return SafeFailBack ("Failed to set vertex shader source");
1118 TCollection_AsciiString aFiles[] = { aFolder + "/RaytraceBase.fs", aFolder + "/RaytraceRender.fs" };
1120 myRaytraceShaderSource.Load (aFiles, 2);
1122 TCollection_AsciiString aStackSizeStr =
1123 TCollection_AsciiString ("#define STACK_SIZE ") + TCollection_AsciiString (myTraversalStackSize);
1125 myRaytraceShaderSource.SetPrefix (aStackSizeStr);
1127 myRaytraceShader = LoadShader (myRaytraceShaderSource, GL_FRAGMENT_SHADER);
1129 if (myRaytraceShader.IsNull())
1131 aBasicVertShader->Release (myGlContext.operator->());
1133 return SafeFailBack ("Failed to set ray-trace fragment shader source");
1136 myRaytraceProgram = new OpenGl_ShaderProgram;
1138 if (!myRaytraceProgram->Create (myGlContext))
1140 aBasicVertShader->Release (myGlContext.operator->());
1142 return SafeFailBack ("Failed to create ray-trace shader program");
1145 if (!myRaytraceProgram->AttachShader (myGlContext, aBasicVertShader)
1146 || !myRaytraceProgram->AttachShader (myGlContext, myRaytraceShader))
1148 aBasicVertShader->Release (myGlContext.operator->());
1150 return SafeFailBack ("Failed to attach ray-trace shader objects");
1153 if (!myRaytraceProgram->Link (myGlContext))
1155 TCollection_AsciiString aLinkLog;
1157 if (myRaytraceProgram->FetchInfoLog (myGlContext, aLinkLog))
1159 #ifdef RAY_TRACE_PRINT_INFO
1160 std::cout << aLinkLog << std::endl;
1164 return SafeFailBack ("Failed to link ray-trace shader program");
1169 Handle(OpenGl_ShaderObject) aBasicVertShader = LoadShader (
1170 ShaderSource (aFolder + "/RaytraceBase.vs"), GL_VERTEX_SHADER);
1172 if (aBasicVertShader.IsNull())
1174 return SafeFailBack ("Failed to set vertex shader source");
1177 TCollection_AsciiString aFiles[] = { aFolder + "/RaytraceBase.fs", aFolder + "/RaytraceSmooth.fs" };
1179 myPostFSAAShaderSource.Load (aFiles, 2);
1181 TCollection_AsciiString aStackSizeStr =
1182 TCollection_AsciiString ("#define STACK_SIZE ") + TCollection_AsciiString (myTraversalStackSize);
1184 myPostFSAAShaderSource.SetPrefix (aStackSizeStr);
1186 myPostFSAAShader = LoadShader (myPostFSAAShaderSource, GL_FRAGMENT_SHADER);
1188 if (myPostFSAAShader.IsNull())
1190 aBasicVertShader->Release (myGlContext.operator->());
1192 return SafeFailBack ("Failed to set FSAA fragment shader source");
1195 myPostFSAAProgram = new OpenGl_ShaderProgram;
1197 if (!myPostFSAAProgram->Create (myGlContext))
1199 aBasicVertShader->Release (myGlContext.operator->());
1201 return SafeFailBack ("Failed to create FSAA shader program");
1204 if (!myPostFSAAProgram->AttachShader (myGlContext, aBasicVertShader)
1205 || !myPostFSAAProgram->AttachShader (myGlContext, myPostFSAAShader))
1207 aBasicVertShader->Release (myGlContext.operator->());
1209 return SafeFailBack ("Failed to attach FSAA shader objects");
1212 if (!myPostFSAAProgram->Link (myGlContext))
1214 TCollection_AsciiString aLinkLog;
1216 if (myPostFSAAProgram->FetchInfoLog (myGlContext, aLinkLog))
1218 #ifdef RAY_TRACE_PRINT_INFO
1219 std::cout << aLinkLog << std::endl;
1223 return SafeFailBack ("Failed to link FSAA shader program");
1228 if (myComputeInitStatus == OpenGl_RT_NONE || aToRebuildShaders)
1230 for (Standard_Integer anIndex = 0; anIndex < 2; ++anIndex)
1232 Handle(OpenGl_ShaderProgram)& aShaderProgram =
1233 (anIndex == 0) ? myRaytraceProgram : myPostFSAAProgram;
1235 aShaderProgram->Bind (myGlContext);
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);
1262 aShaderProgram->SetSampler (myGlContext,
1263 "uFSAAInputTexture", OpenGl_RT_FSAAInputTexture);
1266 myUniformLocations[anIndex][OpenGl_RT_aPosition] =
1267 aShaderProgram->GetAttributeLocation (myGlContext, "aPosition");
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");
1286 myUniformLocations[anIndex][OpenGl_RT_uLightCount] =
1287 aShaderProgram->GetUniformLocation (myGlContext, "uLightCount");
1288 myUniformLocations[anIndex][OpenGl_RT_uLightAmbnt] =
1289 aShaderProgram->GetUniformLocation (myGlContext, "uGlobalAmbient");
1291 myUniformLocations[anIndex][OpenGl_RT_uSceneRad] =
1292 aShaderProgram->GetUniformLocation (myGlContext, "uSceneRadius");
1293 myUniformLocations[anIndex][OpenGl_RT_uSceneEps] =
1294 aShaderProgram->GetUniformLocation (myGlContext, "uSceneEpsilon");
1296 myUniformLocations[anIndex][OpenGl_RT_uShadEnabled] =
1297 aShaderProgram->GetUniformLocation (myGlContext, "uShadowsEnable");
1298 myUniformLocations[anIndex][OpenGl_RT_uReflEnabled] =
1299 aShaderProgram->GetUniformLocation (myGlContext, "uReflectionsEnable");
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");
1309 OpenGl_ShaderProgram::Unbind (myGlContext);
1312 if (myComputeInitStatus != OpenGl_RT_NONE)
1314 return myComputeInitStatus == OpenGl_RT_INIT;
1317 if (myRaytraceFBO1.IsNull())
1319 myRaytraceFBO1 = new OpenGl_FrameBuffer;
1322 if (myRaytraceFBO2.IsNull())
1324 myRaytraceFBO2 = new OpenGl_FrameBuffer;
1327 const GLfloat aVertices[] = { -1.f, -1.f, 0.f,
1334 myRaytraceScreenQuad.Init (myGlContext, 3, 6, aVertices);
1336 myComputeInitStatus = OpenGl_RT_INIT; // initialized in normal way
1338 return Standard_True;
1341 // =======================================================================
1342 // function : NullifyResource
1344 // =======================================================================
1345 inline void NullifyResource (const Handle(OpenGl_Context)& theContext,
1346 Handle(OpenGl_Resource)& theResource)
1348 if (!theResource.IsNull())
1350 theResource->Release (theContext.operator->());
1351 theResource.Nullify();
1355 // =======================================================================
1356 // function : ReleaseRaytraceResources
1357 // purpose : Releases OpenGL/GLSL shader programs
1358 // =======================================================================
1359 void OpenGl_Workspace::ReleaseRaytraceResources()
1361 NullifyResource (myGlContext, myRaytraceFBO1);
1362 NullifyResource (myGlContext, myRaytraceFBO2);
1364 NullifyResource (myGlContext, myRaytraceShader);
1365 NullifyResource (myGlContext, myPostFSAAShader);
1367 NullifyResource (myGlContext, myRaytraceProgram);
1368 NullifyResource (myGlContext, myPostFSAAProgram);
1370 NullifyResource (myGlContext, mySceneNodeInfoTexture);
1371 NullifyResource (myGlContext, mySceneMinPointTexture);
1372 NullifyResource (myGlContext, mySceneMaxPointTexture);
1374 NullifyResource (myGlContext, myObjectNodeInfoTexture);
1375 NullifyResource (myGlContext, myObjectMinPointTexture);
1376 NullifyResource (myGlContext, myObjectMaxPointTexture);
1378 NullifyResource (myGlContext, myGeometryVertexTexture);
1379 NullifyResource (myGlContext, myGeometryNormalTexture);
1380 NullifyResource (myGlContext, myGeometryTriangTexture);
1382 NullifyResource (myGlContext, myRaytraceLightSrcTexture);
1383 NullifyResource (myGlContext, myRaytraceMaterialTexture);
1385 if (myRaytraceScreenQuad.IsValid())
1386 myRaytraceScreenQuad.Release (myGlContext.operator->());
1389 // =======================================================================
1390 // function : UploadRaytraceData
1391 // purpose : Uploads ray-trace data to the GPU
1392 // =======================================================================
1393 Standard_Boolean OpenGl_Workspace::UploadRaytraceData()
1395 if (!myGlContext->IsGlGreaterEqual (3, 1))
1397 #ifdef RAY_TRACE_PRINT_INFO
1398 std::cout << "Error: OpenGL version is less than 3.1" << std::endl;
1400 return Standard_False;
1403 /////////////////////////////////////////////////////////////////////////////
1404 // Create OpenGL texture buffers
1406 if (mySceneNodeInfoTexture.IsNull()) // create hight-level BVH buffers
1408 mySceneNodeInfoTexture = new OpenGl_TextureBufferArb;
1409 mySceneMinPointTexture = new OpenGl_TextureBufferArb;
1410 mySceneMaxPointTexture = new OpenGl_TextureBufferArb;
1412 if (!mySceneNodeInfoTexture->Create (myGlContext)
1413 || !mySceneMinPointTexture->Create (myGlContext)
1414 || !mySceneMaxPointTexture->Create (myGlContext))
1416 #ifdef RAY_TRACE_PRINT_INFO
1417 std::cout << "Error: Failed to create buffers for high-level scene BVH" << std::endl;
1419 return Standard_False;
1423 if (myObjectNodeInfoTexture.IsNull()) // create bottom-level BVH buffers
1425 myObjectNodeInfoTexture = new OpenGl_TextureBufferArb;
1426 myObjectMinPointTexture = new OpenGl_TextureBufferArb;
1427 myObjectMaxPointTexture = new OpenGl_TextureBufferArb;
1429 if (!myObjectNodeInfoTexture->Create (myGlContext)
1430 || !myObjectMinPointTexture->Create (myGlContext)
1431 || !myObjectMaxPointTexture->Create (myGlContext))
1433 #ifdef RAY_TRACE_PRINT_INFO
1434 std::cout << "Error: Failed to create buffers for bottom-level scene BVH" << std::endl;
1436 return Standard_False;
1440 if (myGeometryVertexTexture.IsNull()) // create geometry buffers
1442 myGeometryVertexTexture = new OpenGl_TextureBufferArb;
1443 myGeometryNormalTexture = new OpenGl_TextureBufferArb;
1444 myGeometryTriangTexture = new OpenGl_TextureBufferArb;
1446 if (!myGeometryVertexTexture->Create (myGlContext)
1447 || !myGeometryNormalTexture->Create (myGlContext)
1448 || !myGeometryTriangTexture->Create (myGlContext))
1450 #ifdef RAY_TRACE_PRINT_INFO
1451 std::cout << "Error: Failed to create buffers for triangulation data" << std::endl;
1453 return Standard_False;
1457 if (myRaytraceMaterialTexture.IsNull()) // create material buffer
1459 myRaytraceMaterialTexture = new OpenGl_TextureBufferArb;
1461 if (!myRaytraceMaterialTexture->Create (myGlContext))
1463 #ifdef RAY_TRACE_PRINT_INFO
1464 std::cout << "Error: Failed to create buffers for material data" << std::endl;
1466 return Standard_False;
1470 /////////////////////////////////////////////////////////////////////////////
1471 // Write OpenGL texture buffers
1473 const NCollection_Handle<BVH_Tree<Standard_ShortReal, 4> >& aBVH = myRaytraceGeometry.BVH();
1475 bool aResult = true;
1476 if (!aBVH->NodeInfoBuffer().empty())
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()));
1487 #ifdef RAY_TRACE_PRINT_INFO
1488 std::cout << "Error: Failed to upload buffers for high-level scene BVH" << std::endl;
1490 return Standard_False;
1493 Standard_Size aTotalVerticesNb = 0;
1494 Standard_Size aTotalElementsNb = 0;
1495 Standard_Size aTotalBVHNodesNb = 0;
1497 for (Standard_Integer anElemIndex = 0; anElemIndex < myRaytraceGeometry.Size(); ++anElemIndex)
1499 OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> (
1500 myRaytraceGeometry.Objects().ChangeValue (anElemIndex).operator->());
1502 Standard_ASSERT_RETURN (aTriangleSet != NULL,
1503 "Error: Failed to get triangulation of OpenGL element", Standard_False);
1505 aTotalVerticesNb += aTriangleSet->Vertices.size();
1506 aTotalElementsNb += aTriangleSet->Elements.size();
1508 Standard_ASSERT_RETURN (!aTriangleSet->BVH().IsNull(),
1509 "Error: Failed to get bottom-level BVH of OpenGL element", Standard_False);
1511 aTotalBVHNodesNb += aTriangleSet->BVH()->NodeInfoBuffer().size();
1514 if (aTotalBVHNodesNb != 0)
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));
1523 #ifdef RAY_TRACE_PRINT_INFO
1524 std::cout << "Error: Failed to upload buffers for bottom-level scene BVH" << std::endl;
1526 return Standard_False;
1529 if (aTotalElementsNb != 0)
1531 aResult &= myGeometryTriangTexture->Init (myGlContext, 4, GLsizei (aTotalElementsNb), static_cast<const GLuint*> (NULL));
1534 if (aTotalVerticesNb != 0)
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));
1542 #ifdef RAY_TRACE_PRINT_INFO
1543 std::cout << "Error: Failed to upload buffers for scene geometry" << std::endl;
1545 return Standard_False;
1548 for (Standard_Integer aNodeIdx = 0; aNodeIdx < aBVH->Length(); ++aNodeIdx)
1550 if (!aBVH->IsOuter (aNodeIdx))
1553 OpenGl_TriangleSet* aTriangleSet = myRaytraceGeometry.TriangleSet (aNodeIdx);
1555 Standard_ASSERT_RETURN (aTriangleSet != NULL,
1556 "Error: Failed to get triangulation of OpenGL element", Standard_False);
1558 const Standard_Integer aBVHOffset = myRaytraceGeometry.AccelerationOffset (aNodeIdx);
1560 Standard_ASSERT_RETURN (aBVHOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET,
1561 "Error: Failed to get offset for bottom-level BVH", Standard_False);
1563 const size_t aBVHBuffserSize = aTriangleSet->BVH()->NodeInfoBuffer().size();
1565 if (aBVHBuffserSize != 0)
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()));
1575 #ifdef RAY_TRACE_PRINT_INFO
1576 std::cout << "Error: Failed to upload buffers for bottom-level scene BVHs" << std::endl;
1578 return Standard_False;
1582 const Standard_Integer aVerticesOffset = myRaytraceGeometry.VerticesOffset (aNodeIdx);
1584 Standard_ASSERT_RETURN (aVerticesOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET,
1585 "Error: Failed to get offset for triangulation vertices of OpenGL element", Standard_False);
1587 if (!aTriangleSet->Vertices.empty())
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()));
1595 const Standard_Integer anElementsOffset = myRaytraceGeometry.ElementsOffset (aNodeIdx);
1597 Standard_ASSERT_RETURN (anElementsOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET,
1598 "Error: Failed to get offset for triangulation elements of OpenGL element", Standard_False);
1600 if (!aTriangleSet->Elements.empty())
1602 aResult &= myGeometryTriangTexture->SubData (myGlContext, anElementsOffset, GLsizei (aTriangleSet->Elements.size()),
1603 reinterpret_cast<const GLuint*> (&aTriangleSet->Elements.front()));
1608 #ifdef RAY_TRACE_PRINT_INFO
1609 std::cout << "Error: Failed to upload triangulation buffers for OpenGL element" << std::endl;
1611 return Standard_False;
1615 if (myRaytraceGeometry.Materials.size() != 0)
1617 const GLfloat* aDataPtr = myRaytraceGeometry.Materials.front().Packed();
1618 aResult &= myRaytraceMaterialTexture->Init (myGlContext, 4, GLsizei (myRaytraceGeometry.Materials.size() * 7), aDataPtr);
1621 #ifdef RAY_TRACE_PRINT_INFO
1622 std::cout << "Error: Failed to upload material buffer" << std::endl;
1624 return Standard_False;
1628 myIsRaytraceDataValid = myRaytraceGeometry.Objects().Size() != 0;
1630 #ifdef RAY_TRACE_PRINT_INFO
1632 Standard_ShortReal aMemUsed = 0.f;
1634 for (Standard_Integer anElemIdx = 0; anElemIdx < myRaytraceGeometry.Size(); ++anElemIdx)
1636 OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> (
1637 myRaytraceGeometry.Objects().ChangeValue (anElemIdx).operator->());
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));
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));
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));
1661 std::cout << "GPU Memory Used (MB): ~" << aMemUsed / 1048576 << std::endl;
1668 // =======================================================================
1669 // function : ResizeRaytraceBuffers
1670 // purpose : Resizes OpenGL frame buffers
1671 // =======================================================================
1672 Standard_Boolean OpenGl_Workspace::ResizeRaytraceBuffers (const Standard_Integer theSizeX,
1673 const Standard_Integer theSizeY)
1675 if (myRaytraceFBO1->GetVPSizeX() != theSizeX
1676 || myRaytraceFBO1->GetVPSizeY() != theSizeY)
1678 myRaytraceFBO1->Init (myGlContext, theSizeX, theSizeY);
1679 myRaytraceFBO2->Init (myGlContext, theSizeX, theSizeY);
1682 return Standard_True;
1685 // =======================================================================
1686 // function : UpdateCamera
1687 // purpose : Generates viewing rays for corners of screen quad
1688 // =======================================================================
1689 void OpenGl_Workspace::UpdateCamera (const NCollection_Mat4<GLdouble>& theOrientation,
1690 const NCollection_Mat4<GLdouble>& theViewMapping,
1691 OpenGl_Vec3 theOrigins[4],
1692 OpenGl_Vec3 theDirects[4])
1694 NCollection_Mat4<GLdouble> aInvModelProj;
1696 // compute invserse model-view-projection matrix
1697 (theViewMapping * theOrientation).Inverted (aInvModelProj);
1699 Standard_Integer aOriginIndex = 0;
1700 Standard_Integer aDirectIndex = 0;
1702 for (Standard_Integer aY = -1; aY <= 1; aY += 2)
1704 for (Standard_Integer aX = -1; aX <= 1; aX += 2)
1706 OpenGl_Vec4d aOrigin (GLdouble(aX),
1711 aOrigin = aInvModelProj * aOrigin;
1713 aOrigin.x() = aOrigin.x() / aOrigin.w();
1714 aOrigin.y() = aOrigin.y() / aOrigin.w();
1715 aOrigin.z() = aOrigin.z() / aOrigin.w();
1717 OpenGl_Vec4d aDirect (GLdouble(aX),
1722 aDirect = aInvModelProj * aDirect;
1724 aDirect.x() = aDirect.x() / aDirect.w();
1725 aDirect.y() = aDirect.y() / aDirect.w();
1726 aDirect.z() = aDirect.z() / aDirect.w();
1728 aDirect = aDirect - aOrigin;
1730 GLdouble aInvLen = 1.0 / sqrt (aDirect.x() * aDirect.x() +
1731 aDirect.y() * aDirect.y() +
1732 aDirect.z() * aDirect.z());
1734 theOrigins[aOriginIndex++] = OpenGl_Vec3 (static_cast<GLfloat> (aOrigin.x()),
1735 static_cast<GLfloat> (aOrigin.y()),
1736 static_cast<GLfloat> (aOrigin.z()));
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));
1745 // =======================================================================
1746 // function : RunRaytraceShaders
1747 // purpose : Runs ray-tracing shader programs
1748 // =======================================================================
1749 Standard_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)
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);
1768 if (theCView.IsAntialiasingEnabled) // render source image to FBO
1770 myRaytraceFBO1->BindBuffer (myGlContext);
1772 glDisable (GL_BLEND);
1775 myRaytraceProgram->Bind (myGlContext);
1777 Standard_Integer aLightSourceBufferSize =
1778 static_cast<Standard_Integer> (myRaytraceGeometry.Sources.size());
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);
1809 myGlContext->core20fwd->glEnableVertexAttribArray (
1810 myUniformLocations[0][OpenGl_RT_aPosition]);
1812 myGlContext->core20fwd->glVertexAttribPointer (
1813 myUniformLocations[0][OpenGl_RT_aPosition], 3, GL_FLOAT, GL_FALSE, 0, NULL);
1815 myGlContext->core15fwd->glDrawArrays (GL_TRIANGLES, 0, 6);
1817 myGlContext->core20fwd->glDisableVertexAttribArray (
1818 myUniformLocations[0][OpenGl_RT_aPosition]);
1820 if (!theCView.IsAntialiasingEnabled)
1822 myRaytraceProgram->Unbind (myGlContext);
1824 return Standard_True;
1827 myGlContext->core20fwd->glActiveTexture (
1828 GL_TEXTURE0 + OpenGl_RT_FSAAInputTexture); // texture unit for FBO texture
1830 myRaytraceFBO1->BindTexture (myGlContext);
1832 myPostFSAAProgram->Bind (myGlContext);
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);
1863 const Standard_ShortReal aMaxOffset = 0.559017f;
1864 const Standard_ShortReal aMinOffset = 0.186339f;
1866 myGlContext->core20fwd->glEnableVertexAttribArray (
1867 myUniformLocations[1][OpenGl_RT_aPosition]);
1869 myGlContext->core20fwd->glVertexAttribPointer (
1870 myUniformLocations[1][OpenGl_RT_aPosition], 3, GL_FLOAT, GL_FALSE, 0, NULL);
1872 // Perform multi-pass adaptive FSAA using ping-pong technique
1873 for (Standard_Integer anIt = 0; anIt < 4; ++anIt)
1875 GLfloat aOffsetX = 1.f / theSizeX;
1876 GLfloat aOffsetY = 1.f / theSizeY;
1880 aOffsetX *= anIt < 1 ? aMinOffset : -aMaxOffset;
1881 aOffsetY *= anIt < 1 ? aMaxOffset : aMinOffset;
1885 aOffsetX *= anIt > 2 ? aMaxOffset : -aMinOffset;
1886 aOffsetY *= anIt > 2 ? -aMinOffset : -aMaxOffset;
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);
1896 Handle(OpenGl_FrameBuffer)& aFramebuffer = anIt % 2 ? myRaytraceFBO1 : myRaytraceFBO2;
1898 if (anIt == 3) // disable FBO on last iteration
1900 glEnable (GL_BLEND);
1902 if (theFrameBuffer != NULL)
1903 theFrameBuffer->BindBuffer (myGlContext);
1907 aFramebuffer->BindBuffer (myGlContext);
1910 myGlContext->core15fwd->glDrawArrays (GL_TRIANGLES, 0, 6);
1912 if (anIt != 3) // set input for the next pass
1914 aFramebuffer->BindTexture (myGlContext);
1915 aFramebuffer->UnbindBuffer (myGlContext);
1919 myGlContext->core20fwd->glDisableVertexAttribArray (
1920 myUniformLocations[1][OpenGl_RT_aPosition]);
1922 myPostFSAAProgram->Unbind (myGlContext);
1924 return Standard_True;
1927 // =======================================================================
1928 // function : Raytrace
1929 // purpose : Redraws the window using OpenGL/GLSL ray-tracing
1930 // =======================================================================
1931 Standard_Boolean OpenGl_Workspace::Raytrace (const Graphic3d_CView& theCView,
1932 const Standard_Integer theSizeX,
1933 const Standard_Integer theSizeY,
1934 const Standard_Boolean theToSwap,
1935 OpenGl_FrameBuffer* theFrameBuffer)
1937 if (!UpdateRaytraceGeometry (Standard_True))
1938 return Standard_False;
1940 if (!InitRaytraceResources())
1941 return Standard_False;
1943 if (!ResizeRaytraceBuffers (theSizeX, theSizeY))
1944 return Standard_False;
1946 if (!UpdateRaytraceEnvironmentMap())
1947 return Standard_False;
1949 // Get model-view and projection matrices
1950 TColStd_Array2OfReal theOrientation (0, 3, 0, 3);
1951 TColStd_Array2OfReal theViewMapping (0, 3, 0, 3);
1953 myView->GetMatrices (theOrientation, theViewMapping);
1955 NCollection_Mat4<GLdouble> aOrientationMatrix;
1956 NCollection_Mat4<GLdouble> aViewMappingMatrix;
1958 for (Standard_Integer j = 0; j < 4; ++j)
1960 for (Standard_Integer i = 0; i < 4; ++i)
1962 aOrientationMatrix [4 * j + i] = theOrientation (i, j);
1963 aViewMappingMatrix [4 * j + i] = theViewMapping (i, j);
1967 NCollection_Mat4<GLdouble> aInvOrientationMatrix;
1968 aOrientationMatrix.Inverted (aInvOrientationMatrix);
1970 if (!UpdateRaytraceLightSources (aInvOrientationMatrix))
1971 return Standard_False;
1973 OpenGl_Vec3 aOrigins[4];
1974 OpenGl_Vec3 aDirects[4];
1976 UpdateCamera (aOrientationMatrix,
1982 glPushAttrib (GL_ENABLE_BIT |
1984 GL_COLOR_BUFFER_BIT |
1985 GL_DEPTH_BUFFER_BIT);
1987 glDisable (GL_DEPTH_TEST);
1989 if (NamedStatus & OPENGL_NS_WHITEBACK)
1991 glClearColor (1.0f, 1.0f, 1.0f, 1.0f);
1995 glClearColor (myBgColor.rgb[0],
2001 glClear (GL_COLOR_BUFFER_BIT);
2003 if (theFrameBuffer != NULL)
2004 theFrameBuffer->BindBuffer (myGlContext);
2006 myView->DrawBackground (*this);
2008 // Generate ray-traced image
2009 glMatrixMode (GL_PROJECTION);
2012 glMatrixMode (GL_MODELVIEW);
2015 glEnable (GL_BLEND);
2016 glBlendFunc (GL_ONE, GL_SRC_ALPHA);
2018 if (myIsRaytraceDataValid)
2020 myRaytraceScreenQuad.Bind (myGlContext);
2022 RunRaytraceShaders (theCView,
2029 myRaytraceScreenQuad.Unbind (myGlContext);
2032 if (theFrameBuffer != NULL)
2033 theFrameBuffer->UnbindBuffer (myGlContext);
2040 GetGlContext()->SwapBuffers();
2041 myBackBufferRestored = Standard_False;
2048 return Standard_True;