|
呵呵 是在flysky发的例子基础上改造了一下下
#include <osgViewer/Viewer>
#include <osg/Camera>
#include <osg/Image>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
class ImageHandler : public osgGA::GUIEventHandler
{
public:
ImageHandler(osg::Image* image):_img(image){};
~ImageHandler(){};
bool handle( const osgGA::GUIEventAdapter& ea,
osgGA::GUIActionAdapter& aa )
{
osgViewer::Viewer* viewer =
dynamic_cast<osgViewer::Viewer*>( &aa );
if (!viewer)
return false;
switch(ea.getEventType())
{
case(osgGA::GUIEventAdapter::KEYDOWN):
{
if(ea.getKey() == 'c')
{
//osg::ref_ptr<osg::Image> image = new osg::Image;
//image->readPixels(0,0,1280,1024,GL_RGB,GL_UNSIGNED_BYTE);
//image->setFileName("a.bmp");
osgDB::writeImageFile(*_img,"e:\\a.png");
_img->dirty();
}
}
default:
return false;
}
}
private:
osg::Image* _img;
};
class ImageDrawCallback :public osg::Camera:rawCallback
{
public:
ImageDrawCallback(osg::Image* image):
_image(image)
{
}
~ImageDrawCallback(){}
virtual void operator () (const osg::Camera&) const
{
//osg::ref_ptr<osg::Image> image = new osg::Image;
//osg::GraphicsContext::WindowingSystemInterface *wsi = osg::GraphicsContext::getWindowingSystemInterface();
//unsigned int width,height;
//wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width,height);
_image->readPixels( 0, 0, 1024, 768, GL_RGB, GL_UNSIGNED_BYTE);
//osgDB::writeImageFile( *_image, "e:\\b.png");
_image->dirty();
}
private:
osg::Image* _image;
};
int
main( int, char ** )
{
osgViewer::Viewer viewer;
viewer.setSceneData( osgDB::readNodeFile( "cow.osg" ) );
if (!viewer.getSceneData())
{
osg::notify( osg::FATAL ) << "Unable to load data file. Exiting." << std::endl;
return 1;
}
osg::ref_ptr<osg::Image> image = new osg::Image;
image->allocateImage(1024,768,1,GL_RGB,GL_UNSIGNED_BYTE);
viewer.getCamera()->setPostDrawCallback(new ImageDrawCallback(image));
viewer.addEventHandler( new ImageHandler(image) );
return viewer.run();
}
不过在退出的时候有错误,请哪位帮忙看一下啊 |
|