1. 程式人生 > >8.osg中使用Tesselator分格化(三角剖分)

8.osg中使用Tesselator分格化(三角剖分)

在球的基礎上進行操作。

效果為與主視窗和子視窗新增一個邊框

        osg::ref_ptr<osg::Vec3Array>vertice2 = new osg::Vec3Array(8);

        //外邊界逆針

        (*vertice2)[0].set(1,1,0);

        (*vertice2)[1].set(w-1,1,0);

        (*vertice2)[2].set(w-1,h-1,0);

        (*vertice2)[3].set(1,h-1,0);

        //內邊界順時針

        (*vertice2)[4].set(100,100,0);

        (*vertice2)[5].set(100,h-100,0);

        (*vertice2)[6].set(w-100,h-100,0);

        (*vertice2)[7].set(w-100,100,0);

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

        geom->setVertexArray(vertice2);

        osg::Vec3Array* normals = newosg::Vec3Array;

        normals->push_back(osg::Vec3(0.0f,0.0f, 1.0f));

        geom->setNormalArray(normals);

        geom->setNormalBinding(osg::Geometry::BIND_OVERALL);

        osg::Vec4Array* colors = newosg::Vec4Array;

        colors->push_back(osg::Vec4(0.0f,0.0f,0.0f,1.0f));

        geom->setColorArray(colors);

        geom->setColorBinding(osg::Geometry::BIND_OVERALL);

        geom->addPrimitiveSet(newosg::DrawArrays(osg::PrimitiveSet::QUADS,0,vertice2->size()));

執行到此處顯然這兩個矩形會覆蓋整個螢幕,需要進行分格化操作。

分格化:

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_NONZERO);

tscx->retessellatePolygons(*geom);

geode->addDrawable(geom);

//TESS_WINDING_POSITIVE渲染環繞數為正的區域

//TESS_WINDING_NEGETIVE渲染環繞數為負的區域

//TESS_WINDING_ODD渲染環繞數為奇數的區域

//TESS_WINDING_NONZERO渲染環繞數不為0的區域

//TESS_WINDING_ABS_GEQ_TWO渲染環繞數絕對值大於2的區域

//逆時針為正,順時針為負

效果如下: