|
如题:
我想在我的程序当中用多个相机渲染场景,其中一个主相机,两个从相机,我的程序当中有CEGUI做的界面。用一个主相机渲染的时候,界面是可以显示的。但是如果在主相机下添加两个从相机以后,界面就不能显示了,或者直接中断报错!不知道是不是CEGUI的问题?
部分代码如下:
osg::Camera* createCamera(int x, int y, int w, int h,osg::Vec4 color)
{
osg::ref_ptr<osg::GraphicsContext::Traits> trait = new osg::GraphicsContext::Traits();
trait->x = x;
trait->y = y;
trait->width = w;
trait->height = h;
trait->windowDecoration = false;
trait->doubleBuffer = true;
//trait->sharedContext = 0;
trait->setInheritedWindowPixelFormat = true;
//trait->inheritedWindowData = windata;
osg:isplaySettings *ds = osg::DisplaySettings::instance();
trait->alpha = ds->getMinimumNumAlphaBits();
trait->stencil = ds->getMinimumNumStencilBits();
trait->sampleBuffers = ds->getMultiSamples();
trait->samples = ds->getNumMultiSamples();
double fov;
double ratio;
double nearPlane;
double farPlane;
osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(trait.get());
osg::ref_ptr<osg::Camera> camera = new osg::Camera();
camera->setGraphicsContext(gc.get());
camera->setViewport(new osg::Viewport(0,0,w,h));
camera->setProjectionResizePolicy(osg::Camera:rojectionResizePolicy::FIXED);
//换取对称视景个参数并显示,每个屏幕都有对应的ratio,所以必须先get在set
camera->getProjectionMatrixAsPerspective( fov,ratio,nearPlane,farPlane);
//设置视景参数,将yz平面视野角度设为50°
camera->setProjectionMatrixAsPerspective(50.0f, ratio, 1, 500.0);
camera->setNearFarRatio(0.000003f);
camera->setClearColor(color);
return camera.release();
}
int main(int argc, char *argv[])
{
pathPoint = new osg::Vec3Array;
softwareRigister softwareRegister;
//防止程序启动多个
HANDLE hMutex = CreateMutex(NULL,true, "xxxx-xxxx");
if(hMutex !=0&&GetLastError()==ERROR_ALREADY_EXISTS)
{
MessageBoxA(NULL, "A INSTANCE HAS ALREADY BEEN STARTED!", "Warning", MB_OK);
return 0;
}
HWND hwnd=GetForegroundWindow();//
SendMessage(hwnd,WM_SETICON,ICON_SMALL,(LPARAM)LoadIcon(NULL,(LPCSTR)IDI_ICON1));
//场景根节点
osg::ref_ptr<osg::Group> root = new osg::Group;
SpecialEffect *specialEffect = new SpecialEffect();
InfoVisitor* infoVisitor = new InfoVisitor();
//物理世界根节点
PhysicsManager *physicsMgr = new PhysicsManager(infoVisitor);
btDynamicsWorld* world = physicsMgr->initializePhysicWorld();
//场景Viewer
osg::ref_ptr<osgViewer::Viewer> viewer=new osgViewer::Viewer();
// viewer->setUpViewInWindow(-10,0,800,800);
//viewer->getCamera()->setClearMask(GL_DEPTH_TEST);//深度测试
//场景漫游器
osg::ref_ptr<TravelManipulator> mainCamera =new TravelManipulator(physicsMgr,viewer);
viewer->setCameraManipulator(mainCamera);
mainCamera->m_pHostViewer =viewer;
//场景管理器
SceneManager* sceneMgr=new SceneManager(root,world,viewer,mainCamera,physicsMgr,specialEffect,infoVisitor);
PhysicsManager::_scene = sceneMgr;
PhysicsManager::m_pInfovisitor=infoVisitor;
PhysicsManager::_root=root;
//得到窗口的图形环境
osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
if (!wsi)
{
osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl;
}
unsigned int tileWidth, tileHeight;
wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), tileWidth ,tileHeight);
// 局部变量存放窗口矩形
RECT rect;
HWND m_hWnd;
// 得到当前窗口矩形
::GetWindowRect(m_hWnd, &rect);
// 初始化窗口变量,为OSG所用
osg::ref_ptr<osg::Referenced> windata = new osgViewer::GraphicsWindowWin32::WindowData(m_hWnd);
//初始化主相机图形环境
osg::ref_ptr<osg::GraphicsContext::Traits> mastertrait = new osg::GraphicsContext::Traits();
mastertrait->x = (rect.right - rect.left)/3;
mastertrait->y = 0;
mastertrait->width = (rect.right - rect.left)/3;//rect.right - rect.left
mastertrait->height = rect.bottom - rect.top;//rect.bottom - rect.top;
mastertrait->windowDecoration = false;
mastertrait->doubleBuffer = true;
mastertrait->useCursor = false;
//mastertrait->sharedContext = 0;
mastertrait->setInheritedWindowPixelFormat = true;
mastertrait->inheritedWindowData = windata;
osg::DisplaySettings *ds = osg::DisplaySettings::instance();
mastertrait->alpha = ds->getMinimumNumAlphaBits();
mastertrait->stencil = ds->getMinimumNumStencilBits();
mastertrait->sampleBuffers = ds->getMultiSamples();
mastertrait->samples = ds->getNumMultiSamples();
osg::ref_ptr<osg::GraphicsContext> mastergc = osg::GraphicsContext::createGraphicsContext(mastertrait.get());
osg::ref_ptr<osg::Camera> masterCamera = new osg::Camera();
masterCamera = viewer->getCamera();//
masterCamera->setGraphicsContext(mastergc.get());
masterCamera->setViewport(new osg::Viewport(0,0,mastertrait->width , mastertrait->height));
GLenum buffer = mastertrait->doubleBuffer ? GL_BACK : GL_FRONT;
masterCamera->setDrawBuffer(buffer);
masterCamera->setReadBuffer(buffer);
double fov;
double aspectRatio;
double nearPlane;
double farPlane;
double newAspectRatio;
masterCamera->setProjectionResizePolicy(osg::Camera::ProjectionResizePolicy::FIXED);
//换取对称视景个参数并显示,每个屏幕都有对应的ratio,所以必须先get在set
masterCamera->getProjectionMatrixAsPerspective( fov,aspectRatio,nearPlane,farPlane);
newAspectRatio = double(tileWidth)/double(tileHeight);
double aspectRatiochange = newAspectRatio/aspectRatio;
//设置视景参数,将yz平面视野角度设为50°
masterCamera->setProjectionMatrixAsPerspective(90.0f, aspectRatio, 1, 500.0);
masterCamera->setNearFarRatio(0.000003f);
//viewer->setUpViewAcrossAllScreens();//必须干掉这句话
viewer->setReleaseContextAtEndOfFrameHint(false);
viewer->setKeyEventSetsDone(0);//ESC
viewer->setLightingMode(osg::View:ightingMode::HEADLIGHT);
masterCamera->setClearColor(osg::Vec4(0.0,0.0,0.0,0.5));
viewer->addSlave(createCamera(0,0,mastertrait->width,mastertrait->height,osg::Vec4(1.0,0.0,0.0,0.5)),osg::Matrixd::translate(2.0,0.0,0.0),osg::Matrixd());
viewer->addSlave(createCamera(2*mastertrait->width,0,mastertrait->width,mastertrait->height,osg::Vec4(0.0,1.0,0.0,0.5)),osg::Matrixd::translate(-2.0,0.0,0.0),osg::Matrixd());
//SceneSoundManager SoundCreate;
osg::ref_ptr<osgAudio::SoundNode> KeySoundNode = SoundCreate->createKeystrokeSound("../Media/Sound/KeyWater.WAV");
root->addChild(KeySoundNode);
osg::ref_ptr<osgAudio::SoundNode> ClickSoundNode = SoundCreate->createClickstrokeSound("../Media/Sound/strokeball.WAV");
root->addChild(ClickSoundNode);
//Weather
Weather* weather = NULL;
weather = Weather::getInstance();
weather->setViewer(viewer);
weather->setPhysics(physicsMgr);
root->addEventCallback(weather);
osg::ref_ptr<osgViewer::GraphicsWindowWin32> gcwin32=dynamic_cast<osgViewer::GraphicsWindowWin32*>(masterCamera->getGraphicsContext());
//root->addEventCallback(new TestMinimizeHandler(gcwin32));
//UI
viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
viewer->realize();
masterCamera->getGraphicsContext()->makeCurrent();
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
osg::ref_ptr<CEGUIDrawable> cd = new CEGUIDrawable(sceneMgr,KeySoundNode,viewer,ClickSoundNode,gcwin32/*,printer.get()*/);//界面节点
geode->addDrawable(cd.get());
cd->Init();//初始化事件处理函数pinfo
root->addChild(geode.get());
geode->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
geode->getOrCreateStateSet()->setAttributeAndModes( new osg::BlendFunc );//初始化UI
root->addEventCallback(new CEGUIUpdateCallback(geode,cd,sceneMgr,physicsMgr));
viewer->setSceneData( root );
softwareRegister.detecInstanseFunction();
//传感器
root->addUpdateCallback(new InputDataUpdateCallback(physicsMgr,viewer,mainCamera));
osg::Timer_t frame_tick = osg::Timer::instance()->tick();
while(!viewer->done())
{
// Physics update
osg::Timer_t now_tick = osg::Timer::instance()->tick();
float dt = osg::Timer::instance()->delta_s(frame_tick, now_tick);
frame_tick = now_tick;
/* int numSimSteps = */
world->stepSimulation(dt, maxSubStep, btScalar(1.)/btScalar(fixedTimeStep)); //, 10, 0.01);
//world->stepSimulation(1.0/fixedTimeStep);
world->updateAabbs();
viewer->frame();
}
} |
|