9 #include <argos3/core/utility/math/vector2.h>
10 #include <argos3/core/simulator/entity/embodied_entity.h>
11 #include <argos3/plugins/robots/prototype/simulator/prototype_entity.h>
12 #include <argos3/plugins/robots/prototype/simulator/prototype_link_equipped_entity.h>
13 #include <argos3/plugins/simulator/entities/directional_led_equipped_entity.h>
14 #include <argos3/plugins/simulator/entities/led_equipped_entity.h>
15 #include <argos3/plugins/simulator/entities/magnet_equipped_entity.h>
16 #include <argos3/plugins/simulator/entities/tag_equipped_entity.h>
17 #include <argos3/plugins/simulator/visualizations/qt-opengl/qtopengl_widget.h>
25 const GLfloat
SPECULAR[] = { 0.0f, 0.0f, 0.0f, 1.0f };
27 const GLfloat
EMISSION[] = { 0.0f, 0.0f, 0.0f, 1.0f };
28 static const Real LED_RADIUS = 0.0025f;
30 static const Real FIELD_SCALE_FACTOR = 0.0005f;
39 m_unBaseList = glGenLists(6);
41 m_unBoxList = m_unBaseList;
42 m_unCylinderList = m_unBaseList + 1;
43 m_unSphereList = m_unBaseList + 2;
44 m_unLEDList = m_unBaseList + 3;
45 m_unPoleList = m_unBaseList + 4;
46 m_unTagList = m_unBaseList + 5;
48 glNewList(m_unBoxList, GL_COMPILE);
52 glNewList(m_unCylinderList, GL_COMPILE);
56 glNewList(m_unSphereList, GL_COMPILE);
60 glNewList(m_unLEDList, GL_COMPILE);
64 glNewList(m_unPoleList, GL_COMPILE);
69 glNewList(m_unTagList, GL_COMPILE);
78 glDeleteLists(m_unBaseList, 6);
84 void CQTOpenGLPrototype::MakeLED() {
88 glBegin(GL_TRIANGLE_STRIP);
92 cPoint = LED_RADIUS * cNormal;
93 glNormal3d(cNormal.GetX(), cNormal.GetY(), cNormal.GetZ());
95 cNormal.FromSphericalCoords(1.0f, cInclination + cSlice, cAzimuth);
96 cPoint = LED_RADIUS * cNormal;
97 glNormal3d(cNormal.GetX(), cNormal.GetY(), cNormal.GetZ());
99 cNormal.FromSphericalCoords(1.0f, cInclination, cAzimuth + cSlice);
100 cPoint = LED_RADIUS * cNormal;
101 glNormal3d(cNormal.GetX(), cNormal.GetY(), cNormal.GetZ());
102 glVertex3d(cPoint.
GetX(), cPoint.
GetY(), cPoint.
GetZ());
103 cNormal.FromSphericalCoords(1.0f, cInclination + cSlice, cAzimuth + cSlice);
104 cPoint = LED_RADIUS * cNormal;
105 glNormal3d(cNormal.GetX(), cNormal.GetY(), cNormal.GetZ());
106 glVertex3d(cPoint.
GetX(), cPoint.
GetY(), cPoint.
GetZ());
119 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE,
BODY_COLOR);
121 const CVector3& cPosition = pcLink->GetAnchor().Position;
123 const CQuaternion& cOrientation = pcLink->GetAnchor().Orientation;
127 glTranslated(cPosition.
GetX(), cPosition.
GetY(), cPosition.
GetZ());
128 glRotated(
ToDegrees(cXAngle).GetValue(), 1.0f, 0.0f, 0.0f);
129 glRotated(
ToDegrees(cYAngle).GetValue(), 0.0f, 1.0f, 0.0f);
130 glRotated(
ToDegrees(cZAngle).GetValue(), 0.0f, 0.0f, 1.0f);
132 switch(pcLink->GetGeometry()) {
133 case CPrototypeLinkEntity::EGeometry::BOX:
134 glScaled(pcLink->GetExtents().GetX(),
135 pcLink->GetExtents().GetY(),
136 pcLink->GetExtents().GetZ());
137 glCallList(m_unBoxList);
139 case CPrototypeLinkEntity::EGeometry::CYLINDER:
140 glScaled(pcLink->GetExtents().GetX(),
141 pcLink->GetExtents().GetY(),
142 pcLink->GetExtents().GetZ());
143 glCallList(m_unCylinderList);
145 case CPrototypeLinkEntity::EGeometry::SPHERE:
146 glScaled(pcLink->GetExtents().GetX(),
147 pcLink->GetExtents().GetY(),
148 pcLink->GetExtents().GetZ());
149 glCallList(m_unSphereList);
151 case CPrototypeLinkEntity::EGeometry::CONVEX_HULL:
152 DrawConvexHull(pcLink->GetConvexHullPoints(),
153 pcLink->GetConvexHullFaces());
166 GLfloat pfColor[] = { 0.0f, 0.0f, 0.0f, 1.0f };
167 const GLfloat pfSpecular[] = { 0.0f, 0.0f, 0.0f, 1.0f };
168 const GLfloat pfShininess[] = { 100.0f };
169 const GLfloat pfEmission[] = { 0.0f, 0.0f, 0.0f, 1.0f };
170 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, pfSpecular);
171 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, pfShininess);
172 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, pfEmission);
179 const CColor& cColor = s_instance.LED.GetColor();
180 pfColor[0] = cColor.
GetRed() / 255.0f;
181 pfColor[1] = cColor.
GetGreen() / 255.0f;
182 pfColor[2] = cColor.
GetBlue() / 255.0f;
183 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, pfColor);
185 const CVector3& cPosition = s_instance.LED.GetPosition();
186 glTranslated(cPosition.
GetX(), cPosition.
GetY(), cPosition.
GetZ());
188 glCallList(m_unLEDList);
194 GLfloat pfColor[] = { 0.0f, 0.0f, 0.0f, 1.0f };
195 const GLfloat pfSpecular[] = { 0.0f, 0.0f, 0.0f, 1.0f };
196 const GLfloat pfShininess[] = { 100.0f };
197 const GLfloat pfEmission[] = { 0.0f, 0.0f, 0.0f, 1.0f };
198 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, pfSpecular);
199 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, pfShininess);
200 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, pfEmission);
205 const CColor& cColor = ps_actuator->LED.GetColor();
206 pfColor[0] = cColor.
GetRed() / 255.0f;
207 pfColor[1] = cColor.
GetGreen() / 255.0f;
208 pfColor[2] = cColor.
GetBlue() / 255.0f;
209 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, pfColor);
211 const CVector3& cPosition = ps_actuator->LED.GetPosition();
212 glTranslated(cPosition.
GetX(), cPosition.
GetY(), cPosition.
GetZ());
214 glCallList(m_unLEDList);
225 const CVector3& cTagPosition = s_instance.Tag.GetPosition();
226 const CQuaternion& cTagOrientation = s_instance.Tag.GetOrientation();
228 Real fScaling = s_instance.Tag.GetSideLength();
230 glTranslated(cTagPosition.
GetX(),
232 cTagPosition.
GetZ());
233 glRotated(
ToDegrees(cX).GetValue(), 1.0f, 0.0f, 0.0f);
234 glRotated(
ToDegrees(cY).GetValue(), 0.0f, 1.0f, 0.0f);
235 glRotated(
ToDegrees(cZ).GetValue(), 0.0f, 0.0f, 1.0f);
236 glScaled(fScaling, fScaling, 1.0f);
237 glCallList(m_unTagList);
248 CRadians cFieldOrientationZ, cFieldOrientationY, cFieldOrientationX;
251 cFieldOrigin = s_instance.Offset;
252 cFieldOrigin.
Rotate(s_instance.Anchor.Orientation);
253 cFieldOrigin += s_instance.Anchor.Position;
254 const CVector3& cField = s_instance.Magnet.GetField();
255 cFieldOrientation = s_instance.Anchor.Orientation *
261 glTranslated(cFieldOrigin.
GetX(),
263 cFieldOrigin.
GetZ());
264 glRotated(
ToDegrees(cFieldOrientationX).GetValue(), 1.0f, 0.0f, 0.0f);
265 glRotated(
ToDegrees(cFieldOrientationY).GetValue(), 0.0f, 1.0f, 0.0f);
266 glRotated(
ToDegrees(cFieldOrientationZ).GetValue(), 0.0f, 0.0f, 1.0f);
267 glScaled(1.0f, 1.0f, cField.
Length() * FIELD_SCALE_FACTOR);
268 glCallList(m_unPoleList);
278 void CQTOpenGLPrototype::MakeBox() {
279 glEnable(GL_NORMALIZE);
281 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,
SPECULAR);
282 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS,
SHININESS);
283 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION,
EMISSION);
287 glNormal3d(0.0f, 0.0f, -1.0f);
288 glVertex3d( 0.5f, 0.5f, 0.0f);
289 glVertex3d( 0.5f, -0.5f, 0.0f);
290 glVertex3d(-0.5f, -0.5f, 0.0f);
291 glVertex3d(-0.5f, 0.5f, 0.0f);
293 glNormal3d(0.0f, 0.0f, 1.0f);
294 glVertex3d(-0.5f, -0.5f, 1.0f);
295 glVertex3d( 0.5f, -0.5f, 1.0f);
296 glVertex3d( 0.5f, 0.5f, 1.0f);
297 glVertex3d(-0.5f, 0.5f, 1.0f);
302 glNormal3d(0.0f, -1.0f, 0.0f);
303 glVertex3d(-0.5f, -0.5f, 1.0f);
304 glVertex3d(-0.5f, -0.5f, 0.0f);
305 glVertex3d( 0.5f, -0.5f, 0.0f);
306 glVertex3d( 0.5f, -0.5f, 1.0f);
308 glNormal3d(1.0f, 0.0f, 0.0f);
309 glVertex3d( 0.5f, -0.5f, 1.0f);
310 glVertex3d( 0.5f, -0.5f, 0.0f);
311 glVertex3d( 0.5f, 0.5f, 0.0f);
312 glVertex3d( 0.5f, 0.5f, 1.0f);
314 glNormal3d(0.0f, 1.0f, 0.0f);
315 glVertex3d( 0.5f, 0.5f, 1.0f);
316 glVertex3d( 0.5f, 0.5f, 0.0f);
317 glVertex3d(-0.5f, 0.5f, 0.0f);
318 glVertex3d(-0.5f, 0.5f, 1.0f);
320 glNormal3d(-1.0f, 0.0f, 0.0f);
321 glVertex3d(-0.5f, 0.5f, 1.0f);
322 glVertex3d(-0.5f, 0.5f, 0.0f);
323 glVertex3d(-0.5f, -0.5f, 0.0f);
324 glVertex3d(-0.5f, -0.5f, 1.0f);
326 glDisable(GL_NORMALIZE);
332 void CQTOpenGLPrototype::MakeCylinder() {
333 glEnable(GL_NORMALIZE);
335 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,
SPECULAR);
336 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS,
SHININESS);
337 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION,
EMISSION);
339 CVector2 cVertex(0.5f, 0.0f);
341 glBegin(GL_QUAD_STRIP);
342 for(GLuint i = 0; i <= m_unVertices; i++) {
343 glNormal3d(cVertex.GetX(), cVertex.GetY(), 0.0f);
344 glVertex3d(cVertex.GetX(), cVertex.GetY(), 1.0f);
345 glVertex3d(cVertex.GetX(), cVertex.GetY(), 0.0f);
346 cVertex.Rotate(cAngle);
350 cVertex.Set(0.5f, 0.0f);
352 glNormal3d(0.0f, 0.0f, 1.0f);
353 for(GLuint i = 0; i <= m_unVertices; i++) {
354 glVertex3d(cVertex.GetX(), cVertex.GetY(), 1.0f);
355 cVertex.Rotate(cAngle);
359 cVertex.Set(0.5f, 0.0f);
362 glNormal3d(0.0f, 0.0f, -1.0f);
363 for(GLuint i = 0; i <= m_unVertices; i++) {
364 glVertex3d(cVertex.GetX(), cVertex.GetY(), 0.0f);
365 cVertex.Rotate(cAngle);
368 glDisable(GL_NORMALIZE);
374 void CQTOpenGLPrototype::MakeSphere() {
375 glEnable(GL_NORMALIZE);
376 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,
SPECULAR);
377 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS,
SHININESS);
378 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION,
EMISSION);
379 CVector3 cNormal, cPoint;
381 glBegin(GL_TRIANGLE_STRIP);
382 for(CRadians cInclination; cInclination <=
CRadians::PI; cInclination += cSlice) {
384 cPoint.FromSphericalCoords(0.5f, cInclination, cAzimuth);
385 glNormal3d(cPoint.GetX(), cPoint.GetY(), cPoint.GetZ() + 0.5f);
386 glVertex3d(cPoint.GetX(), cPoint.GetY(), cPoint.GetZ() + 0.5f);
387 cPoint.FromSphericalCoords(0.5f, cInclination + cSlice, cAzimuth);
388 glNormal3d(cPoint.GetX(), cPoint.GetY(), cPoint.GetZ() + 0.5f);
389 glVertex3d(cPoint.GetX(), cPoint.GetY(), cPoint.GetZ() + 0.5f);
390 cPoint.FromSphericalCoords(0.5f, cInclination, cAzimuth + cSlice);
391 glNormal3d(cPoint.GetX(), cPoint.GetY(), cPoint.GetZ() + 0.5f);
392 glVertex3d(cPoint.GetX(), cPoint.GetY(), cPoint.GetZ() + 0.5f);
393 cPoint.FromSphericalCoords(0.5f, cInclination + cSlice, cAzimuth + cSlice);
394 glNormal3d(cPoint.GetX(), cPoint.GetY(), cPoint.GetZ() + 0.5f);
395 glVertex3d(cPoint.GetX(), cPoint.GetY(), cPoint.GetZ() + 0.5f);
399 glDisable(GL_NORMALIZE);
405 void CQTOpenGLPrototype::DrawConvexHull(
const std::vector<CVector3>& vec_points,
406 const std::vector<CConvexHull::SFace>& vec_faces) {
407 glEnable(GL_NORMALIZE);
408 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,
SPECULAR);
409 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS,
SHININESS);
410 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION,
EMISSION);
411 glBegin(GL_TRIANGLES);
412 for(
const CConvexHull::SFace& s_face : vec_faces) {
413 const CVector3& cVertex1 = vec_points[s_face.VertexIndices[0]];
414 const CVector3& cVertex2 = vec_points[s_face.VertexIndices[1]];
415 const CVector3& cVertex3 = vec_points[s_face.VertexIndices[2]];
416 CVector3 cNormal(cVertex2 - cVertex1);
417 cNormal.CrossProduct(cVertex3 - cVertex1);
418 glNormal3d(cNormal.GetX(), cNormal.GetY(), cNormal.GetZ());
419 glVertex3d(cVertex1.GetX(), cVertex1.GetY(), cVertex1.GetZ());
420 glVertex3d(cVertex2.GetX(), cVertex2.GetY(), cVertex2.GetZ());
421 glVertex3d(cVertex3.GetX(), cVertex3.GetY(), cVertex3.GetZ());
424 glDisable(GL_NORMALIZE);
430 void CQTOpenGLPrototype::MakeTagTexture() {
431 glGenTextures(1, &m_unTagTex);
432 glBindTexture(GL_TEXTURE_2D, m_unTagTex);
433 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 4, 4, 0, GL_RGB, GL_FLOAT, m_fTagTexture);
439 void CQTOpenGLPrototype::MakeTag() {
440 glEnable(GL_NORMALIZE);
441 glDisable(GL_LIGHTING);
442 glEnable(GL_TEXTURE_2D);
443 glBindTexture(GL_TEXTURE_2D, m_unTagTex);
444 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
445 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
446 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
447 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
449 glNormal3d(0.0f, 0.0f, 1.0f);
450 glTexCoord2f(1.0f, 1.0f); glVertex2f( 0.5f, 0.5f);
451 glTexCoord2f(0.03f, 1.0f); glVertex2f(-0.5f, 0.5f);
452 glTexCoord2f(0.03f, 0.03f); glVertex2f(-0.5f, -0.5f);
453 glTexCoord2f(1.0f, 0.03f); glVertex2f( 0.5f, -0.5f);
455 glDisable(GL_TEXTURE_2D);
456 glEnable(GL_LIGHTING);
457 glDisable(GL_NORMALIZE);
463 void CQTOpenGLPrototype::MakePoles() {
464 glEnable(GL_NORMALIZE);
465 glDisable(GL_LIGHTING);
469 glColor3f(1.0, 0.0, 0.0);
470 glVertex3d(0.0f, 0.0f, 0.0f);
471 glVertex3d(0.0f, 0.0f, 0.5f);
473 glColor3f(0.0, 0.0, 1.0);
474 glVertex3d(0.0f, 0.0f, 0.0f);
475 glVertex3d(0.0f, 0.0f, -0.5f);
478 glEnable(GL_LIGHTING);
479 glDisable(GL_NORMALIZE);
float Real
Collects all ARGoS code.
The namespace containing all the ARGoS related code.
const GLfloat SHININESS[]
REGISTER_ENTITY_OPERATION(CQTOpenGLOperationDrawNormal, CQTOpenGLWidget, CQTOpenGLOperationDrawPrototypeNormal, void, CPrototypeEntity)
const GLfloat BODY_COLOR[]
CDegrees ToDegrees(const CRadians &c_radians)
Converts CRadians to CDegrees.
UInt8 GetBlue() const
Returns the blue channel of the color.
UInt8 GetGreen() const
Returns the green channel of the color.
UInt8 GetRed() const
Returns the red channel of the color.
It defines the basic type CRadians, used to store an angle value in radians.
static const CRadians PI
The PI constant.
static const CRadians TWO_PI
Set to PI * 2.
void ToEulerAngles(CRadians &c_z_angle, CRadians &c_y_angle, CRadians &c_x_angle) const
Real Length() const
Returns the length of this vector.
CVector3 & Rotate(const CQuaternion &c_quaternion)
Rotates this vector by the given quaternion.
Real GetX() const
Returns the x coordinate of this vector.
CVector3 & FromSphericalCoords(Real f_length, const CRadians &c_inclination, const CRadians &c_azimuth)
Sets the vector contents from spherical coordinates.
Real GetY() const
Returns the y coordinate of this vector.
static const CVector3 Z
The z axis.
Real GetZ() const
Returns the z coordinate of this vector.
bool HasLEDEquippedEntity() const
bool HasControllableEntity() const
CControllableEntity & GetControllableEntity()
bool HasTagEquippedEntity() const
bool HasDirectionalLEDEquippedEntity() const
CLEDEquippedEntity & GetLEDEquippedEntity()
bool HasMagnetEquippedEntity() const
CMagnetEquippedEntity & GetMagnetEquippedEntity()
CTagEquippedEntity & GetTagEquippedEntity()
CDirectionalLEDEquippedEntity & GetDirectionalLEDEquippedEntity()
CEmbodiedEntity & GetEmbodiedEntity()
CPrototypeLinkEquippedEntity & GetLinkEquippedEntity()
CPrototypeLinkEntity::TVector & GetLinks()
void ApplyTo(CQTOpenGLWidget &c_visualization, CPrototypeEntity &c_entity)
void ApplyTo(CQTOpenGLWidget &c_visualization, CPrototypeEntity &c_entity)
virtual void DrawEntity(CPrototypeEntity &c_entity)
virtual void DrawDevices(CPrototypeEntity &c_entity)
virtual ~CQTOpenGLPrototype()
A container of CDirectionalLEDEntity.
SInstance::TVector & GetInstances()
Returns all the LEDs.
SActuator::TList & GetLEDs()
Returns all the LEDs.
A container of CMagnetEntity.
SInstance::TVector & GetInstances()
A container of CTagEntity.
SInstance::TVector & GetInstances()
Returns the tags.
void DrawRays(CControllableEntity &c_entity)
Draws a ray.
void DrawBoundingBox(CEmbodiedEntity &c_entity)
Draws the bounding box of an embodied entity.