|
本帖最后由 bdaabaa3b2a8 于 2012-4-12 16:40 编辑
我参照OSG light例子想写个实现车灯的程序,我看了论坛里的帖子,大体思路是这样的:我用Creator创建了一个圆柱体的模型(暂且用圆柱体模拟),模型的透明度设置为0.2,然后加入场景在加入聚光灯,程序如下:;但是,我没有得到灯光的效果。我自己感觉是没有求出模型的法线,可能还有别的问题,希望大家帮帮我看看哪里出问题了,最主要的是加载的模型节点各面的法线怎么求得。?十分感谢~~
osg::Node* createLights(osg::StateSet* rootStateSet)
{
osg::Group* lightGroup = new osg::Group;
// create a spot light.
osg:: Light* myLight1 = new osg:: Light;
myLight1->setLightNum(0);
myLight1->setPosition(osg::Vec4(0.0f,0.0f,0.0f,1.0f));
myLight1->setAmbient(osg::Vec4(1.0f,0.0f,0.0f,1.0f));
myLight1->setDiffuse(osg::Vec4(1.0f,0.0f,0.0f,1.0f));
myLight1->setSpotCutoff(20.0f);
myLight1->setSpotExponent(50.0f);
myLight1->setDirection(osg::Vec3(-1.0f,0.0f,0.0f));
osg:: LightSource* lightS1 = new osg:: LightSource;
lightS1->setLight(myLight1);
lightS1->setLocalStateSetModes(osg::StateAttribute::ON);
lightS1->setStateSetModes(*rootStateSet,osg::StateAttribute::ON);
lightGroup->addChild(lightS1);
return lightGroup;
}
void main()
{
osgViewer::Viewer viewer;
osg::Node* loadedModel=osgDB::readNodeFile("F:/Model/box.flt");
osg::ref_ptr<osg::Group> root = new osg::Group;
root->addChild(loadedModel);
// create statesets.
osg::StateSet* rootStateSet=new osg::StateSet;
rootStateSet->setMode( GL_LIGHTING, osg::StateAttribute::ON );
rootStateSet->setMode( GL_LIGHT0, osg::StateAttribute::ON );
rootStateSet->setMode(GL_CULL_FACE,osg::StateAttribute::ON);
root->setStateSet(rootStateSet);
root->addChild(createLights(rootStateSet));
viewer.setSceneData(root);
viewer.realize();
viewer.run();
} |
|