0026390: IntTools_Context should provide possibility to set tolerance used by PointOn...
[occt.git] / src / OpenGl / OpenGl_View_Raytrace.cxx
CommitLineData
91c60b57 1// Created on: 2015-02-20
e276548b 2// Created by: Denis BOGOLEPOV
91c60b57 3// Copyright (c) 2015 OPEN CASCADE SAS
e276548b 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
e276548b 6//
d5f74e42 7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
e276548b 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
e276548b 15
25ef750e 16#include <Graphic3d_TextureParams.hxx>
91c60b57 17
fc73a202 18#include <OpenGl_FrameBuffer.hxx>
b7cd4ba7 19#include <OpenGl_PrimitiveArray.hxx>
fc73a202 20#include <OpenGl_VertexBuffer.hxx>
e276548b 21#include <OpenGl_View.hxx>
91c60b57 22
fc73a202 23#include <OSD_Protection.hxx>
91c60b57 24#include <OSD_File.hxx>
e276548b 25
26using namespace OpenGl_Raytrace;
27
28//! Use this macro to output ray-tracing debug info
fc73a202 29// #define RAY_TRACE_PRINT_INFO
e276548b 30
31#ifdef RAY_TRACE_PRINT_INFO
32 #include <OSD_Timer.hxx>
33#endif
34
e276548b 35// =======================================================================
189f85a3 36// function : updateRaytraceGeometry
91c60b57 37// purpose : Updates 3D scene geometry for ray-tracing
e276548b 38// =======================================================================
91c60b57 39Standard_Boolean OpenGl_View::updateRaytraceGeometry (const RaytraceUpdateMode theMode,
40 const Standard_Integer theViewId,
41 const Handle(OpenGl_Context)& theGlContext)
e276548b 42{
91c60b57 43 // In 'check' mode (OpenGl_GUM_CHECK) the scene geometry is analyzed for
44 // modifications. This is light-weight procedure performed on each frame
84c71f29 45 if (theMode == OpenGl_GUM_CHECK)
e276548b 46 {
189f85a3 47 if (myLayerListState != myZLayers.ModificationState())
91c60b57 48 {
49 return updateRaytraceGeometry (OpenGl_GUM_PREPARE, theViewId, theGlContext);
50 }
51 }
84c71f29 52 else if (theMode == OpenGl_GUM_PREPARE)
53 {
54 myRaytraceGeometry.ClearMaterials();
91c60b57 55
84c71f29 56 myArrayToTrianglesMap.clear();
e276548b 57
58 myIsRaytraceDataValid = Standard_False;
59 }
e276548b 60
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;
65
91c60b57 66 // Set to store all currently visible OpenGL primitive arrays
67 // applicable for ray-tracing
8d3f219f 68 std::set<Standard_Size> anArrayIDs;
84c71f29 69
189f85a3 70 // Set to store all non-raytracable elements allowing tracking
71 // of changes in OpenGL scene (only for path tracing)
72 std::set<Standard_Integer> aNonRaytraceIDs;
73
91c60b57 74 const OpenGl_Layer& aLayer = myZLayers.Layer (Graphic3d_ZLayerId_Default);
e276548b 75
91c60b57 76 if (aLayer.NbStructures() != 0)
e276548b 77 {
9f112210 78 const OpenGl_ArrayOfIndexedMapOfStructure& aStructArray = aLayer.ArrayOfStructures();
91c60b57 79
68333c8f 80 for (Standard_Integer anIndex = 0; anIndex < aStructArray.Length(); ++anIndex)
e276548b 81 {
9f112210 82 for (OpenGl_IndexedMapOfStructure::Iterator aStructIt (aStructArray (anIndex)); aStructIt.More(); aStructIt.Next())
e276548b 83 {
84 const OpenGl_Structure* aStructure = aStructIt.Value();
85
84c71f29 86 if (theMode == OpenGl_GUM_CHECK)
e276548b 87 {
91c60b57 88 if (toUpdateStructure (aStructure))
e276548b 89 {
91c60b57 90 return updateRaytraceGeometry (OpenGl_GUM_PREPARE, theViewId, theGlContext);
e276548b 91 }
189f85a3 92 else if (aStructure->IsVisible() && myRaytraceParameters.GlobalIllumination)
93 {
94 aNonRaytraceIDs.insert (aStructure->highlight ? aStructure->Id : -aStructure->Id);
95 }
e2da917a 96 }
84c71f29 97 else if (theMode == OpenGl_GUM_PREPARE)
98 {
91c60b57 99 if (!aStructure->IsRaytracable() || !aStructure->IsVisible())
a1954302 100 {
a272ed94 101 continue;
102 }
91c60b57 103 else if (!aStructure->ViewAffinity.IsNull() && !aStructure->ViewAffinity->IsVisible (theViewId))
a272ed94 104 {
84c71f29 105 continue;
a1954302 106 }
84c71f29 107
108 for (OpenGl_Structure::GroupIterator aGroupIter (aStructure->DrawGroups()); aGroupIter.More(); aGroupIter.Next())
109 {
91c60b57 110 // Extract OpenGL elements from the group (primitives arrays)
84c71f29 111 for (const OpenGl_ElementNode* aNode = aGroupIter.Value()->FirstNode(); aNode != NULL; aNode = aNode->next)
112 {
113 OpenGl_PrimitiveArray* aPrimArray = dynamic_cast<OpenGl_PrimitiveArray*> (aNode->elem);
114
115 if (aPrimArray != NULL)
116 {
8d3f219f 117 anArrayIDs.insert (aPrimArray->GetUID());
84c71f29 118 }
119 }
120 }
e2da917a 121 }
91c60b57 122 else if (theMode == OpenGl_GUM_REBUILD)
e276548b 123 {
124 if (!aStructure->IsRaytracable())
91c60b57 125 {
e276548b 126 continue;
91c60b57 127 }
128 else if (addRaytraceStructure (aStructure, theGlContext))
129 {
130 anElements.insert (aStructure); // structure was processed
131 }
e276548b 132 }
133 }
134 }
135 }
136
84c71f29 137 if (theMode == OpenGl_GUM_PREPARE)
138 {
25ef750e 139 BVH_ObjectSet<Standard_ShortReal, 3>::BVH_ObjectList anUnchangedObjects;
84c71f29 140
91c60b57 141 // Filter out unchanged objects so only their transformations and materials
142 // will be updated (and newly added objects will be processed from scratch)
143 for (Standard_Integer anObjIdx = 0; anObjIdx < myRaytraceGeometry.Size(); ++anObjIdx)
84c71f29 144 {
145 OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> (
91c60b57 146 myRaytraceGeometry.Objects().ChangeValue (anObjIdx).operator->());
84c71f29 147
91c60b57 148 if (aTriangleSet == NULL)
84c71f29 149 {
91c60b57 150 continue;
151 }
152
153 if (anArrayIDs.find (aTriangleSet->AssociatedPArrayID()) != anArrayIDs.end())
154 {
155 anUnchangedObjects.Append (myRaytraceGeometry.Objects().Value (anObjIdx));
84c71f29 156
8d3f219f 157 myArrayToTrianglesMap[aTriangleSet->AssociatedPArrayID()] = aTriangleSet;
84c71f29 158 }
159 }
160
161 myRaytraceGeometry.Objects() = anUnchangedObjects;
162
91c60b57 163 return updateRaytraceGeometry (OpenGl_GUM_REBUILD, theViewId, theGlContext);
84c71f29 164 }
91c60b57 165 else if (theMode == OpenGl_GUM_REBUILD)
e276548b 166 {
91c60b57 167 // Actualize the hash map of structures - remove out-of-date records
d4aaad5b 168 std::map<const OpenGl_Structure*, StructState>::iterator anIter = myStructureStates.begin();
e276548b 169
170 while (anIter != myStructureStates.end())
171 {
172 if (anElements.find (anIter->first) == anElements.end())
173 {
174 myStructureStates.erase (anIter++);
175 }
176 else
177 {
178 ++anIter;
179 }
180 }
181
182 // Actualize OpenGL layer list state
189f85a3 183 myLayerListState = myZLayers.ModificationState();
e276548b 184
91c60b57 185 // Rebuild two-level acceleration structure
265d4508 186 myRaytraceGeometry.ProcessAcceleration();
e276548b 187
91c60b57 188 myRaytraceSceneRadius = 2.f /* scale factor */ * std::max (
189 myRaytraceGeometry.Box().CornerMin().cwiseAbs().maxComp(),
190 myRaytraceGeometry.Box().CornerMax().cwiseAbs().maxComp());
e276548b 191
25ef750e 192 const BVH_Vec3f aSize = myRaytraceGeometry.Box().Size();
e276548b 193
91c60b57 194 myRaytraceSceneEpsilon = Max (1.0e-6f, 1.0e-4f * aSize.Modulus());
fc73a202 195
91c60b57 196 return uploadRaytraceData (theGlContext);
e276548b 197 }
198
189f85a3 199 if (myRaytraceParameters.GlobalIllumination)
200 {
201 Standard_Boolean toRestart =
202 aNonRaytraceIDs.size() != myNonRaytraceStructureIDs.size();
203
204 for (std::set<Standard_Integer>::iterator anID = aNonRaytraceIDs.begin(); anID != aNonRaytraceIDs.end() && !toRestart; ++anID)
205 {
206 if (myNonRaytraceStructureIDs.find (*anID) == myNonRaytraceStructureIDs.end())
207 {
208 toRestart = Standard_True;
209 }
210 }
211
212 if (toRestart)
213 myAccumFrames = 0;
214
215 myNonRaytraceStructureIDs = aNonRaytraceIDs;
216 }
217
e276548b 218 return Standard_True;
219}
220
221// =======================================================================
189f85a3 222// function : toUpdateStructure
e2da917a 223// purpose : Checks to see if the structure is modified
e276548b 224// =======================================================================
91c60b57 225Standard_Boolean OpenGl_View::toUpdateStructure (const OpenGl_Structure* theStructure)
e276548b 226{
227 if (!theStructure->IsRaytracable())
228 {
e276548b 229 if (theStructure->ModificationState() > 0)
230 {
231 theStructure->ResetModificationState();
91c60b57 232
233 return Standard_True; // ray-trace element was removed - need to rebuild
e276548b 234 }
235
91c60b57 236 return Standard_False; // did not contain ray-trace elements
e276548b 237 }
238
d4aaad5b 239 std::map<const OpenGl_Structure*, StructState>::iterator aStructState = myStructureStates.find (theStructure);
e276548b 240
d4aaad5b 241 if (aStructState == myStructureStates.end() || aStructState->second.StructureState != theStructure->ModificationState())
91c60b57 242 {
d4aaad5b 243 return Standard_True;
244 }
245 else if (theStructure->InstancedStructure() != NULL)
246 {
247 return aStructState->second.InstancedState != theStructure->InstancedStructure()->ModificationState();
91c60b57 248 }
e276548b 249
d4aaad5b 250 return Standard_False;
e276548b 251}
252
25ef750e 253// =======================================================================
189f85a3 254// function : buildTextureTransform
25ef750e 255// purpose : Constructs texture transformation matrix
256// =======================================================================
189f85a3 257void buildTextureTransform (const Handle(Graphic3d_TextureParams)& theParams, BVH_Mat4f& theMatrix)
25ef750e 258{
259 theMatrix.InitIdentity();
260
261 // Apply scaling
262 const Graphic3d_Vec2& aScale = theParams->Scale();
263
264 theMatrix.ChangeValue (0, 0) *= aScale.x();
265 theMatrix.ChangeValue (1, 0) *= aScale.x();
266 theMatrix.ChangeValue (2, 0) *= aScale.x();
267 theMatrix.ChangeValue (3, 0) *= aScale.x();
268
269 theMatrix.ChangeValue (0, 1) *= aScale.y();
270 theMatrix.ChangeValue (1, 1) *= aScale.y();
271 theMatrix.ChangeValue (2, 1) *= aScale.y();
272 theMatrix.ChangeValue (3, 1) *= aScale.y();
273
274 // Apply translation
275 const Graphic3d_Vec2 aTrans = -theParams->Translation();
276
277 theMatrix.ChangeValue (0, 3) = theMatrix.GetValue (0, 0) * aTrans.x() +
278 theMatrix.GetValue (0, 1) * aTrans.y();
279
280 theMatrix.ChangeValue (1, 3) = theMatrix.GetValue (1, 0) * aTrans.x() +
281 theMatrix.GetValue (1, 1) * aTrans.y();
282
283 theMatrix.ChangeValue (2, 3) = theMatrix.GetValue (2, 0) * aTrans.x() +
284 theMatrix.GetValue (2, 1) * aTrans.y();
285
286 // Apply rotation
287 const Standard_ShortReal aSin = std::sin (
288 -theParams->Rotation() * static_cast<Standard_ShortReal> (M_PI / 180.0));
289 const Standard_ShortReal aCos = std::cos (
290 -theParams->Rotation() * static_cast<Standard_ShortReal> (M_PI / 180.0));
291
292 BVH_Mat4f aRotationMat;
293 aRotationMat.SetValue (0, 0, aCos);
294 aRotationMat.SetValue (1, 1, aCos);
295 aRotationMat.SetValue (0, 1, -aSin);
296 aRotationMat.SetValue (1, 0, aSin);
297
298 theMatrix = theMatrix * aRotationMat;
299}
300
e276548b 301// =======================================================================
189f85a3 302// function : convertMaterial
e276548b 303// purpose : Creates ray-tracing material properties
304// =======================================================================
91c60b57 305OpenGl_RaytraceMaterial OpenGl_View::convertMaterial (const OpenGl_AspectFace* theAspect,
306 const Handle(OpenGl_Context)& theGlContext)
e276548b 307{
91c60b57 308 OpenGl_RaytraceMaterial theMaterial;
25ef750e 309
91c60b57 310 const OPENGL_SURF_PROP& aProperties = theAspect->IntFront();
25ef750e 311
91c60b57 312 theMaterial.Ambient = BVH_Vec4f (
313 (aProperties.isphysic ? aProperties.ambcol.rgb[0] : aProperties.matcol.rgb[0]) * aProperties.amb,
314 (aProperties.isphysic ? aProperties.ambcol.rgb[1] : aProperties.matcol.rgb[1]) * aProperties.amb,
315 (aProperties.isphysic ? aProperties.ambcol.rgb[2] : aProperties.matcol.rgb[2]) * aProperties.amb,
316 1.f);
25ef750e 317
91c60b57 318 theMaterial.Diffuse = BVH_Vec4f (
319 (aProperties.isphysic ? aProperties.difcol.rgb[0] : aProperties.matcol.rgb[0]) * aProperties.diff,
320 (aProperties.isphysic ? aProperties.difcol.rgb[1] : aProperties.matcol.rgb[1]) * aProperties.diff,
321 (aProperties.isphysic ? aProperties.difcol.rgb[2] : aProperties.matcol.rgb[2]) * aProperties.diff,
322 -1.f /* no texture */);
25ef750e 323
324 theMaterial.Specular = BVH_Vec4f (
325 (aProperties.isphysic ? aProperties.speccol.rgb[0] : 1.f) * aProperties.spec,
326 (aProperties.isphysic ? aProperties.speccol.rgb[1] : 1.f) * aProperties.spec,
327 (aProperties.isphysic ? aProperties.speccol.rgb[2] : 1.f) * aProperties.spec,
328 aProperties.shine);
329
91c60b57 330 theMaterial.Emission = BVH_Vec4f (
331 (aProperties.isphysic ? aProperties.emscol.rgb[0] : aProperties.matcol.rgb[0]) * aProperties.emsv,
332 (aProperties.isphysic ? aProperties.emscol.rgb[1] : aProperties.matcol.rgb[1]) * aProperties.emsv,
333 (aProperties.isphysic ? aProperties.emscol.rgb[2] : aProperties.matcol.rgb[2]) * aProperties.emsv,
334 1.f);
e276548b 335
91c60b57 336 theMaterial.Transparency = BVH_Vec4f (aProperties.trans,
25ef750e 337 1.f - aProperties.trans,
338 aProperties.index == 0 ? 1.f : aProperties.index,
339 aProperties.index == 0 ? 1.f : 1.f / aProperties.index);
e276548b 340
25ef750e 341 const Standard_ShortReal aMaxRefl = Max (theMaterial.Diffuse.x() + theMaterial.Specular.x(),
342 Max (theMaterial.Diffuse.y() + theMaterial.Specular.y(),
343 theMaterial.Diffuse.z() + theMaterial.Specular.z()));
e276548b 344
25ef750e 345 const Standard_ShortReal aReflectionScale = 0.75f / aMaxRefl;
346
347 theMaterial.Reflection = BVH_Vec4f (
348 aProperties.speccol.rgb[0] * aProperties.spec * aReflectionScale,
349 aProperties.speccol.rgb[1] * aProperties.spec * aReflectionScale,
350 aProperties.speccol.rgb[2] * aProperties.spec * aReflectionScale,
351 0.f);
352
189f85a3 353 // Serialize physically-based material properties
354 const Graphic3d_BSDF& aBSDF = aProperties.BSDF;
355
356 theMaterial.BSDF.Le = BVH_Vec4f (aBSDF.Le, 0.f);
357 theMaterial.BSDF.Kd = BVH_Vec4f (aBSDF.Kd, -1.f /* no tex */);
358 theMaterial.BSDF.Kr = BVH_Vec4f (aBSDF.Kr, 0.f);
359 theMaterial.BSDF.Kt = BVH_Vec4f (aBSDF.Kt, 0.f);
360 theMaterial.BSDF.Ks = BVH_Vec4f (aBSDF.Ks, aBSDF.Roughness);
361
362 theMaterial.BSDF.Fresnel = aBSDF.Fresnel.Serialize();
363
364 theMaterial.BSDF.Absorption = BVH_Vec4f (aBSDF.AbsorptionColor,
365 aBSDF.AbsorptionCoeff);
366
367 // Handle material textures
25ef750e 368 if (theAspect->DoTextureMap())
369 {
91c60b57 370 if (theGlContext->arbTexBindless != NULL)
25ef750e 371 {
189f85a3 372 buildTextureTransform (theAspect->TextureParams(), theMaterial.TextureTransform);
91c60b57 373
189f85a3 374 // write texture ID to diffuse w-component
375 theMaterial.Diffuse.w() = theMaterial.BSDF.Kd.w() =
376 static_cast<Standard_ShortReal> (myRaytraceGeometry.AddTexture (theAspect->TextureRes (theGlContext)));
25ef750e 377 }
378 else if (!myIsRaytraceWarnTextures)
379 {
91c60b57 380 const TCollection_ExtendedString aWarnMessage =
381 "Warning: texturing in Ray-Trace requires GL_ARB_bindless_texture extension which is missing. "
382 "Please try to update graphics card driver. At the moment textures will be ignored.";
383
384 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
385 GL_DEBUG_TYPE_PORTABILITY_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aWarnMessage);
386
25ef750e 387 myIsRaytraceWarnTextures = Standard_True;
388 }
389 }
e276548b 390
91c60b57 391 return theMaterial;
e276548b 392}
393
394// =======================================================================
189f85a3 395// function : addRaytraceStructure
e276548b 396// purpose : Adds OpenGL structure to ray-traced scene geometry
397// =======================================================================
91c60b57 398Standard_Boolean OpenGl_View::addRaytraceStructure (const OpenGl_Structure* theStructure,
399 const Handle(OpenGl_Context)& theGlContext)
e276548b 400{
e276548b 401 if (!theStructure->IsVisible())
402 {
d4aaad5b 403 myStructureStates[theStructure] = StructState (theStructure);
91c60b57 404
e276548b 405 return Standard_True;
406 }
407
408 // Get structure material
68333c8f 409 Standard_Integer aStructMatID = -1;
e276548b 410
411 if (theStructure->AspectFace() != NULL)
412 {
265d4508 413 aStructMatID = static_cast<Standard_Integer> (myRaytraceGeometry.Materials.size());
e276548b 414
91c60b57 415 OpenGl_RaytraceMaterial aStructMaterial = convertMaterial (theStructure->AspectFace(), theGlContext);
e276548b 416
265d4508 417 myRaytraceGeometry.Materials.push_back (aStructMaterial);
e276548b 418 }
419
91c60b57 420 Standard_ShortReal aStructTransform[16];
421
0717ddc1 422 if (theStructure->Transformation()->mat != NULL)
423 {
0717ddc1 424 for (Standard_Integer i = 0; i < 4; ++i)
425 {
426 for (Standard_Integer j = 0; j < 4; ++j)
427 {
428 aStructTransform[j * 4 + i] = theStructure->Transformation()->mat[i][j];
429 }
430 }
431 }
432
91c60b57 433 Standard_Boolean aResult = addRaytraceGroups (theStructure, aStructMatID,
434 theStructure->Transformation()->mat ? aStructTransform : NULL, theGlContext);
0717ddc1 435
436 // Process all connected OpenGL structures
d4aaad5b 437 const OpenGl_Structure* anInstanced = theStructure->InstancedStructure();
438
439 if (anInstanced != NULL && anInstanced->IsRaytracable())
0717ddc1 440 {
d4aaad5b 441 aResult &= addRaytraceGroups (anInstanced, aStructMatID,
442 theStructure->Transformation()->mat ? aStructTransform : NULL, theGlContext);
0717ddc1 443 }
444
d4aaad5b 445 myStructureStates[theStructure] = StructState (theStructure);
91c60b57 446
447 return aResult;
0717ddc1 448}
449
450// =======================================================================
189f85a3 451// function : addRaytraceGroups
0717ddc1 452// purpose : Adds OpenGL groups to ray-traced scene geometry
453// =======================================================================
91c60b57 454Standard_Boolean OpenGl_View::addRaytraceGroups (const OpenGl_Structure* theStructure,
455 const Standard_Integer theStructMat,
456 const Standard_ShortReal* theTransform,
457 const Handle(OpenGl_Context)& theGlContext)
0717ddc1 458{
b64d84be 459 for (OpenGl_Structure::GroupIterator aGroupIter (theStructure->DrawGroups()); aGroupIter.More(); aGroupIter.Next())
e276548b 460 {
461 // Get group material
68333c8f 462 Standard_Integer aGroupMatID = -1;
b64d84be 463 if (aGroupIter.Value()->AspectFace() != NULL)
e276548b 464 {
265d4508 465 aGroupMatID = static_cast<Standard_Integer> (myRaytraceGeometry.Materials.size());
e276548b 466
91c60b57 467 OpenGl_RaytraceMaterial aGroupMaterial = convertMaterial (
468 aGroupIter.Value()->AspectFace(), theGlContext);
e276548b 469
265d4508 470 myRaytraceGeometry.Materials.push_back (aGroupMaterial);
e276548b 471 }
472
91c60b57 473 Standard_Integer aMatID = aGroupMatID < 0 ? theStructMat : aGroupMatID;
265d4508 474 if (aMatID < 0)
e276548b 475 {
265d4508 476 aMatID = static_cast<Standard_Integer> (myRaytraceGeometry.Materials.size());
e276548b 477
265d4508 478 myRaytraceGeometry.Materials.push_back (OpenGl_RaytraceMaterial());
e276548b 479 }
480
265d4508 481 // Add OpenGL elements from group (extract primitives arrays and aspects)
b64d84be 482 for (const OpenGl_ElementNode* aNode = aGroupIter.Value()->FirstNode(); aNode != NULL; aNode = aNode->next)
e276548b 483 {
5322131b 484 OpenGl_AspectFace* anAspect = dynamic_cast<OpenGl_AspectFace*> (aNode->elem);
91c60b57 485
5322131b 486 if (anAspect != NULL)
e276548b 487 {
5322131b 488 aMatID = static_cast<Standard_Integer> (myRaytraceGeometry.Materials.size());
e276548b 489
91c60b57 490 OpenGl_RaytraceMaterial aMaterial = convertMaterial (anAspect, theGlContext);
e276548b 491
5322131b 492 myRaytraceGeometry.Materials.push_back (aMaterial);
e276548b 493 }
5322131b 494 else
e276548b 495 {
496 OpenGl_PrimitiveArray* aPrimArray = dynamic_cast<OpenGl_PrimitiveArray*> (aNode->elem);
84c71f29 497
e276548b 498 if (aPrimArray != NULL)
499 {
8d3f219f 500 std::map<Standard_Size, OpenGl_TriangleSet*>::iterator aSetIter = myArrayToTrianglesMap.find (aPrimArray->GetUID());
501
84c71f29 502 if (aSetIter != myArrayToTrianglesMap.end())
503 {
504 OpenGl_TriangleSet* aSet = aSetIter->second;
25ef750e 505
84c71f29 506 BVH_Transform<Standard_ShortReal, 4>* aTransform = new BVH_Transform<Standard_ShortReal, 4>();
507
508 if (theTransform != NULL)
509 {
510 aTransform->SetTransform (*(reinterpret_cast<const BVH_Mat4f*> (theTransform)));
511 }
91c60b57 512
84c71f29 513 aSet->SetProperties (aTransform);
514
25ef750e 515 if (aSet->MaterialIndex() != OpenGl_TriangleSet::INVALID_MATERIAL && aSet->MaterialIndex() != aMatID)
84c71f29 516 {
517 aSet->SetMaterialIndex (aMatID);
518 }
519 }
520 else
521 {
25ef750e 522 NCollection_Handle<BVH_Object<Standard_ShortReal, 3> > aSet =
91c60b57 523 addRaytracePrimitiveArray (aPrimArray, aMatID, 0);
265d4508 524
84c71f29 525 if (!aSet.IsNull())
526 {
527 BVH_Transform<Standard_ShortReal, 4>* aTransform = new BVH_Transform<Standard_ShortReal, 4>;
528
529 if (theTransform != NULL)
530 {
531 aTransform->SetTransform (*(reinterpret_cast<const BVH_Mat4f*> (theTransform)));
532 }
533
534 aSet->SetProperties (aTransform);
535
536 myRaytraceGeometry.Objects().Append (aSet);
537 }
538 }
e276548b 539 }
540 }
541 }
e276548b 542 }
543
e276548b 544 return Standard_True;
545}
546
547// =======================================================================
189f85a3 548// function : addRaytracePrimitiveArray
e276548b 549// purpose : Adds OpenGL primitive array to ray-traced scene geometry
550// =======================================================================
91c60b57 551OpenGl_TriangleSet* OpenGl_View::addRaytracePrimitiveArray (const OpenGl_PrimitiveArray* theArray,
552 const Standard_Integer theMaterial,
553 const OpenGl_Mat4* theTransform)
e276548b 554{
91c60b57 555 const Handle(Graphic3d_BoundBuffer)& aBounds = theArray->Bounds();
871fa103 556 const Handle(Graphic3d_IndexBuffer)& anIndices = theArray->Indices();
557 const Handle(Graphic3d_Buffer)& anAttribs = theArray->Attributes();
91c60b57 558
871fa103 559 if (theArray->DrawMode() < GL_TRIANGLES
91c60b57 560 #ifndef GL_ES_VERSION_2_0
871fa103 561 || theArray->DrawMode() > GL_POLYGON
ca3c13d1 562 #else
563 || theArray->DrawMode() > GL_TRIANGLE_FAN
564 #endif
871fa103 565 || anAttribs.IsNull())
e276548b 566 {
265d4508 567 return NULL;
e276548b 568 }
569
25ef750e 570 OpenGl_Mat4 aNormalMatrix;
571
572 if (theTransform != NULL)
573 {
574 Standard_ASSERT_RETURN (theTransform->Inverted (aNormalMatrix),
575 "Error: Failed to compute normal transformation matrix", NULL);
576
577 aNormalMatrix.Transpose();
578 }
579
8d3f219f 580 OpenGl_TriangleSet* aSet = new OpenGl_TriangleSet (theArray->GetUID());
e276548b 581 {
871fa103 582 aSet->Vertices.reserve (anAttribs->NbElements);
91c60b57 583 aSet->Normals.reserve (anAttribs->NbElements);
584 aSet->TexCrds.reserve (anAttribs->NbElements);
25ef750e 585
871fa103 586 const size_t aVertFrom = aSet->Vertices.size();
91c60b57 587
871fa103 588 for (Standard_Integer anAttribIter = 0; anAttribIter < anAttribs->NbAttributes; ++anAttribIter)
265d4508 589 {
871fa103 590 const Graphic3d_Attribute& anAttrib = anAttribs->Attribute (anAttribIter);
591 const size_t anOffset = anAttribs->AttributeOffset (anAttribIter);
592 if (anAttrib.Id == Graphic3d_TOA_POS)
593 {
594 if (anAttrib.DataType == Graphic3d_TOD_VEC3
595 || anAttrib.DataType == Graphic3d_TOD_VEC4)
596 {
597 for (Standard_Integer aVertIter = 0; aVertIter < anAttribs->NbElements; ++aVertIter)
598 {
25ef750e 599 aSet->Vertices.push_back (
91c60b57 600 *reinterpret_cast<const Graphic3d_Vec3*> (anAttribs->value (aVertIter) + anOffset));
871fa103 601 }
602 }
603 else if (anAttrib.DataType == Graphic3d_TOD_VEC2)
604 {
605 for (Standard_Integer aVertIter = 0; aVertIter < anAttribs->NbElements; ++aVertIter)
606 {
91c60b57 607 const Standard_ShortReal* aCoords =
608 reinterpret_cast<const Standard_ShortReal*> (anAttribs->value (aVertIter) + anOffset);
609
610 aSet->Vertices.push_back (BVH_Vec3f (aCoords[0], aCoords[1], 0.0f));
871fa103 611 }
612 }
613 }
614 else if (anAttrib.Id == Graphic3d_TOA_NORM)
615 {
616 if (anAttrib.DataType == Graphic3d_TOD_VEC3
617 || anAttrib.DataType == Graphic3d_TOD_VEC4)
618 {
619 for (Standard_Integer aVertIter = 0; aVertIter < anAttribs->NbElements; ++aVertIter)
620 {
25ef750e 621 aSet->Normals.push_back (
91c60b57 622 *reinterpret_cast<const Graphic3d_Vec3*> (anAttribs->value (aVertIter) + anOffset));
25ef750e 623 }
624 }
625 }
626 else if (anAttrib.Id == Graphic3d_TOA_UV)
627 {
628 if (anAttrib.DataType == Graphic3d_TOD_VEC2)
629 {
630 for (Standard_Integer aVertIter = 0; aVertIter < anAttribs->NbElements; ++aVertIter)
631 {
632 aSet->TexCrds.push_back (
91c60b57 633 *reinterpret_cast<const Graphic3d_Vec2*> (anAttribs->value (aVertIter) + anOffset));
871fa103 634 }
635 }
636 }
265d4508 637 }
e276548b 638
871fa103 639 if (aSet->Normals.size() != aSet->Vertices.size())
265d4508 640 {
871fa103 641 for (Standard_Integer aVertIter = 0; aVertIter < anAttribs->NbElements; ++aVertIter)
265d4508 642 {
25ef750e 643 aSet->Normals.push_back (BVH_Vec3f());
644 }
645 }
646
647 if (aSet->TexCrds.size() != aSet->Vertices.size())
648 {
649 for (Standard_Integer aVertIter = 0; aVertIter < anAttribs->NbElements; ++aVertIter)
650 {
651 aSet->TexCrds.push_back (BVH_Vec2f());
265d4508 652 }
871fa103 653 }
e276548b 654
25ef750e 655 if (theTransform != NULL)
871fa103 656 {
657 for (size_t aVertIter = aVertFrom; aVertIter < aSet->Vertices.size(); ++aVertIter)
658 {
25ef750e 659 BVH_Vec3f& aVertex = aSet->Vertices[aVertIter];
660
91c60b57 661 BVH_Vec4f aTransVertex = *theTransform *
662 BVH_Vec4f (aVertex.x(), aVertex.y(), aVertex.z(), 1.f);
25ef750e 663
664 aVertex = BVH_Vec3f (aTransVertex.x(), aTransVertex.y(), aTransVertex.z());
871fa103 665 }
666 for (size_t aVertIter = aVertFrom; aVertIter < aSet->Normals.size(); ++aVertIter)
667 {
25ef750e 668 BVH_Vec3f& aNormal = aSet->Normals[aVertIter];
669
91c60b57 670 BVH_Vec4f aTransNormal = aNormalMatrix *
671 BVH_Vec4f (aNormal.x(), aNormal.y(), aNormal.z(), 0.f);
25ef750e 672
673 aNormal = BVH_Vec3f (aTransNormal.x(), aTransNormal.y(), aTransNormal.z());
871fa103 674 }
e276548b 675 }
676
871fa103 677 if (!aBounds.IsNull())
265d4508 678 {
91c60b57 679 for (Standard_Integer aBound = 0, aBoundStart = 0; aBound < aBounds->NbBounds; ++aBound)
265d4508 680 {
871fa103 681 const Standard_Integer aVertNum = aBounds->Bounds[aBound];
e276548b 682
91c60b57 683 if (!addRaytraceVertexIndices (*aSet, theMaterial, aVertNum, aBoundStart, *theArray))
265d4508 684 {
685 delete aSet;
686 return NULL;
687 }
688
689 aBoundStart += aVertNum;
690 }
691 }
692 else
e276548b 693 {
871fa103 694 const Standard_Integer aVertNum = !anIndices.IsNull() ? anIndices->NbElements : anAttribs->NbElements;
e276548b 695
91c60b57 696 if (!addRaytraceVertexIndices (*aSet, theMaterial, aVertNum, 0, *theArray))
e276548b 697 {
265d4508 698 delete aSet;
699 return NULL;
e276548b 700 }
e276548b 701 }
702 }
e276548b 703
265d4508 704 if (aSet->Size() != 0)
91c60b57 705 {
265d4508 706 aSet->MarkDirty();
91c60b57 707 }
e276548b 708
265d4508 709 return aSet;
e276548b 710}
711
712// =======================================================================
189f85a3 713// function : addRaytraceVertexIndices
e276548b 714// purpose : Adds vertex indices to ray-traced scene geometry
715// =======================================================================
91c60b57 716Standard_Boolean OpenGl_View::addRaytraceVertexIndices (OpenGl_TriangleSet& theSet,
717 const Standard_Integer theMatID,
718 const Standard_Integer theCount,
719 const Standard_Integer theOffset,
720 const OpenGl_PrimitiveArray& theArray)
e276548b 721{
871fa103 722 switch (theArray.DrawMode())
e276548b 723 {
91c60b57 724 case GL_TRIANGLES: return addRaytraceTriangleArray (theSet, theMatID, theCount, theOffset, theArray.Indices());
725 case GL_TRIANGLE_FAN: return addRaytraceTriangleFanArray (theSet, theMatID, theCount, theOffset, theArray.Indices());
726 case GL_TRIANGLE_STRIP: return addRaytraceTriangleStripArray (theSet, theMatID, theCount, theOffset, theArray.Indices());
ca3c13d1 727 #if !defined(GL_ES_VERSION_2_0)
91c60b57 728 case GL_QUAD_STRIP: return addRaytraceQuadrangleStripArray (theSet, theMatID, theCount, theOffset, theArray.Indices());
729 case GL_QUADS: return addRaytraceQuadrangleArray (theSet, theMatID, theCount, theOffset, theArray.Indices());
730 case GL_POLYGON: return addRaytracePolygonArray (theSet, theMatID, theCount, theOffset, theArray.Indices());
ca3c13d1 731 #endif
e276548b 732 }
91c60b57 733
871fa103 734 return Standard_False;
e276548b 735}
736
737// =======================================================================
189f85a3 738// function : addRaytraceTriangleArray
e276548b 739// purpose : Adds OpenGL triangle array to ray-traced scene geometry
740// =======================================================================
91c60b57 741Standard_Boolean OpenGl_View::addRaytraceTriangleArray (OpenGl_TriangleSet& theSet,
742 const Standard_Integer theMatID,
743 const Standard_Integer theCount,
744 const Standard_Integer theOffset,
745 const Handle(Graphic3d_IndexBuffer)& theIndices)
e276548b 746{
265d4508 747 if (theCount < 3)
91c60b57 748 {
e276548b 749 return Standard_True;
91c60b57 750 }
e276548b 751
871fa103 752 theSet.Elements.reserve (theSet.Elements.size() + theCount / 3);
265d4508 753
871fa103 754 if (!theIndices.IsNull())
e276548b 755 {
265d4508 756 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; aVert += 3)
e276548b 757 {
871fa103 758 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (aVert + 0),
759 theIndices->Index (aVert + 1),
760 theIndices->Index (aVert + 2),
761 theMatID));
e276548b 762 }
763 }
764 else
765 {
265d4508 766 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; aVert += 3)
e276548b 767 {
91c60b57 768 theSet.Elements.push_back (BVH_Vec4i (aVert + 0, aVert + 1, aVert + 2, theMatID));
e276548b 769 }
770 }
771
772 return Standard_True;
773}
774
775// =======================================================================
189f85a3 776// function : addRaytraceTriangleFanArray
e276548b 777// purpose : Adds OpenGL triangle fan array to ray-traced scene geometry
778// =======================================================================
91c60b57 779Standard_Boolean OpenGl_View::addRaytraceTriangleFanArray (OpenGl_TriangleSet& theSet,
780 const Standard_Integer theMatID,
781 const Standard_Integer theCount,
782 const Standard_Integer theOffset,
783 const Handle(Graphic3d_IndexBuffer)& theIndices)
e276548b 784{
265d4508 785 if (theCount < 3)
91c60b57 786 {
e276548b 787 return Standard_True;
91c60b57 788 }
e276548b 789
871fa103 790 theSet.Elements.reserve (theSet.Elements.size() + theCount - 2);
265d4508 791
871fa103 792 if (!theIndices.IsNull())
e276548b 793 {
265d4508 794 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; ++aVert)
e276548b 795 {
871fa103 796 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (theOffset),
797 theIndices->Index (aVert + 1),
798 theIndices->Index (aVert + 2),
799 theMatID));
e276548b 800 }
801 }
802 else
803 {
265d4508 804 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; ++aVert)
e276548b 805 {
871fa103 806 theSet.Elements.push_back (BVH_Vec4i (theOffset,
807 aVert + 1,
808 aVert + 2,
809 theMatID));
e276548b 810 }
811 }
812
813 return Standard_True;
814}
815
816// =======================================================================
189f85a3 817// function : addRaytraceTriangleStripArray
e276548b 818// purpose : Adds OpenGL triangle strip array to ray-traced scene geometry
819// =======================================================================
91c60b57 820Standard_Boolean OpenGl_View::addRaytraceTriangleStripArray (OpenGl_TriangleSet& theSet,
821 const Standard_Integer theMatID,
822 const Standard_Integer theCount,
823 const Standard_Integer theOffset,
824 const Handle(Graphic3d_IndexBuffer)& theIndices)
e276548b 825{
265d4508 826 if (theCount < 3)
91c60b57 827 {
e276548b 828 return Standard_True;
91c60b57 829 }
e276548b 830
871fa103 831 theSet.Elements.reserve (theSet.Elements.size() + theCount - 2);
265d4508 832
871fa103 833 if (!theIndices.IsNull())
e276548b 834 {
265d4508 835 for (Standard_Integer aVert = theOffset, aCW = 0; aVert < theOffset + theCount - 2; ++aVert, aCW = (aCW + 1) % 2)
e276548b 836 {
871fa103 837 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (aVert + aCW ? 1 : 0),
838 theIndices->Index (aVert + aCW ? 0 : 1),
839 theIndices->Index (aVert + 2),
840 theMatID));
e276548b 841 }
842 }
843 else
844 {
265d4508 845 for (Standard_Integer aVert = theOffset, aCW = 0; aVert < theOffset + theCount - 2; ++aVert, aCW = (aCW + 1) % 2)
e276548b 846 {
871fa103 847 theSet.Elements.push_back (BVH_Vec4i (aVert + aCW ? 1 : 0,
848 aVert + aCW ? 0 : 1,
849 aVert + 2,
850 theMatID));
e276548b 851 }
852 }
853
854 return Standard_True;
855}
856
857// =======================================================================
189f85a3 858// function : addRaytraceQuadrangleArray
e276548b 859// purpose : Adds OpenGL quad array to ray-traced scene geometry
860// =======================================================================
91c60b57 861Standard_Boolean OpenGl_View::addRaytraceQuadrangleArray (OpenGl_TriangleSet& theSet,
862 const Standard_Integer theMatID,
863 const Standard_Integer theCount,
864 const Standard_Integer theOffset,
865 const Handle(Graphic3d_IndexBuffer)& theIndices)
e276548b 866{
265d4508 867 if (theCount < 4)
91c60b57 868 {
e276548b 869 return Standard_True;
91c60b57 870 }
e276548b 871
871fa103 872 theSet.Elements.reserve (theSet.Elements.size() + theCount / 2);
265d4508 873
871fa103 874 if (!theIndices.IsNull())
e276548b 875 {
265d4508 876 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 3; aVert += 4)
e276548b 877 {
871fa103 878 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (aVert + 0),
879 theIndices->Index (aVert + 1),
880 theIndices->Index (aVert + 2),
881 theMatID));
882 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (aVert + 0),
883 theIndices->Index (aVert + 2),
884 theIndices->Index (aVert + 3),
885 theMatID));
e276548b 886 }
887 }
888 else
889 {
265d4508 890 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 3; aVert += 4)
e276548b 891 {
871fa103 892 theSet.Elements.push_back (BVH_Vec4i (aVert + 0, aVert + 1, aVert + 2,
893 theMatID));
894 theSet.Elements.push_back (BVH_Vec4i (aVert + 0, aVert + 2, aVert + 3,
895 theMatID));
e276548b 896 }
897 }
898
899 return Standard_True;
900}
901
902// =======================================================================
189f85a3 903// function : addRaytraceQuadrangleStripArray
e276548b 904// purpose : Adds OpenGL quad strip array to ray-traced scene geometry
905// =======================================================================
91c60b57 906Standard_Boolean OpenGl_View::addRaytraceQuadrangleStripArray (OpenGl_TriangleSet& theSet,
907 const Standard_Integer theMatID,
908 const Standard_Integer theCount,
909 const Standard_Integer theOffset,
910 const Handle(Graphic3d_IndexBuffer)& theIndices)
e276548b 911{
265d4508 912 if (theCount < 4)
91c60b57 913 {
e276548b 914 return Standard_True;
91c60b57 915 }
e276548b 916
871fa103 917 theSet.Elements.reserve (theSet.Elements.size() + 2 * theCount - 6);
265d4508 918
871fa103 919 if (!theIndices.IsNull())
e276548b 920 {
265d4508 921 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 3; aVert += 2)
e276548b 922 {
871fa103 923 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (aVert + 0),
924 theIndices->Index (aVert + 1),
925 theIndices->Index (aVert + 2),
926 theMatID));
927
928 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (aVert + 1),
929 theIndices->Index (aVert + 3),
930 theIndices->Index (aVert + 2),
931 theMatID));
e276548b 932 }
933 }
934 else
935 {
265d4508 936 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 3; aVert += 2)
e276548b 937 {
871fa103 938 theSet.Elements.push_back (BVH_Vec4i (aVert + 0,
939 aVert + 1,
940 aVert + 2,
941 theMatID));
942
943 theSet.Elements.push_back (BVH_Vec4i (aVert + 1,
944 aVert + 3,
945 aVert + 2,
946 theMatID));
e276548b 947 }
948 }
949
950 return Standard_True;
951}
952
953// =======================================================================
189f85a3 954// function : addRaytracePolygonArray
e276548b 955// purpose : Adds OpenGL polygon array to ray-traced scene geometry
956// =======================================================================
91c60b57 957Standard_Boolean OpenGl_View::addRaytracePolygonArray (OpenGl_TriangleSet& theSet,
958 const Standard_Integer theMatID,
959 const Standard_Integer theCount,
960 const Standard_Integer theOffset,
961 const Handle(Graphic3d_IndexBuffer)& theIndices)
e276548b 962{
265d4508 963 if (theCount < 3)
91c60b57 964 {
e276548b 965 return Standard_True;
91c60b57 966 }
e276548b 967
871fa103 968 theSet.Elements.reserve (theSet.Elements.size() + theCount - 2);
265d4508 969
871fa103 970 if (!theIndices.IsNull())
e276548b 971 {
265d4508 972 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; ++aVert)
e276548b 973 {
871fa103 974 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (theOffset),
975 theIndices->Index (aVert + 1),
976 theIndices->Index (aVert + 2),
977 theMatID));
e276548b 978 }
979 }
980 else
981 {
265d4508 982 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; ++aVert)
e276548b 983 {
871fa103 984 theSet.Elements.push_back (BVH_Vec4i (theOffset,
985 aVert + 1,
986 aVert + 2,
987 theMatID));
e276548b 988 }
989 }
990
991 return Standard_True;
992}
993
189f85a3 994const TCollection_AsciiString OpenGl_View::ShaderSource::EMPTY_PREFIX;
995
e276548b 996// =======================================================================
fc73a202 997// function : Source
998// purpose : Returns shader source combined with prefix
e276548b 999// =======================================================================
91c60b57 1000TCollection_AsciiString OpenGl_View::ShaderSource::Source() const
e276548b 1001{
f8ae3605 1002 const TCollection_AsciiString aVersion = "#version 140";
e276548b 1003
fc73a202 1004 if (myPrefix.IsEmpty())
e276548b 1005 {
fc73a202 1006 return aVersion + "\n" + mySource;
e276548b 1007 }
1008
fc73a202 1009 return aVersion + "\n" + myPrefix + "\n" + mySource;
e276548b 1010}
1011
1012// =======================================================================
fc73a202 1013// function : Load
1014// purpose : Loads shader source from specified files
e276548b 1015// =======================================================================
73722cc9 1016Standard_Boolean OpenGl_View::ShaderSource::Load (const TCollection_AsciiString* theFileNames,
1017 const TCollection_AsciiString& thePrefix)
e276548b 1018{
73722cc9 1019 myError.Clear();
fc73a202 1020 mySource.Clear();
73722cc9 1021 TCollection_AsciiString aMissingFiles;
189f85a3 1022 for (Standard_Integer anIndex = 0; !theFileNames[anIndex].IsEmpty(); ++anIndex)
fc73a202 1023 {
1024 OSD_File aFile (theFileNames[anIndex]);
73722cc9 1025 if (aFile.Exists())
1026 {
1027 aFile.Open (OSD_ReadOnly, OSD_Protection());
1028 }
1029 if (!aFile.IsOpen())
1030 {
1031 if (!aMissingFiles.IsEmpty())
1032 {
1033 aMissingFiles += ", ";
1034 }
1035 aMissingFiles += TCollection_AsciiString("'") + theFileNames[anIndex] + "'";
1036 continue;
1037 }
1038 else if (!aMissingFiles.IsEmpty())
1039 {
1040 aFile.Close();
1041 continue;
1042 }
265d4508 1043
fc73a202 1044 TCollection_AsciiString aSource;
fc73a202 1045 aFile.Read (aSource, (Standard_Integer) aFile.Size());
fc73a202 1046 if (!aSource.IsEmpty())
1047 {
1048 mySource += TCollection_AsciiString ("\n") + aSource;
1049 }
fc73a202 1050 aFile.Close();
68333c8f 1051 }
189f85a3 1052
1053 myPrefix = thePrefix;
73722cc9 1054 if (!aMissingFiles.IsEmpty())
1055 {
1056 myError = TCollection_AsciiString("Shader files ") + aMissingFiles + " are missing or inaccessible";
1057 return Standard_False;
1058 }
1059 return Standard_True;
e276548b 1060}
1061
1062// =======================================================================
189f85a3 1063// function : generateShaderPrefix
91c60b57 1064// purpose : Generates shader prefix based on current ray-tracing options
1065// =======================================================================
1066TCollection_AsciiString OpenGl_View::generateShaderPrefix (const Handle(OpenGl_Context)& theGlContext) const
1067{
1068 TCollection_AsciiString aPrefixString =
1069 TCollection_AsciiString ("#define STACK_SIZE ") + TCollection_AsciiString (myRaytraceParameters.StackSize) + "\n" +
1070 TCollection_AsciiString ("#define NB_BOUNCES ") + TCollection_AsciiString (myRaytraceParameters.NbBounces);
1071
1072 if (myRaytraceParameters.TransparentShadows)
1073 {
1074 aPrefixString += TCollection_AsciiString ("\n#define TRANSPARENT_SHADOWS");
1075 }
1076
f483f2ed 1077 // If OpenGL driver supports bindless textures and texturing
1078 // is actually used, activate texturing in ray-tracing mode
1079 if (myRaytraceParameters.UseBindlessTextures && theGlContext->arbTexBindless != NULL)
91c60b57 1080 {
1081 aPrefixString += TCollection_AsciiString ("\n#define USE_TEXTURES") +
1082 TCollection_AsciiString ("\n#define MAX_TEX_NUMBER ") + TCollection_AsciiString (OpenGl_RaytraceGeometry::MAX_TEX_NUMBER);
1083 }
1084
189f85a3 1085 if (myRaytraceParameters.GlobalIllumination)
1086 {
1087 aPrefixString += TCollection_AsciiString ("\n#define PATH_TRACING");
1088 }
1089
91c60b57 1090 return aPrefixString;
1091}
1092
1093// =======================================================================
189f85a3 1094// function : safeFailBack
91c60b57 1095// purpose : Performs safe exit when shaders initialization fails
1096// =======================================================================
1097Standard_Boolean OpenGl_View::safeFailBack (const TCollection_ExtendedString& theMessage,
1098 const Handle(OpenGl_Context)& theGlContext)
1099{
1100 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1101 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, theMessage);
1102
1103 myRaytraceInitStatus = OpenGl_RT_FAIL;
1104
1105 releaseRaytraceResources (theGlContext);
1106
1107 return Standard_False;
1108}
1109
1110// =======================================================================
189f85a3 1111// function : initShader
fc73a202 1112// purpose : Creates new shader object with specified source
e276548b 1113// =======================================================================
91c60b57 1114Handle(OpenGl_ShaderObject) OpenGl_View::initShader (const GLenum theType,
1115 const ShaderSource& theSource,
1116 const Handle(OpenGl_Context)& theGlContext)
e276548b 1117{
fc73a202 1118 Handle(OpenGl_ShaderObject) aShader = new OpenGl_ShaderObject (theType);
e276548b 1119
91c60b57 1120 if (!aShader->Create (theGlContext))
e276548b 1121 {
07039714 1122 const TCollection_ExtendedString aMessage = TCollection_ExtendedString ("Error: Failed to create ") +
1123 (theType == GL_VERTEX_SHADER ? "vertex" : "fragment") + " shader object";
1124
91c60b57 1125 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
fc73a202 1126 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
e276548b 1127
91c60b57 1128 aShader->Release (theGlContext.operator->());
e276548b 1129
fc73a202 1130 return Handle(OpenGl_ShaderObject)();
e276548b 1131 }
5322131b 1132
91c60b57 1133 if (!aShader->LoadSource (theGlContext, theSource.Source()))
fc73a202 1134 {
07039714 1135 const TCollection_ExtendedString aMessage = TCollection_ExtendedString ("Error: Failed to set ") +
1136 (theType == GL_VERTEX_SHADER ? "vertex" : "fragment") + " shader source";
1137
91c60b57 1138 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
fc73a202 1139 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
e276548b 1140
91c60b57 1141 aShader->Release (theGlContext.operator->());
e276548b 1142
fc73a202 1143 return Handle(OpenGl_ShaderObject)();
1144 }
e276548b 1145
fc73a202 1146 TCollection_AsciiString aBuildLog;
e276548b 1147
91c60b57 1148 if (!aShader->Compile (theGlContext))
e276548b 1149 {
91c60b57 1150 aShader->FetchInfoLog (theGlContext, aBuildLog);
fc73a202 1151
07039714 1152 const TCollection_ExtendedString aMessage = TCollection_ExtendedString ("Error: Failed to compile ") +
1153 (theType == GL_VERTEX_SHADER ? "vertex" : "fragment") + " shader object:\n" + aBuildLog;
1154
91c60b57 1155 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
07039714 1156 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
e276548b 1157
91c60b57 1158 aShader->Release (theGlContext.operator->());
fc73a202 1159
1160 return Handle(OpenGl_ShaderObject)();
1161 }
91c60b57 1162 else if (theGlContext->caps->glslWarnings)
fc73a202 1163 {
91c60b57 1164 aShader->FetchInfoLog (theGlContext, aBuildLog);
07039714 1165
1166 if (!aBuildLog.IsEmpty() && !aBuildLog.IsEqual ("No errors.\n"))
fc73a202 1167 {
07039714 1168 const TCollection_ExtendedString aMessage = TCollection_ExtendedString (theType == GL_VERTEX_SHADER ?
1169 "Vertex" : "Fragment") + " shader was compiled with following warnings:\n" + aBuildLog;
1170
91c60b57 1171 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
07039714 1172 GL_DEBUG_TYPE_PORTABILITY_ARB, 0, GL_DEBUG_SEVERITY_LOW_ARB, aMessage);
fc73a202 1173 }
07039714 1174 }
e276548b 1175
fc73a202 1176 return aShader;
e276548b 1177}
1178
1179// =======================================================================
189f85a3 1180// function : initProgram
1181// purpose : Creates GLSL program from the given shader objects
1182// =======================================================================
1183Handle(OpenGl_ShaderProgram) OpenGl_View::initProgram (const Handle(OpenGl_Context)& theGlContext,
1184 const Handle(OpenGl_ShaderObject)& theVertShader,
1185 const Handle(OpenGl_ShaderObject)& theFragShader)
1186{
1187 Handle(OpenGl_ShaderProgram) aProgram = new OpenGl_ShaderProgram;
1188
1189 if (!aProgram->Create (theGlContext))
1190 {
1191 theVertShader->Release (theGlContext.operator->());
1192
1193 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1194 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, "Failed to create shader program");
1195
1196 return Handle(OpenGl_ShaderProgram)();
1197 }
1198
1199 if (!aProgram->AttachShader (theGlContext, theVertShader)
1200 || !aProgram->AttachShader (theGlContext, theFragShader))
1201 {
1202 theVertShader->Release (theGlContext.operator->());
1203
1204 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1205 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, "Failed to attach shader objects");
1206
1207 return Handle(OpenGl_ShaderProgram)();
1208 }
1209
1210 aProgram->SetAttributeName (theGlContext, Graphic3d_TOA_POS, "occVertex");
1211
1212 TCollection_AsciiString aLinkLog;
1213
1214 if (!aProgram->Link (theGlContext))
1215 {
1216 aProgram->FetchInfoLog (theGlContext, aLinkLog);
1217
1218 const TCollection_ExtendedString aMessage = TCollection_ExtendedString (
1219 "Failed to link shader program:\n") + aLinkLog;
1220
1221 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1222 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
1223
1224 return Handle(OpenGl_ShaderProgram)();
1225 }
1226 else if (theGlContext->caps->glslWarnings)
1227 {
1228 myRaytraceProgram->FetchInfoLog (theGlContext, aLinkLog);
1229
1230 if (!aLinkLog.IsEmpty() && !aLinkLog.IsEqual ("No errors.\n"))
1231 {
1232 const TCollection_ExtendedString aMessage = TCollection_ExtendedString (
1233 "Shader program was linked with following warnings:\n") + aLinkLog;
1234
1235 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1236 GL_DEBUG_TYPE_PORTABILITY_ARB, 0, GL_DEBUG_SEVERITY_LOW_ARB, aMessage);
1237 }
1238 }
1239
1240 return aProgram;
1241}
1242
1243// =======================================================================
1244// function : initRaytraceResources
91c60b57 1245// purpose : Initializes OpenGL/GLSL shader programs
e276548b 1246// =======================================================================
91c60b57 1247Standard_Boolean OpenGl_View::initRaytraceResources (const Graphic3d_CView& theCView, const Handle(OpenGl_Context)& theGlContext)
e276548b 1248{
91c60b57 1249 if (myRaytraceInitStatus == OpenGl_RT_FAIL)
1250 {
1251 return Standard_False;
1252 }
265d4508 1253
91c60b57 1254 Standard_Boolean aToRebuildShaders = Standard_False;
265d4508 1255
91c60b57 1256 if (myRaytraceInitStatus == OpenGl_RT_INIT)
1257 {
1258 if (!myIsRaytraceDataValid)
1259 return Standard_True;
07039714 1260
91c60b57 1261 const Standard_Integer aRequiredStackSize =
1262 myRaytraceGeometry.HighLevelTreeDepth() + myRaytraceGeometry.BottomLevelTreeDepth();
e276548b 1263
91c60b57 1264 if (myRaytraceParameters.StackSize < aRequiredStackSize)
1265 {
1266 myRaytraceParameters.StackSize = Max (aRequiredStackSize, THE_DEFAULT_STACK_SIZE);
e276548b 1267
fc73a202 1268 aToRebuildShaders = Standard_True;
1269 }
1270 else
1271 {
bc8c79bb 1272 if (aRequiredStackSize < myRaytraceParameters.StackSize)
fc73a202 1273 {
bc8c79bb 1274 if (myRaytraceParameters.StackSize > THE_DEFAULT_STACK_SIZE)
fc73a202 1275 {
bc8c79bb 1276 myRaytraceParameters.StackSize = Max (aRequiredStackSize, THE_DEFAULT_STACK_SIZE);
fc73a202 1277 aToRebuildShaders = Standard_True;
1278 }
1279 }
1280 }
e276548b 1281
25ef750e 1282 if (theCView.RenderParams.RaytracingDepth != myRaytraceParameters.NbBounces)
bc8c79bb 1283 {
25ef750e 1284 myRaytraceParameters.NbBounces = theCView.RenderParams.RaytracingDepth;
bc8c79bb 1285 aToRebuildShaders = Standard_True;
1286 }
1287
f483f2ed 1288 if (myRaytraceGeometry.HasTextures() != myRaytraceParameters.UseBindlessTextures)
1289 {
1290 myRaytraceParameters.UseBindlessTextures = myRaytraceGeometry.HasTextures();
1291 aToRebuildShaders = Standard_True;
1292 }
1293
bc8c79bb 1294 if (theCView.RenderParams.IsTransparentShadowEnabled != myRaytraceParameters.TransparentShadows)
1295 {
1296 myRaytraceParameters.TransparentShadows = theCView.RenderParams.IsTransparentShadowEnabled;
1297 aToRebuildShaders = Standard_True;
1298 }
1299
189f85a3 1300 if (theCView.RenderParams.IsGlobalIlluminationEnabled != myRaytraceParameters.GlobalIllumination)
1301 {
1302 myRaytraceParameters.GlobalIllumination = theCView.RenderParams.IsGlobalIlluminationEnabled;
1303 aToRebuildShaders = Standard_True;
1304 }
1305
fc73a202 1306 if (aToRebuildShaders)
1307 {
189f85a3 1308 // Reject accumulated frames
1309 myAccumFrames = 0;
e276548b 1310
189f85a3 1311 // We need to update environment texture
91c60b57 1312 myToUpdateEnvironmentMap = Standard_True;
84c71f29 1313
91c60b57 1314 TCollection_AsciiString aPrefixString = generateShaderPrefix (theGlContext);
bc8c79bb 1315
1316#ifdef RAY_TRACE_PRINT_INFO
1317 std::cout << "GLSL prefix string:" << std::endl << aPrefixString << std::endl;
1318#endif
265d4508 1319
bc8c79bb 1320 myRaytraceShaderSource.SetPrefix (aPrefixString);
1321 myPostFSAAShaderSource.SetPrefix (aPrefixString);
265d4508 1322
91c60b57 1323 if (!myRaytraceShader->LoadSource (theGlContext, myRaytraceShaderSource.Source())
1324 || !myPostFSAAShader->LoadSource (theGlContext, myPostFSAAShaderSource.Source()))
fc73a202 1325 {
91c60b57 1326 return safeFailBack ("Failed to load source into ray-tracing fragment shaders", theGlContext);
fc73a202 1327 }
265d4508 1328
91c60b57 1329 if (!myRaytraceShader->Compile (theGlContext)
1330 || !myPostFSAAShader->Compile (theGlContext))
fc73a202 1331 {
91c60b57 1332 return safeFailBack ("Failed to compile ray-tracing fragment shaders", theGlContext);
fc73a202 1333 }
1334
91c60b57 1335 myRaytraceProgram->SetAttributeName (theGlContext, Graphic3d_TOA_POS, "occVertex");
1336 myPostFSAAProgram->SetAttributeName (theGlContext, Graphic3d_TOA_POS, "occVertex");
1337 if (!myRaytraceProgram->Link (theGlContext)
1338 || !myPostFSAAProgram->Link (theGlContext))
fc73a202 1339 {
91c60b57 1340 return safeFailBack ("Failed to initialize vertex attributes for ray-tracing program", theGlContext);
fc73a202 1341 }
1342 }
e276548b 1343 }
1344
91c60b57 1345 if (myRaytraceInitStatus == OpenGl_RT_NONE)
fc73a202 1346 {
91c60b57 1347 if (!theGlContext->IsGlGreaterEqual (3, 1))
fc73a202 1348 {
91c60b57 1349 return safeFailBack ("Ray-tracing requires OpenGL 3.1 and higher", theGlContext);
25ef750e 1350 }
91c60b57 1351 else if (!theGlContext->arbTboRGB32)
25ef750e 1352 {
91c60b57 1353 return safeFailBack ("Ray-tracing requires OpenGL 4.0+ or GL_ARB_texture_buffer_object_rgb32 extension", theGlContext);
1354 }
1355 else if (!theGlContext->arbFBOBlit)
1356 {
1357 return safeFailBack ("Ray-tracing requires EXT_framebuffer_blit extension", theGlContext);
fc73a202 1358 }
5322131b 1359
25ef750e 1360 myRaytraceParameters.NbBounces = theCView.RenderParams.RaytracingDepth;
bc8c79bb 1361
fc73a202 1362 TCollection_AsciiString aFolder = Graphic3d_ShaderProgram::ShadersFolder();
265d4508 1363
fc73a202 1364 if (aFolder.IsEmpty())
1365 {
91c60b57 1366 return safeFailBack ("Failed to locate shaders directory", theGlContext);
fc73a202 1367 }
265d4508 1368
fc73a202 1369 if (myIsRaytraceDataValid)
1370 {
bc8c79bb 1371 myRaytraceParameters.StackSize = Max (THE_DEFAULT_STACK_SIZE,
fc73a202 1372 myRaytraceGeometry.HighLevelTreeDepth() + myRaytraceGeometry.BottomLevelTreeDepth());
1373 }
265d4508 1374
91c60b57 1375 TCollection_AsciiString aPrefixString = generateShaderPrefix (theGlContext);
bc8c79bb 1376
1377#ifdef RAY_TRACE_PRINT_INFO
1378 std::cout << "GLSL prefix string:" << std::endl << aPrefixString << std::endl;
1379#endif
1380
73722cc9 1381 ShaderSource aBasicVertShaderSrc;
fc73a202 1382 {
73722cc9 1383 TCollection_AsciiString aFiles[] = { aFolder + "/RaytraceBase.vs", "" };
1384 if (!aBasicVertShaderSrc.Load (aFiles))
fc73a202 1385 {
73722cc9 1386 return safeFailBack (aBasicVertShaderSrc.ErrorDescription(), theGlContext);
fc73a202 1387 }
73722cc9 1388 }
265d4508 1389
73722cc9 1390 {
07039714 1391 TCollection_AsciiString aFiles[] = { aFolder + "/RaytraceBase.fs",
189f85a3 1392 aFolder + "/PathtraceBase.fs",
1393 aFolder + "/RaytraceRender.fs",
1394 "" };
73722cc9 1395 if (!myRaytraceShaderSource.Load (aFiles, aPrefixString))
1396 {
1397 return safeFailBack (myRaytraceShaderSource.ErrorDescription(), theGlContext);
1398 }
e276548b 1399
73722cc9 1400 Handle(OpenGl_ShaderObject) aBasicVertShader = initShader (GL_VERTEX_SHADER, aBasicVertShaderSrc, theGlContext);
1401 if (aBasicVertShader.IsNull())
1402 {
1403 return safeFailBack ("Failed to initialize ray-trace vertex shader", theGlContext);
1404 }
265d4508 1405
91c60b57 1406 myRaytraceShader = initShader (GL_FRAGMENT_SHADER, myRaytraceShaderSource, theGlContext);
fc73a202 1407 if (myRaytraceShader.IsNull())
1408 {
91c60b57 1409 aBasicVertShader->Release (theGlContext.operator->());
91c60b57 1410 return safeFailBack ("Failed to initialize ray-trace fragment shader", theGlContext);
fc73a202 1411 }
265d4508 1412
189f85a3 1413 myRaytraceProgram = initProgram (theGlContext, aBasicVertShader, myRaytraceShader);
189f85a3 1414 if (myRaytraceProgram.IsNull())
fc73a202 1415 {
189f85a3 1416 return safeFailBack ("Failed to initialize ray-trace shader program", theGlContext);
fc73a202 1417 }
1418 }
265d4508 1419
fc73a202 1420 {
73722cc9 1421 TCollection_AsciiString aFiles[] = { aFolder + "/RaytraceBase.fs",
1422 aFolder + "/RaytraceSmooth.fs",
1423 "" };
1424 if (!myPostFSAAShaderSource.Load (aFiles, aPrefixString))
1425 {
1426 return safeFailBack (myPostFSAAShaderSource.ErrorDescription(), theGlContext);
1427 }
265d4508 1428
73722cc9 1429 Handle(OpenGl_ShaderObject) aBasicVertShader = initShader (GL_VERTEX_SHADER, aBasicVertShaderSrc, theGlContext);
fc73a202 1430 if (aBasicVertShader.IsNull())
1431 {
91c60b57 1432 return safeFailBack ("Failed to initialize FSAA vertex shader", theGlContext);
fc73a202 1433 }
265d4508 1434
91c60b57 1435 myPostFSAAShader = initShader (GL_FRAGMENT_SHADER, myPostFSAAShaderSource, theGlContext);
fc73a202 1436 if (myPostFSAAShader.IsNull())
1437 {
91c60b57 1438 aBasicVertShader->Release (theGlContext.operator->());
91c60b57 1439 return safeFailBack ("Failed to initialize FSAA fragment shader", theGlContext);
fc73a202 1440 }
265d4508 1441
189f85a3 1442 myPostFSAAProgram = initProgram (theGlContext, aBasicVertShader, myPostFSAAShader);
189f85a3 1443 if (myPostFSAAProgram.IsNull())
fc73a202 1444 {
189f85a3 1445 return safeFailBack ("Failed to initialize FSAA shader program", theGlContext);
fc73a202 1446 }
189f85a3 1447 }
fc73a202 1448
189f85a3 1449 {
73722cc9 1450 ShaderSource aDispShaderSrc;
1451 TCollection_AsciiString aFiles[] = { aFolder + "/Display.fs", "" };
1452 if (!aDispShaderSrc.Load (aFiles, aPrefixString))
1453 {
1454 return safeFailBack (aDispShaderSrc.ErrorDescription(), theGlContext);
1455 }
fc73a202 1456
73722cc9 1457 Handle(OpenGl_ShaderObject) aBasicVertShader = initShader (GL_VERTEX_SHADER, aBasicVertShaderSrc, theGlContext);
189f85a3 1458 if (aBasicVertShader.IsNull())
1459 {
1460 return safeFailBack ("Failed to set vertex shader source", theGlContext);
fc73a202 1461 }
1462
73722cc9 1463 Handle(OpenGl_ShaderObject) aDisplayShader = initShader (GL_FRAGMENT_SHADER, aDispShaderSrc, theGlContext);
189f85a3 1464 if (aDisplayShader.IsNull())
fc73a202 1465 {
189f85a3 1466 aBasicVertShader->Release (theGlContext.operator->());
189f85a3 1467 return safeFailBack ("Failed to set display fragment shader source", theGlContext);
07039714 1468 }
fc73a202 1469
189f85a3 1470 myOutImageProgram = initProgram (theGlContext, aBasicVertShader, aDisplayShader);
189f85a3 1471 if (myOutImageProgram.IsNull())
1472 {
1473 return safeFailBack ("Failed to initialize output shader program", theGlContext);
fc73a202 1474 }
e276548b 1475 }
fc73a202 1476 }
e276548b 1477
91c60b57 1478 if (myRaytraceInitStatus == OpenGl_RT_NONE || aToRebuildShaders)
fc73a202 1479 {
1480 for (Standard_Integer anIndex = 0; anIndex < 2; ++anIndex)
e276548b 1481 {
fc73a202 1482 Handle(OpenGl_ShaderProgram)& aShaderProgram =
1483 (anIndex == 0) ? myRaytraceProgram : myPostFSAAProgram;
1484
91c60b57 1485 theGlContext->BindProgram (aShaderProgram);
fc73a202 1486
91c60b57 1487 aShaderProgram->SetSampler (theGlContext,
fc73a202 1488 "uSceneMinPointTexture", OpenGl_RT_SceneMinPointTexture);
91c60b57 1489 aShaderProgram->SetSampler (theGlContext,
fc73a202 1490 "uSceneMaxPointTexture", OpenGl_RT_SceneMaxPointTexture);
91c60b57 1491 aShaderProgram->SetSampler (theGlContext,
fc73a202 1492 "uSceneNodeInfoTexture", OpenGl_RT_SceneNodeInfoTexture);
91c60b57 1493 aShaderProgram->SetSampler (theGlContext,
fc73a202 1494 "uGeometryVertexTexture", OpenGl_RT_GeometryVertexTexture);
91c60b57 1495 aShaderProgram->SetSampler (theGlContext,
fc73a202 1496 "uGeometryNormalTexture", OpenGl_RT_GeometryNormalTexture);
91c60b57 1497 aShaderProgram->SetSampler (theGlContext,
25ef750e 1498 "uGeometryTexCrdTexture", OpenGl_RT_GeometryTexCrdTexture);
91c60b57 1499 aShaderProgram->SetSampler (theGlContext,
25ef750e 1500 "uGeometryTriangTexture", OpenGl_RT_GeometryTriangTexture);
91c60b57 1501 aShaderProgram->SetSampler (theGlContext,
84c71f29 1502 "uSceneTransformTexture", OpenGl_RT_SceneTransformTexture);
91c60b57 1503 aShaderProgram->SetSampler (theGlContext,
84c71f29 1504 "uEnvironmentMapTexture", OpenGl_RT_EnvironmentMapTexture);
91c60b57 1505 aShaderProgram->SetSampler (theGlContext,
25ef750e 1506 "uRaytraceMaterialTexture", OpenGl_RT_RaytraceMaterialTexture);
91c60b57 1507 aShaderProgram->SetSampler (theGlContext,
25ef750e 1508 "uRaytraceLightSrcTexture", OpenGl_RT_RaytraceLightSrcTexture);
fc73a202 1509
91c60b57 1510 aShaderProgram->SetSampler (theGlContext,
a89742cf 1511 "uOpenGlColorTexture", OpenGl_RT_OpenGlColorTexture);
91c60b57 1512 aShaderProgram->SetSampler (theGlContext,
a89742cf 1513 "uOpenGlDepthTexture", OpenGl_RT_OpenGlDepthTexture);
1514
fc73a202 1515 if (anIndex == 1)
1516 {
91c60b57 1517 aShaderProgram->SetSampler (theGlContext,
189f85a3 1518 "uFSAAInputTexture", OpenGl_RT_FsaaInputTexture);
1519 }
1520 else
1521 {
1522 aShaderProgram->SetSampler (theGlContext,
1523 "uAccumTexture", OpenGl_RT_PrevAccumTexture);
fc73a202 1524 }
265d4508 1525
fc73a202 1526 myUniformLocations[anIndex][OpenGl_RT_aPosition] =
91c60b57 1527 aShaderProgram->GetAttributeLocation (theGlContext, "occVertex");
fc73a202 1528
1529 myUniformLocations[anIndex][OpenGl_RT_uOriginLB] =
91c60b57 1530 aShaderProgram->GetUniformLocation (theGlContext, "uOriginLB");
fc73a202 1531 myUniformLocations[anIndex][OpenGl_RT_uOriginRB] =
91c60b57 1532 aShaderProgram->GetUniformLocation (theGlContext, "uOriginRB");
fc73a202 1533 myUniformLocations[anIndex][OpenGl_RT_uOriginLT] =
91c60b57 1534 aShaderProgram->GetUniformLocation (theGlContext, "uOriginLT");
fc73a202 1535 myUniformLocations[anIndex][OpenGl_RT_uOriginRT] =
91c60b57 1536 aShaderProgram->GetUniformLocation (theGlContext, "uOriginRT");
fc73a202 1537 myUniformLocations[anIndex][OpenGl_RT_uDirectLB] =
91c60b57 1538 aShaderProgram->GetUniformLocation (theGlContext, "uDirectLB");
fc73a202 1539 myUniformLocations[anIndex][OpenGl_RT_uDirectRB] =
91c60b57 1540 aShaderProgram->GetUniformLocation (theGlContext, "uDirectRB");
fc73a202 1541 myUniformLocations[anIndex][OpenGl_RT_uDirectLT] =
91c60b57 1542 aShaderProgram->GetUniformLocation (theGlContext, "uDirectLT");
fc73a202 1543 myUniformLocations[anIndex][OpenGl_RT_uDirectRT] =
91c60b57 1544 aShaderProgram->GetUniformLocation (theGlContext, "uDirectRT");
25ef750e 1545 myUniformLocations[anIndex][OpenGl_RT_uUnviewMat] =
91c60b57 1546 aShaderProgram->GetUniformLocation (theGlContext, "uUnviewMat");
fc73a202 1547
1548 myUniformLocations[anIndex][OpenGl_RT_uSceneRad] =
91c60b57 1549 aShaderProgram->GetUniformLocation (theGlContext, "uSceneRadius");
fc73a202 1550 myUniformLocations[anIndex][OpenGl_RT_uSceneEps] =
91c60b57 1551 aShaderProgram->GetUniformLocation (theGlContext, "uSceneEpsilon");
25ef750e 1552 myUniformLocations[anIndex][OpenGl_RT_uLightCount] =
91c60b57 1553 aShaderProgram->GetUniformLocation (theGlContext, "uLightCount");
25ef750e 1554 myUniformLocations[anIndex][OpenGl_RT_uLightAmbnt] =
91c60b57 1555 aShaderProgram->GetUniformLocation (theGlContext, "uGlobalAmbient");
fc73a202 1556
1557 myUniformLocations[anIndex][OpenGl_RT_uOffsetX] =
91c60b57 1558 aShaderProgram->GetUniformLocation (theGlContext, "uOffsetX");
fc73a202 1559 myUniformLocations[anIndex][OpenGl_RT_uOffsetY] =
91c60b57 1560 aShaderProgram->GetUniformLocation (theGlContext, "uOffsetY");
fc73a202 1561 myUniformLocations[anIndex][OpenGl_RT_uSamples] =
91c60b57 1562 aShaderProgram->GetUniformLocation (theGlContext, "uSamples");
84c71f29 1563
189f85a3 1564 myUniformLocations[anIndex][OpenGl_RT_uTexSamplersArray] =
91c60b57 1565 aShaderProgram->GetUniformLocation (theGlContext, "uTextureSamplers");
25ef750e 1566
189f85a3 1567 myUniformLocations[anIndex][OpenGl_RT_uShadowsEnabled] =
1568 aShaderProgram->GetUniformLocation (theGlContext, "uShadowsEnabled");
1569 myUniformLocations[anIndex][OpenGl_RT_uReflectEnabled] =
1570 aShaderProgram->GetUniformLocation (theGlContext, "uReflectEnabled");
1571 myUniformLocations[anIndex][OpenGl_RT_uSphereMapEnabled] =
1572 aShaderProgram->GetUniformLocation (theGlContext, "uSphereMapEnabled");
1573 myUniformLocations[anIndex][OpenGl_RT_uSphereMapForBack] =
1574 aShaderProgram->GetUniformLocation (theGlContext, "uSphereMapForBack");
1575
1576 myUniformLocations[anIndex][OpenGl_RT_uSampleWeight] =
1577 aShaderProgram->GetUniformLocation (theGlContext, "uSampleWeight");
1578 myUniformLocations[anIndex][OpenGl_RT_uFrameRndSeed] =
1579 aShaderProgram->GetUniformLocation (theGlContext, "uFrameRndSeed");
1580
1581 myUniformLocations[anIndex][OpenGl_RT_uBackColorTop] =
1582 aShaderProgram->GetUniformLocation (theGlContext, "uBackColorTop");
1583 myUniformLocations[anIndex][OpenGl_RT_uBackColorBot] =
1584 aShaderProgram->GetUniformLocation (theGlContext, "uBackColorBot");
fc73a202 1585 }
265d4508 1586
189f85a3 1587 theGlContext->BindProgram (myOutImageProgram);
1588
1589 myOutImageProgram->SetSampler (theGlContext,
1590 "uInputTexture", OpenGl_RT_PrevAccumTexture);
1591
91c60b57 1592 theGlContext->BindProgram (NULL);
fc73a202 1593 }
265d4508 1594
91c60b57 1595 if (myRaytraceInitStatus != OpenGl_RT_NONE)
fc73a202 1596 {
91c60b57 1597 return myRaytraceInitStatus == OpenGl_RT_INIT;
fc73a202 1598 }
265d4508 1599
fc73a202 1600 if (myRaytraceFBO1.IsNull())
1601 {
189f85a3 1602 myRaytraceFBO1 = new OpenGl_FrameBuffer (GL_RGBA32F);
fc73a202 1603 }
265d4508 1604
fc73a202 1605 if (myRaytraceFBO2.IsNull())
1606 {
189f85a3 1607 myRaytraceFBO2 = new OpenGl_FrameBuffer (GL_RGBA32F);
fc73a202 1608 }
265d4508 1609
fc73a202 1610 const GLfloat aVertices[] = { -1.f, -1.f, 0.f,
1611 -1.f, 1.f, 0.f,
1612 1.f, 1.f, 0.f,
1613 1.f, 1.f, 0.f,
1614 1.f, -1.f, 0.f,
1615 -1.f, -1.f, 0.f };
265d4508 1616
91c60b57 1617 myRaytraceScreenQuad.Init (theGlContext, 3, 6, aVertices);
265d4508 1618
91c60b57 1619 myRaytraceInitStatus = OpenGl_RT_INIT; // initialized in normal way
ca3c13d1 1620
fc73a202 1621 return Standard_True;
1622}
265d4508 1623
fc73a202 1624// =======================================================================
189f85a3 1625// function : nullifyResource
fc73a202 1626// purpose :
1627// =======================================================================
189f85a3 1628inline void nullifyResource (const Handle(OpenGl_Context)& theGlContext,
fc73a202 1629 Handle(OpenGl_Resource)& theResource)
1630{
1631 if (!theResource.IsNull())
1632 {
91c60b57 1633 theResource->Release (theGlContext.operator->());
fc73a202 1634 theResource.Nullify();
1635 }
1636}
265d4508 1637
fc73a202 1638// =======================================================================
189f85a3 1639// function : releaseRaytraceResources
fc73a202 1640// purpose : Releases OpenGL/GLSL shader programs
1641// =======================================================================
91c60b57 1642void OpenGl_View::releaseRaytraceResources (const Handle(OpenGl_Context)& theGlContext)
fc73a202 1643{
189f85a3 1644 nullifyResource (theGlContext, myOpenGlFBO);
1645 nullifyResource (theGlContext, myRaytraceFBO1);
1646 nullifyResource (theGlContext, myRaytraceFBO2);
265d4508 1647
189f85a3 1648 nullifyResource (theGlContext, myRaytraceShader);
1649 nullifyResource (theGlContext, myPostFSAAShader);
265d4508 1650
189f85a3 1651 nullifyResource (theGlContext, myRaytraceProgram);
1652 nullifyResource (theGlContext, myPostFSAAProgram);
1653 nullifyResource (theGlContext, myOutImageProgram);
265d4508 1654
189f85a3 1655 nullifyResource (theGlContext, mySceneNodeInfoTexture);
1656 nullifyResource (theGlContext, mySceneMinPointTexture);
1657 nullifyResource (theGlContext, mySceneMaxPointTexture);
265d4508 1658
189f85a3 1659 nullifyResource (theGlContext, myGeometryVertexTexture);
1660 nullifyResource (theGlContext, myGeometryNormalTexture);
1661 nullifyResource (theGlContext, myGeometryTexCrdTexture);
1662 nullifyResource (theGlContext, myGeometryTriangTexture);
1663 nullifyResource (theGlContext, mySceneTransformTexture);
265d4508 1664
189f85a3 1665 nullifyResource (theGlContext, myRaytraceLightSrcTexture);
1666 nullifyResource (theGlContext, myRaytraceMaterialTexture);
265d4508 1667
fc73a202 1668 if (myRaytraceScreenQuad.IsValid())
91c60b57 1669 myRaytraceScreenQuad.Release (theGlContext.operator->());
1670}
1671
1672// =======================================================================
189f85a3 1673// function : resizeRaytraceBuffers
91c60b57 1674// purpose : Resizes OpenGL frame buffers
1675// =======================================================================
1676Standard_Boolean OpenGl_View::resizeRaytraceBuffers (const Standard_Integer theSizeX,
1677 const Standard_Integer theSizeY,
1678 const Handle(OpenGl_Context)& theGlContext)
1679{
1680 if (myRaytraceFBO1->GetVPSizeX() != theSizeX
1681 || myRaytraceFBO1->GetVPSizeY() != theSizeY)
1682 {
1683 myRaytraceFBO1->Init (theGlContext, theSizeX, theSizeY);
1684 myRaytraceFBO2->Init (theGlContext, theSizeX, theSizeY);
1685 }
1686
1687 return Standard_True;
1688}
1689
1690// =======================================================================
189f85a3 1691// function : updateCamera
91c60b57 1692// purpose : Generates viewing rays for corners of screen quad
1693// =======================================================================
1694void OpenGl_View::updateCamera (const OpenGl_Mat4& theOrientation,
1695 const OpenGl_Mat4& theViewMapping,
1696 OpenGl_Vec3* theOrigins,
1697 OpenGl_Vec3* theDirects,
1698 OpenGl_Mat4& theUnview)
1699{
1700 // compute inverse model-view-projection matrix
1701 (theViewMapping * theOrientation).Inverted (theUnview);
1702
1703 Standard_Integer aOriginIndex = 0;
1704 Standard_Integer aDirectIndex = 0;
1705
1706 for (Standard_Integer aY = -1; aY <= 1; aY += 2)
1707 {
1708 for (Standard_Integer aX = -1; aX <= 1; aX += 2)
1709 {
1710 OpenGl_Vec4 aOrigin (GLfloat(aX),
1711 GLfloat(aY),
1712 -1.0f,
1713 1.0f);
1714
1715 aOrigin = theUnview * aOrigin;
1716
1717 aOrigin.x() = aOrigin.x() / aOrigin.w();
1718 aOrigin.y() = aOrigin.y() / aOrigin.w();
1719 aOrigin.z() = aOrigin.z() / aOrigin.w();
1720
1721 OpenGl_Vec4 aDirect (GLfloat(aX),
1722 GLfloat(aY),
1723 1.0f,
1724 1.0f);
1725
1726 aDirect = theUnview * aDirect;
1727
1728 aDirect.x() = aDirect.x() / aDirect.w();
1729 aDirect.y() = aDirect.y() / aDirect.w();
1730 aDirect.z() = aDirect.z() / aDirect.w();
1731
1732 aDirect = aDirect - aOrigin;
1733
1734 GLdouble aInvLen = 1.0 / sqrt (aDirect.x() * aDirect.x() +
1735 aDirect.y() * aDirect.y() +
1736 aDirect.z() * aDirect.z());
1737
1738 theOrigins[aOriginIndex++] = OpenGl_Vec3 (static_cast<GLfloat> (aOrigin.x()),
1739 static_cast<GLfloat> (aOrigin.y()),
1740 static_cast<GLfloat> (aOrigin.z()));
1741
1742 theDirects[aDirectIndex++] = OpenGl_Vec3 (static_cast<GLfloat> (aDirect.x() * aInvLen),
1743 static_cast<GLfloat> (aDirect.y() * aInvLen),
1744 static_cast<GLfloat> (aDirect.z() * aInvLen));
1745 }
1746 }
fc73a202 1747}
265d4508 1748
fc73a202 1749// =======================================================================
189f85a3 1750// function : uploadRaytraceData
fc73a202 1751// purpose : Uploads ray-trace data to the GPU
1752// =======================================================================
91c60b57 1753Standard_Boolean OpenGl_View::uploadRaytraceData (const Handle(OpenGl_Context)& theGlContext)
fc73a202 1754{
91c60b57 1755 if (!theGlContext->IsGlGreaterEqual (3, 1))
fc73a202 1756 {
265d4508 1757#ifdef RAY_TRACE_PRINT_INFO
fc73a202 1758 std::cout << "Error: OpenGL version is less than 3.1" << std::endl;
265d4508 1759#endif
fc73a202 1760 return Standard_False;
e276548b 1761 }
1762
189f85a3 1763 myAccumFrames = 0; // accumulation should be restarted
1764
25ef750e 1765 /////////////////////////////////////////////////////////////////////////////
1766 // Prepare OpenGL textures
1767
91c60b57 1768 if (theGlContext->arbTexBindless != NULL)
25ef750e 1769 {
1770 // If OpenGL driver supports bindless textures we need
1771 // to get unique 64- bit handles for using on the GPU
91c60b57 1772 if (!myRaytraceGeometry.UpdateTextureHandles (theGlContext))
25ef750e 1773 {
1774#ifdef RAY_TRACE_PRINT_INFO
1775 std::cout << "Error: Failed to get OpenGL texture handles" << std::endl;
1776#endif
1777 return Standard_False;
1778 }
1779 }
1780
265d4508 1781 /////////////////////////////////////////////////////////////////////////////
e2da917a 1782 // Create OpenGL BVH buffers
265d4508 1783
e2da917a 1784 if (mySceneNodeInfoTexture.IsNull()) // create scene BVH buffers
e276548b 1785 {
e2da917a 1786 mySceneNodeInfoTexture = new OpenGl_TextureBufferArb;
1787 mySceneMinPointTexture = new OpenGl_TextureBufferArb;
1788 mySceneMaxPointTexture = new OpenGl_TextureBufferArb;
84c71f29 1789 mySceneTransformTexture = new OpenGl_TextureBufferArb;
265d4508 1790
91c60b57 1791 if (!mySceneNodeInfoTexture->Create (theGlContext)
1792 || !mySceneMinPointTexture->Create (theGlContext)
1793 || !mySceneMaxPointTexture->Create (theGlContext)
1794 || !mySceneTransformTexture->Create (theGlContext))
e276548b 1795 {
fc73a202 1796#ifdef RAY_TRACE_PRINT_INFO
e2da917a 1797 std::cout << "Error: Failed to create scene BVH buffers" << std::endl;
fc73a202 1798#endif
1799 return Standard_False;
1800 }
1801 }
5322131b 1802
e2da917a 1803 if (myGeometryVertexTexture.IsNull()) // create geometry buffers
265d4508 1804 {
fc73a202 1805 myGeometryVertexTexture = new OpenGl_TextureBufferArb;
1806 myGeometryNormalTexture = new OpenGl_TextureBufferArb;
25ef750e 1807 myGeometryTexCrdTexture = new OpenGl_TextureBufferArb;
fc73a202 1808 myGeometryTriangTexture = new OpenGl_TextureBufferArb;
e276548b 1809
91c60b57 1810 if (!myGeometryVertexTexture->Create (theGlContext)
1811 || !myGeometryNormalTexture->Create (theGlContext)
1812 || !myGeometryTexCrdTexture->Create (theGlContext)
1813 || !myGeometryTriangTexture->Create (theGlContext))
fc73a202 1814 {
1815#ifdef RAY_TRACE_PRINT_INFO
1816 std::cout << "Error: Failed to create buffers for triangulation data" << std::endl;
1817#endif
1818 return Standard_False;
1819 }
265d4508 1820 }
5322131b 1821
e2da917a 1822 if (myRaytraceMaterialTexture.IsNull()) // create material buffer
fc73a202 1823 {
1824 myRaytraceMaterialTexture = new OpenGl_TextureBufferArb;
e276548b 1825
91c60b57 1826 if (!myRaytraceMaterialTexture->Create (theGlContext))
fc73a202 1827 {
1828#ifdef RAY_TRACE_PRINT_INFO
1829 std::cout << "Error: Failed to create buffers for material data" << std::endl;
e276548b 1830#endif
fc73a202 1831 return Standard_False;
1832 }
1833 }
e2da917a 1834
84c71f29 1835 /////////////////////////////////////////////////////////////////////////////
1836 // Write transform buffer
1837
1838 BVH_Mat4f* aNodeTransforms = new BVH_Mat4f[myRaytraceGeometry.Size()];
84c71f29 1839
e2da917a 1840 bool aResult = true;
1841
84c71f29 1842 for (Standard_Integer anElemIndex = 0; anElemIndex < myRaytraceGeometry.Size(); ++anElemIndex)
1843 {
1844 OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> (
1845 myRaytraceGeometry.Objects().ChangeValue (anElemIndex).operator->());
1846
1847 const BVH_Transform<Standard_ShortReal, 4>* aTransform =
1848 dynamic_cast<const BVH_Transform<Standard_ShortReal, 4>* > (aTriangleSet->Properties().operator->());
1849
1850 Standard_ASSERT_RETURN (aTransform != NULL,
1851 "OpenGl_TriangleSet does not contain transform", Standard_False);
1852
1853 aNodeTransforms[anElemIndex] = aTransform->Inversed();
84c71f29 1854 }
1855
91c60b57 1856 aResult &= mySceneTransformTexture->Init (theGlContext, 4,
84c71f29 1857 myRaytraceGeometry.Size() * 4, reinterpret_cast<const GLfloat*> (aNodeTransforms));
1858
25ef750e 1859 delete [] aNodeTransforms;
84c71f29 1860
1861 /////////////////////////////////////////////////////////////////////////////
bc8c79bb 1862 // Write geometry and bottom-level BVH buffers
84c71f29 1863
fc73a202 1864 Standard_Size aTotalVerticesNb = 0;
1865 Standard_Size aTotalElementsNb = 0;
1866 Standard_Size aTotalBVHNodesNb = 0;
265d4508 1867
fc73a202 1868 for (Standard_Integer anElemIndex = 0; anElemIndex < myRaytraceGeometry.Size(); ++anElemIndex)
1869 {
1870 OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> (
1871 myRaytraceGeometry.Objects().ChangeValue (anElemIndex).operator->());
265d4508 1872
fc73a202 1873 Standard_ASSERT_RETURN (aTriangleSet != NULL,
1874 "Error: Failed to get triangulation of OpenGL element", Standard_False);
1875
1876 aTotalVerticesNb += aTriangleSet->Vertices.size();
1877 aTotalElementsNb += aTriangleSet->Elements.size();
e276548b 1878
fc73a202 1879 Standard_ASSERT_RETURN (!aTriangleSet->BVH().IsNull(),
1880 "Error: Failed to get bottom-level BVH of OpenGL element", Standard_False);
265d4508 1881
fc73a202 1882 aTotalBVHNodesNb += aTriangleSet->BVH()->NodeInfoBuffer().size();
1883 }
e276548b 1884
e2da917a 1885 aTotalBVHNodesNb += myRaytraceGeometry.BVH()->NodeInfoBuffer().size();
1886
fc73a202 1887 if (aTotalBVHNodesNb != 0)
e276548b 1888 {
e2da917a 1889 aResult &= mySceneNodeInfoTexture->Init (
91c60b57 1890 theGlContext, 4, GLsizei (aTotalBVHNodesNb), static_cast<const GLuint*> (NULL));
e2da917a 1891 aResult &= mySceneMinPointTexture->Init (
91c60b57 1892 theGlContext, 3, GLsizei (aTotalBVHNodesNb), static_cast<const GLfloat*> (NULL));
e2da917a 1893 aResult &= mySceneMaxPointTexture->Init (
91c60b57 1894 theGlContext, 3, GLsizei (aTotalBVHNodesNb), static_cast<const GLfloat*> (NULL));
fc73a202 1895 }
265d4508 1896
fc73a202 1897 if (!aResult)
1898 {
1899#ifdef RAY_TRACE_PRINT_INFO
1900 std::cout << "Error: Failed to upload buffers for bottom-level scene BVH" << std::endl;
1901#endif
1902 return Standard_False;
1903 }
e276548b 1904
fc73a202 1905 if (aTotalElementsNb != 0)
1906 {
25ef750e 1907 aResult &= myGeometryTriangTexture->Init (
91c60b57 1908 theGlContext, 4, GLsizei (aTotalElementsNb), static_cast<const GLuint*> (NULL));
fc73a202 1909 }
265d4508 1910
fc73a202 1911 if (aTotalVerticesNb != 0)
1912 {
25ef750e 1913 aResult &= myGeometryVertexTexture->Init (
91c60b57 1914 theGlContext, 3, GLsizei (aTotalVerticesNb), static_cast<const GLfloat*> (NULL));
25ef750e 1915 aResult &= myGeometryNormalTexture->Init (
91c60b57 1916 theGlContext, 3, GLsizei (aTotalVerticesNb), static_cast<const GLfloat*> (NULL));
25ef750e 1917 aResult &= myGeometryTexCrdTexture->Init (
91c60b57 1918 theGlContext, 2, GLsizei (aTotalVerticesNb), static_cast<const GLfloat*> (NULL));
fc73a202 1919 }
265d4508 1920
fc73a202 1921 if (!aResult)
1922 {
1923#ifdef RAY_TRACE_PRINT_INFO
1924 std::cout << "Error: Failed to upload buffers for scene geometry" << std::endl;
1925#endif
1926 return Standard_False;
1927 }
265d4508 1928
e2da917a 1929 const NCollection_Handle<BVH_Tree<Standard_ShortReal, 3> >& aBVH = myRaytraceGeometry.BVH();
91c60b57 1930
1931 if (aBVH->Length() > 0)
1e99558f 1932 {
91c60b57 1933 aResult &= mySceneNodeInfoTexture->SubData (theGlContext, 0, aBVH->Length(),
1e99558f 1934 reinterpret_cast<const GLuint*> (&aBVH->NodeInfoBuffer().front()));
91c60b57 1935 aResult &= mySceneMinPointTexture->SubData (theGlContext, 0, aBVH->Length(),
1e99558f 1936 reinterpret_cast<const GLfloat*> (&aBVH->MinPointBuffer().front()));
91c60b57 1937 aResult &= mySceneMaxPointTexture->SubData (theGlContext, 0, aBVH->Length(),
1e99558f 1938 reinterpret_cast<const GLfloat*> (&aBVH->MaxPointBuffer().front()));
1939 }
e2da917a 1940
fc73a202 1941 for (Standard_Integer aNodeIdx = 0; aNodeIdx < aBVH->Length(); ++aNodeIdx)
1942 {
1943 if (!aBVH->IsOuter (aNodeIdx))
1944 continue;
265d4508 1945
fc73a202 1946 OpenGl_TriangleSet* aTriangleSet = myRaytraceGeometry.TriangleSet (aNodeIdx);
e276548b 1947
fc73a202 1948 Standard_ASSERT_RETURN (aTriangleSet != NULL,
1949 "Error: Failed to get triangulation of OpenGL element", Standard_False);
e276548b 1950
e2da917a 1951 Standard_Integer aBVHOffset = myRaytraceGeometry.AccelerationOffset (aNodeIdx);
e276548b 1952
fc73a202 1953 Standard_ASSERT_RETURN (aBVHOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET,
1954 "Error: Failed to get offset for bottom-level BVH", Standard_False);
e276548b 1955
e2da917a 1956 const Standard_Integer aBvhBuffersSize = aTriangleSet->BVH()->Length();
265d4508 1957
e2da917a 1958 if (aBvhBuffersSize != 0)
fc73a202 1959 {
91c60b57 1960 aResult &= mySceneNodeInfoTexture->SubData (theGlContext, aBVHOffset, aBvhBuffersSize,
1961 reinterpret_cast<const GLuint*> (&aTriangleSet->BVH()->NodeInfoBuffer().front()));
1962 aResult &= mySceneMinPointTexture->SubData (theGlContext, aBVHOffset, aBvhBuffersSize,
1963 reinterpret_cast<const GLfloat*> (&aTriangleSet->BVH()->MinPointBuffer().front()));
1964 aResult &= mySceneMaxPointTexture->SubData (theGlContext, aBVHOffset, aBvhBuffersSize,
1965 reinterpret_cast<const GLfloat*> (&aTriangleSet->BVH()->MaxPointBuffer().front()));
1966
fc73a202 1967 if (!aResult)
e276548b 1968 {
fc73a202 1969#ifdef RAY_TRACE_PRINT_INFO
1970 std::cout << "Error: Failed to upload buffers for bottom-level scene BVHs" << std::endl;
1971#endif
e276548b 1972 return Standard_False;
1973 }
1974 }
1975
fc73a202 1976 const Standard_Integer aVerticesOffset = myRaytraceGeometry.VerticesOffset (aNodeIdx);
265d4508 1977
fc73a202 1978 Standard_ASSERT_RETURN (aVerticesOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET,
1979 "Error: Failed to get offset for triangulation vertices of OpenGL element", Standard_False);
265d4508 1980
fc73a202 1981 if (!aTriangleSet->Vertices.empty())
1982 {
91c60b57 1983 aResult &= myGeometryNormalTexture->SubData (theGlContext, aVerticesOffset,
1984 GLsizei (aTriangleSet->Normals.size()), reinterpret_cast<const GLfloat*> (&aTriangleSet->Normals.front()));
1985 aResult &= myGeometryTexCrdTexture->SubData (theGlContext, aVerticesOffset,
1986 GLsizei (aTriangleSet->TexCrds.size()), reinterpret_cast<const GLfloat*> (&aTriangleSet->TexCrds.front()));
1987 aResult &= myGeometryVertexTexture->SubData (theGlContext, aVerticesOffset,
1988 GLsizei (aTriangleSet->Vertices.size()), reinterpret_cast<const GLfloat*> (&aTriangleSet->Vertices.front()));
fc73a202 1989 }
e276548b 1990
fc73a202 1991 const Standard_Integer anElementsOffset = myRaytraceGeometry.ElementsOffset (aNodeIdx);
265d4508 1992
fc73a202 1993 Standard_ASSERT_RETURN (anElementsOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET,
1994 "Error: Failed to get offset for triangulation elements of OpenGL element", Standard_False);
e276548b 1995
fc73a202 1996 if (!aTriangleSet->Elements.empty())
e276548b 1997 {
91c60b57 1998 aResult &= myGeometryTriangTexture->SubData (theGlContext, anElementsOffset, GLsizei (aTriangleSet->Elements.size()),
5cff985a 1999 reinterpret_cast<const GLuint*> (&aTriangleSet->Elements.front()));
e276548b 2000 }
265d4508 2001
fc73a202 2002 if (!aResult)
2003 {
2004#ifdef RAY_TRACE_PRINT_INFO
2005 std::cout << "Error: Failed to upload triangulation buffers for OpenGL element" << std::endl;
e276548b 2006#endif
fc73a202 2007 return Standard_False;
2008 }
e276548b 2009 }
e276548b 2010
25ef750e 2011 /////////////////////////////////////////////////////////////////////////////
2012 // Write material buffer
2013
fc73a202 2014 if (myRaytraceGeometry.Materials.size() != 0)
2015 {
91c60b57 2016 aResult &= myRaytraceMaterialTexture->Init (theGlContext, 4,
189f85a3 2017 GLsizei (myRaytraceGeometry.Materials.size() * 18), myRaytraceGeometry.Materials.front().Packed());
25ef750e 2018
fc73a202 2019 if (!aResult)
2020 {
2021#ifdef RAY_TRACE_PRINT_INFO
2022 std::cout << "Error: Failed to upload material buffer" << std::endl;
2023#endif
2024 return Standard_False;
2025 }
2026 }
e276548b 2027
fc73a202 2028 myIsRaytraceDataValid = myRaytraceGeometry.Objects().Size() != 0;
e276548b 2029
fc73a202 2030#ifdef RAY_TRACE_PRINT_INFO
e276548b 2031
fc73a202 2032 Standard_ShortReal aMemUsed = 0.f;
e276548b 2033
fc73a202 2034 for (Standard_Integer anElemIdx = 0; anElemIdx < myRaytraceGeometry.Size(); ++anElemIdx)
2035 {
2036 OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> (
2037 myRaytraceGeometry.Objects().ChangeValue (anElemIdx).operator->());
e276548b 2038
fc73a202 2039 aMemUsed += static_cast<Standard_ShortReal> (
25ef750e 2040 aTriangleSet->Vertices.size() * sizeof (BVH_Vec3f));
fc73a202 2041 aMemUsed += static_cast<Standard_ShortReal> (
25ef750e 2042 aTriangleSet->Normals.size() * sizeof (BVH_Vec3f));
2043 aMemUsed += static_cast<Standard_ShortReal> (
2044 aTriangleSet->TexCrds.size() * sizeof (BVH_Vec2f));
fc73a202 2045 aMemUsed += static_cast<Standard_ShortReal> (
2046 aTriangleSet->Elements.size() * sizeof (BVH_Vec4i));
e276548b 2047
fc73a202 2048 aMemUsed += static_cast<Standard_ShortReal> (
2049 aTriangleSet->BVH()->NodeInfoBuffer().size() * sizeof (BVH_Vec4i));
2050 aMemUsed += static_cast<Standard_ShortReal> (
25ef750e 2051 aTriangleSet->BVH()->MinPointBuffer().size() * sizeof (BVH_Vec3f));
fc73a202 2052 aMemUsed += static_cast<Standard_ShortReal> (
25ef750e 2053 aTriangleSet->BVH()->MaxPointBuffer().size() * sizeof (BVH_Vec3f));
fc73a202 2054 }
e276548b 2055
fc73a202 2056 aMemUsed += static_cast<Standard_ShortReal> (
2057 myRaytraceGeometry.BVH()->NodeInfoBuffer().size() * sizeof (BVH_Vec4i));
2058 aMemUsed += static_cast<Standard_ShortReal> (
25ef750e 2059 myRaytraceGeometry.BVH()->MinPointBuffer().size() * sizeof (BVH_Vec3f));
fc73a202 2060 aMemUsed += static_cast<Standard_ShortReal> (
25ef750e 2061 myRaytraceGeometry.BVH()->MaxPointBuffer().size() * sizeof (BVH_Vec3f));
e276548b 2062
fc73a202 2063 std::cout << "GPU Memory Used (MB): ~" << aMemUsed / 1048576 << std::endl;
e276548b 2064
fc73a202 2065#endif
e276548b 2066
fc73a202 2067 return aResult;
2068}
e276548b 2069
fc73a202 2070// =======================================================================
189f85a3 2071// function : updateRaytraceLightSources
91c60b57 2072// purpose : Updates 3D scene light sources for ray-tracing
fc73a202 2073// =======================================================================
91c60b57 2074Standard_Boolean OpenGl_View::updateRaytraceLightSources (const OpenGl_Mat4& theInvModelView, const Handle(OpenGl_Context)& theGlContext)
fc73a202 2075{
91c60b57 2076 myRaytraceGeometry.Sources.clear();
2077
2078 myRaytraceGeometry.Ambient = BVH_Vec4f (0.0f, 0.0f, 0.0f, 0.0f);
2079
189f85a3 2080 for (OpenGl_ListOfLight::Iterator aLightIter (myLights); aLightIter.More(); aLightIter.Next())
91c60b57 2081 {
189f85a3 2082 const OpenGl_Light& aLight = aLightIter.Value();
91c60b57 2083
2084 if (aLight.Type == Visual3d_TOLS_AMBIENT)
2085 {
189f85a3 2086 myRaytraceGeometry.Ambient += BVH_Vec4f (aLight.Color.r() * aLight.Intensity,
2087 aLight.Color.g() * aLight.Intensity,
2088 aLight.Color.b() * aLight.Intensity,
91c60b57 2089 0.0f);
2090 continue;
2091 }
2092
189f85a3 2093 BVH_Vec4f aDiffuse (aLight.Color.r() * aLight.Intensity,
2094 aLight.Color.g() * aLight.Intensity,
2095 aLight.Color.b() * aLight.Intensity,
91c60b57 2096 1.0f);
2097
2098 BVH_Vec4f aPosition (-aLight.Direction.x(),
2099 -aLight.Direction.y(),
2100 -aLight.Direction.z(),
2101 0.0f);
2102
2103 if (aLight.Type != Visual3d_TOLS_DIRECTIONAL)
2104 {
2105 aPosition = BVH_Vec4f (aLight.Position.x(),
2106 aLight.Position.y(),
2107 aLight.Position.z(),
2108 1.0f);
189f85a3 2109
2110 // store smoothing radius in w-component
2111 aDiffuse.w() = Max (aLight.Smoothness, 0.f);
2112 }
2113 else
2114 {
2115 // store cosine of smoothing angle in w-component
2116 aDiffuse.w() = cosf (Min (Max (aLight.Smoothness, 0.f), static_cast<Standard_ShortReal> (M_PI / 2.0)));
91c60b57 2117 }
2118
2119 if (aLight.IsHeadlight)
2120 {
2121 aPosition = theInvModelView * aPosition;
2122 }
2123
2124 myRaytraceGeometry.Sources.push_back (OpenGl_RaytraceLight (aDiffuse, aPosition));
2125 }
2126
2127 if (myRaytraceLightSrcTexture.IsNull()) // create light source buffer
2128 {
2129 myRaytraceLightSrcTexture = new OpenGl_TextureBufferArb;
2130
2131 if (!myRaytraceLightSrcTexture->Create (theGlContext))
2132 {
2133#ifdef RAY_TRACE_PRINT_INFO
2134 std::cout << "Error: Failed to create light source buffer" << std::endl;
2135#endif
2136 return Standard_False;
2137 }
2138 }
189f85a3 2139
91c60b57 2140 if (myRaytraceGeometry.Sources.size() != 0)
fc73a202 2141 {
91c60b57 2142 const GLfloat* aDataPtr = myRaytraceGeometry.Sources.front().Packed();
2143 if (!myRaytraceLightSrcTexture->Init (theGlContext, 4, GLsizei (myRaytraceGeometry.Sources.size() * 2), aDataPtr))
2144 {
2145#ifdef RAY_TRACE_PRINT_INFO
2146 std::cout << "Error: Failed to upload light source buffer" << std::endl;
2147#endif
2148 return Standard_False;
2149 }
fc73a202 2150 }
e276548b 2151
fc73a202 2152 return Standard_True;
e276548b 2153}
2154
2155// =======================================================================
189f85a3 2156// function : updateRaytraceEnvironmentMap
91c60b57 2157// purpose : Updates environment map for ray-tracing
e276548b 2158// =======================================================================
91c60b57 2159Standard_Boolean OpenGl_View::updateRaytraceEnvironmentMap (const Handle(OpenGl_Context)& theGlContext)
e276548b 2160{
91c60b57 2161 Standard_Boolean aResult = Standard_True;
e276548b 2162
91c60b57 2163 if (!myToUpdateEnvironmentMap)
e276548b 2164 {
91c60b57 2165 return aResult;
2166 }
e276548b 2167
91c60b57 2168 for (Standard_Integer anIdx = 0; anIdx < 2; ++anIdx)
2169 {
2170 const Handle(OpenGl_ShaderProgram)& aProgram =
2171 anIdx == 0 ? myRaytraceProgram : myPostFSAAProgram;
fc73a202 2172
91c60b57 2173 if (!aProgram.IsNull())
2174 {
2175 aResult &= theGlContext->BindProgram (aProgram);
e276548b 2176
91c60b57 2177 if (!myTextureEnv.IsNull() && mySurfaceDetail != Visual3d_TOD_NONE)
2178 {
2179 myTextureEnv->Bind (theGlContext,
2180 GL_TEXTURE0 + OpenGl_RT_EnvironmentMapTexture);
b5ac8292 2181
91c60b57 2182 aResult &= aProgram->SetUniform (theGlContext,
189f85a3 2183 myUniformLocations[anIdx][OpenGl_RT_uSphereMapEnabled], 1);
91c60b57 2184 }
2185 else
2186 {
2187 aResult &= aProgram->SetUniform (theGlContext,
189f85a3 2188 myUniformLocations[anIdx][OpenGl_RT_uSphereMapEnabled], 0);
91c60b57 2189 }
2190 }
2191 }
e276548b 2192
91c60b57 2193 myToUpdateEnvironmentMap = Standard_False;
e276548b 2194
91c60b57 2195 theGlContext->BindProgram (NULL);
fc73a202 2196
91c60b57 2197 return aResult;
fc73a202 2198}
2199
25ef750e 2200// =======================================================================
189f85a3 2201// function : setUniformState
25ef750e 2202// purpose : Sets uniform state for the given ray-tracing shader program
2203// =======================================================================
91c60b57 2204Standard_Boolean OpenGl_View::setUniformState (const Graphic3d_CView& theCView,
2205 const OpenGl_Vec3* theOrigins,
2206 const OpenGl_Vec3* theDirects,
2207 const OpenGl_Mat4& theUnviewMat,
2208 const Standard_Integer theProgramId,
2209 const Handle(OpenGl_Context)& theGlContext)
25ef750e 2210{
91c60b57 2211 Handle(OpenGl_ShaderProgram)& theProgram =
2212 theProgramId == 0 ? myRaytraceProgram : myPostFSAAProgram;
2213
2214 if (theProgram.IsNull())
25ef750e 2215 {
2216 return Standard_False;
2217 }
2218
91c60b57 2219 const Standard_Integer aLightSourceBufferSize =
2220 static_cast<Standard_Integer> (myRaytraceGeometry.Sources.size());
2221
25ef750e 2222 // Set camera state
189f85a3 2223 theProgram->SetUniform (theGlContext,
91c60b57 2224 myUniformLocations[theProgramId][OpenGl_RT_uOriginLB], theOrigins[0]);
189f85a3 2225 theProgram->SetUniform (theGlContext,
91c60b57 2226 myUniformLocations[theProgramId][OpenGl_RT_uOriginRB], theOrigins[1]);
189f85a3 2227 theProgram->SetUniform (theGlContext,
91c60b57 2228 myUniformLocations[theProgramId][OpenGl_RT_uOriginLT], theOrigins[2]);
189f85a3 2229 theProgram->SetUniform (theGlContext,
91c60b57 2230 myUniformLocations[theProgramId][OpenGl_RT_uOriginRT], theOrigins[3]);
189f85a3 2231 theProgram->SetUniform (theGlContext,
91c60b57 2232 myUniformLocations[theProgramId][OpenGl_RT_uDirectLB], theDirects[0]);
189f85a3 2233 theProgram->SetUniform (theGlContext,
91c60b57 2234 myUniformLocations[theProgramId][OpenGl_RT_uDirectRB], theDirects[1]);
189f85a3 2235 theProgram->SetUniform (theGlContext,
91c60b57 2236 myUniformLocations[theProgramId][OpenGl_RT_uDirectLT], theDirects[2]);
189f85a3 2237 theProgram->SetUniform (theGlContext,
91c60b57 2238 myUniformLocations[theProgramId][OpenGl_RT_uDirectRT], theDirects[3]);
189f85a3 2239 theProgram->SetUniform (theGlContext,
91c60b57 2240 myUniformLocations[theProgramId][OpenGl_RT_uUnviewMat], theUnviewMat);
312a4043 2241
25ef750e 2242 // Set scene parameters
189f85a3 2243 theProgram->SetUniform (theGlContext,
91c60b57 2244 myUniformLocations[theProgramId][OpenGl_RT_uSceneRad], myRaytraceSceneRadius);
189f85a3 2245 theProgram->SetUniform (theGlContext,
91c60b57 2246 myUniformLocations[theProgramId][OpenGl_RT_uSceneEps], myRaytraceSceneEpsilon);
189f85a3 2247 theProgram->SetUniform (theGlContext,
91c60b57 2248 myUniformLocations[theProgramId][OpenGl_RT_uLightCount], aLightSourceBufferSize);
189f85a3 2249 theProgram->SetUniform (theGlContext,
91c60b57 2250 myUniformLocations[theProgramId][OpenGl_RT_uLightAmbnt], myRaytraceGeometry.Ambient);
2251
2252 // Set run-time rendering options
189f85a3 2253 theProgram->SetUniform (theGlContext,
2254 myUniformLocations[theProgramId][OpenGl_RT_uShadowsEnabled], theCView.RenderParams.IsShadowEnabled ? 1 : 0);
2255 theProgram->SetUniform (theGlContext,
2256 myUniformLocations[theProgramId][OpenGl_RT_uReflectEnabled], theCView.RenderParams.IsReflectionEnabled ? 1 : 0);
25ef750e 2257
2258 // Set array of 64-bit texture handles
91c60b57 2259 if (theGlContext->arbTexBindless != NULL && myRaytraceGeometry.HasTextures())
25ef750e 2260 {
189f85a3 2261 theProgram->SetUniform (theGlContext, myUniformLocations[theProgramId][OpenGl_RT_uTexSamplersArray],
2262 static_cast<GLsizei> (myRaytraceGeometry.TextureHandles().size()), &myRaytraceGeometry.TextureHandles()[0]);
25ef750e 2263 }
2264
189f85a3 2265 // Set background colors (only gradient background supported)
2266 if (myBgGradientArray != NULL)
25ef750e 2267 {
189f85a3 2268 theProgram->SetUniform (theGlContext,
2269 myUniformLocations[theProgramId][OpenGl_RT_uBackColorTop], myBgGradientArray->GradientColor (0));
2270 theProgram->SetUniform (theGlContext,
2271 myUniformLocations[theProgramId][OpenGl_RT_uBackColorBot], myBgGradientArray->GradientColor (1));
25ef750e 2272 }
2273
189f85a3 2274 theProgram->SetUniform (theGlContext,
2275 myUniformLocations[theProgramId][OpenGl_RT_uSphereMapForBack], theCView.RenderParams.UseEnvironmentMapBackground ? 1 : 0);
2276
2277 return Standard_True;
25ef750e 2278}
2279
91c60b57 2280// =======================================================================
189f85a3 2281// function : bindRaytraceTextures
91c60b57 2282// purpose : Binds ray-trace textures to corresponding texture units
2283// =======================================================================
2284void OpenGl_View::bindRaytraceTextures (const Handle(OpenGl_Context)& theGlContext)
2285{
2286 mySceneMinPointTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneMinPointTexture);
2287 mySceneMaxPointTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneMaxPointTexture);
2288 mySceneNodeInfoTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneNodeInfoTexture);
2289 myGeometryVertexTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryVertexTexture);
2290 myGeometryNormalTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryNormalTexture);
2291 myGeometryTexCrdTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryTexCrdTexture);
2292 myGeometryTriangTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryTriangTexture);
2293 mySceneTransformTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneTransformTexture);
2294 myRaytraceMaterialTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceMaterialTexture);
2295 myRaytraceLightSrcTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceLightSrcTexture);
2296
2297 if (!myOpenGlFBO.IsNull())
2298 {
2299 myOpenGlFBO->ColorTexture()->Bind (theGlContext, GL_TEXTURE0 + OpenGl_RT_OpenGlColorTexture);
2300 myOpenGlFBO->DepthStencilTexture()->Bind (theGlContext, GL_TEXTURE0 + OpenGl_RT_OpenGlDepthTexture);
2301 }
2302}
2303
2304// =======================================================================
189f85a3 2305// function : unbindRaytraceTextures
91c60b57 2306// purpose : Unbinds ray-trace textures from corresponding texture units
2307// =======================================================================
2308void OpenGl_View::unbindRaytraceTextures (const Handle(OpenGl_Context)& theGlContext)
2309{
2310 mySceneMinPointTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneMinPointTexture);
2311 mySceneMaxPointTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneMaxPointTexture);
2312 mySceneNodeInfoTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneNodeInfoTexture);
2313 myGeometryVertexTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryVertexTexture);
2314 myGeometryNormalTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryNormalTexture);
2315 myGeometryTexCrdTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryTexCrdTexture);
2316 myGeometryTriangTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryTriangTexture);
2317 mySceneTransformTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneTransformTexture);
2318 myRaytraceMaterialTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceMaterialTexture);
2319 myRaytraceLightSrcTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceLightSrcTexture);
2320
2321 if (!myOpenGlFBO.IsNull())
2322 {
2323 myOpenGlFBO->ColorTexture()->Unbind (theGlContext, GL_TEXTURE0 + OpenGl_RT_OpenGlColorTexture);
2324 myOpenGlFBO->DepthStencilTexture()->Unbind (theGlContext, GL_TEXTURE0 + OpenGl_RT_OpenGlDepthTexture);
2325 }
2326
2327 theGlContext->core15fwd->glActiveTexture (GL_TEXTURE0);
2328}
2329
fc73a202 2330// =======================================================================
189f85a3 2331// function : runRaytraceShaders
fc73a202 2332// purpose : Runs ray-tracing shader programs
2333// =======================================================================
91c60b57 2334Standard_Boolean OpenGl_View::runRaytraceShaders (const Graphic3d_CView& theCView,
2335 const Standard_Integer theSizeX,
2336 const Standard_Integer theSizeY,
2337 const OpenGl_Vec3* theOrigins,
2338 const OpenGl_Vec3* theDirects,
2339 const OpenGl_Mat4& theUnviewMat,
38a0206f 2340 OpenGl_FrameBuffer* theReadDrawFbo,
91c60b57 2341 const Handle(OpenGl_Context)& theGlContext)
fc73a202 2342{
91c60b57 2343 bindRaytraceTextures (theGlContext);
fc73a202 2344
189f85a3 2345 Handle(OpenGl_FrameBuffer) aRenderFramebuffer;
2346 Handle(OpenGl_FrameBuffer) anAccumFramebuffer;
2347
2348 if (myRaytraceParameters.GlobalIllumination) // if path-tracing is used
2349 {
2350 for (int anIdx = 0; anIdx < 3; ++anIdx)
2351 {
2352 if (fabsf (theOrigins[anIdx].x() - myPreviousOrigins[anIdx].x()) > std::numeric_limits<Standard_ShortReal>::epsilon()
2353 || fabsf (theOrigins[anIdx].y() - myPreviousOrigins[anIdx].y()) > std::numeric_limits<Standard_ShortReal>::epsilon()
2354 || fabsf (theOrigins[anIdx].z() - myPreviousOrigins[anIdx].z()) > std::numeric_limits<Standard_ShortReal>::epsilon())
2355 {
2356 myAccumFrames = 0; // camera has been moved
2357 }
2358
2359 myPreviousOrigins[anIdx] = theOrigins[anIdx];
2360 }
2361
2362 aRenderFramebuffer = myAccumFrames % 2 ? myRaytraceFBO1 : myRaytraceFBO2;
2363 anAccumFramebuffer = myAccumFrames % 2 ? myRaytraceFBO2 : myRaytraceFBO1;
2364
2365 anAccumFramebuffer->ColorTexture()->Bind (
2366 theGlContext, GL_TEXTURE0 + OpenGl_RT_PrevAccumTexture);
2367
2368 aRenderFramebuffer->BindBuffer (theGlContext);
2369 }
2370 else if (theCView.RenderParams.IsAntialiasingEnabled) // if 2-pass ray-tracing is used
fc73a202 2371 {
91c60b57 2372 myRaytraceFBO1->BindBuffer (theGlContext);
2373
fc73a202 2374 glDisable (GL_BLEND);
2375 }
2376
91c60b57 2377 Standard_Boolean aResult = theGlContext->BindProgram (myRaytraceProgram);
fc73a202 2378
91c60b57 2379 aResult &= setUniformState (theCView,
2380 theOrigins,
2381 theDirects,
2382 theUnviewMat,
2383 0, // ID of RT program
2384 theGlContext);
fc73a202 2385
189f85a3 2386 if (myRaytraceParameters.GlobalIllumination)
fc73a202 2387 {
189f85a3 2388 // Set frame accumulation weight
2389 myRaytraceProgram->SetUniform (theGlContext,
2390 myUniformLocations[0][OpenGl_RT_uSampleWeight], 1.f / (myAccumFrames + 1));
2391
2392 // Set random number generator seed
2393 myRaytraceProgram->SetUniform (theGlContext,
2394 myUniformLocations[0][OpenGl_RT_uFrameRndSeed], static_cast<Standard_Integer> (myRNG.NextInt() >> 2));
fc73a202 2395 }
25ef750e 2396
189f85a3 2397 theGlContext->core20fwd->glDrawArrays (GL_TRIANGLES, 0, 6);
fc73a202 2398
189f85a3 2399 if (myRaytraceParameters.GlobalIllumination)
2400 {
2401 // Output accumulated image
2402 glDisable (GL_BLEND);
a89742cf 2403
189f85a3 2404 theGlContext->BindProgram (myOutImageProgram);
fc73a202 2405
189f85a3 2406 if (theReadDrawFbo != NULL)
2407 {
2408 theReadDrawFbo->BindBuffer (theGlContext);
2409 }
2410 else
2411 {
2412 aRenderFramebuffer->UnbindBuffer (theGlContext);
2413 }
fc73a202 2414
189f85a3 2415 aRenderFramebuffer->ColorTexture()->Bind (
2416 theGlContext, GL_TEXTURE0 + OpenGl_RT_PrevAccumTexture);
fc73a202 2417
189f85a3 2418 theGlContext->core20fwd->glDrawArrays (GL_TRIANGLES, 0, 6);
fc73a202 2419
189f85a3 2420 ++myAccumFrames;
2421 }
2422 else if (theCView.RenderParams.IsAntialiasingEnabled)
fc73a202 2423 {
189f85a3 2424 myRaytraceFBO1->ColorTexture()->Bind (theGlContext, GL_TEXTURE0 + OpenGl_RT_FsaaInputTexture);
2425
2426 aResult &= theGlContext->BindProgram (myPostFSAAProgram);
2427
2428 aResult &= setUniformState (theCView,
2429 theOrigins,
2430 theDirects,
2431 theUnviewMat,
2432 1, // ID of FSAA program
2433 theGlContext);
2434
91c60b57 2435 // Perform multi-pass adaptive FSAA using ping-pong technique.
2436 // We use 'FLIPTRI' sampling pattern changing for every pixel
2437 // (3 additional samples per pixel, the 1st sample is already
2438 // available from initial ray-traced image).
2439 for (Standard_Integer anIt = 1; anIt < 4; ++anIt)
50d0e1ce 2440 {
91c60b57 2441 GLfloat aOffsetX = 1.f / theSizeX;
2442 GLfloat aOffsetY = 1.f / theSizeY;
50d0e1ce 2443
91c60b57 2444 if (anIt == 1)
2445 {
2446 aOffsetX *= -0.55f;
2447 aOffsetY *= 0.55f;
2448 }
2449 else if (anIt == 2)
2450 {
2451 aOffsetX *= 0.00f;
2452 aOffsetY *= -0.55f;
2453 }
2454 else if (anIt == 3)
2455 {
2456 aOffsetX *= 0.55f;
2457 aOffsetY *= 0.00f;
2458 }
fc73a202 2459
91c60b57 2460 aResult &= myPostFSAAProgram->SetUniform (theGlContext,
2461 myUniformLocations[1][OpenGl_RT_uSamples], anIt + 1);
2462 aResult &= myPostFSAAProgram->SetUniform (theGlContext,
2463 myUniformLocations[1][OpenGl_RT_uOffsetX], aOffsetX);
2464 aResult &= myPostFSAAProgram->SetUniform (theGlContext,
2465 myUniformLocations[1][OpenGl_RT_uOffsetY], aOffsetY);
fc73a202 2466
91c60b57 2467 Handle(OpenGl_FrameBuffer)& aFramebuffer = anIt % 2 ? myRaytraceFBO2 : myRaytraceFBO1;
fc73a202 2468
91c60b57 2469 if (anIt == 3) // disable FBO on last iteration
2470 {
38a0206f 2471 if (theReadDrawFbo != NULL)
189f85a3 2472 {
38a0206f 2473 theReadDrawFbo->BindBuffer (theGlContext);
189f85a3 2474 }
2475 else
2476 {
2477 aFramebuffer->UnbindBuffer (theGlContext);
2478 }
91c60b57 2479 }
2480 else
2481 {
2482 aFramebuffer->BindBuffer (theGlContext);
2483 }
50d0e1ce 2484
91c60b57 2485 theGlContext->core20fwd->glDrawArrays (GL_TRIANGLES, 0, 6);
e276548b 2486
91c60b57 2487 if (anIt != 3) // set input for the next pass
2488 {
189f85a3 2489 aFramebuffer->ColorTexture()->Bind (theGlContext, GL_TEXTURE0 + OpenGl_RT_FsaaInputTexture);
91c60b57 2490 }
e276548b 2491 }
2492 }
fc73a202 2493
91c60b57 2494 unbindRaytraceTextures (theGlContext);
a89742cf 2495
91c60b57 2496 theGlContext->BindProgram (NULL);
fc73a202 2497
91c60b57 2498 return aResult;
e276548b 2499}
2500
2501// =======================================================================
189f85a3 2502// function : raytrace
fc73a202 2503// purpose : Redraws the window using OpenGL/GLSL ray-tracing
e276548b 2504// =======================================================================
91c60b57 2505Standard_Boolean OpenGl_View::raytrace (const Graphic3d_CView& theCView,
2506 const Standard_Integer theSizeX,
2507 const Standard_Integer theSizeY,
38a0206f 2508 OpenGl_FrameBuffer* theReadDrawFbo,
91c60b57 2509 const Handle(OpenGl_Context)& theGlContext)
e276548b 2510{
91c60b57 2511 if (!initRaytraceResources (theCView, theGlContext))
2512 {
e276548b 2513 return Standard_False;
91c60b57 2514 }
2515
2516 if (!resizeRaytraceBuffers (theSizeX, theSizeY, theGlContext))
2517 {
2518 return Standard_False;
2519 }
e276548b 2520
91c60b57 2521 if (!updateRaytraceEnvironmentMap (theGlContext))
2522 {
e276548b 2523 return Standard_False;
91c60b57 2524 }
e276548b 2525
2526 // Get model-view and projection matrices
38a0206f 2527 OpenGl_Mat4 aOrientationMatrix = myCamera->OrientationMatrixF();
2528 OpenGl_Mat4 aViewMappingMatrix = theGlContext->ProjectionState.Current();
e276548b 2529
38a0206f 2530 OpenGl_Mat4 aInverOrientMatrix;
91c60b57 2531 aOrientationMatrix.Inverted (aInverOrientMatrix);
91c60b57 2532 if (!updateRaytraceLightSources (aInverOrientMatrix, theGlContext))
2533 {
e276548b 2534 return Standard_False;
91c60b57 2535 }
e276548b 2536
fc73a202 2537 OpenGl_Vec3 aOrigins[4];
2538 OpenGl_Vec3 aDirects[4];
25ef750e 2539 OpenGl_Mat4 anUnviewMat;
e276548b 2540
91c60b57 2541 updateCamera (aOrientationMatrix,
fc73a202 2542 aViewMappingMatrix,
2543 aOrigins,
a89742cf 2544 aDirects,
25ef750e 2545 anUnviewMat);
e276548b 2546
189f85a3 2547 glDisable (GL_BLEND);
e276548b 2548 glDisable (GL_DEPTH_TEST);
2549
38a0206f 2550 if (theReadDrawFbo != NULL)
a89742cf 2551 {
38a0206f 2552 theReadDrawFbo->BindBuffer (theGlContext);
a89742cf 2553 }
2554
c827ea3a 2555 // Generate ray-traced image
e276548b 2556 if (myIsRaytraceDataValid)
2557 {
189f85a3 2558 myRaytraceScreenQuad.BindVertexAttrib (theGlContext, Graphic3d_TOA_POS);
fc73a202 2559
91c60b57 2560 if (!myRaytraceGeometry.AcquireTextures (theGlContext))
25ef750e 2561 {
91c60b57 2562 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB, GL_DEBUG_TYPE_ERROR_ARB,
2563 0, GL_DEBUG_SEVERITY_MEDIUM_ARB, "Error: Failed to acquire OpenGL image textures");
25ef750e 2564 }
2565
91c60b57 2566 Standard_Boolean aResult = runRaytraceShaders (theCView,
2567 theSizeX,
2568 theSizeY,
2569 aOrigins,
2570 aDirects,
2571 anUnviewMat,
38a0206f 2572 theReadDrawFbo,
91c60b57 2573 theGlContext);
fc73a202 2574
91c60b57 2575 if (!aResult)
25ef750e 2576 {
91c60b57 2577 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB, GL_DEBUG_TYPE_ERROR_ARB,
2578 0, GL_DEBUG_SEVERITY_MEDIUM_ARB, "Error: Failed to execute ray-tracing shaders");
2579 }
25ef750e 2580
91c60b57 2581 if (!myRaytraceGeometry.ReleaseTextures (theGlContext))
2582 {
2583 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB, GL_DEBUG_TYPE_ERROR_ARB,
2584 0, GL_DEBUG_SEVERITY_MEDIUM_ARB, "Error: Failed to release OpenGL image textures");
25ef750e 2585 }
2586
189f85a3 2587 myRaytraceScreenQuad.UnbindVertexAttrib (theGlContext, Graphic3d_TOA_POS);
e276548b 2588 }
2589
91c60b57 2590 glDisable (GL_BLEND);
2591 glEnable (GL_DEPTH_TEST);
a89742cf 2592
e276548b 2593 return Standard_True;
f8ae3605 2594}