qtopengl_eyebot.cpp
Go to the documentation of this file.
1 
7 #include "qtopengl_eyebot.h"
8 #include "eyebot_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 
22  /* Legs */
23  static const Real LEG_HEIGHT = 0.166f;
24  static const Real LEG_WIDTH = 0.02f;
25  static const Real LEG_HALF_WIDTH = LEG_WIDTH * 0.5f;
26  static const Real LEG_DEPTH = 0.005f;
27  static const Real LEG_HALF_DEPTH = LEG_DEPTH * 0.5f;
28 
29  /* Ring */
30  static const Real RING_OUTER_RADIUS = 0.25f;
31  static const Real RING_INNER_RADIUS = 0.237f;
32  static const Real RING_HEIGHT = 0.015f;
33  static const Real RING_ELEVATION = LEG_HEIGHT - RING_HEIGHT;
34 
35  /* Led */
36  static const Real LED_HEIGHT = 0.01f;
37  static const Real LED_SIDE = RING_HEIGHT;
38  static const Real LED_HALF_SIDE = LED_SIDE * 0.5f;
39  static const Real LED_SIDE_RING_ELEVATION = RING_ELEVATION + LED_HALF_SIDE;
40  static const Real LED_BOTTOM_RING_ELEVATION = RING_ELEVATION - LED_HEIGHT;
41  static const Real LED_BOTTOM_RING_DISTANCE = RING_OUTER_RADIUS - LEG_HALF_WIDTH;
42 
43  /* Main rod */
44  static const Real ROD_SIDE = 0.07f;
45  static const Real ROD_HALF_SIDE = ROD_SIDE * 0.5f;
46  static const Real ROD_HEIGHT = 0.4f;
47  static const Real ROD_ELEVATION = LEG_HEIGHT;
48 
49  /****************************************/
50  /****************************************/
51 
53  m_unVertices(40),
54  m_fLEDAngleSlice(360.0f / 16.0f) {
55  /* Reserve the needed display lists */
56  m_unLists = glGenLists(4);
57 
58  /* Assign indices for better referencing (later) */
59  m_unLegList = m_unLists;
60  m_unBodyList = m_unLists + 1;
61  m_unSideLEDList = m_unLists + 2;
62  m_unBottomLEDList = m_unLists + 3;
63 
64  /* Create the led display list */
65  glNewList(m_unLegList, GL_COMPILE);
66  MakeLeg();
67  glEndList();
68 
69  /* Create the body display list */
70  glNewList(m_unBodyList, GL_COMPILE);
71  DrawBody();
72  glEndList();
73 
74  /* Create the side LED display list */
75  glNewList(m_unSideLEDList, GL_COMPILE);
76  DrawSideLED();
77  glEndList();
78 
79  /* Create the bottom LED display list */
80  glNewList(m_unBottomLEDList, GL_COMPILE);
81  DrawBottomLED();
82  glEndList();
83  }
84 
85  /****************************************/
86  /****************************************/
87 
89  glDeleteLists(m_unLists, 4);
90  }
91 
92  /****************************************/
93  /****************************************/
94 
97  /* Place the legs */
98  glPushMatrix();
99  glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
100  glTranslatef(RING_OUTER_RADIUS, 0.0f, 0.0f);
101  glCallList(m_unLegList);
102  glPopMatrix();
103  glPushMatrix();
104  glRotatef(135.0f, 0.0f, 0.0f, 1.0f);
105  glTranslatef(RING_OUTER_RADIUS, 0.0f, 0.0f);
106  glCallList(m_unLegList);
107  glPopMatrix();
108  glPushMatrix();
109  glRotatef(225.0f, 0.0f, 0.0f, 1.0f);
110  glTranslatef(RING_OUTER_RADIUS, 0.0f, 0.0f);
111  glCallList(m_unLegList);
112  glPopMatrix();
113  glPushMatrix();
114  glRotatef(315.0f, 0.0f, 0.0f, 1.0f);
115  glTranslatef(RING_OUTER_RADIUS, 0.0f, 0.0f);
116  glCallList(m_unLegList);
117  glPopMatrix();
118  /* Place the body */
119  glCallList(m_unBodyList);
120  /* Place the LEDs */
121  glPushMatrix();
122  CLEDEquippedEntity& cLEDEquippedEntity = c_entity.GetLEDEquippedEntity();
123  glRotatef(m_fLEDAngleSlice * 0.5f, 0.0f, 0.0f, 1.0f);
124  const CColor& cUpColor = cLEDEquippedEntity.GetLED(0).GetColor();
125  const CColor& cLowColor = cLEDEquippedEntity.GetLED(16).GetColor();
126  SetLEDMaterial(cUpColor.GetRed(),
127  cUpColor.GetGreen(),
128  cUpColor.GetBlue());
129  glCallList(m_unSideLEDList);
130  SetLEDMaterial(cLowColor.GetRed(),
131  cLowColor.GetGreen(),
132  cLowColor.GetBlue());
133  glCallList(m_unBottomLEDList);
134  for(UInt32 i = 1; i < 16; i++) {
135  glRotatef(m_fLEDAngleSlice, 0.0f, 0.0f, 1.0f);
136  const CColor& cUpColor = cLEDEquippedEntity.GetLED(i).GetColor();
137  const CColor& cLowColor = cLEDEquippedEntity.GetLED(i+16).GetColor();
138  SetLEDMaterial(cUpColor.GetRed(),
139  cUpColor.GetGreen(),
140  cUpColor.GetBlue());
141  glCallList(m_unSideLEDList);
142  SetLEDMaterial(cLowColor.GetRed(),
143  cLowColor.GetGreen(),
144  cLowColor.GetBlue());
145  glCallList(m_unBottomLEDList);
146  }
147  glRotatef(135.0f, 0.0f, 0.0f, 1.0f);
148  glTranslatef(-0.3*RING_OUTER_RADIUS, 0.0f, 0.0f);
149  const CColor& cBottomLedColor = cLEDEquippedEntity.GetLED(32).GetColor();
150  SetLEDMaterial(cBottomLedColor.GetRed(),
151  cBottomLedColor.GetGreen(),
152  cBottomLedColor.GetBlue());
153  glCallList(m_unBottomLEDList);
154  glPopMatrix();
155  }
156 
157  /****************************************/
158  /****************************************/
159 
160  void CQTOpenGLEyeBot::SetLEDMaterial(GLfloat f_red, GLfloat f_green, GLfloat f_blue) {
161  const GLfloat pfColor[] = { f_red, f_green, f_blue, 1.0f };
162  const GLfloat pfSpecular[] = { 0.0f, 0.0f, 0.0f, 1.0f };
163  const GLfloat pfShininess[] = { 0.0f };
164  const GLfloat pfEmission[] = { f_red, f_green, f_blue, 1.0f };
165  glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, pfColor);
166  glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, pfSpecular);
167  glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, pfShininess);
168  glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, pfEmission);
169  }
170 
171  /****************************************/
172  /****************************************/
173 
175  const GLfloat pfColor[] = { 0.25f, 0.25f, 0.25f, 1.0f };
176  const GLfloat pfSpecular[] = { 0.9f, 0.9f, 0.9f, 1.0f };
177  const GLfloat pfShininess[] = { 100.0f };
178  const GLfloat pfEmission[] = { 0.0f, 0.0f, 0.0f, 1.0f };
179  glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, pfColor);
180  glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, pfSpecular);
181  glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, pfShininess);
182  glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, pfEmission);
183  }
184 
185  /****************************************/
186  /****************************************/
187 
189  /* Top face */
190  glBegin(GL_QUADS);
191  glNormal3f(0.0f, 0.0f, 1.0f);
192  glVertex3f(-LEG_HALF_WIDTH, -LEG_HALF_DEPTH, LEG_HEIGHT);
193  glVertex3f( LEG_HALF_WIDTH, -LEG_HALF_DEPTH, LEG_HEIGHT);
194  glVertex3f( LEG_HALF_WIDTH, LEG_HALF_DEPTH, LEG_HEIGHT);
195  glVertex3f(-LEG_HALF_WIDTH, LEG_HALF_DEPTH, LEG_HEIGHT);
196  glEnd();
197  /* Bottom face */
198  glBegin(GL_QUADS);
199  glNormal3f(0.0f, 0.0f, -1.0f);
200  glVertex3f(-LEG_HALF_WIDTH, -LEG_HALF_DEPTH, 0.0f);
201  glVertex3f(-LEG_HALF_WIDTH, LEG_HALF_DEPTH, 0.0f);
202  glVertex3f( LEG_HALF_WIDTH, LEG_HALF_DEPTH, 0.0f);
203  glVertex3f( LEG_HALF_WIDTH, -LEG_HALF_DEPTH, 0.0f);
204  glEnd();
205  /* This part covers the faces (South, East, North, West) */
206  glBegin(GL_QUAD_STRIP);
207  /* Starting vertex */
208  glNormal3f(0.0f, -1.0f, 0.0f);
209  glVertex3f(-LEG_HALF_WIDTH, -LEG_HALF_DEPTH, LEG_HEIGHT);
210  glVertex3f(-LEG_HALF_WIDTH, -LEG_HALF_DEPTH, 0.0f);
211  /* South face */
212  glVertex3f( LEG_HALF_WIDTH, -LEG_HALF_DEPTH, LEG_HEIGHT);
213  glVertex3f( LEG_HALF_WIDTH, -LEG_HALF_DEPTH, 0.0f);
214  /* East face */
215  glNormal3f(1.0f, 0.0f, 0.0f);
216  glVertex3f( LEG_HALF_WIDTH, LEG_HALF_DEPTH, LEG_HEIGHT);
217  glVertex3f( LEG_HALF_WIDTH, LEG_HALF_DEPTH, 0.0f);
218  /* North face */
219  glNormal3f(0.0f, 1.0f, 0.0f);
220  glVertex3f(-LEG_HALF_WIDTH, LEG_HALF_DEPTH, LEG_HEIGHT);
221  glVertex3f(-LEG_HALF_WIDTH, LEG_HALF_DEPTH, 0.0f);
222  /* West face */
223  glNormal3f(-1.0f, 0.0f, 0.0f);
224  glVertex3f(-LEG_HALF_WIDTH, -LEG_HALF_DEPTH, LEG_HEIGHT);
225  glVertex3f(-LEG_HALF_WIDTH, -LEG_HALF_DEPTH, 0.0f);
226  glEnd();
227  }
228 
229  /****************************************/
230  /****************************************/
231 
233  /* Draw it as a tiny pyramid pointing to X
234  (with no base, cause it's not visible anyway) */
235  glBegin(GL_TRIANGLES);
236  /* Top */
237  glVertex3f(LED_HEIGHT + RING_OUTER_RADIUS, 0.0f, LED_SIDE_RING_ELEVATION );
238  glVertex3f( RING_OUTER_RADIUS, LED_HALF_SIDE, LED_SIDE_RING_ELEVATION + LED_HALF_SIDE);
239  glVertex3f( RING_OUTER_RADIUS, -LED_HALF_SIDE, LED_SIDE_RING_ELEVATION + LED_HALF_SIDE);
240  /* Bottom */
241  glVertex3f(LED_HEIGHT + RING_OUTER_RADIUS, 0.0f, LED_SIDE_RING_ELEVATION );
242  glVertex3f( RING_OUTER_RADIUS, -LED_HALF_SIDE, LED_SIDE_RING_ELEVATION - LED_HALF_SIDE);
243  glVertex3f( RING_OUTER_RADIUS, LED_HALF_SIDE, LED_SIDE_RING_ELEVATION - LED_HALF_SIDE);
244  /* Left */
245  glVertex3f(LED_HEIGHT + RING_OUTER_RADIUS, 0.0f, LED_SIDE_RING_ELEVATION );
246  glVertex3f( RING_OUTER_RADIUS, LED_HALF_SIDE, LED_SIDE_RING_ELEVATION - LED_HALF_SIDE);
247  glVertex3f( RING_OUTER_RADIUS, LED_HALF_SIDE, LED_SIDE_RING_ELEVATION + LED_HALF_SIDE);
248  /* Right */
249  glVertex3f(LED_HEIGHT + RING_OUTER_RADIUS, 0.0f, LED_SIDE_RING_ELEVATION );
250  glVertex3f( RING_OUTER_RADIUS, -LED_HALF_SIDE, LED_SIDE_RING_ELEVATION + LED_HALF_SIDE);
251  glVertex3f( RING_OUTER_RADIUS, -LED_HALF_SIDE, LED_SIDE_RING_ELEVATION - LED_HALF_SIDE);
252  glEnd();
253  }
254 
255  /****************************************/
256  /****************************************/
257 
259  /* Draw it as a tiny pyramid pointing to Z downwards
260  (with no base, cause it's not visible anyway) */
261  glBegin(GL_TRIANGLES);
262  /* North */
263  glVertex3f(LED_BOTTOM_RING_DISTANCE, 0.0f, LED_BOTTOM_RING_ELEVATION);
264  glVertex3f(RING_OUTER_RADIUS, LED_HALF_SIDE, RING_ELEVATION);
265  glVertex3f(RING_OUTER_RADIUS, -LED_HALF_SIDE, RING_ELEVATION);
266  /* South */
267  glVertex3f(LED_BOTTOM_RING_DISTANCE, 0.0f, LED_BOTTOM_RING_ELEVATION);
268  glVertex3f(RING_OUTER_RADIUS - LED_SIDE, -LED_HALF_SIDE, RING_ELEVATION);
269  glVertex3f(RING_OUTER_RADIUS - LED_SIDE, LED_HALF_SIDE, RING_ELEVATION);
270  /* West */
271  glVertex3f(LED_BOTTOM_RING_DISTANCE, 0.0f, LED_BOTTOM_RING_ELEVATION);
272  glVertex3f(RING_OUTER_RADIUS - LED_SIDE, LED_HALF_SIDE, RING_ELEVATION);
273  glVertex3f(RING_OUTER_RADIUS , LED_HALF_SIDE, RING_ELEVATION);
274  /* East */
275  glVertex3f(LED_BOTTOM_RING_DISTANCE, 0.0f, LED_BOTTOM_RING_ELEVATION);
276  glVertex3f(RING_OUTER_RADIUS , -LED_HALF_SIDE, RING_ELEVATION);
277  glVertex3f(RING_OUTER_RADIUS - LED_SIDE, -LED_HALF_SIDE, RING_ELEVATION);
278  glEnd();
279  }
280 
281  /****************************************/
282  /****************************************/
283 
285  /* Draw the ring */
286  CVector2 cVertex(RING_OUTER_RADIUS, 0.0f);
287  CRadians cAngle(-CRadians::TWO_PI / m_unVertices);
288  /* Bottom part */
289  glBegin(GL_POLYGON);
290  glNormal3f(0.0f, 0.0f, -1.0f);
291  for(GLuint i = 0; i <= m_unVertices; i++) {
292  glVertex3f(cVertex.GetX(), cVertex.GetY(), RING_ELEVATION);
293  cVertex.Rotate(cAngle);
294  }
295  glEnd();
296  /* Side surface */
297  cAngle = -cAngle;
298  CVector2 cNormal(1.0f, 0.0f);
299  cVertex.Set(RING_OUTER_RADIUS, 0.0f);
300  glBegin(GL_QUAD_STRIP);
301  for(GLuint i = 0; i <= m_unVertices; i++) {
302  glNormal3f(cNormal.GetX(), cNormal.GetY(), 0.0f);
303  glVertex3f(cVertex.GetX(), cVertex.GetY(), RING_ELEVATION + RING_HEIGHT);
304  glVertex3f(cVertex.GetX(), cVertex.GetY(), RING_ELEVATION);
305  cVertex.Rotate(cAngle);
306  cNormal.Rotate(cAngle);
307  }
308  glEnd();
309  /* Top part */
310  glBegin(GL_POLYGON);
311  glNormal3f(0.0f, 0.0f, 1.0f);
312  cVertex.Set(RING_OUTER_RADIUS, 0.0f);
313  for(GLuint i = 0; i <= m_unVertices; i++) {
314  glVertex3f(cVertex.GetX(), cVertex.GetY(), RING_ELEVATION + RING_HEIGHT);
315  cVertex.Rotate(cAngle);
316  }
317  glEnd();
318  /* Draw the rod */
319  /* Top face */
320  glBegin(GL_QUADS);
321  glNormal3f(0.0f, 0.0f, 1.0f);
322  glVertex3f(-ROD_HALF_SIDE, -ROD_HALF_SIDE, ROD_ELEVATION+ROD_HEIGHT);
323  glVertex3f( ROD_HALF_SIDE, -ROD_HALF_SIDE, ROD_ELEVATION+ROD_HEIGHT);
324  glVertex3f( ROD_HALF_SIDE, ROD_HALF_SIDE, ROD_ELEVATION+ROD_HEIGHT);
325  glVertex3f(-ROD_HALF_SIDE, ROD_HALF_SIDE, ROD_ELEVATION+ROD_HEIGHT);
326  glEnd();
327  /* This part covers the faces (South, East, North, West) */
328  glBegin(GL_QUAD_STRIP);
329  /* Starting vertex */
330  glNormal3f(0.0f, -1.0f, 0.0f);
331  glVertex3f(-ROD_HALF_SIDE, -ROD_HALF_SIDE, ROD_ELEVATION+ROD_HEIGHT);
332  glVertex3f(-ROD_HALF_SIDE, -ROD_HALF_SIDE, ROD_ELEVATION);
333  /* South face */
334  glVertex3f( ROD_HALF_SIDE, -ROD_HALF_SIDE, ROD_ELEVATION+ROD_HEIGHT);
335  glVertex3f( ROD_HALF_SIDE, -ROD_HALF_SIDE, ROD_ELEVATION);
336  /* East face */
337  glNormal3f(1.0f, 0.0f, 0.0f);
338  glVertex3f( ROD_HALF_SIDE, ROD_HALF_SIDE, ROD_ELEVATION+ROD_HEIGHT);
339  glVertex3f( ROD_HALF_SIDE, ROD_HALF_SIDE, ROD_ELEVATION);
340  /* North face */
341  glNormal3f(0.0f, 1.0f, 0.0f);
342  glVertex3f(-ROD_HALF_SIDE, ROD_HALF_SIDE, ROD_ELEVATION+ROD_HEIGHT);
343  glVertex3f(-ROD_HALF_SIDE, ROD_HALF_SIDE, ROD_ELEVATION);
344  /* West face */
345  glNormal3f(-1.0f, 0.0f, 0.0f);
346  glVertex3f(-ROD_HALF_SIDE, -ROD_HALF_SIDE, ROD_ELEVATION+ROD_HEIGHT);
347  glVertex3f(-ROD_HALF_SIDE, -ROD_HALF_SIDE, ROD_ELEVATION);
348  glEnd();
349  }
350 
351  /****************************************/
352  /****************************************/
353 
355  public:
356  void ApplyTo(CQTOpenGLWidget& c_visualization,
357  CEyeBotEntity& c_entity) {
358  static CQTOpenGLEyeBot m_cModel;
359  c_visualization.DrawRays(c_entity.GetControllableEntity());
360  c_visualization.DrawEntity(c_entity.GetEmbodiedEntity());
361  m_cModel.Draw(c_entity);
362  }
363  };
364 
366  public:
367  void ApplyTo(CQTOpenGLWidget& c_visualization,
368  CEyeBotEntity& c_entity) {
369  c_visualization.DrawBoundingBox(c_entity.GetEmbodiedEntity());
370  }
371  };
372 
373  REGISTER_QTOPENGL_ENTITY_OPERATION(CQTOpenGLOperationDrawNormal, CQTOpenGLOperationDrawEyeBotNormal, CEyeBotEntity);
374 
375  REGISTER_QTOPENGL_ENTITY_OPERATION(CQTOpenGLOperationDrawSelected, CQTOpenGLOperationDrawEyeBotSelected, CEyeBotEntity);
376 
377  /****************************************/
378  /****************************************/
379 
380 }
const CColor & GetColor() const
Returns the current color of the LED.
Definition: led_entity.h:58
void DrawEntity(CPositionalEntity &c_entity)
Draws a positional entity.
REGISTER_QTOPENGL_ENTITY_OPERATION(CQTOpenGLOperationDrawNormal, CQTOpenGLOperationDrawEPuckNormal, CEPuckEntity)
CEmbodiedEntity & GetEmbodiedEntity()
Definition: eyebot_entity.h:57
CVector2 & Rotate(const CRadians &c_angle)
Rotates this vector by the wanted angle.
Definition: vector2.h:169
Real GetX() const
Returns the x coordinate of this vector.
Definition: vector2.h:78
void DrawSideLED()
A single LED of the side ring.
unsigned int UInt32
32-bit unsigned integer.
Definition: datatypes.h:97
CLEDEntity & GetLED(UInt32 un_index)
Returns an LED by numeric index.
void SetLEDMaterial(GLfloat f_red, GLfloat f_green, GLfloat f_blue)
Sets a colored LED material.
void ApplyTo(CQTOpenGLWidget &c_visualization, CEyeBotEntity &c_entity)
UInt8 GetRed() const
Returns the red channel of the color.
Definition: color.h:79
The basic color type.
Definition: color.h:25
void Set(Real f_x, Real f_y)
Sets the vector contents from Cartesian coordinates.
Definition: vector2.h:111
CControllableEntity & GetControllableEntity()
Definition: eyebot_entity.h:53
void DrawBoundingBox(CEmbodiedEntity &c_entity)
Draws the bounding box of an embodied entity.
void SetBodyMaterial()
Sets a shiny gray material.
A container of CLEDEntity.
static const CRadians TWO_PI
Set to PI * 2.
Definition: angles.h:54
void MakeLeg()
An eye-bot leg.
void DrawBody()
The eye-bot body.
UInt8 GetBlue() const
Returns the blue channel of the color.
Definition: color.h:101
It defines the basic type CRadians, used to store an angle value in radians.
Definition: angles.h:42
CLEDEquippedEntity & GetLEDEquippedEntity()
Definition: eyebot_entity.h:61
void DrawBottomLED()
A single LED of the bottom ring.
virtual void Draw(CEyeBotEntity &c_entity)
A 2D vector class.
Definition: vector2.h:25
Real GetY() const
Returns the y coordinate of this vector.
Definition: vector2.h:94
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
void ApplyTo(CQTOpenGLWidget &c_visualization, CEyeBotEntity &c_entity)
UInt8 GetGreen() const
Returns the green channel of the color.
Definition: color.h:90
void DrawRays(CControllableEntity &c_entity)
Draws a ray.
float Real
Collects all ARGoS code.
Definition: datatypes.h:39