When I run this code, the objects highlighted are mostly near my mouse
click. But they are weirdly picked(not a simple translation from where
I've clicked).
myViewer::myViewer( QWidget *parent, const char *name)
:myQglViewer(parent,name)
{
myViewer::selection = new SoSelection; // myQglViewer parent class
created a scene
// ->
make myViewer::selection new root for that scene
myViewer::selection->ref();
myViewer::selection->addChild(scene);
sm->setSceneGraph(myViewer::selection);
myViewer::selection->policy = SoSelection::SHIFT;
myViewer::selection->addSelectionCallback(&myViewer::madeSelection,
(void*)1L);
myViewer::selection-
>addDeselectionCallback(&myViewer::madeSelection, (void*)0L);
modelGeometry = new SoGroup;
scene->addChild(modelGeometry);
//fill scene with objects
modelGeometry->addChild(...)
modelGeometry->addChild(...)
...
}
this calls myQglViewer(parent, name) to create a QGLWidget with
members: scene, scenemanager, camera and headlight which looks as
follows:
myQglViewer::myQglViewer(QWidget *parent, const char *name)
: QGLWidget(QGLFormat(QGL::DoubleBuffer | QGL::DepthBuffer |
QGL::AlphaChannel | QGL::DirectRendering) , parent, name),
{
SoDB::init();
vpr = new SbViewportRegion(400, 400);
sm = new SoSceneManager();
scene = new SoSeparator();
scene->ref();
camera = new SoPerspectiveCamera;
scene->addChild(camera);
headlight = new SoPointLight;
scene->addChild(headlight);
sm->setSceneGraph(scene);
SoGLRenderAction *ra = new SoBoxHighlightRenderAction(*vpr);
sm->setGLRenderAction(ra);
}
Now because I'm not using SoQtRenderArea or similar classes I have to
translate QEvent to SoEvent manually
I do it this way:
void myViewer::mousePressEvent(QMouseEvent *e)
{
myQglViewer::mousePressEvent(e); //allows parent viewer to keep
rotating view on mouse interaction
if(e->button() == e->LeftButton)
{
SoMouseButtonEvent *mbev = new SoMouseButtonEvent;
mbev->setButton(SoMouseButtonEvent::BUTTON1);
mbev->setState(SoButtonEvent::DOWN);
mbev->setPosition(SbVec2s(e->x(), vpr->getWindowSize()[1] - e->y()
- 1));
mbev->setAltDown((e->state()&e->AltButton));
mbev->setCtrlDown((e->state()&e->ControlButton));
mbev->setShiftDown((e->state()&e->ShiftButton));
mbev->setTime(SbTime::getTimeOfDay());
cout << "MOUSE POS x = " << e->x() << "; y = " << e->y() << endl;
SoHandleEventAction *heva = new SoHandleEventAction(*vpr);
heva->setEvent(mbev);
heva->apply(myViewer::selection);
}
}
I do the same for mouseReleaseEvent and mouseMoveEvent. mouseMoveEvent
I translate to Location2Event
Your help is very appreciated=) Didn't sleep the whole night to fix
this, but no luck!