1 // Created on: 2015-02-20
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2015 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 <Graphic3d_TextureParams.hxx>
18 #include <OpenGl_FrameBuffer.hxx>
19 #include <OpenGl_PrimitiveArray.hxx>
20 #include <OpenGl_VertexBuffer.hxx>
21 #include <OpenGl_View.hxx>
23 #include <OSD_Protection.hxx>
24 #include <OSD_File.hxx>
26 using namespace OpenGl_Raytrace;
28 //! Use this macro to output ray-tracing debug info
29 // #define RAY_TRACE_PRINT_INFO
31 #ifdef RAY_TRACE_PRINT_INFO
32 #include <OSD_Timer.hxx>
35 // =======================================================================
36 // function : UpdateRaytraceGeometry
37 // purpose : Updates 3D scene geometry for ray-tracing
38 // =======================================================================
39 Standard_Boolean OpenGl_View::updateRaytraceGeometry (const RaytraceUpdateMode theMode,
40 const Standard_Integer theViewId,
41 const Handle(OpenGl_Context)& theGlContext)
43 // In 'check' mode (OpenGl_GUM_CHECK) the scene geometry is analyzed for
44 // modifications. This is light-weight procedure performed on each frame
45 if (theMode == OpenGl_GUM_CHECK)
47 if (myLayersModificationStatus != myZLayers.ModificationState())
49 return updateRaytraceGeometry (OpenGl_GUM_PREPARE, theViewId, theGlContext);
52 else if (theMode == OpenGl_GUM_PREPARE)
54 myRaytraceGeometry.ClearMaterials();
56 myArrayToTrianglesMap.clear();
58 myIsRaytraceDataValid = Standard_False;
61 // The set of processed structures (reflected to ray-tracing)
62 // This set is used to remove out-of-date records from the
63 // hash map of structures
64 std::set<const OpenGl_Structure*> anElements;
66 // Set to store all currently visible OpenGL primitive arrays
67 // applicable for ray-tracing
68 std::set<Standard_Size> anArrayIDs;
70 const OpenGl_Layer& aLayer = myZLayers.Layer (Graphic3d_ZLayerId_Default);
72 if (aLayer.NbStructures() != 0)
74 const OpenGl_ArrayOfStructure& aStructArray = aLayer.ArrayOfStructures();
76 for (Standard_Integer anIndex = 0; anIndex < aStructArray.Length(); ++anIndex)
78 for (OpenGl_SequenceOfStructure::Iterator aStructIt (aStructArray (anIndex)); aStructIt.More(); aStructIt.Next())
80 const OpenGl_Structure* aStructure = aStructIt.Value();
82 if (theMode == OpenGl_GUM_CHECK)
84 if (toUpdateStructure (aStructure))
86 return updateRaytraceGeometry (OpenGl_GUM_PREPARE, theViewId, theGlContext);
89 else if (theMode == OpenGl_GUM_PREPARE)
91 if (!aStructure->IsRaytracable() || !aStructure->IsVisible())
95 else if (!aStructure->ViewAffinity.IsNull() && !aStructure->ViewAffinity->IsVisible (theViewId))
100 for (OpenGl_Structure::GroupIterator aGroupIter (aStructure->DrawGroups()); aGroupIter.More(); aGroupIter.Next())
102 // Extract OpenGL elements from the group (primitives arrays)
103 for (const OpenGl_ElementNode* aNode = aGroupIter.Value()->FirstNode(); aNode != NULL; aNode = aNode->next)
105 OpenGl_PrimitiveArray* aPrimArray = dynamic_cast<OpenGl_PrimitiveArray*> (aNode->elem);
107 if (aPrimArray != NULL)
109 anArrayIDs.insert (aPrimArray->GetUID());
114 else if (theMode == OpenGl_GUM_REBUILD)
116 if (!aStructure->IsRaytracable())
120 else if (addRaytraceStructure (aStructure, theGlContext))
122 anElements.insert (aStructure); // structure was processed
129 if (theMode == OpenGl_GUM_PREPARE)
131 BVH_ObjectSet<Standard_ShortReal, 3>::BVH_ObjectList anUnchangedObjects;
133 // Filter out unchanged objects so only their transformations and materials
134 // will be updated (and newly added objects will be processed from scratch)
135 for (Standard_Integer anObjIdx = 0; anObjIdx < myRaytraceGeometry.Size(); ++anObjIdx)
137 OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> (
138 myRaytraceGeometry.Objects().ChangeValue (anObjIdx).operator->());
140 if (aTriangleSet == NULL)
145 if (anArrayIDs.find (aTriangleSet->AssociatedPArrayID()) != anArrayIDs.end())
147 anUnchangedObjects.Append (myRaytraceGeometry.Objects().Value (anObjIdx));
149 myArrayToTrianglesMap[aTriangleSet->AssociatedPArrayID()] = aTriangleSet;
153 myRaytraceGeometry.Objects() = anUnchangedObjects;
155 return updateRaytraceGeometry (OpenGl_GUM_REBUILD, theViewId, theGlContext);
157 else if (theMode == OpenGl_GUM_REBUILD)
159 // Actualize the hash map of structures - remove out-of-date records
160 std::map<const OpenGl_Structure*, Standard_Size>::iterator anIter = myStructureStates.begin();
162 while (anIter != myStructureStates.end())
164 if (anElements.find (anIter->first) == anElements.end())
166 myStructureStates.erase (anIter++);
174 // Actualize OpenGL layer list state
175 myLayersModificationStatus = myZLayers.ModificationState();
177 // Rebuild two-level acceleration structure
178 myRaytraceGeometry.ProcessAcceleration();
180 myRaytraceSceneRadius = 2.f /* scale factor */ * std::max (
181 myRaytraceGeometry.Box().CornerMin().cwiseAbs().maxComp(),
182 myRaytraceGeometry.Box().CornerMax().cwiseAbs().maxComp());
184 const BVH_Vec3f aSize = myRaytraceGeometry.Box().Size();
186 myRaytraceSceneEpsilon = Max (1.0e-6f, 1.0e-4f * aSize.Modulus());
188 return uploadRaytraceData (theGlContext);
191 return Standard_True;
194 // =======================================================================
195 // function : ToUpdateStructure
196 // purpose : Checks to see if the structure is modified
197 // =======================================================================
198 Standard_Boolean OpenGl_View::toUpdateStructure (const OpenGl_Structure* theStructure)
200 if (!theStructure->IsRaytracable())
202 if (theStructure->ModificationState() > 0)
204 theStructure->ResetModificationState();
206 return Standard_True; // ray-trace element was removed - need to rebuild
209 return Standard_False; // did not contain ray-trace elements
212 std::map<const OpenGl_Structure*, Standard_Size>::iterator aStructState = myStructureStates.find (theStructure);
214 if (aStructState != myStructureStates.end())
216 return aStructState->second != theStructure->ModificationState();
219 return Standard_True;
222 // =======================================================================
223 // function : BuildTextureTransform
224 // purpose : Constructs texture transformation matrix
225 // =======================================================================
226 void BuildTextureTransform (const Handle(Graphic3d_TextureParams)& theParams, BVH_Mat4f& theMatrix)
228 theMatrix.InitIdentity();
231 const Graphic3d_Vec2& aScale = theParams->Scale();
233 theMatrix.ChangeValue (0, 0) *= aScale.x();
234 theMatrix.ChangeValue (1, 0) *= aScale.x();
235 theMatrix.ChangeValue (2, 0) *= aScale.x();
236 theMatrix.ChangeValue (3, 0) *= aScale.x();
238 theMatrix.ChangeValue (0, 1) *= aScale.y();
239 theMatrix.ChangeValue (1, 1) *= aScale.y();
240 theMatrix.ChangeValue (2, 1) *= aScale.y();
241 theMatrix.ChangeValue (3, 1) *= aScale.y();
244 const Graphic3d_Vec2 aTrans = -theParams->Translation();
246 theMatrix.ChangeValue (0, 3) = theMatrix.GetValue (0, 0) * aTrans.x() +
247 theMatrix.GetValue (0, 1) * aTrans.y();
249 theMatrix.ChangeValue (1, 3) = theMatrix.GetValue (1, 0) * aTrans.x() +
250 theMatrix.GetValue (1, 1) * aTrans.y();
252 theMatrix.ChangeValue (2, 3) = theMatrix.GetValue (2, 0) * aTrans.x() +
253 theMatrix.GetValue (2, 1) * aTrans.y();
256 const Standard_ShortReal aSin = std::sin (
257 -theParams->Rotation() * static_cast<Standard_ShortReal> (M_PI / 180.0));
258 const Standard_ShortReal aCos = std::cos (
259 -theParams->Rotation() * static_cast<Standard_ShortReal> (M_PI / 180.0));
261 BVH_Mat4f aRotationMat;
262 aRotationMat.SetValue (0, 0, aCos);
263 aRotationMat.SetValue (1, 1, aCos);
264 aRotationMat.SetValue (0, 1, -aSin);
265 aRotationMat.SetValue (1, 0, aSin);
267 theMatrix = theMatrix * aRotationMat;
270 // =======================================================================
271 // function : ConvertMaterial
272 // purpose : Creates ray-tracing material properties
273 // =======================================================================
274 OpenGl_RaytraceMaterial OpenGl_View::convertMaterial (const OpenGl_AspectFace* theAspect,
275 const Handle(OpenGl_Context)& theGlContext)
277 OpenGl_RaytraceMaterial theMaterial;
279 const OPENGL_SURF_PROP& aProperties = theAspect->IntFront();
281 theMaterial.Ambient = BVH_Vec4f (
282 (aProperties.isphysic ? aProperties.ambcol.rgb[0] : aProperties.matcol.rgb[0]) * aProperties.amb,
283 (aProperties.isphysic ? aProperties.ambcol.rgb[1] : aProperties.matcol.rgb[1]) * aProperties.amb,
284 (aProperties.isphysic ? aProperties.ambcol.rgb[2] : aProperties.matcol.rgb[2]) * aProperties.amb,
287 theMaterial.Diffuse = BVH_Vec4f (
288 (aProperties.isphysic ? aProperties.difcol.rgb[0] : aProperties.matcol.rgb[0]) * aProperties.diff,
289 (aProperties.isphysic ? aProperties.difcol.rgb[1] : aProperties.matcol.rgb[1]) * aProperties.diff,
290 (aProperties.isphysic ? aProperties.difcol.rgb[2] : aProperties.matcol.rgb[2]) * aProperties.diff,
291 -1.f /* no texture */);
293 theMaterial.Specular = BVH_Vec4f (
294 (aProperties.isphysic ? aProperties.speccol.rgb[0] : 1.f) * aProperties.spec,
295 (aProperties.isphysic ? aProperties.speccol.rgb[1] : 1.f) * aProperties.spec,
296 (aProperties.isphysic ? aProperties.speccol.rgb[2] : 1.f) * aProperties.spec,
299 theMaterial.Emission = BVH_Vec4f (
300 (aProperties.isphysic ? aProperties.emscol.rgb[0] : aProperties.matcol.rgb[0]) * aProperties.emsv,
301 (aProperties.isphysic ? aProperties.emscol.rgb[1] : aProperties.matcol.rgb[1]) * aProperties.emsv,
302 (aProperties.isphysic ? aProperties.emscol.rgb[2] : aProperties.matcol.rgb[2]) * aProperties.emsv,
305 theMaterial.Transparency = BVH_Vec4f (aProperties.trans,
306 1.f - aProperties.trans,
307 aProperties.index == 0 ? 1.f : aProperties.index,
308 aProperties.index == 0 ? 1.f : 1.f / aProperties.index);
310 const Standard_ShortReal aMaxRefl = Max (theMaterial.Diffuse.x() + theMaterial.Specular.x(),
311 Max (theMaterial.Diffuse.y() + theMaterial.Specular.y(),
312 theMaterial.Diffuse.z() + theMaterial.Specular.z()));
314 const Standard_ShortReal aReflectionScale = 0.75f / aMaxRefl;
316 theMaterial.Reflection = BVH_Vec4f (
317 aProperties.speccol.rgb[0] * aProperties.spec * aReflectionScale,
318 aProperties.speccol.rgb[1] * aProperties.spec * aReflectionScale,
319 aProperties.speccol.rgb[2] * aProperties.spec * aReflectionScale,
322 if (theAspect->DoTextureMap())
324 if (theGlContext->arbTexBindless != NULL)
326 BuildTextureTransform (theAspect->TextureParams(), theMaterial.TextureTransform);
328 // write texture ID in the w-component
329 theMaterial.Diffuse.w() = static_cast<Standard_ShortReal> (
330 myRaytraceGeometry.AddTexture (theAspect->TextureRes (theGlContext)));
332 else if (!myIsRaytraceWarnTextures)
334 const TCollection_ExtendedString aWarnMessage =
335 "Warning: texturing in Ray-Trace requires GL_ARB_bindless_texture extension which is missing. "
336 "Please try to update graphics card driver. At the moment textures will be ignored.";
338 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
339 GL_DEBUG_TYPE_PORTABILITY_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aWarnMessage);
341 myIsRaytraceWarnTextures = Standard_True;
348 // =======================================================================
349 // function : AddRaytraceStructure
350 // purpose : Adds OpenGL structure to ray-traced scene geometry
351 // =======================================================================
352 Standard_Boolean OpenGl_View::addRaytraceStructure (const OpenGl_Structure* theStructure,
353 const Handle(OpenGl_Context)& theGlContext)
355 if (!theStructure->IsVisible())
357 myStructureStates[theStructure] = theStructure->ModificationState();
359 return Standard_True;
362 // Get structure material
363 Standard_Integer aStructMatID = -1;
365 if (theStructure->AspectFace() != NULL)
367 aStructMatID = static_cast<Standard_Integer> (myRaytraceGeometry.Materials.size());
369 OpenGl_RaytraceMaterial aStructMaterial = convertMaterial (theStructure->AspectFace(), theGlContext);
371 myRaytraceGeometry.Materials.push_back (aStructMaterial);
374 Standard_ShortReal aStructTransform[16];
376 if (theStructure->Transformation()->mat != NULL)
378 for (Standard_Integer i = 0; i < 4; ++i)
380 for (Standard_Integer j = 0; j < 4; ++j)
382 aStructTransform[j * 4 + i] = theStructure->Transformation()->mat[i][j];
387 Standard_Boolean aResult = addRaytraceGroups (theStructure, aStructMatID,
388 theStructure->Transformation()->mat ? aStructTransform : NULL, theGlContext);
390 // Process all connected OpenGL structures
391 for (OpenGl_ListOfStructure::Iterator anIts (theStructure->ConnectedStructures()); anIts.More(); anIts.Next())
393 if (anIts.Value()->IsRaytracable())
395 aResult &= addRaytraceGroups (anIts.Value(), aStructMatID,
396 theStructure->Transformation()->mat ? aStructTransform : NULL, theGlContext);
400 myStructureStates[theStructure] = theStructure->ModificationState();
405 // =======================================================================
406 // function : AddRaytraceGroups
407 // purpose : Adds OpenGL groups to ray-traced scene geometry
408 // =======================================================================
409 Standard_Boolean OpenGl_View::addRaytraceGroups (const OpenGl_Structure* theStructure,
410 const Standard_Integer theStructMat,
411 const Standard_ShortReal* theTransform,
412 const Handle(OpenGl_Context)& theGlContext)
414 for (OpenGl_Structure::GroupIterator aGroupIter (theStructure->DrawGroups()); aGroupIter.More(); aGroupIter.Next())
416 // Get group material
417 Standard_Integer aGroupMatID = -1;
418 if (aGroupIter.Value()->AspectFace() != NULL)
420 aGroupMatID = static_cast<Standard_Integer> (myRaytraceGeometry.Materials.size());
422 OpenGl_RaytraceMaterial aGroupMaterial = convertMaterial (
423 aGroupIter.Value()->AspectFace(), theGlContext);
425 myRaytraceGeometry.Materials.push_back (aGroupMaterial);
428 Standard_Integer aMatID = aGroupMatID < 0 ? theStructMat : aGroupMatID;
431 aMatID = static_cast<Standard_Integer> (myRaytraceGeometry.Materials.size());
433 myRaytraceGeometry.Materials.push_back (OpenGl_RaytraceMaterial());
436 // Add OpenGL elements from group (extract primitives arrays and aspects)
437 for (const OpenGl_ElementNode* aNode = aGroupIter.Value()->FirstNode(); aNode != NULL; aNode = aNode->next)
439 OpenGl_AspectFace* anAspect = dynamic_cast<OpenGl_AspectFace*> (aNode->elem);
441 if (anAspect != NULL)
443 aMatID = static_cast<Standard_Integer> (myRaytraceGeometry.Materials.size());
445 OpenGl_RaytraceMaterial aMaterial = convertMaterial (anAspect, theGlContext);
447 myRaytraceGeometry.Materials.push_back (aMaterial);
451 OpenGl_PrimitiveArray* aPrimArray = dynamic_cast<OpenGl_PrimitiveArray*> (aNode->elem);
453 if (aPrimArray != NULL)
455 std::map<Standard_Size, OpenGl_TriangleSet*>::iterator aSetIter = myArrayToTrianglesMap.find (aPrimArray->GetUID());
457 if (aSetIter != myArrayToTrianglesMap.end())
459 OpenGl_TriangleSet* aSet = aSetIter->second;
461 BVH_Transform<Standard_ShortReal, 4>* aTransform = new BVH_Transform<Standard_ShortReal, 4>();
463 if (theTransform != NULL)
465 aTransform->SetTransform (*(reinterpret_cast<const BVH_Mat4f*> (theTransform)));
468 aSet->SetProperties (aTransform);
470 if (aSet->MaterialIndex() != OpenGl_TriangleSet::INVALID_MATERIAL && aSet->MaterialIndex() != aMatID)
472 aSet->SetMaterialIndex (aMatID);
477 NCollection_Handle<BVH_Object<Standard_ShortReal, 3> > aSet =
478 addRaytracePrimitiveArray (aPrimArray, aMatID, 0);
482 BVH_Transform<Standard_ShortReal, 4>* aTransform = new BVH_Transform<Standard_ShortReal, 4>;
484 if (theTransform != NULL)
486 aTransform->SetTransform (*(reinterpret_cast<const BVH_Mat4f*> (theTransform)));
489 aSet->SetProperties (aTransform);
491 myRaytraceGeometry.Objects().Append (aSet);
499 return Standard_True;
502 // =======================================================================
503 // function : AddRaytracePrimitiveArray
504 // purpose : Adds OpenGL primitive array to ray-traced scene geometry
505 // =======================================================================
506 OpenGl_TriangleSet* OpenGl_View::addRaytracePrimitiveArray (const OpenGl_PrimitiveArray* theArray,
507 const Standard_Integer theMaterial,
508 const OpenGl_Mat4* theTransform)
510 const Handle(Graphic3d_BoundBuffer)& aBounds = theArray->Bounds();
511 const Handle(Graphic3d_IndexBuffer)& anIndices = theArray->Indices();
512 const Handle(Graphic3d_Buffer)& anAttribs = theArray->Attributes();
514 if (theArray->DrawMode() < GL_TRIANGLES
515 #ifndef GL_ES_VERSION_2_0
516 || theArray->DrawMode() > GL_POLYGON
518 || theArray->DrawMode() > GL_TRIANGLE_FAN
520 || anAttribs.IsNull())
525 OpenGl_Mat4 aNormalMatrix;
527 if (theTransform != NULL)
529 Standard_ASSERT_RETURN (theTransform->Inverted (aNormalMatrix),
530 "Error: Failed to compute normal transformation matrix", NULL);
532 aNormalMatrix.Transpose();
535 OpenGl_TriangleSet* aSet = new OpenGl_TriangleSet (theArray->GetUID());
537 aSet->Vertices.reserve (anAttribs->NbElements);
538 aSet->Normals.reserve (anAttribs->NbElements);
539 aSet->TexCrds.reserve (anAttribs->NbElements);
541 const size_t aVertFrom = aSet->Vertices.size();
543 for (Standard_Integer anAttribIter = 0; anAttribIter < anAttribs->NbAttributes; ++anAttribIter)
545 const Graphic3d_Attribute& anAttrib = anAttribs->Attribute (anAttribIter);
546 const size_t anOffset = anAttribs->AttributeOffset (anAttribIter);
547 if (anAttrib.Id == Graphic3d_TOA_POS)
549 if (anAttrib.DataType == Graphic3d_TOD_VEC3
550 || anAttrib.DataType == Graphic3d_TOD_VEC4)
552 for (Standard_Integer aVertIter = 0; aVertIter < anAttribs->NbElements; ++aVertIter)
554 aSet->Vertices.push_back (
555 *reinterpret_cast<const Graphic3d_Vec3*> (anAttribs->value (aVertIter) + anOffset));
558 else if (anAttrib.DataType == Graphic3d_TOD_VEC2)
560 for (Standard_Integer aVertIter = 0; aVertIter < anAttribs->NbElements; ++aVertIter)
562 const Standard_ShortReal* aCoords =
563 reinterpret_cast<const Standard_ShortReal*> (anAttribs->value (aVertIter) + anOffset);
565 aSet->Vertices.push_back (BVH_Vec3f (aCoords[0], aCoords[1], 0.0f));
569 else if (anAttrib.Id == Graphic3d_TOA_NORM)
571 if (anAttrib.DataType == Graphic3d_TOD_VEC3
572 || anAttrib.DataType == Graphic3d_TOD_VEC4)
574 for (Standard_Integer aVertIter = 0; aVertIter < anAttribs->NbElements; ++aVertIter)
576 aSet->Normals.push_back (
577 *reinterpret_cast<const Graphic3d_Vec3*> (anAttribs->value (aVertIter) + anOffset));
581 else if (anAttrib.Id == Graphic3d_TOA_UV)
583 if (anAttrib.DataType == Graphic3d_TOD_VEC2)
585 for (Standard_Integer aVertIter = 0; aVertIter < anAttribs->NbElements; ++aVertIter)
587 aSet->TexCrds.push_back (
588 *reinterpret_cast<const Graphic3d_Vec2*> (anAttribs->value (aVertIter) + anOffset));
594 if (aSet->Normals.size() != aSet->Vertices.size())
596 for (Standard_Integer aVertIter = 0; aVertIter < anAttribs->NbElements; ++aVertIter)
598 aSet->Normals.push_back (BVH_Vec3f());
602 if (aSet->TexCrds.size() != aSet->Vertices.size())
604 for (Standard_Integer aVertIter = 0; aVertIter < anAttribs->NbElements; ++aVertIter)
606 aSet->TexCrds.push_back (BVH_Vec2f());
610 if (theTransform != NULL)
612 for (size_t aVertIter = aVertFrom; aVertIter < aSet->Vertices.size(); ++aVertIter)
614 BVH_Vec3f& aVertex = aSet->Vertices[aVertIter];
616 BVH_Vec4f aTransVertex = *theTransform *
617 BVH_Vec4f (aVertex.x(), aVertex.y(), aVertex.z(), 1.f);
619 aVertex = BVH_Vec3f (aTransVertex.x(), aTransVertex.y(), aTransVertex.z());
621 for (size_t aVertIter = aVertFrom; aVertIter < aSet->Normals.size(); ++aVertIter)
623 BVH_Vec3f& aNormal = aSet->Normals[aVertIter];
625 BVH_Vec4f aTransNormal = aNormalMatrix *
626 BVH_Vec4f (aNormal.x(), aNormal.y(), aNormal.z(), 0.f);
628 aNormal = BVH_Vec3f (aTransNormal.x(), aTransNormal.y(), aTransNormal.z());
632 if (!aBounds.IsNull())
634 for (Standard_Integer aBound = 0, aBoundStart = 0; aBound < aBounds->NbBounds; ++aBound)
636 const Standard_Integer aVertNum = aBounds->Bounds[aBound];
638 if (!addRaytraceVertexIndices (*aSet, theMaterial, aVertNum, aBoundStart, *theArray))
644 aBoundStart += aVertNum;
649 const Standard_Integer aVertNum = !anIndices.IsNull() ? anIndices->NbElements : anAttribs->NbElements;
651 if (!addRaytraceVertexIndices (*aSet, theMaterial, aVertNum, 0, *theArray))
659 if (aSet->Size() != 0)
667 // =======================================================================
668 // function : AddRaytraceVertexIndices
669 // purpose : Adds vertex indices to ray-traced scene geometry
670 // =======================================================================
671 Standard_Boolean OpenGl_View::addRaytraceVertexIndices (OpenGl_TriangleSet& theSet,
672 const Standard_Integer theMatID,
673 const Standard_Integer theCount,
674 const Standard_Integer theOffset,
675 const OpenGl_PrimitiveArray& theArray)
677 switch (theArray.DrawMode())
679 case GL_TRIANGLES: return addRaytraceTriangleArray (theSet, theMatID, theCount, theOffset, theArray.Indices());
680 case GL_TRIANGLE_FAN: return addRaytraceTriangleFanArray (theSet, theMatID, theCount, theOffset, theArray.Indices());
681 case GL_TRIANGLE_STRIP: return addRaytraceTriangleStripArray (theSet, theMatID, theCount, theOffset, theArray.Indices());
682 #if !defined(GL_ES_VERSION_2_0)
683 case GL_QUAD_STRIP: return addRaytraceQuadrangleStripArray (theSet, theMatID, theCount, theOffset, theArray.Indices());
684 case GL_QUADS: return addRaytraceQuadrangleArray (theSet, theMatID, theCount, theOffset, theArray.Indices());
685 case GL_POLYGON: return addRaytracePolygonArray (theSet, theMatID, theCount, theOffset, theArray.Indices());
689 return Standard_False;
692 // =======================================================================
693 // function : AddRaytraceTriangleArray
694 // purpose : Adds OpenGL triangle array to ray-traced scene geometry
695 // =======================================================================
696 Standard_Boolean OpenGl_View::addRaytraceTriangleArray (OpenGl_TriangleSet& theSet,
697 const Standard_Integer theMatID,
698 const Standard_Integer theCount,
699 const Standard_Integer theOffset,
700 const Handle(Graphic3d_IndexBuffer)& theIndices)
704 return Standard_True;
707 theSet.Elements.reserve (theSet.Elements.size() + theCount / 3);
709 if (!theIndices.IsNull())
711 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; aVert += 3)
713 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (aVert + 0),
714 theIndices->Index (aVert + 1),
715 theIndices->Index (aVert + 2),
721 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; aVert += 3)
723 theSet.Elements.push_back (BVH_Vec4i (aVert + 0, aVert + 1, aVert + 2, theMatID));
727 return Standard_True;
730 // =======================================================================
731 // function : AddRaytraceTriangleFanArray
732 // purpose : Adds OpenGL triangle fan array to ray-traced scene geometry
733 // =======================================================================
734 Standard_Boolean OpenGl_View::addRaytraceTriangleFanArray (OpenGl_TriangleSet& theSet,
735 const Standard_Integer theMatID,
736 const Standard_Integer theCount,
737 const Standard_Integer theOffset,
738 const Handle(Graphic3d_IndexBuffer)& theIndices)
742 return Standard_True;
745 theSet.Elements.reserve (theSet.Elements.size() + theCount - 2);
747 if (!theIndices.IsNull())
749 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; ++aVert)
751 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (theOffset),
752 theIndices->Index (aVert + 1),
753 theIndices->Index (aVert + 2),
759 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; ++aVert)
761 theSet.Elements.push_back (BVH_Vec4i (theOffset,
768 return Standard_True;
771 // =======================================================================
772 // function : AddRaytraceTriangleStripArray
773 // purpose : Adds OpenGL triangle strip array to ray-traced scene geometry
774 // =======================================================================
775 Standard_Boolean OpenGl_View::addRaytraceTriangleStripArray (OpenGl_TriangleSet& theSet,
776 const Standard_Integer theMatID,
777 const Standard_Integer theCount,
778 const Standard_Integer theOffset,
779 const Handle(Graphic3d_IndexBuffer)& theIndices)
783 return Standard_True;
786 theSet.Elements.reserve (theSet.Elements.size() + theCount - 2);
788 if (!theIndices.IsNull())
790 for (Standard_Integer aVert = theOffset, aCW = 0; aVert < theOffset + theCount - 2; ++aVert, aCW = (aCW + 1) % 2)
792 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (aVert + aCW ? 1 : 0),
793 theIndices->Index (aVert + aCW ? 0 : 1),
794 theIndices->Index (aVert + 2),
800 for (Standard_Integer aVert = theOffset, aCW = 0; aVert < theOffset + theCount - 2; ++aVert, aCW = (aCW + 1) % 2)
802 theSet.Elements.push_back (BVH_Vec4i (aVert + aCW ? 1 : 0,
809 return Standard_True;
812 // =======================================================================
813 // function : AddRaytraceQuadrangleArray
814 // purpose : Adds OpenGL quad array to ray-traced scene geometry
815 // =======================================================================
816 Standard_Boolean OpenGl_View::addRaytraceQuadrangleArray (OpenGl_TriangleSet& theSet,
817 const Standard_Integer theMatID,
818 const Standard_Integer theCount,
819 const Standard_Integer theOffset,
820 const Handle(Graphic3d_IndexBuffer)& theIndices)
824 return Standard_True;
827 theSet.Elements.reserve (theSet.Elements.size() + theCount / 2);
829 if (!theIndices.IsNull())
831 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 3; aVert += 4)
833 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (aVert + 0),
834 theIndices->Index (aVert + 1),
835 theIndices->Index (aVert + 2),
837 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (aVert + 0),
838 theIndices->Index (aVert + 2),
839 theIndices->Index (aVert + 3),
845 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 3; aVert += 4)
847 theSet.Elements.push_back (BVH_Vec4i (aVert + 0, aVert + 1, aVert + 2,
849 theSet.Elements.push_back (BVH_Vec4i (aVert + 0, aVert + 2, aVert + 3,
854 return Standard_True;
857 // =======================================================================
858 // function : AddRaytraceQuadrangleStripArray
859 // purpose : Adds OpenGL quad strip array to ray-traced scene geometry
860 // =======================================================================
861 Standard_Boolean OpenGl_View::addRaytraceQuadrangleStripArray (OpenGl_TriangleSet& theSet,
862 const Standard_Integer theMatID,
863 const Standard_Integer theCount,
864 const Standard_Integer theOffset,
865 const Handle(Graphic3d_IndexBuffer)& theIndices)
869 return Standard_True;
872 theSet.Elements.reserve (theSet.Elements.size() + 2 * theCount - 6);
874 if (!theIndices.IsNull())
876 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 3; aVert += 2)
878 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (aVert + 0),
879 theIndices->Index (aVert + 1),
880 theIndices->Index (aVert + 2),
883 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (aVert + 1),
884 theIndices->Index (aVert + 3),
885 theIndices->Index (aVert + 2),
891 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 3; aVert += 2)
893 theSet.Elements.push_back (BVH_Vec4i (aVert + 0,
898 theSet.Elements.push_back (BVH_Vec4i (aVert + 1,
905 return Standard_True;
908 // =======================================================================
909 // function : AddRaytracePolygonArray
910 // purpose : Adds OpenGL polygon array to ray-traced scene geometry
911 // =======================================================================
912 Standard_Boolean OpenGl_View::addRaytracePolygonArray (OpenGl_TriangleSet& theSet,
913 const Standard_Integer theMatID,
914 const Standard_Integer theCount,
915 const Standard_Integer theOffset,
916 const Handle(Graphic3d_IndexBuffer)& theIndices)
920 return Standard_True;
923 theSet.Elements.reserve (theSet.Elements.size() + theCount - 2);
925 if (!theIndices.IsNull())
927 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; ++aVert)
929 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (theOffset),
930 theIndices->Index (aVert + 1),
931 theIndices->Index (aVert + 2),
937 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; ++aVert)
939 theSet.Elements.push_back (BVH_Vec4i (theOffset,
946 return Standard_True;
949 // =======================================================================
951 // purpose : Returns shader source combined with prefix
952 // =======================================================================
953 TCollection_AsciiString OpenGl_View::ShaderSource::Source() const
955 static const TCollection_AsciiString aVersion = "#version 140";
957 if (myPrefix.IsEmpty())
959 return aVersion + "\n" + mySource;
962 return aVersion + "\n" + myPrefix + "\n" + mySource;
965 // =======================================================================
967 // purpose : Loads shader source from specified files
968 // =======================================================================
969 void OpenGl_View::ShaderSource::Load (const TCollection_AsciiString* theFileNames,
970 const Standard_Integer theCount)
974 for (Standard_Integer anIndex = 0; anIndex < theCount; ++anIndex)
976 OSD_File aFile (theFileNames[anIndex]);
978 Standard_ASSERT_RETURN (aFile.Exists(),
979 "Error: Failed to find shader source file", /* none */);
981 aFile.Open (OSD_ReadOnly, OSD_Protection());
983 TCollection_AsciiString aSource;
985 Standard_ASSERT_RETURN (aFile.IsOpen(),
986 "Error: Failed to open shader source file", /* none */);
988 aFile.Read (aSource, (Standard_Integer) aFile.Size());
990 if (!aSource.IsEmpty())
992 mySource += TCollection_AsciiString ("\n") + aSource;
999 // =======================================================================
1000 // function : GenerateShaderPrefix
1001 // purpose : Generates shader prefix based on current ray-tracing options
1002 // =======================================================================
1003 TCollection_AsciiString OpenGl_View::generateShaderPrefix (const Handle(OpenGl_Context)& theGlContext) const
1005 TCollection_AsciiString aPrefixString =
1006 TCollection_AsciiString ("#define STACK_SIZE ") + TCollection_AsciiString (myRaytraceParameters.StackSize) + "\n" +
1007 TCollection_AsciiString ("#define NB_BOUNCES ") + TCollection_AsciiString (myRaytraceParameters.NbBounces);
1009 if (myRaytraceParameters.TransparentShadows)
1011 aPrefixString += TCollection_AsciiString ("\n#define TRANSPARENT_SHADOWS");
1014 // If OpenGL driver supports bindless textures,
1015 // activate texturing in ray-tracing mode
1016 if (theGlContext->arbTexBindless != NULL)
1018 aPrefixString += TCollection_AsciiString ("\n#define USE_TEXTURES") +
1019 TCollection_AsciiString ("\n#define MAX_TEX_NUMBER ") + TCollection_AsciiString (OpenGl_RaytraceGeometry::MAX_TEX_NUMBER);
1022 return aPrefixString;
1025 // =======================================================================
1026 // function : SafeFailBack
1027 // purpose : Performs safe exit when shaders initialization fails
1028 // =======================================================================
1029 Standard_Boolean OpenGl_View::safeFailBack (const TCollection_ExtendedString& theMessage,
1030 const Handle(OpenGl_Context)& theGlContext)
1032 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1033 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, theMessage);
1035 myRaytraceInitStatus = OpenGl_RT_FAIL;
1037 releaseRaytraceResources (theGlContext);
1039 return Standard_False;
1042 // =======================================================================
1043 // function : InitShader
1044 // purpose : Creates new shader object with specified source
1045 // =======================================================================
1046 Handle(OpenGl_ShaderObject) OpenGl_View::initShader (const GLenum theType,
1047 const ShaderSource& theSource,
1048 const Handle(OpenGl_Context)& theGlContext)
1050 Handle(OpenGl_ShaderObject) aShader = new OpenGl_ShaderObject (theType);
1052 if (!aShader->Create (theGlContext))
1054 const TCollection_ExtendedString aMessage = TCollection_ExtendedString ("Error: Failed to create ") +
1055 (theType == GL_VERTEX_SHADER ? "vertex" : "fragment") + " shader object";
1057 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1058 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
1060 aShader->Release (theGlContext.operator->());
1062 return Handle(OpenGl_ShaderObject)();
1065 if (!aShader->LoadSource (theGlContext, theSource.Source()))
1067 const TCollection_ExtendedString aMessage = TCollection_ExtendedString ("Error: Failed to set ") +
1068 (theType == GL_VERTEX_SHADER ? "vertex" : "fragment") + " shader source";
1070 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1071 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
1073 aShader->Release (theGlContext.operator->());
1075 return Handle(OpenGl_ShaderObject)();
1078 TCollection_AsciiString aBuildLog;
1080 if (!aShader->Compile (theGlContext))
1082 aShader->FetchInfoLog (theGlContext, aBuildLog);
1084 const TCollection_ExtendedString aMessage = TCollection_ExtendedString ("Error: Failed to compile ") +
1085 (theType == GL_VERTEX_SHADER ? "vertex" : "fragment") + " shader object:\n" + aBuildLog;
1087 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1088 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
1090 aShader->Release (theGlContext.operator->());
1092 return Handle(OpenGl_ShaderObject)();
1094 else if (theGlContext->caps->glslWarnings)
1096 aShader->FetchInfoLog (theGlContext, aBuildLog);
1098 if (!aBuildLog.IsEmpty() && !aBuildLog.IsEqual ("No errors.\n"))
1100 const TCollection_ExtendedString aMessage = TCollection_ExtendedString (theType == GL_VERTEX_SHADER ?
1101 "Vertex" : "Fragment") + " shader was compiled with following warnings:\n" + aBuildLog;
1103 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1104 GL_DEBUG_TYPE_PORTABILITY_ARB, 0, GL_DEBUG_SEVERITY_LOW_ARB, aMessage);
1111 // =======================================================================
1112 // function : InitRaytraceResources
1113 // purpose : Initializes OpenGL/GLSL shader programs
1114 // =======================================================================
1115 Standard_Boolean OpenGl_View::initRaytraceResources (const Graphic3d_CView& theCView, const Handle(OpenGl_Context)& theGlContext)
1117 if (myRaytraceInitStatus == OpenGl_RT_FAIL)
1119 return Standard_False;
1122 Standard_Boolean aToRebuildShaders = Standard_False;
1124 if (myRaytraceInitStatus == OpenGl_RT_INIT)
1126 if (!myIsRaytraceDataValid)
1127 return Standard_True;
1129 const Standard_Integer aRequiredStackSize =
1130 myRaytraceGeometry.HighLevelTreeDepth() + myRaytraceGeometry.BottomLevelTreeDepth();
1132 if (myRaytraceParameters.StackSize < aRequiredStackSize)
1134 myRaytraceParameters.StackSize = Max (aRequiredStackSize, THE_DEFAULT_STACK_SIZE);
1136 aToRebuildShaders = Standard_True;
1140 if (aRequiredStackSize < myRaytraceParameters.StackSize)
1142 if (myRaytraceParameters.StackSize > THE_DEFAULT_STACK_SIZE)
1144 myRaytraceParameters.StackSize = Max (aRequiredStackSize, THE_DEFAULT_STACK_SIZE);
1145 aToRebuildShaders = Standard_True;
1150 if (theCView.RenderParams.RaytracingDepth != myRaytraceParameters.NbBounces)
1152 myRaytraceParameters.NbBounces = theCView.RenderParams.RaytracingDepth;
1153 aToRebuildShaders = Standard_True;
1156 if (theCView.RenderParams.IsTransparentShadowEnabled != myRaytraceParameters.TransparentShadows)
1158 myRaytraceParameters.TransparentShadows = theCView.RenderParams.IsTransparentShadowEnabled;
1159 aToRebuildShaders = Standard_True;
1162 if (aToRebuildShaders)
1164 #ifdef RAY_TRACE_PRINT_INFO
1165 std::cout << "Info: Rebuild shaders with stack size: " << myRaytraceParameters.StackSize << std::endl;
1168 // Change state to force update all uniforms
1169 myToUpdateEnvironmentMap = Standard_True;
1171 TCollection_AsciiString aPrefixString = generateShaderPrefix (theGlContext);
1173 #ifdef RAY_TRACE_PRINT_INFO
1174 std::cout << "GLSL prefix string:" << std::endl << aPrefixString << std::endl;
1177 myRaytraceShaderSource.SetPrefix (aPrefixString);
1178 myPostFSAAShaderSource.SetPrefix (aPrefixString);
1180 if (!myRaytraceShader->LoadSource (theGlContext, myRaytraceShaderSource.Source())
1181 || !myPostFSAAShader->LoadSource (theGlContext, myPostFSAAShaderSource.Source()))
1183 return safeFailBack ("Failed to load source into ray-tracing fragment shaders", theGlContext);
1186 if (!myRaytraceShader->Compile (theGlContext)
1187 || !myPostFSAAShader->Compile (theGlContext))
1189 return safeFailBack ("Failed to compile ray-tracing fragment shaders", theGlContext);
1192 myRaytraceProgram->SetAttributeName (theGlContext, Graphic3d_TOA_POS, "occVertex");
1193 myPostFSAAProgram->SetAttributeName (theGlContext, Graphic3d_TOA_POS, "occVertex");
1194 if (!myRaytraceProgram->Link (theGlContext)
1195 || !myPostFSAAProgram->Link (theGlContext))
1197 return safeFailBack ("Failed to initialize vertex attributes for ray-tracing program", theGlContext);
1202 if (myRaytraceInitStatus == OpenGl_RT_NONE)
1204 if (!theGlContext->IsGlGreaterEqual (3, 1))
1206 return safeFailBack ("Ray-tracing requires OpenGL 3.1 and higher", theGlContext);
1208 else if (!theGlContext->arbTboRGB32)
1210 return safeFailBack ("Ray-tracing requires OpenGL 4.0+ or GL_ARB_texture_buffer_object_rgb32 extension", theGlContext);
1212 else if (!theGlContext->arbFBOBlit)
1214 return safeFailBack ("Ray-tracing requires EXT_framebuffer_blit extension", theGlContext);
1217 myRaytraceParameters.NbBounces = theCView.RenderParams.RaytracingDepth;
1219 TCollection_AsciiString aFolder = Graphic3d_ShaderProgram::ShadersFolder();
1221 if (aFolder.IsEmpty())
1223 return safeFailBack ("Failed to locate shaders directory", theGlContext);
1226 if (myIsRaytraceDataValid)
1228 myRaytraceParameters.StackSize = Max (THE_DEFAULT_STACK_SIZE,
1229 myRaytraceGeometry.HighLevelTreeDepth() + myRaytraceGeometry.BottomLevelTreeDepth());
1232 TCollection_AsciiString aPrefixString = generateShaderPrefix (theGlContext);
1234 #ifdef RAY_TRACE_PRINT_INFO
1235 std::cout << "GLSL prefix string:" << std::endl << aPrefixString << std::endl;
1239 Handle(OpenGl_ShaderObject) aBasicVertShader = initShader (
1240 GL_VERTEX_SHADER, ShaderSource (aFolder + "/RaytraceBase.vs"), theGlContext);
1242 if (aBasicVertShader.IsNull())
1244 return safeFailBack ("Failed to initialize ray-trace vertex shader", theGlContext);
1247 TCollection_AsciiString aFiles[] = { aFolder + "/RaytraceBase.fs",
1248 aFolder + "/RaytraceRender.fs" };
1250 myRaytraceShaderSource.Load (aFiles, 2);
1252 myRaytraceShaderSource.SetPrefix (aPrefixString);
1254 myRaytraceShader = initShader (GL_FRAGMENT_SHADER, myRaytraceShaderSource, theGlContext);
1256 if (myRaytraceShader.IsNull())
1258 aBasicVertShader->Release (theGlContext.operator->());
1260 return safeFailBack ("Failed to initialize ray-trace fragment shader", theGlContext);
1263 myRaytraceProgram = new OpenGl_ShaderProgram;
1265 if (!myRaytraceProgram->Create (theGlContext))
1267 aBasicVertShader->Release (theGlContext.operator->());
1269 return safeFailBack ("Failed to create ray-trace shader program", theGlContext);
1272 if (!myRaytraceProgram->AttachShader (theGlContext, aBasicVertShader)
1273 || !myRaytraceProgram->AttachShader (theGlContext, myRaytraceShader))
1275 aBasicVertShader->Release (theGlContext.operator->());
1277 return safeFailBack ("Failed to attach ray-trace shader objects", theGlContext);
1280 myRaytraceProgram->SetAttributeName (theGlContext, Graphic3d_TOA_POS, "occVertex");
1282 TCollection_AsciiString aLinkLog;
1284 if (!myRaytraceProgram->Link (theGlContext))
1286 myRaytraceProgram->FetchInfoLog (theGlContext, aLinkLog);
1288 return safeFailBack (TCollection_ExtendedString (
1289 "Failed to link ray-trace shader program:\n") + aLinkLog, theGlContext);
1291 else if (theGlContext->caps->glslWarnings)
1293 myRaytraceProgram->FetchInfoLog (theGlContext, aLinkLog);
1295 if (!aLinkLog.IsEmpty() && !aLinkLog.IsEqual ("No errors.\n"))
1297 const TCollection_ExtendedString aMessage = TCollection_ExtendedString (
1298 "Ray-trace shader program was linked with following warnings:\n") + aLinkLog;
1300 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1301 GL_DEBUG_TYPE_PORTABILITY_ARB, 0, GL_DEBUG_SEVERITY_LOW_ARB, aMessage);
1307 Handle(OpenGl_ShaderObject) aBasicVertShader = initShader (
1308 GL_VERTEX_SHADER, ShaderSource (aFolder + "/RaytraceBase.vs"), theGlContext);
1310 if (aBasicVertShader.IsNull())
1312 return safeFailBack ("Failed to initialize FSAA vertex shader", theGlContext);
1315 TCollection_AsciiString aFiles[] = { aFolder + "/RaytraceBase.fs",
1316 aFolder + "/RaytraceSmooth.fs" };
1318 myPostFSAAShaderSource.Load (aFiles, 2);
1320 myPostFSAAShaderSource.SetPrefix (aPrefixString);
1322 myPostFSAAShader = initShader (GL_FRAGMENT_SHADER, myPostFSAAShaderSource, theGlContext);
1324 if (myPostFSAAShader.IsNull())
1326 aBasicVertShader->Release (theGlContext.operator->());
1328 return safeFailBack ("Failed to initialize FSAA fragment shader", theGlContext);
1331 myPostFSAAProgram = new OpenGl_ShaderProgram;
1333 if (!myPostFSAAProgram->Create (theGlContext))
1335 aBasicVertShader->Release (theGlContext.operator->());
1337 return safeFailBack ("Failed to create FSAA shader program", theGlContext);
1340 if (!myPostFSAAProgram->AttachShader (theGlContext, aBasicVertShader)
1341 || !myPostFSAAProgram->AttachShader (theGlContext, myPostFSAAShader))
1343 aBasicVertShader->Release (theGlContext.operator->());
1345 return safeFailBack ("Failed to attach FSAA shader objects", theGlContext);
1348 myPostFSAAProgram->SetAttributeName (theGlContext, Graphic3d_TOA_POS, "occVertex");
1350 TCollection_AsciiString aLinkLog;
1352 if (!myPostFSAAProgram->Link (theGlContext))
1354 myPostFSAAProgram->FetchInfoLog (theGlContext, aLinkLog);
1356 return safeFailBack (TCollection_ExtendedString (
1357 "Failed to link FSAA shader program:\n") + aLinkLog, theGlContext);
1359 else if (theGlContext->caps->glslWarnings)
1361 myPostFSAAProgram->FetchInfoLog (theGlContext, aLinkLog);
1363 if (!aLinkLog.IsEmpty() && !aLinkLog.IsEqual ("No errors.\n"))
1365 const TCollection_ExtendedString aMessage = TCollection_ExtendedString (
1366 "FSAA shader program was linked with following warnings:\n") + aLinkLog;
1368 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1369 GL_DEBUG_TYPE_PORTABILITY_ARB, 0, GL_DEBUG_SEVERITY_LOW_ARB, aMessage);
1375 if (myRaytraceInitStatus == OpenGl_RT_NONE || aToRebuildShaders)
1377 for (Standard_Integer anIndex = 0; anIndex < 2; ++anIndex)
1379 Handle(OpenGl_ShaderProgram)& aShaderProgram =
1380 (anIndex == 0) ? myRaytraceProgram : myPostFSAAProgram;
1382 theGlContext->BindProgram (aShaderProgram);
1384 aShaderProgram->SetSampler (theGlContext,
1385 "uSceneMinPointTexture", OpenGl_RT_SceneMinPointTexture);
1386 aShaderProgram->SetSampler (theGlContext,
1387 "uSceneMaxPointTexture", OpenGl_RT_SceneMaxPointTexture);
1388 aShaderProgram->SetSampler (theGlContext,
1389 "uSceneNodeInfoTexture", OpenGl_RT_SceneNodeInfoTexture);
1390 aShaderProgram->SetSampler (theGlContext,
1391 "uGeometryVertexTexture", OpenGl_RT_GeometryVertexTexture);
1392 aShaderProgram->SetSampler (theGlContext,
1393 "uGeometryNormalTexture", OpenGl_RT_GeometryNormalTexture);
1394 aShaderProgram->SetSampler (theGlContext,
1395 "uGeometryTexCrdTexture", OpenGl_RT_GeometryTexCrdTexture);
1396 aShaderProgram->SetSampler (theGlContext,
1397 "uGeometryTriangTexture", OpenGl_RT_GeometryTriangTexture);
1398 aShaderProgram->SetSampler (theGlContext,
1399 "uSceneTransformTexture", OpenGl_RT_SceneTransformTexture);
1400 aShaderProgram->SetSampler (theGlContext,
1401 "uEnvironmentMapTexture", OpenGl_RT_EnvironmentMapTexture);
1402 aShaderProgram->SetSampler (theGlContext,
1403 "uRaytraceMaterialTexture", OpenGl_RT_RaytraceMaterialTexture);
1404 aShaderProgram->SetSampler (theGlContext,
1405 "uRaytraceLightSrcTexture", OpenGl_RT_RaytraceLightSrcTexture);
1407 aShaderProgram->SetSampler (theGlContext,
1408 "uOpenGlColorTexture", OpenGl_RT_OpenGlColorTexture);
1409 aShaderProgram->SetSampler (theGlContext,
1410 "uOpenGlDepthTexture", OpenGl_RT_OpenGlDepthTexture);
1414 aShaderProgram->SetSampler (theGlContext,
1415 "uFSAAInputTexture", OpenGl_RT_FSAAInputTexture);
1418 myUniformLocations[anIndex][OpenGl_RT_aPosition] =
1419 aShaderProgram->GetAttributeLocation (theGlContext, "occVertex");
1421 myUniformLocations[anIndex][OpenGl_RT_uOriginLB] =
1422 aShaderProgram->GetUniformLocation (theGlContext, "uOriginLB");
1423 myUniformLocations[anIndex][OpenGl_RT_uOriginRB] =
1424 aShaderProgram->GetUniformLocation (theGlContext, "uOriginRB");
1425 myUniformLocations[anIndex][OpenGl_RT_uOriginLT] =
1426 aShaderProgram->GetUniformLocation (theGlContext, "uOriginLT");
1427 myUniformLocations[anIndex][OpenGl_RT_uOriginRT] =
1428 aShaderProgram->GetUniformLocation (theGlContext, "uOriginRT");
1429 myUniformLocations[anIndex][OpenGl_RT_uDirectLB] =
1430 aShaderProgram->GetUniformLocation (theGlContext, "uDirectLB");
1431 myUniformLocations[anIndex][OpenGl_RT_uDirectRB] =
1432 aShaderProgram->GetUniformLocation (theGlContext, "uDirectRB");
1433 myUniformLocations[anIndex][OpenGl_RT_uDirectLT] =
1434 aShaderProgram->GetUniformLocation (theGlContext, "uDirectLT");
1435 myUniformLocations[anIndex][OpenGl_RT_uDirectRT] =
1436 aShaderProgram->GetUniformLocation (theGlContext, "uDirectRT");
1437 myUniformLocations[anIndex][OpenGl_RT_uUnviewMat] =
1438 aShaderProgram->GetUniformLocation (theGlContext, "uUnviewMat");
1440 myUniformLocations[anIndex][OpenGl_RT_uSceneRad] =
1441 aShaderProgram->GetUniformLocation (theGlContext, "uSceneRadius");
1442 myUniformLocations[anIndex][OpenGl_RT_uSceneEps] =
1443 aShaderProgram->GetUniformLocation (theGlContext, "uSceneEpsilon");
1444 myUniformLocations[anIndex][OpenGl_RT_uLightCount] =
1445 aShaderProgram->GetUniformLocation (theGlContext, "uLightCount");
1446 myUniformLocations[anIndex][OpenGl_RT_uLightAmbnt] =
1447 aShaderProgram->GetUniformLocation (theGlContext, "uGlobalAmbient");
1449 myUniformLocations[anIndex][OpenGl_RT_uOffsetX] =
1450 aShaderProgram->GetUniformLocation (theGlContext, "uOffsetX");
1451 myUniformLocations[anIndex][OpenGl_RT_uOffsetY] =
1452 aShaderProgram->GetUniformLocation (theGlContext, "uOffsetY");
1453 myUniformLocations[anIndex][OpenGl_RT_uSamples] =
1454 aShaderProgram->GetUniformLocation (theGlContext, "uSamples");
1455 myUniformLocations[anIndex][OpenGl_RT_uWinSizeX] =
1456 aShaderProgram->GetUniformLocation (theGlContext, "uWinSizeX");
1457 myUniformLocations[anIndex][OpenGl_RT_uWinSizeY] =
1458 aShaderProgram->GetUniformLocation (theGlContext, "uWinSizeY");
1460 myUniformLocations[anIndex][OpenGl_RT_uTextures] =
1461 aShaderProgram->GetUniformLocation (theGlContext, "uTextureSamplers");
1463 myUniformLocations[anIndex][OpenGl_RT_uShadEnabled] =
1464 aShaderProgram->GetUniformLocation (theGlContext, "uShadowsEnable");
1465 myUniformLocations[anIndex][OpenGl_RT_uReflEnabled] =
1466 aShaderProgram->GetUniformLocation (theGlContext, "uReflectionsEnable");
1467 myUniformLocations[anIndex][OpenGl_RT_uEnvMapEnable] =
1468 aShaderProgram->GetUniformLocation (theGlContext, "uEnvironmentEnable");
1471 theGlContext->BindProgram (NULL);
1474 if (myRaytraceInitStatus != OpenGl_RT_NONE)
1476 return myRaytraceInitStatus == OpenGl_RT_INIT;
1479 if (myRaytraceFBO1.IsNull())
1481 myRaytraceFBO1 = new OpenGl_FrameBuffer;
1484 if (myRaytraceFBO2.IsNull())
1486 myRaytraceFBO2 = new OpenGl_FrameBuffer;
1489 const GLfloat aVertices[] = { -1.f, -1.f, 0.f,
1496 myRaytraceScreenQuad.Init (theGlContext, 3, 6, aVertices);
1498 myRaytraceInitStatus = OpenGl_RT_INIT; // initialized in normal way
1500 return Standard_True;
1503 // =======================================================================
1504 // function : NullifyResource
1506 // =======================================================================
1507 inline void NullifyResource (const Handle(OpenGl_Context)& theGlContext,
1508 Handle(OpenGl_Resource)& theResource)
1510 if (!theResource.IsNull())
1512 theResource->Release (theGlContext.operator->());
1513 theResource.Nullify();
1517 // =======================================================================
1518 // function : ReleaseRaytraceResources
1519 // purpose : Releases OpenGL/GLSL shader programs
1520 // =======================================================================
1521 void OpenGl_View::releaseRaytraceResources (const Handle(OpenGl_Context)& theGlContext)
1523 NullifyResource (theGlContext, myOpenGlFBO);
1524 NullifyResource (theGlContext, myRaytraceFBO1);
1525 NullifyResource (theGlContext, myRaytraceFBO2);
1527 NullifyResource (theGlContext, myRaytraceShader);
1528 NullifyResource (theGlContext, myPostFSAAShader);
1530 NullifyResource (theGlContext, myRaytraceProgram);
1531 NullifyResource (theGlContext, myPostFSAAProgram);
1533 NullifyResource (theGlContext, mySceneNodeInfoTexture);
1534 NullifyResource (theGlContext, mySceneMinPointTexture);
1535 NullifyResource (theGlContext, mySceneMaxPointTexture);
1537 NullifyResource (theGlContext, myGeometryVertexTexture);
1538 NullifyResource (theGlContext, myGeometryNormalTexture);
1539 NullifyResource (theGlContext, myGeometryTexCrdTexture);
1540 NullifyResource (theGlContext, myGeometryTriangTexture);
1541 NullifyResource (theGlContext, mySceneTransformTexture);
1543 NullifyResource (theGlContext, myRaytraceLightSrcTexture);
1544 NullifyResource (theGlContext, myRaytraceMaterialTexture);
1546 if (myRaytraceScreenQuad.IsValid())
1547 myRaytraceScreenQuad.Release (theGlContext.operator->());
1550 // =======================================================================
1551 // function : ResizeRaytraceBuffers
1552 // purpose : Resizes OpenGL frame buffers
1553 // =======================================================================
1554 Standard_Boolean OpenGl_View::resizeRaytraceBuffers (const Standard_Integer theSizeX,
1555 const Standard_Integer theSizeY,
1556 const Handle(OpenGl_Context)& theGlContext)
1558 if (myRaytraceFBO1->GetVPSizeX() != theSizeX
1559 || myRaytraceFBO1->GetVPSizeY() != theSizeY)
1561 myRaytraceFBO1->Init (theGlContext, theSizeX, theSizeY);
1562 myRaytraceFBO2->Init (theGlContext, theSizeX, theSizeY);
1565 return Standard_True;
1568 // =======================================================================
1569 // function : UpdateCamera
1570 // purpose : Generates viewing rays for corners of screen quad
1571 // =======================================================================
1572 void OpenGl_View::updateCamera (const OpenGl_Mat4& theOrientation,
1573 const OpenGl_Mat4& theViewMapping,
1574 OpenGl_Vec3* theOrigins,
1575 OpenGl_Vec3* theDirects,
1576 OpenGl_Mat4& theUnview)
1578 // compute inverse model-view-projection matrix
1579 (theViewMapping * theOrientation).Inverted (theUnview);
1581 Standard_Integer aOriginIndex = 0;
1582 Standard_Integer aDirectIndex = 0;
1584 for (Standard_Integer aY = -1; aY <= 1; aY += 2)
1586 for (Standard_Integer aX = -1; aX <= 1; aX += 2)
1588 OpenGl_Vec4 aOrigin (GLfloat(aX),
1593 aOrigin = theUnview * aOrigin;
1595 aOrigin.x() = aOrigin.x() / aOrigin.w();
1596 aOrigin.y() = aOrigin.y() / aOrigin.w();
1597 aOrigin.z() = aOrigin.z() / aOrigin.w();
1599 OpenGl_Vec4 aDirect (GLfloat(aX),
1604 aDirect = theUnview * aDirect;
1606 aDirect.x() = aDirect.x() / aDirect.w();
1607 aDirect.y() = aDirect.y() / aDirect.w();
1608 aDirect.z() = aDirect.z() / aDirect.w();
1610 aDirect = aDirect - aOrigin;
1612 GLdouble aInvLen = 1.0 / sqrt (aDirect.x() * aDirect.x() +
1613 aDirect.y() * aDirect.y() +
1614 aDirect.z() * aDirect.z());
1616 theOrigins[aOriginIndex++] = OpenGl_Vec3 (static_cast<GLfloat> (aOrigin.x()),
1617 static_cast<GLfloat> (aOrigin.y()),
1618 static_cast<GLfloat> (aOrigin.z()));
1620 theDirects[aDirectIndex++] = OpenGl_Vec3 (static_cast<GLfloat> (aDirect.x() * aInvLen),
1621 static_cast<GLfloat> (aDirect.y() * aInvLen),
1622 static_cast<GLfloat> (aDirect.z() * aInvLen));
1627 // =======================================================================
1628 // function : UploadRaytraceData
1629 // purpose : Uploads ray-trace data to the GPU
1630 // =======================================================================
1631 Standard_Boolean OpenGl_View::uploadRaytraceData (const Handle(OpenGl_Context)& theGlContext)
1633 if (!theGlContext->IsGlGreaterEqual (3, 1))
1635 #ifdef RAY_TRACE_PRINT_INFO
1636 std::cout << "Error: OpenGL version is less than 3.1" << std::endl;
1638 return Standard_False;
1641 /////////////////////////////////////////////////////////////////////////////
1642 // Prepare OpenGL textures
1644 if (theGlContext->arbTexBindless != NULL)
1646 // If OpenGL driver supports bindless textures we need
1647 // to get unique 64- bit handles for using on the GPU
1648 if (!myRaytraceGeometry.UpdateTextureHandles (theGlContext))
1650 #ifdef RAY_TRACE_PRINT_INFO
1651 std::cout << "Error: Failed to get OpenGL texture handles" << std::endl;
1653 return Standard_False;
1657 /////////////////////////////////////////////////////////////////////////////
1658 // Create OpenGL BVH buffers
1660 if (mySceneNodeInfoTexture.IsNull()) // create scene BVH buffers
1662 mySceneNodeInfoTexture = new OpenGl_TextureBufferArb;
1663 mySceneMinPointTexture = new OpenGl_TextureBufferArb;
1664 mySceneMaxPointTexture = new OpenGl_TextureBufferArb;
1665 mySceneTransformTexture = new OpenGl_TextureBufferArb;
1667 if (!mySceneNodeInfoTexture->Create (theGlContext)
1668 || !mySceneMinPointTexture->Create (theGlContext)
1669 || !mySceneMaxPointTexture->Create (theGlContext)
1670 || !mySceneTransformTexture->Create (theGlContext))
1672 #ifdef RAY_TRACE_PRINT_INFO
1673 std::cout << "Error: Failed to create scene BVH buffers" << std::endl;
1675 return Standard_False;
1679 if (myGeometryVertexTexture.IsNull()) // create geometry buffers
1681 myGeometryVertexTexture = new OpenGl_TextureBufferArb;
1682 myGeometryNormalTexture = new OpenGl_TextureBufferArb;
1683 myGeometryTexCrdTexture = new OpenGl_TextureBufferArb;
1684 myGeometryTriangTexture = new OpenGl_TextureBufferArb;
1686 if (!myGeometryVertexTexture->Create (theGlContext)
1687 || !myGeometryNormalTexture->Create (theGlContext)
1688 || !myGeometryTexCrdTexture->Create (theGlContext)
1689 || !myGeometryTriangTexture->Create (theGlContext))
1691 #ifdef RAY_TRACE_PRINT_INFO
1692 std::cout << "Error: Failed to create buffers for triangulation data" << std::endl;
1694 return Standard_False;
1698 if (myRaytraceMaterialTexture.IsNull()) // create material buffer
1700 myRaytraceMaterialTexture = new OpenGl_TextureBufferArb;
1702 if (!myRaytraceMaterialTexture->Create (theGlContext))
1704 #ifdef RAY_TRACE_PRINT_INFO
1705 std::cout << "Error: Failed to create buffers for material data" << std::endl;
1707 return Standard_False;
1711 /////////////////////////////////////////////////////////////////////////////
1712 // Write transform buffer
1714 BVH_Mat4f* aNodeTransforms = new BVH_Mat4f[myRaytraceGeometry.Size()];
1716 bool aResult = true;
1718 for (Standard_Integer anElemIndex = 0; anElemIndex < myRaytraceGeometry.Size(); ++anElemIndex)
1720 OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> (
1721 myRaytraceGeometry.Objects().ChangeValue (anElemIndex).operator->());
1723 const BVH_Transform<Standard_ShortReal, 4>* aTransform =
1724 dynamic_cast<const BVH_Transform<Standard_ShortReal, 4>* > (aTriangleSet->Properties().operator->());
1726 Standard_ASSERT_RETURN (aTransform != NULL,
1727 "OpenGl_TriangleSet does not contain transform", Standard_False);
1729 aNodeTransforms[anElemIndex] = aTransform->Inversed();
1732 aResult &= mySceneTransformTexture->Init (theGlContext, 4,
1733 myRaytraceGeometry.Size() * 4, reinterpret_cast<const GLfloat*> (aNodeTransforms));
1735 delete [] aNodeTransforms;
1737 /////////////////////////////////////////////////////////////////////////////
1738 // Write geometry and bottom-level BVH buffers
1740 Standard_Size aTotalVerticesNb = 0;
1741 Standard_Size aTotalElementsNb = 0;
1742 Standard_Size aTotalBVHNodesNb = 0;
1744 for (Standard_Integer anElemIndex = 0; anElemIndex < myRaytraceGeometry.Size(); ++anElemIndex)
1746 OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> (
1747 myRaytraceGeometry.Objects().ChangeValue (anElemIndex).operator->());
1749 Standard_ASSERT_RETURN (aTriangleSet != NULL,
1750 "Error: Failed to get triangulation of OpenGL element", Standard_False);
1752 aTotalVerticesNb += aTriangleSet->Vertices.size();
1753 aTotalElementsNb += aTriangleSet->Elements.size();
1755 Standard_ASSERT_RETURN (!aTriangleSet->BVH().IsNull(),
1756 "Error: Failed to get bottom-level BVH of OpenGL element", Standard_False);
1758 aTotalBVHNodesNb += aTriangleSet->BVH()->NodeInfoBuffer().size();
1761 aTotalBVHNodesNb += myRaytraceGeometry.BVH()->NodeInfoBuffer().size();
1763 if (aTotalBVHNodesNb != 0)
1765 aResult &= mySceneNodeInfoTexture->Init (
1766 theGlContext, 4, GLsizei (aTotalBVHNodesNb), static_cast<const GLuint*> (NULL));
1767 aResult &= mySceneMinPointTexture->Init (
1768 theGlContext, 3, GLsizei (aTotalBVHNodesNb), static_cast<const GLfloat*> (NULL));
1769 aResult &= mySceneMaxPointTexture->Init (
1770 theGlContext, 3, GLsizei (aTotalBVHNodesNb), static_cast<const GLfloat*> (NULL));
1775 #ifdef RAY_TRACE_PRINT_INFO
1776 std::cout << "Error: Failed to upload buffers for bottom-level scene BVH" << std::endl;
1778 return Standard_False;
1781 if (aTotalElementsNb != 0)
1783 aResult &= myGeometryTriangTexture->Init (
1784 theGlContext, 4, GLsizei (aTotalElementsNb), static_cast<const GLuint*> (NULL));
1787 if (aTotalVerticesNb != 0)
1789 aResult &= myGeometryVertexTexture->Init (
1790 theGlContext, 3, GLsizei (aTotalVerticesNb), static_cast<const GLfloat*> (NULL));
1791 aResult &= myGeometryNormalTexture->Init (
1792 theGlContext, 3, GLsizei (aTotalVerticesNb), static_cast<const GLfloat*> (NULL));
1793 aResult &= myGeometryTexCrdTexture->Init (
1794 theGlContext, 2, GLsizei (aTotalVerticesNb), static_cast<const GLfloat*> (NULL));
1799 #ifdef RAY_TRACE_PRINT_INFO
1800 std::cout << "Error: Failed to upload buffers for scene geometry" << std::endl;
1802 return Standard_False;
1805 const NCollection_Handle<BVH_Tree<Standard_ShortReal, 3> >& aBVH = myRaytraceGeometry.BVH();
1807 if (aBVH->Length() > 0)
1809 aResult &= mySceneNodeInfoTexture->SubData (theGlContext, 0, aBVH->Length(),
1810 reinterpret_cast<const GLuint*> (&aBVH->NodeInfoBuffer().front()));
1811 aResult &= mySceneMinPointTexture->SubData (theGlContext, 0, aBVH->Length(),
1812 reinterpret_cast<const GLfloat*> (&aBVH->MinPointBuffer().front()));
1813 aResult &= mySceneMaxPointTexture->SubData (theGlContext, 0, aBVH->Length(),
1814 reinterpret_cast<const GLfloat*> (&aBVH->MaxPointBuffer().front()));
1817 for (Standard_Integer aNodeIdx = 0; aNodeIdx < aBVH->Length(); ++aNodeIdx)
1819 if (!aBVH->IsOuter (aNodeIdx))
1822 OpenGl_TriangleSet* aTriangleSet = myRaytraceGeometry.TriangleSet (aNodeIdx);
1824 Standard_ASSERT_RETURN (aTriangleSet != NULL,
1825 "Error: Failed to get triangulation of OpenGL element", Standard_False);
1827 Standard_Integer aBVHOffset = myRaytraceGeometry.AccelerationOffset (aNodeIdx);
1829 Standard_ASSERT_RETURN (aBVHOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET,
1830 "Error: Failed to get offset for bottom-level BVH", Standard_False);
1832 const Standard_Integer aBvhBuffersSize = aTriangleSet->BVH()->Length();
1834 if (aBvhBuffersSize != 0)
1836 aResult &= mySceneNodeInfoTexture->SubData (theGlContext, aBVHOffset, aBvhBuffersSize,
1837 reinterpret_cast<const GLuint*> (&aTriangleSet->BVH()->NodeInfoBuffer().front()));
1838 aResult &= mySceneMinPointTexture->SubData (theGlContext, aBVHOffset, aBvhBuffersSize,
1839 reinterpret_cast<const GLfloat*> (&aTriangleSet->BVH()->MinPointBuffer().front()));
1840 aResult &= mySceneMaxPointTexture->SubData (theGlContext, aBVHOffset, aBvhBuffersSize,
1841 reinterpret_cast<const GLfloat*> (&aTriangleSet->BVH()->MaxPointBuffer().front()));
1845 #ifdef RAY_TRACE_PRINT_INFO
1846 std::cout << "Error: Failed to upload buffers for bottom-level scene BVHs" << std::endl;
1848 return Standard_False;
1852 const Standard_Integer aVerticesOffset = myRaytraceGeometry.VerticesOffset (aNodeIdx);
1854 Standard_ASSERT_RETURN (aVerticesOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET,
1855 "Error: Failed to get offset for triangulation vertices of OpenGL element", Standard_False);
1857 if (!aTriangleSet->Vertices.empty())
1859 aResult &= myGeometryNormalTexture->SubData (theGlContext, aVerticesOffset,
1860 GLsizei (aTriangleSet->Normals.size()), reinterpret_cast<const GLfloat*> (&aTriangleSet->Normals.front()));
1861 aResult &= myGeometryTexCrdTexture->SubData (theGlContext, aVerticesOffset,
1862 GLsizei (aTriangleSet->TexCrds.size()), reinterpret_cast<const GLfloat*> (&aTriangleSet->TexCrds.front()));
1863 aResult &= myGeometryVertexTexture->SubData (theGlContext, aVerticesOffset,
1864 GLsizei (aTriangleSet->Vertices.size()), reinterpret_cast<const GLfloat*> (&aTriangleSet->Vertices.front()));
1867 const Standard_Integer anElementsOffset = myRaytraceGeometry.ElementsOffset (aNodeIdx);
1869 Standard_ASSERT_RETURN (anElementsOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET,
1870 "Error: Failed to get offset for triangulation elements of OpenGL element", Standard_False);
1872 if (!aTriangleSet->Elements.empty())
1874 aResult &= myGeometryTriangTexture->SubData (theGlContext, anElementsOffset, GLsizei (aTriangleSet->Elements.size()),
1875 reinterpret_cast<const GLuint*> (&aTriangleSet->Elements.front()));
1880 #ifdef RAY_TRACE_PRINT_INFO
1881 std::cout << "Error: Failed to upload triangulation buffers for OpenGL element" << std::endl;
1883 return Standard_False;
1887 /////////////////////////////////////////////////////////////////////////////
1888 // Write material buffer
1890 if (myRaytraceGeometry.Materials.size() != 0)
1892 aResult &= myRaytraceMaterialTexture->Init (theGlContext, 4,
1893 GLsizei (myRaytraceGeometry.Materials.size() * 11), myRaytraceGeometry.Materials.front().Packed());
1897 #ifdef RAY_TRACE_PRINT_INFO
1898 std::cout << "Error: Failed to upload material buffer" << std::endl;
1900 return Standard_False;
1904 myIsRaytraceDataValid = myRaytraceGeometry.Objects().Size() != 0;
1906 #ifdef RAY_TRACE_PRINT_INFO
1908 Standard_ShortReal aMemUsed = 0.f;
1910 for (Standard_Integer anElemIdx = 0; anElemIdx < myRaytraceGeometry.Size(); ++anElemIdx)
1912 OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> (
1913 myRaytraceGeometry.Objects().ChangeValue (anElemIdx).operator->());
1915 aMemUsed += static_cast<Standard_ShortReal> (
1916 aTriangleSet->Vertices.size() * sizeof (BVH_Vec3f));
1917 aMemUsed += static_cast<Standard_ShortReal> (
1918 aTriangleSet->Normals.size() * sizeof (BVH_Vec3f));
1919 aMemUsed += static_cast<Standard_ShortReal> (
1920 aTriangleSet->TexCrds.size() * sizeof (BVH_Vec2f));
1921 aMemUsed += static_cast<Standard_ShortReal> (
1922 aTriangleSet->Elements.size() * sizeof (BVH_Vec4i));
1924 aMemUsed += static_cast<Standard_ShortReal> (
1925 aTriangleSet->BVH()->NodeInfoBuffer().size() * sizeof (BVH_Vec4i));
1926 aMemUsed += static_cast<Standard_ShortReal> (
1927 aTriangleSet->BVH()->MinPointBuffer().size() * sizeof (BVH_Vec3f));
1928 aMemUsed += static_cast<Standard_ShortReal> (
1929 aTriangleSet->BVH()->MaxPointBuffer().size() * sizeof (BVH_Vec3f));
1932 aMemUsed += static_cast<Standard_ShortReal> (
1933 myRaytraceGeometry.BVH()->NodeInfoBuffer().size() * sizeof (BVH_Vec4i));
1934 aMemUsed += static_cast<Standard_ShortReal> (
1935 myRaytraceGeometry.BVH()->MinPointBuffer().size() * sizeof (BVH_Vec3f));
1936 aMemUsed += static_cast<Standard_ShortReal> (
1937 myRaytraceGeometry.BVH()->MaxPointBuffer().size() * sizeof (BVH_Vec3f));
1939 std::cout << "GPU Memory Used (MB): ~" << aMemUsed / 1048576 << std::endl;
1946 // =======================================================================
1947 // function : UpdateRaytraceLightSources
1948 // purpose : Updates 3D scene light sources for ray-tracing
1949 // =======================================================================
1950 Standard_Boolean OpenGl_View::updateRaytraceLightSources (const OpenGl_Mat4& theInvModelView, const Handle(OpenGl_Context)& theGlContext)
1952 myRaytraceGeometry.Sources.clear();
1954 myRaytraceGeometry.Ambient = BVH_Vec4f (0.0f, 0.0f, 0.0f, 0.0f);
1956 for (OpenGl_ListOfLight::Iterator anItl (LightList()); anItl.More(); anItl.Next())
1958 const OpenGl_Light& aLight = anItl.Value();
1960 if (aLight.Type == Visual3d_TOLS_AMBIENT)
1962 myRaytraceGeometry.Ambient += BVH_Vec4f (aLight.Color.r(),
1969 BVH_Vec4f aDiffuse (aLight.Color.r(),
1974 BVH_Vec4f aPosition (-aLight.Direction.x(),
1975 -aLight.Direction.y(),
1976 -aLight.Direction.z(),
1979 if (aLight.Type != Visual3d_TOLS_DIRECTIONAL)
1981 aPosition = BVH_Vec4f (aLight.Position.x(),
1982 aLight.Position.y(),
1983 aLight.Position.z(),
1987 if (aLight.IsHeadlight)
1989 aPosition = theInvModelView * aPosition;
1992 myRaytraceGeometry.Sources.push_back (OpenGl_RaytraceLight (aDiffuse, aPosition));
1995 if (myRaytraceLightSrcTexture.IsNull()) // create light source buffer
1997 myRaytraceLightSrcTexture = new OpenGl_TextureBufferArb;
1999 if (!myRaytraceLightSrcTexture->Create (theGlContext))
2001 #ifdef RAY_TRACE_PRINT_INFO
2002 std::cout << "Error: Failed to create light source buffer" << std::endl;
2004 return Standard_False;
2008 if (myRaytraceGeometry.Sources.size() != 0)
2010 const GLfloat* aDataPtr = myRaytraceGeometry.Sources.front().Packed();
2011 if (!myRaytraceLightSrcTexture->Init (theGlContext, 4, GLsizei (myRaytraceGeometry.Sources.size() * 2), aDataPtr))
2013 #ifdef RAY_TRACE_PRINT_INFO
2014 std::cout << "Error: Failed to upload light source buffer" << std::endl;
2016 return Standard_False;
2020 return Standard_True;
2023 // =======================================================================
2024 // function : UpdateRaytraceEnvironmentMap
2025 // purpose : Updates environment map for ray-tracing
2026 // =======================================================================
2027 Standard_Boolean OpenGl_View::updateRaytraceEnvironmentMap (const Handle(OpenGl_Context)& theGlContext)
2029 Standard_Boolean aResult = Standard_True;
2031 if (!myToUpdateEnvironmentMap)
2036 for (Standard_Integer anIdx = 0; anIdx < 2; ++anIdx)
2038 const Handle(OpenGl_ShaderProgram)& aProgram =
2039 anIdx == 0 ? myRaytraceProgram : myPostFSAAProgram;
2041 if (!aProgram.IsNull())
2043 aResult &= theGlContext->BindProgram (aProgram);
2045 if (!myTextureEnv.IsNull() && mySurfaceDetail != Visual3d_TOD_NONE)
2047 myTextureEnv->Bind (theGlContext,
2048 GL_TEXTURE0 + OpenGl_RT_EnvironmentMapTexture);
2050 aResult &= aProgram->SetUniform (theGlContext,
2051 myUniformLocations[anIdx][OpenGl_RT_uEnvMapEnable], 1);
2055 aResult &= aProgram->SetUniform (theGlContext,
2056 myUniformLocations[anIdx][OpenGl_RT_uEnvMapEnable], 0);
2061 myToUpdateEnvironmentMap = Standard_False;
2063 theGlContext->BindProgram (NULL);
2068 // =======================================================================
2069 // function : SetUniformState
2070 // purpose : Sets uniform state for the given ray-tracing shader program
2071 // =======================================================================
2072 Standard_Boolean OpenGl_View::setUniformState (const Graphic3d_CView& theCView,
2073 const OpenGl_Vec3* theOrigins,
2074 const OpenGl_Vec3* theDirects,
2075 const OpenGl_Mat4& theUnviewMat,
2076 const Standard_Integer theProgramId,
2077 const Handle(OpenGl_Context)& theGlContext)
2079 Handle(OpenGl_ShaderProgram)& theProgram =
2080 theProgramId == 0 ? myRaytraceProgram : myPostFSAAProgram;
2082 if (theProgram.IsNull())
2084 return Standard_False;
2087 Standard_Boolean aResult = Standard_True;
2089 const Standard_Integer aLightSourceBufferSize =
2090 static_cast<Standard_Integer> (myRaytraceGeometry.Sources.size());
2093 aResult &= theProgram->SetUniform (theGlContext,
2094 myUniformLocations[theProgramId][OpenGl_RT_uOriginLB], theOrigins[0]);
2095 aResult &= theProgram->SetUniform (theGlContext,
2096 myUniformLocations[theProgramId][OpenGl_RT_uOriginRB], theOrigins[1]);
2097 aResult &= theProgram->SetUniform (theGlContext,
2098 myUniformLocations[theProgramId][OpenGl_RT_uOriginLT], theOrigins[2]);
2099 aResult &= theProgram->SetUniform (theGlContext,
2100 myUniformLocations[theProgramId][OpenGl_RT_uOriginRT], theOrigins[3]);
2101 aResult &= theProgram->SetUniform (theGlContext,
2102 myUniformLocations[theProgramId][OpenGl_RT_uDirectLB], theDirects[0]);
2103 aResult &= theProgram->SetUniform (theGlContext,
2104 myUniformLocations[theProgramId][OpenGl_RT_uDirectRB], theDirects[1]);
2105 aResult &= theProgram->SetUniform (theGlContext,
2106 myUniformLocations[theProgramId][OpenGl_RT_uDirectLT], theDirects[2]);
2107 aResult &= theProgram->SetUniform (theGlContext,
2108 myUniformLocations[theProgramId][OpenGl_RT_uDirectRT], theDirects[3]);
2109 aResult &= theProgram->SetUniform (theGlContext,
2110 myUniformLocations[theProgramId][OpenGl_RT_uUnviewMat], theUnviewMat);
2112 // Set scene parameters
2113 aResult &= theProgram->SetUniform (theGlContext,
2114 myUniformLocations[theProgramId][OpenGl_RT_uSceneRad], myRaytraceSceneRadius);
2115 aResult &= theProgram->SetUniform (theGlContext,
2116 myUniformLocations[theProgramId][OpenGl_RT_uSceneEps], myRaytraceSceneEpsilon);
2117 aResult &= theProgram->SetUniform (theGlContext,
2118 myUniformLocations[theProgramId][OpenGl_RT_uLightCount], aLightSourceBufferSize);
2119 aResult &= theProgram->SetUniform (theGlContext,
2120 myUniformLocations[theProgramId][OpenGl_RT_uLightAmbnt], myRaytraceGeometry.Ambient);
2122 // Set run-time rendering options
2123 aResult &= theProgram->SetUniform (theGlContext,
2124 myUniformLocations[theProgramId][OpenGl_RT_uShadEnabled], theCView.RenderParams.IsShadowEnabled ? 1 : 0);
2125 aResult &= theProgram->SetUniform (theGlContext,
2126 myUniformLocations[theProgramId][OpenGl_RT_uReflEnabled], theCView.RenderParams.IsReflectionEnabled ? 1 : 0);
2128 // Set array of 64-bit texture handles
2129 if (theGlContext->arbTexBindless != NULL && myRaytraceGeometry.HasTextures())
2131 aResult &= theProgram->SetUniform (theGlContext, "uTextureSamplers", static_cast<GLsizei> (
2132 myRaytraceGeometry.TextureHandles().size()), &myRaytraceGeometry.TextureHandles()[0]);
2137 #ifdef RAY_TRACE_PRINT_INFO
2138 std::cout << "Info: Not all uniforms were detected for program " << theProgramId << std::endl;
2145 // =======================================================================
2146 // function : BindRaytraceTextures
2147 // purpose : Binds ray-trace textures to corresponding texture units
2148 // =======================================================================
2149 void OpenGl_View::bindRaytraceTextures (const Handle(OpenGl_Context)& theGlContext)
2151 mySceneMinPointTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneMinPointTexture);
2152 mySceneMaxPointTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneMaxPointTexture);
2153 mySceneNodeInfoTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneNodeInfoTexture);
2154 myGeometryVertexTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryVertexTexture);
2155 myGeometryNormalTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryNormalTexture);
2156 myGeometryTexCrdTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryTexCrdTexture);
2157 myGeometryTriangTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryTriangTexture);
2158 mySceneTransformTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneTransformTexture);
2159 myRaytraceMaterialTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceMaterialTexture);
2160 myRaytraceLightSrcTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceLightSrcTexture);
2162 if (!myOpenGlFBO.IsNull())
2164 myOpenGlFBO->ColorTexture()->Bind (theGlContext, GL_TEXTURE0 + OpenGl_RT_OpenGlColorTexture);
2165 myOpenGlFBO->DepthStencilTexture()->Bind (theGlContext, GL_TEXTURE0 + OpenGl_RT_OpenGlDepthTexture);
2169 // =======================================================================
2170 // function : UnbindRaytraceTextures
2171 // purpose : Unbinds ray-trace textures from corresponding texture units
2172 // =======================================================================
2173 void OpenGl_View::unbindRaytraceTextures (const Handle(OpenGl_Context)& theGlContext)
2175 mySceneMinPointTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneMinPointTexture);
2176 mySceneMaxPointTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneMaxPointTexture);
2177 mySceneNodeInfoTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneNodeInfoTexture);
2178 myGeometryVertexTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryVertexTexture);
2179 myGeometryNormalTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryNormalTexture);
2180 myGeometryTexCrdTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryTexCrdTexture);
2181 myGeometryTriangTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryTriangTexture);
2182 mySceneTransformTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneTransformTexture);
2183 myRaytraceMaterialTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceMaterialTexture);
2184 myRaytraceLightSrcTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceLightSrcTexture);
2186 if (!myOpenGlFBO.IsNull())
2188 myOpenGlFBO->ColorTexture()->Unbind (theGlContext, GL_TEXTURE0 + OpenGl_RT_OpenGlColorTexture);
2189 myOpenGlFBO->DepthStencilTexture()->Unbind (theGlContext, GL_TEXTURE0 + OpenGl_RT_OpenGlDepthTexture);
2192 theGlContext->core15fwd->glActiveTexture (GL_TEXTURE0);
2195 // =======================================================================
2196 // function : RunRaytraceShaders
2197 // purpose : Runs ray-tracing shader programs
2198 // =======================================================================
2199 Standard_Boolean OpenGl_View::runRaytraceShaders (const Graphic3d_CView& theCView,
2200 const Standard_Integer theSizeX,
2201 const Standard_Integer theSizeY,
2202 const OpenGl_Vec3* theOrigins,
2203 const OpenGl_Vec3* theDirects,
2204 const OpenGl_Mat4& theUnviewMat,
2205 OpenGl_FrameBuffer* theOutputFBO,
2206 const Handle(OpenGl_Context)& theGlContext)
2208 bindRaytraceTextures (theGlContext);
2210 if (theCView.RenderParams.IsAntialiasingEnabled) // render source image to FBO
2212 myRaytraceFBO1->BindBuffer (theGlContext);
2214 glDisable (GL_BLEND);
2217 Standard_Boolean aResult = theGlContext->BindProgram (myRaytraceProgram);
2219 aResult &= setUniformState (theCView,
2223 0, // ID of RT program
2226 myRaytraceScreenQuad.BindVertexAttrib (theGlContext, Graphic3d_TOA_POS);
2229 theGlContext->core20fwd->glDrawArrays (GL_TRIANGLES, 0, 6);
2231 myRaytraceScreenQuad.UnbindVertexAttrib (theGlContext, Graphic3d_TOA_POS);
2233 if (!theCView.RenderParams.IsAntialiasingEnabled || !aResult)
2235 unbindRaytraceTextures (theGlContext);
2237 theGlContext->BindProgram (NULL);
2242 myRaytraceFBO1->ColorTexture()->Bind (theGlContext, GL_TEXTURE0 + OpenGl_RT_FSAAInputTexture);
2244 aResult &= theGlContext->BindProgram (myPostFSAAProgram);
2246 aResult &= setUniformState (theCView,
2250 1, // ID of FSAA program
2253 myRaytraceScreenQuad.BindVertexAttrib (theGlContext, Graphic3d_TOA_POS);
2255 // Perform multi-pass adaptive FSAA using ping-pong technique.
2256 // We use 'FLIPTRI' sampling pattern changing for every pixel
2257 // (3 additional samples per pixel, the 1st sample is already
2258 // available from initial ray-traced image).
2259 for (Standard_Integer anIt = 1; anIt < 4; ++anIt)
2261 GLfloat aOffsetX = 1.f / theSizeX;
2262 GLfloat aOffsetY = 1.f / theSizeY;
2280 aResult &= myPostFSAAProgram->SetUniform (theGlContext,
2281 myUniformLocations[1][OpenGl_RT_uSamples], anIt + 1);
2282 aResult &= myPostFSAAProgram->SetUniform (theGlContext,
2283 myUniformLocations[1][OpenGl_RT_uOffsetX], aOffsetX);
2284 aResult &= myPostFSAAProgram->SetUniform (theGlContext,
2285 myUniformLocations[1][OpenGl_RT_uOffsetY], aOffsetY);
2287 Handle(OpenGl_FrameBuffer)& aFramebuffer = anIt % 2 ? myRaytraceFBO2 : myRaytraceFBO1;
2289 if (anIt == 3) // disable FBO on last iteration
2291 glEnable (GL_BLEND);
2293 if (theOutputFBO != NULL)
2294 theOutputFBO->BindBuffer (theGlContext);
2298 aFramebuffer->BindBuffer (theGlContext);
2301 theGlContext->core20fwd->glDrawArrays (GL_TRIANGLES, 0, 6);
2303 if (anIt != 3) // set input for the next pass
2305 aFramebuffer->ColorTexture()->Bind (theGlContext, GL_TEXTURE0 + OpenGl_RT_FSAAInputTexture);
2309 myRaytraceScreenQuad.UnbindVertexAttrib (theGlContext, Graphic3d_TOA_POS);
2311 unbindRaytraceTextures (theGlContext);
2313 theGlContext->BindProgram (NULL);
2318 // =======================================================================
2319 // function : Raytrace
2320 // purpose : Redraws the window using OpenGL/GLSL ray-tracing
2321 // =======================================================================
2322 Standard_Boolean OpenGl_View::raytrace (const Graphic3d_CView& theCView,
2323 const Standard_Integer theSizeX,
2324 const Standard_Integer theSizeY,
2325 OpenGl_FrameBuffer* theOutputFBO,
2326 const Handle(OpenGl_Context)& theGlContext)
2328 if (!initRaytraceResources (theCView, theGlContext))
2330 return Standard_False;
2333 if (!resizeRaytraceBuffers (theSizeX, theSizeY, theGlContext))
2335 return Standard_False;
2338 if (!updateRaytraceEnvironmentMap (theGlContext))
2340 return Standard_False;
2343 // Get model-view and projection matrices
2344 OpenGl_Mat4 aOrientationMatrix;
2345 OpenGl_Mat4 aViewMappingMatrix;
2346 OpenGl_Mat4 aInverOrientMatrix;
2348 GetMatrices (aOrientationMatrix,
2349 aViewMappingMatrix);
2351 aOrientationMatrix.Inverted (aInverOrientMatrix);
2353 if (!updateRaytraceLightSources (aInverOrientMatrix, theGlContext))
2355 return Standard_False;
2358 OpenGl_Vec3 aOrigins[4];
2359 OpenGl_Vec3 aDirects[4];
2360 OpenGl_Mat4 anUnviewMat;
2362 updateCamera (aOrientationMatrix,
2368 glEnable (GL_BLEND);
2369 glDisable (GL_DEPTH_TEST);
2370 glBlendFunc (GL_ONE, GL_SRC_ALPHA);
2372 if (theOutputFBO != NULL)
2374 theOutputFBO->BindBuffer (theGlContext);
2377 // Generate ray-traced image
2378 if (myIsRaytraceDataValid)
2380 myRaytraceScreenQuad.Bind (theGlContext);
2382 if (!myRaytraceGeometry.AcquireTextures (theGlContext))
2384 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB, GL_DEBUG_TYPE_ERROR_ARB,
2385 0, GL_DEBUG_SEVERITY_MEDIUM_ARB, "Error: Failed to acquire OpenGL image textures");
2388 Standard_Boolean aResult = runRaytraceShaders (theCView,
2399 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB, GL_DEBUG_TYPE_ERROR_ARB,
2400 0, GL_DEBUG_SEVERITY_MEDIUM_ARB, "Error: Failed to execute ray-tracing shaders");
2403 if (!myRaytraceGeometry.ReleaseTextures (theGlContext))
2405 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB, GL_DEBUG_TYPE_ERROR_ARB,
2406 0, GL_DEBUG_SEVERITY_MEDIUM_ARB, "Error: Failed to release OpenGL image textures");
2409 myRaytraceScreenQuad.Unbind (theGlContext);
2412 glDisable (GL_BLEND);
2413 glEnable (GL_DEPTH_TEST);
2415 return Standard_True;