]> OCCT Git - occt-copy.git/commitdiff
0032325: Visualization - Possibility to use fixed positioned light source CR32325
authornds <nds@opencascade.com>
Fri, 23 Apr 2021 15:06:50 +0000 (18:06 +0300)
committernds <nds@opencascade.com>
Fri, 23 Apr 2021 15:06:50 +0000 (18:06 +0300)
- implementation for Positional light only.

src/AIS/AIS_LightSource.cxx
src/Graphic3d/Graphic3d_CLight.cxx
src/Graphic3d/Graphic3d_CLight.hxx
src/OpenGl/OpenGl_ShaderManager.cxx
src/OpenGl/OpenGl_ShaderManager.hxx
src/ViewerTest/ViewerTest_ViewerCommands.cxx
tests/v3d/light_source/positional_2dpers [new file with mode: 0644]

index 49c718a61261947758310d51315c49b96a5528a9..6df4091e53bb143f3fc8fb7d3ac3c83f0aead7e5 100644 (file)
@@ -248,6 +248,11 @@ void AIS_LightSource::updateLightTransformPersistence()
       else
       {
         aTrsfPers.Nullify();
+        if (myLightSource->Is2DPers() && myLightSource->Type() == Graphic3d_TOLS_POSITIONAL)
+        {
+          aTrsfPers = new Graphic3d_TransformPers (Graphic3d_TMF_2d, Aspect_TOTP_LEFT_LOWER,
+            Graphic3d_Vec2i(myLightSource->Position().X(), myLightSource->Position().Y()));
+        }
       }
       break;
     }
@@ -291,7 +296,10 @@ void AIS_LightSource::updateLightLocalTransformation()
       if (myIsZoomable)
       {
         gp_Trsf aTrsf;
-        aTrsf.SetTranslation (gp::Origin(), myLightSource->Position());
+        if (!myLightSource->Is2DPers())
+        {
+          aTrsf.SetTranslation (gp::Origin(), myLightSource->Position());
+        }
         myLocalTransformation = new TopLoc_Datum3D (aTrsf);
       }
       break;
index 228c6a64f8cb4cb3b30297f29ff37d4f297faa97..e7200aa62e595f94f18bc496176cc8a15a8f4a03 100644 (file)
@@ -57,6 +57,7 @@ Graphic3d_CLight::Graphic3d_CLight (Graphic3d_TypeOfLightSource theType)
   myType       (theType),
   myRevision   (0),
   myIsHeadlight(false),
+  myIs2DPers(false),
   myIsEnabled  (true),
   myToCastShadows (false)
 {
@@ -138,6 +139,19 @@ void Graphic3d_CLight::SetHeadlight (Standard_Boolean theValue)
   myIsHeadlight = theValue;
 }
 
+// =======================================================================
+// function : SetIs2DPers
+// purpose  :
+// =======================================================================
+void Graphic3d_CLight::SetIs2DPers (Standard_Boolean theValue)
+{
+  if (myType == Graphic3d_TOLS_AMBIENT)
+  {
+    throw Standard_ProgramError ("Graphic3d_CLight::SetIs2DPers() is not applicable to ambient light");
+  }
+  myIs2DPers = theValue;
+}
+
 // =======================================================================
 // function : SetDirection
 // purpose  :
@@ -322,5 +336,6 @@ void Graphic3d_CLight::DumpJson (Standard_OStream& theOStream, Standard_Integer
   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myType)
   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myRevision)
   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myIsHeadlight)
+  OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myIs2DPers)
   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myIsEnabled)
 }
index cb44777709457f0cdf136288da77992b4ed5e26f..9cc5f4acb6c6bb6c9dea0b6a2affdce38f112657 100644 (file)
@@ -75,6 +75,13 @@ public:
   //! Setup headlight flag.
   Standard_EXPORT void SetHeadlight (Standard_Boolean theValue);
 
+  //! Returns true if the light is positioned in 2D persistence; FALSE by default.
+  //! Headlight flag means that light position/direction are defined not in a World coordinate system, but relative to the camera orientation.
+  Standard_Boolean Is2DPers() const { return myIs2DPers; }
+
+  //! Setup 2D persistence for positioning light.
+  Standard_EXPORT void SetIs2DPers (Standard_Boolean theValue);
+
 //! @name positional/spot light properties
 public:
 
@@ -275,6 +282,7 @@ protected:
   const Graphic3d_TypeOfLightSource myType;        //!< Graphic3d_TypeOfLightSource enumeration
   Standard_Size                     myRevision;    //!< modification counter
   Standard_Boolean                  myIsHeadlight; //!< flag to mark head light
+  Standard_Boolean                  myIs2DPers;    //!< flag to mark head light position in 2D persistence
   Standard_Boolean                  myIsEnabled;   //!< enabled state
   Standard_Boolean                  myToCastShadows;//!< casting shadows is requested
 
index a1cc8c7077ce83916095e64f2618d659775888d7..334a66f554686b66b1c90755039667937b825677 100644 (file)
@@ -354,6 +354,22 @@ void OpenGl_ShaderManager::UpdateWorldViewStateTo (const OpenGl_Mat4& theWorldVi
   myWorldViewState.Update();
 }
 
+// =======================================================================
+// function : convertTo2DPersPosition
+// purpose  :
+// =======================================================================
+gp_Pnt OpenGl_ShaderManager::convertTo2DPersPosition (const gp_Pnt& thePosition) const
+{
+  OpenGl_Mat4 aProjMat = myContext->ProjectionState.Current();
+  OpenGl_Mat4 aModelMat = myContext->ModelWorldState.Current() * myContext->WorldViewState.Current();
+
+  Standard_ShortReal aX, anY, aZ;
+  Graphic3d_TransformUtils::UnProject<Standard_ShortReal> (thePosition.X(), thePosition.Y(), 0,
+                                                           aModelMat, aProjMat, myContext->Viewport(),
+                                                           aX, anY, aZ);
+  return gp_Pnt(aX, anY, aZ);
+}
+
 // =======================================================================
 // function : pushLightSourceState
 // purpose  :
@@ -520,6 +536,15 @@ void OpenGl_ShaderManager::pushLightSourceState (const Handle(OpenGl_ShaderProgr
           const Graphic3d_Mat4& anOrientInv = myWorldViewState.WorldViewMatrixInverse();
           aLightParams.Position = anOrientInv * Graphic3d_Vec4 (aLightParams.Position.xyz(), 1.0f);
         }
+        else if (aLight.Is2DPers())
+        {
+          gp_Pnt aPosition = convertTo2DPersPosition (aLight.Position());
+
+          aLightParams.Position.x() = static_cast<float>(aPosition.X());
+          aLightParams.Position.y() = static_cast<float>(aPosition.Y());
+          aLightParams.Position.z() = static_cast<float>(aPosition.Z());
+          aLightParams.Position.w() = 0.0f;
+        }
         else
         {
           aLightParams.Position.x() = static_cast<float>(aLight.Position().X() - myLocalOrigin.X());
index b1653f9f55526c1cca47e71a787756a7d7f33451..fc896c0d6283e5cb6df118eec21eb2631d471dd0 100644 (file)
@@ -273,6 +273,9 @@ public:
   //! Invalidate state of OCCT light sources.
   Standard_EXPORT void UpdateLightSourceState();
 
+  //! Converts position to 3D point in 2D pesistence.
+  gp_Pnt convertTo2DPersPosition (const gp_Pnt& thePosition) const;
+
   //! Pushes current state of OCCT light sources to specified program (only on state change).
   //! Note that light sources definition depends also on WorldViewState.
   void PushLightSourceState (const Handle(OpenGl_ShaderProgram)& theProgram) const
index e67e667bba4032641c2be3335ce0f9fd14dd3c18..8cab68998b37bdebaf7874ed81fa569df72d01a3 100644 (file)
@@ -11044,6 +11044,24 @@ static int VLight (Draw_Interpretor& theDi,
       }
       aLightCurr->SetHeadlight (isHeadLight);
     }
+    else if (anArgCase.IsEqual ("2DPERS") ||
+             anArgCase.IsEqual ("-2DPERS"))
+    {
+      if (aLightCurr.IsNull()
+       || aLightCurr->Type() == Graphic3d_TOLS_AMBIENT)
+      {
+        Message::SendFail() << "Syntax error at argument '" << anArg << "'";
+        return 1;
+      }
+
+      Standard_Boolean is2DPers = Standard_False;
+      if (anArgIt + 1 < theArgsNb
+       && Draw::ParseOnOff (theArgVec[anArgIt + 1], is2DPers))
+      {
+        ++anArgIt;
+      }
+      aLightCurr->SetIs2DPers (is2DPers);
+    }
     else if (anArgCase.IsEqual ("NAME")
           || anArgCase.IsEqual ("-NAME"))
     {
@@ -15091,6 +15109,7 @@ void ViewerTest::ViewerCommands(Draw_Interpretor& theCommands)
     "\n        -{dir}ection X Y Z (for directional light or for spotlight)"
     "\n        -color colorName"
     "\n        -{head}light 0|1"
+    "\n        -2dpers 0|1"
     "\n        -castShadows 0|1"
     "\n        -{sm}oothness value"
     "\n        -{int}ensity value"
diff --git a/tests/v3d/light_source/positional_2dpers b/tests/v3d/light_source/positional_2dpers
new file mode 100644 (file)
index 0000000..2d54e72
--- /dev/null
@@ -0,0 +1,32 @@
+puts "================================="
+puts "0032325: Visualization - Possibility to use fixed positioned light source"
+puts "================================="
+
+pload MODELING VISUALIZATION
+vclear
+vinit View1
+vlight -clear
+
+vbackground -color GRAY
+vrenderparams -shadingModel PHONG
+vlight -add ambient -COLOR WHITE -intensity 0.1
+
+box b 10 10 10 30 30 30
+vdisplay b -dispMode 1
+vaspects b -material Brass
+vfit
+
+puts "=== Check headlight option with positional light ==="
+vlight -add positional -2dpers 1 -color WHITE -display Light1 -pos 30 20 0
+
+vlight -add positional -2dpers 1 -color WHITE -display Light2 -pos 380 370 0
+
+vlight -add positional -2dpers 1 -color WHITE -display Light3 -pos 30 380 0
+
+vdump $imagedir/${casename}_1.png
+
+vrotate 10 0 0
+vdump $imagedir/${casename}_2.png
+
+vpan -60 0
+vdump $imagedir/${casename}_3.png