|
我在一个eventhandler的handle函数中,实现场景自定义分辨率抓图,想请教下为什么抓出来的图看起来只有背景色呢?
osgViewer::View * viewer = dynamic_cast<osgViewer::View*>(&aa);
osg::Camera * mCamera = viewer->getCamera();
osg::ref_ptr<osg::Camera> camera = new osg::Camera();//新添加的相机实现离屏抓图
m_pFrameImage = new osg::Image;
m_pFrameImage->allocateImage(1000,800,1, GL_RGBA, GL_UNSIGNED_BYTE);
for (int col=0;col<2;col++)
{
for (int row=0;row<2;row++)
{
osg::Matrix m_mtxCurView;
osg::Matrix m_mtxCurProj;
m_mtxCurView = mCamera->getViewMatrix();
m_mtxCurProj = mCamera->getProjectionMatrix();
camera->setViewport(0,0,mCamera->getGraphicsContext()->getTraits()->width,mCamera->getGraphicsContext()->getTraits()->height);
/* osg::Matrix offsetMatrix =
osg::Matrix::scale(2, 2, 1.0) *
osg::Matrix::translate(2-1-2*col, 2-1-2*row, 0.0);*/
camera->setViewMatrix(m_mtxCurView);
camera->setProjectionMatrix( m_mtxCurProj);// * offsetMatrix
camera->setGraphicsContext(mCamera->getGraphicsContext());
camera->setClearColor(osg::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
camera->setAllowEventFocus(false);
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
camera->addChild(m_pEnviro->GetVtScene()->GetRootNode()->GetOsgGroup());
viewer->addSlave(camera);
osg::ref_ptr<osg::Image> image = new osg::Image;
image->allocateImage(1000/2,800/2, 1, GL_RGBA, GL_UNSIGNED_BYTE );
camera->setRenderOrder(osg::Camera:RE_RENDER);
camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
//camera->detach( osg::Camera::COLOR_BUFFER );
camera->attach( osg::Camera::COLOR_BUFFER, image.get(), 0, 0 );
viewer->removeSlave(0);
for (int t = 0; t < image->t(); ++t)
{
unsigned char * source = image->data(0, t);
unsigned char * target = m_pFrameImage->data( col * 500, t + row * 400 );
memcpy(target, source, image->s() * 4 * sizeof(unsigned char));
}
//
}
} |
|