osg入门系列12-多边形分格

来源:互联网 发布:手机数据接口 type-c 编辑:程序博客网 时间:2024/04/29 00:46
#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 <osgUtil/Tessellator>

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

    osg::ref_ptr<osg::Geometry> geom = new osg::Geometry();

    geode->addDrawable(geom.get());

    const float wall[5][3] =
    {
        {2200.0f, 0.0f, 1130.0f},
        {2600.0f, 0.0f, 1130.0f},
        {2600.0f, 0.0f, 1340.0f},
        {2400.0f, 0.0f, 1440.0f},
        {2200.0f, 0.0f, 1340.0f}
    };

    const float door[4][3] =
    {
        {2360.0f, 0.0f, 1130.0f},
        {2440.0f, 0.0f, 1130.0f},
        {2440.0f, 0.0f, 1230.0f},
        {2360.0f, 0.0f, 1230.0f}
    };

    const float windows[8][3] =
    {
        {2240.0f, 0.0f, 1180.0f},
        {2330.0f, 0.0f, 1180.0f},
        {2330.0f, 0.0f, 1220.0f},
        {2240.0f, 0.0f, 1220.0f},

        {2460.0f, 0.0f, 1180.0f},
        {2560.0f, 0.0f, 1180.0f},
        {2560.0f, 0.0f, 1220.0f},
        {2460.0f, 0.0f, 1220.0f}
    };

    osg::ref_ptr<osg::Vec3Array> coords = new osg::Vec3Array();
    geom->setVertexArray(coords.get());

    osg::ref_ptr<osg::Vec3Array> normal = new osg::Vec3Array();
    normal->push_back(osg::Vec3(0.0f, -1.0f, 0.0f));
    geom->setNormalArray(normal.get());
    geom->setNormalBinding(osg::Geometry::BIND_OVERALL);


    for(int i =0 ; i< 5; i++)
    {
        coords->push_back(osg::Vec3(wall[i][0], wall[i][1], wall[i][2]));
    }

    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON, 0, 5));

    for(int i =0; i<4; i++)
    {
        coords->push_back(osg::Vec3(door[i][0], door[i][1], door[i][2]));
    }

    for (int i =0; i<8; i++)
    {
        coords->push_back(osg::Vec3(windows[i][0], windows[i][1], windows[i][2]));
    }

    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 5, 12));

    osg::ref_ptr<osgUtil::Tessellator> tscx = new osgUtil::Tessellator();
    tscx->setTessellationType(osgUtil::Tessellator::TESS_TYPE_GEOMETRY);
    tscx->setBoundaryOnly(false);
    tscx->setWindingType(osgUtil::Tessellator::TESS_WINDING_ODD);

    tscx->retessellatePolygons(*(geom.get()));

    return geode.get();

}

#include <iostream>
using namespace std;

int main()
{
    osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();

    osg::ref_ptr<osg::Group> root = new osg::Group();

    osg::ref_ptr<osg::Geode> geode = tesslatorGeometry();

    root->addChild(geode.get());

    osgUtil::Optimizer optimizer;
    optimizer.optimize(root.get());

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

    viewer->realize();

    viewer->run();
    return 0;
}

---


0 0
原创粉丝点击