ARGoS  3
A parallel, multi-engine simulator for swarm robotics
qtopengl_spiri.cpp
Go to the documentation of this file.
1 
7 #include "qtopengl_spiri.h"
8 #include "spiri_entity.h"
9 #include <argos3/core/simulator/entity/embodied_entity.h>
10 #include <argos3/core/utility/math/vector2.h>
11 #include <argos3/core/utility/math/vector3.h>
12 #include <argos3/plugins/simulator/entities/led_equipped_entity.h>
13 #include <argos3/plugins/simulator/visualizations/qt-opengl/qtopengl_widget.h>
14 
15 namespace argos {
16 
17  /****************************************/
18  /****************************************/
19 
20  /* All measures are in meters */
21  static const GLfloat SIDE = 0.470f;
22  static const GLfloat DIAGONAL = SIDE * ::sqrt(2);
23  static const GLfloat OUT_RADIUS = DIAGONAL / 5.0f;
24  static const GLfloat IN_RADIUS = OUT_RADIUS * 0.95f;
25  static const GLfloat HEIGHT = 0.090f;
26 
27  static const GLfloat PROP_HEIGHT = HEIGHT / 2.0f;
28  static const GLfloat PROP_ELEVATION = HEIGHT - PROP_HEIGHT;
29  static const GLfloat PROP_ARM = DIAGONAL / 2.0f - OUT_RADIUS;
30 
31  static const GLfloat LEG_RADIUS = OUT_RADIUS * 0.05f;
32  static const GLfloat LEG_HEIGHT = HEIGHT - PROP_HEIGHT;
33 
34  static const GLfloat BODY_SIDE = OUT_RADIUS;
35  static const GLfloat BODY_HEIGHT = HEIGHT * 3.0f / 4.0f;
36  static const GLfloat BODY_ELEVATION = HEIGHT - BODY_HEIGHT;
37 
38  /****************************************/
39  /****************************************/
40 
42  m_unVertices(40) {
43  /* Reserve the needed display lists */
44  m_unList = glGenLists(1);
45  glNewList(m_unList, GL_COMPILE);
46  MakeModel();
47  glEndList();
48  }
49 
50  /****************************************/
51  /****************************************/
52 
54  glDeleteLists(m_unList, 1);
55  }
56 
57  /****************************************/
58  /****************************************/
59 
61  glCallList(m_unList);
62  /* Save attributes and current matrix */
63  glPushAttrib(GL_LINE_BIT);
64  /* Set line attributes */
65  glEnable(GL_LINE_SMOOTH);
66  glLineWidth(2.0);
67  /* Set color */
68  SetMainBodyMaterial();
69  /* Draw ray */
70  glBegin(GL_LINES);
71  glVertex3f(0,0,0);
72  glVertex3f(DIAGONAL,0,0);
73  glEnd();
74  /* Restore saved stuff */
75  glPopAttrib();
76  }
77 
78  /****************************************/
79  /****************************************/
80 
81  void CQTOpenGLSpiri::SetPlasticMaterial() {
82  const GLfloat pfColor[] = { 1.0f, 1.0f, 1.0f, 1.0f };
83  const GLfloat pfSpecular[] = { 0.9f, 0.9f, 0.9f, 1.0f };
84  const GLfloat pfShininess[] = { 100.0f };
85  const GLfloat pfEmission[] = { 0.0f, 0.0f, 0.0f, 1.0f };
86  glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, pfColor);
87  glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, pfSpecular);
88  glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, pfShininess);
89  glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, pfEmission);
90  }
91 
92  /****************************************/
93  /****************************************/
94 
95  void CQTOpenGLSpiri::SetPropellerMaterial() {
96  const GLfloat pfColor[] = { 0.7f, 0.7f, 0.7f, 1.0f };
97  const GLfloat pfSpecular[] = { 0.9f, 0.9f, 0.9f, 1.0f };
98  const GLfloat pfShininess[] = { 100.0f };
99  const GLfloat pfEmission[] = { 0.0f, 0.0f, 0.0f, 1.0f };
100  glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, pfColor);
101  glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, pfSpecular);
102  glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, pfShininess);
103  glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, pfEmission);
104  }
105 
106  /****************************************/
107  /****************************************/
108 
109  void CQTOpenGLSpiri::SetMainBodyMaterial() {
110  const GLfloat pfColor[] = { 0.0f, 0.0f, 1.0f, 1.0f };
111  const GLfloat pfSpecular[] = { 0.9f, 0.9f, 0.9f, 1.0f };
112  const GLfloat pfShininess[] = { 100.0f };
113  const GLfloat pfEmission[] = { 0.0f, 0.0f, 0.0f, 1.0f };
114  glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, pfColor);
115  glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, pfSpecular);
116  glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, pfShininess);
117  glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, pfEmission);
118  }
119 
120  /****************************************/
121  /****************************************/
122 
123  void CQTOpenGLSpiri::MakeModel() {
124  glPushMatrix();
125  glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
126  /* Main body */
127  MakeMainBody();
128  /* Front propeller */
129  glTranslatef(PROP_ARM, 0.0f, 0.0f);
130  MakePropeller();
131  /* Left propeller */
132  glTranslatef(-PROP_ARM, PROP_ARM, 0.0f);
133  MakePropeller();
134  /* Back propeller */
135  glTranslatef(-PROP_ARM, -PROP_ARM, 0.0f);
136  MakePropeller();
137  /* Right propeller */
138  glTranslatef(PROP_ARM, -PROP_ARM, 0.0f);
139  MakePropeller();
140  glTranslatef(0.0f, PROP_ARM, 0.0f);
141  glPopMatrix();
142  }
143 
144  /****************************************/
145  /****************************************/
146 
147  void CQTOpenGLSpiri::MakePropeller() {
148  glPushMatrix();
149  SetPlasticMaterial();
150  /*
151  * Leg
152  */
153  MakeCylinder(LEG_RADIUS, LEG_HEIGHT);
154  /*
155  * Propeller itself
156  */
157  /* Go above the leg */
158  glTranslatef(0.0f, 0.0f, PROP_ELEVATION);
159  /* Draw propeller side surfaces */
160  MakeCylinderSurface(OUT_RADIUS, PROP_HEIGHT);
161  glCullFace(GL_FRONT);
162  MakeCylinderSurface(IN_RADIUS, PROP_HEIGHT);
163  glCullFace(GL_BACK);
164  /* Draw propeller bottom */
165  glRotatef(180.0f, 1.0f, 0.0f, 0.0f);
166  SetPlasticMaterial();
167  MakeRing(OUT_RADIUS, IN_RADIUS);
168  /* Draw rotating propeller */
169  glRotatef(-180.0f, 1.0f, 0.0f, 0.0f);
170  glTranslatef(0.0f, 0.0f, PROP_HEIGHT / 2.0f);
171  SetPropellerMaterial();
172  MakeDisk(IN_RADIUS);
173  /* Draw propeller top */
174  glTranslatef(0.0f, 0.0f, PROP_HEIGHT / 2.0f);
175  SetPlasticMaterial();
176  MakeRing(OUT_RADIUS, IN_RADIUS);
177  glPopMatrix();
178  }
179 
180  /****************************************/
181  /****************************************/
182 
183  void CQTOpenGLSpiri::MakeMainBody() {
184  glPushMatrix();
185  SetMainBodyMaterial();
186  glTranslatef(0.0f, 0.0f, BODY_ELEVATION);
187  MakeBox(BODY_SIDE, BODY_SIDE, BODY_HEIGHT);
188  glPopMatrix();
189  }
190 
191  /****************************************/
192  /****************************************/
193 
194  void CQTOpenGLSpiri::MakeRing(GLfloat f_out_radius,
195  GLfloat f_in_radius) {
196  CVector2 cVertex(1.0f, 0.0f);
197  CRadians cAngle(CRadians::TWO_PI / m_unVertices);
198  glBegin(GL_QUAD_STRIP);
199  glNormal3f(0.0f, 0.0f, 1.0f);
200  for(GLuint i = 0; i <= m_unVertices; i++) {
201  glVertex3f(cVertex.GetX() * f_in_radius, cVertex.GetY() * f_in_radius, 0.0f);
202  glVertex3f(cVertex.GetX() * f_out_radius, cVertex.GetY() * f_out_radius, 0.0f);
203  cVertex.Rotate(cAngle);
204  }
205  glEnd();
206  }
207 
208  /****************************************/
209  /****************************************/
210 
211  void CQTOpenGLSpiri::MakeDisk(GLfloat f_radius) {
212  CVector2 cVertex(f_radius, 0.0f);
213  CRadians cAngle(CRadians::TWO_PI / m_unVertices);
214  glBegin(GL_POLYGON);
215  glNormal3f(0.0f, 0.0f, 1.0f);
216  for(GLuint i = 0; i <= m_unVertices; i++) {
217  glVertex3f(cVertex.GetX(), cVertex.GetY(), 0.0f);
218  cVertex.Rotate(cAngle);
219  }
220  glEnd();
221  }
222 
223  /****************************************/
224  /****************************************/
225 
226  void CQTOpenGLSpiri::MakeCylinderSurface(GLfloat f_radius,
227  GLfloat f_height) {
228  CVector2 cVertex(f_radius, 0.0f);
229  CRadians cAngle(CRadians::TWO_PI / m_unVertices);
230  glEnable(GL_NORMALIZE);
231  glBegin(GL_QUAD_STRIP);
232  for(GLuint i = 0; i <= m_unVertices; i++) {
233  glNormal3f(cVertex.GetX(), cVertex.GetY(), 0.0f);
234  glVertex3f(cVertex.GetX(), cVertex.GetY(), f_height);
235  glVertex3f(cVertex.GetX(), cVertex.GetY(), 0.0f);
236  cVertex.Rotate(cAngle);
237  }
238  glEnd();
239  glDisable(GL_NORMALIZE);
240  }
241 
242  /****************************************/
243  /****************************************/
244 
245  void CQTOpenGLSpiri::MakeCylinder(GLfloat f_radius,
246  GLfloat f_height) {
247  glPushMatrix();
248  MakeDisk(f_radius);
249  MakeCylinderSurface(f_radius, f_height);
250  glRotatef(180.0f, 1.0f, 0.0f, 0.0f);
251  MakeDisk(f_radius);
252  glPopMatrix();
253  }
254 
255  /****************************************/
256  /****************************************/
257 
258  void CQTOpenGLSpiri::MakeBox(GLfloat f_length,
259  GLfloat f_width,
260  GLfloat f_height) {
261  glEnable(GL_NORMALIZE);
262  glPushMatrix();
263  glScalef(f_length, f_width, f_height);
264  /* This part covers the top and bottom faces (parallel to XY) */
265  glBegin(GL_QUADS);
266  /* Bottom face */
267  glNormal3f(0.0f, 0.0f, -1.0f);
268  glVertex3f( 0.5f, 0.5f, 0.0f);
269  glVertex3f( 0.5f, -0.5f, 0.0f);
270  glVertex3f(-0.5f, -0.5f, 0.0f);
271  glVertex3f(-0.5f, 0.5f, 0.0f);
272  /* Top face */
273  glNormal3f(0.0f, 0.0f, 1.0f);
274  glVertex3f(-0.5f, -0.5f, 1.0f);
275  glVertex3f( 0.5f, -0.5f, 1.0f);
276  glVertex3f( 0.5f, 0.5f, 1.0f);
277  glVertex3f(-0.5f, 0.5f, 1.0f);
278  glEnd();
279  /* This part covers the faces (South, East, North, West) */
280  glBegin(GL_QUADS);
281  /* South face */
282  glNormal3f(0.0f, -1.0f, 0.0f);
283  glVertex3f(-0.5f, -0.5f, 1.0f);
284  glVertex3f(-0.5f, -0.5f, 0.0f);
285  glVertex3f( 0.5f, -0.5f, 0.0f);
286  glVertex3f( 0.5f, -0.5f, 1.0f);
287  /* East face */
288  glNormal3f(1.0f, 0.0f, 0.0f);
289  glVertex3f( 0.5f, -0.5f, 1.0f);
290  glVertex3f( 0.5f, -0.5f, 0.0f);
291  glVertex3f( 0.5f, 0.5f, 0.0f);
292  glVertex3f( 0.5f, 0.5f, 1.0f);
293  /* North face */
294  glNormal3f(0.0f, 1.0f, 0.0f);
295  glVertex3f( 0.5f, 0.5f, 1.0f);
296  glVertex3f( 0.5f, 0.5f, 0.0f);
297  glVertex3f(-0.5f, 0.5f, 0.0f);
298  glVertex3f(-0.5f, 0.5f, 1.0f);
299  /* West face */
300  glNormal3f(-1.0f, 0.0f, 0.0f);
301  glVertex3f(-0.5f, 0.5f, 1.0f);
302  glVertex3f(-0.5f, 0.5f, 0.0f);
303  glVertex3f(-0.5f, -0.5f, 0.0f);
304  glVertex3f(-0.5f, -0.5f, 1.0f);
305  glEnd();
306  /* The shape definitions is finished */
307  glPopMatrix();
308  glDisable(GL_NORMALIZE);
309  }
310 
311  /****************************************/
312  /****************************************/
313 
315  public:
316  void ApplyTo(CQTOpenGLWidget& c_visualization,
317  CSpiriEntity& c_entity) {
318  static CQTOpenGLSpiri m_cModel;
319  c_visualization.DrawRays(c_entity.GetControllableEntity());
320  c_visualization.DrawEntity(c_entity.GetEmbodiedEntity());
321  m_cModel.Draw(c_entity);
322  }
323  };
324 
326  public:
327  void ApplyTo(CQTOpenGLWidget& c_visualization,
328  CSpiriEntity& c_entity) {
329  c_visualization.DrawBoundingBox(c_entity.GetEmbodiedEntity());
330  }
331  };
332 
333  REGISTER_QTOPENGL_ENTITY_OPERATION(CQTOpenGLOperationDrawNormal, CQTOpenGLOperationDrawSpiriNormal, CSpiriEntity);
334 
335  REGISTER_QTOPENGL_ENTITY_OPERATION(CQTOpenGLOperationDrawSelected, CQTOpenGLOperationDrawSpiriSelected, CSpiriEntity);
336 
337  /****************************************/
338  /****************************************/
339 
340 }
argos::CQTOpenGLSpiri::CQTOpenGLSpiri
CQTOpenGLSpiri()
Definition: qtopengl_spiri.cpp:41
argos
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
argos::CQTOpenGLWidget
Definition: qtopengl_widget.h:56
argos::CQTOpenGLSpiri::~CQTOpenGLSpiri
virtual ~CQTOpenGLSpiri()
Definition: qtopengl_spiri.cpp:53
argos::CQTOpenGLOperationDrawNormal
Definition: qtopengl_widget.h:40
argos::REGISTER_QTOPENGL_ENTITY_OPERATION
REGISTER_QTOPENGL_ENTITY_OPERATION(CQTOpenGLOperationDrawNormal, CQTOpenGLOperationDrawEPuckNormal, CEPuckEntity)
argos::CQTOpenGLWidget::DrawEntity
void DrawEntity(CPositionalEntity &c_entity)
Draws a positional entity.
Definition: qtopengl_widget.cpp:468
argos::CQTOpenGLWidget::DrawBoundingBox
void DrawBoundingBox(CEmbodiedEntity &c_entity)
Draws the bounding box of an embodied entity.
Definition: qtopengl_widget.cpp:538
argos::CQTOpenGLOperationDrawSpiriSelected
Definition: qtopengl_spiri.cpp:325
argos::CSpiriEntity::GetControllableEntity
CControllableEntity & GetControllableEntity()
Definition: spiri_entity.h:45
qtopengl_spiri.h
argos::CSpiriEntity::GetEmbodiedEntity
CEmbodiedEntity & GetEmbodiedEntity()
Definition: spiri_entity.h:49
argos::CQTOpenGLOperationDrawSpiriSelected::ApplyTo
void ApplyTo(CQTOpenGLWidget &c_visualization, CSpiriEntity &c_entity)
Definition: qtopengl_spiri.cpp:327
argos::CSpiriEntity
Definition: spiri_entity.h:23
argos::CQTOpenGLOperationDrawSpiriNormal
Definition: qtopengl_spiri.cpp:314
argos::CQTOpenGLOperationDrawSelected
Definition: qtopengl_widget.h:45
argos::CQTOpenGLSpiri::Draw
virtual void Draw(CSpiriEntity &c_entity)
Definition: qtopengl_spiri.cpp:60
argos::CQTOpenGLSpiri
Definition: qtopengl_spiri.h:24
argos::CQTOpenGLWidget::DrawRays
void DrawRays(CControllableEntity &c_entity)
Draws a ray.
Definition: qtopengl_widget.cpp:504
argos::CRadians::TWO_PI
static const CRadians TWO_PI
Set to PI * 2.
Definition: angles.h:54
argos::CQTOpenGLOperationDrawSpiriNormal::ApplyTo
void ApplyTo(CQTOpenGLWidget &c_visualization, CSpiriEntity &c_entity)
Definition: qtopengl_spiri.cpp:316
spiri_entity.h