2010-06-23 03:59:12 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file modelviewgadgetwidget.cpp
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
2010-07-16 16:06:09 +02:00
|
|
|
* @addtogroup GCSPlugins GCS Plugins
|
2010-06-23 03:59:12 +02:00
|
|
|
* @{
|
2010-07-16 16:06:09 +02:00
|
|
|
* @addtogroup ModelViewPlugin ModelView Plugin
|
|
|
|
* @{
|
|
|
|
* @brief A gadget that displays a 3D representation of the UAV
|
2010-06-23 03:59:12 +02:00
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* 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 "modelviewgadgetwidget.h"
|
|
|
|
#include "extensionsystem/pluginmanager.h"
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
ModelViewGadgetWidget::ModelViewGadgetWidget(QWidget *parent)
|
2010-06-29 11:46:40 +02:00
|
|
|
: QGLWidget(QGLFormat(QGL::SampleBuffers),parent)
|
|
|
|
// : QGLWidget(parent)
|
2010-06-23 03:59:12 +02:00
|
|
|
, m_pFactory(GLC_Factory::instance(this->context()))
|
|
|
|
, m_Light()
|
|
|
|
, m_World()
|
|
|
|
, m_GlView(this)
|
|
|
|
, m_MoverController()
|
|
|
|
, m_ModelBoundingBox()
|
|
|
|
, m_MotionTimer()
|
2010-10-03 12:38:35 +02:00
|
|
|
, vboEnable(false)
|
2010-06-23 03:59:12 +02:00
|
|
|
{
|
2010-10-03 12:38:35 +02:00
|
|
|
// Prevent crash on non-VBO enabled systems during GLC geometry creation
|
|
|
|
GLC_State::setVboUsage(vboEnable);
|
|
|
|
|
2010-06-23 03:59:12 +02:00
|
|
|
setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
|
2010-09-10 17:25:05 +02:00
|
|
|
mvInitGLSuccess = false;
|
2010-06-23 03:59:12 +02:00
|
|
|
|
2010-08-10 18:57:14 +02:00
|
|
|
CreateScene();
|
2010-06-23 03:59:12 +02:00
|
|
|
|
|
|
|
m_Light.setPosition(4000.0, 40000.0, 80000.0);
|
|
|
|
m_Light.setAmbientColor(Qt::lightGray);
|
|
|
|
|
2010-10-04 11:53:31 +02:00
|
|
|
m_GlView.cameraHandle()->setDefaultUpVector(glc::Z_AXIS);
|
|
|
|
m_GlView.cameraHandle()->setFrontView();
|
2010-06-23 03:59:12 +02:00
|
|
|
|
2010-08-17 21:53:24 +02:00
|
|
|
QColor repColor;
|
|
|
|
repColor.setRgbF(1.0, 0.11372, 0.11372, 0.0);
|
|
|
|
m_MoverController= m_pFactory->createDefaultMoverController(repColor, &m_GlView);
|
|
|
|
|
2010-06-23 03:59:12 +02:00
|
|
|
// Get required UAVObjects
|
|
|
|
ExtensionSystem::PluginManager* pm = ExtensionSystem::PluginManager::instance();
|
|
|
|
UAVObjectManager* objManager = pm->getObject<UAVObjectManager>();
|
|
|
|
attActual = AttitudeActual::GetInstance(objManager);
|
|
|
|
|
|
|
|
// Create objects to display
|
|
|
|
connect(&m_MotionTimer, SIGNAL(timeout()), this, SLOT(updateAttitude()));
|
|
|
|
}
|
|
|
|
|
|
|
|
ModelViewGadgetWidget::~ModelViewGadgetWidget()
|
|
|
|
{
|
|
|
|
delete m_pFactory;
|
|
|
|
}
|
|
|
|
|
|
|
|
//// Public funcitons ////
|
|
|
|
void ModelViewGadgetWidget::reloadScene()
|
|
|
|
{
|
|
|
|
CreateScene();
|
|
|
|
}
|
|
|
|
|
|
|
|
//// Private functions ////
|
|
|
|
void ModelViewGadgetWidget::initializeGL()
|
|
|
|
{
|
2010-09-09 23:18:43 +02:00
|
|
|
if (loadError)
|
|
|
|
return;
|
2010-06-23 03:59:12 +02:00
|
|
|
// OpenGL initialization
|
|
|
|
m_GlView.initGl();
|
2010-10-03 12:38:35 +02:00
|
|
|
|
|
|
|
// Enable VBO usage if configured (default is to disable)
|
|
|
|
GLC_State::setVboUsage(vboEnable);
|
|
|
|
if (!vboEnable)
|
2010-06-23 03:59:12 +02:00
|
|
|
{
|
2010-08-17 21:53:24 +02:00
|
|
|
qDebug("VBOs disabled. Enable for better performance if GPU supports it. (Most do)");
|
2010-06-23 03:59:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
m_GlView.reframe(m_ModelBoundingBox);
|
|
|
|
// Calculate camera depth of view
|
|
|
|
m_GlView.setDistMinAndMax(m_World.boundingBox());
|
|
|
|
glEnable(GL_NORMALIZE);
|
2010-06-29 11:46:40 +02:00
|
|
|
// Enable antialiasing
|
|
|
|
glEnable(GL_MULTISAMPLE);
|
2010-06-23 03:59:12 +02:00
|
|
|
|
|
|
|
m_MotionTimer.start(100);
|
2010-10-04 12:17:59 +02:00
|
|
|
setFocusPolicy(Qt::StrongFocus); // keyboard capture for camera switching
|
2010-06-23 03:59:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ModelViewGadgetWidget::paintGL()
|
|
|
|
{
|
2010-09-09 23:18:43 +02:00
|
|
|
if (loadError)
|
|
|
|
return;
|
2010-06-23 03:59:12 +02:00
|
|
|
// Clear screen
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
// Load identity matrix
|
|
|
|
glLoadIdentity();
|
|
|
|
|
2010-06-29 11:46:40 +02:00
|
|
|
// Enable antialiasing
|
|
|
|
glEnable(GL_MULTISAMPLE);
|
|
|
|
|
2010-06-23 03:59:12 +02:00
|
|
|
// define the light
|
|
|
|
m_Light.enable();
|
|
|
|
|
|
|
|
// define view matrix
|
|
|
|
m_GlView.glExecuteCam();
|
|
|
|
m_Light.glExecute();
|
|
|
|
|
|
|
|
// Display the collection of GLC_Object
|
2010-09-07 14:52:52 +02:00
|
|
|
m_World.render(0, glc::TransparentRenderFlag);
|
2010-08-10 18:57:14 +02:00
|
|
|
m_World.render(0, glc::ShadingFlag);
|
2010-06-23 03:59:12 +02:00
|
|
|
|
|
|
|
// Display UI Info (orbit circle)
|
|
|
|
m_MoverController.drawActiveMoverRep();
|
2010-09-10 17:25:05 +02:00
|
|
|
mvInitGLSuccess = true;
|
|
|
|
|
2010-06-23 03:59:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ModelViewGadgetWidget::resizeGL(int width, int height)
|
|
|
|
{
|
|
|
|
m_GlView.setWinGLSize(width, height); // Compute window aspect ratio
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create GLC_Object to display
|
|
|
|
void ModelViewGadgetWidget::CreateScene()
|
|
|
|
{
|
2010-08-10 18:57:14 +02:00
|
|
|
try
|
2010-06-23 03:59:12 +02:00
|
|
|
{
|
|
|
|
m_GlView.loadBackGroundImage(bgFilename);
|
|
|
|
}
|
2010-08-10 18:57:14 +02:00
|
|
|
catch(GLC_Exception e)
|
|
|
|
{
|
|
|
|
qDebug("ModelView background image file loading failed.");
|
|
|
|
}
|
2010-06-23 03:59:12 +02:00
|
|
|
|
2010-08-10 18:57:14 +02:00
|
|
|
try
|
2010-06-23 03:59:12 +02:00
|
|
|
{
|
2010-08-10 18:57:14 +02:00
|
|
|
if(QFile::exists(acFilename))
|
|
|
|
{
|
|
|
|
QFile aircraft(acFilename);
|
|
|
|
m_World= GLC_Factory::instance()->createWorldFromFile(aircraft);
|
|
|
|
m_ModelBoundingBox= m_World.boundingBox();
|
2010-10-04 13:56:50 +02:00
|
|
|
m_GlView.reframe(m_ModelBoundingBox); // center 3d models in the scene
|
|
|
|
m_GlView.setDistMinAndMax(m_World.boundingBox());
|
2010-09-09 23:18:43 +02:00
|
|
|
loadError = false;
|
2010-09-10 17:25:05 +02:00
|
|
|
if (!mvInitGLSuccess)
|
|
|
|
initializeGL();
|
2010-09-09 23:18:43 +02:00
|
|
|
} else {
|
|
|
|
loadError = true;
|
|
|
|
}
|
2010-08-10 18:57:14 +02:00
|
|
|
}
|
|
|
|
catch(GLC_Exception e)
|
|
|
|
{
|
|
|
|
qDebug("ModelView aircraft texture file loading failed.");
|
2010-09-09 23:18:43 +02:00
|
|
|
loadError = true;
|
2010-06-23 03:59:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-17 21:53:24 +02:00
|
|
|
void ModelViewGadgetWidget::wheelEvent(QWheelEvent * e)
|
|
|
|
{
|
|
|
|
double delta = m_GlView.cameraHandle()->distEyeTarget() - (e->delta()/120) ;
|
|
|
|
m_GlView.cameraHandle()->setDistEyeTarget(delta);
|
|
|
|
m_GlView.setDistMinAndMax(m_World.boundingBox());
|
|
|
|
}
|
|
|
|
|
2010-06-23 03:59:12 +02:00
|
|
|
void ModelViewGadgetWidget::mousePressEvent(QMouseEvent *e)
|
|
|
|
{
|
|
|
|
if (m_MoverController.hasActiveMover()) return;
|
|
|
|
|
|
|
|
switch (e->button())
|
|
|
|
{
|
|
|
|
case (Qt::LeftButton):
|
|
|
|
m_MotionTimer.stop();
|
2010-08-10 18:57:14 +02:00
|
|
|
m_MoverController.setActiveMover(GLC_MoverController::TurnTable, e);
|
2010-06-23 03:59:12 +02:00
|
|
|
updateGL();
|
|
|
|
break;
|
2010-09-07 14:52:52 +02:00
|
|
|
case (Qt::RightButton):
|
2010-10-03 12:38:35 +02:00
|
|
|
printf("VBO enabled: %s, VBO supported: %s, VBO used: %s\n",
|
|
|
|
vboEnable ? "yes" : "no",
|
|
|
|
GLC_State::vboSupported() ? "yes" : "no",
|
|
|
|
GLC_State::vboUsed() ? "yes" : "no");
|
2010-09-07 14:52:52 +02:00
|
|
|
printf("Renderer - %s \n", (char*)glGetString(GL_RENDERER));
|
|
|
|
printf("Extensions - %s\n", (char*)glGetString(GL_EXTENSIONS));
|
|
|
|
break;
|
2010-06-23 03:59:12 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModelViewGadgetWidget::mouseMoveEvent(QMouseEvent * e)
|
|
|
|
{
|
|
|
|
if (not m_MoverController.hasActiveMover()) return;
|
2010-08-10 18:57:14 +02:00
|
|
|
m_MoverController.move(e);
|
2010-06-23 03:59:12 +02:00
|
|
|
m_GlView.setDistMinAndMax(m_World.boundingBox());
|
|
|
|
updateGL();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModelViewGadgetWidget::mouseReleaseEvent(QMouseEvent*)
|
|
|
|
{
|
|
|
|
if (not m_MoverController.hasActiveMover()) return;
|
|
|
|
m_MoverController.setNoMover();
|
|
|
|
m_MotionTimer.start();
|
|
|
|
updateGL();
|
|
|
|
}
|
|
|
|
|
2010-10-04 12:17:59 +02:00
|
|
|
void ModelViewGadgetWidget::keyPressEvent(QKeyEvent * e) // switch between camera
|
|
|
|
{
|
|
|
|
if (e->key() == Qt::Key_1)
|
|
|
|
{
|
|
|
|
m_GlView.cameraHandle()->setIsoView();
|
|
|
|
updateGL();
|
|
|
|
}
|
|
|
|
if (e->key() == Qt::Key_2)
|
|
|
|
{
|
|
|
|
m_GlView.cameraHandle()->setFrontView();
|
|
|
|
updateGL();
|
|
|
|
}
|
|
|
|
if (e->key() == Qt::Key_3)
|
|
|
|
{
|
|
|
|
m_GlView.cameraHandle()->setIsoView();
|
|
|
|
m_GlView.cameraHandle()->rotateAroundTarget(glc::Z_AXIS,glc::toRadian(90));
|
|
|
|
updateGL();
|
|
|
|
}
|
|
|
|
if (e->key() == Qt::Key_4)
|
|
|
|
{
|
|
|
|
m_GlView.cameraHandle()->setLeftView();
|
|
|
|
updateGL();
|
|
|
|
}
|
|
|
|
if (e->key() == Qt::Key_5)
|
|
|
|
{
|
|
|
|
m_GlView.cameraHandle()->setTopView();
|
|
|
|
m_GlView.cameraHandle()->rotateAroundTarget(glc::Z_AXIS,glc::toRadian(180));
|
|
|
|
updateGL();
|
|
|
|
}
|
|
|
|
if (e->key() == Qt::Key_6)
|
|
|
|
{
|
|
|
|
m_GlView.cameraHandle()->setRightView();;
|
|
|
|
updateGL();
|
|
|
|
}
|
|
|
|
if (e->key() == Qt::Key_7)
|
|
|
|
{
|
|
|
|
m_GlView.cameraHandle()->setIsoView();
|
|
|
|
m_GlView.cameraHandle()->rotateAroundTarget(glc::Z_AXIS,glc::toRadian(-90));
|
|
|
|
updateGL();
|
|
|
|
}
|
|
|
|
if (e->key() == Qt::Key_8)
|
|
|
|
{
|
|
|
|
m_GlView.cameraHandle()->setRearView();;
|
|
|
|
updateGL();
|
|
|
|
}
|
|
|
|
if (e->key() == Qt::Key_9)
|
|
|
|
{
|
|
|
|
m_GlView.cameraHandle()->setIsoView();
|
|
|
|
m_GlView.cameraHandle()->rotateAroundTarget(glc::Z_AXIS,glc::toRadian(180));
|
|
|
|
updateGL();
|
|
|
|
}
|
|
|
|
if (e->key() == Qt::Key_0)
|
|
|
|
{
|
|
|
|
m_GlView.cameraHandle()->setBottomView();
|
|
|
|
m_GlView.cameraHandle()->rotateAroundTarget(glc::Z_AXIS,glc::toRadian(180));
|
|
|
|
updateGL();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-23 03:59:12 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
// Private slots Functions
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
void ModelViewGadgetWidget::updateAttitude()
|
|
|
|
{
|
|
|
|
AttitudeActual::DataFields data = attActual->getData();
|
2010-10-04 11:53:31 +02:00
|
|
|
GLC_StructOccurence* rootObject= m_World.rootOccurence();
|
|
|
|
GLC_Matrix4x4 rootObjectPosition(0, 0, 0); // will be changed when the acceleration movement is tested
|
2010-10-04 12:17:59 +02:00
|
|
|
double pitch= glc::toRadian(-data.Pitch);
|
2010-10-04 11:53:31 +02:00
|
|
|
double roll= glc::toRadian(data.Roll);
|
2010-10-04 12:17:59 +02:00
|
|
|
double yaw= glc::toRadian(-data.Yaw);
|
2010-10-04 11:53:31 +02:00
|
|
|
GLC_Matrix4x4 rootObjectRotation(GLC_Matrix4x4().fromEuler(pitch, roll, yaw));
|
|
|
|
rootObject->structInstance()->setMatrix(rootObjectPosition * rootObjectRotation);
|
|
|
|
rootObject->updateChildrenAbsoluteMatrix();
|
2010-06-23 03:59:12 +02:00
|
|
|
updateGL();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|