OSG:幼儿园篇 第二章 几何体绘制类
一.简介
在osg中,通常有三种生成几何体的方法:
1.用封装的OpenGL绘图基元osg::Geometry
2.是用OSG中预定义的基本几何体
3.是从文件中导入场景模型
二.绘制几何体流程
1.指定向量数据
参考:OSG:先导篇 数据类型
2.设置绑定方式
数据绑定主要有两项:法线绑定和颜色绑定
3.数据解析
指定绘制三角形的方式,从而绘制不同的图形
#include <osgViewer/Viewer> #include <osg/Node> #include <osg/Geode> #include <osg/Group> #include <osgDB/ReadFile> #include <osgDB/WriteFile> #include <osgUtil/Optimizer> //创建一个四边形节点 osg::ref_ptr<osg::Node> 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)); //添加到叶节点 geode->addDrawable(geom.get()); return geode.get(); } int main() { //创建Viewer对象,场景浏览器 osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer(); osg::ref_ptr<osg::Group> root = new osg::Group(); //添加到场景 root->addChild(createQuad()); //优化场景数据 osgUtil::Optimizer optimizer; optimizer.optimize(root.get()); viewer->setSceneData(root.get()); viewer->realize(); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)