OSG灯光

      在场景中增加光源是很简单的,光源在OSG中被当做渲染的节点。必须注意的是物体有发现才能对灯光进行响应,灯光对没有法线的物体是没有效果的。

     在场景中加入灯光的顺序一般如下,首先创建一个osg::Light类,然后指定灯光的位置,以及相关的衰减参数,没有衰减的灯光将会照亮场景中的每一个角落,没有纹理的模型全部渲染成灯光的颜色;创建osg::Light之后,创建一个LihghtSource类对象,将Light对象加入其中就可以实现灯光效果了。

#include <Windows.h>
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osgViewer/ViewerEventHandlers>
#include <osg/Light>
#include <osg/LightSource>
#include <osg/ShapeDrawable>
//创建一个球体,球体位置于光源位置重合
osg::ref_ptr<osg::Node> CreateSphere(osg::Vec3 position)
{
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(position,0.2)));
return geode;
}
int main(int argc,char** argv)
{
osgViewer::Viewer view;
osg::Group* root = new osg::Group();
root->addChild(osgDB::readNodeFile("glider.osg"));


//创建一个Light对象
osg::ref_ptr<osg::Light> topLight = new osg::Light;
topLight->setLightNum(0);   //Light对象设置一个ID
topLight->setPosition(osg::Vec4(0,0,1.5,1.0));  //位置
topLight->setAmbient(osg::Vec4(1.0,1.0,1.0,1.0));//光强
topLight->setDiffuse(osg::Vec4(1.0,1.0,1.0,1.0));
topLight->setConstantAttenuation(0.1);//光的衰减值
topLight->setLinearAttenuation(0.1);//设置线性衰减
topLight->setQuadraticAttenuation(0.1);
osg::ref_ptr<osg::LightSource> ls = new osg::LightSource;
ls->setLight(topLight);
ls->setLocalStateSetModes(osg::StateAttribute::ON);
root->addChild(ls);
root->addChild(CreateSphere(osg::Vec3(0,0,1.5)));
view.setSceneData(root);
view.run();
}




  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值