同事由于新加了一个屏幕,本来用
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer(); viewer->setSceneData(groupRoot.get()); return viewer->run();
就直接可以跑全屏的程序了,然是现在会在两块屏出现全屏,他很苦恼。
于是他:
osgViewer::View* createView(int screenNum) { unsigned int width = 1920, height = 1080; osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface(); if (wsi) { wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(screenNum),width,height); } osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; traits->screenNum = screenNum; traits->x = 0; traits->y = 0; traits->width = width; traits->height = height; traits->windowDecoration = false; traits->doubleBuffer = true; traits->sharedContext = 0; osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get()); if (!gc) return NULL; osg::ref_ptr<osg::Camera> camera = new osg::Camera; camera->setGraphicsContext(gc.get()); camera->setViewport(new osg::Viewport(0,0,width,height)); camera->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(width) / static_cast<double>(height), 1.0f, 10000.0f); GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; camera->setDrawBuffer(buffer); camera->setReadBuffer(buffer); osg::ref_ptr<osgViewer::View> view = new osgViewer::View; view->setCamera(camera.get()); view->setCameraManipulator(new osgGA::TrackballManipulator); return view.release(); }
调用时:
osg::ref_ptr<osgViewer::CompositeViewer> viewer = new osgViewer::CompositeViewer(); osgViewer::View* view = createView(0); if (view) { view->setSceneData(groupRoot.get()); viewer->addView(view); } return viewer->run();
但是遇到问题
他加载了一个使用透明图片的billboard
在之前的代码中显示是透明的,
在用了自己的createView()函数之后,就不透明的。
百思不得其解啊
我看看了,觉得是透明的问题。
给他的
osg::ref_ptr<osg::StateSet> pStateSet = new osg::StateSet(); // 启用混合 pStateSet->setMode(GL_BLEND,osg::StateAttribute::ON); ···· //主要是下面这句 pStateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); pGeometry->setStateSet(pStateSet.get());
设置了TRANSPARENT_BIN就透明了的。
透明的问题解决了。。。
那么新问题来了:
1.为什么用viewer自带的就不需要设置?
2.透明测试和深度测试会有问题(老问题)
那这些以后解决吧
当天又解决了:
// 状态属性对象 osg::ref_ptr<osg::StateSet> pStateSet = new osg::StateSet(); // 创建一个2D属性对象 osg::ref_ptr<osg::Texture2D> pTexture2D = new osg::Texture2D(); // 关联Image对象 pTexture2D->setImage(image.get()); // 关联Texture2D纹理对象 pStateSet->setTextureAttributeAndModes(0, pTexture2D,osg::StateAttribute::ON); // 启用混合 pStateSet->setMode(GL_BLEND,osg::StateAttribute::ON); // 关闭光照 pStateSet->setMode(GL_LIGHTING,osg::StateAttribute::OFF); ···················· osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer(); viewer->setSceneData(groupRoot.get()); //其实下面这句话就可以解决我同事多屏幕的问题 viewer->setUpViewOnSingleScreen(); return viewer->run();