5

抱歉,如果这是重复的,但我正在尝试找出 Open Inventor 中 SoRayPickAction 的实现。我正在尝试实现它,以便在单击鼠标时选择一个特定节点,以便我可以平移、旋转等。我有三个节点:桌子、灯和框架(相框)。但是,我不认为我的代码完全正确。我也有各种方法,例如 MouseButtonCallback(它将检查鼠标是否被单击,然后使用导航器)和 MouseMoveCallback(相同的想法)。所以这是我的代码,但你有什么建议吗?现在,好吧,它什么也没做。

SbViewportRegion viewport(400,300); 
    SoRayPickAction m(viewport); 
    m.setRay(SbVec3f(0.0,0.0,0.0), SbVec3f(0.0,0.0,-1.0));
    m.apply(callback_node);
    const SoPickedPoint *mpp = m.getPickedPoint(); 
    if(mpp != NULL) {
        std::cout << "test" << std::endl;
    }

您是否还知道 OpenInventor 中可以在场景中“放置”节点的动作,即将台灯放置在桌面上,将框架放置在墙上等。是否带有路径?不幸的是,我什至不知道我在寻找什么。非常感谢你的帮助!!

编辑:这看起来怎么样?SoSeparator *desk2; SoSeparator *lamp2; SoSeparator *pic_frame2; SoSeparator *选中;

void MouseButtonCallback(void* data, SoEventCallback* node)
{
   SoHandleEventAction* action = node->getAction();
   const SoMouseButtonEvent* event = static_cast<const SoMouseButtonEvent*>(action-          >getEvent());
   Navigator* nav = static_cast<Navigator*>(data);

   if (SoMouseButtonEvent::isButtonPressEvent(event, event->getButton())) 
       nav->OnMouseDown(event, action);
   else
         nav->OnMouseUp(event, action);

   const SbViewportRegion & viewportRegion = action->getViewportRegion();
   SoRayPickAction pickAction(viewportRegion); 

   SbVec2s mousePos = event->getPosition(viewportRegion); 
   pickAction.setPoint(mousePos); 
   pickAction.setPickAll(TRUE); 
   pickAction.setRadius(2.0F); 
   pickAction.setRay(SbVec3f(0.0,0.0,0.0), SbVec3f(0.0,0.0,-1.0));
   pickAction.apply(node);
   const SoPickedPoint *mpp = pickAction.getPickedPoint(); 
   if(mpp != NULL) {
  SoPath *path = mpp->getPath(); 

 if(desk2 != NULL && path->containsNode(desk2))
  {             //but this doesn't respond with cout when I try to test it :(
      if (SoMouseButtonEvent::isButtonPressEvent(event, event->getButton()))
      *picked = *desk2; 
  }
  else if(lamp2 != NULL && path->containsNode(lamp2))
  {
      if (SoMouseButtonEvent::isButtonPressEvent(event, event->getButton()))
      *picked = *lamp2;
  }
  else if(pic_frame2 != NULL && path->containsNode(pic_frame2))
  {
      if (SoMouseButtonEvent::isButtonPressEvent(event, event->getButton())) 
      *picked = *pic_frame2;
  }

           action->setHandled();
   }
 void MouseMoveCallback(void* data, SoEventCallback* node)
{
    SoHandleEventAction* action = node->getAction();
    const SoLocation2Event* event = static_cast<const SoLocation2Event*>(action->getEvent());
    Navigator* nav = static_cast<Navigator*>(data);

    nav->OnMouseMove(event, action);

     const SbViewportRegion & viewportRegion = action->getViewportRegion();
    SoRayPickAction pickAction(viewportRegion); 

    SbVec2s mousePos = event->getPosition(viewportRegion); 
    pickAction.setPoint(mousePos); 
    pickAction.setPickAll(TRUE); 
    pickAction.setRadius(2.0F); 
    pickAction.setRay(SbVec3f(0.0,0.0,0.0), SbVec3f(0.0,0.0,-1.0));
    pickAction.apply(node);
    const SoPickedPoint *mpp = pickAction.getPickedPoint(); 
    if(mpp != NULL) {
  SoPath *path = mpp->getPath(); 
  if(desk2 != NULL && path->containsNode(desk2))
  {
      *picked = *desk2; //can't remember how to set pointers, will figure that out
  }
  else if(lamp2 != NULL && path->containsNode(lamp2))
  {
      *picked = *lamp2;
  }
  else if(pic_frame2 != NULL && path->containsNode(pic_frame2))
  {
       *picked = *pic_frame2;
  }
   }  
        action->setHandled();
    }

  (part of main method) 

 //desk 
SoTransform *desk_transform = new SoTransform; 
desk_transform->translation.setValue(SbVec3f(380,340,330)); 
SoSeparator* desk2 = new SoSeparator(); 
desk2->addChild(desk_transform);
desk2->addChild(desk); 
root->addChild(desk2);

  SoTransformerManip* picked_transform = new SoTransformerManip(); 
   picked_transform->translation.setValue(SbVec3f(200,340,330));
  SoSeparator* pick2 = new SoSeparator(); 
  pick2->addChild(picked_transform); 
  pick2->addChild(picked); 
  root->addChild(pick2); 

 std::auto_ptr<btCollisionShape> picked_shape(new btBoxShape(btVector3(10.0f, 10.0f,  10.0f)));
  CollisionEngine* picked_collision = new CollisionEngine(collision_world.get(),      picked_shape.get());
  picked_collision->translation_in.connectFrom(&picked_transform->translation);
  picked_collision->rotation_in.connectFrom(&picked_transform->rotation);
  picked_transform->translation.connectFrom(&picked_collision->translation_out);
4

1 回答 1

2

你有选择的点。然后你得到一个SoPath你猜到的。然后查看路径中是否包含您想要使用的节点。

SbViewportRegion viewport(400,300); 
    SoRayPickAction m(viewport); 
    m.setRay(SbVec3f(0.0,0.0,0.0), SbVec3f(0.0,0.0,-1.0));
    m.apply(callback_node);
    const SoPickedPoint *mpp = m.getPickedPoint(); 
    if(mpp != NULL) {
        std::cout << "test" << std::endl;
        SoPath * path = pickedPoint->getPath();

        if (deskSeparator != NULL && path->containsNode(deskSeparator)
        {
        }
        else if (lampSeparator != NULL && path->containsNode(lampSeparator)
        {
        }
        else if (radomeSeparator != NULL && path->containsNode(radomeSeparator)
        {
            if (    SoMouseButtonEvent::isButtonPressEvent( event, SoMouseButtonEvent::BUTTON2 )
                 || ( SoMouseButtonEvent::isButtonPressEvent( event, SoMouseButtonEvent::BUTTON1 ) && event->wasShiftDown() ) )
            {
                modelPointMoving = true;
                const SoDetail * detail = modelPickedPoint->getDetail( 0 );
                int face = -1;
                if ( detail && detail->isOfType( SoFaceDetail::getClassTypeId() ) )
                {
                    const SoFaceDetail * faceDetail = static_cast<const SoFaceDetail *>( detail );
                    face = faceDetail->getFaceIndex();
                }
                updateStatusBar( face, point.getValue(), normal.getValue() );
                graphicModel.postNote( pickedPoint );
                break;
            }
            else if ( SoMouseButtonEvent::isButtonPressEvent( event, SoMouseButtonEvent::BUTTON1 ) )
            {
            }
            else if ( SoMouseButtonEvent::isButtonReleaseEvent( event, SoMouseButtonEvent::BUTTON1 ) )
            {
            }
        }
    }

您最终会希望将拾取射线连接到鼠标位置,如下所示:

//  Set an 2-pixel wide region around the pixel.
SbVec2s mousePosition = event->getPosition( viewportRegion );
pickAction.setPoint( mousePosition );
pickAction.setPickAll( TRUE );
pickAction.setRadius( 2.0F );

.apply()当然是在您选择操作之前完成的。

我想我的代码是你和我的混合体,但我认为它应该给你一个开始。此外,这是一个处理鼠标事件的函数:

void
RenderWindow::mouseEvent( void *, SoEventCallback * eventCallback )
{
    const SoEvent *event = eventCallback->getEvent();

    if ( ! event )
    {
        qDebug() << "  **  Error in mousePressCallback: Event not found.";
        return;
    }

    //SoType eventType = event->getTypeId();   
    //SbName eventTypeName = eventType.getName();
    //const char * eventTypeString = eventTypeName.getString();
    SoHandleEventAction * action = eventCallback->getAction();
    const SbViewportRegion & viewportRegion = action->getViewportRegion();
    SoRayPickAction pickAction( viewportRegion );

在主程序或设置例程中,我注册了鼠标事件(对于单击操作和位置(将鼠标移动到视口上):

//  Add a mouse event callback to catch mouse button presses.
SoEventCallback * mouseEventCallback = new SoEventCallback();
mouseEventCallback->setName( "MOUSE_EVENT_CALLBACK" );
mouseEventCallback->addEventCallback( SoMouseButtonEvent::getClassTypeId(), &::mouseEvent, static_cast<void *>( this ) );
//  Add a mouse event callback to catch mouse motion.
mouseEventCallback->addEventCallback( SoLocation2Event::getClassTypeId(), &::mouseEvent, static_cast<void *>( this ) );
rootSeparator->addChild( mouseEventCallback );

现在我看着它,我以相反的顺序写了这些块;-)。对不起。

祝你好运

于 2012-05-16T03:45:02.493 回答