osg学习笔记3 简单几何模型

时间:2023-03-09 15:21:04
osg学习笔记3 简单几何模型

osg::Geode (geometry node)

osg::Geode类表示场景中的渲染几何叶节点,它包含了渲染用的几何信息,没有子节点。

要绘制的几何数据保存在osg::Geode管理的一组osg::Drawable对象中。osg::Drawable是一个接口,它有很多实现类渲染模型,图像,文本到OpenGL管线。这些可渲染统称为drawables.

osg::Geode提供了几个方法来绑定和解绑drawables:

  1. addDrawable()
  2. removeDrawable(), removeDrawables()
  3. getDrawable()
  4. getNumDrawables()

渲染基本模型Shape

osg::ShapeDrawable继承自osg::Drawable。

setShape()方法通常分配和设置一个模型shape,如:

shapeDrawable->setShape( new osg::Box(osg::Vec3(1.0f, 0.0f, 0.0f), 10.0f, 10.0f, 5.0f) );

示例

#include <osg/ShapeDrawable>
#include <osg/Geode>
#include <osgViewer/Viewer> int main(int argc, char **argv)
{
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::ref_ptr<osg::Geode> root = new osg::Geode;
root->addDrawable(shape1.get());
root->addDrawable(shape2.get());
root->addDrawable(shape3.get()); osgViewer::Viewer viewer;
viewer.setSceneData(root.get());
return viewer.run();
}