Hi,
for my current project I need to render the depth buffer of a scene off screen
into a texture or an image. During the last few days I tried to solve it but I
couldn't get a good result. I got it work to render the color buffer into an
image, but I need the depth buffer. In the following code I tried different
ways to solve the problem, so it's a little bit mixed. Maybe somebody can give
me a hint? I think I'm not far away from the solution.
Thank you in advance,
Jojo
Code:
osg::Image* renderToImage()
{
osgViewer::Viewer* v = new osgViewer::Viewer;
v->setCameraManipulator(new osgGA::TrackballManipulator());
v->getCameraManipulator()->setHomePosition(getCameraPosition2(),
getLookAtPoint(), getUpVector());
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new
osg::GraphicsContext::Traits;
traits->x =0;
traits->y = 0;
traits->width = 720;
traits->height = 576;
traits->windowDecoration = false;
traits->doubleBuffer = false;
traits->sharedContext = 0;
traits->pbuffer = true;
osg::GraphicsContext* _gc=
osg::GraphicsContext::createGraphicsContext(traits.get());
osg::Camera* camera = v->getCamera();
camera->setGraphicsContext(_gc);
camera->setViewport(new osg::Viewport(0,0,720,576));
double fovy, aspectRatio, zNear, zFar;
camera->getProjectionMatrixAsPerspective(fovy, aspectRatio, zNear,
zFar);
std::cout<< fovy << " " << aspectRatio<< " " << zNear << " " <<
zFar<<"\n";
fovy+=41.0;
camera->setProjectionMatrixAsPerspective(fovy, aspectRatio, zNear,
zFar);
std::cout<< fovy << " " << aspectRatio<< " " << zNear << " " <<
zFar<<"\n";
//camera->setViewMatrixAsLookAt(getCameraPosition2(), getLookAtPoint(),
getUpVector());
v->setSceneData(osgDB::readNodeFile("Scene1.osg"));
v->setDataVariance(osg::Object::DYNAMIC);
v->setThreadingModel(osgViewer::Viewer::SingleThreaded);
v->realize();
osg::ref_ptr<osg::Image> depth = new osg::Image;
//depth->allocateImage(720, 576, 1, GL_LUMINANCE, GL_UNSIGNED_BYTE);
camera->attach(osg::Camera::DEPTH_BUFFER, depth.get(),0,0);
v->frame();
float* bl =(float*)depth->data();
for(int i=0; i < 10000; i++)
std::cout<< "Depth: " << bl[i]<<"\n";
osgDB::writeImageFile(*depth.get(),"depth.png");
osg::ref_ptr<osg::Image> depthReadP = new osg::Image;
depthReadP->readPixels(0,0,719,575, GL_RGB ,GL_UNSIGNED_BYTE);
osgDB::writeImageFile(*depthReadP.get(),"depthReadP.png");
delete v;
osg::notify(osg::NOTICE)<<"Success"<<std::endl;
return new osg::Image();
}
[/code]
------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=11145#11145
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org