]> OCCT Git - occt.git/commitdiff
Don't move grid during rotation
authormzernova <mzernova@opencascade.com>
Mon, 20 Jan 2025 01:34:53 +0000 (01:34 +0000)
committermzernova <mzernova@opencascade.com>
Mon, 20 Jan 2025 01:34:53 +0000 (01:34 +0000)
src/AIS/AIS_ViewController.cxx
src/Graphic3d/Graphic3d_Camera.hxx
src/OpenGl/OpenGl_View.cxx

index 1bf18c6d49c154b79c39ef3437543e829a29d7de..9a463779af916bf671e57c7bd00753933e036197 100644 (file)
@@ -1728,6 +1728,7 @@ void AIS_ViewController::handleOrbitRotation (const Handle(V3d_View)& theView,
     myCamStartOpToEye    = gp_Vec (myRotatePnt3d, aCam->Eye()).Transformed (aTrsf);
     myCamStartOpToCenter = gp_Vec (myRotatePnt3d, aCam->Center()).Transformed (aTrsf);
 
+    aCam->SetRotationPoint (myRotatePnt3d);
     theView->Invalidate();
   }
 
@@ -1840,6 +1841,7 @@ void AIS_ViewController::handleViewRotation (const Handle(V3d_View)& theView,
     const gp_Quaternion aRot = aTrsf.GetRotation();
     double aRollDummy = 0.0;
     aRot.GetEulerAngles (gp_YawPitchRoll, myRotateStartYawPitchRoll[0], myRotateStartYawPitchRoll[1], aRollDummy);
+    aCam->SetRotationPoint (gp::Origin());
   }
   if (toRotateAnyway)
   {
index aeebc7cbf61f839f7ee111ae2eb61231d7a0045d..17875b5c74aea6f662fb85929d631f3fc1981cdd 100644 (file)
@@ -275,6 +275,20 @@ public:
   //! @param theCenter [in] the point where the camera looks at.
   Standard_EXPORT void SetCenter (const gp_Pnt& theCenter);
 
+  //! Get active rotation center of the camera (gravity point).
+  //! @return the gravity point.
+  gp_Pnt RotationPoint() const
+  {
+    return myRotationPoint;
+  }
+
+  //! Sets active rotation center of the camera (gravity point).
+  //! @param theRotationPoint [in] the gravity point.
+  void SetRotationPoint (const gp_Pnt& theRotationPoint)
+  {
+    myRotationPoint = theRotationPoint;
+  };
+
   //! Get distance of Eye from camera Center.
   //! @return the distance.
   Standard_Real Distance() const { return myDistance; }
@@ -839,6 +853,7 @@ private:
   gp_Dir        myUp;       //!< Camera up direction vector
   gp_Dir        myDirection;//!< Camera view direction (from eye)
   gp_Pnt        myEye;      //!< Camera eye position
+  gp_Pnt        myRotationPoint;
   Standard_Real myDistance; //!< distance from Eye to Center
 
   gp_XYZ myAxialScale; //!< World axial scale.
index 3f11f59093d3302b9f9ab34be9dabe2a07403ee4..d6b9b776a9cf0c440addb72e9f9c8d7bd7f8ec32 100644 (file)
@@ -2563,33 +2563,43 @@ void OpenGl_View::renderGrid()
   }
 
   const Handle(OpenGl_Context)& aContext = myWorkspace->GetGlContext();
+  const Handle(Graphic3d_Camera)& aCamera = aContext->Camera();
 
   Bnd_Box aBnd = MinMaxValues (Standard_True);
   if (myGridParams.IsBackground() || aBnd.IsOut (myGridParams.Position()))
   {
     aBnd.Add (myGridParams.Position());
-    aContext->Camera()->ZFitAll (1.0, aBnd, aBnd);
+    aCamera->ZFitAll (1.0, aBnd, aBnd);
   }
 
-  const Standard_Real aZNear = aContext->Camera()->ZNear();
-  const Standard_Real aZFar = aContext->Camera()->ZFar();
-  const Graphic3d_Camera::Projection aProjectionType = aContext->Camera()->ProjectionType();
+  const Standard_Real aZNear = aCamera->ZNear();
+  const Standard_Real aZFar = aCamera->ZFar();
+  const Graphic3d_Camera::Projection aProjectionType = aCamera->ProjectionType();
 
-  aContext->Camera()->SetZRange (aZNear, Max (aZNear * 1.001, aZFar));
+  aCamera->SetZRange (aZNear, Max (aZNear * 1.001, aZFar));
   if (myGridParams.IsBackground())
   {
-    aContext->Camera()->SetProjectionType (Graphic3d_Camera::Projection_Orthographic);
+    aCamera->SetProjectionType (Graphic3d_Camera::Projection_Orthographic);
   }
 
   aContext->ProjectionState.Push();
-  aContext->ProjectionState.SetCurrent (aContext->Camera()->ProjectionMatrixF());
+  aContext->ProjectionState.SetCurrent (aCamera->ProjectionMatrixF());
   aContext->ApplyProjectionMatrix();
 
   const OpenGl_Mat4& aWorldViewCurrent = aContext->WorldViewState.Current();
   OpenGl_Mat4 aWorldViewState = myGridParams.IsBackground() ? OpenGl_Mat4() : aWorldViewCurrent;
-  const gp_Pnt& aPosition = myGridParams.IsBackground()
-                          ? gp_Pnt (aWorldViewCurrent.GetValue (0, 3), aWorldViewCurrent.GetValue (1, 3), -aZFar)
-                          : myGridParams.Position();
+
+  gp_Pnt aPosition = myGridParams.Position();
+  if (myGridParams.IsBackground())
+  {
+    gp_Pnt aRotationPoint = aCamera->RotationPoint();
+    Graphic3d_Vec4 aRotationVec ((float)aRotationPoint.X(), (float)aRotationPoint.Y(), (float)aRotationPoint.Z(), 1.0);
+    OpenGl_Mat4 aTranslation, aTranslationInv;
+    aTranslation.SetColumn (3, aRotationVec);
+    aTranslationInv.SetColumn (3, -aRotationVec);
+    OpenGl_Mat4 aWorldViewStateCorrected = aTranslationInv * aWorldViewCurrent * aTranslation;
+    aPosition.ChangeCoord() += gp_XYZ (aWorldViewStateCorrected (0, 3), aWorldViewStateCorrected (1, 3), -aZFar);
+  }
   OpenGl_Mat4 aTranslation;
   aTranslation.SetColumn (3, Graphic3d_Vec4 ((float)aPosition.X(), (float)aPosition.Y(), (float)aPosition.Z(), 1.0));
 
@@ -2607,7 +2617,7 @@ void OpenGl_View::renderGrid()
     aContext->core11fwd->glEnable (GL_DEPTH_CLAMP);
   }
 
-  const Standard_Real aCameraScale = aContext->Camera()->Scale();
+  const Standard_Real aCameraScale = aCamera->Scale();
   Standard_Real aScale = myGridParams.IsInfinity()
                        ? 10.0 / pow (10.0, floor (log10 (Max (aCameraScale, 1.0))) + 1.0)
                        : myGridParams.Scale();
@@ -2615,8 +2625,8 @@ void OpenGl_View::renderGrid()
   if (aContext->ShaderManager()->BindGridProgram())
   {
     const Handle(OpenGl_ShaderProgram)& aProg = aContext->ActiveProgram();
-    aProg->SetUniform (aContext, "uZNear", GLfloat (aContext->Camera()->ZNear()));
-    aProg->SetUniform (aContext, "uZFar",  GLfloat (aContext->Camera()->ZFar()));
+    aProg->SetUniform (aContext, "uZNear", GLfloat (aCamera->ZNear()));
+    aProg->SetUniform (aContext, "uZFar",  GLfloat (aCamera->ZFar()));
     aProg->SetUniform (aContext, "uScale", GLfloat (aScale));
     aProg->SetUniform (aContext, "uThickness", GLfloat (myGridParams.LineThickness()));
     aProg->SetUniform (aContext, "uColor", OpenGl_Vec3 (myGridParams.Color().Rgb()));
@@ -2627,8 +2637,8 @@ void OpenGl_View::renderGrid()
     aContext->BindProgram (NULL);
   }
 
-  aContext->Camera()->SetZRange (aZNear, aZFar);
-  aContext->Camera()->SetProjectionType (aProjectionType);
+  aCamera->SetZRange (aZNear, aZFar);
+  aCamera->SetProjectionType (aProjectionType);
   aContext->core11fwd->glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   if (aContext->arbDepthClamp && !wasDepthClamped)
   {