目录
资源
前言
固定流水是: 1.顶点 + 顶点连接方式 2.颜色+颜色法向量 只有这两个数据在Geo中,
其他比如:线宽,线性,透明,都在StatAttribute并放在节点中
Geode叶节点,起到样式的作用
一 绘制直线
osg::Geode * CreateLine()
{
//Geode 叶节点 -- 最终图形管理节点
osg::Geode* node = new osg::Geode;
//Geometry 可以自定义到几何
osg::ref_ptr<osg::Geometry> geo = new osg::Geometry;
//1 设置坐标
osg::ref_ptr<osg::Vec3Array> coords = new osg::Vec3Array;
geo->setVertexArray(coords.get());
float a=10.f;
coords->push_back(osg::Vec3(-a,-a,0.f));
coords->push_back(osg::Vec3(-a,a,0.f));
coords->push_back(osg::Vec3(a,a,0.f));
coords->push_back(osg::Vec3(a,-a,0.f));
//2. 顶点连接方式
geo->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));
//3.设置颜色
osg::ref_ptr<osg::Vec4Array> veccolor = new osg::Vec4Array;
geo->setColorArray(veccolor.get());
veccolor->push_back(osg::Vec4(1.0f,0.f,0.f,0.3f));
veccolor->push_back(osg::Vec4(0.0f,1.f,0.f,0.3f));
veccolor->push_back(osg::Vec4(0.0f,0.f,1.f,0.3f));
veccolor->push_back(osg::Vec4(1.0f,1.f,0.f,0.3f));
//每个顶点一个颜色
geo->setColorBinding(osg::Geometry::BIND_PER_VERTEX); // BIND_PER_PRIMITIVE_SET == 所有节点相同颜色
//4. 设置法向量
osg::ref_ptr<osg::Vec3Array> vecNorm = new osg::Vec3Array;
geo->setNormalArray(vecNorm.get(),osg::Array::BIND_OVERALL);
vecNorm->push_back(osg::Vec3(0.f,-1.f,0.f));
//5. 打开透明
node->getOrCreateStateSet()->setMode(GL_BLEND,osg::StateAttribute::ON);
//6 线宽
osg::ref_ptr<osg::LineWidth> linew=new osg::LineWidth;
linew->setWidth(20);
node->getOrCreateStateSet()->setAttributeAndModes(linew.get(),osg::StateAttribute::ON);
//最后:节点加入geo
node->addChild(geo);
return node;
}
1. Geometry – 自定节点
1. vec3Array = 点 = 3个坐标
osg::ref_ptr<osg::Geometry> geo = new osg::Geometry;
osg::ref_ptr<osg::Vec3Array> coords = new osg::Vec3Array;
geo->setVertexArray(coords.get());
float a=10.f;
coords->push_back(osg::Vec3(-a,-a,0.f));
coords->push_back(osg::Vec3(-a,a,0.f));
coords->push_back(osg::Vec3(a,a,0.f));
coords->push_back(osg::Vec3(a,-a,0.f));
2. 设置节点连接方式 addPrrimitiveSet
osg::ref_ptr<osg::Geometry> geo = new osg::Geometry;
geo->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));
3.设置颜色
osg::Geometry::BIND_PER_VERTEX | 逐个指定 |
osg::Geometry::BIND_PER_PRIMITIVE_SET | 所有点都用一个颜色 |
//3.设置颜色
osg::ref_ptr<osg::Vec4Array> veccolor = new osg::Vec4Array;
geo->setColorArray(veccolor.get());
veccolor->push_back(osg::Vec4(1.0f,0.f,0.f,0.3f));
veccolor->push_back(osg::Vec4(0.0f,1.f,0.f,0.3f));
veccolor->push_back(osg::Vec4(0.0f,0.f,1.f,0.3f));
veccolor->push_back(osg::Vec4(1.0f,1.f,0.f,0.3f));
//每个顶点一个颜色
geo->setColorBinding(osg::Geometry::BIND_PER_VERTEX); // BIND_PER_PRIMITIVE_SET == 所有节点相同颜色
4.设置法向量
osg::ref_ptr<osg::Vec3Array> vecNorm = new osg::Vec3Array;
geo->setNormalArray(vecNorm.get(),osg::Array::BIND_OVERALL);
vecNorm->push_back(osg::Vec3(0.f,-1.f,0.f));
5.打开半透明
node->getOrCreateStateSet()->setMode(GL_BLEND,osg::StateAttribute::ON);
6.线宽
osg::ref_ptr<osg::LineWidth> linew=new osg::LineWidth;
linew->setWidth(20);
node->getOrCreateStateSet()->setAttributeAndModes(linew.get(),osg::StateAttribute::ON);