|
本帖最后由 xuliezjq 于 2010-4-1 00:24 编辑
- void myViewer::Init()
- {
- osg::ref_ptr<osgGA::GUIEventAdapter> initEvent = _eventQueue->createEvent();
- initEvent->setEventType(osgGA::GUIEventAdapter::FRAME);
- if (_cameraManipulator.valid())
- {
- _cameraManipulator->init(*initEvent, *this);
- }
- }
- myViewer::~myViewer(void)
- {
- }
- void myViewer::addViewport(osg::Node* rootnode,float x /* = 0.0 */, float y /* = 0.0 */, float width /* = 1.0 */, float
- height /* = 1.0 */)
- {
- osgUtil::SceneView *sceneView = new osgUtil::SceneView(_displaySettings.get());
- //sceneView->setDefaults();
- sceneView->setSceneData(rootnode);
- if (_cameraManipulator.valid())
- {
- _cameraManipulator->setNode(rootnode);
- osg::ref_ptr<osgGA::GUIEventAdapter> ea = _eventQueue->createEvent();
- _cameraManipulator->home(*ea,*this);
- }
- sceneView->getCamera()->addChild(rootnode);
- return addViewport( sceneView, x, y, width, height );
- }
- void myViewer::addViewport(osgUtil::SceneView* sv, float x /* = 0.0 */, float y /* = 0.0 */, float width /* = 1.0 */, float
- height /* = 1.0 */)
- {
- _signalViewport.sceneView = sv;
- _signalViewport.viewport[0] = x;
- _signalViewport.viewport[1] = y;
- _signalViewport.viewport[2] = width;
- _signalViewport.viewport[3] = height;
- }
- void myViewer::updateSceneView(osgUtil::SceneView* sceneView)
- {
- osg::Camera* masterCamera = _signalViewport.sceneView->getCamera();
- osg::StateSet* stateset = masterCamera->getOrCreateStateSet();
- if (sceneView->getGlobalStateSet()!=stateset)
- {
- sceneView->setGlobalStateSet(stateset);
- }
- osg:isplaySettings* ds = osg:isplaySettings::instance();
- sceneView->setDisplaySettings(ds);
- }
- void myViewer::signalupdate()
- {
- _updateVisitor->reset();
- _updateVisitor->setFrameStamp(getFrameStamp());
- _updateVisitor->setTraversalNumber(getFrameStamp()->getFrameNumber());
- getSceneView()->update();
- if (_cameraManipulator.valid())
- {
- getSceneView()->getCamera()->setViewMatrix(_cameraManipulator->getInverseMatrix());
- }
- }
- void myViewer::signalcull()
- {
- // do cull traversal.
- updateSceneView(getSceneView());
- getSceneView()->cull();
- }
- void myViewer::signaldraw()
- {
- // do draw traversal.
- getSceneView()->draw();
- }
- void myViewer::setSceneData(osg::Node* node)
- {
- if (node == getSceneView()->getSceneData())
- return;
- getSceneView()->setSceneData(node);
- if (_cameraManipulator.valid())
- {
- _cameraManipulator->setNode(node);
- osg::ref_ptr<osgGA::GUIEventAdapter> ea = new osgGA::GUIEventAdapter;
- _cameraManipulator->home(*ea,*this);
- }
- getSceneView()->getCamera()->addChild(node);
- }
- void myViewer:pen()
- {
- osgUtil::SceneView* sceneView = _signalViewport.sceneView.get();
- int view[4] = { int(_signalViewport.viewport[0]*_ww), int(_signalViewport.viewport[1]*_wh),
- int(_signalViewport.viewport[2]*_ww), int(_signalViewport.viewport[3]*_wh) };
- sceneView->getCamera()->setClearMask(GL_DEPTH_BUFFER_BIT);
- sceneView->setViewport(view[0], view[1], view[2], view[3]);
- const osg:isplaySettings* vs = sceneView->getDisplaySettings();
- if (vs)
- {
- _displaySettings->merge(*vs);
- }
- else
- {
- // one does not already exist so attach the viewers visualsSettins to the SceneView
- sceneView->setDisplaySettings(_displaySettings.get());
- }
- osgUtil:isplayRequirementsVisitor drv;
- drv.setDisplaySettings(_displaySettings.get());
- osg::Node* node = sceneView->getSceneData();
- if (node) node->accept(drv);
- osg::StateSet* stateset = sceneView->getCamera()->getOrCreateStateSet();
- sceneView->setGlobalStateSet(stateset);
- sceneView->setDefaults();
- }
- void myViewer::signalrun()
- {
- if (_firstFrame)
- {
- Init();
- _firstFrame = false;
- }
- eventTraversal();
- signalupdate();
- signalcull();
- signaldraw();
- }
- void myViewer::addMatrixManipulator(osgGA::MatrixManipulator *manipulator)
- {
- _cameraManipulator = manipulator;
- if (_cameraManipulator.valid())
- {
- if (getSceneView()->getSceneData())
- {
- _cameraManipulator->setNode(getSceneView()->getSceneData());
- }
- osg::ref_ptr<osgGA::GUIEventAdapter> dummyEvent = _eventQueue->createEvent();
- _cameraManipulator->home(*dummyEvent,*this);
- }
- }
- void myViewer::prependEventHandler(osgGA::GUIEventHandler* handler,unsigned int viewport)
- {
- ViewportDef &viewp = _signalViewport;
- viewp._eventHandlerList.push_front( handler );
- }
- void myViewer::appendEventHandler(osgGA::GUIEventHandler* handler,unsigned int viewport)
- {
- ViewportDef &viewp = _signalViewport;
- viewp._eventHandlerList.push_back( handler );
- }
- void myViewer::resized(int x, int y, int width, int height)
- {
- }
- void myViewer::transformMouseXY(float &x, float &y)
- {
- if (_eventQueue->getUseFixedMouseInputRange())
- {
- osgGA::GUIEventAdapter* eventState = _eventQueue->getCurrentEventState();
- x = eventState->getXmin() + (eventState->getXmax()-eventState->getXmin())*x/float(_ww);
- y = eventState->getYmin() + (eventState->getYmax()-eventState->getYmin())*y/float(_wh);
- }
- }
- void myViewer::fullscreen()
- {
- }
- void myViewer::eventTraversal()
- {
- osgGA::EventQueue::Events events;
- _eventQueue->frame(getFrameStamp()->getReferenceTime());
- if(_eventQueue->takeEvents(events))
- {
- ishaveEvents = false;
- return;
- }
- if (_eventVisitor.valid() && getSceneView()->getSceneData())
- {
- _eventVisitor->setFrameStamp(getFrameStamp());
- _eventVisitor->setTraversalNumber(getFrameStamp()->getFrameNumber());
- for (osgGA::EventQueue::Events::iterator itr = events.begin();
- itr != events.end();++itr)
- {
- osgGA::GUIEventAdapter *event = itr->get();
- _eventVisitor->reset();
- _eventVisitor->addEvent(event);
- getSceneView()->getSceneData()->accept(*_eventVisitor);
- osg::NodeVisitor::TraversalMode tm = _eventVisitor->getTraversalMode();
- _eventVisitor->setTraversalMode(osg::NodeVisitor::TRAVERSE_NONE);
- if (getSceneView()->getCamera() &&
- getSceneView()->getCamera()->getEventCallback())
- {
- getSceneView()->getCamera()->accept(*_eventVisitor);
- }
- _eventVisitor->setTraversalMode(tm);
- }
- }
- for(osgGA::EventQueue::Events::iterator itr = events.begin();
- itr != events.end();
- ++itr)
- {
- osgGA::GUIEventAdapter* event = itr->get();
- for(EventHandlerList::iterator hitr = _signalViewport._eventHandlerList.begin();
- hitr != _signalViewport._eventHandlerList.end();
- ++hitr)
- {
- (*hitr)->handle(*event,*this);
- }
- }
- for(osgGA::EventQueue::Events::iterator itr = events.begin();
- itr != events.end();
- ++itr)
- {
- osgGA::GUIEventAdapter* event = itr->get();
- if (_cameraManipulator.valid())
- {
- //_cameraManipulator->handleWithCheckAgainstIgnoreHandledEventsMask( *event, *this);
- _cameraManipulator->handle(*event,*this);
- }
- }
- }
- void myViewer::mouseMove(float x, float y)
- {
- RECT rect;
- ::GetWindowRect(_windowHandle,&rect);
- InvalidateRect(_windowHandle,&rect,true);
- osg::Camera* camera = getSceneView()->getCamera();
- osg::Viewport* viewport = camera->getViewport();
- //
- camera->getProjectionMatrix() *= osg::Matrix::rotate(10.0,0.0,0.0,1.0);
- //camera->getProjectionMatrix() *= osg::Matrix::scale(1.0/4.0,1.0,1.0);
- camera->setViewport(new osg::Viewport(0, 0, 1024, 768));
- UpdateWindow(_windowHandle);
- }
- LRESULT myViewer::handleNativeWindowingEvent( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
- {
- double eventTime = getEventQueue()->getTime();
- switch (uMsg)
- {
- case WM_LBUTTONDOWN:
- {
- ::SetCapture(_windowHandle);
- int button;
- button = 1;
- float mx = GET_X_LPARAM(lParam);
- float my = GET_Y_LPARAM(lParam);
- transformMouseXY(mx, my);
- getEventQueue()->mouseButtonPress(mx, my, button, eventTime);
- //mouseMove(mx,my);
- }
- break;
- case WM_LBUTTONUP:
- {
- ::ReleaseCapture();
- int button;
- button = 1;
- float mx = GET_X_LPARAM(lParam);
- float my = GET_Y_LPARAM(lParam);
- transformMouseXY(mx,my);
- getEventQueue()->mouseButtonRelease(mx,my,button,eventTime);
- }
- break;
- }
- return ::CallWindowProc(_windowProcedure, hwnd, uMsg, wParam, lParam);
- }
复制代码 这是我源码的主要部分,但是我在应用程序中
addMatrixManipulator(new osgGA::TrackballManipulator)后,漫游器不响应
但是当我用例如源码中的mouseMove(float , float)函数那样操作sceneView的相机后,确能响应。
求高人指点。。。。。 |
|