osg与opengl管理数据存储的区别
- opengl
- 即时模式:glBegin()和glEnd()之间的顶点
- 顶点数组模式:数组在内存
- 显示列表模式:顶点和像素数据编译后拷贝入显存,重复使用
- 顶点缓冲数组模式:数组在显存
- osg
- 顶点数组模式
- 显示列表模式
快速绘制
示例
#include <osg/ShapeDrawable>
#include <osg/Geode>
#include <osgViewer/Viewer>
int main(int argc, char** argv)
{
//osg::drawable+osg::shape
osg::ref_ptr<osg::ShapeDrawable> shape1 = new osg::ShapeDrawable;
shape1->setShape(new osg::Box(osg::Vec3(-3.0f, 0.0f, 0.0f),
2.0f, 2.0f, 1.0f));
osg::ref_ptr<osg::ShapeDrawable> shape2 = new osg::ShapeDrawable;
shape2->setShape(new osg::Sphere(osg::Vec3(3.0f, 0.0f, 0.0f),
1.0f));
shape2->setColor(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f));
osg::ref_ptr<osg::ShapeDrawable> shape3 = new osg::ShapeDrawable;
shape3->setShape(new osg::Cone(osg::Vec3(0.0f, 0.0f, 0.0f),
1.0f, 1.0f));
shape3->setColor(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f));
//osg::Geode
osg::ref_ptr<osg::Geode> root = new osg::Geode;
root->addDrawable(shape1.get());
root->addDrawable(shape2.get());
root->addDrawable(shape3.get());
//osg::viewer
osgViewer::Viewer viewer;
viewer.setSceneData(root.get());
return viewer.run();
}
代码分析
osg::Array
- 用于支持顶点数组
- 子类继承vector模板,支持push_back/pop_back/size操作
- osg::Vec2Array
- osg::Vec3Array
- osg::UIntArray
- 基本数据类型为osg::Vec2/osg::Vec3,etc
- 由智能指针申请
osg::Geometry
- 继承自osg::Drawable
- 封装opengl的顶点数组绘制
顶点数组管理
顶点数组辅助函数
-
setColorBinding
-
setNormalBinding
-
setVertexAttribBinding
-
setFogCoordBinding
-
setSecondaryColorBinding
geom->setColorBinding( osg::Geometry::BIND_PER_VERTEX );
geom->setColorBinding( osg::Geometry::BIND_OVERALL );
组元管理
- addPrimitiveSet
- removePrimitiveSet
- getPrimitiveSet
- getNumPrimitiveSets
组元
- osg::PrimitiveSet
- 组元不能直接实例化,但是子类封装了opengl的glDrawArrays和glDrawElements
- 子类osg::DrawArrays和osg::DrawElementsUInt生成一系列组元
- osg::DrawArrays不用智能指针申请
- osg::DrawElem