I have a problem.
I Have a OpenGl project and i want to set the camera matrix of the OpenGl into a OpenSceneGraph camera to have the same view.
I've this code to get OpenGl camera :
GLdouble camera_pos[3];
double rationZoom = // RatioZoom
int* iViewport = // the ViewPort
double* projetMatrix = // Projection Matrix
double* modelMatrix = // ModelView matrix
// screenCenter is the position of my model.
double screenCenterX = // The center in x Axis
double screenCenterY = // The center in y Axis
gluUnProject((iViewport[2] - iViewport[0]) / 2, (iViewport[3] - iViewport[1]) / 2,
0.0, modelMatrix, projetMatrix, iViewport, &camera_pos[0], &camera_pos[1], &camera_pos[2]);`
//Camera_pos is the position X,Y,Z of my camera.
And in OpenSceneGraph i make this code to set the camera with eye/center/up to LookAt of the camera (to have the same view as OpenGl) :
// i use a zoom ration to have the same distance.
double X = ((camera_pos[0]/2) - ((screenCenterX)))/rationZoom;
double Y = ((camera_pos[1]/2) - ((screenCenterY)))/rationZoom;
double Z = ((camera_pos[2]/2)) / rationZoom;
osg::Vec3 updateCameraPosition(X, Y, Z);
osg::Matrix matrixCameraPosition;
matrixCameraPosition.setTrans(updateCameraPosition);
// Yes, for the center i invert the position matrix of the camera
osg::Matrix matrixCameraCenter;
matrixCameraCenter.invert(matrixCameraPosition);
osg::Vec3f eye(matrixCameraPosition.getTrans());
osg::Vec3f up(0, 0, 1);
osg::Vec3f centre = osg::Vec3f(matrixCameraCenter.getTrans().x(),
matrixCameraCenter.getTrans().y(),
matrixCameraCenter.getTrans().z());
// And a set the view into the camera
nodeCamera->setViewMatrixAsLookAt(eye, centre, up);
For the initialisation of the position i've no problem but if i panning the model of the OpenGl project i don't have the same view.
If I'm not mistaken for OpenGl the coordinate system is : X-left, Y-up and Z-backward, and for OpenSceneGraph this is : X-left, Y-backward, Z-up.
Maybe this is the problem and i have to set the Y up instead of Z in OpenSceneGraph ?