[osg-users] unable to save render-to-texture image

sun qi qsun at foxmail.com
Sun Apr 17 23:26:43 PDT 2011


Hi,all

I was trying to save a render-to-texture image file many ways, however, failed each time.
when i attach texture to camera.it works fine.
but i attach image to camera. it doesn't work right.
I saved the image data to .jpg, .png, .bmp format . but when i double-click the saved image file, there's nothing but blank.and when i tried do the same thing in the osg example osgprerender, the problem was still that.  i use 
osgDB::writeImageFile(*image, "d:\\data\\test.bmp"); 
 why? Is there someone met this problem? How did you fix it?

[code]
osg::ref_ptr<osg::Texture2D> colorMap = new osg::Texture2D;
colorMap->setTextureSize(width, height);
colorMap->setInternalFormat(GL_RGBA);
colorMap->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR);
colorMap->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR);

osg::ref_ptr<osg::Texture2D> depthMap = new osg::Texture2D;
depthMap->setTextureSize(width,height);
depthMap->setInternalFormat(GL_DEPTH_COMPONENT);
depthMap->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR);
depthMap->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR);

osg::ref_ptr<osg::Camera> camera = new osg::Camera;
camera->setClearColor(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
camera->setReferenceFrame(osg::Transform::RELATIVE_RF);
camera->setProjectionMatrixAsFrustum(-proj_right, proj_right, -proj_top, proj_top, znear, zfar);
//camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
camera->setProjectionMatrix(osg::Matrixd::identity());
camera->setViewMatrixAsLookAt(bs.center()-osg::Vec3(0.0f,2.0f,0.0f)*bs.radius(),bs.center(),osg::Vec3(0.0f,0.0f,1.0f));
camera->setViewMatrix(osg::Matrixd::identity());
camera->setViewport(0, 0, width, height);
camera->setRenderOrder(osg::Camera::PRE_RENDER);
camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
camera->attach(osg::Camera::DEPTH_BUFFER, depthMap);
camera->attach(osg::Camera::COLOR_BUFFER, colorMap);
osg::ref_ptr<osg::Image> image = new osg::Image;
image->allocateImage(width, height, 1, GL_RGBA, GL_FLOAT);
image->setInternalTextureFormat(GL_RGBA);
camera->attach(osg::Camera::COLOR_BUFFER, image.get());
osg::ref_ptr<osg::Geode> geo = createCube(colorMap, depthMap);
camera->addChild(modelNode.get());
osg::ref_ptr<osg::Group> parent = new osg::Group;
parent->addChild(geo.get());
parent->addChild(camera.get());
osgDB::writeImageFile(*image, "test.bmp");
//colorMap->setImage(0, image);
return parent.get();
[/code]
... 

Thank you!

Cheers,
sun

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=38548#38548








More information about the osg-users mailing list