qtopengl_cylinder.cpp
Go to the documentation of this file.
1 
7 #include "qtopengl_cylinder.h"
8 #include <argos3/core/utility/math/vector2.h>
9 #include <argos3/plugins/simulator/entities/led_equipped_entity.h>
10 #include <argos3/plugins/simulator/entities/cylinder_entity.h>
11 #include <argos3/plugins/simulator/visualizations/qt-opengl/qtopengl_widget.h>
12 
13 namespace argos {
14 
15  /****************************************/
16  /****************************************/
17 
18  static const Real LED_RADIUS = 0.01f;
19  const GLfloat MOVABLE_COLOR[] = { 0.0f, 1.0f, 0.0f, 1.0f };
20  const GLfloat NONMOVABLE_COLOR[] = { 0.7f, 0.7f, 0.7f, 1.0f };
21  const GLfloat SPECULAR[] = { 0.0f, 0.0f, 0.0f, 1.0f };
22  const GLfloat SHININESS[] = { 0.0f };
23  const GLfloat EMISSION[] = { 0.0f, 0.0f, 0.0f, 1.0f };
24 
25  /****************************************/
26  /****************************************/
27 
29  m_unVertices(20) {
30 
31  /* Reserve the needed display lists */
32  m_unBaseList = glGenLists(1);
33  m_unBodyList = m_unBaseList;
34  m_unLEDList = m_unBaseList + 1;
35 
36  /* Make body list */
37  glNewList(m_unBodyList, GL_COMPILE);
38  MakeBody();
39  glEndList();
40 
41  /* Make LED list */
42  glNewList(m_unLEDList, GL_COMPILE);
43  MakeLED();
44  glEndList();
45  }
46 
47  /****************************************/
48  /****************************************/
49 
51  glDeleteLists(m_unBaseList, 2);
52  }
53 
54  /****************************************/
55  /****************************************/
56 
58  /* Draw the LEDs */
59  GLfloat pfColor[] = { 0.0f, 0.0f, 0.0f, 1.0f };
60  const GLfloat pfSpecular[] = { 0.0f, 0.0f, 0.0f, 1.0f };
61  const GLfloat pfShininess[] = { 100.0f };
62  const GLfloat pfEmission[] = { 0.0f, 0.0f, 0.0f, 1.0f };
63  glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, pfSpecular);
64  glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, pfShininess);
65  glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, pfEmission);
66  CLEDEquippedEntity& cLEDEquippedEntity = c_entity.GetLEDEquippedEntity();
67  for(UInt32 i = 0; i < cLEDEquippedEntity.GetLEDs().size(); ++i) {
68  glPushMatrix();
69  /* Set the material */
70  const CColor& cColor = cLEDEquippedEntity.GetLED(i).GetColor();
71  pfColor[0] = cColor.GetRed() / 255.0f;
72  pfColor[1] = cColor.GetGreen() / 255.0f;
73  pfColor[2] = cColor.GetBlue() / 255.0f;
74  glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, pfColor);
75  /* Perform rototranslation */
76  const CVector3& cPosition = cLEDEquippedEntity.GetLEDOffset(i);
77  glTranslated(cPosition.GetX(), cPosition.GetY(), cPosition.GetZ());
78  /* Draw the LED */
79  glCallList(m_unLEDList);
80  glPopMatrix();
81  }
82  }
83 
84  /****************************************/
85  /****************************************/
86 
88  /* Draw the body */
89  if(c_entity.GetEmbodiedEntity().IsMovable()) {
90  glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, MOVABLE_COLOR);
91  }
92  else {
93  glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, NONMOVABLE_COLOR);
94  }
95  glPushMatrix();
96  glScaled(c_entity.GetRadius(), c_entity.GetRadius(), c_entity.GetHeight());
97  glCallList(m_unBodyList);
98  glPopMatrix();
99  }
100 
101  /****************************************/
102  /****************************************/
103 
104  void CQTOpenGLCylinder::MakeBody() {
105  /* Since this shape can be stretched,
106  make sure the normal vectors are unit-long */
107  glEnable(GL_NORMALIZE);
108 
109  /* Set the material */
110  glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, SPECULAR);
111  glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, SHININESS);
112  glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, EMISSION);
113 
114  /* Let's start the actual shape */
115  /* Side surface */
116  CVector2 cVertex(1.0f, 0.0f);
117  CRadians cAngle(CRadians::TWO_PI / m_unVertices);
118  glBegin(GL_QUAD_STRIP);
119  for(GLuint i = 0; i <= m_unVertices; i++) {
120  glNormal3d(cVertex.GetX(), cVertex.GetY(), 0.0f);
121  glVertex3d(cVertex.GetX(), cVertex.GetY(), 1.0f);
122  glVertex3d(cVertex.GetX(), cVertex.GetY(), 0.0f);
123  cVertex.Rotate(cAngle);
124  }
125  glEnd();
126  /* Top disk */
127  cVertex.Set(1.0f, 0.0f);
128  glBegin(GL_POLYGON);
129  glNormal3d(0.0f, 0.0f, 1.0f);
130  for(GLuint i = 0; i <= m_unVertices; i++) {
131  glVertex3d(cVertex.GetX(), cVertex.GetY(), 1.0f);
132  cVertex.Rotate(cAngle);
133  }
134  glEnd();
135  /* Bottom disk */
136  cVertex.Set(1.0f, 0.0f);
137  cAngle = -cAngle;
138  glBegin(GL_POLYGON);
139  glNormal3d(0.0f, 0.0f, -1.0f);
140  for(GLuint i = 0; i <= m_unVertices; i++) {
141  glVertex3d(cVertex.GetX(), cVertex.GetY(), 0.0f);
142  cVertex.Rotate(cAngle);
143  }
144  glEnd();
145  /* The shape definition is finished */
146 
147  /* We don't need it anymore */
148  glDisable(GL_NORMALIZE);
149 
150  }
151 
152  /****************************************/
153  /****************************************/
154 
155  void CQTOpenGLCylinder::MakeLED() {
156  CVector3 cNormal, cPoint;
157  CRadians cSlice(CRadians::TWO_PI / m_unVertices);
158 
159  glBegin(GL_TRIANGLE_STRIP);
160  for(CRadians cInclination; cInclination <= CRadians::PI; cInclination += cSlice) {
161  for(CRadians cAzimuth; cAzimuth <= CRadians::TWO_PI; cAzimuth += cSlice) {
162 
163  cNormal.FromSphericalCoords(1.0f, cInclination, cAzimuth);
164  cPoint = LED_RADIUS * cNormal;
165  glNormal3d(cNormal.GetX(), cNormal.GetY(), cNormal.GetZ());
166  glVertex3d(cPoint.GetX(), cPoint.GetY(), cPoint.GetZ());
167 
168  cNormal.FromSphericalCoords(1.0f, cInclination + cSlice, cAzimuth);
169  cPoint = LED_RADIUS * cNormal;
170  glNormal3d(cNormal.GetX(), cNormal.GetY(), cNormal.GetZ());
171  glVertex3d(cPoint.GetX(), cPoint.GetY(), cPoint.GetZ());
172 
173  cNormal.FromSphericalCoords(1.0f, cInclination, cAzimuth + cSlice);
174  cPoint = LED_RADIUS * cNormal;
175  glNormal3d(cNormal.GetX(), cNormal.GetY(), cNormal.GetZ());
176  glVertex3d(cPoint.GetX(), cPoint.GetY(), cPoint.GetZ());
177 
178  cNormal.FromSphericalCoords(1.0f, cInclination + cSlice, cAzimuth + cSlice);
179  cPoint = LED_RADIUS * cNormal;
180  glNormal3d(cNormal.GetX(), cNormal.GetY(), cNormal.GetZ());
181  glVertex3d(cPoint.GetX(), cPoint.GetY(), cPoint.GetZ());
182 
183  }
184  }
185  glEnd();
186  }
187 
188  /****************************************/
189  /****************************************/
190 
192  public:
193  void ApplyTo(CQTOpenGLWidget& c_visualization,
194  CCylinderEntity& c_entity) {
195  static CQTOpenGLCylinder m_cModel;
196  c_visualization.DrawEntity(c_entity.GetEmbodiedEntity());
197  m_cModel.Draw(c_entity);
198  m_cModel.DrawLEDs(c_entity);
199  }
200  };
201 
203  public:
204  void ApplyTo(CQTOpenGLWidget& c_visualization,
205  CCylinderEntity& c_entity) {
206  c_visualization.DrawBoundingBox(c_entity.GetEmbodiedEntity());
207  }
208  };
209 
211 
213 
214  /****************************************/
215  /****************************************/
216 
217 }
unsigned int UInt32
32-bit unsigned integer.
Definition: datatypes.h:97
float Real
Collects all ARGoS code.
Definition: datatypes.h:39
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
const GLfloat MOVABLE_COLOR[]
REGISTER_QTOPENGL_ENTITY_OPERATION(CQTOpenGLOperationDrawNormal, CQTOpenGLOperationDrawEPuckNormal, CEPuckEntity)
const GLfloat SHININESS[]
const GLfloat SPECULAR[]
const GLfloat EMISSION[]
const GLfloat NONMOVABLE_COLOR[]
bool IsMovable() const
Returns true if the entity is movable.
The basic color type.
Definition: color.h:25
UInt8 GetBlue() const
Returns the blue channel of the color.
Definition: color.h:101
UInt8 GetGreen() const
Returns the green channel of the color.
Definition: color.h:90
UInt8 GetRed() const
Returns the red channel of the color.
Definition: color.h:79
It defines the basic type CRadians, used to store an angle value in radians.
Definition: angles.h:42
static const CRadians PI
The PI constant.
Definition: angles.h:49
static const CRadians TWO_PI
Set to PI * 2.
Definition: angles.h:54
A 2D vector class.
Definition: vector2.h:27
A 3D vector class.
Definition: vector3.h:31
Real GetX() const
Returns the x coordinate of this vector.
Definition: vector3.h:105
Real GetY() const
Returns the y coordinate of this vector.
Definition: vector3.h:121
Real GetZ() const
Returns the z coordinate of this vector.
Definition: vector3.h:137
CEmbodiedEntity & GetEmbodiedEntity()
CLEDEquippedEntity & GetLEDEquippedEntity()
const CColor & GetColor() const
Returns the current color of the LED.
Definition: led_entity.h:58
A container of CLEDEntity.
CLEDEntity & GetLED(UInt32 un_index)
Returns an LED by numeric index.
SActuator::TList & GetLEDs()
Returns all the LEDs.
const CVector3 & GetLEDOffset(size_t un_idx) const
Returns the offset position of the given LED.
void ApplyTo(CQTOpenGLWidget &c_visualization, CCylinderEntity &c_entity)
void ApplyTo(CQTOpenGLWidget &c_visualization, CCylinderEntity &c_entity)
virtual void Draw(CCylinderEntity &c_entity)
void DrawLEDs(CCylinderEntity &c_entity)
void DrawEntity(CPositionalEntity &c_entity)
Draws a positional entity.
void DrawBoundingBox(CEmbodiedEntity &c_entity)
Draws the bounding box of an embodied entity.