dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
modelviewgadgetwidget.cpp
Go to the documentation of this file.
1 
12 /*
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  * for more details.
22  *
23  * You should have received a copy of the GNU General Public License along
24  * with this program; if not, see <http://www.gnu.org/licenses/>
25  */
26 #include "QtDebug"
27 #ifdef __APPLE__
28 #include "OpenGL/OpenGL.h"
29 #endif
30 #include "modelviewgadgetwidget.h"
32 #include "glc_lib/glc_context.h"
33 #include "glc_lib/glc_exception.h"
34 #include "glc_lib/glc_openglexception.h"
35 #include "glc_lib/viewport/glc_userinput.h"
36 
37 #include <iostream>
38 
39 // Model 3d object and background image used when specific one isn't available
40 const QString ModelViewGadgetWidget::fallbackAcFilename =
41  QString(":/modelview/models/warning_sign.obj");
42 const QString ModelViewGadgetWidget::fallbackBgFilename = QString(":/modelview/models/black.jpg");
43 
45  : QGLWidget(new GLC_Context(QGLFormat(QGL::SampleBuffers)), parent)
46  , m_Light()
47  , m_World()
48  , m_GlView()
49  , m_MoverController()
50  , m_ModelBoundingBox()
51  , m_MotionTimer()
52  , acFilename(fallbackAcFilename)
53  , bgFilename(fallbackBgFilename)
54  , vboEnable(false)
55 {
56  connect(&m_GlView, &GLC_Viewport::updateOpenGL, this, &QGLWidget::updateGL);
57  setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
58 
59  m_Light.setPosition(4000.0, 40000.0, 80000.0);
60  // m_GlView.setBackgroundColor(Qt::white);
61  m_Light.setAmbientColor(Qt::lightGray);
62 
63  m_GlView.cameraHandle()->setDefaultUpVector(glc::Z_AXIS);
64  m_GlView.cameraHandle()->setRearView();
65 
66  QColor repColor;
67  repColor.setRgbF(1.0, 0.11372, 0.11372, 0.0);
68  m_MoverController = GLC_Factory::instance()->createDefaultMoverController(repColor, &m_GlView);
69 
70  CreateScene();
71  // Get required UAVObjects
72  ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
73  UAVObjectManager *objManager = pm->getObject<UAVObjectManager>();
74  attState = AttitudeActual::GetInstance(objManager);
75 
76  connect(&m_MotionTimer, &QTimer::timeout, this,
77  QOverload<>::of(&ModelViewGadgetWidget::updateAttitude));
78 }
79 
81 
83 {
84  if (QFile::exists(acf)) {
85  acFilename = acf;
86  } else {
87  acFilename = acf = fallbackAcFilename;
88  m_GlView.cameraHandle()->setFrontView(); // set to front camera to see/read the warning sign
89  }
90 }
91 
93 {
94  if (QFile::exists(bgFilename)) {
95  bgFilename = bgf;
96  } else {
97  qDebug() << "file " << bgf << " doesn't exists";
98  bgFilename = fallbackBgFilename; // will put a black background if there's no background
99  }
100 }
101 
103 {
104  vboEnable = eVbo;
105  m_World.collection()->setVboUsage(vboEnable);
106 }
107 
110 {
111  CreateScene();
112 }
113 
115 void ModelViewGadgetWidget::initializeGL()
116 {
117 // For VSYNC problem under Mac OS X
118 #if defined(Q_OS_MAC)
119  const GLint swapInterval = 1;
120  CGLSetParameter(CGLGetCurrentContext(), kCGLCPSwapInterval, &swapInterval);
121 #endif
122 
123  // OpenGL initialization
124  m_GlView.initGl();
125  // Reframe the scene
126  m_GlView.reframe(m_ModelBoundingBox);
127 
128  glEnable(GL_NORMALIZE);
129  // Enable antialiasing
130  glEnable(GL_MULTISAMPLE);
131 
132  m_MotionTimer.start(100);
133  setFocusPolicy(Qt::StrongFocus); // keyboard capture for camera switching
134 }
135 
136 void ModelViewGadgetWidget::paintGL()
137 {
138  try {
139  // Clear screen
140  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
141 
142  // OpenGL error handler
143  {
144  GLenum error = glGetError();
145  if (error != GL_NO_ERROR) {
146  GLC_OpenGlException OpenGlException("ModelViewGadgetWidget::paintGL() ", error);
147  throw(OpenGlException);
148  }
149  }
150 
151  // Load identity matrix
152  GLC_Context::current()->glcLoadIdentity();
153 
154  // Enable antialiasing
155  glEnable(GL_MULTISAMPLE);
156 
157  // Calculate camera depth of view
158  m_GlView.setDistMinAndMax(m_World.boundingBox());
159 
160  // define view matrix
161  m_GlView.glExecuteCam();
162  m_Light.glExecute();
163 
164  // Display the collection of GLC_Object
165  m_World.render(0, glc::ShadingFlag);
166  m_World.render(0, glc::TransparentRenderFlag);
167 
168  // Display UI Info (orbit circle)
169  m_MoverController.drawActiveMoverRep();
170  } catch (GLC_Exception &e) {
171  qDebug() << e.what();
172  }
173 }
174 
175 void ModelViewGadgetWidget::resizeGL(int width, int height)
176 {
177  m_GlView.setWinGLSize(width, height); // Compute window aspect ratio
178  // OpenGL error handler
179  {
180  GLenum error = glGetError();
181  if (error != GL_NO_ERROR) {
182  GLC_OpenGlException OpenGlException("ModelViewGadgetWidget::resizeGL() ", error);
183  throw(OpenGlException);
184  }
185  }
186 }
187 
188 // Create GLC_Object to display
189 void ModelViewGadgetWidget::CreateScene()
190 {
191  // put a black background if the 3D model is invalid or if the background image is also invalid
192  if (acFilename == fallbackAcFilename || !QFile::exists(bgFilename)) {
193  bgFilename = fallbackBgFilename;
194  }
195 
196  try {
197  m_GlView.loadBackGroundImage(bgFilename);
198  } catch (GLC_Exception e) {
199  qDebug("ModelView: background image file loading failed.");
200  }
201 
202  try {
203  if (QFile::exists(acFilename)) {
204  QFile aircraft(acFilename);
205  m_World = GLC_Factory::instance()->createWorldFromFile(aircraft);
206  m_ModelBoundingBox = m_World.boundingBox();
207  m_GlView.reframe(m_ModelBoundingBox); // center 3D model in the scene
208  } else {
209  qDebug() << "ModelView: aircraft file not found:" << acFilename;
210  }
211  } catch (GLC_Exception e) {
212  qDebug("ModelView: aircraft file loading failed.");
213  }
214 }
215 
216 void ModelViewGadgetWidget::wheelEvent(QWheelEvent *e)
217 {
218  double delta = m_GlView.cameraHandle()->distEyeTarget() - (e->delta() / 4);
219 
220  m_GlView.cameraHandle()->setDistEyeTarget(delta);
221  m_GlView.setDistMinAndMax(m_World.boundingBox());
222 }
223 
224 void ModelViewGadgetWidget::mousePressEvent(QMouseEvent *e)
225 {
226  GLC_UserInput userInput(e->x(), e->y());
227 
228  if (m_MoverController.hasActiveMover()) {
229  return;
230  }
231 
232  switch (e->button()) {
233  case (Qt::LeftButton):
234  m_MotionTimer.stop();
235  m_MoverController.setActiveMover(GLC_MoverController::TurnTable, userInput);
236  updateGL();
237  break;
238  case (Qt::RightButton):
239  printf("VBO enabled: %s, VBO supported: %s, VBO used: %s\n", vboEnable ? "yes" : "no",
240  GLC_State::vboSupported() ? "yes" : "no", GLC_State::vboUsed() ? "yes" : "no");
241  printf("Renderer - %s \n", reinterpret_cast<const char *>(glGetString(GL_RENDERER)));
242  printf("Extensions - %s\n", reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS)));
243  break;
244  default:
245  break;
246  }
247 }
248 
249 void ModelViewGadgetWidget::mouseMoveEvent(QMouseEvent *e)
250 {
251  GLC_UserInput userInput(e->x(), e->y());
252 
253  if (!m_MoverController.hasActiveMover()) {
254  return;
255  }
256  m_MoverController.move(userInput);
257  m_GlView.setDistMinAndMax(m_World.boundingBox());
258  updateGL();
259 }
260 
261 void ModelViewGadgetWidget::mouseReleaseEvent(QMouseEvent *)
262 {
263  if (!m_MoverController.hasActiveMover()) {
264  return;
265  }
266  m_MoverController.setNoMover();
267  m_MotionTimer.start();
268  updateGL();
269 }
270 
271 void ModelViewGadgetWidget::keyPressEvent(QKeyEvent *e) // switch between camera
272 {
273  if (e->key() == Qt::Key_1) {
274  m_GlView.cameraHandle()->setIsoView();
275  updateGL();
276  }
277  if (e->key() == Qt::Key_2) {
278  m_GlView.cameraHandle()->setFrontView();
279  updateGL();
280  }
281  if (e->key() == Qt::Key_3) {
282  m_GlView.cameraHandle()->setIsoView();
283  m_GlView.cameraHandle()->rotateAroundTarget(glc::Z_AXIS, glc::toRadian(90));
284  updateGL();
285  }
286  if (e->key() == Qt::Key_4) {
287  m_GlView.cameraHandle()->setLeftView();
288  updateGL();
289  }
290  if (e->key() == Qt::Key_5) {
291  m_GlView.cameraHandle()->setTopView();
292  m_GlView.cameraHandle()->rotateAroundTarget(glc::Z_AXIS, glc::toRadian(180));
293  updateGL();
294  }
295  if (e->key() == Qt::Key_6) {
296  m_GlView.cameraHandle()->setRightView();
297  ;
298  updateGL();
299  }
300  if (e->key() == Qt::Key_7) {
301  m_GlView.cameraHandle()->setIsoView();
302  m_GlView.cameraHandle()->rotateAroundTarget(glc::Z_AXIS, glc::toRadian(-90));
303  updateGL();
304  }
305  if (e->key() == Qt::Key_8) {
306  m_GlView.cameraHandle()->setRearView();
307  ;
308  updateGL();
309  }
310  if (e->key() == Qt::Key_9) {
311  m_GlView.cameraHandle()->setIsoView();
312  m_GlView.cameraHandle()->rotateAroundTarget(glc::Z_AXIS, glc::toRadian(180));
313  updateGL();
314  }
315  if (e->key() == Qt::Key_0) {
316  m_GlView.cameraHandle()->setBottomView();
317  m_GlView.cameraHandle()->rotateAroundTarget(glc::Z_AXIS, glc::toRadian(180));
318  updateGL();
319  }
320 }
321 
323 // Private slots Functions
326 {
327  AttitudeActual::DataFields data = attState->getData(); // get attitude data
328  GLC_StructOccurence *rootObject = m_World.rootOccurence(); // get the full 3D model
329  double x = data.q3;
330  double y = data.q2;
331  double z = data.q4;
332  double w = data.q1;
333 
334  if (w == 0.0) {
335  w = 1.0;
336  }
337  // create and gives the product of 2 4x4 matrices to get the rotation of the 3D model's matrix
338  QMatrix4x4 m1;
339  m1.setRow(0, QVector4D(w, z, -y, x));
340  m1.setRow(1, QVector4D(-z, w, x, y));
341  m1.setRow(2, QVector4D(y, -x, w, z));
342  m1.setRow(3, QVector4D(-x, -y, -z, w));
343  QMatrix4x4 m2;
344  m2.setRow(0, QVector4D(w, z, -y, -x));
345  m2.setRow(1, QVector4D(-z, w, x, -y));
346  m2.setRow(2, QVector4D(y, -x, w, -z));
347  m2.setRow(3, QVector4D(x, y, z, w));
348  QMatrix4x4 m0 = m1 * m2;
349  // convert QMatrix4x4 to GLC_Matrix4x4
350  GLC_Matrix4x4 rootObjectRotation(m0.data());
351  rootObject->structInstance()->setMatrix(rootObjectRotation);
352  rootObject->updateChildrenAbsoluteMatrix();
353  updateGL();
354 }
Core plugin system that manages the plugins, their life cycle and their registered objects...
Definition: pluginmanager.h:53
DataFields data
z
Definition: OPPlots.m:102
ModelViewGadgetWidget(QWidget *parent=nullptr)
void setAcFilename(QString acf)
delta
Definition: OPPlots.m:114
void setBgFilename(QString bgf)
else error('Your technical computing program does not support file choosers.Please input the file name in the argument. ') end elseif nargin >0 logfile
x
Definition: OPPlots.m:100
void updateAttitude(int value)
e
Definition: OPPlots.m:99
y
Definition: OPPlots.m:101