]> OCCT Git - occt.git/commitdiff
0032752: Visualization, TKOpenGl - extend V3d_View::ToPixMap() options with Z-layer CR32173_1
authordrochalo <diogo.lopes@opencascade.com>
Thu, 11 Apr 2024 16:53:27 +0000 (17:53 +0100)
committerdrochalo <diogo.lopes@opencascade.com>
Thu, 11 Apr 2024 16:53:27 +0000 (17:53 +0100)
Changed shadow cubemap calculation of up and direction vectors.
Included point light range to shader calculations.
Replaced use of define values in shaders for uniforms vectors with range
parameters.
Code cleanup.

15 files changed:
src/Graphic3d/Graphic3d_Camera.hxx
src/Graphic3d/Graphic3d_CubeMap.cxx
src/Graphic3d/Graphic3d_LightSet.cxx
src/Graphic3d/Graphic3d_ShaderManager.cxx
src/OpenGl/OpenGl_FrameBuffer.cxx
src/OpenGl/OpenGl_ShaderManager.cxx
src/OpenGl/OpenGl_ShaderManager.hxx
src/OpenGl/OpenGl_ShaderProgram.cxx
src/OpenGl/OpenGl_ShaderProgram.hxx
src/OpenGl/OpenGl_ShadowMap.cxx
src/OpenGl/OpenGl_Texture.cxx
src/OpenGl/OpenGl_View.cxx
src/Shaders/LightPointShadow.glsl
src/Shaders/Shaders_LightPointShadow_glsl.pxx
tests/opengl/data/shadows/pointlight

index a44fe69d77142ab8a4369a3f34e95ef0411b0db2..1f6961498a51a561518429abd752ba9f8d6f9d90 100644 (file)
@@ -203,10 +203,10 @@ public:
 public:
 
   //! return default valut for znear.
-  Standard_EXPORT Standard_Real GetDefaultZNear();
+  Standard_EXPORT static Standard_Real GetDefaultZNear();
 
   //! return default valut for zfar.
-  Standard_EXPORT Standard_Real GetDefaultZFar();
+  Standard_EXPORT static Standard_Real GetDefaultZFar();
 
   //! Get camera look direction.
   //! @return camera look direction.
index 4f8b40d929f602376a317ba1704c7e27af90a28e..5912c0ad8bb22e51465d549b2aa4f479170d9af4 100644 (file)
@@ -59,41 +59,11 @@ Graphic3d_CubeMap::~Graphic3d_CubeMap()
 // =======================================================================
 gp_Dir Graphic3d_CubeMap::GetCubeDirection (Graphic3d_CubeMapSide theFace)
 {
-  gp_Dir aResult;
-  switch (theFace)
-  {
-    case (Graphic3d_CMS_POS_X):
-    {
-      aResult = gp_Dir(1.0, 0.0, 0.0);
-    }
-    break;
-    case (Graphic3d_CMS_NEG_X):
-    {
-      aResult = gp_Dir(-1.0, 0.0, 0.0);
-    }
-    break;
-    case (Graphic3d_CMS_POS_Y):
-    {
-      aResult = gp_Dir(0.0, 1.0, 0.0);
-    }
-    break;
-    case (Graphic3d_CMS_NEG_Y):
-    {
-      aResult = gp_Dir(0.0, -1.0, 0.0);
-    }
-    break;
-    case (Graphic3d_CMS_POS_Z):
-    {
-      aResult = gp_Dir(0.0, 0.0, 1.0);
-    }
-    break;
-    case (Graphic3d_CMS_NEG_Z):
-    {
-      aResult = gp_Dir(0.0, 0.0, -1.0);
-    }
-    break;
-  }
-  return aResult;
+  const Standard_Integer aDiv2 = theFace / 2;
+  const Standard_Integer aMod2 = theFace % 2;
+  return gp_Dir (aDiv2 == 0 ? aMod2 == 0 ? 1.0 : -1.0 : 0.0,
+                 aDiv2 == 1 ? aMod2 == 0 ? 1.0 : -1.0 : 0.0,
+                 aDiv2 == 2 ? aMod2 == 0 ? 1.0 : -1.0 : 0.0);
 }
 
 // =======================================================================
@@ -102,39 +72,9 @@ gp_Dir Graphic3d_CubeMap::GetCubeDirection (Graphic3d_CubeMapSide theFace)
 // =======================================================================
 gp_Dir Graphic3d_CubeMap::GetCubeUp (Graphic3d_CubeMapSide theFace)
 {
-  gp_Dir aResult;
-  switch (theFace)
-  {
-    case (Graphic3d_CMS_POS_X):
-    {
-      aResult = -gp_Dir(0.0, -1.0, 0.0);
-    }
-    break;
-    case (Graphic3d_CMS_NEG_X):
-    {
-      aResult = -gp_Dir(0.0, -1.0, 0.0);
-    }
-    break;
-    case (Graphic3d_CMS_POS_Y):
-    {
-      aResult = gp_Dir(0.0, 0.0, 1.0);
-    }
-    break;
-    case (Graphic3d_CMS_NEG_Y):
-    {
-      aResult = gp_Dir(0.0, 0.0, -1.0);
-    }
-    break;
-    case (Graphic3d_CMS_POS_Z):
-    {
-      aResult = gp_Dir(0.0, -1.0, 0.0);
-    }
-    break;
-    case (Graphic3d_CMS_NEG_Z):
-    {
-      aResult = gp_Dir(0.0, -1.0, 0.0);
-    }
-    break;
-  }
-  return aResult;
+  const Standard_Integer aDiv2 = theFace / 2;
+  const Standard_Integer aMod2 = theFace % 2;
+  return gp_Dir (0.0,
+                 aDiv2 == 0 ? 1.0 : aDiv2 == 2 ? -1.0 : 0.0,
+                 aDiv2 == 1 ? aMod2 == 0 ? 1.0 : -1.0 : 0.0);
 }
index a4571f4f4cc61bf8b9acaf77dd9fa8015c851f93..3c272d0974e75351ed8e39ca3e4d722e24a37447 100644 (file)
@@ -97,10 +97,6 @@ void Graphic3d_LightSet::CalculateNbShadows (Standard_Integer& theNb2DShadows, S
   for (NCollection_IndexedDataMap<Handle(Graphic3d_CLight), Standard_Size>::Iterator aLightIter(myLights); aLightIter.More(); aLightIter.Next())
   {
     const Handle(Graphic3d_CLight)& aLight = aLightIter.Key();
-    //if (!aLight->IsEnabled())
-    //{
-    //  continue;
-    //}
     if (aLight->ToCastShadows())
     {
       if (aLight->Type() == Graphic3d_TypeOfLightSource_Positional)
index 284d6a1f544a009e09667f942b70f9addfc6fd5a..dc9b19ae81e17c024a5f857436237cd463b01760 100644 (file)
@@ -1720,6 +1720,7 @@ Handle(Graphic3d_ShaderProgram) Graphic3d_ShaderManager::getStdProgramPhong (con
   if (theNbShadowMaps + theNbShadowCubeMaps > 0)
   {
     aUniforms.Append (Graphic3d_ShaderObject::ShaderVariable ("mat4 occShadowMapMatrices[THE_NB_SHADOWMAPS]", Graphic3d_TOS_VERTEX));
+    aUniforms.Append (Graphic3d_ShaderObject::ShaderVariable ("vec2 occShadowMapRangeParams[THE_NB_SHADOWMAPS]", Graphic3d_TOS_FRAGMENT));
     aUniforms.Append (Graphic3d_ShaderObject::ShaderVariable ("vec2 occShadowMapSizeBias", Graphic3d_TOS_FRAGMENT));
     if (theNbShadowMaps > 0)
     {
index e370a81576e7f9d8bea09738f4c38c0ad833c3c4..722ed2fe3febebeece87cdbcd9f7b07440036f07 100644 (file)
@@ -474,12 +474,9 @@ Standard_Boolean OpenGl_FrameBuffer::Init (const Handle(OpenGl_Context)& theGlCo
     {
       if (theIsCubeMap)
       {
-        for (Standard_Integer aCubeFace = 0; aCubeFace < 6; ++aCubeFace)
-        {
-          theGlContext->arbFBO->glFramebufferTexture2D (GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
-                                                        GLenum(GL_TEXTURE_CUBE_MAP_POSITIVE_X + aCubeFace),
-                                                        myDepthStencilTexture->TextureId(), 0);
-        }
+        theGlContext->arbFBO->glFramebufferTexture2D (GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
+                                                      GLenum(GL_TEXTURE_CUBE_MAP_POSITIVE_X),
+                                                      myDepthStencilTexture->TextureId(), 0);
       }
       else
       {
@@ -491,15 +488,12 @@ Standard_Boolean OpenGl_FrameBuffer::Init (const Handle(OpenGl_Context)& theGlCo
     {
       if (theIsCubeMap)
       {
-        for (Standard_Integer aCubeFace = 0; aCubeFace < 6; ++aCubeFace)
-        {
-          theGlContext->arbFBO->glFramebufferTexture2D (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
-                                                        GLenum(GL_TEXTURE_CUBE_MAP_POSITIVE_X + aCubeFace),
-                                                        myDepthStencilTexture->TextureId(), 0);
-          theGlContext->arbFBO->glFramebufferTexture2D (GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
-                                                        GLenum(GL_TEXTURE_CUBE_MAP_POSITIVE_X + aCubeFace),
-                                                        myDepthStencilTexture->TextureId(), 0);
-        }
+        theGlContext->arbFBO->glFramebufferTexture2D (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
+                                                      GLenum(GL_TEXTURE_CUBE_MAP_POSITIVE_X),
+                                                      myDepthStencilTexture->TextureId(), 0);
+        theGlContext->arbFBO->glFramebufferTexture2D (GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
+                                                      GLenum(GL_TEXTURE_CUBE_MAP_POSITIVE_X),
+                                                      myDepthStencilTexture->TextureId(), 0);
       }
       else
       {
@@ -952,9 +946,21 @@ void OpenGl_FrameBuffer::BindReadBuffer (const Handle(OpenGl_Context)& theGlCtx)
 void OpenGl_FrameBuffer::BindBufferCube (const Handle(OpenGl_Context)& theGlCtx, const Standard_Integer theFace)
 {
   theGlCtx->arbFBO->glBindFramebuffer (GL_DRAW_FRAMEBUFFER, myGlFBufferId);
-  theGlCtx->arbFBO->glFramebufferTexture2D (GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
-                                            GLenum(GL_TEXTURE_CUBE_MAP_POSITIVE_X + theFace),
-                                            myDepthStencilTexture->TextureId(), 0);
+  if (hasDepthStencilAttach (theGlCtx))
+  {
+    theGlCtx->arbFBO->glFramebufferTexture2D (GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
+                                              GLenum(GL_TEXTURE_CUBE_MAP_POSITIVE_X + theFace),
+                                              myDepthStencilTexture->TextureId(), 0);
+  }
+  else
+  {
+    theGlCtx->arbFBO->glFramebufferTexture2D (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
+                                              GLenum(GL_TEXTURE_CUBE_MAP_POSITIVE_X + theFace),
+                                              myDepthStencilTexture->TextureId(), 0);
+    theGlCtx->arbFBO->glFramebufferTexture2D (GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
+                                              GLenum(GL_TEXTURE_CUBE_MAP_POSITIVE_X + theFace),
+                                              myDepthStencilTexture->TextureId(), 0);
+  }
 }
 
 // =======================================================================
index 0dd894ff1d81ee61ac7c5d90c29285d46703610f..91d6d4a05466836cb46da559a7d3ffdc16790530 100644 (file)
@@ -15,6 +15,7 @@
 
 #include <OpenGl_ShaderManager.hxx>
 
+#include <Graphic3d_Camera.hxx>
 #include <Graphic3d_CubeMapPacked.hxx>
 #include <Graphic3d_TextureParams.hxx>
 #include <OpenGl_Aspects.hxx>
@@ -576,6 +577,25 @@ void OpenGl_ShaderManager::pushLightSourceState (const Handle(OpenGl_ShaderProgr
     theProgram->SetUniform (myContext, aShadowMatLoc, aNbShadowMaps, &myShadowMatArray.First());
     theProgram->SetUniform (myContext, theProgram->GetStateLocation (OpenGl_OCC_LIGHT_SHADOWMAP_SIZE_BIAS), aSizeBias);
   }
+  if (const OpenGl_ShaderUniformLocation aShadowRangeLoc = theProgram->GetStateLocation (OpenGl_OCC_LIGHT_SHADOWMAP_RANGEPARAMS))
+  {
+    Standard_Integer aNbShadowMaps = theProgram->NbShadowMaps() + theProgram->NbShadowCubeMaps();
+    if (myShadowRangeArray.Size() < aNbShadowMaps)
+    {
+      myShadowRangeArray.Resize (0, aNbShadowMaps - 1, false);
+    }
+
+    if (myLightSourceState.HasShadowMaps())
+    {
+      const Standard_Integer aNbShadows = Min (aNbShadowMaps, myLightSourceState.ShadowMaps()->Size());
+      for (Standard_Integer aShadowIter = 0; aShadowIter < aNbShadows; ++aShadowIter)
+      {
+        const Handle(OpenGl_ShadowMap)& aShadow = myLightSourceState.ShadowMaps()->Value (aShadowIter);
+        myShadowRangeArray[aShadowIter] = Graphic3d_Vec2 (aShadow->Camera()->ZNear(), aShadow->Camera()->ZFar());
+      }
+    }
+    theProgram->SetUniform (myContext, aShadowRangeLoc, aNbShadowMaps, &myShadowRangeArray.First());
+  }
 }
 
 // =======================================================================
index f8dc9da70b343e9f38da0edc0d32e1e0d0fda089..ba898e8df6fbe5faae89b40ae9017742c3a76465 100644 (file)
@@ -801,6 +801,7 @@ protected:
   mutable NCollection_Array1<Standard_Integer>             myLightTypeArray;
   mutable NCollection_Array1<OpenGl_ShaderLightParameters> myLightParamsArray;
   mutable NCollection_Array1<Graphic3d_Mat4>               myShadowMatArray;
+  mutable NCollection_Array1<Graphic3d_Vec2>               myShadowRangeArray;
   mutable NCollection_Array1<OpenGl_Vec4>                  myClipPlaneArray;
   mutable NCollection_Array1<OpenGl_Vec4d>                 myClipPlaneArrayFfp;
   mutable NCollection_Array1<Standard_Integer>             myClipChainArray;
index 211aa13e48587fea87403842a3425f31fec78fb8..4088c4e59116dda8cfe1081e91dda3c4cbbf0895 100755 (executable)
@@ -62,6 +62,7 @@ Standard_CString OpenGl_ShaderProgram::PredefinedKeywords[] =
   "occShadowMapSamplers",     // OpenGl_OCC_LIGHT_SHADOWMAP_SAMPLERS,
   "occShadowCubeMapSamplers", // OpenGl_OCC_LIGHT_SHADOWCUBEMAP_SAMPLERS,
   "occShadowMapMatrices",     // OpenGl_OCC_LIGHT_SHADOWMAP_MATRICES,
+  "occShadowMapRangeParams",  // OpenGl_OCC_LIGHT_SHADOWMAP_RANGEPARAMS,
 
   "occTextureEnable",         // OpenGl_OCCT_TEXTURE_ENABLE
   "occDistinguishingMode",    // OpenGl_OCCT_DISTINGUISH_MODE
index 73d588b462bd016fab8446fda362106c9f9bc5c9..f14a2e00345bc6fb091f9cb62641c1a0f99496c2 100755 (executable)
@@ -61,6 +61,7 @@ enum OpenGl_StateVariable
   OpenGl_OCC_LIGHT_SHADOWMAP_SAMPLERS,     // occShadowMapSamplers
   OpenGl_OCC_LIGHT_SHADOWCUBEMAP_SAMPLERS, // occShadowCubeMapSamplers
   OpenGl_OCC_LIGHT_SHADOWMAP_MATRICES,     // occShadowMapMatrices
+  OpenGl_OCC_LIGHT_SHADOWMAP_RANGEPARAMS,  // occShadowMapRangeParams
 
   // Material state
   OpenGl_OCCT_TEXTURE_ENABLE,
index 296d205be88dc90ff49ddcb6ced1480ec7aca142..9d3f2c7179689196286eb6c372d0e6d883deb959 100644 (file)
@@ -138,13 +138,12 @@ bool OpenGl_ShadowMap::UpdateCamera (const Graphic3d_CView& theView,
       myShadowCamera->SetZeroToOneDepth (theView.Camera()->IsZeroToOneDepth());
       myShadowCamera->SetProjectionType (Graphic3d_Camera::Projection_Perspective);
       myShadowCamera->SetFOVy (90.0);
-      const gp_Pnt& aLightPos = myShadowLight->Position();
-      myShadowCamera->MoveEyeTo (aLightPos);
+      myShadowCamera->MoveEyeTo (myShadowLight->Position());
       // calculate direction and up vector for the given cubemap face
       myShadowCamera->SetDirectionFromEye (Graphic3d_CubeMap::GetCubeDirection ((Graphic3d_CubeMapSide)theFace));
       myShadowCamera->SetUp (Graphic3d_CubeMap::GetCubeUp ((Graphic3d_CubeMapSide)theFace));
       // setup znear and zfar (default value)
-      myShadowCamera->SetZRange (1.0, myShadowCamera->GetDefaultZFar());
+      myShadowCamera->SetZRange (1.0, myShadowLight->Range() <= 1.0 ? Graphic3d_Camera::GetDefaultZFar() : myShadowLight->Range());
       myLightMatrix = myShadowCamera->ProjectionMatrixF() * myShadowCamera->OrientationMatrixF();
 
       return true;
index 1e00967edc7f079bab2482751e418d950be567bf..b0c00e60f964ab30035ed5a99d30ea47a52a4e10 100644 (file)
@@ -524,12 +524,12 @@ bool OpenGl_Texture::Init (const Handle(OpenGl_Context)& theCtx,
       if (theCtx->GraphicsLibrary() == Aspect_GraphicsLibrary_OpenGL)
       {
         // use proxy to check texture could be created or not
-        theCtx->core11fwd->glTexImage2D (GL_PROXY_TEXTURE_2D, 0, anIntFormat,
+        theCtx->core11fwd->glTexImage2D (GL_PROXY_TEXTURE_CUBE_MAP, 0, anIntFormat,
                                          theSizeXYZ.x(), theSizeXYZ.y(), 0,
                                          theFormat.PixelFormat(), theFormat.DataType(), NULL);
-        theCtx->core11fwd->glGetTexLevelParameteriv (GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &aTestWidth);
-        theCtx->core11fwd->glGetTexLevelParameteriv (GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &aTestHeight);
-        theCtx->core11fwd->glGetTexLevelParameteriv (GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &mySizedFormat);
+        theCtx->core11fwd->glGetTexLevelParameteriv (GL_PROXY_TEXTURE_CUBE_MAP, 0, GL_TEXTURE_WIDTH, &aTestWidth);
+        theCtx->core11fwd->glGetTexLevelParameteriv (GL_PROXY_TEXTURE_CUBE_MAP, 0, GL_TEXTURE_HEIGHT, &aTestHeight);
+        theCtx->core11fwd->glGetTexLevelParameteriv (GL_PROXY_TEXTURE_CUBE_MAP, 0, GL_TEXTURE_INTERNAL_FORMAT, &mySizedFormat);
         if (aTestWidth == 0 || aTestHeight == 0)
         {
           // no memory or broken input parameters
index 22ee42fad0acefde0b8c112ab01ddf60fed6069c..34fe02ac05d2e25f7e7bb25996daf29dbb050ea0 100644 (file)
@@ -1732,7 +1732,7 @@ void OpenGl_View::Redraw()
         aShadowMap->SetLightSource (aLight);
         if (aLight->Type() == Graphic3d_TypeOfLightSource_Positional)
         {
-          // point light shadows are not currently supported on opengles 2.0.
+          // cube shadow maps are not currently working on opengles 2.0.
           if (aCtx->GraphicsLibrary() != Aspect_GraphicsLibrary_OpenGLES
            || aCtx->VersionMajor() >= 3)
           {
index 463287c40e7c26638747b081a028bb3062164a2f..318ad94982c94dd2de355b4c186f82b0eecac89c 100644 (file)
@@ -1,6 +1,5 @@
 //! Function computes point light shadow attenuation (1.0 means no shadow).
 float occLightPointShadow (in samplerCube theShadow,
-                           //in vec2 theDepthRange,
                            in int  theId,
                            in vec3 thePoint,
                            in vec3 theNormal)
@@ -11,8 +10,9 @@ float occLightPointShadow (in samplerCube theShadow,
   vec3 anAbsVec = abs (aLightDir);
   float aLocalZcomp = max (anAbsVec.x, max (anAbsVec.y, anAbsVec.z));
   // set znear and zfar
-  float aNear = POINTLIGHT_ZNEAR;
-  float aFar = POINTLIGHT_ZFAR;
+  float aRange = occShadowMapRangeParams[theId].y;
+  float aNear = occShadowMapRangeParams[theId].x;
+  float aFar = aRange <= aNear ? POINTLIGHT_ZFAR : aRange;
   float aNormZComp = (aFar + aNear) / (aFar - aNear) - (2.0 * aFar * aNear) / (aFar - aNear) / aLocalZcomp;
   float aDist = (aNormZComp + 1.0) * 0.5;
   // calculate bias and test depth.
index 1389d6ba3251961e8ff2edf9543d4772602cc91c..14713875e4150908811fba8171b77e958344df63 100644 (file)
@@ -14,8 +14,9 @@ static const char Shaders_LightPointShadow_glsl[] =
   "  vec3 anAbsVec = abs (aLightDir);\n"
   "  float aLocalZcomp = max (anAbsVec.x, max (anAbsVec.y, anAbsVec.z));\n"
   "  // set znear and zfar\n"
-  "  float aNear = POINTLIGHT_ZNEAR;\n"
-  "  float aFar = POINTLIGHT_ZFAR;\n"
+  "  float aRange = occShadowMapRangeParams[theId].y;\n"
+  "  float aNear = occShadowMapRangeParams[theId].x;\n"
+  "  float aFar = aRange <= aNear ? POINTLIGHT_ZFAR : aRange;\n"
   "  float aNormZComp = (aFar + aNear) / (aFar-aNear) - (2.0 * aFar * aNear) / (aFar - aNear) / aLocalZcomp;\n"
   "  float aDist = (aNormZComp + 1.0) * 0.5;\n"
   "  // calculate bias and test depth.\n"
index 0c5ae9fcd4e537fd50849f166c24308d3ebb34be..e3ff8e7514ab55736be7794b3a087cc88d48d829 100644 (file)
@@ -28,7 +28,7 @@ vsetcolor b6 SALMON3
 vsetcolor b7 PURPLE3
 # add light
 vlight -clear
-vlight pntlight1 -type POSITIONAL -pos 0 100 0 -intensity 1000 -castShadows 1
+vlight pntlight1 -type POSITIONAL -pos 0 100 0 -intensity 1000 -castShadows 1 -range 1000
 
 #dump single light phong
 vviewparams -scale 8.0 -proj 0.0 1.0 0.0 -up 0.0 0.0 1.0 -at 0.0 0.0 0.0