第6讲 基本图元(下)

资源

视频连接

前言

固定流水是: 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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值