基础图元 简单矢量

这篇博客介绍了如何使用osg库在OpenSceneGraph中创建并渲染一个带有透明效果的3D正方形。首先,申请顶点数组定义四边形的四个顶点,然后设置颜色数组为半透明颜色,接着指定法向量。接着,创建几何对象Geometry,并设置顶点、颜色和法向量数组。最后,在Geode中添加这个几何对象,并开启透明模式和设置线宽,以呈现线条轮廓。

创建简单矢量图

基础:

OSG坐标系,与笛卡尔三维坐标系方向相同

步骤:

  1. 申请顶点数组,设置垂直xoy面的正方形的四个顶点

    osg::ref_ptr<osg::Vec3Array> coords = new osg::Vec3Array;
    
    coords->push_back(osg::Vec3(-10.0, 5.0, -10.0));
    coords->push_back(osg::Vec3(10.0, 5.0, -10.0));
    coords->push_back(osg::Vec3(10.0, 5.0, 10.0));
    coords->push_back(osg::Vec3(-10.0, 5.0, 10.0));
    
  2. 申请颜色数组,设置半透明的颜色组

    osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
    
    colors->push_back(osg::Vec4f(1.0, 0.0, 0.0, 0.5));
    colors->push_back(osg::Vec4f(0.0, 1.0, 0.0, 0.5));
    colors->push_back(osg::Vec4f(0.0, 0.0, 1.0, 0.5));
    colors->push_back(osg::Vec4f(1.0, 1.0, 0.0, 0.5));
    
  3. 申请法向量,令光源在正方形的一侧,压入一个法向量

    osg::ref_ptr<osg::Vec3Array> norms = new osg::Vec3Array;
    
    norms->push_back(osg::Vec3(0.0, -1.0, 0.0));
    
  4. 申请一个几何Geometry

    osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
    
  5. 设置顶点和顶点的关联方式

    geom->setVertexArray(coords.get());
    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::Mode::QUADS, 0, 4));
    
  6. 设置顶点颜色

    geom->setColorArray(colors.get());
    geom->setColorBinding(osg::Geometry::AttributeBinding::BIND_PER_VERTEX);
    
  7. 设置法向量

    geom->setNormalArray(norms.get());
    geom->setNormalBinding(osg::Geometry::AttributeBinding::BIND_OVERALL);
    
  8. 申请一个Geode

    osg::ref_ptr<osg::Geode> geode = new osg::Geode;
    
  9. 在Geode中绘制几何,打开透明

    geode->addDrawable(geom.get());
    geode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
    
  10. 将上一节中main函数中的CreateBox()改为CreateSimple(),运行

  11. 申请一个限制线宽

    osg::ref_ptr<osg::LineWidth> width = new osg::LineWidth;
    
  12. 将顶点关联方式改为LINE_LOOP

    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::Mode::LINE_LOOP, 0, 4));
    
  13. 设置线宽

    width->setWidth(5.0);
    
  14. 运行,查看结果

完整代码

#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osgGA/GUIEventAdapter>
#include <osgViewer/ViewerEventHandlers>
#include <osg/Geode>
#include <osg/ShapeDrawable>
#include <osg/Material>
#include <osg/StateSet>
#include <osg/Image>
#include <osg/Texture2D>
#include <osg/LineWidth>
#include <iostream>
using namespace std;

osg::ref_ptr<osg::Geode> CreateSimple() {
	osg::ref_ptr<osg::Geode> geode = new osg::Geode;
	osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
	osg::ref_ptr<osg::Vec3Array> coords = new osg::Vec3Array;
	osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
	osg::ref_ptr<osg::Vec3Array> norms = new osg::Vec3Array;
	osg::ref_ptr<osg::LineWidth> width = new osg::LineWidth;
	
    geode->addDrawable(geom.get());
	geode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
	geom->setVertexArray(coords.get());
	geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::Mode::LINE_LOOP, 0, 4));
	geom->setColorArray(colors.get());
	geom->setColorBinding(osg::Geometry::AttributeBinding::BIND_PER_VERTEX);
	geom->setNormalArray(norms.get());
	geom->setNormalBinding(osg::Geometry::AttributeBinding::BIND_OVERALL);
	width->setWidth(5.0);
	geode->getOrCreateStateSet()->setAttributeAndModes(width.get(), osg::StateAttribute::ON);

	coords->push_back(osg::Vec3(-10.0, 5.0, -10.0));
	coords->push_back(osg::Vec3(10.0, 5.0, -10.0));
	coords->push_back(osg::Vec3(10.0, 5.0, 10.0));
	coords->push_back(osg::Vec3(-10.0, 5.0, 10.0));

	colors->push_back(osg::Vec4f(1.0, 0.0, 0.0, 0.5));
	colors->push_back(osg::Vec4f(0.0, 1.0, 0.0, 0.5));
	colors->push_back(osg::Vec4f(0.0, 0.0, 1.0, 0.5));
	colors->push_back(osg::Vec4f(1.0, 1.0, 0.0, 0.5));

	norms->push_back(osg::Vec3(0.0, -1.0, 0.0));

	return geode;
}

int main() {

	osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
	osg::ref_ptr<osg::Group> group = new osg::Group;
	
	group->addChild(CreateSimple());
	viewer->setSceneData(group.get());

	return viewer->run();
}

STM32电机库无感代注释无传感器版本龙贝格观测三电阻双AD采样前馈控制弱磁控制斜坡启动内容概要:本文档为一份关于STM32电机控制的无传感器版本代注释资源,聚焦于龙贝格观测器在永磁同步电机(PMSM)无感控制中的应用。内容涵盖三电阻双通道AD采样技术、前馈控制、弱磁控制及斜坡启动等关键控制策略的实现方法,旨在通过详细的代解析帮助开发者深入理解基于STM32平台的高性能电机控制算法设计与工程实现。文档适用于从事电机控制开发的技术人员,重点解析了无位置传感器控制下的转子初始定位、速度估算与系统稳定性优化等问题。; 适合人群:具备一定嵌入式开发基础,熟悉STM32平台及电机控制原理的工程师或研究人员,尤其适合从事无感FOC开发的中高级技术人员。; 使用场景及目标:①掌握龙贝格观测器在PMSM无感控制中的建模与实现;②理解三电阻采样与双AD同步采集的硬件匹配与软件处理机制;③实现前馈补偿提升动态响应、弱磁扩速控制策略以及平稳斜坡启动过程;④为实际项目中调试和优化无感FOC系统提供代参考和技术支持; 阅读建议:建议结合STM32电机控制硬件平台进行代对照阅读与实验验证,重点关注观测器设计、电流采样校准、PI参数整定及各控制模块之间的协同逻辑,建议配合示波器进行信号观测以加深对控制时序与性能表现的理解。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值