osg三角带绘制

来源:互联网 发布:mysql desc 命令 编辑:程序博客网 时间:2024/04/28 15:41
#include <osgViewer/Viewer>#include <osgViewer/ViewerEventHandlers>#include <osg/Node>#include <osg/Group>#include <osgDB/ReadFile>#include <osgDB/WriteFile>#include <osgGA/StateSetManipulator>#include <osgUtil/Optimizer>#include <osgUtil/TriStripVisitor>osg::ref_ptr<osg::Geometry> createQuad(){    //创建一个叶节点对象    osg::ref_ptr<osg::Geode> geode = new osg::Geode() ;    //创建一个几何体对象    osg::ref_ptr<osg::Geometry> geom = new osg::Geometry() ;    //创建顶点数组,注意顶点数组是逆时针添加的    osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array() ;    //添加数据    v->push_back(osg::Vec3(0.0f , 0.0f , 0.0f)) ;    v->push_back(osg::Vec3(1.0f , 0.0f , 0.0f)) ;    v->push_back(osg::Vec3(1.0f , 0.0f , 1.0f)) ;    v->push_back(osg::Vec3(0.0f , 0.0f , 1.0f)) ;    geom->setVertexArray(v.get()) ;    //创建纹理坐标    osg::ref_ptr<osg::Vec2Array> vt = new osg::Vec2Array() ;    //添加数据    vt->push_back(osg::Vec2(0.0f , 0.0f)) ;    vt->push_back(osg::Vec2(1.0f , 0.0f)) ;    vt->push_back(osg::Vec2(1.0f , 1.0f)) ;    vt->push_back(osg::Vec2(0.0f , 1.0f)) ;    //设置纹理坐标    geom->setTexCoordArray(0 , vt.get()) ;    //创建颜色数组    osg::ref_ptr<osg::Vec4Array> vc = new osg::Vec4Array() ;    vc->push_back(osg::Vec4(1.0f , 0.0f , 0.0f , 1.0f)) ;    vc->push_back(osg::Vec4(0.0f , 1.0f , 0.0f , 1.0f)) ;    vc->push_back(osg::Vec4(0.0f , 0.0f , 1.0f , 1.0f)) ;    vc->push_back(osg::Vec4(1.0f , 1.0f , 0.0f , 1.0f)) ;    //设置颜色数组    geom->setColorArray(vc.get()) ;    //绑定方式为每个顶点    geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX) ;    //创建法线数组    osg::ref_ptr<osg::Vec3Array> nc = new osg::Vec3Array() ;    nc->push_back(osg::Vec3(0.0f , -1.0f , 0.0f)) ;    //设置法线数组    geom->setNormalArray(nc.get()) ;    //设置法线的绑定方式为全部顶点    geom->setNormalBinding(osg::Geometry::BIND_OVERALL) ;    //添加图元,绘图基元为四边形    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS , 0 , 4)) ;    return geom.get() ;}int main(){    osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer() ;    // 方便查看在多边形之间切换,以查看三角网    viewer->addEventHandler(new osgGA::StateSetManipulator(viewer->getCamera()->getOrCreateStateSet())) ;    osg::ref_ptr<osg::Group> root = new osg::Group() ;    //创建一个几何体对象    osg::ref_ptr<osg::Geometry> geometry = createQuad() ;    //对几何体进行条带化    osgUtil::TriStripVisitor stripper ;    stripper.stripify(*(geometry.get())) ;    // 添加到叶节点    osg::ref_ptr<osg::Geode> geode = new  osg::Geode() ;    geode->addDrawable(geometry.get()) ;    //添加到场景    root->addChild(geode.get()) ;    osgUtil::Optimizer optimizer ;    optimizer.optimize(root.get()) ;    viewer->setSceneData(root.get()) ;    viewer->realize() ;    viewer->run() ;    return 0 ;}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 58同城小区名输不了怎么办 e路发注册直接有积分怎么办 大众速腾的不锈钢圈被碰花了怎么办 公司卡着生育险不给怎么办 济南图书馆借书超过期限了怎么办 淘宝解绑支付宝支付密码不对怎么办 微博支付宝支付密码忘记了怎么办 图虫签约师通过了认证标识怎么办 签证做假工作证明资料被拒怎么办 在广州办个建设厅电工证怎么办 水利考的五大员证到有效期怎么办 额头注射玻尿酸吸收后不平怎么办 施工员证书挂靠拿不回来怎么办 森林公安未转政法编制的人员怎么办 北京的限行新政策外地车怎么办 报考二级建造师工作年限不够怎么办 郑州航院图书馆密码忘了怎么办 无锡妇幼预约挂号过号了怎么办 云南建筑八大员考试没通过怎么办 订车ax7一个月提不到车怎么办 提车时间到了却没车怎么办 一汽大众速腾气囊灯亮该怎么办呢? 幼儿园上课时候电脑上的课件怎么办 黑米紫薯红豆粥不好煮怎么办 母狗生了小狗后不吃东西没奶怎么办 狗给扑倒了主人不想负责怎么办 山东政务网个人中心账号忘了怎么办 网易博客忘记登入名和密码了怎么办 奶水不够怎么办怎样让奶水变多 和初恋分手多年又爱上初恋该怎么办 分手六年的初恋想要和我复合怎么办 不小心把手机里的视频删了怎么办 网签过了中介不配合过户怎么办 房屋被中介恶意网签了怎么办 重介质选矿块矿品位低怎么办 去泰国酒吧选小姐只会说中文怎么办 脚被窝烫伤后发炎里面有龙怎么办 脚被窝烫了发炎有龙怎么办 辣椒落花的药喷到孩子嘴巴了怎么办 香炉里的小米生虫子了怎么办 香炉里面放的小米生虫子了怎么办