Index: trunk/geant4/visualization/OpenGL/src/G4OpenGLQtViewer.cc
===================================================================
--- trunk/geant4/visualization/OpenGL/src/G4OpenGLQtViewer.cc	(revision 800)
+++ trunk/geant4/visualization/OpenGL/src/G4OpenGLQtViewer.cc	(revision 801)
@@ -102,11 +102,11 @@
 {
   G4OpenGLViewer::ClearView ();
-  //  glLoadIdentity();
-  glRotatef(fRotationAngleX, 1.0, 0.0, 0.0);
-  glRotatef(fRotationAngleY, 0.0, 1.0, 0.0);
-  glRotatef(fRotationAngleZ, 0.0, 0.0, 1.0);
-  fRotationAngleZ +=fDeltaRotationAngleZ;
-  fRotationAngleY +=fDeltaRotationAngleY;
-  fRotationAngleX +=fDeltaRotationAngleX;
+  // glLoadIdentity();
+  //   glRotatef(fRotationAngleX, 1.0, 0.0, 0.0);
+//    glRotatef(fRotationAngleY, 0.0, 1.0, 0.0);
+//    glRotatef(fRotationAngleZ, 0.0, 0.0, 1.0);
+//    fRotationAngleZ +=fDeltaRotationAngleZ;
+//    fRotationAngleY +=fDeltaRotationAngleY;
+//    fRotationAngleX +=fDeltaRotationAngleX;
 }
 
@@ -1605,30 +1605,15 @@
   fLastPos2 = fLastPos1;
   fLastPos1 = QPoint(pos_x, pos_y);
+
   int deltaX = fLastPos2.x()-fLastPos1.x();
   int deltaY = fLastPos2.y()-fLastPos1.y();
 
-  // try to get the x,y mouve into user friendly rotation
-
-  float cox = std::cos ((fRotationAngleX*M_PI)/180);
-  float coy = std::cos ((fRotationAngleY*M_PI)/180);
-  float coz = 1;
-  float six = std::sin ((fRotationAngleX*M_PI)/180);
-  float siy = std::sin ((fRotationAngleY*M_PI)/180);
-  float siz = 0;
-
-  float dx = coy*(siz*deltaY+coz*deltaX);
-  float dy = six*(siy*(siz*deltaY+coz*deltaX))+cox*(coz*deltaY-siz*deltaX);
-  float dz = cox*(siy*(siz*deltaY+coz*deltaX))-six*(coz*deltaY-siz*deltaX);
-  
-#ifdef GEANT4_QT_DEBUG
-  printf("G4OpenGLQtViewer::MouseMove %d,%d Rot:%f,%f new : %f %f %f\n",deltaX,deltaY,fRotationAngleX,fRotationAngleY,dx,dy,dz);
-#endif
   if (fMouseAction == STYLE1) {  // rotate
     if (mButtons & Qt::LeftButton) {
-      rotateQtScene(fLastPos2.x()-fLastPos1.x(),fLastPos2.y()-fLastPos1.y());
+      rotateQtScene(deltaX,deltaY);
     }
   } else if (fMouseAction == STYLE2) {  // move
     if (mButtons & Qt::LeftButton) {
-      moveScene(fLastPos1.x()-fLastPos2.x(),fLastPos1.y()-fLastPos2.y(),0,true);
+      moveScene(-deltaX,-deltaY,0,true);
     }
   }
@@ -1682,6 +1667,6 @@
     return;
   fHoldRotateEvent = true;
-  fDeltaRotationAngleX = dx;
-  fDeltaRotationAngleY = dy;
+
+  rotateSceneTest2(dx,dy,fDeltaRotation);
   //  rotateScene(dx,0,fDeltaRotation);
   //  rotateScene(0,dy,fDeltaRotation);
@@ -1703,4 +1688,60 @@
   fHoldRotateEvent = true;
 
+  rotateSceneTest2(dx,dy,fDeltaRotation);
+  //  rotateScene(dx,dy,fDeltaRotation);
+  updateQWidget();
+  
+  fHoldRotateEvent = false;
+}
+
+
+void G4OpenGLQtViewer::rotateSceneTest2(float dx, float dy,float deltaRot)
+{
+  fDeltaRotationAngleX = dx;
+  fDeltaRotationAngleY = dy;
+  //  rotateScene(dx,dy,fDeltaRotation);
+
+  const G4Point3D targetPoint
+    = fSceneHandler.GetScene()->GetStandardTargetPoint()
+    + fVP.GetCurrentTargetPoint ();
+  G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
+  if(radius<=0.) radius = 1.;
+  const G4double cameraDistance = fVP.GetCameraDistance (radius);
+  const G4Point3D cameraPosition =
+    targetPoint + cameraDistance * fVP.GetViewpointDirection().unit();
+  G4Vector3D up = fVP.GetUpVector ().unit ();
+  G4Vector3D new_dx ;
+  G4Vector3D new_cameraPosition ;
+  G4Vector3D new_up ;
+  G4Vector3D delta ;
+
+  if ((up[1]==1) &&(up[0]==0) && (up[2]==0)) {
+    up[1] = cameraPosition[2];
+  }
+  new_dx[2] = (cameraPosition[0]*up[1]-cameraPosition[1]*up[0])/100; // EN TEST  
+  new_dx[0] = (cameraPosition[1]*up[2]-cameraPosition[2]*up[1])/100;  // EN TEST 
+  new_dx[1] = (cameraPosition[2]*up[0]-cameraPosition[0]*up[2])/100;  // EN TEST 
+
+  delta = new_dx*dx*deltaRot + up*dy*deltaRot;
+  new_cameraPosition = cameraPosition + delta;
+  
+  new_up[2] = new_cameraPosition[0]*(-delta[1])-new_cameraPosition[1]*(-delta[0]); 
+  new_up[0] = new_cameraPosition[1]*(-delta[2])-new_cameraPosition[2]*(-delta[1]); 
+  new_up[1] = new_cameraPosition[2]*(-delta[0])-new_cameraPosition[0]*(-delta[2]); 
+
+
+#ifdef GEANT4_QT_DEBUG
+  printf("G4OpenGLQtViewer::Rotate2 Tp %f,%f %f up:%f %f %f dx:%f %f %f\n",
+         targetPoint[0],targetPoint[1],targetPoint[2],up[0],up[1],up[2],new_dx[0],new_dx[1],new_dx[2]);
+  printf("G4OpenGLQtViewer::Rotate2 Cp %f,%f %f new_up:%f %f %f delta:%f %f %f\n",
+         new_cameraPosition[0],new_cameraPosition[1],new_cameraPosition[2],new_up[0],new_up[1],new_up[2],delta[0],delta[1],delta[2]);
+#endif
+
+  fVP.SetUpVector(new_up);
+  fVP.SetViewAndLights (new_cameraPosition);
+}
+
+void G4OpenGLQtViewer::rotateSceneTest(float dx, float dy,float delta)
+{
   fDeltaRotationAngleX = dx;
   fDeltaRotationAngleY = dy;
@@ -1725,9 +1766,88 @@
   fDeltaRotationAngleZ = dz2;
 
-  //  rotateScene(dx,dy,fDeltaRotation);
-  updateQWidget();
-  
-  fHoldRotateEvent = false;
-}
+}
+
+void G4OpenGLQtViewer::rotateSceneInventor(float dx, float dy,float delta)
+{
+  // __________________________
+  // From Inventor
+  // --------------------------
+  //SoGuiExaminerViewerP::rotateCamera(SoCamera * cam,
+  //                                   const SbVec3f & aroundaxis,
+  //                                   const float delta)
+
+  // position of camera 
+  /*
+  const G4Point3D targetPoint
+    = fSceneHandler.GetScene()->GetStandardTargetPoint()
+    + fVP.GetCurrentTargetPoint ();
+  G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
+  if(radius<=0.) radius = 1.;
+  const G4double cameraDistance = fVP.GetCameraDistance (radius);
+  const G4Point3D cameraPosition =
+    targetPoint + cameraDistance * fVP.GetViewpointDirection().unit();
+
+  //
+
+  G4Point3D currentdir = fVP.GetViewpointDirection().unit();
+
+  const G4Point3D focalpoint = cameraPosition +
+    cameraDistance * currentdir;
+
+  // set new orientation
+  float rotation[4];
+  float quatRotationX[4];
+
+  // from SbRotation::setValue(axis,radians)
+  this->quatRotationX[3] = (float)cos(radians/2);
+  const float sineval = (float)sin(radians/2);
+  G4Point3D a = axis;
+  // we test for a null vector above
+  (void) a.normalize();
+  this->quatRotationX[0] = a[0] * sineval;
+  this->quatRotationX[1] = a[1] * sineval;
+  this->quatRotationX[2] = a[2] * sineval;
+  
+  // from SbRotation operator *
+currentdir
+  float currentdir[0], currentdir[1], currentdir[2], currentdir[3];
+  q.getValue(currentdir[0], currentdir[1], currentdir[2], currentdir[3]);
+
+  this->setValue(currentdir[3]*quatRotationX[0] + currentdir[0]*quatRotationX[3] + currentdir[1]*quatRotationX[2] - currentdir[2]*quatRotationX[1],
+                 currentdir[3]*quatRotationX[1] - currentdir[0]*quatRotationX[2] + currentdir[1]*quatRotationX[3] + currentdir[2]*quatRotationX[0],
+                 currentdir[3]*quatRotationX[2] + currentdir[0]*quatRotationX[1] - currentdir[1]*quatRotationX[0] + currentdir[2]*quatRotationX[3],
+                 currentdir[3]*quatRotationX[3] - currentdir[0]*quatRotationX[0] - currentdir[1]*quatRotationX[1] - currentdir[2]*quatRotationX[2]);
+
+
+  //  cam->orientation = SbRotation(aroundaxis, delta) * currentorientation;
+
+  SbVec3f newdir;
+  cam->orientation.getValue().multVec(DEFAULTDIRECTION, newdir);
+  cam->position = focalpoint - cam->focalDistance.getValue() * newdir;
+
+  // __________________________
+  // END From Inventor
+  // --------------------------
+  */
+}
+
+/*
+float[4] G4OpenGLQtViewer::makeRotationMatrix(const G4Point3D axis, const float radians)
+{
+  float res[4];
+  // From <http://www.automation.hut.fi/~jaro/thesis/hyper/node9.html>.
+
+  res[3] = (float)cos(radians/2);
+
+  const float sineval = (float)sin(radians/2);
+  SbVec3f a = axis;
+  // we test for a null vector above
+  (void) a.normalize();
+  res[0] = a[0] * sineval;
+  res[1] = a[1] * sineval;
+  res[2] = a[2] * sineval;
+
+}
+  */
 
 /** This is the benning of a rescale function. It does nothing for the moment
