|
我自己用MFC做了个OSG的程序,想实现osganimationeasemotion例子中的菜单效果。但是做出来后,就是响应不了单击。鼠标移动可以,但是单击之后,鼠标移动也不响应了。
调试了一下,好像是没有计算到交点,但是没弄清楚原因,不知道那里不对,还请高手指教
关键代码如下,ColorLabelMenu类就是从例子osganimationeasemotion中拷过来的
- // 局部变量存放窗口矩形
- RECT rect;
- // 创建一个viewer
- mViewer = new osgViewer::Viewer();
- // 得到当前窗口矩形
- ::GetClientRect(m_hWnd, &rect);
- // 初始化
- osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
- // 初始化窗口变量,为OSG所用
- osg::ref_ptr<osg::Referenced> windata = new osgViewer::GraphicsWindowWin32::WindowData(m_hWnd);
- // 设置一些个参数
- traits->x = 0;
- traits->y = 0;
- traits->width = rect.right - rect.left;
- traits->height = rect.bottom - rect.top-30;
- traits->windowDecoration = false;
- traits->doubleBuffer = true;
- traits->sharedContext = 0;
- traits->setInheritedWindowPixelFormat = true;
- traits->inheritedWindowData = windata;
-
- // 创建图形上下文
- osg::GraphicsContext* gc = osg::GraphicsContext::createGraphicsContext(traits.get());
- // 初始化一个相机
- osg::ref_ptr<osg::Camera> camera = new osg::Camera;
- // 绑
- camera->setGraphicsContext(gc);
- //相机视口设置
- camera->setViewport(new osg::Viewport(traits->x, traits->y, traits->width, traits->height));
- // 添加相机到VIEWER
- mViewer->addSlave(camera.get());
- // 添加操作器到VIEWER
- mViewer->setCameraManipulator(keyswitchManipulator.get());
- mViewer->addEventHandler(new osgViewer::StatsHandler);
- //旋转视图
- osg::ref_ptr<osg::MatrixTransform > mt(new osg::MatrixTransform);
- mt->setMatrix(osg::Matrix::rotate(osg::DegreesToRadians(-20.0),osg::Vec3(0,0,1))*osg::Matrix::rotate(osg::DegreesToRadians(20.0),osg::Vec3(1,0,0)));
- mt->addChild(mRoot);
-
- osgUtil::Optimizer optimzer;
- optimzer.optimize(mt);
- mViewer->addEventHandler(new GeomPickHandler(mViewer));
- //创建windowsManager对象
- osgWidget::WindowManager* wm = new osgWidget::WindowManager(mViewer,rect.right-rect.left,rect.bottom-rect.top-30,0xF0000000);
- osgWidget::Window* menu = new osgWidget::Box("menu", osgWidget::Box::HORIZONTAL);
- //增加菜单
- menu->addWidget(new ColorLabelMenu("Choose EaseMotion"));
- menu->getBackground()->setColor(1.0f, 1.0f, 1.0f, 1.0f);
- menu->setPosition(15.0f, 35.0f, 0.0f);
- wm->addChild(menu);
- //设置相机
- osg::Camera * camera2 = wm->createParentOrthoCamera();
- osg::Group * group = new osg::Group();
- group->addChild(camera2);
- group->addChild(mt);
- mViewer->addEventHandler(new osgWidget::MouseHandler(wm));
- wm->resizeAllWindows();
- mViewer->setSceneData(group);
- mViewer->realize();
复制代码 |
|