查看: 1382|回复: 3

求教:漫游器问题

[复制链接]

该用户从未签到

发表于 2010-4-1 00:22:37 | 显示全部楼层 |阅读模式
本帖最后由 xuliezjq 于 2010-4-1 00:24 编辑
  1. void myViewer::Init()
  2. {
  3. osg::ref_ptr<osgGA::GUIEventAdapter> initEvent = _eventQueue->createEvent();
  4. initEvent->setEventType(osgGA::GUIEventAdapter::FRAME);

  5. if (_cameraManipulator.valid())
  6. {
  7. _cameraManipulator->init(*initEvent, *this);
  8. }
  9. }

  10. myViewer::~myViewer(void)
  11. {
  12. }


  13. void myViewer::addViewport(osg::Node* rootnode,float x /* = 0.0 */, float y /* = 0.0 */, float width /* = 1.0 */, float

  14. height /* = 1.0 */)
  15. {
  16. osgUtil::SceneView *sceneView = new osgUtil::SceneView(_displaySettings.get());
  17. //sceneView->setDefaults();
  18. sceneView->setSceneData(rootnode);
  19. if (_cameraManipulator.valid())
  20. {
  21. _cameraManipulator->setNode(rootnode);
  22. osg::ref_ptr<osgGA::GUIEventAdapter> ea = _eventQueue->createEvent();
  23. _cameraManipulator->home(*ea,*this);
  24. }
  25. sceneView->getCamera()->addChild(rootnode);

  26. return addViewport( sceneView, x, y, width, height );
  27. }

  28. void myViewer::addViewport(osgUtil::SceneView* sv, float x /* = 0.0 */, float y /* = 0.0 */, float width /* = 1.0 */, float

  29. height /* = 1.0 */)
  30. {
  31. _signalViewport.sceneView = sv;
  32. _signalViewport.viewport[0] = x;
  33. _signalViewport.viewport[1] = y;
  34. _signalViewport.viewport[2] = width;
  35. _signalViewport.viewport[3] = height;

  36. }


  37. void myViewer::updateSceneView(osgUtil::SceneView* sceneView)
  38. {
  39. osg::Camera* masterCamera = _signalViewport.sceneView->getCamera();
  40. osg::StateSet* stateset = masterCamera->getOrCreateStateSet();

  41. if (sceneView->getGlobalStateSet()!=stateset)
  42. {
  43. sceneView->setGlobalStateSet(stateset);
  44. }

  45. osg:isplaySettings* ds = osg:isplaySettings::instance();
  46. sceneView->setDisplaySettings(ds);
  47. }

  48. void myViewer::signalupdate()
  49. {
  50. _updateVisitor->reset();
  51. _updateVisitor->setFrameStamp(getFrameStamp());
  52. _updateVisitor->setTraversalNumber(getFrameStamp()->getFrameNumber());

  53. getSceneView()->update();

  54. if (_cameraManipulator.valid())
  55. {
  56. getSceneView()->getCamera()->setViewMatrix(_cameraManipulator->getInverseMatrix());
  57. }
  58. }


  59. void myViewer::signalcull()
  60. {
  61. // do cull traversal.
  62. updateSceneView(getSceneView());
  63. getSceneView()->cull();
  64. }


  65. void myViewer::signaldraw()
  66. {

  67. // do draw traversal.
  68. getSceneView()->draw();

  69. }

  70. void myViewer::setSceneData(osg::Node* node)
  71. {
  72. if (node == getSceneView()->getSceneData())
  73. return;
  74. getSceneView()->setSceneData(node);
  75. if (_cameraManipulator.valid())
  76. {
  77. _cameraManipulator->setNode(node);
  78. osg::ref_ptr<osgGA::GUIEventAdapter> ea = new osgGA::GUIEventAdapter;
  79. _cameraManipulator->home(*ea,*this);
  80. }
  81. getSceneView()->getCamera()->addChild(node);
  82. }

  83. void myViewer:pen()
  84. {
  85. osgUtil::SceneView* sceneView = _signalViewport.sceneView.get();
  86. int view[4] = { int(_signalViewport.viewport[0]*_ww), int(_signalViewport.viewport[1]*_wh),
  87. int(_signalViewport.viewport[2]*_ww), int(_signalViewport.viewport[3]*_wh) };

  88. sceneView->getCamera()->setClearMask(GL_DEPTH_BUFFER_BIT);

  89. sceneView->setViewport(view[0], view[1], view[2], view[3]);

  90. const osg:isplaySettings* vs = sceneView->getDisplaySettings();
  91. if (vs)
  92. {
  93. _displaySettings->merge(*vs);
  94. }
  95. else
  96. {
  97. // one does not already exist so attach the viewers visualsSettins to the SceneView
  98. sceneView->setDisplaySettings(_displaySettings.get());
  99. }

  100. osgUtil:isplayRequirementsVisitor drv;
  101. drv.setDisplaySettings(_displaySettings.get());
  102. osg::Node* node = sceneView->getSceneData();
  103. if (node) node->accept(drv);

  104. osg::StateSet* stateset = sceneView->getCamera()->getOrCreateStateSet();

  105. sceneView->setGlobalStateSet(stateset);
  106. sceneView->setDefaults();
  107. }

  108. void myViewer::signalrun()
  109. {
  110. if (_firstFrame)
  111. {
  112. Init();
  113. _firstFrame = false;

  114. }
  115. eventTraversal();
  116. signalupdate();
  117. signalcull();
  118. signaldraw();
  119. }

  120. void myViewer::addMatrixManipulator(osgGA::MatrixManipulator *manipulator)
  121. {
  122. _cameraManipulator = manipulator;
  123. if (_cameraManipulator.valid())
  124. {
  125. if (getSceneView()->getSceneData())
  126. {
  127. _cameraManipulator->setNode(getSceneView()->getSceneData());
  128. }
  129. osg::ref_ptr<osgGA::GUIEventAdapter> dummyEvent = _eventQueue->createEvent();
  130. _cameraManipulator->home(*dummyEvent,*this);
  131. }
  132. }


  133. void myViewer::prependEventHandler(osgGA::GUIEventHandler* handler,unsigned int viewport)
  134. {
  135. ViewportDef &viewp = _signalViewport;
  136. viewp._eventHandlerList.push_front( handler );
  137. }

  138. void myViewer::appendEventHandler(osgGA::GUIEventHandler* handler,unsigned int viewport)
  139. {
  140. ViewportDef &viewp = _signalViewport;
  141. viewp._eventHandlerList.push_back( handler );

  142. }


  143. void myViewer::resized(int x, int y, int width, int height)
  144. {
  145. }


  146. void myViewer::transformMouseXY(float &x, float &y)
  147. {
  148. if (_eventQueue->getUseFixedMouseInputRange())
  149. {
  150. osgGA::GUIEventAdapter* eventState = _eventQueue->getCurrentEventState();
  151. x = eventState->getXmin() + (eventState->getXmax()-eventState->getXmin())*x/float(_ww);
  152. y = eventState->getYmin() + (eventState->getYmax()-eventState->getYmin())*y/float(_wh);
  153. }
  154. }

  155. void myViewer::fullscreen()
  156. {
  157. }


  158. void myViewer::eventTraversal()
  159. {
  160. osgGA::EventQueue::Events events;
  161. _eventQueue->frame(getFrameStamp()->getReferenceTime());
  162. if(_eventQueue->takeEvents(events))
  163. {
  164. ishaveEvents = false;
  165. return;
  166. }

  167. if (_eventVisitor.valid() && getSceneView()->getSceneData())
  168. {
  169. _eventVisitor->setFrameStamp(getFrameStamp());
  170. _eventVisitor->setTraversalNumber(getFrameStamp()->getFrameNumber());

  171. for (osgGA::EventQueue::Events::iterator itr = events.begin();
  172. itr != events.end();++itr)
  173. {
  174. osgGA::GUIEventAdapter *event = itr->get();
  175. _eventVisitor->reset();
  176. _eventVisitor->addEvent(event);

  177. getSceneView()->getSceneData()->accept(*_eventVisitor);
  178. osg::NodeVisitor::TraversalMode tm = _eventVisitor->getTraversalMode();
  179. _eventVisitor->setTraversalMode(osg::NodeVisitor::TRAVERSE_NONE);

  180. if (getSceneView()->getCamera() &&
  181. getSceneView()->getCamera()->getEventCallback())
  182. {
  183. getSceneView()->getCamera()->accept(*_eventVisitor);
  184. }

  185. _eventVisitor->setTraversalMode(tm);
  186. }
  187. }

  188. for(osgGA::EventQueue::Events::iterator itr = events.begin();
  189. itr != events.end();
  190. ++itr)
  191. {
  192. osgGA::GUIEventAdapter* event = itr->get();

  193. for(EventHandlerList::iterator hitr = _signalViewport._eventHandlerList.begin();
  194. hitr != _signalViewport._eventHandlerList.end();
  195. ++hitr)
  196. {
  197. (*hitr)->handle(*event,*this);
  198. }

  199. }

  200. for(osgGA::EventQueue::Events::iterator itr = events.begin();
  201. itr != events.end();
  202. ++itr)
  203. {
  204. osgGA::GUIEventAdapter* event = itr->get();
  205. if (_cameraManipulator.valid())
  206. {
  207. //_cameraManipulator->handleWithCheckAgainstIgnoreHandledEventsMask( *event, *this);
  208. _cameraManipulator->handle(*event,*this);
  209. }
  210. }
  211. }



  212. void myViewer::mouseMove(float x, float y)
  213. {
  214. RECT rect;
  215. ::GetWindowRect(_windowHandle,&rect);
  216. InvalidateRect(_windowHandle,&rect,true);
  217. osg::Camera* camera = getSceneView()->getCamera();
  218. osg::Viewport* viewport = camera->getViewport();
  219. //
  220. camera->getProjectionMatrix() *= osg::Matrix::rotate(10.0,0.0,0.0,1.0);
  221. //camera->getProjectionMatrix() *= osg::Matrix::scale(1.0/4.0,1.0,1.0);
  222. camera->setViewport(new osg::Viewport(0, 0, 1024, 768));

  223. UpdateWindow(_windowHandle);
  224. }


  225. LRESULT myViewer::handleNativeWindowingEvent( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  226. {
  227. double eventTime = getEventQueue()->getTime();
  228. switch (uMsg)
  229. {
  230. case WM_LBUTTONDOWN:
  231. {
  232. ::SetCapture(_windowHandle);
  233. int button;

  234. button = 1;

  235. float mx = GET_X_LPARAM(lParam);
  236. float my = GET_Y_LPARAM(lParam);
  237. transformMouseXY(mx, my);
  238. getEventQueue()->mouseButtonPress(mx, my, button, eventTime);
  239. //mouseMove(mx,my);

  240. }
  241. break;
  242. case WM_LBUTTONUP:

  243. {
  244. ::ReleaseCapture();

  245. int button;
  246. button = 1;
  247. float mx = GET_X_LPARAM(lParam);
  248. float my = GET_Y_LPARAM(lParam);

  249. transformMouseXY(mx,my);
  250. getEventQueue()->mouseButtonRelease(mx,my,button,eventTime);

  251. }

  252. break;
  253. }
  254. return ::CallWindowProc(_windowProcedure, hwnd, uMsg, wParam, lParam);
  255. }
复制代码
这是我源码的主要部分,但是我在应用程序中
addMatrixManipulator(new osgGA::TrackballManipulator)后,漫游器不响应
但是当我用例如源码中的mouseMove(float , float)函数那样操作sceneView的相机后,确能响应。
求高人指点。。。。。

该用户从未签到

发表于 2010-4-1 09:12:53 | 显示全部楼层
if (_cameraManipulator.valid())

{

getSceneView()->getCamera()->setViewMatrix(_cameraManipulator->getInverseMatrix());

}
你这段代码啥意思??你操作器的操作都给抵消了

该用户从未签到

 楼主| 发表于 2010-4-1 22:59:45 | 显示全部楼层
用漫游器的姿态矩阵去更新主相机的视图矩阵。。。。。。
您觉得该怎么改呢?

该用户从未签到

发表于 2010-4-3 09:22:54 | 显示全部楼层
只需要重写Manipulator的getMatrix函数就可以了,它会自动被系统内部调用
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表