Hi,
Im trying to add shadows to my scene, code taken from osgshadow example but my
application crashes when it gets to viewer.renderingTraversals().
If I dont set the setShadowTechnique then my application does not crash, but i
obviously don't get shadows either. Ive tried each of the shadow techniques
without sucess.
Heres my code, can you see any mistakes?
Thank you.
Paul.
Code:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static int ReceivesShadowTraversalMask = 0x1;
static int CastsShadowTraversalMask = 0x2;
int main(int argc, char **argv)
{
// use an ArgumentParser object to manage the program arguments.
osg::ArgumentParser arguments(&argc, argv);
// construct the viewer.
osgViewer::CompositeViewer viewer(arguments);
// Turn on FSAA, makes the lines look better.
//osg::DisplaySettings::instance()->setNumMultiSamples( 4 );
osg::ref_ptr shadowedScene = new
osgShadow::ShadowedScene;
shadowedScene->setReceivesShadowTraversalMask(ReceivesShadowTraversalMask);
shadowedScene->setCastsShadowTraversalMask(CastsShadowTraversalMask);
osg::ref_ptr sm = new osgShadow::ShadowMap;
shadowedScene->setShadowTechnique(sm.get());
sm->setTextureSize(osg::Vec2s(1024, 1024));
//osg::ref_ptr< osg::Group > root = new osg::Group;
osg::ref_ptr< osg::Node > scene;
scene = osgDB::readNodeFile("cow.osg");
// set up the texture of the base.
osg::StateSet* stateset = new osg::StateSet();
osg::Image* image = osgDB::readImageFile("Images/lz.rgb");
if (image)
{
osg::Texture2D* texture = new osg::Texture2D;
texture->setImage(image);
stateset->setTextureAttributeAndModes(0, texture,
osg::StateAttribute::ON);
}
scene->setStateSet(stateset);
scene->setNodeMask(CastsShadowTraversalMask |
ReceivesShadowTraversalMask);
osg::Vec4 lightpos;
lightpos.set(0.5f, 0.25f, 0.8f, 0.0f);
osg::ref_ptr ls = new osg::LightSource;
ls->getLight()->setPosition(lightpos);
ls->getLight()->setAmbient(osg::Vec4(0.2, 0.2, 0.2, 1.0));
ls->getLight()->setDiffuse(osg::Vec4(0.8, 0.8, 0.8, 1.0));
osg::ref_ptr sceneTransform = new
osg::MatrixTransform();
osg::Matrix zTrans = osg::Matrix::translate(0.0f, 0.0f, 0.0f);
sceneTransform->setMatrix(zTrans);
sceneTransform->addChild(scene.get());
shadowedScene->addChild(sceneTransform);
osg::ref_ptr sceneTransform2 = new
osg::MatrixTransform();
osg::Matrix zTrans2 = osg::Matrix::translate(1.5f, 0.0f, 1.5f);
sceneTransform2->setMatrix(zTrans2);
sceneTransform2->addChild(scene.get());
sceneTransform->addChild(sceneTransform2);
shadowedScene->addChild(ls.get());
osgGA::TrackballManipulator* manipulator;
osgGA::TrackballManipulator* manipulator2;
// Create Editor View
{
osgViewer::Viewer* view = new osgViewer::Viewer();
viewer.addView(view);
osg::Camera* camera = view->getCamera();
camera->setViewport(new osg::Viewport(0, 0, 640, 480));
GLenum buffer = GL_BACK;
camera->setDrawBuffer(buffer);
camera->setReadBuffer(buffer);
camera->setClearColor(osg::Vec4(0.0, 0.0, 0.0, 1.0)) ;
camera->setCullingMode(camera->getCullingMode() &
~osg::CullStack::SMALL_FEATURE_CULLING);
view->setSceneData(shadowedScene.get());
manipulator = new osgGA::TrackballManipulator;
view->setCameraManipulator(manipulator);
manipulator->setHomePosition(osg::Vec3(0, -10, 0), osg::Vec3(0,
0, 0), osg::Vec3(0, 0, 5));
view->setUpViewInWindow(20, 20, 640, 480);
}
// Create Controls View
{
osgViewer::Viewer* view = new osgViewer::Viewer();
viewer.addView(view);
osg::Camera* camera = view->getCamera();
camera->setViewport(new osg::Viewport(0, 0, 640, 480));
GLenum buffer = GL_BACK;
camera->setDrawBuffer(buffer);
camera->setReadBuffer(buffer);
camera->setClearColor(osg::Vec4(0.0, 0.0, 0.0, 1.0)) ;
camera->setCullingMode(camera->getCullingMode() &
~osg::CullStack::SMALL_FEATURE_CULLING);
view->setSceneData(shadowedScene.get());
manipulator2 = new osgGA::TrackballManipulator;
view->setCameraManipulator(manipulator);
view->setUpViewInWindow(40, 40, 640, 480);
}
viewer.setThreadingModel(osgViewer::CompositeViewer::CullDrawThreadPerContext);
viewer.realize();
while (!viewer.done