From ee333b42b92296e6ece46a12bca70fee90ce4562 Mon Sep 17 00:00:00 2001 From: nds Date: Fri, 23 Apr 2021 18:06:50 +0300 Subject: [PATCH] 0032325: Visualization - Possibility to use fixed positioned light source - implementation for Positional light only. --- src/AIS/AIS_LightSource.cxx | 10 +++++- src/Graphic3d/Graphic3d_CLight.cxx | 15 +++++++++ src/Graphic3d/Graphic3d_CLight.hxx | 8 +++++ src/OpenGl/OpenGl_ShaderManager.cxx | 25 +++++++++++++++ src/OpenGl/OpenGl_ShaderManager.hxx | 3 ++ src/ViewerTest/ViewerTest_ViewerCommands.cxx | 19 ++++++++++++ tests/v3d/light_source/positional_2dpers | 32 ++++++++++++++++++++ 7 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 tests/v3d/light_source/positional_2dpers diff --git a/src/AIS/AIS_LightSource.cxx b/src/AIS/AIS_LightSource.cxx index 49c718a612..6df4091e53 100644 --- a/src/AIS/AIS_LightSource.cxx +++ b/src/AIS/AIS_LightSource.cxx @@ -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; diff --git a/src/Graphic3d/Graphic3d_CLight.cxx b/src/Graphic3d/Graphic3d_CLight.cxx index 228c6a64f8..e7200aa62e 100644 --- a/src/Graphic3d/Graphic3d_CLight.cxx +++ b/src/Graphic3d/Graphic3d_CLight.cxx @@ -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) } diff --git a/src/Graphic3d/Graphic3d_CLight.hxx b/src/Graphic3d/Graphic3d_CLight.hxx index cb44777709..9cc5f4acb6 100644 --- a/src/Graphic3d/Graphic3d_CLight.hxx +++ b/src/Graphic3d/Graphic3d_CLight.hxx @@ -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 diff --git a/src/OpenGl/OpenGl_ShaderManager.cxx b/src/OpenGl/OpenGl_ShaderManager.cxx index a1cc8c7077..334a66f554 100644 --- a/src/OpenGl/OpenGl_ShaderManager.cxx +++ b/src/OpenGl/OpenGl_ShaderManager.cxx @@ -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 (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(aPosition.X()); + aLightParams.Position.y() = static_cast(aPosition.Y()); + aLightParams.Position.z() = static_cast(aPosition.Z()); + aLightParams.Position.w() = 0.0f; + } else { aLightParams.Position.x() = static_cast(aLight.Position().X() - myLocalOrigin.X()); diff --git a/src/OpenGl/OpenGl_ShaderManager.hxx b/src/OpenGl/OpenGl_ShaderManager.hxx index b1653f9f55..fc896c0d62 100644 --- a/src/OpenGl/OpenGl_ShaderManager.hxx +++ b/src/OpenGl/OpenGl_ShaderManager.hxx @@ -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 diff --git a/src/ViewerTest/ViewerTest_ViewerCommands.cxx b/src/ViewerTest/ViewerTest_ViewerCommands.cxx index e67e667bba..8cab68998b 100644 --- a/src/ViewerTest/ViewerTest_ViewerCommands.cxx +++ b/src/ViewerTest/ViewerTest_ViewerCommands.cxx @@ -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 index 0000000000..2d54e72247 --- /dev/null +++ b/tests/v3d/light_source/positional_2dpers @@ -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 -- 2.39.5