|
我的一段抓图的代码,在我自己的机子上运行没有出现问题,可把这段代码放到别人机子上运行时,只有第一次运行抓到了图,再抓就只抓到灰色的图片(我都怀疑我第一次是不是错觉)。在另一个机子上运行,结果直接出错,好像是提示跟Buffer有关。我想问一下高人,attach用的时候是不是跟硬件有关(上述三台机子配置都不一样),使用attach时应该注意哪些(我感觉抓图前是不是少写什么了)- bool CVrEditorView::exportSnapshot(const std::string &filename)
- {
-
- osg::Viewport* vp=mOSG->getViewer()->getSlave(0)._camera->getViewport();
- int width=vp->width();
- int height=vp->height();
-
-
- osg::ref_ptr<osg::GraphicsContext> gc = mOSG->getViewer()->getSlave(0)._camera->getGraphicsContext();
- const osg::GraphicsContext::Traits* traits=gc->getTraits();
-
- osg::ref_ptr<osg::Camera> camera = new osg::Camera();
- camera->setGraphicsContext(gc.get());
- camera->setViewport(new osg::Viewport(0,0,width,height));
- GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
- camera->setDrawBuffer(buffer);
- camera->setReadBuffer(buffer);
- double left,right,bottom,top,zNear,zFar;
- mOSG->getViewer()->getCamera()->getProjectionMatrixAsFrustum(left,right,bottom,top,zNear,zFar);
- double h=top-bottom;
- double aspectRatio=(double)width/(double)height;//主辅相机aspectRatio不一样
- double wid=h*aspectRatio;
- left=-wid*0.5;
- right=wid*0.5;
- top=h*0.5;
- bottom=-h*0.5;
- mOSG->getViewer()->addSlave(camera.get());//, osg::Matrixd(), osg::Matrixd::scale(aspectRatioScale,1.0,1.0));
- osg::ref_ptr<osg::Image> img=new osg::Image();
- img->allocateImage(width*viewportScale,height*viewportScale,1,GL_RGB,GL_UNSIGNED_BYTE);
- camera->setRenderOrder(osg::Camera::PRE_RENDER);
- camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
- camera->attach(osg::Camera::COLOR_BUFFER,img.get());
- camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- camera->setProjectionMatrixAsFrustum(left,right,bottom,top,zNear,zFar);
- mOSG->getViewer()->renderingTraversals();
- osgDB::writeImageFile(*img.get(),filename);
-
-
- //移除抓图用的相机
- unsigned int slave_num=mOSG->getViewer()->getNumSlaves();
- slave_num--;
- mOSG->getViewer()->removeSlave(slave_num);
- //for(int i=0;i<slave_num;i++)
- //{
- // if(mOSG->getViewer()->getSlave(i)._camera==camera)
- // {
- // mOSG->getViewer()->removeSlave(i);
- // break;
- // }
- //}
- return true;
- }
复制代码 |
|