g2o
g2o_qglviewer.cpp
Go to the documentation of this file.
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3 //
4 // This file is part of g2o.
5 //
6 // g2o is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // g2o is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with g2o. If not, see <http://www.gnu.org/licenses/>.
18 
19 #include "g2o_qglviewer.h"
23 
24 // some macro helpers for identifying the version number of QGLViewer
25 // QGLViewer changed some parts of its API in version 2.6.
26 // The following preprocessor hack accounts for this. THIS SUCKS!!!
27 #if (((QGLVIEWER_VERSION & 0xff0000) >> 16) >= 2 && ((QGLVIEWER_VERSION & 0x00ff00) >> 8) >= 6)
28 #define qglv_real qreal
29 #else
30 #define qglv_real float
31 #endif
32 
33 // Again, some API changes in QGLViewer which produce annoying text in the console
34 // if the old API is used.
35 #if (((QGLVIEWER_VERSION & 0xff0000) >> 16) >= 2 && ((QGLVIEWER_VERSION & 0x00ff00) >> 8) >= 5)
36 #define QGLVIEWER_DEPRECATED_MOUSEBINDING
37 #endif
38 
39 #ifdef __APPLE__
40 #include <OpenGL/gl.h>
41 #else
42 #include <GL/gl.h>
43 #include <GL/glu.h>
44 #endif
45 
46 #include <iostream>
47 using namespace std;
48 
49 namespace g2o {
50 
51 namespace {
52 
56  class StandardCamera : public qglviewer::Camera
57  {
58  public:
59  StandardCamera() : _standard(true) {};
60 
61  qglv_real zNear() const {
62  if (_standard)
63  return qglv_real(0.001);
64  else
65  return Camera::zNear();
66  }
67 
68  qglv_real zFar() const
69  {
70  if (_standard)
71  return qglv_real(10000.0);
72  else
73  return Camera::zFar();
74  }
75 
76  bool standard() const {return _standard;}
77  void setStandard(bool s) { _standard = s;}
78 
79  private:
80  bool _standard;
81  };
82 
83 } // end anonymous namespace
84 
85 G2oQGLViewer::G2oQGLViewer(QWidget* parent, const QGLWidget* shareWidget, Qt::WindowFlags flags) :
86  QGLViewer(parent, shareWidget, flags),
87  graph(0), _drawActions(0), _drawList(0)
88 {
89  setAxisIsDrawn(false);
91 }
92 
94 {
95  delete _drawActionParameters;
96  glDeleteLists(_drawList, 1);
97 }
98 
100 {
101  if (! graph)
102  return;
103 
104  if (_drawActions == 0) {
106  assert(_drawActions);
107  }
108 
109  if (! _drawActions) // avoid segmentation fault in release build
110  return;
111  if (_updateDisplay) {
112  _updateDisplay = false;
113  glNewList(_drawList, GL_COMPILE_AND_EXECUTE);
115  glEndList();
116  } else {
117  glCallList(_drawList);
118  }
119 }
120 
122 {
123  QGLViewer::init();
124  //glDisable(GL_LIGHT0);
125  //glDisable(GL_LIGHTING);
126 
127  setBackgroundColor(QColor::fromRgb(51, 51, 51));
128 
129  // some default settings i like
130  glEnable(GL_LINE_SMOOTH);
131  glEnable(GL_BLEND);
132  glEnable(GL_DEPTH_TEST);
133  glEnable(GL_NORMALIZE);
134  //glEnable(GL_CULL_FACE);
135  glShadeModel(GL_FLAT);
136  //glShadeModel(GL_SMOOTH);
137  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
138 
139  setAxisIsDrawn();
140 
141  // don't save state
142  setStateFileName(QString::null);
143 
144  // mouse bindings
145 #ifdef QGLVIEWER_DEPRECATED_MOUSEBINDING
146  setMouseBinding(Qt::NoModifier, Qt::RightButton, CAMERA, ZOOM);
147  setMouseBinding(Qt::NoModifier, Qt::MidButton, CAMERA, TRANSLATE);
148 #else
149  setMouseBinding(Qt::RightButton, CAMERA, ZOOM);
150  setMouseBinding(Qt::MidButton, CAMERA, TRANSLATE);
151 #endif
152 
153  // keyboard shortcuts
154  setShortcut(CAMERA_MODE, 0);
155  setShortcut(EXIT_VIEWER, 0);
156  //setShortcut(SAVE_SCREENSHOT, 0);
157 
158  // replace camera
159  qglviewer::Camera* oldcam = camera();
160  qglviewer::Camera* cam = new StandardCamera();
161  setCamera(cam);
162  cam->setPosition(qglviewer::Vec(0., 0., 75.));
163  cam->setUpVector(qglviewer::Vec(0., 1., 0.));
164  cam->lookAt(qglviewer::Vec(0., 0., 0.));
165  delete oldcam;
166 
167  // getting a display list
168  _drawList = glGenLists(1);
169 }
170 
172 {
174 }
175 
176 } // end namespace
SparseOptimizer * graph
Definition: g2o_qglviewer.h:51
void applyAction(HyperGraph *graph, HyperGraphElementAction *action, HyperGraphElementAction::Parameters *params, const std::string &typeName)
#define qglv_real
HyperGraphElementAction * _drawActions
Definition: g2o_qglviewer.h:54
bool updateDisplay() const
Definition: g2o_qglviewer.h:45
void setUpdateDisplay(bool updateDisplay)
static HyperGraphActionLibrary * instance()
return the single instance of the HyperGraphActionLibrary
HyperGraphElementAction * actionByName(const std::string &name)
Protocol The SLAM executable accepts such as solving the and retrieving or vertices in the graph
Definition: protocol.txt:7
bool _standard
DrawAction::Parameters * _drawActionParameters
Definition: g2o_qglviewer.h:57