iOS:设置 OSG 绘制背景为透明
2014年11月11日
在 OSG 3.2.1 中需要设置 OSG 绘制背景为透明的时候,需要做出如下设置:
1、设置 windata:
1 2 3 | // Init the Windata Variable that holds the handle for the Window to display OSG in. osg::ref_ptr<osgViewer::GraphicsWindowIOS::WindowData> windowData = new osgViewer::GraphicsWindowIOS::WindowData(self->osgView, osgViewer::GraphicsWindowIOS::WindowData::IGNORE_ORIENTATION, windowFactor); windowData->setCreateTransparentView(true); |
2、设置 trails:
1 | traits->alpha = 1; |
3、设置 camera 透明色:
1 2 3 4 5 6 7 8 9 10 11 | - (void)setupCamera { //set the clear color of the camera to be semitransparent _viewer->getCamera()->setClearColor(osg::Vec4(0.0f, 0.0f, 0.0f, 0.0f)); //set the clear mask for the camera _viewer->getCamera()->setClearMask(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); _viewer->getCamera()->setRenderOrder(osg::Camera::POST_RENDER); _viewer->getCamera()->setComputeNearFarMode(osgUtil::CullVisitor::DO_NOT_COMPUTE_NEAR_FAR); } |
完整示例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | //-- Set viewer // Set context for viewer //create our graphics context directly so we can pass our own window osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; float windowFactor = 2.0; // Init the Windata Variable that holds the handle for the Window to display OSG in. osg::ref_ptr<osgViewer::GraphicsWindowIOS::WindowData> windowData = new osgViewer::GraphicsWindowIOS::WindowData(self->osgView, osgViewer::GraphicsWindowIOS::WindowData::IGNORE_ORIENTATION, windowFactor); windowData->setCreateTransparentView(true); // Init the Windata Variable that holds the handle for the Window to display OSG in. osg::ref_ptr<osg::Referenced> windata = windowData; // Setup the traits parameters traits->x = 0; traits->y = 0; traits->width = m_glview.frame.size.width*windowFactor; traits->height = m_glview.frame.size.height*windowFactor; traits->depth = 16; //keep memory down, default is currently 24 traits->alpha = 1; //traits->stencil = 8; traits->windowDecoration = false; traits->doubleBuffer = true; traits->sharedContext = 0; traits->setInheritedWindowPixelFormat = true; //traits->windowName = "osgViewer"; traits->inheritedWindowData = windata; // Create the Graphics Context osg::ref_ptr<osg::GraphicsContext> graphicsContext = osg::GraphicsContext::createGraphicsContext(traits.get()); |
参考材料:
[1] http://forum.openscenegraph.org/viewtopic.php?t=9472
[2] http://stackoverflow.com/questions/1394934/iphone-layering-a-transparent-opengl-view-on-top-of-a-uiview