我需要在 OpenSceneGraph 中的景观上方设置一个点 Source,它的作用就像太阳一样。我已经知道如何设置灯光,可以通过以下方式完成:
//LIGHT CODE ------------------------
osg::ref_ptr<osg::Group> lightGroup (new osg::Group);
osg::ref_ptr<osg::StateSet> lightSS (root->getOrCreateStateSet());
osg::ref_ptr<osg::LightSource> lightSource1 = new osg::LightSource;
osg::ref_ptr<osg::LightSource> lightSource2 = new osg::LightSource;
// create a local light.
float xCenter = tree->getRoot()->getXCenter();
float yCenter = tree->getRoot()->getYCenter();
osg::Vec4f lightPosition (osg::Vec4f(xCenter, yCenter,75.0,1.0f));
osg::ref_ptr<osg::Light> myLight = new osg::Light;
myLight->setLightNum(1);
myLight->setPosition(lightPosition);
myLight->setAmbient(osg::Vec4(0.8f,0.8f,0.8f,1.0f));
myLight->setDiffuse(osg::Vec4(0.1f,0.4f,0.1f,1.0f));
myLight->setConstantAttenuation(1.0f);
myLight->setDirection(osg::Vec3(0.0f, 0.0f, -1.0f));
lightSource1->setLight(myLight.get());
lightSource1->setLocalStateSetModes(osg::StateAttribute::ON);
lightSource1->setStateSetModes(*lightSS,osg::StateAttribute::ON);
//osg::StateSet* lightSS (lightGroup->getOrCreateStateSet());
lightGroup->addChild(lightSource1.get());
//Light markers: small spheres
osg::ref_ptr<osg::Geode> lightMarkerGeode (new osg::Geode);
lightMarkerGeode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3f(xCenter,yCenter,75),10.0f)));
//Tuto 9: lighting code
root->addChild(lightGroup.get());
//Tuto 9: Adding the light marker geode
root->addChild(lightMarkerGeode.get());
//LIGHTCODE END----------------
这将产生一个看起来像这样的景观:
上面有灯光的风景(灯光由球体表示)
不过,这种光源似乎并没有真正对景观产生影响。问题是需要什么样的灯光设置(即氛围、漫射等)来制作模拟太阳光。