|
楼主 |
发表于 2008-9-24 20:41:58
|
显示全部楼层
已经能够实现了,代码如下
运行时可以看到抓屏时屏幕一直在变,如果在应用程序里面,这样不太容易接受。有没有可能抓屏时不让它变化呢?即使变,能不能达到让人感觉不到的地步呢?
#include <osgViewer/Viewer>
#include <osgDB/WriteFile>
#include <osgDB/ReadFile>
#include <osgGA/TrackballManipulator>
#include <iostream>
bool snap(osg::ref_ptr<osgViewer::Viewer> v,int scale=1)
{
int x=0,y=0;
int width = v->getCamera()->getViewport()->width();
int height=v->getCamera()->getViewport()->height();
double left,right,bottom,top,zNear,zFar;
double newLeft,newRight,newBottom,newTop;
v->getCamera()->getProjectionMatrixAsFrustum(left,right,bottom,top,zNear,zFar);
printf("left=%.2f\tright=%.2f\nbottom=%.2f\ttop=%.2f",left,right,bottom,top);
double wid=right-left;
double h=top-bottom;
osg::ref_ptr<osg::Image> Image=new osg::Image;
Image->allocateImage(width*scale,height*scale,1,GL_RGB,GL_UNSIGNED_BYTE);
for(int i=0;i<scale;i++)
{
for(int j=0;j<scale;j++)
{
newLeft=left+wid*i/(double)scale;
newRight=newLeft+wid/(double)scale;
newBottom=bottom+h*j/(double)scale;
newTop=newBottom+h/(double)scale;
v->getCamera()->setProjectionMatrixAsFrustum(newLeft,newRight,newBottom,newTop,zNear,zFar);
v->renderingTraversals();
osg::ref_ptr<osg::Image> image=new osg::Image;
image->readPixels(x,y,width,height,GL_RGB,GL_UNSIGNED_BYTE);
Image->copySubImage(width*i,height*j,0,image.get());
char filename[50];
sprintf(filename,"d:\\pic%d_%d.jpg",i,j);
osgDB::writeImageFile(*image,filename);
}
}
osgDB::writeImageFile(*Image,"d:\\pic.jpg");
return true;
}
int main()
{
osg::ref_ptr<osgViewer::Viewer>v=new osgViewer::Viewer;
v->setSceneData(osgDB::readNodeFile("cow.osg"));
v->setCameraManipulator(new osgGA::TrackballManipulator());
v->frame();
snap(v,2);
return 1;
} |
|