【问题标题】:Create Sun light source in OSG在 OSG 中创建太阳光源
【发布时间】:2012-05-07 18:56:27
【问题描述】:

我需要在 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----------------

这将产生如下所示的景观:

上面有灯光的风景(灯光由球体表示)

不过,这种光源似乎并没有真正对景观产生影响。 问题是需要什么样的灯光设置(即氛围、漫射等)来制作模拟太阳光。

【问题讨论】:

  • 定向光不是对太阳更有意义吗?
  • 它是定向的,方向是从景观上方非常高的地方直接向下。

标签: c++ opengl lighting openscenegraph


【解决方案1】:

就其价值而言,OSG 论坛/邮件列表通常非常适合回答问题: http://forum.openscenegraph.org/

在这里尝试回答您的问题 - 这取决于您尝试点亮的材料的属性。

我发现我加载的某些模型上的材质只会对 3 种灯光类型中的一种做出反应(具体来说,某些模型是仅镜面反射的),所以我只打开所有 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

对于您的情况,您也可以重新定义地形的环境和/或漫反射属性。

【讨论】:

  • 听起来是一个合理的想法,但由于他的灯光靠近场景,这使它变得非常明亮。我希望有一个更原生的 osg 解决方案。
  • 当然,我的示例中的灯光一直向上,但如果您不希望灯光那么亮,则始终可以使用小于 1 的值。或者打开其他一些衰减因子。
猜你喜欢
  • 2012-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多