生成Geometry

    // 由一组点集生成一张三角面片网格Geometry
    osg::Geometry* createTRIANGLESGeometry(MyMesh &mesh)
    {
        osg::ref_ptr< osg::Geometry > triGeom = new osg::Geometry();

        // 顶点坐标数组
        int vertexNum=mesh.vertex.size();
        osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array();
        triGeom->setVertexArray(vertices);

        // 颜色数组
        osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
        colors->push_back(osg::Vec4(0.0f,1.0f,0.0f,1.0f));
        triGeom->setColorArray(colors);
        triGeom->setColorBinding(osg::Geometry::BIND_OVERALL);

        // 法向量数组
        int normalNum=mesh.normal.size();
        osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array();
        triGeom->setNormalArray(normals);
        triGeom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);// 一个顶点对应一个法向量

        triGeom->addPrimitiveSet(
            new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLES,
            mesh.triangleNum*3,// 索引个数
            (unsigned short*)&mesh.index.at( 0 )));

        return triGeom.release();
    }

 

posted @ 2015-06-19 09:49  酷熊  阅读(844)  评论(0编辑  收藏  举报