0025351: Visualization - provide pseudo random number generator in OCCT ray-tracing...
authordbp <dbp@opencascade.org>
Tue, 23 Dec 2014 11:39:18 +0000 (14:39 +0300)
committerbugmaster <bugmaster@opencascade.com>
Thu, 25 Dec 2014 15:24:26 +0000 (18:24 +0300)
src/OpenGl/OpenGl_Workspace.hxx
src/OpenGl/OpenGl_Workspace_Raytrace.cxx
src/Shaders/RaytraceBase.fs

index 6c5d4ae..5064235 100644 (file)
@@ -317,6 +317,8 @@ protected:
     OpenGl_RT_uOffsetX,
     OpenGl_RT_uOffsetY,
     OpenGl_RT_uSamples,
+    OpenGl_RT_uWinSizeX,
+    OpenGl_RT_uWinSizeY,
 
     OpenGl_RT_uTextures,
 
@@ -533,6 +535,8 @@ protected: //! @name methods related to ray-tracing
 
   //! Sets uniform state for the given ray-tracing shader program.
   Standard_Boolean SetUniformState (const Graphic3d_CView&        theCView,
+                                    const Standard_Integer        theSizeX,
+                                    const Standard_Integer        theSizeY,
                                     const OpenGl_Vec3*            theOrigins,
                                     const OpenGl_Vec3*            theDirects,
                                     const OpenGl_Mat4&            theUnviewMat,
index 21fe122..543663b 100644 (file)
@@ -1566,6 +1566,10 @@ Standard_Boolean OpenGl_Workspace::InitRaytraceResources (const Graphic3d_CView&
         aShaderProgram->GetUniformLocation (myGlContext, "uOffsetY");
       myUniformLocations[anIndex][OpenGl_RT_uSamples] =
         aShaderProgram->GetUniformLocation (myGlContext, "uSamples");
+      myUniformLocations[anIndex][OpenGl_RT_uWinSizeX] =
+        aShaderProgram->GetUniformLocation (myGlContext, "uWinSizeX");
+      myUniformLocations[anIndex][OpenGl_RT_uWinSizeY] =
+        aShaderProgram->GetUniformLocation (myGlContext, "uWinSizeY");
 
       myUniformLocations[anIndex][OpenGl_RT_uTextures] =
         aShaderProgram->GetUniformLocation (myGlContext, "uTextureSamplers");
@@ -2084,6 +2088,8 @@ void OpenGl_Workspace::UpdateCamera (const OpenGl_Mat4& theOrientation,
 // purpose  : Sets uniform state for the given ray-tracing shader program
 // =======================================================================
 Standard_Boolean OpenGl_Workspace::SetUniformState (const Graphic3d_CView&        theCView,
+                                                    const Standard_Integer        theSizeX,
+                                                    const Standard_Integer        theSizeY,
                                                     const OpenGl_Vec3*            theOrigins,
                                                     const OpenGl_Vec3*            theDirects,
                                                     const OpenGl_Mat4&            theUnviewMat,
@@ -2120,6 +2126,12 @@ Standard_Boolean OpenGl_Workspace::SetUniformState (const Graphic3d_CView&
   aResult &= theRaytraceProgram->SetUniform (myGlContext,
     myUniformLocations[theProgramIndex][OpenGl_RT_uUnviewMat], theUnviewMat);
 
+  // Set window size
+  aResult &= theRaytraceProgram->SetUniform (myGlContext,
+    myUniformLocations[theProgramIndex][OpenGl_RT_uWinSizeX], theSizeX);
+  aResult &= theRaytraceProgram->SetUniform (myGlContext,
+    myUniformLocations[theProgramIndex][OpenGl_RT_uWinSizeY], theSizeY);
+
   // Set scene parameters
   aResult &= theRaytraceProgram->SetUniform (myGlContext,
     myUniformLocations[theProgramIndex][OpenGl_RT_uSceneRad], myRaytraceSceneRadius);
@@ -2146,7 +2158,7 @@ Standard_Boolean OpenGl_Workspace::SetUniformState (const Graphic3d_CView&
   if (!aResult)
   {
 #ifdef RAY_TRACE_PRINT_INFO
-    std::cout << "Error: Failed to set uniform state for ray-tracing program" << theProgramIndex << std::endl;
+    std::cout << "Info: Not all uniforms were detected (for program " << theProgramIndex << ")" << std::endl;
 #endif
   }
 
@@ -2192,6 +2204,8 @@ Standard_Boolean OpenGl_Workspace::RunRaytraceShaders (const Graphic3d_CView& th
   myGlContext->BindProgram (myRaytraceProgram);
 
   SetUniformState (theCView,
+                   theSizeX,
+                   theSizeY,
                    theOrigins,
                    theDirects,
                    theUnviewMat,
@@ -2235,6 +2249,8 @@ Standard_Boolean OpenGl_Workspace::RunRaytraceShaders (const Graphic3d_CView& th
   myGlContext->BindProgram (myPostFSAAProgram);
 
   SetUniformState (theCView,
+                   theSizeX,
+                   theSizeY,
                    theOrigins,
                    theDirects,
                    theUnviewMat,
index 10bf8af..cff32f8 100644 (file)
@@ -19,6 +19,11 @@ uniform vec3 uOriginRT;
 //! Origin of viewing ray in right-bottom corner.
 uniform vec3 uOriginRB;
 
+//! Width of the rendering window.
+uniform int uWinSizeX;
+//! Height of the rendering window.
+uniform int uWinSizeY;
+
 //! Direction of viewing ray in left-top corner.
 uniform vec3 uDirectLT;
 //! Direction of viewing ray in left-bottom corner.
@@ -141,6 +146,47 @@ vec3 MatrixRowMultiplyDir (in vec3 v,
                dot (m2.xyz, v));
 }
 
+//! 32-bit state of random number generator.
+uint RandState;
+
+// =======================================================================
+// function : SeedRand
+// purpose  : Applies hash function by Thomas Wang to randomize seeds
+//            (see http://www.burtleburtle.net/bob/hash/integer.html)
+// =======================================================================
+void SeedRand (in int theSeed)
+{
+  RandState = uint (int (gl_FragCoord.y) * uWinSizeX + int (gl_FragCoord.x) + theSeed);
+
+  RandState = (RandState + 0x479ab41du) + (RandState <<  8);
+  RandState = (RandState ^ 0xe4aa10ceu) ^ (RandState >>  5);
+  RandState = (RandState + 0x9942f0a6u) - (RandState << 14);
+  RandState = (RandState ^ 0x5aedd67du) ^ (RandState >>  3);
+  RandState = (RandState + 0x17bea992u) + (RandState <<  7);
+}
+
+// =======================================================================
+// function : RandInt
+// purpose  : Generates integer using Xorshift algorithm by G. Marsaglia
+// =======================================================================
+uint RandInt()
+{
+  RandState ^= (RandState << 13);
+  RandState ^= (RandState >> 17);
+  RandState ^= (RandState <<  5);
+
+  return RandState;
+}
+
+// =======================================================================
+// function : RandFloat
+// purpose  : Generates a random float in [0, 1) range
+// =======================================================================
+float RandFloat()
+{
+  return float (RandInt()) * (1.f / 4294967296.f);
+}
+
 // =======================================================================
 // function : MatrixColMultiplyPnt
 // purpose  : Multiplies a vector by matrix