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