|
楼主 |
发表于 2014-6-24 23:26:25
|
显示全部楼层
struct MyCameraFinalDrawCallback : public osg::Camera:rawCallback
{
MyCameraFinalDrawCallback(osg::Image* image):
_image(image)
{
}
virtual void operator () (const osg::Camera& /*camera*/) const
{
if (_image && _image->getPixelFormat()==GL_RGBA && _image->getDataType()==GL_UNSIGNED_BYTE)
{
// we'll pick out the center 1/2 of the whole image,
int column_start = _image->s()/4;
int column_end = 3*column_start;
int row_start = _image->t()/4;
int row_end = 3*row_start;
// and then invert these pixels
for(int r=row_start; r<row_end; ++r)
{
unsigned char* data = _image->data(column_start, r);
for(int c=column_start; c<column_end; ++c)
{
(*data) = 255-(*data); ++data;
(*data) = 255-(*data); ++data;
(*data) = 255-(*data); ++data;
(*data) = 255; ++data;
}
}
// dirty the image (increments the modified count) so that any textures
// using the image can be informed that they need to update.
_image->dirty();
}
else if (_image && _image->getPixelFormat()==GL_RGBA && _image->getDataType()==GL_FLOAT)
{
// we'll pick out the center 1/2 of the whole image,
int column_start = _image->s()/4;
int column_end = 3*column_start;
int row_start = _image->t()/4;
int row_end = 3*row_start;
// and then invert these pixels
for(int r=row_start; r<row_end; ++r)
{
float* data = (float*)_image->data(column_start, r);
for(int c=column_start; c<column_end; ++c)
{
(*data) = 1.0f-(*data); ++data;
(*data) = 1.0f-(*data); ++data;
(*data) = 1.0f-(*data); ++data;
(*data) = 1.0f; ++data;
}
}
// dirty the image (increments the modified count) so that any textures
// using the image can be informed that they need to update.
// _image->dirty();
}
}
osg::Image* _image;
};
int main()
{
osg::ref_ptr<osg::Group> root = new osg::Group;
osg::ref_ptr<osg::Node> model=new osg::Node;
model=osgDB::readNodeFile("glider.osg");
osg::ref_ptr<osg::Camera> camera=new osg::Camera;
osg::ref_ptr<osg::Image> image= new osg::Image;
camera->setViewport(0,0,800,600);
camera->setClearColor(osg::Vec4(0.1f,0.1f,0.3f,1.0f));
camera->attach(osg::Camera::COLOR_BUFFER,image.get());
camera->addChild(model);
image->allocateImage(800,600,1,GL_RGB,GL_UNSIGNED_BYTE);
camera->setFinalDrawCallback(new MyCameraFinalDrawCallback(image));
root->addChild(camera.get());
osg::ref_ptr<osgViewer::Viewer> viewer =new osgViewer::Viewer;
viewer->setSceneData(root.get());
viewer->frame();
osgDB::writeImageFile(*image,"first.bmp");
viewer->run(); |
|