Your code is simply taking the results of the GUIEventAdapter's mouse position conversion from the window system to osgGA. Since the mouse moves in integer increments, I'm not surprised that you're getting confusing results.

A knowledge of how OpenGL coordinate systems work would save you a lot of time. OSG just uses OpenGL under the hood, and OpenGL's window system coordinates are well-defined and documented.

For starters, they are mathematically floating point. This means the lower-left corner pixel covers a mathematical area ranging from 0.0,0.0 at the lower-left corner of that pixel, to 1.0,1.0 at its upper-right corner.

Likewise (in your 640,480 case), the upper-right pixel also covers an area, from 639.0,479.0 at its lower-left corner to 640.0,480.0 at its upper-right corner.

Hope that helps,
   -Paul


On 2/17/2012 2:09 AM, Jen Hunter wrote:
Dear community,

I use the OpenCV Library to collect feature points in an image and I need to do 
intersection testing through the 2D-coordinates of these feature points with 
osg.

The origin of the opencv image data structure is top left. The topmost pixel on 
the left side is 0,0 and the bottommost on the right side is 639, 479 for 
images with the resolution 640x480.

OpenSceneGraph has its origin at the bottom left.  That's no problem so far. If 
the origin of Osg was at 0,0 bottom left I would just need to use (x, (height - 
1) - y) to get a conversion.

But is the Origin of the visible osgviewer at 0,0? because when I use a 
PickHandler i get at each corner:

Bottom left: 0, 1
Bottom right: 639, 1
Top left: 0, 480
Top right: 639, 480

So, is the origin really at 0,1? I thought it was strange that x and y 
coordinate origin are different. If it was like that I would need to use (x, 
height - y) .

This is the code I used to check the coordinates. Is there something I missed 
or are my thoughts correct?


Code:

#include<windows.h>
#include<iostream>
#include<osgViewer/Viewer>
#include<osgViewer/ViewerEventHandlers>
#include<osgGA/TrackballManipulator>

class PickHandler : public osgGA::GUIEventHandler {

public:
        PickHandler(){}
     ~PickHandler() {}
     bool handle(const osgGA::GUIEventAdapter&  ea,osgGA::GUIActionAdapter&  
aa);
     virtual void pick(osgViewer::View* view, const osgGA::GUIEventAdapter&  
ea);

};

bool PickHandler::handle(const osgGA::GUIEventAdapter&  
ea,osgGA::GUIActionAdapter&  aa){

        switch(ea.getEventType())
     {
        case(osgGA::GUIEventAdapter::PUSH):
                {
                        osgViewer::View* view = 
dynamic_cast<osgViewer::View*>(&aa);
             if (view) pick(view,ea);
                        return false;
         }
        default:
                return false;
     }
}

void PickHandler::pick(osgViewer::View* view, const osgGA::GUIEventAdapter&  
ea){
     float x = ea.getX();
     float y = ea.getY();
        std::cout<<"X:"<<x<<"  Y:"<<y<<std::endl;
}

int main( int argc, char **argv ){
        
        osg::ref_ptr<osg::Group>  root = new osg::Group;
        osg::ref_ptr<osgViewer::Viewer>  viewer = new osgViewer::Viewer();
        viewer->setSceneData(root.get());
        viewer->addEventHandler(new osgViewer::StatsHandler);
        viewer->setCameraManipulator(new osgGA::TrackballManipulator);
        viewer->addEventHandler(new PickHandler());
        viewer->setUpViewInWindow(100, 50, 640, 480);
        viewer->run();
}




Thank you!
Dakota

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





_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to