|
本帖最后由 wanggang8826 于 2012-8-10 15:15 编辑
MatrixTransform是怎么影响法向量的?
下面的函数生成一个带有材质的Geode,然后我使用MatrixTransform放置一个Geode,为什么位置是对的,但是材质显示不对呢?我想是Geometry的法向量没有被矩阵影响,怎么解决?
osg::ref_ptr< osg::MatrixTransform > mt = new osg::MatrixTransform( m );// m是变换矩阵
mt->addChild( createTRIANGLESGeode( ) );
root->addChild( mt.get() );
osg::Geode* createTRIANGLESGeode( ) const
{
osg::ref_ptr< osg::Geode > geode = new osg::Geode();
osg::ref_ptr< osg::Geometry > triGeom = new osg::Geometry();
// 顶点坐标数组
osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array( 顶点个数,顶点数组地址);
triGeom->setVertexArray(vertices);
// 颜色数组
osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
colors->push_back(osg::Vec4(0.0f,1.0f,0.0f,1.0f));
triGeom->setColorArray(colors);
triGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
// 法向量数组
osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array( 法向量个数,法向量数组地址 );
triGeom->setNormalArray(normals);
triGeom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);// 一个顶点对应一个法向量
triGeom->addPrimitiveSet(
new osg:rawElementsUShort(osg:rimitiveSet::TRIANGLES, 索引个数 ,索引数组地址));
// 材质
osg::ref_ptr<osg::StateSet > ss = geode->getOrCreateStateSet();
osg::ref_ptr<osg::Material> mat=new osg::Material;
mat->setDiffuse(osg::Material::FRONT_AND_BACK, diffuse);
mat->setAmbient(osg::Material::FRONT_AND_BACK, ambient);
mat->setSpecular(osg::Material::FRONT_AND_BACK, specular);
mat->setEmission(osg::Material::FRONT_AND_BACK, emissive);
mat->setShininess(osg::Material::FRONT_AND_BACK, material.power);
ss->setAttribute(mat.get());
//ss->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
triGeom->setStateSet( ss );
}
}
geode->addDrawable(triGeom);
return geode.release();
}
|
|