|
请教一下,我之前定义了一个DrawCallBack类,用来截屏每一帧,并保存为图片。
在main函数里setPostDrawcallBack,当使用viewer.run时,可以成功截屏每一帧。但是当使用
while(!viewer.done())
{
viewer.frame():
}
时,截图结果不对,请问是什么情况?
- static long ii=1;
- class WindowCaptureCallback : public osg::Camera::DrawCallback
- //
- {
- public:
- WindowCaptureCallback(GLenum readBuffer, const std::string& name):
- _readBuffer(readBuffer),
- _fileName(name)
- {
- _image = new osg::Image;
- }
- virtual void operator () (osg::RenderInfo& renderInfo) const
- {
- #if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE)
- glReadBuffer(_readBuffer);
- #else
- osg::notify(osg::NOTICE)<<"Error: GLES unable to do glReadBuffer"<<std::endl;
- #endif
- OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
- osg::GraphicsContext* gc = renderInfo.getState()->getGraphicsContext();
- if (gc->getTraits())
- {
- GLenum pixelFormat;
- if (gc->getTraits()->alpha)
- pixelFormat = GL_RGBA;
- else
- pixelFormat = GL_RGB;
- #if defined(OSG_GLES1_AVAILABLE) || defined(OSG_GLES2_AVAILABLE)
- if (pixelFormat == GL_RGB)
- {
- GLint value = 0;
- #ifndef GL_IMPLEMENTATION_COLOR_READ_FORMAT
- #define GL_IMPLEMENTATION_COLOR_READ_FORMAT 0x8B9B
- #endif
- glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT, &value);
- if ( value != GL_RGB ||
- value != GL_UNSIGNED_BYTE )
- {
- pixelFormat = GL_RGBA;//always supported
- }
- }
- #endif
- int width = gc->getTraits()->width;
- int height = gc->getTraits()->height;
- std::cout<<"Capture: size="<<width<<"x"<<height<<", format="<<(pixelFormat == GL_RGBA ? "GL_RGBA":"GL_RGB")<<std::endl;
- _image->readPixels(0, 0, width, height, pixelFormat, GL_UNSIGNED_BYTE);
- }
- if (!_fileName.empty())
- {
- char filename[20]={0};
- sprintf(filename,"OUTPUT_%05d.bmp",ii++);
- std::cout << "Writing to: " << filename << std::endl;
- osgDB::writeImageFile(*_image, filename);
- }
- }
- protected:
- GLenum _readBuffer;
- std::string _fileName;
- osg::ref_ptr<osg::Image> _image;
- mutable OpenThreads::Mutex _mutex;
- };
- int main( int argc, char** argv )
- {
- osgViewer::Viewer viewer;
- osg::ref_ptr<osg::Node> model = osgDB::readNodeFile( "cessna.osg" );
-
- osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
- traits->x = 40;
- traits->y = 40;
- traits->width = 600;
- traits->height = 480;
- traits->windowDecoration = true;
- traits->doubleBuffer = true;
- traits->sharedContext = 0;
- traits->pbuffer=true;
- osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
- viewer.getCamera()->setGraphicsContext(gc.get());
- viewer.getCamera()->setViewport(0,0,600,480);
- viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
- viewer.setSceneData( model.get() );
- viewer.realize();
-
- GLenum buffer = viewer.getCamera()->getGraphicsContext()->getTraits()->doubleBuffer ? GL_BACK : GL_FRONT;
- viewer.getCamera()->setPostDrawCallback(new WindowCaptureCallback(buffer,"capture"));
- viewer.realize();
- //将下面的语句改为viewer.run()就可以正常运行,请问是什么情况?
- while(!viewer.done())
- {
- viewer.frame();
- }
- return 0;
- }
复制代码 |
|