|
有两个分别属于两个不同的view的camera,它们共享一个graphics context。如何能使一个键盘事件同时被两个view都捕获?
//Setup scene data for the views
fused->addChild(positionIndicator) ; //Add position indicator to the scene
fused->addChild(viewerData.getTerrainData()) ;
fused->addChild(viewerData.getVTPData()) ;
osgUtil::Optimizer optimizer ;
optimizer.optimize(fused.get()) ; //Optimize the scene, remove rendundent nodes and state etc.
positionView->setSceneData(fused.get()) ;
environmentView->setSceneData(fused.get()) ;
positionReporterView->setSceneData(positionReporter) ; //Set the scene of the reporter view
//Get the number of windows to be created
const int windowNumber = viewerData.getNumEnvironmentWindow() ;
//Create graphics context traits
std::vector<osg::ref_ptr<osg::GraphicsContext::Traits> > environmentContextsTraits ;
for (int i = 0; i < windowNumber; i++)
environmentContextsTraits.push_back(new osg::GraphicsContext::Traits()) ;
//Set traits for environment view contexts
for (size_t i = 0; i < environmentContextsTraits.size(); i++){
std::string temp = "Environment View " ;
char num[256] ;
temp.append(itoa(i, num, 10)) ;
environmentContextsTraits->windowName = temp ;
environmentContextsTraits->windowDecoration = true ;
environmentContextsTraits->doubleBuffer = true ;
environmentContextsTraits->x = i * GRAPHICS_CONTEXT_WIDTH + (i + 1) * GRAPHICS_CONTEXT_LEFT_OFFSET ;
environmentContextsTraits->y = GRAPHICS_CONTEXT_TOP_OFFSET ;
environmentContextsTraits->width = GRAPHICS_CONTEXT_WIDTH ;
environmentContextsTraits->height = GRAPHICS_CONTEXT_HEIGHT ;
}
//Create the graphic contexts
std::vector<osg::ref_ptr<osg::GraphicsContext> > environmentContexts ;
for (int i = 0; i < windowNumber; i++)
environmentContexts.push_back(osg::GraphicsContext::createGraphicsContext(environmentContextsTraits.get())) ;
//Associate the slave cameras of environment view to their graphics contexts
for (int i = 0; i < windowNumber; i++){
const double angleBetweenCameras = (4 < windowNumber)? double(360) / windowNumber: 90.0 ;
osg::Matrix viewOffset ;
double angleToRotate = (i - double(windowNumber - 1) / 2) * angleBetweenCameras ;
viewOffset.makeRotate(angleToRotate / 180 * osg:I, osg::Vec3(0, 1, 0)) ;
osg::Camera *envCamera = new osg::Camera() ;
environmentView->addSlave(envCamera, osg::Matrix(), viewOffset) ;
envCamera->setGraphicsContext(environmentContexts.get()) ;
envCamera->setViewport(new osg::Viewport(0, 0, environmentContextsTraits->width, environmentContextsTraits->height)) ;
GLenum envCameraBuffer = environmentContextsTraits->doubleBuffer? GL_BACK: GL_FRONT ;
envCamera->setDrawBuffer(envCameraBuffer) ;
envCamera->setReadBuffer(envCameraBuffer) ;
envCamera->setDataVariance(osg::Object:YNAMIC) ;
envCamera->setProjectionMatrixAsPerspective(90.0, double(environmentContextsTraits->width) / environmentContextsTraits->height, 0.1, 10000.0) ;
}
if (windowNumber > 0){
//Associate the camera of position view to the first environment view's graphics context
osg::Camera *positionViewCamera = positionView->getCamera() ;
positionViewCamera->setGraphicsContext(environmentContexts[0].get()) ;
positionViewCamera->setViewport(new osg::Viewport(0, 0, environmentContextsTraits[0]->width / 4, environmentContextsTraits[0]->height / 4)) ;
GLenum positionViewCameraBuffer = environmentContextsTraits[0]->doubleBuffer? GL_BACK: GL_FRONT ;
positionViewCamera->setDrawBuffer(positionViewCameraBuffer) ;
positionViewCamera->setReadBuffer(positionViewCameraBuffer) ;
positionViewCamera->setDataVariance(osg::Object::DYNAMIC) ;
positionViewCamera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR) ; //Crucial!!
//The OSG by default computes the near/far planes on the fly, overriding the values set by the projection matrix. This default behaviour
//ensures that a wide range of models are loadable in OSG applications without application programmers having to check the size of the model
//and manually setting the near/far to the range. Must switch this functionality off explicitly to make sure that the culling is performed
//according to the data specified in the projection matrix
//Associate the camera of position reporter view to the first environment view's graphics context
osg::Camera *positionReporterViewCamera = positionReporterView->getCamera() ;
positionReporterViewCamera->setGraphicsContext(environmentContexts[0].get()) ;
positionReporterViewCamera->setViewport(new osg::Viewport(0, 0, environmentContextsTraits[0]->width, environmentContextsTraits[0]->height)) ;
positionReporterViewCamera->setProjectionMatrixAsOrtho2D(0, environmentContextsTraits[0]->width, 0, environmentContextsTraits[0]->height) ;
positionReporterViewCamera->setClearMask(GL_DEPTH_BUFFER_BIT) ;
// positionReporterViewCamera->setRenderOrder(osg::Camera::POST_RENDER) ;
positionReporterViewCamera->setAllowEventFocus(false) ;
//Disable the position reporter view's camera from receiving events
}
//Setup manipulators for the views
PositionViewGUIEventHandler *positionViewGUIHandler = new PositionViewGUIEventHandler() ;
positionView->addEventHandler(positionViewGUIHandler) ;
EnvironmentViewGUIEventHandler *environmentViewGUIHandler = new EnvironmentViewGUIEventHandler() ;
environmentView->addEventHandler(environmentViewGUIHandler) ;
在如上代码中,position view和position reporter view的主camera都关联到同一个graphics context,请问如何能使这个graphics context产生的键盘事件被两个view的事件响应句柄捕获?谢谢! |
|