osg.DrawElementsUByte
类用于在OpenSceneGraph中绘制网格模型中的顶点。
这个类是 osg.DrawElements
类的子类,用于绘制使用 unsigned char 数组类型定义的顶点索引。
osg::ref_ptr<osg::DrawElementsUByte> drawElements = new osg::DrawElementsUByte(mode, count);
mode
:渲染模式,可以是以下值之一:
GL_POINTS
:绘制单独的点GL_LINES
:绘制两个点之间的直线GL_LINE_STRIP
:绘制连接所有点的直线GL_TRIANGLES
:绘制三角形GL_TRIANGLE_STRIP
:绘制连接所有三角形的带状三角形GL_TRIANGLE_FAN
:绘制从一个点开始连接所有三角形的扇形三角形count
:顶点索引数目。
#include <osg/Geode>
#include <osg/Geometry>
int main()
{
// 创建顶点坐标
osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
vertices->push_back(osg::Vec3(-1.0f,0.0f,0.0f));
vertices->push_back(osg::Vec3(0.0f,1.0f,0.0f));
vertices->push_back(osg::Vec3(1.0f,0.0f,0.0f));
// 创建绘制元素
osg::ref_ptr<osg::DrawElementsUByte> drawElements = new osg::DrawElementsUByte(GL_TRIANGLES, 3);
(*drawElements)[0] = 0;
(*drawElements)[1] = 1;
(*drawElements)[2] = 2;
// 创建几何体并添加顶点和绘制元素
osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry;
geometry->setVertexArray(vertices.get());
geometry->addPrimitiveSet(drawElements.get());
// 创建节点并将几何体添加至节点
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
geode->addDrawable(geometry.get());
// 创建渲染窗口并渲染场景
osgViewer::Viewer viewer;
viewer.setSceneData(geode.get());
return viewer.run();
}