一开始,怎么也写不进obj模型里面,而可以写到Ive和.osg里面,
后来查了下,obj只支持材质,
这样就好说了,
加上以下几句话就OK了
osg::ref_ptrosg::StateSet state = geode->getOrCreateStateSet();
//创建材质对象
osg::ref_ptrosg::Material mat = new osg::Material;
//设置正面散射颜色
mat->setDiffuse(osg::Material::FRONT, color);
state->setAttribute(mat.get());
即
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
osg::ref_ptr<osg::Geometry> geoTop = new osg::Geometry;
osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array;
osg::ref_ptr<osg::Vec3Array> vecs= new osg::Vec3Array;
for (int m = 0;m<vecs.size();m++)
{
vertexArray->push_back(vecs.at(m));
normals->push_back(osg::Vec3d(0, 0, 1));
}
//面
geoTop->setVertexArray(vertexArray.get());
geoTop->setNormalArray(normals.get(), osg::Array::BIND_OVERALL);
geoTop->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_STRIP, 0, vertexArray->size()));
geode->addDrawable(geoTop.get());
osg::ref_ptr<osg::StateSet> state = geode->getOrCreateStateSet();
//创建材质对象
osg::ref_ptr<osg::Material> mat = new osg::Material;
//设置正面散射颜色
mat->setDiffuse(osg::Material::FRONT, color);
state->setAttribute(mat.get());
return geode.release();
把这个geode可以写到obj里面
osgDB::writeNodeFile(*geode, “d:/test/test.obj”);
这时发现,相应的mtl文件也不为空
再读这个obj,就有颜色了