[osg-users] Mapping world cordinates to screen cordinates

Suraj Paul suraj at isac.gov.in
Sun Sep 18 02:24:04 PDT 2016


Hi,

I am trying to find out what is the screen cordinates corresponding to the world cordinates of a moving node. 

I read the post: http://forum.openscenegraph.org/viewtopic.php?t=2292
 and referred to OSG beginners guide also.

So, I did the following:-

//set viewport of viewer for screensize 1280X768
viewer.getCamera()->setViewport( new osg::Viewport(0, 0, 1280, 768) );


//Setup the viewer position used to position the camera in simulation loop

osg::Vec3d cam_eye( 0., -50.0, 0.0);
osg::Vec3d cam_center( 0.,-40.0, 0.0 );
osg::Vec3d cam_up( 0.0, 0.0,1.0 );


//compute the camera matrix using modelview*ProjectionMat*windowMat

osg::Matrix mv =  viewer.getCamera()->getViewMatrix();
osg::Matrix windowMatrix = viewer.getCamera()->getViewport()->computeWindowMatrix();
osg::Matrix projectionMatrix = viewer.getCamera()->getProjectionMatrix();

//get the camera matrix
osg::Matrix cameraMatrix = mv* projectionMatrix * windowMatrix;

//Now make a moving node in the scene that moves in steps of delta in every frame

float delta = 0.;

 while(!viewer.done())
        {
            viewer.getCamera()->setViewMatrixAsLookAt( cam_eye, cam_center, cam_up );
            viewer.getCamera()->setProjectionMatrixAsPerspective(80, 1280.0/768.0, 0.1f, 80.0f);

//I translate the model in x -direction everyframe and get its world cordinate
            trans->setMatrix( osg::Matrix::translate(osg::Vec3d(delta,0.,0.)));
            osg::Vec3 posInWorld = model->getBound().center() *osg::computeLocalToWorld(model->getParentalNodePaths()[0]);

            //compute the screen coordinates of model
            osg::Vec3d ssc = posInWorld*cameraMatrix;
       

           delta+=2;  //increment the position of model
            viewer.frame();
    }


I have checked that [0 , 0, 0] vector in WCS is mapped to ssc = [0, 0, 0]*cameraMatrix which gives ssc= [640, 384, 0.984]. Neglecting the z-cordinate, it is same as center of the screen.

My question is: Upon printing the 3 dimensional vector 'ssc' (screen-space cordinate),  i am  getting some values of its x-cordinate larger than 1268 (the viewport dimension!). This happens even when the moving 'model' is still well within the screen. 
For example the WCS point [18, 0, 0] gives ssc as = [1348.92, 384, 0.984]

Where i have gone wrong in computing the screen cordinates? Shouldn't the screen cordinates be lying in range 0<x<1268  and 0<y<768 ?

I want to find out when the image of the moving node on the screen moves out of the screen.

Please reply.


Thank you!

Cheers,
Suraj

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=68648#68648








More information about the osg-users mailing list