2

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 ?

Dada
  • 21
  • 5
  • "X-left, Y-up and Z-right" what does it even mean? how X and Z can point in opposite directions? relative to the camera OpenGL has X-right, Y-up, Z-backwards; relative to the world you can set it up any way you like, and I stick to X-east, Y-north, Z-up. I don't know about OpenSceneGraph though. – Yakov Galka Jan 20 '23 at 23:15
  • YES SORRY, my mistake, it's Z- backward not right sorry. On OpenSceneGraph by default the backward is Y and the up is Z but when I try to change the coordinates of the system to have the same axes as on OpenGL the camera ends up with a rotation at 90 degrees. – Dada Jan 21 '23 at 14:30

1 Answers1

0

I have solved my problem.

I don't need to calculate the camera or set the setViewMatrixAsLookAt but just to get the modelview matrix and the projection matrix of OpenGl and set to the OpenScenGraph camera, like this:

double* projetMatrix = // the Projection matrix
double* modelMatrix = // The modelView matrix

osg::Matrixd modelViewMatrix(modelMatrix[0], modelMatrix[1], modelMatrix[2], modelMatrix[3],
    modelMatrix[4], modelMatrix[5], modelMatrix[6], modelMatrix[7],
    modelMatrix[8], modelMatrix[9], modelMatrix[10], modelMatrix[11],
    modelMatrix[12], modelMatrix[13], modelMatrix[14], modelMatrix[15]
);

osg::Matrixd projectionMatrix(projetMatrix[0], projetMatrix[1], projetMatrix[2], projetMatrix[3],
    projetMatrix[4], projetMatrix[5], projetMatrix[6], projetMatrix[7],
    projetMatrix[8], projetMatrix[9], projetMatrix[10], projetMatrix[11],
    projetMatrix[12], projetMatrix[13], projetMatrix[14], projetMatrix[15]
);

And :

camera->setViewMatrix(modelViewMatrix);
camera->setProjectionMatrix(projectionMatrix);
camera->setViewport(new osg::Viewport(iViewport[0], iViewport[1], iViewport[2], iViewport[3]));
Dada
  • 21
  • 5