在导入飞机模型时,发现模型是黑咕隆咚一片,然后翻阅资料,找到三个可能性。
(一)几何模型没有法线导致
利用osgUtil::SmoothingVisitor自动生成法线
#include <osgUtil/SmoothingVisitor>
osg::ref_ptr<osg::Geometry> houseRoof = new osg::Geometry;
osgUtil::SmoothingVisitor smv;
smv.smooth(model);
(二)模型放大缩小导致
放大缩小后,法线不会跟着变化,因此需要进行设置,如下:
//添加模型
osg::Node* model = osgDB::readNodeFile("cessna.osg");
osg::MatrixTransform* mtFlySelf = new osg::MatrixTransform;//用于调整模型本身的姿态
mtFlySelf->addChild(model);
mtFlySelf->getOrCreateStateSet()->setMode(GL_RESCALE_NORMAL, osg::StateAttribute::ON);
(三)没有给模型添加光源导致
之前我一直以为模型的光源是跟着地表的灯光,默认添加的,现在发现并不是。可以添加八个光源,我添加了两个。
mtFlySelf->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::ON);
mtFlySelf->getOrCreateStateSet()->setMode(GL_LIGHT0,osg::StateAttribute::ON);
mtFlySelf->getOrCreateStateSet()->setMode(GL_LIGHT1,osg::StateAttribute::ON);