4

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.

Bart
  • 19,692
  • 7
  • 68
  • 77
Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175

1 Answers1

4

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.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Ruan Caiman
  • 881
  • 5
  • 5
  • Sounds like a reasonable idea, but because of the proximity of he light to the scene this makes it much to bright. I was hoping for a more native osg solution. – Fantastic Mr Fox May 03 '12 at 03:51
  • Sure, my example has the lights all the way up, but you could always use values less than 1 if you don't want the lights as bright. Or turn on some of the other attenuation factors. – Ruan Caiman May 03 '12 at 13:38