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