Hallo Martin,
After I saw your mail I put up a little example program which does what you 
want (i think).
Basically it prints the screen space coordinates of the center of the 3d 
sphere, which is in world space (0,0,0).
Use the normal osgViewer mouse controls to move the sphere model around.
Just copy the code and look at the print outs. I used a modified example from 
osghelp.com

Enjoy
Dimi



#include <osgViewer/Viewer>
#include <osgGA/GUIEventHandler>
#include <osg/ShapeDrawable>
#include <osg/PositionAttitudeTransform>
#include <osg/LineSegment>
#include <osgUtil/IntersectVisitor>
#include<stdio.h>

class CInputHandler : public osgGA::GUIEventHandler 
{
public:
    CInputHandler( osg::PositionAttitudeTransform* pPatSphere, osg::Node* 
pGraph )
    {
        m_rPatSphere    = pPatSphere;
        m_rGraph        = pGraph;
    }

    virtual bool handle( const osgGA::GUIEventAdapter& ea, 
osgGA::GUIActionAdapter& aa, osg::Object* pObject, osg::NodeVisitor* 
pNodeVisitor )
    {
        osgViewer::Viewer* pViewer = dynamic_cast<osgViewer::Viewer*>(&aa);
        if ( !pViewer )
        {
            return false;
        }
        
            // normalized mouse position
            float x = ea.getXnormalized();
            float y = ea.getYnormalized();

            osg::Matrixd proj = pViewer->getCamera()->getProjectionMatrix();

            osg::Viewport* viewport = pViewer->getCamera()->getViewport();
            double mx = viewport->x() + (int)((double 
)viewport->width()*(x*0.5+0.5));
            double my = viewport->y() + (int)((double 
)viewport->height()*(y*0.5+0.5));

            osg::Matrix MVPW = pViewer->getCamera()->getViewMatrix() * proj;
            MVPW.postMult( 
pViewer->getCamera()->getViewport()->computeWindowMatrix() );

        
            osg::Vec3 nearPoint = osg::Vec3(0.f,0.f,0.0f) * MVPW;
            printf("Screen Space: %f %f\n", nearPoint[0], nearPoint[1]);
        

        return false;
    }

private:
    osg::ref_ptr<osg::PositionAttitudeTransform>    m_rPatSphere;
    osg::ref_ptr<osg::Node>                         m_rGraph;
};

osg::Node* CreateScene( osgViewer::Viewer* pViewer )
{
    // create a group to contain our floor and sphere
    osg::Group* pGroup = new osg::Group;

    // create floor
    osg::Geode* pGeodeFloor = new osg::Geode;
    pGeodeFloor->addDrawable( new osg::ShapeDrawable( new 
osg::Box(osg::Vec3(0.0f,0.0f,-0.5f),100.0f, 100.0f, 1.0f) ) );
    pGroup->addChild( pGeodeFloor );

    // create sphere
    osg::Geode* pGeodeSphere = new osg::Geode;
    pGeodeSphere->addDrawable( new osg::ShapeDrawable( new 
osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),5.0f) ) );

    // create transform to move sphere
    osg::PositionAttitudeTransform* pPat = new osg::PositionAttitudeTransform;
    pPat->addChild( pGeodeSphere );
    pGroup->addChild( pPat );

    // add the handler to the viewer
    pViewer->addEventHandler( new CInputHandler(pPat, pGeodeFloor ) );

    return pGroup;
}

int main(int argc, char* argv[])
{
    // construct the viewer
    osg::ref_ptr<osgViewer::Viewer> rViewer = new osgViewer::Viewer;

    // make the viewer create a 512x512 window and position it at 32, 32
    rViewer->setUpViewInWindow( 32, 32, 512, 512 );

    // set the scene-graph data the viewer will render
    rViewer->setSceneData( CreateScene( rViewer.get() ) );

    // execute main loop
    return rViewer->run();
}







----- Original Message -----
From: Sebastian Messerschmidt <[email protected]>
To: [email protected]
Cc: Martin Haffner <[email protected]>
Sent: Thursday, December 8, 2011 11:19 AM
Subject: Re: [osg-users] World to Screen Space

Hello Martin,
there are some things I'd check:

1. Transformation order (i'm not quite sure about the osg::Matrix order)
So try    mat * vec instead of vec* mat

2. viewMat, this should take you from object space to view-space if  I'm 
not mistaken.
So if you really have world-space you just need the view-transform here.

Basically I'd setup an simple example and check the matrices. Start with 
a simple translation etc.
As you don't state what exactly is wrong, I guess no one will be able to 
simply solve your problem.

cheers
Sebastian

> Hi,
>
> I want to transform a point from world space to screen space with this code:
> Camera* cam = ...
> Matrix viewMat = cam->getViewMatrix();
> Matrix projMat = cam->getProjectionMatrix();
> Viewport* vp   = cam->getViewport();
> Matrix vpMat   = vp->computeWindowMatrix();    
> Vec3 ls1SS = ls1WSPos * viewMat * projMat * vpMat;
>
> Unfortunately the result vector ls1SS is not correct. Is my approach 
> generelly right or did I miss something?
>
> Thank you!
>
> Cheers,
> Martin
>
> ------------------
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=44277#44277
>
>
>
>
>
> _______________________________________________
> osg-users mailing list
> [email protected]
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to