Create Sun light source in OSG

独自空忆成欢 提交于 2019-12-06 08:29:08

问题


I need to set a point Source above my landscape in OpenSceneGraph that will act like a sun. I already know how to set up the light and it can be done in this fashion:

//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----------------

And this will produce a landscape that looks like this:

The Landscape with light above (Light is indicated by the sphere)

This light source doesn't really seem to make a difference to the landscape though. The question is what sort of light settings (ie. ambiance, diffusion etc) are needed to make a Sun emulating light.


回答1:


For what it's worth, the OSG forum/mailing list is usually pretty good about answering questions: http://forum.openscenegraph.org/

To try to answer your question here - it depends on the properties of the material you are trying to light.

I've found that materials on some models I load will only react to one particular of the 3 light types (specifically, some model are specular-only), so I just turn on all 3:

osg::Light *light = new osg::Light;
light->setAmbient(osg::Vec4(1.0,1.0,1.0,1.0));
light->setDiffuse(osg::Vec4(1.0,1.0,1.0,1.0));
light->setSpecular(osg::Vec4(1,1,1,1));  // some examples don't have this one

For your case, you might alternatively be able to redefine the ambient and/or diffuse properties of your terrain.



来源:https://stackoverflow.com/questions/10343643/create-sun-light-source-in-osg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!