ARGoS  3
A parallel, multi-engine simulator for swarm robotics
qtopengl_widget.h
Go to the documentation of this file.
1 
7 #ifndef QTOPENGL_WIDGET_H
8 #define QTOPENGL_WIDGET_H
9 
10 namespace argos {
11  class CQTOpenGLWidget;
12  class CQTOpenGLMainWindow;
13  class CSpace;
14  class CSimulator;
15  class CQTOpenGLBox;
16  class CQTOpenGLUserFunctions;
17  class CPositionalEntity;
18  class CControllableEntity;
19  class CEmbodiedEntity;
20 }
21 
22 #include <argos3/plugins/simulator/visualizations/qt-opengl/qtopengl_camera.h>
23 #include <argos3/core/simulator/entity/entity.h>
24 #include <argos3/core/utility/datatypes/datatypes.h>
25 #include <QOpenGLWidget>
26 #include <QOpenGLFunctions>
27 #include <QOpenGLTexture>
28 
29 #ifdef __APPLE__
30 #include <glu.h>
31 #else
32 #include <GL/glu.h>
33 #endif
34 
35 namespace argos {
36 
37  /****************************************/
38  /****************************************/
39 
40  class CQTOpenGLOperationDrawNormal : public CEntityOperation<CQTOpenGLOperationDrawNormal, CQTOpenGLWidget, void> {
41  public:
43  };
44 
45  class CQTOpenGLOperationDrawSelected : public CEntityOperation<CQTOpenGLOperationDrawSelected, CQTOpenGLWidget, void> {
46  public:
48  };
49 
50 #define REGISTER_QTOPENGL_ENTITY_OPERATION(ACTION, OPERATION, ENTITY) \
51  REGISTER_ENTITY_OPERATION(ACTION, CQTOpenGLWidget, OPERATION, void, ENTITY);
52 
53  /****************************************/
54  /****************************************/
55 
56  class CQTOpenGLWidget : public QOpenGLWidget, protected QOpenGLFunctions {
57 
58  Q_OBJECT
59 
60  public:
61 
65  struct SFrameGrabData {
66  bool Grabbing; // true when grabbing
67  QString Directory; // output directory
68  QString BaseName; // frame file basename
69  QString Format; // output file format
70  SInt32 Quality; // output quality [0-100]
71 
73  Grabbing(false),
74  Directory("."),
75  BaseName("frame_"),
76  Format("png"),
77  Quality(-1) {}
78 
79  void Init(TConfigurationNode& t_tree);
80  };
81 
85  struct SSelectionInfo {
86  bool IsSelected; // true when a robot is selected
87  size_t Index; // the index of the selected robot
88 
90  IsSelected(false),
91  Index(0) {}
92  };
93 
94  public:
95 
102  CQTOpenGLWidget(QWidget* pc_parent,
103  CQTOpenGLMainWindow& c_main_window,
104  CQTOpenGLUserFunctions& c_user_functions);
108  virtual ~CQTOpenGLWidget();
109 
115  virtual void initializeGL();
116 
120  virtual void paintGL();
121 
126  CRay3 RayFromWindowCoord(int n_x,
127  int n_y);
128 
133  int n_y);
134 
140 
145  void SelectEntity(CEntity& c_entity);
146 
151  void DeselectEntity();
152 
158  void SelectInScene(UInt32 un_x,
159  UInt32 un_y);
160 
164  void DrawEntity(CPositionalEntity& c_entity);
165 
169  void DrawEntity(CEmbodiedEntity& c_entity);
170 
174  void DrawRays(CControllableEntity& c_entity);
175 
179  void DrawBoundingBox(CEmbodiedEntity& c_entity);
180 
184  inline virtual int heightForWidth(int w) const {
185  return (w * 3) / 4;
186  }
187 
191  inline virtual QSize sizeHint() const {
192  return QSize(1024,768);
193  }
194 
198  inline virtual QSize minimumSize() const {
199  return QSize(320,240);
200  }
201 
206  return m_cUserFunctions;
207  }
208 
213  return m_cCamera;
214  }
215 
220  return m_sFrameGrabData;
221  }
222 
226  inline void SetInvertMouse(bool b_InvertMouse) {
227  m_bInvertMouse = b_InvertMouse;
228  }
229 
230  signals:
231 
236  void StepDone(int n_step);
237 
243  void ExperimentDone();
244 
249  void EntitySelected(size_t un_index);
250 
255  void EntityDeselected(size_t un_index);
256 
257  public slots:
258 
264  void PlayExperiment();
265 
270  void FastForwardExperiment();
271 
277  void PauseExperiment();
278 
282  void StepExperiment();
283 
288  void ResetExperiment();
289 
293  void SetDrawFrameEvery(int n_every);
294 
298  void SetGrabFrame(bool b_grab_on);
299 
304  void SetCamera(int n_camera);
305 
310  void SetCameraFocalLength(double f_length);
311 
315  void KeyPressed(QKeyEvent* pc_event);
316 
320  void KeyReleased(QKeyEvent* pc_event);
321 
322  protected:
323 
324  void DrawScene();
325  void DrawArena();
326  void DrawAxes();
327 
328  virtual void timerEvent(QTimerEvent* pc_event);
329  virtual void mousePressEvent(QMouseEvent* pc_event);
330  virtual void mouseReleaseEvent(QMouseEvent* pc_event);
331  virtual void mouseMoveEvent(QMouseEvent* pc_event);
332  virtual void keyPressEvent(QKeyEvent* pc_event);
333  virtual void keyReleaseEvent(QKeyEvent* pc_event);
334  virtual void resizeEvent(QResizeEvent* pc_event);
335  void reactToKeyEvent();
336 
337  private:
338 
340  CQTOpenGLMainWindow& m_cMainWindow;
342  CQTOpenGLUserFunctions& m_cUserFunctions;
343 
345  SInt32 nTimerId;
347  bool m_bFastForwarding;
349  SInt32 m_nDrawFrameEvery;
351  SInt32 m_nFrameCounter;
352 
354  bool m_bMouseGrabbed;
356  bool m_bShiftPressed;
358  QPoint m_cMouseGrabPos;
360  bool m_bInvertMouse;
362  SSelectionInfo m_sSelectionInfo;
363 
365  CSimulator& m_cSimulator;
367  CSpace& m_cSpace;
368 
370  bool m_bUsingFloorTexture;
372  QOpenGLTexture* m_pcFloorTexture;
374  QOpenGLTexture* m_pcGroundTexture;
375 
377  GLfloat* m_pfLightAmbient;
379  GLfloat* m_pfLightDiffuse;
381  GLfloat* m_pfLight0Position;
383  GLfloat* m_pfLight1Position;
384 
386  GLuint m_unArenaList;
388  GLuint m_unFloorList;
390  GLuint* m_punSelectionBuffer;
391 
393  CQTOpenGLCamera m_cCamera;
395  SFrameGrabData m_sFrameGrabData;
396 
398  enum EDirection {
399  DIRECTION_UP = 1,
400  DIRECTION_DOWN,
401  DIRECTION_LEFT,
402  DIRECTION_RIGHT,
403  DIRECTION_FORWARDS,
404  DIRECTION_BACKWARDS,
405  };
406 
408  QMap<EDirection, int> m_mapPressedKeys;
409 
410  CRay3 m_cSelectionRay;
411  };
412 
413 }
414 
415 #endif
argos::CQTOpenGLWidget::SSelectionInfo::IsSelected
bool IsSelected
Definition: qtopengl_widget.h:86
argos::CQTOpenGLWidget::SFrameGrabData::Init
void Init(TConfigurationNode &t_tree)
Definition: qtopengl_widget.cpp:1055
argos::CEntityOperation
The basic operation to be stored in the vtable.
Definition: entity.h:278
argos::CQTOpenGLWidget::PauseExperiment
void PauseExperiment()
Pauses the experiment.
Definition: qtopengl_widget.cpp:613
argos::CQTOpenGLWidget::keyReleaseEvent
virtual void keyReleaseEvent(QKeyEvent *pc_event)
Definition: qtopengl_widget.cpp:1013
argos::CQTOpenGLWidget::initializeGL
virtual void initializeGL()
Called when the GL context must be initialized.
Definition: qtopengl_widget.cpp:93
argos
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
argos::CVector3
A 3D vector class.
Definition: vector3.h:29
argos::CSpace
Definition: space.h:30
argos::CQTOpenGLCamera
Definition: qtopengl_camera.h:31
argos::CQTOpenGLWidget
Definition: qtopengl_widget.h:56
argos::CQTOpenGLWidget::DrawArena
void DrawArena()
Definition: qtopengl_widget.cpp:783
argos::CRay3
Definition: ray3.h:19
argos::CQTOpenGLOperationDrawNormal
Definition: qtopengl_widget.h:40
argos::CQTOpenGLWidget::SetCamera
void SetCamera(int n_camera)
Sets the current camera in use.
Definition: qtopengl_widget.cpp:671
argos::CSimulator
The core class of ARGOS.
Definition: simulator.h:62
argos::CEmbodiedEntity
This entity is a link to a body in the physics engine.
Definition: embodied_entity.h:48
argos::CQTOpenGLWidget::SetGrabFrame
void SetGrabFrame(bool b_grab_on)
Toggles frame grabbing.
Definition: qtopengl_widget.cpp:664
argos::CQTOpenGLWidget::SSelectionInfo
Data arelated to robot selection.
Definition: qtopengl_widget.h:85
argos::TConfigurationNode
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
Definition: argos_configuration.h:27
argos::CQTOpenGLOperationDrawSelected::~CQTOpenGLOperationDrawSelected
virtual ~CQTOpenGLOperationDrawSelected()
Definition: qtopengl_widget.h:47
argos::CQTOpenGLWidget::minimumSize
virtual QSize minimumSize() const
Called internally by Qt to know the minimum widget size.
Definition: qtopengl_widget.h:198
argos::CQTOpenGLWidget::SelectEntity
void SelectEntity(CEntity &c_entity)
Selects the passed entity.
Definition: qtopengl_widget.cpp:320
argos::CQTOpenGLWidget::DrawEntity
void DrawEntity(CPositionalEntity &c_entity)
Draws a positional entity.
Definition: qtopengl_widget.cpp:468
argos::CQTOpenGLWidget::GetSelectedEntity
CEntity * GetSelectedEntity()
Returns the currently selected entity, or NULL if none is selected.
Definition: qtopengl_widget.cpp:311
argos::CQTOpenGLWidget::mousePressEvent
virtual void mousePressEvent(QMouseEvent *pc_event)
Definition: qtopengl_widget.cpp:898
argos::CQTOpenGLWidget::ExperimentDone
void ExperimentDone()
Emitted when the experiment is finished.
argos::CQTOpenGLWidget::DrawBoundingBox
void DrawBoundingBox(CEmbodiedEntity &c_entity)
Draws the bounding box of an embodied entity.
Definition: qtopengl_widget.cpp:538
argos::CPositionalEntity
Definition: positional_entity.h:20
argos::CQTOpenGLWidget::resizeEvent
virtual void resizeEvent(QResizeEvent *pc_event)
Definition: qtopengl_widget.cpp:1045
argos::CQTOpenGLWidget::FastForwardExperiment
void FastForwardExperiment()
Fast forwards the experiment.
Definition: qtopengl_widget.cpp:603
argos::CQTOpenGLWidget::SetCameraFocalLength
void SetCameraFocalLength(double f_length)
Sets the focal length of the current camera.
Definition: qtopengl_widget.cpp:680
argos::CQTOpenGLWidget::SFrameGrabData::SFrameGrabData
SFrameGrabData()
Definition: qtopengl_widget.h:72
argos::CQTOpenGLWidget::SSelectionInfo::SSelectionInfo
SSelectionInfo()
Definition: qtopengl_widget.h:89
argos::CQTOpenGLOperationDrawNormal::~CQTOpenGLOperationDrawNormal
virtual ~CQTOpenGLOperationDrawNormal()
Definition: qtopengl_widget.h:42
argos::CQTOpenGLWidget::EntityDeselected
void EntityDeselected(size_t un_index)
Emitted when an entity is deselected.
argos::CQTOpenGLWidget::RayFromWindowCoord
CRay3 RayFromWindowCoord(int n_x, int n_y)
Casts a ray from the given window coordinate.
Definition: qtopengl_widget.cpp:223
argos::CQTOpenGLWidget::mouseReleaseEvent
virtual void mouseReleaseEvent(QMouseEvent *pc_event)
Definition: qtopengl_widget.cpp:920
argos::CQTOpenGLWidget::KeyReleased
void KeyReleased(QKeyEvent *pc_event)
Handles key release events.
Definition: qtopengl_widget.cpp:737
argos::CQTOpenGLWidget::paintGL
virtual void paintGL()
Logic for scene drawing.
Definition: qtopengl_widget.cpp:133
argos::CQTOpenGLWidget::~CQTOpenGLWidget
virtual ~CQTOpenGLWidget()
Class destructor.
Definition: qtopengl_widget.cpp:78
argos::CQTOpenGLWidget::GetWindowCoordInWorld
CVector3 GetWindowCoordInWorld(int n_x, int n_y)
Returns the position in the world corresponding to the given window coordinate.
Definition: qtopengl_widget.cpp:269
argos::CQTOpenGLUserFunctions
The QTOpenGL user functions.
Definition: qtopengl_user_functions.h:74
argos::CQTOpenGLWidget::DeselectEntity
void DeselectEntity()
Deselects the currently selected entity.
Definition: qtopengl_widget.cpp:350
argos::CQTOpenGLWidget::keyPressEvent
virtual void keyPressEvent(QKeyEvent *pc_event)
Definition: qtopengl_widget.cpp:1006
argos::CQTOpenGLWidget::DrawAxes
void DrawAxes()
Definition: qtopengl_widget.cpp:885
SInt32
signed int SInt32
32-bit signed integer.
Definition: datatypes.h:93
argos::CQTOpenGLWidget::SelectInScene
void SelectInScene(UInt32 un_x, UInt32 un_y)
Selects the entity closest to the camera at the given screen coordinates.
Definition: qtopengl_widget.cpp:364
argos::CQTOpenGLWidget::timerEvent
virtual void timerEvent(QTimerEvent *pc_event)
Definition: qtopengl_widget.cpp:891
argos::CQTOpenGLWidget::sizeHint
virtual QSize sizeHint() const
Called internally by Qt to know the preferred widget size.
Definition: qtopengl_widget.h:191
argos::CQTOpenGLMainWindow
Definition: qtopengl_main_window.h:36
argos::CEntity
The basic entity type.
Definition: entity.h:89
argos::CQTOpenGLWidget::StepExperiment
void StepExperiment()
Executes one experiment time step.
Definition: qtopengl_widget.cpp:622
argos::CQTOpenGLWidget::SFrameGrabData::Directory
QString Directory
Definition: qtopengl_widget.h:67
argos::CQTOpenGLWidget::ResetExperiment
void ResetExperiment()
Resets the state of the experiment to its state right after initialization.
Definition: qtopengl_widget.cpp:646
argos::CQTOpenGLWidget::SFrameGrabData::BaseName
QString BaseName
Definition: qtopengl_widget.h:68
UInt32
unsigned int UInt32
32-bit unsigned integer.
Definition: datatypes.h:97
argos::CQTOpenGLWidget::reactToKeyEvent
void reactToKeyEvent()
Definition: qtopengl_widget.cpp:1020
argos::CQTOpenGLWidget::SetDrawFrameEvery
void SetDrawFrameEvery(int n_every)
When fast-forwarding, sets every how many steps a frame must be drawn.
Definition: qtopengl_widget.cpp:657
argos::CQTOpenGLWidget::SetInvertMouse
void SetInvertMouse(bool b_InvertMouse)
Sets whether the mouse should be inverted when moving.
Definition: qtopengl_widget.h:226
argos::CQTOpenGLWidget::PlayExperiment
void PlayExperiment()
Plays the experiment.
Definition: qtopengl_widget.cpp:594
argos::CQTOpenGLOperationDrawSelected
Definition: qtopengl_widget.h:45
argos::CQTOpenGLWidget::SFrameGrabData::Grabbing
bool Grabbing
Definition: qtopengl_widget.h:66
argos::CQTOpenGLWidget::GetUserFunctions
CQTOpenGLUserFunctions & GetUserFunctions()
Returns a reference to the user functions.
Definition: qtopengl_widget.h:205
argos::CQTOpenGLWidget::GetCamera
CQTOpenGLCamera & GetCamera()
Returns a reference to the camera.
Definition: qtopengl_widget.h:212
argos::CQTOpenGLWidget::CQTOpenGLWidget
CQTOpenGLWidget(QWidget *pc_parent, CQTOpenGLMainWindow &c_main_window, CQTOpenGLUserFunctions &c_user_functions)
Class constructor.
Definition: qtopengl_widget.cpp:38
argos::CQTOpenGLWidget::heightForWidth
virtual int heightForWidth(int w) const
Called internally by Qt to set the aspect ratio.
Definition: qtopengl_widget.h:184
argos::CQTOpenGLWidget::EntitySelected
void EntitySelected(size_t un_index)
Emitted when an entity is selected.
argos::CQTOpenGLWidget::SSelectionInfo::Index
size_t Index
Definition: qtopengl_widget.h:87
argos::CControllableEntity
An entity that contains a pointer to the user-defined controller.
Definition: controllable_entity.h:26
argos::CQTOpenGLWidget::DrawScene
void DrawScene()
argos::CQTOpenGLWidget::StepDone
void StepDone(int n_step)
Emitted whenever a time step has been executed.
argos::CQTOpenGLWidget::SFrameGrabData::Format
QString Format
Definition: qtopengl_widget.h:69
argos::CQTOpenGLWidget::SFrameGrabData
Data regarding frame grabbing.
Definition: qtopengl_widget.h:65
argos::CQTOpenGLWidget::DrawRays
void DrawRays(CControllableEntity &c_entity)
Draws a ray.
Definition: qtopengl_widget.cpp:504
argos::CQTOpenGLWidget::SFrameGrabData::Quality
SInt32 Quality
Definition: qtopengl_widget.h:70
argos::CQTOpenGLWidget::mouseMoveEvent
virtual void mouseMoveEvent(QMouseEvent *pc_event)
Definition: qtopengl_widget.cpp:972
argos::CQTOpenGLWidget::KeyPressed
void KeyPressed(QKeyEvent *pc_event)
Handles key press events.
Definition: qtopengl_widget.cpp:691
argos::CQTOpenGLWidget::GetFrameGrabData
SFrameGrabData & GetFrameGrabData()
Returns the current frame grabbing data.
Definition: qtopengl_widget.h:219