Cheetah Software  1.0
Graphics3D.cpp
Go to the documentation of this file.
1 
8 #include "Graphics3D.h"
9 #include "Utilities/utilities.h"
10 
11 #include <GL/glut.h>
12 #include <unistd.h>
13 #include <iostream>
14 
15 // background color
16 static constexpr auto clearColor = windows2000;
17 
18 // shader program for drawing models where each vertex is assigned a color
19 static constexpr char vertexShaderSourceColorArray[] = R"(
20 // inputs:
21 attribute highp vec3 posAttr; // position
22 attribute lowp vec3 colAttr; // color
23 attribute highp vec3 normAttr; // normal
24 uniform highp mat4 matrix; // transformation
25 // outputs:
26 varying lowp vec4 col; // color
27 varying vec3 normal; // normal
28 varying vec3 pos_world; // position
29 void main() {
30  col = vec4(colAttr,1);
31  gl_Position = matrix * vec4(posAttr,1);
32  normal = (matrix * vec4(normAttr,0)).xyz;
33  pos_world = posAttr;
34 }
35 )";
36 
37 // shader program for drawing models where each vertex
38 static constexpr char vertexShaderSourceSolidColor[] = R"(
39 // inputs:
40 attribute highp vec3 posAttr; // position
41 uniform lowp vec4 colUniform; // color
42 attribute lowp vec3 colAttr; // color
43 attribute highp vec3 normAttr; // normal
44 uniform highp mat4 matrix; // transformation
45 // outputs:
46 varying lowp vec4 col; // color
47 varying vec3 normal; // normal
48 varying vec3 pos_world; // position
49 void main() {
50  col = colUniform;
51  gl_Position = matrix * vec4(posAttr,1);
52  normal = (matrix * vec4(normAttr,0)).xyz;
53  pos_world = posAttr;
54 }
55 )";
56 
57 static constexpr char fragmentShaderSource[] = R"(
58 varying lowp vec4 col;
59 varying vec3 pos_world;
60 varying vec3 normal;
61 void main() {
62  vec3 light_pos = vec3(0,0,4);
63  float light_dist = length(light_pos - pos_world);
64  vec3 light_color = vec3(1,1,1);
65  vec3 mat_ambient = vec3(.1,.1,.1);
66  vec3 mat_spec = vec3(.3,.3,.3);
67  vec3 n = normalize(normal);
68  vec3 l = normalize(light_pos);
69  float angle_thing = clamp( dot(-n,l), 0., 1.);
70  gl_FragColor = col + vec4(mat_ambient*light_color*40.*angle_thing/(light_dist*light_dist),0);
71 }
72 )";
73 
77 Graphics3D::Graphics3D(QWidget *parent)
78  : QOpenGLWidget(parent),
79  _animating(false),
80  _colorArrayProgram(0),
81  _frame(0),
82  _v0(0, 0, 0),
83  _freeCamFilter(1, 60, _v0) {
84  std::cout << "[SIM GRAPHICS] New graphics window. \n";
85 
86  _r[0] = 0.2422;
87  _g[0] = 0.1504;
88  _b[0] = 0.6603;
89  _r[1] = 0.2810;
90  _g[1] = 0.3228;
91  _b[1] = 0.9579;
92  _r[2] = 0.1786;
93  _g[2] = 0.5289;
94  _b[2] = 0.9682;
95  _r[3] = 0.0689;
96  _g[3] = 0.6948;
97  _b[3] = 0.8394;
98  _r[4] = 0.2161;
99  _g[4] = 0.7843;
100  _b[4] = 0.5923;
101  _r[5] = 0.6720;
102  _g[5] = 0.7793;
103  _b[5] = 0.2227;
104  _r[6] = 0.9970;
105  _g[6] = 0.7659;
106  _b[6] = 0.2199;
107  _r[7] = 0.9769;
108  _g[7] = 0.9839;
109  _b[7] = 0.0805;
110 }
111 
113 
117 size_t Graphics3D::setupCheetah3(Vec4<float> color, bool useOld) { return _drawList.addCheetah3(color, useOld); }
118 
122 size_t Graphics3D::setupMiniCheetah(Vec4<float> color, bool useOld) { return _drawList.addMiniCheetah(color, useOld); }
123 
129  _cameraMatrix.setToIdentity();
130  _cameraMatrix.perspective(
131  60.f, float(size().width()) / float(size().height()), .001f, 50.f);
132 
133  if (_arrowsPressed[0]) _ry -= _targetSpeed / 2.f;
134  if (_arrowsPressed[1]) _ry += _targetSpeed / 2.f;
135  if (_arrowsPressed[2]) _rx += _targetSpeed / 2.f;
136  if (_arrowsPressed[3]) _rx -= _targetSpeed / 2.f;
137  if (!_rotOrig) {
138  _ry = coerce<float>(_ry, -180, 0);
139  // velocity in camera coordinates
140  // we want the inverse transformation (coordinateRotation goes the opposite
141  // way as QMatrix.rotate())
142  RotMat<float> R =
143  coordinateRotation<float>(CoordinateAxis::Z, deg2rad(_rx)) *
144  coordinateRotation<float>(CoordinateAxis::X, deg2rad(_ry));
146  v = R * v;
147 
148  // integrate and filter
150  for (size_t i = 0; i < 3; i++)
152 
153  // apply
154  _cameraMatrix.rotate(_ry, 1, 0, 0);
155  _cameraMatrix.rotate(_rx, 0, 0, 1);
156  _cameraMatrix.translate(_freeCamPos[0], _freeCamPos[1], _freeCamPos[2]);
157  } else {
158  _cameraMatrix.translate(0.f, 0.f, -.45f * _zoom);
159  _cameraMatrix.rotate(_ry, 1, 0, 0);
160  _cameraMatrix.rotate(_rx, 0, 0, 1);
162  _cameraMatrix.translate(-_cameraTarget[0],
163  -_cameraTarget[1], 0);
164  }
165 }
166 
168  std::cout << "[Graphics3D] Initialize OpenGL...\n";
169  _cameraTarget.setZero();
170  initializeOpenGLFunctions();
171  // create GPU shaders
172  _colorArrayProgram = new QOpenGLShaderProgram(this);
173  _colorArrayProgram->addShaderFromSourceCode(QOpenGLShader::Vertex,
175  _colorArrayProgram->addShaderFromSourceCode(QOpenGLShader::Fragment,
177  _colorArrayProgram->link();
178 
179  _solidColorProgram = new QOpenGLShaderProgram(this);
180  _solidColorProgram->addShaderFromSourceCode(QOpenGLShader::Vertex,
182  _solidColorProgram->addShaderFromSourceCode(QOpenGLShader::Fragment,
184  _solidColorProgram->link();
185 
186  // setup attributes/uniforms (inputs to shaders)
187  _posAttrColorArray = (GLuint)_colorArrayProgram->attributeLocation("posAttr");
188  _colAttrColorArray = (GLuint)_colorArrayProgram->attributeLocation("colAttr");
190  (GLuint)_colorArrayProgram->attributeLocation("normAttr");
192  (GLuint)_colorArrayProgram->uniformLocation("matrix");
193 
194  _posAttrSolidColor = (GLuint)_solidColorProgram->attributeLocation("posAttr");
196  (GLuint)_solidColorProgram->uniformLocation("colUniform");
197  _colAttrSolidColor = (GLuint)_solidColorProgram->attributeLocation("colAttr");
199  (GLuint)_solidColorProgram->attributeLocation("normAttr");
200  _normAttrSolidColor = (GLuint)_solidColorProgram->uniformLocation("matrix");
201 
202  // set clear color:
203  glClearColor(clearColor[0], clearColor[1], clearColor[2], 0.f);
204 }
205 
206 /*-----------------------------------------*
207  * Mouse Handlers for Orbit and Zoom *
208  *-----------------------------------------*/
209 
210 void Graphics3D::mousePressEvent(QMouseEvent *event) {
211  _orbiting = true;
212  _orbiting_x_start = event->pos().x();
213  _orbiting_y_start = event->pos().y();
214  _rx_base = _rx;
215  _ry_base = _ry;
216 }
217 
218 void Graphics3D::mouseMoveEvent(QMouseEvent *event) {
219  if (!_orbiting) return;
220  _rx = _rx_base + _pixel_to_rad * (event->pos().x() - _orbiting_x_start);
221  _ry = _ry_base + _pixel_to_rad * (event->pos().y() - _orbiting_y_start);
222 }
223 
224 void Graphics3D::mouseReleaseEvent(QMouseEvent *event) {
225  _orbiting = false;
226  _rx_base = _rx_base + _pixel_to_rad * (event->pos().x() - _orbiting_x_start);
227  _ry_base = _ry_base + _pixel_to_rad * (event->pos().y() - _orbiting_y_start);
228 }
229 
230 void Graphics3D::wheelEvent(QWheelEvent *e) {
231  if (e->angleDelta().y() > 0) {
232  if (_zoom > .1) _zoom = 0.8f * _zoom;
233  } else {
234  if (_zoom < 100) _zoom = 1.2f * _zoom;
235  }
236 }
237 
238 void Graphics3D::keyPressEvent(QKeyEvent *e) {
239  if (e->key() == Qt::Key_Control) {
240  _targetSpeed *= .5f;
241  } else if (e->key() == Qt::Key_Shift) {
242  _targetSpeed *= 2.f;
243  }
244  if (e->key() == Qt::Key_W)
246  else if (e->key() == Qt::Key_S)
248 
249  if (e->key() == Qt::Key_A)
251  else if (e->key() == Qt::Key_D)
253 
254  if (e->key() == Qt::Key_R)
256  else if (e->key() == Qt::Key_F)
258 
259  if (e->key() == Qt::Key_Up)
260  _arrowsPressed[0] = true;
261  else if (e->key() == Qt::Key_Down)
262  _arrowsPressed[1] = true;
263 
264  if (e->key() == Qt::Key_Right)
265  _arrowsPressed[2] = true;
266  else if (e->key() == Qt::Key_Left)
267  _arrowsPressed[3] = true;
268 
269  if (e->key() == Qt::Key_V) {
270  if (_pause)
271  _pause = false;
272  else
273  _pause = true;
274  }
275 
276  if(e->key() == Qt::Key_T) {
277  _turbo = true;
278  }
279 
280  if (e->key() == Qt::Key_Tab) {
281  _freeCamPos[0] = 0.f;
282  _freeCamPos[1] = 0.f;
283  _freeCamPos[2] = 0.f;
284  _rx = 0.f;
285  _ry = 0.f;
286  }
287 }
288 
289 void Graphics3D::keyReleaseEvent(QKeyEvent *e) {
290  if (e->key() == Qt::Key_Control) {
291  _targetSpeed /= .5f;
292  } else if (e->key() == Qt::Key_Shift) {
293  _targetSpeed /= 2.f;
294  } else if (e->key() == Qt::Key_Space) {
295  _rotOrig = !_rotOrig;
296  }
297 
298  if (e->key() == Qt::Key_W)
299  _freeCamMove[2] = 0;
300  else if (e->key() == Qt::Key_S)
301  _freeCamMove[2] = 0;
302 
303  if (e->key() == Qt::Key_A)
304  _freeCamMove[0] = 0;
305  else if (e->key() == Qt::Key_D)
306  _freeCamMove[0] = 0;
307 
308  if (e->key() == Qt::Key_R)
309  _freeCamMove[1] = 0;
310  else if (e->key() == Qt::Key_F)
311  _freeCamMove[1] = 0;
312 
313  if (e->key() == Qt::Key_Up)
314  _arrowsPressed[0] = false;
315  else if (e->key() == Qt::Key_Down)
316  _arrowsPressed[1] = false;
317 
318  if (e->key() == Qt::Key_Right)
319  _arrowsPressed[2] = false;
320  else if (e->key() == Qt::Key_Left)
321  _arrowsPressed[3] = false;
322 
323  if(e->key() == Qt::Key_T) {
324  _turbo = false;
325  }
326 }
327 
331 void Graphics3D::setAnimating(bool animating) { _animating = animating; }
332 
334  // reload if needed
335  if (_drawList.needsReload()) {
336  // upload data
337 
338  glDisableVertexAttribArray(2);
339  glDisableVertexAttribArray(1);
340  glDisableVertexAttribArray(0);
341 
342  glGenBuffers(3, _buffID);
343 
344  glBindBuffer(GL_ARRAY_BUFFER, _buffID[0]);
345  glBufferData(GL_ARRAY_BUFFER, sizeof(float) * _drawList.getSizeOfAllData(),
346  _drawList.getVertexArray(), GL_STATIC_DRAW);
347  glVertexAttribPointer(_posAttrColorArray, 3, GL_FLOAT, GL_FALSE, 0, 0);
348 
349  glBindBuffer(GL_ARRAY_BUFFER, _buffID[1]);
350  glBufferData(GL_ARRAY_BUFFER, sizeof(float) * _drawList.getSizeOfAllData(),
351  _drawList.getColorArray(), GL_STATIC_DRAW);
352  glVertexAttribPointer(_colAttrColorArray, 3, GL_FLOAT, GL_FALSE, 0, 0);
353 
354  glBindBuffer(GL_ARRAY_BUFFER, _buffID[2]);
355  glBufferData(GL_ARRAY_BUFFER, sizeof(float) * _drawList.getSizeOfAllData(),
356  _drawList.getNormalArray(), GL_STATIC_DRAW);
357  glVertexAttribPointer(_normAttrColorArray, 3, GL_FLOAT, GL_FALSE, 0, 0);
358 
359  glEnableVertexAttribArray(0);
360  glEnableVertexAttribArray(1);
361  glEnableVertexAttribArray(2);
362  printf("[Graphics 3D] Uploaded data (%f MB)\n",
364  glBindBuffer(GL_ARRAY_BUFFER, 0);
365  }
366 
367  // draw objects with color arrays
368  _colorArrayProgram->bind();
369 
370  glBindBuffer(GL_ARRAY_BUFFER, _buffID[0]);
371  glVertexAttribPointer(_posAttrColorArray, 3, GL_FLOAT, GL_FALSE, 0, 0);
372  glBindBuffer(GL_ARRAY_BUFFER, _buffID[1]);
373  glVertexAttribPointer(_colAttrColorArray, 3, GL_FLOAT, GL_FALSE, 0, 0);
374  glBindBuffer(GL_ARRAY_BUFFER, _buffID[2]);
375  glVertexAttribPointer(_normAttrColorArray, 3, GL_FLOAT, GL_FALSE, 0, 0);
376  glEnableVertexAttribArray(0);
377  glEnableVertexAttribArray(1);
378  glEnableVertexAttribArray(2);
379 
380  for (size_t id = 0; id < _drawList.getNumObjectsToDraw(); id++) {
381  if (_drawList._instanceColor[id].useSolidColor) {
382  } else {
383  _colorArrayProgram->setUniformValue(
387 
388  glDrawArrays(GL_TRIANGLES, _drawList.getGLDrawArrayOffset(id) / 3,
389  _drawList.getGLDrawArraySize(id) / 3);
390  }
391  }
392  glDisableVertexAttribArray(2);
393  glDisableVertexAttribArray(1);
394  glDisableVertexAttribArray(0);
395 
396  glBindBuffer(GL_ARRAY_BUFFER, 0);
397 
398  _colorArrayProgram->release();
399 
400  // draw objects without color arrays
401  _solidColorProgram->bind();
402 
403  glBindBuffer(GL_ARRAY_BUFFER, _buffID[0]);
404  glVertexAttribPointer(_posAttrSolidColor, 3, GL_FLOAT, GL_FALSE, 0, 0);
405  glBindBuffer(GL_ARRAY_BUFFER, _buffID[1]);
406  glVertexAttribPointer(_colAttrSolidColor, 3, GL_FLOAT, GL_FALSE, 0, 0);
407  glBindBuffer(GL_ARRAY_BUFFER, _buffID[2]);
408  glVertexAttribPointer(_normAttrSolidColor, 3, GL_FLOAT, GL_FALSE, 0, 0);
409  glEnableVertexAttribArray(0);
410  glEnableVertexAttribArray(1);
411  glEnableVertexAttribArray(2);
412  for (size_t id = 0; id < _drawList.getNumObjectsToDraw(); id++) {
413  if (_drawList._instanceColor[id].useSolidColor) {
414  _solidColorProgram->setUniformValue(
418  auto &col = _drawList._instanceColor[id];
419  _solidColorProgram->setUniformValue(
421  QVector4D(col.rgba[0], col.rgba[1], col.rgba[2], col.rgba[3]));
422 
423  glDrawArrays(GL_TRIANGLES, _drawList.getGLDrawArrayOffset(id) / 3,
424  _drawList.getGLDrawArraySize(id) / 3);
425  } else {
426  }
427  }
428  glDisableVertexAttribArray(2);
429  glDisableVertexAttribArray(1);
430  glDisableVertexAttribArray(0);
431 
432  glBindBuffer(GL_ARRAY_BUFFER, 0);
433 
434  _solidColorProgram->release();
435 }
436 
438  switch (pass) {
439  case 0:
440  // clear screen
441  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
442 
443  // update camera math
444 
445  glDisable(GL_CULL_FACE);
446 
447  glEnable(GL_BLEND);
448  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
449  glEnable(GL_DEPTH_TEST);
450  glDepthFunc(GL_LESS);
451  break;
452  case 1:
453  glEnable(GL_CULL_FACE);
454  glCullFace(GL_FRONT);
455  glDepthFunc(GL_ALWAYS);
456  break;
457  case 2:
458  glEnable(GL_CULL_FACE);
459  glCullFace(GL_FRONT);
460  glDepthFunc(GL_LEQUAL);
461  break;
462  case 3:
463  glEnable(GL_CULL_FACE);
464  glCullFace(GL_BACK);
465  glDepthFunc(GL_ALWAYS);
466  break;
467  case 4:
468  glDisable(GL_CULL_FACE);
469  glDepthFunc(GL_LEQUAL);
470  break;
471 
472  default:
473  assert(false);
474  }
475 }
476 
478  // update joystick:
480  if (!_animating) return;
481  if (_frame % 60 == 0) {
482  qint64 now = QDateTime::currentMSecsSinceEpoch();
483  _fps = (60.f * 1000.f / (now - last_frame_ms));
484  // std::cout << "FPS: " << _fps << "\n";
485  last_frame_ms = now;
486  }
487  QPainter painter2(this);
489 
490  int passes[] = {0, 1, 2, 3, 4};
491  for (int pass : passes) {
492  // setup pass:
493  glShadeModel(GL_SMOOTH);
494  configOpenGLPass(pass);
495 
496  // do rendering on all passes:
497  glEnable(GL_BLEND);
498  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
499  renderDrawlist();
500  _Additional_Drawing(pass);
502  }
503 
505 
506  glDisable(GL_DEPTH_TEST);
507  painter2.setPen(QColor(100, 100, 100, 255));
508  painter2.fillRect(QRect(30, 30, 400, 200), QColor(100, 100, 100, 220));
509  QFont font("Monospace", 20);
510  painter2.setPen(QColor(210, 100, 100));
511  painter2.setFont(font);
512  painter2.drawText(QRect(30, 30, 1000, 1000), Qt::AlignLeft,
513  QString(infoString));
514  painter2.end();
515 
516  ++_frame;
517 }
518 
520  glLoadMatrixf(_cameraMatrix.data());
521  glPushAttrib(GL_LIGHTING_BIT);
522  glDisable(GL_LIGHTING);
523 
524  glPushMatrix();
525  glTranslatef(_drawList.getHeightMapLeftCorner()[0],
528 
529  double height_min = _drawList.getHeightMapMax();
530  double height_max = _drawList.getHeightMapMin();
531  double height_gap = height_max - height_min;
532  double scaled_height(0.);
533 
534  float color_r(0.f);
535  float color_g(0.f);
536  float color_b(0.f);
537 
538  double grid_size(_drawList.getGridSize());
539  double height;
540  for (int i(0); i < _drawList.getHeightMap().rows(); ++i) {
541  glBegin(GL_LINE_STRIP);
542  glPushAttrib(GL_COLOR_BUFFER_BIT);
543  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
544 
545  for (int j(0); j < _drawList.getHeightMap().cols(); ++j) {
546  height = _drawList.getHeightMap()(i, j);
547  scaled_height = (height - height_min) / height_gap;
548  getHeightColor(scaled_height, color_r, color_g, color_b);
549  glColor4f(color_r, color_g, color_b, 1.0f);
550  glVertex3d(i * grid_size, j * grid_size, height);
551  }
552  glPopAttrib();
553  glDisable(GL_BLEND);
554  glEnd();
555  }
556  for (int j(0); j < _drawList.getHeightMap().cols(); ++j) {
557  glBegin(GL_LINE_STRIP);
558  glPushAttrib(GL_COLOR_BUFFER_BIT);
559  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
560 
561  for (int i(0); i < _drawList.getHeightMap().rows(); ++i) {
562  height = _drawList.getHeightMap()(i, j);
563  scaled_height = (height - height_min) / height_gap;
564  getHeightColor(scaled_height, color_r, color_g, color_b);
565  glColor4f(color_r, color_g, color_b, 1.0f);
566  glVertex3d(i * grid_size, j * grid_size, height);
567  }
568  glPopAttrib();
569  glDisable(GL_BLEND);
570  glEnd();
571  }
572  glPopMatrix();
573  glEnable(GL_LIGHTING);
574  glPopAttrib();
575 }
576 
577 void Graphics3D::getHeightColor(const double &h, float &r, float &g, float &b) {
578  // 0 : 0.2422 0.1504 0.6603
579  // 1/7: 0.2810 0.3228 0.9579
580  // 2/7: 0.1786 0.5289 0.9682
581  // 3/7: 0.0689 0.6948 0.8394
582  // 4/7: 0.2161 0.7843 0.5923
583  // 5/7: 0.6720 0.7793 0.2227
584  // 6/7: 0.9970 0.7659 0.2199
585  // 1: 0.9769 0.9839 0.0805
586  double step(1. / 7.);
587  if (h > 6 * step) {
588  _SetRGBHeight(h, step, 6, r, g, b);
589  } else if (h > 5 * step) {
590  _SetRGBHeight(h, step, 5, r, g, b);
591  } else if (h > 4 * step) {
592  _SetRGBHeight(h, step, 4, r, g, b);
593  } else if (h > 3 * step) {
594  _SetRGBHeight(h, step, 3, r, g, b);
595  } else if (h > 2 * step) {
596  _SetRGBHeight(h, step, 2, r, g, b);
597  } else if (h > 1 * step) {
598  _SetRGBHeight(h, step, 1, r, g, b);
599  } else {
600  _SetRGBHeight(h, step, 0, r, g, b);
601  }
602 }
603 void Graphics3D::_SetRGBHeight(const double &h, const double &step,
604  const int &idx, float &r, float &g, float &b) {
605  double grade = (h - idx * step) / step;
606  r = (_r[idx + 1] - _r[idx]) * grade + _r[idx];
607  g = (_g[idx + 1] - _g[idx]) * grade + _g[idx];
608  b = (_b[idx + 1] - _b[idx]) * grade + _b[idx];
609 }
610 
612  glLoadMatrixf(_cameraMatrix.data());
613  glPushAttrib(GL_LIGHTING_BIT);
614  glDisable(GL_LIGHTING);
615 
616  size_t nBox = _drawList.getBoxInfoList().size();
617  for (size_t i(0); i < nBox; ++i) {
618  glPushMatrix();
619  glMultMatrixf(_drawList.getBoxInfoList()[i].frame); // column major
620  _DrawBox(_drawList.getBoxInfoList()[i].depth,
621  _drawList.getBoxInfoList()[i].width,
622  _drawList.getBoxInfoList()[i].height);
623  glPopMatrix();
624  }
625  glEnable(GL_LIGHTING);
626  glPopAttrib();
627 }
628 
629 void Graphics3D::_DrawBox(double depth, double width, double height) {
630  double x = depth / 2.0;
631  double y = width / 2.0;
632  double z = height / 2.0;
633 
634  glPushAttrib(GL_COLOR_BUFFER_BIT);
635 
636  glEnable(GL_BLEND);
637  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
638 
639  glColor4f(_color1[0], _color1[1], _color1[2], 0.7f);
640 
641  glBegin(GL_QUADS);
642  {
643  glVertex3d(x, y, -z);
644  glVertex3d(-x, y, -z);
645  glVertex3d(-x, y, z);
646  glVertex3d(x, y, z);
647 
648  glVertex3d(x, -y, -z);
649  glVertex3d(x, y, -z);
650  glVertex3d(x, y, z);
651  glVertex3d(x, -y, z);
652 
653  glVertex3d(x, -y, z);
654  glVertex3d(-x, -y, z);
655  glVertex3d(-x, -y, -z);
656  glVertex3d(x, -y, -z);
657 
658  glVertex3d(-x, -y, z);
659  glVertex3d(-x, y, z);
660  glVertex3d(-x, y, -z);
661  glVertex3d(-x, -y, -z);
662 
663  glVertex3d(-x, -y, -z);
664  glVertex3d(-x, y, -z);
665  glVertex3d(x, y, -z);
666  glVertex3d(x, -y, -z);
667 
668  glVertex3d(-x, -y, z);
669  glVertex3d(x, -y, z);
670  glVertex3d(x, y, z);
671  glVertex3d(-x, y, z);
672  } // GL_QUADS
673  glEnd();
674 
675  glPopAttrib();
676  glDisable(GL_BLEND);
677 }
678 
680  glLoadMatrixf(_cameraMatrix.data());
681  glPushAttrib(GL_LIGHTING_BIT);
682  glDisable(GL_LIGHTING);
683 
686 
687  configOpenGLPass(pass);
688  glEnable(GL_BLEND);
689  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
690 
691  for (size_t i = 0; i < _drawList._visualizationData->num_arrows; i++) {
693  }
694 
695  for (size_t i = 0; i < _drawList._visualizationData->num_cones; i++) {
697  }
698 
699  for (size_t i = 0; i < _drawList._visualizationData->num_blocks; i++) {
701  }
702 
703  for (size_t i = 0; i < _drawList._visualizationData->num_spheres; i++) {
705  }
706  glDisable(GL_BLEND);
707 
708  for (size_t i = 0; i < _drawList._visualizationData->num_paths; i++) {
710  glColor4f(path.color[0], path.color[1], path.color[2], path.color[3]);
711  glBegin(GL_LINE_STRIP);
712  for (size_t j = 0; j < path.num_points; j++) {
713  glVertex3d(path.position[j][0], path.position[j][1], path.position[j][2]);
714  }
715  glEnd();
716  }
717 
718  glPopAttrib();
719  glEnable(GL_LIGHTING);
720 }
721 
723  glLineWidth(2.0);
724  // double scale(0.02);
725  double scale(0.0025);
726 
727  glPushAttrib(GL_COLOR_BUFFER_BIT);
728  glEnable(GL_BLEND);
729  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
730 
731  for (size_t i(0); i < _drawList.getTotalNumGC(); ++i) {
732  glColor4f(0.8f, 0.0f, 0.f, 0.5f);
733  Vec3<float> floatForce, floatPos;
734  for (size_t j = 0; j < 3; j++) {
735  floatPos(j) = _drawList.getGCPos(i)[j];
736  floatForce(j) = scale * _drawList.getGCForce(i)[j];
737  }
738  _drawArrow(floatPos, floatForce, .005, .015, .04);
739  }
740  glPopAttrib();
741  glDisable(GL_BLEND);
742 }
743 
745  glPointSize(5);
746 
747  glPushAttrib(GL_COLOR_BUFFER_BIT);
748  glEnable(GL_BLEND);
749  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
750 
751  for (size_t i(0); i < _drawList.getTotalNumGC(); ++i) {
752  glBegin(GL_POINTS);
753  glColor4f(0.8f, 0.0f, 0.1f, 0.3f);
754 
755  glVertex3f(_drawList.getGCPos(i)[0], _drawList.getGCPos(i)[1],
756  _drawList.getGCPos(i)[2]);
757 
758  glEnd();
759  }
760  glPopAttrib();
761  glDisable(GL_BLEND);
762 }
763 
765  float len = direction.norm();
766  float dxn = direction(0) / len;
767  float dyn = direction(1) / len;
768  float dzn = direction(2) / len;
769 
770  // Note: We need to create a rotation such that the z-axis points in the
771  // direction of the 'direction' argument
772  // Thus, we can realize the rotation with a pure x-y axial rotation
773  // The rotation matrix of the rotated frame 'r' to the current frame 'c'
774  // (c_R_r) has special form. It's 3-rd column in particular has form: [
775  // wy*sin(theta) -wx*sin(theta) (1- cos(theta))]' where theta , [wx wy
776  // 0] is the angle, axis of rotation
777 
778  // Find the rotation angle by comparing the z-axis of the current and rotated
779  // frame
780  const double cosTheta = dzn;
781  const double theta = acos(cosTheta);
782  const double sinTheta = sin(theta);
783 
784  // Exploit the special form of the rotation matrix (explained above) for find
785  // the axis of rotation
786  float rX, rY, rZ;
787  if (theta > 0) {
788  rX = -dyn / sinTheta;
789  rY = dxn / sinTheta;
790  rZ = 0;
791  } else {
792  rX = 0;
793  rY = 0;
794  rZ = 1;
795  }
796  glRotatef(ori::rad2deg(theta), rX, rY, rZ);
797 }
798 
800  static GLUquadric *quad = gluNewQuadric();
801  glPushMatrix();
802  _translate(sphere.position);
803  _setColor(sphere.color);
804  gluSphere(quad, sphere.radius, 16, 16);
805  glPopMatrix();
806 }
807 
809  static GLUquadric *quad = gluNewQuadric();
810  float len = cone.direction.norm();
811 
812  glPushMatrix();
813  _setColor(cone.color);
816  const int detail = 32;
817  gluCylinder(quad, 0, cone.radius, len, detail, 1);
818  glPopMatrix();
819 }
820 
822  glPushMatrix();
824  _setColor(box.color);
825  glRotatef(box.rpy(2), 0, 0, 1);
826  glRotatef(box.rpy(1), 0, 1, 0);
827  glRotatef(box.rpy(0), 1, 0, 0);
828 
829  glScalef(box.dimension(0), box.dimension(1), box.dimension(2));
830  glTranslatef(.5, .5, .5);
831  _DrawBox(1, 1, 1);
832  glPopMatrix();
833 }
834 
836  _setColor(arrow.color);
837  _drawArrow(arrow.base_position, arrow.direction, arrow.shaft_width,
838  arrow.head_width, arrow.head_length);
839 }
840 
841 void Graphics3D::_drawArrow(const Vec3<float> &position,
842  const Vec3<float> &direction, float lineWidth,
843  float headWidth, float headLength) {
844  double len = direction.norm();
845 
846  glPushMatrix();
847  _translate(position);
848  _rotateZtoDirection(direction);
849 
850  double cylinderLength = len;
851  if (cylinderLength > headLength) {
852  cylinderLength -= headLength;
853  } else {
854  headLength = cylinderLength;
855  cylinderLength = 0;
856  }
857 
858  const int detail = 32;
859  static GLUquadric *quad = gluNewQuadric();
860 
861  // glPolygonMode(GL_FRONT, GL_FILL);
862  // Draw Cylinder
863  gluCylinder(quad, lineWidth, lineWidth, cylinderLength, detail, 1);
864 
865  // Draw Cylinder Base
866  glRotated(180, 1, 0, 0);
867  gluDisk(quad, 0, lineWidth, detail, detail);
868  glRotated(180, 1, 0, 0);
869 
870  glTranslatef(0, 0, cylinderLength);
871  // Draw Arrowhead
872  gluCylinder(quad, headWidth, 0.0f, headLength, detail, detail);
873 
874  glRotated(180, 1, 0, 0);
875  // Draw Arrowhead Base
876  gluDisk(quad, lineWidth, headWidth, detail, detail);
877  glPopMatrix();
878 }
void keyPressEvent(QKeyEvent *event) override
Definition: Graphics3D.cpp:238
static constexpr char fragmentShaderSource[]
Definition: Graphics3D.cpp:57
ArrowVisualization arrows[VISUALIZATION_MAX_ITEMS]
void initializeGL() override
Definition: Graphics3D.cpp:167
size_t addCheetah3(Vec4< float > color, bool useOld)
Definition: DrawList.cpp:48
VisualizationData * _visualizationData
Definition: DrawList.h:51
T rad2deg(T rad)
void _DrawContactPoint()
Definition: Graphics3D.cpp:744
float _ry_base
Definition: Graphics3D.h:136
FirstOrderIIRFilter< Vec3< float >, float > _freeCamFilter
Definition: Graphics3D.h:148
bool _arrowsPressed[4]
Definition: Graphics3D.h:154
int _orbiting_x_start
Definition: Graphics3D.h:133
void getHeightColor(const double &height, float &r, float &g, float &b)
Definition: Graphics3D.cpp:577
float getGLDataSizeMB()
Definition: DrawList.h:142
Vec3< float > point_position
float _g[8]
Definition: Graphics3D.h:159
Vec4< float > color
void updateGamepadCommand(GamepadCommand &gamepadCommand)
void keyReleaseEvent(QKeyEvent *e) override
Definition: Graphics3D.cpp:289
void renderDrawlist()
Definition: Graphics3D.cpp:333
float * getNormalArray()
Definition: DrawList.h:114
size_t setupMiniCheetah(Vec4< float > color, bool useOld)
Definition: Graphics3D.cpp:122
static constexpr char vertexShaderSourceSolidColor[]
Definition: Graphics3D.cpp:38
GameController _gameController
Definition: Graphics3D.h:80
Vec3< float > corner_position
size_t addMiniCheetah(Vec4< float > color, bool useOld)
Definition: DrawList.cpp:129
SphereVisualization spheres[VISUALIZATION_MAX_ITEMS]
typename Eigen::Matrix< T, 3, 1 > Vec3
Definition: cppTypes.h:26
const Vec3< double > & getCameraOrigin()
Definition: DrawList.h:277
ConeVisualization cones[VISUALIZATION_MAX_ITEMS]
void _drawBlock(BlockVisualization &box)
Definition: Graphics3D.cpp:821
GLuint _colUniformSolidColor
Definition: Graphics3D.h:115
virtual ~Graphics3D()
Definition: Graphics3D.cpp:112
Vec4< float > color
bool _turbo
Definition: Graphics3D.h:143
QOpenGLShaderProgram * _solidColorProgram
Definition: Graphics3D.h:124
GLuint _normAttrColorArray
Definition: Graphics3D.h:112
Vec3< float > direction
GLuint _buffID[3]
Definition: Graphics3D.h:120
size_t setupCheetah3(Vec4< float > color, bool useOld)
Definition: Graphics3D.cpp:117
float _rx
Definition: Graphics3D.h:137
const std::vector< double > & getGCPos(size_t idx)
Definition: DrawList.h:265
float * getColorArray()
Definition: DrawList.h:121
BlockVisualization blocks[VISUALIZATION_MAX_ITEMS]
vectorAligned< SolidColor > _instanceColor
Definition: DrawList.h:278
void updateCameraMatrix()
Definition: Graphics3D.cpp:128
const Vec3< double > & getHeightMapLeftCorner()
Definition: DrawList.h:270
void wheelEvent(QWheelEvent *e) override
Definition: Graphics3D.cpp:230
void _Additional_Drawing(int pass)
Definition: Graphics3D.cpp:679
QOpenGLShaderProgram * _colorArrayProgram
Definition: Graphics3D.h:123
void configOpenGLPass(int pass)
Definition: Graphics3D.cpp:437
static constexpr char vertexShaderSourceColorArray[]
Definition: Graphics3D.cpp:19
void _translate(const Vec3< float > &position)
Definition: Graphics3D.h:101
void _DrawContactForce()
Definition: Graphics3D.cpp:722
void _rotateZtoDirection(const Vec3< float > &direction)
Definition: Graphics3D.cpp:764
float _pixel_to_rad
Definition: Graphics3D.h:139
GLuint _colAttrColorArray
Definition: Graphics3D.h:110
size_t getSizeOfAllData()
Definition: DrawList.h:116
size_t getNumObjectsToDraw()
Definition: DrawList.h:85
QMatrix4x4 & getModelKinematicTransform(size_t i)
Definition: DrawList.h:134
const double & getHeightMapMax()
Definition: DrawList.h:273
GLuint _colAttrSolidColor
Definition: Graphics3D.h:116
GamepadCommand _driverCommand
Definition: Graphics3D.h:81
float * getVertexArray()
Definition: DrawList.h:108
const size_t & getTotalNumGC()
Definition: DrawList.h:264
void mousePressEvent(QMouseEvent *event) override
Definition: Graphics3D.cpp:210
typename Eigen::Matrix< T, 4, 1 > Vec4
Definition: cppTypes.h:30
static constexpr auto clearColor
Definition: Graphics3D.cpp:16
void _MeshObstacleDrawing()
Definition: Graphics3D.cpp:519
bool _pause
Definition: Graphics3D.h:166
static constexpr float windows2000[]
Definition: Colors.h:10
void setAnimating(bool animating)
Definition: Graphics3D.cpp:331
QMatrix4x4 _cameraMatrix
Definition: Graphics3D.h:145
bool _orbiting
Definition: Graphics3D.h:132
void mouseReleaseEvent(QMouseEvent *event) override
Definition: Graphics3D.cpp:224
T deg2rad(T deg)
size_t getGLDrawArraySize(size_t i)
Definition: DrawList.h:100
void _drawArrow(ArrowVisualization &arrow)
Definition: Graphics3D.cpp:835
qint64 last_frame_ms
Definition: Graphics3D.h:129
PathVisualization paths[VISUALIZATION_MAX_PATHS]
bool needsReload()
Definition: DrawList.h:153
float _frameTime
Definition: Graphics3D.h:152
void paintGL() override
Definition: Graphics3D.cpp:477
Vec3< double > _cameraTarget
Definition: Graphics3D.h:147
float _freeCamPos[3]
Definition: Graphics3D.h:151
float _ry
Definition: Graphics3D.h:138
void _DrawBox(double depth, double width, double height)
Definition: Graphics3D.cpp:629
Vec3< float > base_position
Vec3< float > direction
Vec3< float > position[VISUALIZATION_MAX_PATH_POINTS]
const std::vector< BoxInfo > & getBoxInfoList()
Definition: DrawList.h:267
size_t getGLDrawArrayOffset(size_t i)
Definition: DrawList.h:93
bool _rotOrig
Definition: Graphics3D.h:142
void _BoxObstacleDrawing()
Definition: Graphics3D.cpp:611
Vec3< float > dimension
GLuint _matrixUniformSolidColor
Definition: Graphics3D.h:117
const double & getGridSize()
Definition: DrawList.h:275
void _setColor(const Vec4< float > &color)
Definition: Graphics3D.h:98
float _r[8]
Definition: Graphics3D.h:158
void _drawSphere(SphereVisualization &sphere)
Definition: Graphics3D.cpp:799
float _rx_base
Definition: Graphics3D.h:135
float _b[8]
Definition: Graphics3D.h:160
void _SetRGBHeight(const double &h, const double &step, const int &idx, float &r, float &g, float &b)
Definition: Graphics3D.cpp:603
QMatrix4x4 & getModelBaseTransform(size_t i)
Definition: DrawList.h:128
int _orbiting_y_start
Definition: Graphics3D.h:134
float _targetSpeed
Definition: Graphics3D.h:156
GLuint _posAttrColorArray
Definition: Graphics3D.h:109
Vec3< float > position
GLuint _posAttrSolidColor
Definition: Graphics3D.h:114
GLuint _normAttrSolidColor
Definition: Graphics3D.h:118
typename Eigen::Matrix< T, 3, 3 > RotMat
Definition: cppTypes.h:18
void _drawCone(ConeVisualization &cone)
Definition: Graphics3D.cpp:808
float _freeCamMove[3]
Definition: Graphics3D.h:150
const std::vector< double > & getGCForce(size_t idx)
Definition: DrawList.h:266
const DMat< double > & getHeightMap()
Definition: DrawList.h:269
Graphics3D(QWidget *parent=0)
Definition: Graphics3D.cpp:77
DrawList _drawList
Definition: Graphics3D.h:49
float _zoom
Definition: Graphics3D.h:140
void mouseMoveEvent(QMouseEvent *event) override
Definition: Graphics3D.cpp:218
const double & getHeightMapMin()
Definition: DrawList.h:274
Visualizer window for simulator.
bool _animating
Definition: Graphics3D.h:106
MX f(const MX &x, const MX &u)
char infoString[200]
Definition: Graphics3D.h:50
double _fps
Definition: Graphics3D.h:48
GLuint _matrixUniformColorArray
Definition: Graphics3D.h:111
float _color1[3]
Definition: Graphics3D.h:75