通过设置不同颜色的光源,使得物体表面反射不同的颜色,从而改变观察物体的颜色
#include<Windows.h>
#include<osgViewer/Viewer>
#include<osgDB/ReadFile>
#include<osgViewer/ViewerEventHandlers>
void createLight()
{
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
viewer->addEventHandler(new osgViewer::WindowSizeHandler());
osg::ref_ptr<osg::Group> root = new osg::Group;
//开启光照
osg::ref_ptr<osg::StateSet> state = root->getOrCreateStateSet();
//设置接受光照
state->setMode(GL_LIGHTING, osg::StateAttribute::ON);
//设置开启0 1光源
state->setMode(GL_LIGHT0, osg::StateAttribute::ON);
state->setMode(GL_LIGHT1, osg::StateAttribute::ON);
//创建光源1
osg::ref_ptr<osg::LightSource> ls = new osg::LightSource();
//创建光对象1
osg::ref_ptr<osg::Light> lt = new osg::Light();
lt->setLightNum(0);
//设置方向
lt->setDirection(osg::Vec3(0.0, 1.0, 0.0));
//设置位置
lt->setPosition(osg::V