[osg-users] Scene renders black when replacing a ShadowedScene

Chris Denham c.m.denham at gmail.com
Thu Nov 1 10:56:06 PDT 2007


I'm getting a problem when assigning a new shadowed scene to the viewer.
What seems to happen is that once the first scene has been rendered,
the assignment of a new shadowed scene results in a everything in the
scene being drawn black. Also odd is the fact that there doesn't seem
to be a problem when a texture is assigned to objects being drawn.

I have created a minimal example that exhibits the problem, and pasted
it below.
Any ideas what's going wrong? Can anyone reproduce the effect?

[I'm using osg2.2 on windowsXP with Radeon X300 with latest drivers.]

----------------------------------------------------------------------------------------------------
#include <osg/ShapeDrawable>
#include <osg/Geode>
#include <osgGA/TrackballManipulator>
#include <osgShadow/ShadowedScene>
#include <osgShadow/ShadowMap>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>

const int ReceivesShadowTraversalMask = 0x1;
const int CastsShadowTraversalMask = 0x2;

osg::Node* createShadowedScene(bool withTexture)
{
    osgShadow::ShadowedScene* shadowedScene = new osgShadow::ShadowedScene;
    shadowedScene->setReceivesShadowTraversalMask(ReceivesShadowTraversalMask);
    shadowedScene->setCastsShadowTraversalMask(CastsShadowTraversalMask);
    shadowedScene->setShadowTechnique(new osgShadow::ShadowMap());

    osg::LightSource* lightSource = new osg::LightSource;
    lightSource->getLight()->setPosition(osg::Vec4(0, 0.2, 1, 0));

    osg::ShapeDrawable * shape = new osg::ShapeDrawable(new
osg::Sphere(osg::Vec3(0.0f, 0.0f, 0.0f), 200.0));
    shape->setColor(osg::Vec4(0.8f, 0.8f, 0.8f, 1.0f));
    osg::Geode* geode = new osg::Geode();
    geode->addDrawable(shape);

    if (withTexture)
    {
        geode->getOrCreateStateSet()->setTextureAttributeAndModes( 0,
new osg::Texture2D(osgDB::readImageFile("Images/lz.rgb")),
osg::StateAttribute::ON);
    }
	
    shadowedScene->addChild(geode);
    shadowedScene->addChild(lightSource);

    return shadowedScene;
}

int main(int argc, char** argv)
{
	bool withTexture = false;

    osgViewer::Viewer viewer;
    viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
	viewer.setCameraManipulator(new osgGA::TrackballManipulator());
    viewer.realize();

    viewer.setSceneData(createShadowedScene(withTexture));
#if 1
	// Drawing the frame then assigning a new shadowed scene
	// seems to result in scene being rendered black.
	// Although, seems ok when texture is assigned to the object.
	viewer.frame();
	viewer.setSceneData(createShadowedScene(withTexture));
#endif

	viewer.run();

    return 0;
}

#endif


More information about the osg-users mailing list