[osg-users] Can't seem to compute world to screen coordinates, please help

Morné Pistorius mpistorius.osg at googlemail.com
Mon Sep 8 04:05:14 PDT 2008

Hi OSGers,

I am having trouble computing a node's position in screen coordinates.
 I went through the archives and got the proper transform sequence (I
think), but the coordinates returned are wrong.

Attached is a screenshot of what I am trying to do:
The bands around the model are use to rotate the model around the axis
at the center of the band, using the glowing editor widgets.  I am
trying to compute the angle between the mouse position from the center
of the band, and the zero vector from the center to the band.

This is the code snippet that I use to compute the angle:

void VOSGRTSEditor_R::OnDrag( osgViewer::Viewer * i_pViewer, float
i_X, float i_Y )
  // Compute m_Center in screen coordinates
//  if ( xForm.isIdentity() )
    osg::Matrix & world = getWorldMatrices( i_pViewer->getSceneData() )[0];
    osg::Matrix & view = i_pViewer->getCamera()->getViewMatrix();
    osg::Matrix & proj = i_pViewer->getCamera()->getProjectionMatrix();
    osg::Matrix wind =
    xForm = world * view * proj * wind;

  // Compute angle between center and mouse pos
  osg::Vec3 ScreenZeroVec = m_Center * xForm - ( m_Center + m_Axis *
m_Radius ) * xForm;
  osg::Vec3 MouseToCenterVec = m_Center * xForm - osg::Vec3( i_X, i_Y, 0 );
  m_Angle = acos( ( ScreenZeroVec * MouseToCenterVec ) / (
ScreenZeroVec.length() * MouseToCenterVec.length() ) );


For debugging, I also draw these two lines (4 vertices):
m_Center * xForm
( m_Center + m_Axis * m_Radius ) * xForm
m_Center * xForm
osg::Vec3( i_X, i_Y, 0 )

As shown in the screenshot, the point where the two vectors meet
(m_Center * xForm) is not in the center of the band, and changes as I
move the mouse (this is computed in a MOUSE_DRAG event).

I tried using getWorldMatrices() without any stop_at_node argument,
but it's still wrong. The node I call this from should only have one
parent path.  Can anyone see where I am going wrong?  I would really
appreciate any help here, I'm stumped.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: RTS editor.jpg
Type: image/jpeg
Size: 95659 bytes
Desc: not available
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20080908/76a6a2a8/attachment-0002.jpg>

More information about the osg-users mailing list