I have taken the code I think I need from the osgShadow example and tried to
add shadows to my existing application. The problem is that the shadow
map displayed in the debug window is just a gray-ish circle centered on a
white-ish background, nothing at all what I expected and no details that
look anything like the objects in my scene. This remains the same
regardless of whare I put my light source. Any suggestions?
My code is as follows:
W
osgViewer::CompositeViewer viewer;
osgViewer::View* view = new osgViewer::View;
viewer.addView(view);
view->setUpViewInWindow(50, 50, currentWidth, currentHeight, 0);
osg::ref_ptr<osgShadow::ShadowedScene> shadowedScene = new
osgShadow::ShadowedScene;
shadowedScene->setReceivesShadowTraversalMask(ReceivesShadowTraversalMask);
shadowedScene->setCastsShadowTraversalMask(CastsShadowTraversalMask);
osg::ref_ptr<osgShadow::ShadowMap> sm = new osgShadow::ShadowMap;
shadowedScene->setShadowTechnique(sm.get());
int mapres = 1024;
sm->setTextureSize(osg::Vec2s(mapres,mapres));
m_World = new osg::Group;
// add some things to m_World.. Removed for brevity sake
// get the bounds of the model.
osg::ComputeBoundsVisitor cbbv;
m_World->accept(cbbv);
osg::BoundingBox bb = cbbv.getBoundingBox();
osg::Vec4 lightpos;
viewer.getView(0)->getEventHandlers().push_front(new
osgViewer::StatsHandler);
viewer.getView(0)->getEventHandlers().push_front(mykey);
std::vector<osg::Camera*> cameras;
viewer.getCameras(cameras);
cameras[0]->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR
);
cameras[0]->setProjectionMatrixAsPerspective(40.0, 1.333333333, 1.0,
SKYDOME_RANGE*1.1f);
bool postionalLight = true;
if (postionalLight)
{
lightpos.set(bb.center().x(), bb.center().y(), bb.zMax() +
bb.radius()*2.0f ,1.0f);
}
else
{
lightpos.set(0.5f,0.25f,0.8f,0.0f);
}
osg::ref_ptr<osg::LightSource> ls = new osg::LightSource;
ls->getLight()->setPosition(lightpos);
shadowedScene->addChild((osg::Node*)m_World);
shadowedScene->addChild(ls.get());
bool updateLightPosition = true;
viewer.realize();
viewer.getView(0)->setSceneData(shadowedScene.get());
bool debugHUD = true;
if(debugHUD)
{
osgViewer::Viewer::Windows windows;
viewer.getWindows(windows);
if (windows.empty()) return 1;
osgShadow::ShadowMap* sm =
dynamic_cast<osgShadow::ShadowMap*>(shadowedScene->getShadowTechnique());
if( sm ) {
osg::ref_ptr<osg::Camera> hudCamera = sm->makeDebugHUD();
// set up cameras to rendering on the first window available.
hudCamera->setGraphicsContext(windows[0]);
hudCamera->setViewport(0,0,windows[0]->getTraits()->width,
windows[0]->getTraits()->height);
viewer.getView(0)->addSlave(hudCamera.get(), false);
}
}
// display the help
key->showHelp();
while( !viewer.done() )
{
cameras[0]->setViewMatrix(key->getViewMat());
if (updateLightPosition)
{
float t = viewer.getFrameStamp()->getSimulationTime();
if (postionalLight)
{
lightpos.set(bb.center().x()+sinf(t)*bb.radius(),
bb.center().y() + cosf(t)*bb.radius(), bb.zMax() + bb.radius()*3.0f ,1.0f);
}
else
{
lightpos.set(sinf(t),cosf(t),1.0f,0.0f);
}
ls->getLight()->setPosition(lightpos);
osg::Vec3f lightDir(-lightpos.x(),-lightpos.y(),-lightpos.z());
lightDir.normalize();
ls->getLight()->setDirection(lightDir);
}
viewer.frame();
}
I also must add that I am using my own shaders and have reproduced the
shadowmap shader code as follows:
uniform sampler2D osgShadow_baseTexture;
uniform sampler2DShadow osgShadow_shadowTexture;
uniform vec2 osgShadow_ambientBias;
vec4 myCalculations()
{
// Details Omitted;
}
void main (void)
{
// My shader code details omitted
// shadow mapping
vec4 color = gl_Color * texture2D( osgShadow_baseTexture,
gl_TexCoord[0].xy );
gl_FragColor = color * (osgShadow_ambientBias.x + shadow2DProj(
osgShadow_shadowTexture, gl_TexCoord[1] ) * osgShadow_ambientBias.y);
}
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org