#pragma once
#include <osg/Node>
#include <osg/MatrixTransform>
#include "Pendant.h"//三维实体的圆锥波束class WaveBeamCone :public Pendant {public:WaveBeamCone();~WaveBeamCone();void clearSelf()override;//创建圆锥形波束void createWaveBeamCone(osg::MatrixTransform* node,double angle, double length, osg::Vec4 color, osg::Vec4 lineColor, double lineWidth);//改变锥形波束的目标点,mt=波束的节点,latitude, longitude, height目标点的坐标,ifDynamic=是否打开动态跟踪,如果是false,则波束只会改变一次朝向,模型运动的时候,波束的方向不变,如果为true,则波束会跟踪目标点,实现动态的方向更改。void changeWaveBeamConeTarget(/*osg::MatrixTransform* mt,*/ double latitude, double longitude, double height, bool ifDynamic);//改变锥形波束的外观,color=波束颜色 lineColor=线的颜色 lineWidth=线的宽度 注意最后一个参数线宽,如果小于0.1,则线会被隐藏void changeWaveBeamConeAppearance(osg::Vec4 color, osg::Vec4 lineColor, double lineWidth);osg::ref_ptr<osg::MatrixTransform> getWaveBeamCone() {return _waveBeamCone;}
private:osg::ref_ptr<osg::MatrixTransform> _waveBeamCone;
};
#include "WaveBeamCone.h"
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/Material>
#include <osgFX/Outline>
#include <osgFX/Scribe>
#include <osg/MatrixTransform>
#include "WaveBeamConeCallBack.h"
#include "EarthMapManager.h"WaveBeamCone::WaveBeamCone() :Pendant(PST3D_WAVEBEAN_CONE) {}WaveBeamCone::~WaveBeamCone() {}void WaveBeamCone::clearSelf() {}void WaveBeamCone::createWaveBeamCone(osg::MatrixTransform* node, double angle, double length, osg::Vec4 color, osg::Vec4 lineColor, double lineWidth) {double angleD = osg::DegreesToRadians(angle);double radius = std::tan(angleD*0.5) * length;int splitCount = 20;double angleStep = osg::PI * 2.0 / splitCount;osg::ref_ptr<osg::Geode> geode = new osg::Geode();osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;osg::ref_ptr<osg::Vec3Array> vertex = new osg::Vec3Array;osg::ref_ptr<osg::Vec3Array> normal = new osg::Vec3Array;osg::ref_ptr<osg::DrawElementsUInt> drawElemUInt = new osg::DrawElementsUInt(GL_TRIANGLE_FAN);osg::ref_ptr<osg::DrawElementsUInt> drawElemUInt2 = new osg::DrawElementsUInt(GL_TRIANGLE_FAN);geom->setVertexArray(vertex);geom->setNormalArray(normal);geode->addDrawable(geom);vertex->push_back(osg::Vec3(0, 0, 0));drawElemUInt->push_back(0);normal->push_back(osg::Vec3(0, -1, 0));//侧面for (int i = 0; i <= splitCount; i++) {double tempAngle = i*angleStep;osg::Vec3 pos(radius * cos(tempAngle), length, radius * sin(tempAngle) + 3);vertex->push_back(osg::Vec3(pos));pos.normalize();normal->push_back(pos);drawElemUInt->push_back(i + 1);}//底面int indexBegin = vertex->size();vertex->push_back(osg::Vec3(0, length, 0));drawElemUInt2->push_back(indexBegin);normal->push_back(osg::Vec3(0, 1, 0));for (int i = 0; i <= splitCount; i++) {double tempAngle = i*angleStep;osg::Vec3 pos(radius * cos(tempAngle), length, radius * sin(tempAngle) + 3);vertex->push_back(osg::Vec3(pos));normal->push_back(osg::Vec3(0, 1, 0));drawElemUInt2->push_back(indexBegin + i + 1);}geom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);geom->addPrimitiveSet(drawElemUInt);geom->addPrimitiveSet(drawElemUInt2);//创建材质对象osg::ref_ptr<osg::Material> mat = new osg::Material;//设置正面散射颜色mat->setDiffuse(osg::Material::FRONT, color);//1.0, 0.0, 0.0, 0.3//设置正面镜面颜色mat->setSpecular(osg::Material::FRONT, color);//1.0, 0.0, 0.0, 0.3geode->getOrCreateStateSet()->setAttribute(mat.get());geode->getOrCreateStateSet()->setMode(GL_RESCALE_NORMAL, osg::StateAttribute::ON);//设置透明效果geode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);geode->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);geode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);//设置渲染顺序 仿真模型被波束遮盖 ,1000000-指的是若有1000000个Node 则此节点最后一个被渲染geode->getOrCreateStateSet()->setRenderBinDetails(1000000, "RenderBin");osg::ref_ptr<osgFX::Scribe>  nodeFX = new osgFX::Scribe;nodeFX->setWireframeColor(lineColor);nodeFX->setWireframeLineWidth(lineWidth);nodeFX->addChild(geode);osg::ref_ptr<osg::MatrixTransform> mtCone = new osg::MatrixTransform;mtCone->addChild(nodeFX);//给callback中赋值WaveBeamConeCallBack* coneCallBack = new WaveBeamConeCallBack;coneCallBack->m_angle = angle;coneCallBack->m_length = length;coneCallBack->m_color = color;coneCallBack->m_lineColor = lineColor;coneCallBack->m_lineWidth = lineWidth;coneCallBack->m_geode = geode;coneCallBack->m_geom = geom;coneCallBack->m_pointVector = vertex;coneCallBack->m_nodeFX = nodeFX;//如果线宽小于0.1,则关闭线框显示效果if (lineWidth < 0.1) {mtCone->addChild(coneCallBack->m_geode);mtCone->removeChild(coneCallBack->m_nodeFX);} else {mtCone->removeChild(coneCallBack->m_geode);mtCone->addChild(coneCallBack->m_nodeFX);}mtCone->setUpdateCallback(coneCallBack);mtCone->addDescription("WaveBeamCone");//添加到模型中osg::MatrixTransform* mtR = dynamic_cast<osg::MatrixTransform*>(node->getChild(0));osg::MatrixTransform* mtS = dynamic_cast<osg::MatrixTransform*>(mtR->getChild(0));mtR->addChild(mtCone);_waveBeamCone = mtCone;
}void WaveBeamCone::changeWaveBeamConeTarget(double latitude, double longitude, double height, bool ifDynamic) {WaveBeamConeCallBack* coneCallBack = dynamic_cast<WaveBeamConeCallBack*>(_waveBeamCone->getUpdateCallback());if (coneCallBack != NULL) {coneCallBack->m_ifDynamic = ifDynamic;coneCallBack->m_latitude = latitude;coneCallBack->m_longitude = longitude;coneCallBack->m_height = height;if (ifDynamic == false) {osg::MatrixTransform* mtCone = _waveBeamCone;osg::MatrixTransform* mtR = dynamic_cast<osg::MatrixTransform*>(mtCone->getParent(0));osg::MatrixTransform* mt = dynamic_cast<osg::MatrixTransform*>(mtR->getParent(0));//osg::Matrix m = osg::computeWorldToLocal(mtCone->getParentalNodePaths().at(0));osg::Matrix m = osg::Matrix::inverse(mt->getMatrix()*mtR->getMatrix());osg::Matrix mTarget;double x, y, z;EarthMapMgr()->getMapNode()->getMapSRS()->getEllipsoid()->convertLatLongHeightToXYZ(osg::DegreesToRadians(latitude),osg::DegreesToRadians(longitude),height, x, y, z);mTarget.setTrans(x, y, z);osg::Matrix mConeRate = osg::Matrix::rotate(osg::Vec3d(0, 1, 0), (mTarget*m).getTrans());mtCone->setMatrix(mConeRate);}}
}void WaveBeamCone::changeWaveBeamConeAppearance( osg::Vec4 color, osg::Vec4 lineColor, double lineWidth) {WaveBeamConeCallBack* coneCallBack = dynamic_cast<WaveBeamConeCallBack*>(_waveBeamCone->getUpdateCallback());if (coneCallBack != NULL) {coneCallBack->m_color = color;coneCallBack->m_lineColor = lineColor;coneCallBack->m_lineWidth = lineWidth;//创建材质对象osg::ref_ptr<osg::Material> mat = new osg::Material;//设置正面散射颜色mat->setDiffuse(osg::Material::FRONT, color);//1.0, 0.0, 0.0, 0.3//设置正面镜面颜色mat->setSpecular(osg::Material::FRONT, color);//1.0, 0.0, 0.0, 0.3                      coneCallBack->m_geode->getOrCreateStateSet()->setAttribute(mat.get());if (lineWidth < 0.1) {_waveBeamCone->addChild(coneCallBack->m_geode);_waveBeamCone->removeChild(coneCallBack->m_nodeFX);} else {_waveBeamCone->removeChild(coneCallBack->m_geode);_waveBeamCone->addChild(coneCallBack->m_nodeFX);coneCallBack->m_nodeFX->setWireframeColor(lineColor);coneCallBack->m_nodeFX->setWireframeLineWidth(lineWidth);}}
}
#pragma once
#include <osg/Geometry>
#include <osgFX/Scribe>
#include <osg/NodeCallback>class WaveBeamConeCallBack : public osg::NodeCallback {public:WaveBeamConeCallBack();~WaveBeamConeCallBack();virtual void operator() (osg::Node *node, osg::NodeVisitor *nv);
public:double m_latitude;double m_longitude;double m_height;bool m_ifDynamic;double m_angle;double m_length;osg::Vec4 m_color;osg::Vec4 m_lineColor;double m_lineWidth;osg::ref_ptr<osg::Geode> m_geode;osg::ref_ptr<osg::Geometry> m_geom;osg::ref_ptr<osg::Vec4Array> m_colorArray;osg::ref_ptr<osg::Vec3Array> m_pointVector;osg::ref_ptr<osgFX::Scribe> m_nodeFX;
};
#include "WaveBeamConeCallBack.h"
#include "EarthMapManager.h"
#include <osg/MatrixTransform>WaveBeamConeCallBack::WaveBeamConeCallBack() {m_latitude = 0.0;m_longitude = 0.0;m_height = -6371000;m_ifDynamic = false;m_angle = 20.0;m_length = 100000;m_color = osg::Vec4(1, 0, 0, 0.5);m_lineColor = osg::Vec4(1.0, 0.0, 0.0, 1.0);m_lineWidth = 1.0;
}WaveBeamConeCallBack::~WaveBeamConeCallBack() {}void WaveBeamConeCallBack::operator()(osg::Node *node, osg::NodeVisitor *nv) {if (m_ifDynamic == false)return;//std::cout << "WaveBeamConeCallBack info=" << m_latitude << "," << m_longitude << "," << m_height << std::endl;osg::MatrixTransform* mtCone = dynamic_cast<osg::MatrixTransform*>(node);if (mtCone != NULL) {osg::MatrixTransform* mtR = dynamic_cast<osg::MatrixTransform*>(mtCone->getParent(0));osg::MatrixTransform* mt = dynamic_cast<osg::MatrixTransform*>(mtR->getParent(0));//osg::Matrix m = osg::computeWorldToLocal(mtCone->getParentalNodePaths().at(0));osg::Matrix m = osg::Matrix::inverse(mt->getMatrix()*mtR->getMatrix());osg::Matrix mTarget;double x, y, z;EarthMapMgr()->getMapNode()->getMapSRS()->getEllipsoid()->convertLatLongHeightToXYZ(osg::DegreesToRadians(m_latitude),osg::DegreesToRadians(m_longitude),m_height, x, y, z);mTarget.setTrans(x, y, z);osg::Matrix mConeRate = osg::Matrix::rotate(osg::Vec3d(0, 1, 0), (mTarget*m).getTrans());mtCone->setMatrix(mConeRate);//mTarget*m//更改cone的形状----------------------------------------double length = (mTarget*m).getTrans().length();double angle = osg::DegreesToRadians(m_angle);double radius = std::tan(angle*0.5) * length;int splitCount = 20;double angleStep = osg::PI * 2.0 / splitCount;//侧面for (int i = 1; i <= splitCount + 1; i++) {double tempAngle = (i - 1)*angleStep;osg::Vec3 pos(radius * cos(tempAngle), length, radius * sin(tempAngle) + 3);m_pointVector->at(i) = pos;}m_pointVector->at(splitCount + 2) = osg::Vec3(0, length, 0);//底面for (int i = splitCount + 3; i <= splitCount + 3 + splitCount; i++) {double tempAngle = (i - splitCount - 3) *angleStep;osg::Vec3 pos(radius * cos(tempAngle), length, radius * sin(tempAngle) + 3);m_pointVector->at(i) = pos;}m_geom->dirtyBound();m_geom->dirtyDisplayList();}
}

OsgEarth添加圆锥体相关推荐

  1. osgEarth添加图例

    在osgEarth地图上添加图例 多的不说了直接上代码: 首先是添加控件(使用的是自带控件Control) //由于该功能属于项目的一部分,所以部分使用到的外部变量可能看不懂 //根据API更替为自己 ...

  2. osgEarth添加线特征

    部分关键代码: //三维航迹class Track3D:public Pendant {public:Track3D();~Track3D();//在OsgEarth上创建线void createTr ...

  3. osgEarth在斜面内绕自身Z轴旋转的锥体

    添加斜面描述变量 为了指定斜面的倾斜角度,需要添加一些描述角度的变量,此处为了简化,只添加了俯仰角(及绕物体x轴旋转的角度).代码如下: double pitch = osg::PI * 30 / 1 ...

  4. Cesium雷达追踪圆锥体

    Cesium雷达追踪圆锥体 实现效果 实现思路 通过原点与目标点,计算原点处的方向角,推算圆锥体的相关参数 关键代码 //添加圆锥体addSpaceFollow() {this.viewer.enti ...

  5. 在osgEarth中添加模型的简单示例

    一个在osgEarth中加载模型的小示例,方便在此基础上进行其它场景编辑,资源为osgEarth自带Data. #include "common.h" #include <W ...

  6. 实现osgEarth三维仿真场景模型雾的效果的添加解决方法

    在osgearth项目开发的过程中,为了实现逼真的三维场景可视化效果,需要对模型进行雾化处理,从而实现雾的自然天气情况模拟.在这过程中,利用OSG的osg::Fog类实现雾的效果,实现的效果如图1所示 ...

  7. OsgEarth下给地球添加网格、鹰眼

    void QtGuiAppEarth::slotGride() {if (ui.actionGride->isChecked()){if (m_pGraticule == nullptr){m_ ...

  8. osgEarth示例分析——osgearth_lights

    前言 osgearth_lights示例,模拟仿真了白天黑夜,添加了星空背景(太阳.月亮.其他天体),支持通过控件更改时间进而改变光照,支持随机更新图层颜色. earth文件中,需要添加<sky ...

  9. osgearth+vs2010安装

    OSGEARTH + VS2010 安装 *VS 平台不重要,本教程也适用于VS2008等.假设我的OSG目录为:D:/OSG *本教程参考网上osgearth+vs2008安装. 一.准备工作 下载 ...

最新文章

  1. IntelliJ IDEA 2016.3.1 学习git 码云插件 学习笔记
  2. 为MyEclipse加入自己定义凝视
  3. android camera入门,android 基础一 Camera1 预览、拍照
  4. WPF触屏Touch事件在嵌套控件中的响应问题
  5. java web 自定义标签_JavaWeb学习——自定义标签
  6. WebApi单元测试记录
  7. 95-910-165-源码-FlinkSQL-Flink SQL 中的时间属性
  8. 通过Server 2019中的组策略部署桌面墙纸 详解组策略环回处理
  9. 打印表格_Excel表格打印技巧,让你分分钟打印出完美表格!建议收藏!
  10. od怎么解java打包的exe_关于打包java文件并生成可执行文件的问题
  11. C++基础教程之如何定义数字
  12. Autodesk MapGuide Enterprise 2012开发技术入门培训视频录像下载
  13. 如何下载Xcode DMG或XIP文件?
  14. Echarts实现柱状图下钻功能
  15. 百度一面 / 二面 总结
  16. Home Assistant启动失败,页面报404
  17. CAN通讯程序C语言,AT90CAN单片机CAN通信模块介绍及软件编程
  18. 黎曼 zeta 函数与黎曼猜想
  19. db2 dec函数oracle,DB2常用函数与Oracle有什么区别?
  20. 51物联卡:浅谈物联网卡在智能安防中的发展前景

热门文章

  1. Android中你不得不知的几个问题及解决方法
  2. BitComet的简介与下载
  3. 如何开发微信小程序呢
  4. (一)unity自带的着色器源码剖析之——————UnityShaderVariables.cginc文件
  5. android pppd流程,Android pppd_gprs脚本启动过程
  6. 任意装修、在线DIY定制商城系统,跟紧时代的潮流
  7. win10每回打开程序都弹出“是否允许更改设置”提示怎么办
  8. Java如何设置word中某段文字的字体/段落样式
  9. C#毕业设计——基于C#+asp.net+sqlserver的精品课程教学网站设计与实现(毕业论文+程序源码)——教学网站
  10. GJCTF 官方WP PWN部分