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 GUIGrabbing; // true when grabbing enabled from GUI
67  bool HeadlessGrabbing; // true when headless grabbing enabled
68  UInt32 HeadlessFrameRate; // Grab every nth frame
69  QString Directory; // output directory
70  QString BaseName; // frame file basename
71  QString Format; // output file format
72  SInt32 Quality; // output quality [0-100]
73  QSize Size; // Frame size
74 
76  GUIGrabbing(false),
77  HeadlessGrabbing(false),
79  Directory("."),
80  BaseName("frame_"),
81  Format("png"),
82  Quality(-1),
83  Size(1600, 1200) {}
84 
85  void Init(TConfigurationNode& t_tree);
86  };
87 
91  struct SSelectionInfo {
92  bool IsSelected; // true when an entity is selected
93  CEntity* Entity; // the selected entity
94 
96  IsSelected(false),
97  Entity(NULL) {}
98  };
99 
100  public:
101 
108  CQTOpenGLWidget(QWidget* pc_parent,
109  CQTOpenGLMainWindow& c_main_window,
110  CQTOpenGLUserFunctions& c_user_functions);
114  virtual ~CQTOpenGLWidget();
115 
121  virtual void initializeGL();
122 
126  virtual void paintGL();
127 
132  CRay3 RayFromWindowCoord(int n_x,
133  int n_y);
134 
139  int n_y);
140 
146 
151  void SelectEntity(CEntity& c_entity);
152 
157  void DeselectEntity();
158 
164  void SelectInScene(UInt32 un_x,
165  UInt32 un_y);
166 
170  void DrawEntity(CPositionalEntity& c_entity);
171 
175  void DrawEntity(CEmbodiedEntity& c_entity);
176 
180  void DrawRays(CControllableEntity& c_entity);
181 
185  void DrawBoundingBox(CEmbodiedEntity& c_entity);
186 
190  inline virtual int heightForWidth(int w) const {
191  return (w * 3) / 4;
192  }
193 
197  inline virtual QSize sizeHint() const {
198  return QSize(1024,768);
199  }
200 
204  inline virtual QSize minimumSize() const {
205  return QSize(320,240);
206  }
207 
212  return m_cUserFunctions;
213  }
214 
219  return m_cCamera;
220  }
221 
226  return m_sFrameGrabData;
227  }
228 
232  inline void SetInvertMouse(bool b_invert_mouse) {
233  m_bInvertMouse = b_invert_mouse;
234  }
235 
239  inline void SetShowBoundary(bool b_show_boundary) {
240  m_bShowBoundary = b_show_boundary;
241  }
242 
243  signals:
244 
249  void StepDone(int n_step);
250 
257 
262  void EntitySelected(CEntity* pc_entity);
263 
268  void EntityDeselected(CEntity* pc_entity);
269 
270  public slots:
271 
277  void PlayExperiment();
278 
283  void FastForwardExperiment();
284 
290  void PauseExperiment();
291 
295  void StepExperiment();
296 
301  void ResetExperiment();
302 
306  void SetDrawFrameEvery(int n_every);
307 
311  void SetGrabFrame(bool b_grab_on);
312 
317  void SetCamera(int n_camera);
318 
323  void SetCameraFocalLength(double f_length);
324 
328  void KeyPressed(QKeyEvent* pc_event);
329 
333  void KeyReleased(QKeyEvent* pc_event);
334 
335  protected:
336 
337  void DrawScene();
338  void DrawArena();
339  void DrawAxes();
340 
341  virtual void timerEvent(QTimerEvent* pc_event);
342  virtual void mousePressEvent(QMouseEvent* pc_event);
343  virtual void mouseReleaseEvent(QMouseEvent* pc_event);
344  virtual void mouseMoveEvent(QMouseEvent* pc_event);
345  virtual void wheelEvent(QWheelEvent *pc_event);
346  virtual void keyPressEvent(QKeyEvent* pc_event);
347  virtual void keyReleaseEvent(QKeyEvent* pc_event);
348  virtual void resizeEvent(QResizeEvent* pc_event);
349  void reactToKeyEvent();
350 
351  private:
352 
354  CQTOpenGLMainWindow& m_cMainWindow;
356  CQTOpenGLUserFunctions& m_cUserFunctions;
357 
359  SInt32 nTimerId;
361  bool m_bFastForwarding;
363  SInt32 m_nDrawFrameEvery;
365  SInt32 m_nFrameCounter;
366 
368  bool m_bMouseGrabbed;
370  bool m_bShiftPressed;
372  QPoint m_cMouseGrabPos;
374  bool m_bInvertMouse;
376  SSelectionInfo m_sSelectionInfo;
377 
379  CSimulator& m_cSimulator;
381  CSpace& m_cSpace;
382 
384  bool m_bShowBoundary;
385 
387  bool m_bUsingFloorTexture;
389  QOpenGLTexture* m_pcFloorTexture;
391  QOpenGLTexture* m_pcGroundTexture;
392 
394  GLfloat* m_pfLightAmbient;
396  GLfloat* m_pfLightDiffuse;
398  GLfloat* m_pfLight0Position;
400  GLfloat* m_pfLight1Position;
401 
403  //GLuint m_unArenaList;
405  //GLuint m_unFloorList;
406 
408  CQTOpenGLCamera m_cCamera;
410  SFrameGrabData m_sFrameGrabData;
411 
413  enum EDirection {
414  DIRECTION_UP = 1,
415  DIRECTION_DOWN,
416  DIRECTION_LEFT,
417  DIRECTION_RIGHT,
418  DIRECTION_FORWARDS,
419  DIRECTION_BACKWARDS,
420  };
421 
423  QMap<EDirection, int> m_mapPressedKeys;
424  };
425 
426 }
427 
428 #endif
signed int SInt32
32-bit signed integer.
Definition: datatypes.h:93
unsigned int UInt32
32-bit unsigned integer.
Definition: datatypes.h:97
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
An entity that contains a pointer to the user-defined controller.
This entity is a link to a body in the physics engine.
The basic entity type.
Definition: entity.h:90
The basic operation to be stored in the vtable.
Definition: entity.h:331
The core class of ARGOS.
Definition: simulator.h:62
A 3D vector class.
Definition: vector3.h:31
The QTOpenGL user functions.
CEntity * GetSelectedEntity()
Returns the currently selected entity, or NULL if none is selected.
void StepExperiment()
Executes one experiment time step.
void SetShowBoundary(bool b_show_boundary)
Sets whether the boundary walls should be rendered.
CVector3 GetWindowCoordInWorld(int n_x, int n_y)
Returns the position in the world corresponding to the given window coordinate.
virtual void mouseReleaseEvent(QMouseEvent *pc_event)
virtual void wheelEvent(QWheelEvent *pc_event)
virtual QSize sizeHint() const
Called internally by Qt to know the preferred widget size.
void KeyPressed(QKeyEvent *pc_event)
Handles key press events.
CRay3 RayFromWindowCoord(int n_x, int n_y)
Casts a ray from the given window coordinate.
virtual ~CQTOpenGLWidget()
Class destructor.
void SelectEntity(CEntity &c_entity)
Selects the passed entity.
void KeyReleased(QKeyEvent *pc_event)
Handles key release events.
void SetCameraFocalLength(double f_length)
Sets the focal length of the current camera.
void FastForwardExperiment()
Fast forwards the experiment.
CQTOpenGLUserFunctions & GetUserFunctions()
Returns a reference to the user functions.
void EntitySelected(CEntity *pc_entity)
Emitted when an entity is selected.
virtual QSize minimumSize() const
Called internally by Qt to know the minimum widget size.
void DrawRays(CControllableEntity &c_entity)
Draws a ray.
void PlayExperiment()
Plays the experiment.
void ResetExperiment()
Resets the state of the experiment to its state right after initialization.
virtual void mouseMoveEvent(QMouseEvent *pc_event)
void EntityDeselected(CEntity *pc_entity)
Emitted when an entity is deselected.
void DrawEntity(CPositionalEntity &c_entity)
Draws a positional entity.
void DeselectEntity()
Deselects the currently selected entity.
void SetGrabFrame(bool b_grab_on)
Toggles frame grabbing.
CQTOpenGLCamera & GetCamera()
Returns a reference to the camera.
void SetInvertMouse(bool b_invert_mouse)
Sets whether the mouse should be inverted when moving.
virtual void keyReleaseEvent(QKeyEvent *pc_event)
SFrameGrabData & GetFrameGrabData()
Returns the current frame grabbing data.
void DrawBoundingBox(CEmbodiedEntity &c_entity)
Draws the bounding box of an embodied entity.
virtual void keyPressEvent(QKeyEvent *pc_event)
void SetDrawFrameEvery(int n_every)
When fast-forwarding, sets every how many steps a frame must be drawn.
void SelectInScene(UInt32 un_x, UInt32 un_y)
Selects the entity closest to the camera at the given screen coordinates.
virtual void paintGL()
Logic for scene drawing.
CQTOpenGLWidget(QWidget *pc_parent, CQTOpenGLMainWindow &c_main_window, CQTOpenGLUserFunctions &c_user_functions)
Class constructor.
virtual int heightForWidth(int w) const
Called internally by Qt to set the aspect ratio.
void ExperimentDone()
Emitted when the experiment is finished.
virtual void initializeGL()
Called when the GL context must be initialized.
virtual void mousePressEvent(QMouseEvent *pc_event)
void PauseExperiment()
Pauses the experiment.
virtual void resizeEvent(QResizeEvent *pc_event)
void StepDone(int n_step)
Emitted whenever a time step has been executed.
void SetCamera(int n_camera)
Sets the current camera in use.
virtual void timerEvent(QTimerEvent *pc_event)
Data regarding frame grabbing.
void Init(TConfigurationNode &t_tree)
Data arelated to robot selection.