Screen当光源非常靠近球体时,会出现第二个阴影,否则一切都会按原样运行......

资源:

#define ptr(x,y) osg::ref_ptr <x> y = new x

添加光源:

ptr(osg::LightSource, lightSource);
lightSource->getLight()->setPosition(v4(0.0f, -8.0f, 8.0f, 1.0f));
lightSource->getLight()->setAmbient(v4(0.0f, 0.0f, 0.0f, 1.0f));
lightSource->getLight()->setDiffuse(v4(0.8f, 0.8f, 0.8f, 1.0f));
lightSource->getLight()->setLightNum(0);

场景创作:

osg::ref_ptr <osg::Geode> grass = createTexture2DGeode(-50.0f, -50.0f, 0.0f, 100.0f, 100.0f, 0.0f, osg::Texture::REPEAT, 20.0f);

ptr(osg::ShapeDrawable, sphere);
sphere->setShape(new osg::Sphere(v3(0.0f, 0.0f, 5.0f), 5.0f));
sphere->setColor(v4(0.0f, 0.0f, 1.0f, 1.0f));

ptr(osgShadow::ShadowedScene, root);

osg::ref_ptr <osgShadow::ShadowSettings > settings = root->getShadowSettings();`
settings->setReceivesShadowTraversalMask(RECEIVE_SHADOW);
settings->setCastsShadowTraversalMask(CAST_SHADOW);

ptr(osgShadow::SoftShadowMap, softShadowMap);

root->setShadowTechnique(softShadowMap);
root->addChild(lightSource);
root->addChild(sphere);
root->addChild(grass);

纹理创建:

osg::Geode *createTexture2DGeode(float x, float y, float z, float width, float height, float high, osg::Texture::WrapMode wrapMode, float count)
{
    osg::ref_ptr <osg::Geometry> quad = osg::createTexturedQuadGeometry(v3(x, y, z), v3(width, 0.0f, 0.0f), v3(0.0f, height, high), count, count);

    osg::ref_ptr <osg::Image> image = osgDB::readImageFile("images/grass.png");

    osg::ref_ptr <osg::Texture2D> texture = new osg::Texture2D(image);
    texture->setWrap(osg::Texture::WRAP_S, wrapMode);
    texture->setWrap(osg::Texture::WRAP_T, wrapMode);

    osg::Geode *geode = new osg::Geode;
    geode->addDrawable(quad);
    geode->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);

    return geode;
}