|
楼主 |
发表于 2011-5-31 16:53:25
|
显示全部楼层
回复 9# robinhit
打扰了,帮忙看下,写的不清楚的地方请告诉我!
#define IMAGE_WIDTH 256
#define IMAGE_HEIGHT 256
//左相机抓图类的成员函数定义
LeftCameraCallBack:eftCameraCallBack(osg::ref_ptr<osg::Image>image)
{
_image = image;
/*counter = 0;*/
}
void LeftCameraCallBack:perator()( osg::RenderInfo &renderInfo)const
{
osg::GraphicsContext* gc = renderInfo.getState()->getGraphicsContext();
if (gc->getTraits())
{
GLenum pixelFormat;
// 计算图象数据格式GL_RGB或GL_RGBA,即是否带alpha通道
// 也可以理解成像素是24bits或32bits
if (gc->getTraits()->alpha)
pixelFormat = GL_RGBA;
else
pixelFormat = GL_RGB;
// 获得图象的大小,即为当前窗口的大小
int width = IMAGE_WIDTH;
int height = IMAGE_HEIGHT;
// 使用osg::Image类提供的函数来读取像素数据并保存到osg::Image中
_image->allocateImage(width,height,1,pixelFormat,GL_UNSIGNED_BYTE);
_image->readPixels(0,0,width,height,pixelFormat,GL_UNSIGNED_BYTE);
}
}
void CCoreOSG::InitCameraConfig(void)
{
// 局部变量存放窗口矩形
RECT rect;
// 创建一个复合viewer
mViewer = new osgViewer::CompositeViewer();
// 得到当前窗口矩形
::GetWindowRect(m_hWnd, &rect);
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
// 初始化窗口变量,为OSG所用
osg::ref_ptr<osg::Referenced> windata = new osgViewer::GraphicsWindowWin32::WindowData(m_hWnd);
// 设置一些个参数
traits->screenNum = 3;
traits->x = 0;
traits->y = 0;
traits->width = GetSystemMetrics (SM_CXSCREEN);
traits->height =GetSystemMetrics (SM_CYSCREEN);
traits->windowDecoration = false;
traits->doubleBuffer = true;
traits->sharedContext = 0;
traits->setInheritedWindowPixelFormat = true;
traits->inheritedWindowData = windata;
// 创建图形上下文
osg::GraphicsContext* gc = osg::GraphicsContext::createGraphicsContext(traits.get());
/* debug */
gc->setClearColor(osg::Vec4f(0.0f, 0.0f, 0.0f, 1.0f));
gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//全局图像View
viewers[1] = new osgViewer::View;
viewers[1]->setName("View two");
mViewer->addView(viewers[1]);
viewers[1]->setUpViewOnSingleScreen(1);
leftCamera = viewers[1]->getCamera();
viewers[1]->setSceneData(mRoot.get());
viewers[1]->getCamera()->setViewport(new osg::Viewport(0,0, traits->width, traits->height));
viewers[1]->getCamera()->setGraphicsContext(gc);
viewers[1]->getCamera()->setProjectionMatrixAsPerspective(60.0, double(traits->width) / double(traits->height), 0.001, 1000.0);
viewers[1]->setCameraManipulator(trackball);
viewers[1]->addEventHandler( new osgViewer::WindowSizeHandler );
//左眼相机View
viewers[2] = new osgViewer::View;
viewers[2]->setName("View two");
mViewer->addView(viewers[2]);
//viewers[2]->setUpViewOnSingleScreen(2);
leftCamera = viewers[2]->getCamera();
viewers[2]->setSceneData(mRoot.get());
viewers[2]->getCamera()->setViewport(new osg::Viewport(0,0, IMAGE_WIDTH,IMAGE_HEIGHT));
viewers[2]->getCamera()->setGraphicsContext(gc);
viewers[2]->getCamera()->setProjectionMatrixAsPerspective(60.0,1.0, 0.001, 1000.0);
viewers[2]->setCameraManipulator(new osgGA::TrackballManipulator);
limage_c = new osg::Image();
viewers[2]->getCamera()->setPostDrawCallback(new LeftCameraCallBack(limage_c.get()));
}
osg::ref_ptr<osg::Image> CCoreOSG::getLeftImage()
{
return limage_c;
}
void CRobotSimView::OnTestSaveleft()
{
// TODO: 在此添加命令处理程序代码
osg::ref_ptr<osg::Image>leftImage = mOSG->getLeftImage();
std::string leftC;
CString str = "LeftCamera";
CString strCounter;
strCounter.Format("%d", leftcounter);
str = str + strCounter+".bmp";
leftC = (LPCTSTR)str;
osgDB::writeImageFile(*(leftImage.get()),leftC);
}
程序就是调用CRobotSimView::OnTestSaveleft函数,来保存图像的。
截图的效果就是这样的,readpixels函数截的图片正好是我想要的大小,可是为什么大于视口的范围啊?而且当整个窗口大小变化时,为什么这个视口的大小也随着变啊? |
|