OpenSceneGraph的osg.MultiDrawElementsIndirectUByte
类实现了基于GPU缓冲区的多次绘制操作。该类使用OpenGL的glMultiDrawElementsIndirect
函数,对指定的几何图元进行批量绘制,从而实现更高效的绘制操作。
osg::ref_ptr<osg::DrawElements> drawElements;
osg::ref_ptr<osg::DrawElementsUByte> primitiveSet;
unsigned int drawCount;
osg::Vec3Array *vertices;
osg::Vec3Array *normals;
osg::DrawElementsUByte::ElementBufferType buffer;
osg::GLBufferObject::Extensions *ext;
osg::MultiDrawElementsIndirectUByte::Vec4BufferType drawCommands;
std::vector<unsigned char> vertexBuffer;
osg::BufferObject *vertexBufferObject;
std::vector<unsigned char> indexBuffer;
osg::BufferObject *indexBufferObject;
osg::VertexBufferObject::Extensions *vboExt;
osg::ElementBufferObject::Extensions *eboExt;
bool useVertexArrayObject;
osg::VertexArrayState *vas;
float lineWidth;
unsigned int numInstances;
下面列出了osg.MultiDrawElementsIndirectUByte
的参数及其说明:
drawElements
: 绘制元素列表指针。primitiveSet
: 几何图元指针。drawCount
: 要绘制的几何图元的数量。vertices
: 顶点数组指针。normals
: 法向量数组指针。buffer
: 缓冲类型。ext
: 缓冲扩展。drawCommands
: 绘制命令数组。vertexBuffer
: 顶点缓冲区数组。vertexBufferObject
: 顶点缓冲区对象。indexBuffer
: 索引缓冲区数组。indexBufferObject
: 索引缓冲区对象。vboExt
: 顶点缓冲区对象扩展。eboExt
: 索引缓冲区对象扩展。useVertexArrayObject
: 是否使用顶点数组对象。vas
: 顶点数组状态。lineWidth
: 线宽。numInstances
: 实例个数。#include <osg/MultiDrawElementsIndirectUByte>
osg::ref_ptr<osg::DrawElements> drawElements = new osg::DrawElementsUByte(GL_TRIANGLES);
drawElements->push_back(0); drawElements->push_back(1); drawElements->push_back(2);
drawElements->push_back(2); drawElements->push_back(1); drawElements->push_back(3);
osg::ref_ptr<osg::DrawElementsUByte> primitiveSet = new osg::DrawElementsUByte(GL_TRIANGLES);
primitiveSet->push_back(0); primitiveSet->push_back(1); primitiveSet->push_back(2);
primitiveSet->push_back(2); primitiveSet->push_back(1); primitiveSet->push_back(3);
unsigned int drawCount = 2;
osg::Vec3Array *vertices = new osg::Vec3Array;
vertices->push_back(osg::Vec3(0,0,0));
vertices->push_back(osg::Vec3(1,0,0));
vertices->push_back(osg::Vec3(0,1,0));
vertices->push_back(osg::Vec3(1,1,0));
osg::Vec3Array *normals = new osg::Vec3Array;
normals->push_back(osg::Vec3(0,0,1));
osg::DrawElementsUByte::ElementBufferType buffer = osg::DrawElementsUByte::ELEMENT_BUFFER_OBJECT;
osg::GLBufferObject::Extensions *ext = new osg::GLBufferObject::Extensions;
osg::MultiDrawElementsIndirectUByte::Vec4BufferType drawCommands;
drawCommands.push_back(osg::Vec4u(6, 1, 0, 0));
drawCommands.push_back(osg::Vec4u(6, 1, 6, 0));
std::vector<unsigned char> vertexBuffer;
osg::BufferObject *vertexBufferObject = NULL;
std::vector<unsigned char> indexBuffer;
osg::BufferObject *indexBufferObject = NULL;
osg::VertexBufferObject::Extensions *vboExt = new osg::VertexBufferObject::Extensions;
osg::ElementBufferObject::Extensions *eboExt = new osg::ElementBufferObject::Extensions;
bool useVertexArrayObject = false;
osg::VertexArrayState *vas = NULL;
float lineWidth = 1.0f;
unsigned int numInstances = 1;
osg::MultiDrawElementsIndirectUByte *multiDraw = new osg::MultiDrawElementsIndirectUByte(drawElements, primitiveSet, drawCount, vertices, normals, buffer, ext, drawCommands,
vertexBuffer, vertexBufferObject, indexBuffer, indexBufferObject,
vboExt, eboExt, useVertexArrayObject, vas, lineWidth, numInstances);