创建图元大致结构
- osg::Geode
- osg::ShapeDrawable
- osg::Color
- osg::Material
- osg::Texture2D
- osg::Image
- osg::ShapeDrawable
创建osg::Geode
的实例
osg::ref_ptr<osg::Geode> geode=new osg::Geode;
Image的读取可以使用以下模板例子
osg::ref_ptr<osg::Texture2D> t2d=new osg::Texture2D;
osg::ref_ptr<osg::Image> image=osgDB::readImageFile("imagepath");
if(image.valid()){
t2d->setImage(0,image.get());
}
最后将osg::Texture2D
添加到osg::Geode
的属性中
geode->getOrCreateStateSet()->setTextureAttributeAndModes(0,t2d.get(),osg::StateAttribute::ON);
注 * 以上省略了向geode中添加osg::shapedrawable的操作
关于自定义图形形状
- 需要用到
osg::Geometry
,将设计好的osg::Geometry
添加到osg::Geode
中。 osg::Geode
继承自osg::Node
可以自定义形状
- 顶点位置
- 顶点颜色
- 形状的法线
- 形状的线宽
osg::ref_ptr<osg::Vec3Array> coord = new osg::Vec3Array;
osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array;
osg::ref_ptr<osg::LineWidth> width = new osg::LineWidth;
其中,形状的线宽需要在osg::Geode
中设置,而非osg::Geometry
。
本文为博主学习OSG时的自我总结和记录,如出现错误的地方,还请留言指正,共同进步