osg入门系列11- 预订义几何体

来源:互联网 发布:网络法制知识竞赛答题 编辑:程序博客网 时间:2024/04/30 13:21
#include <osgViewer/Viewer>

#include <osg/Node>
#include <osg/Geode>
#include <osg/Group>
#include <osg/ShapeDrawable>

#include <osgDB/ReadFile>
#include <osgDB/WriteFile>

#include <osgUtil/Optimizer>

#include <iostream>
using namespace std;

osg::ref_ptr<osg::Geode> createShape()
{
    osg::ref_ptr<osg::Geode> geode = new osg::Geode();

    float radius = 0.8f;
    float height = 1.0f;

    osg::ref_ptr<osg::TessellationHints> hints = new osg::TessellationHints;
    hints->setDetailRatio(0.8f);

    geode->addDrawable(new osg::ShapeDrawable(
                           new osg::Sphere(
                               osg::Vec3(0.0f, 0.0f, 0.0f), radius), hints.get()));

    geode->addDrawable(new osg::ShapeDrawable(
                           new osg::Box(
                               osg::Vec3(2.0f, 0.0f, 0.0f), 2*radius), hints.get()));

    geode->addDrawable(new osg::ShapeDrawable(
                           new osg::Cone(
                               osg::Vec3(4.0f, 0.0f, 0.0f), radius, height), hints.get()));

    geode->addDrawable(new osg::ShapeDrawable(
                           new osg::Cylinder(
                               osg::Vec3(6.0f, 0.0f, 0.0f), radius, height), hints.get()));


    geode->addDrawable(new osg::ShapeDrawable(
                           new osg::Capsule(
                               osg::Vec3(8.0f, 0.0f, 0.0f), radius, height), hints.get()));

    return geode.get();
}

int main()
{
    //cout << "Hello World!" << endl;
    osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
    osg::ref_ptr<osg::Group> root = new osg::Group();
    root->addChild(createShape());
    osgUtil::Optimizer optimizer;

    optimizer.optimize(root.get());

    viewer->setSceneData(root.get());

    viewer->realize();

    viewer->run();

    return 0;
}

----


0 0
原创粉丝点击