diff --git a/ground/openpilotgcs/src/plugins/osgearthview/osgearthview.pro b/ground/openpilotgcs/src/plugins/osgearthview/osgearthview.pro index d83dd701a..5873d3e82 100644 --- a/ground/openpilotgcs/src/plugins/osgearthview/osgearthview.pro +++ b/ground/openpilotgcs/src/plugins/osgearthview/osgearthview.pro @@ -8,14 +8,16 @@ include(osgearthview_dependencies.pri) include(osg.pri) include(osgearth.pri) -HEADERS += osgearthviewplugin.h +HEADERS += osgearthviewplugin.h \ + osgviewerwidget.h HEADERS += osgearthviewgadget.h HEADERS += osgearthviewwidget.h HEADERS += osgearthviewgadgetfactory.h HEADERS += osgearthviewgadgetconfiguration.h HEADERS += osgearthviewgadgetoptionspage.h -SOURCES += osgearthviewplugin.cpp +SOURCES += osgearthviewplugin.cpp \ + osgviewerwidget.cpp SOURCES += osgearthviewgadget.cpp SOURCES += osgearthviewwidget.cpp SOURCES += osgearthviewgadgetfactory.cpp @@ -26,3 +28,5 @@ FORMS += osgearthviewgadgetoptionspage.ui OTHER_FILES += OsgEarthviewGadget.pluginspec RESOURCES += osgearthview.qrc + + diff --git a/ground/openpilotgcs/src/plugins/osgearthview/osgearthviewwidget.cpp b/ground/openpilotgcs/src/plugins/osgearthview/osgearthviewwidget.cpp index 3cb63838b..4191ad0da 100644 --- a/ground/openpilotgcs/src/plugins/osgearthview/osgearthviewwidget.cpp +++ b/ground/openpilotgcs/src/plugins/osgearthview/osgearthviewwidget.cpp @@ -99,134 +99,19 @@ using namespace Utils; OsgEarthviewWidget::OsgEarthviewWidget(QWidget *parent) : QWidget(parent) { - setThreadingModel(osgViewer::ViewerBase::CullThreadPerCameraDrawThreadPerContext); + viewWidget = new OsgViewerWidget(this); + viewWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); - //setThreadingModel(osgViewer::ViewerBase::SingleThreaded); - setAttribute(Qt::WA_PaintOnScreen, true); - setAttribute(Qt::WA_NoSystemBackground, true); - - osg::Group* root = new osg::Group; - osg::Node* earth = osgDB::readNodeFile("/Users/jcotton81/Documents/Programming/osgearth/tests/boston.earth"); - osgEarth::MapNode * mapNode = osgEarth::MapNode::findMapNode( earth ); - if (!mapNode) - { - qDebug() <<"Uhoh"; - } - - root->addChild(earth); - - osg::Node* airplane = createAirplane(); - uavPos = new osgEarth::Util::ObjectLocatorNode(mapNode->getMap()); - uavPos->getLocator()->setPosition( osg::Vec3d(-71.0763, 42.34425, 150) ); - uavPos->addChild(airplane); - - root->addChild(uavPos); - - osgUtil::Optimizer optimizer; - optimizer.optimize(root); - - QWidget* popupWidget = createViewWidget( createCamera(0,0,600,600,"Earth",true), root); - popupWidget->show(); setLayout(new QVBoxLayout()); - //layout()->addWidget(popupWidget); - - connect( &_timer, SIGNAL(timeout()), this, SLOT(update()) ); - _timer.start( 10 ); + layout()->addWidget(viewWidget); } OsgEarthviewWidget::~OsgEarthviewWidget() { } -QWidget* OsgEarthviewWidget::createViewWidget( osg::Camera* camera, osg::Node* scene ) -{ - osgViewer::View* view = new osgViewer::View; - view->setCamera( camera ); - - addView( view ); - - view->setSceneData( scene ); - view->addEventHandler( new osgViewer::StatsHandler ); - view->getDatabasePager()->setDoPreCompile( true ); - - manip = new EarthManipulator(); - view->setCameraManipulator( manip ); - - Grid* grid = new Grid(); - grid->setControl(0,0,new LabelControl("OpenPilot")); - ControlCanvas::get(view, true)->addControl(grid); - - // zoom to a good startup position - manip->setViewpoint( Viewpoint(-71.0763, 42.34425, 0, 24.261, -21.6, 650.0), 5.0 ); - //manip->setHomeViewpoint(Viewpoint("Boston", osg::Vec3d(-71.0763, 42.34425, 0), 24.261, -21.6, 3450.0)); - - osgQt::GraphicsWindowQt* gw = dynamic_cast( camera->getGraphicsContext() ); - return gw ? gw->getGLWidget() : NULL; -} - -osg::Camera* OsgEarthviewWidget::createCamera( int x, int y, int w, int h, const std::string& name="", bool windowDecoration=false ) -{ - osg::DisplaySettings* ds = osg::DisplaySettings::instance().get(); - osg::ref_ptr traits = new osg::GraphicsContext::Traits; - traits->windowName = name; - traits->windowDecoration = windowDecoration; - traits->x = x; - traits->y = y; - traits->width = w; - traits->height = h; - traits->doubleBuffer = true; - traits->alpha = ds->getMinimumNumAlphaBits(); - traits->stencil = ds->getMinimumNumStencilBits(); - traits->sampleBuffers = ds->getMultiSamples(); - traits->samples = ds->getNumMultiSamples(); - - osg::ref_ptr camera = new osg::Camera; - camera->setGraphicsContext( new osgQt::GraphicsWindowQt(traits.get()) ); - - camera->setClearColor( osg::Vec4(0.2, 0.2, 0.6, 1.0) ); - camera->setViewport( new osg::Viewport(0, 0, traits->width, traits->height) ); - camera->setProjectionMatrixAsPerspective( - 30.0f, static_cast(traits->width)/static_cast(traits->height), 1.0f, 10000.0f ); - return camera.release(); -} - -osg::Node* OsgEarthviewWidget::createAirplane() -{ - osg::Group* model = new osg::Group; - osg::Node *cessna = osgDB::readNodeFile("/Users/jcotton81/Documents/Programming/OpenPilot/artwork/3D Model/multi/joes_cnc/J14-QT_+.3DS"); - if(cessna) { - uavAttitudeAndScale = new osg::MatrixTransform(); - uavAttitudeAndScale->setMatrix(osg::Matrixd::scale(0.2e0,0.2e0,0.2e0)); - uavAttitudeAndScale->addChild( cessna ); - - model->addChild(uavAttitudeAndScale); - } else - qDebug() << "Bad model file"; - return model; -} - void OsgEarthviewWidget::paintEvent( QPaintEvent* event ) { - ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance(); - UAVObjectManager * objMngr = pm->getObject(); - - PositionActual *positionActualObj = PositionActual::GetInstance(objMngr); - PositionActual::DataFields positionActual = positionActualObj->getData(); - - double LLA[3]; - double NED[3] = {positionActual.North, positionActual.East, positionActual.Down}; - double homeLLA[3] = {-71.0763, 42.34425, 50}; - - CoordinateConversions().GetLLA(homeLLA, NED, LLA); - uavPos->getLocator()->setPosition( osg::Vec3d(LLA[0], LLA[1], LLA[2]) ); - - AttitudeActual *attitudeActualObj = AttitudeActual::GetInstance(objMngr); - AttitudeActual::DataFields attitudeActual = attitudeActualObj->getData(); - - osg::Quat quat(-attitudeActual.q2, -attitudeActual.q3, -attitudeActual.q4,attitudeActual.q1); - uavAttitudeAndScale->setMatrix(osg::Matrixd::scale(0.05e0,0.05e0,0.05e0) * osg::Matrixd::rotate(quat)); - - frame(); } void OsgEarthviewWidget::resizeEvent(QResizeEvent *event) diff --git a/ground/openpilotgcs/src/plugins/osgearthview/osgearthviewwidget.h b/ground/openpilotgcs/src/plugins/osgearthview/osgearthviewwidget.h index 24c37399b..d5ad0f0d6 100644 --- a/ground/openpilotgcs/src/plugins/osgearthview/osgearthviewwidget.h +++ b/ground/openpilotgcs/src/plugins/osgearthview/osgearthviewwidget.h @@ -26,6 +26,7 @@ #ifndef OSGEARTHVIEWWIDGET_H_ #define OSGEARTHVIEWWIDGET_H_ +#include "osgviewerwidget.h" #include "osgearthviewgadgetconfiguration.h" #include "extensionsystem/pluginmanager.h" #include "uavobjectmanager.h" @@ -80,7 +81,7 @@ using namespace osgEarth::Annotation; #include -class OsgEarthviewWidget : public QWidget, public osgViewer::CompositeViewer +class OsgEarthviewWidget : public QWidget { Q_OBJECT @@ -94,22 +95,6 @@ protected: /* Protected methods */ void paintEvent(QPaintEvent *event); void resizeEvent(QResizeEvent *event); - /* Create a osgQt::GraphicsWindowQt to add to the widget */ - QWidget* createViewWidget( osg::Camera* camera, osg::Node* scene ); - - /* Create an osg::Camera which sets up the OSG view */ - osg::Camera* createCamera( int x, int y, int w, int h, const std::string& name, bool windowDecoration ); - - /* Get the model to render */ - osg::Node* createAirplane(); -private slots: - -private: /* Private methods */ - -private: /* Private variables */ - QTimer _timer; - EarthManipulator* manip; - osgEarth::Util::ObjectLocatorNode* uavPos; - osg::MatrixTransform* uavAttitudeAndScale; + OsgViewerWidget *viewWidget; }; #endif /* OSGEARTHVIEWWIDGET_H_ */ diff --git a/ground/openpilotgcs/src/plugins/osgearthview/osgviewerwidget.cpp b/ground/openpilotgcs/src/plugins/osgearthview/osgviewerwidget.cpp new file mode 100644 index 000000000..ca2e3aaa5 --- /dev/null +++ b/ground/openpilotgcs/src/plugins/osgearthview/osgviewerwidget.cpp @@ -0,0 +1,247 @@ +/******************************************************************************** +* @file osgviewerwidget.cpp +* @author The OpenPilot Team Copyright (C) 2012. +* @addtogroup GCSPlugins GCS Plugins +* @{ +* @addtogroup OsgEarthview Plugin Widget +* @{ +* @brief Osg Earth view of UAV +*****************************************************************************/ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "osgviewerwidget.h" +#include "osgearthviewwidget.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + + +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include + +#include + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +using namespace osgEarth::Util; +using namespace osgEarth::Util::Controls; +using namespace osgEarth::Symbology; +using namespace osgEarth::Drivers; +using namespace osgEarth::Annotation; + +#include +#include + +#include + +#include + +#include + +#include + +#include "utils/stylehelper.h" +#include "utils/homelocationutil.h" +#include "utils/worldmagmodel.h" +#include "utils/coordinateconversions.h" +#include "attitudeactual.h" +#include "gpsposition.h" +#include "homelocation.h" +#include "positionactual.h" + +using namespace Utils; + +OsgViewerWidget::OsgViewerWidget(QWidget *parent) : QWidget(parent) +{ + osgQt::initQtWindowingSystem(); + osgQt::setViewer(this); + + setThreadingModel(osgViewer::ViewerBase::CullThreadPerCameraDrawThreadPerContext); + + setThreadingModel(osgViewer::ViewerBase::SingleThreaded); + setAttribute(Qt::WA_PaintOnScreen, true); + setAttribute(Qt::WA_NoSystemBackground, true); + + osg::Group* root = new osg::Group; + osg::Node* earth = osgDB::readNodeFile("/Users/jcotton81/Documents/Programming/osgearth/tests/boston.earth"); + osgEarth::MapNode * mapNode = osgEarth::MapNode::findMapNode( earth ); + if (!mapNode) + { + qDebug() <<"Uhoh"; + } + + root->addChild(earth); + + osg::Node* airplane = createAirplane(); + uavPos = new osgEarth::Util::ObjectLocatorNode(mapNode->getMap()); + uavPos->getLocator()->setPosition( osg::Vec3d(-71.100549, 42.349273, 150) ); + uavPos->addChild(airplane); + + root->addChild(uavPos); + + osgUtil::Optimizer optimizer; + optimizer.optimize(root); + + QWidget* viewWidget = createViewWidget( createCamera(0,0,200,200,"Earth",false), root); + //viewWidget->show(); + setLayout(new QVBoxLayout(this)); + + viewWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + layout()->addWidget(viewWidget); + + + connect( &_timer, SIGNAL(timeout()), this, SLOT(update()) ); + _timer.start( 10 ); +} + +OsgViewerWidget::~OsgViewerWidget() +{ +} + +QWidget* OsgViewerWidget::createViewWidget( osg::Camera* camera, osg::Node* scene ) +{ + osgViewer::View* view = new osgViewer::View; + view->setCamera( camera ); + + addView( view ); + + view->setSceneData( scene ); + view->addEventHandler( new osgViewer::StatsHandler ); + view->getDatabasePager()->setDoPreCompile( true ); + + manip = new EarthManipulator(); + view->setCameraManipulator( manip ); + + Grid* grid = new Grid(); + grid->setControl(0,0,new LabelControl("OpenPilot")); + ControlCanvas::get(view, true)->addControl(grid); + + // zoom to a good startup position + manip->setViewpoint( Viewpoint(-71.100549, 42.349273, 0, 24.261, -21.6, 650.0), 5.0 ); + //manip->setHomeViewpoint(Viewpoint("Boston", osg::Vec3d(-71.0763, 42.34425, 0), 24.261, -21.6, 3450.0)); + + osgQt::GraphicsWindowQt* gw = dynamic_cast( camera->getGraphicsContext() ); + return gw ? gw->getGLWidget() : NULL; +} + +osg::Camera* OsgViewerWidget::createCamera( int x, int y, int w, int h, const std::string& name="", bool windowDecoration=false ) +{ + osg::DisplaySettings* ds = osg::DisplaySettings::instance().get(); + osg::ref_ptr traits = new osg::GraphicsContext::Traits; + traits->windowName = name; + traits->windowDecoration = windowDecoration; + traits->x = x; + traits->y = y; + traits->width = w; + traits->height = h; + traits->doubleBuffer = true; + traits->alpha = ds->getMinimumNumAlphaBits(); + traits->stencil = ds->getMinimumNumStencilBits(); + traits->sampleBuffers = ds->getMultiSamples(); + traits->samples = ds->getNumMultiSamples(); + + osg::ref_ptr camera = new osg::Camera; + camera->setGraphicsContext( new osgQt::GraphicsWindowQt(traits.get()) ); + + camera->setClearColor( osg::Vec4(0.2, 0.2, 0.6, 1.0) ); + camera->setViewport( new osg::Viewport(0, 0, traits->width, traits->height) ); + camera->setProjectionMatrixAsPerspective( + 30.0f, static_cast(traits->width)/static_cast(traits->height), 1.0f, 10000.0f ); + return camera.release(); +} + +osg::Node* OsgViewerWidget::createAirplane() +{ + osg::Group* model = new osg::Group; + osg::Node *cessna = osgDB::readNodeFile("/Users/jcotton81/Documents/Programming/OpenPilot/artwork/3D Model/multi/joes_cnc/J14-QT_+.3DS"); + if(cessna) { + uavAttitudeAndScale = new osg::MatrixTransform(); + uavAttitudeAndScale->setMatrix(osg::Matrixd::scale(0.2e0,0.2e0,0.2e0)); + uavAttitudeAndScale->addChild( cessna ); + + model->addChild(uavAttitudeAndScale); + } else + qDebug() << "Bad model file"; + return model; +} + +void OsgViewerWidget::paintEvent( QPaintEvent* event ) +{ + ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance(); + UAVObjectManager * objMngr = pm->getObject(); + + PositionActual *positionActualObj = PositionActual::GetInstance(objMngr); + PositionActual::DataFields positionActual = positionActualObj->getData(); + double NED[3] = {positionActual.North, positionActual.East, positionActual.Down}; + + bool positionActualUpdate = true; + if(positionActualUpdate) { + HomeLocation *homeLocationObj = HomeLocation::GetInstance(objMngr); + HomeLocation::DataFields homeLocation = homeLocationObj->getData(); + double homeLLA[3] = {homeLocation.Latitude / 10.0e6, homeLocation.Longitude / 10.0e6, homeLocation.Altitude}; + + double LLA[3]; + CoordinateConversions().GetLLA(homeLLA, NED, LLA); + uavPos->getLocator()->setPosition( osg::Vec3d(LLA[1], LLA[0], LLA[2]) ); // Note this takes longtitude first + } else { + GPSPosition *gpsPosObj = GPSPosition::GetInstance(objMngr); + GPSPosition::DataFields gpsPos = gpsPosObj->getData(); + uavPos->getLocator()->setPosition( osg::Vec3d(gpsPos.Longitude / 10.0e6, gpsPos.Latitude / 10.0e6, gpsPos.Altitude)); + } + + // Set the attitude (reverse the attitude) + AttitudeActual *attitudeActualObj = AttitudeActual::GetInstance(objMngr); + AttitudeActual::DataFields attitudeActual = attitudeActualObj->getData(); + osg::Quat quat(-attitudeActual.q2, -attitudeActual.q3, -attitudeActual.q4,attitudeActual.q1); + uavAttitudeAndScale->setMatrix(osg::Matrixd::scale(0.05e0,0.05e0,0.05e0) * osg::Matrixd::rotate(quat)); + + frame(); +} diff --git a/ground/openpilotgcs/src/plugins/osgearthview/osgviewerwidget.h b/ground/openpilotgcs/src/plugins/osgearthview/osgviewerwidget.h new file mode 100644 index 000000000..c0a8582e6 --- /dev/null +++ b/ground/openpilotgcs/src/plugins/osgearthview/osgviewerwidget.h @@ -0,0 +1,115 @@ +/******************************************************************************** +* @file osgviewerwidget.h +* @author The OpenPilot Team Copyright (C) 2012. +* @addtogroup GCSPlugins GCS Plugins +* @{ +* @addtogroup OsgEarthview Plugin +* @{ +* @brief Osg Earth view of UAV +*****************************************************************************/ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef OSGVIEWERWIDGET_H +#define OSGVIEWERWIDGET_H + +#include + +#include "osgearthviewgadgetconfiguration.h" +#include "extensionsystem/pluginmanager.h" +#include "uavobjectmanager.h" +#include "uavobject.h" + +#include + +#include +#include + +#include + +#include +#include +#include + +#include + +#include +#include +#include + +#include +#include +#include + +#include + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +using namespace osgEarth::Util; +using namespace osgEarth::Util::Controls; +using namespace osgEarth::Symbology; +using namespace osgEarth::Drivers; +using namespace osgEarth::Annotation; + +#include + +#include + +class OsgViewerWidget : public QWidget, public osgViewer::CompositeViewer +{ + Q_OBJECT +public: + explicit OsgViewerWidget(QWidget *parent = 0); + ~OsgViewerWidget(); +signals: + +public slots: + +protected: + void paintEvent(QPaintEvent *event); + + /* Create a osgQt::GraphicsWindowQt to add to the widget */ + QWidget* createViewWidget( osg::Camera* camera, osg::Node* scene ); + + /* Create an osg::Camera which sets up the OSG view */ + osg::Camera* createCamera( int x, int y, int w, int h, const std::string& name, bool windowDecoration ); + + /* Get the model to render */ + osg::Node* createAirplane(); + +private: /* Private variables */ + QTimer _timer; + EarthManipulator* manip; + osgEarth::Util::ObjectLocatorNode* uavPos; + osg::MatrixTransform* uavAttitudeAndScale; +}; + + +#endif // OSGVIEWERWIDGET_H