osg::Drawable表示可绘制的物体的抽象接口,比如文字,基本图形。 可绘制的物体本身不能被加到场景中,要使用Geode将可绘制物体附加到其上。
这个其实之前的osgText::Text也属于Drawable的范围。还有一些其他的类继承自Drawable,但是由于我没用过,画出来也碍事,就没画了。
#include<osgViewer/Viewer>
int main(int argc, char **argv)
{
<span style="white-space:pre"> </span>osgViewer::Viewer viewer;
<span style="white-space:pre"> </span>//创建三角形的顶点数组
<span style="white-space:pre"> </span>osg::Vec3 myCoords[] =
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>osg::Vec3(-10, 0.0, 0.0),
<span style="white-space:pre"> </span>osg::Vec3(10, 0.0f, 0.0),
<span style="white-space:pre"> </span>osg::Vec3(0.0, 0.0f, 10.0)
<span style="white-space:pre"> </span>};
<span style="white-space:pre"> </span>int numCoords = sizeof(myCoords) / sizeof(osg::Vec3);
<span style="white-space:pre"> </span>osg::Vec3Array* vertices = new osg::Vec3Array(numCoords, myCoords);
<span style="white-space:pre"> </span>//创建三角形drawable
<span style="white-space:pre"> </span>osg::Geometry* triangleGeom = new osg::Geometry();
<span style="white-space:pre"> </span>// pass the created vertex array to the points geometry object.
<span style="white-space:pre"> </span>triangleGeom->setVertexArray(vertices);
<span style="white-space:pre"> </span>//使用这些顶点画三角形
<span style="white-space:pre"> </span>triangleGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES, 0, numCoords));
<span style="white-space:pre"> </span>osg::Geode *geode = new osg::Geode;
<span style="white-space:pre"> </span>geode->addDrawable(triangleGeom);
<span style="white-space:pre"> </span>viewer.setSceneData(geode);
<span style="white-space:pre"> </span>return viewer.run();
}