#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);
void changeWaveBeamConeTarget( double latitude, double longitude, double height, bool ifDynamic);
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);
mat->setSpecular(osg::Material::FRONT, color);
geode->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);
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);
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;
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::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);
mat->setSpecular(osg::Material::FRONT, color);
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;
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::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);
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();
}
}