Cheetah Software  1.0
Graphics3D.h
Go to the documentation of this file.
1 
8 #ifndef PROJECT_GRAPHICS3D_H
9 #define PROJECT_GRAPHICS3D_H
10 
11 #include "DrawList.h"
13 #include "obj_loader.h"
14 
15 #include <QMatrix4x4>
16 #include <QOpenGLContext>
17 #include <QOpenGLFunctions>
18 #include <QOpenGLPaintDevice>
19 #include <QOpenGLShaderProgram>
20 #include <QOpenGLWidget>
21 #include <QPainter>
22 #include <QWindow>
24 
25 #include <QDateTime>
26 #include <QMouseEvent>
27 #include <QScreen>
28 #include <QWheelEvent>
29 
30 #include <mutex>
31 #include "GameController.h"
32 
33 class Graphics3D : public QOpenGLWidget, protected QOpenGLFunctions {
34  Q_OBJECT
35  public:
36  explicit Graphics3D(QWidget *parent = 0);
37  virtual ~Graphics3D();
38 
39  void setAnimating(bool animating);
40  size_t setupCheetah3(Vec4<float> color, bool useOld);
41  size_t setupMiniCheetah(Vec4<float> color, bool useOld);
42 
43  void lockGfxMutex() { _gfxMutex.lock(); }
44 
45  void unlockGfxMutex() { _gfxMutex.unlock(); }
46 
47  // set robot state
48  double _fps = 0;
50  char infoString[200] = "";
51 
53 
55 
56  bool IsPaused() { return _pause; }
57  bool wantTurbo() { return _turbo; }
58 
59  protected:
60  void initializeGL() override;
61  // void resizeGL(int w, int h) override;
62  void paintGL() override;
63  // bool event(QEvent *event) override;
64 
65  // void exposeEvent(QExposeEvent *event) override; ??
66 
67  // mouse callbacks for orbit and zoom
68  void mousePressEvent(QMouseEvent *event) override;
69  void mouseMoveEvent(QMouseEvent *event) override;
70  void mouseReleaseEvent(QMouseEvent *event) override;
71  void wheelEvent(QWheelEvent *e) override;
72  void keyReleaseEvent(QKeyEvent *e) override;
73  void keyPressEvent(QKeyEvent *event) override;
74 
75  float _color1[3] = {0.364784, 0.513401, 0.952230};
76  float _color2[3] = {0.553970, 0.477397, 0.628871};
77  float _color3[3] = {0.335223, 0.768230, 0.277775};
78 
79  private:
82 
83  std::mutex _gfxMutex;
84  void updateCameraMatrix();
85  void renderDrawlist();
86  void configOpenGLPass(int pass);
87  void _BoxObstacleDrawing();
88  void _MeshObstacleDrawing();
89  void _DrawBox(double depth, double width, double height);
90  void _Additional_Drawing(int pass);
91  void _DrawContactForce();
92  void _DrawContactPoint();
93  void _drawArrow(ArrowVisualization &arrow);
94  void _drawBlock(BlockVisualization &box);
95  void _drawSphere(SphereVisualization &sphere);
96  void _drawCone(ConeVisualization &cone);
97  void _rotateZtoDirection(const Vec3<float> &direction);
98  void _setColor(const Vec4<float> &color) {
99  glColor4f(color(0), color(1), color(2), color(3));
100  }
101  void _translate(const Vec3<float> &position) {
102  glTranslatef(position(0), position(1), position(2));
103  }
104  void _drawArrow(const Vec3<float> &base, const Vec3<float> &direction,
105  float lineWidth, float headWidth, float headLength);
107 
108  // attributes for shader program
109  GLuint _posAttrColorArray; // position of vertex
110  GLuint _colAttrColorArray; // color of vertex
111  GLuint _matrixUniformColorArray; // transformation matrix
112  GLuint _normAttrColorArray; // vertex normal
113 
114  GLuint _posAttrSolidColor; // position of vertex
115  GLuint _colUniformSolidColor; // color of vertex
117  GLuint _matrixUniformSolidColor; // transformation matrix
118  GLuint _normAttrSolidColor; // vertex normal
119 
120  GLuint _buffID[3];
121 
122  // shader programs
123  QOpenGLShaderProgram *_colorArrayProgram;
124  QOpenGLShaderProgram *_solidColorProgram;
125 
126  // frame count
127  int _frame;
128  // time of last frame
129  qint64 last_frame_ms = 0;
130 
131  // UI orbit/zoom variables
132  bool _orbiting = false;
135  float _rx_base = 0;
136  float _ry_base = 0;
137  float _rx = 0;
138  float _ry = -34;
139  float _pixel_to_rad = .3f;
140  float _zoom = 3.0;
141 
142  bool _rotOrig = true;
143  bool _turbo = false;
144 
145  QMatrix4x4 _cameraMatrix;
149 
150  float _freeCamMove[3] = {0, 0, 0};
151  float _freeCamPos[3] = {0.f, 0.f, 0.f};
152  float _frameTime = 1.f / 60.f;
153 
154  bool _arrowsPressed[4] = {false, false, false, false};
155 
156  float _targetSpeed = 2;
157 
158  float _r[8];
159  float _g[8];
160  float _b[8];
161 
162  void getHeightColor(const double &height, float &r, float &g, float &b);
163  void _SetRGBHeight(const double &h, const double &step, const int &idx,
164  float &r, float &g, float &b);
165 
166  bool _pause = false;
167 };
168 
169 #endif // PROJECT_GRAPHICS3D_H
void resetGameController()
Definition: Graphics3D.h:54
void keyPressEvent(QKeyEvent *event) override
Definition: Graphics3D.cpp:238
void initializeGL() override
Definition: Graphics3D.cpp:167
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 _g[8]
Definition: Graphics3D.h:159
void keyReleaseEvent(QKeyEvent *e) override
Definition: Graphics3D.cpp:289
void renderDrawlist()
Definition: Graphics3D.cpp:333
size_t setupMiniCheetah(Vec4< float > color, bool useOld)
Definition: Graphics3D.cpp:122
GameController _gameController
Definition: Graphics3D.h:80
typename Eigen::Matrix< T, 3, 1 > Vec3
Definition: cppTypes.h:26
void _drawBlock(BlockVisualization &box)
Definition: Graphics3D.cpp:821
GLuint _colUniformSolidColor
Definition: Graphics3D.h:115
virtual ~Graphics3D()
Definition: Graphics3D.cpp:112
bool _turbo
Definition: Graphics3D.h:143
QOpenGLShaderProgram * _solidColorProgram
Definition: Graphics3D.h:124
GLuint _normAttrColorArray
Definition: Graphics3D.h:112
bool IsPaused()
Definition: Graphics3D.h:56
std::mutex _gfxMutex
Definition: Graphics3D.h:83
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
void updateCameraMatrix()
Definition: Graphics3D.cpp:128
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
Data structure to store robot model to be drawn.
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
Code to read the Logitech F310 Game Controller Creates a DriverCommand object to be sent to the robot...
GLuint _colAttrSolidColor
Definition: Graphics3D.h:116
void findNewController()
GamepadCommand _driverCommand
Definition: Graphics3D.h:81
void mousePressEvent(QMouseEvent *event) override
Definition: Graphics3D.cpp:210
Vec3< float > _v0
Definition: Graphics3D.h:146
float _color2[3]
Definition: Graphics3D.h:76
typename Eigen::Matrix< T, 4, 1 > Vec4
Definition: cppTypes.h:30
void _MeshObstacleDrawing()
Definition: Graphics3D.cpp:519
bool _pause
Definition: Graphics3D.h:166
A simple first order filter.
void setAnimating(bool animating)
Definition: Graphics3D.cpp:331
QMatrix4x4 _cameraMatrix
Definition: Graphics3D.h:145
bool _orbiting
Definition: Graphics3D.h:132
GamepadCommand & getDriverCommand()
Definition: Graphics3D.h:52
void mouseReleaseEvent(QMouseEvent *event) override
Definition: Graphics3D.cpp:224
float _color3[3]
Definition: Graphics3D.h:77
void _drawArrow(ArrowVisualization &arrow)
Definition: Graphics3D.cpp:835
qint64 last_frame_ms
Definition: Graphics3D.h:129
float _frameTime
Definition: Graphics3D.h:152
Utility to load .obj files, containing 3D models of robots.
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
void lockGfxMutex()
Definition: Graphics3D.h:43
bool _rotOrig
Definition: Graphics3D.h:142
void _BoxObstacleDrawing()
Definition: Graphics3D.cpp:611
GLuint _matrixUniformSolidColor
Definition: Graphics3D.h:117
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
void unlockGfxMutex()
Definition: Graphics3D.h:45
int _orbiting_y_start
Definition: Graphics3D.h:134
float _targetSpeed
Definition: Graphics3D.h:156
GLuint _posAttrColorArray
Definition: Graphics3D.h:109
GLuint _posAttrSolidColor
Definition: Graphics3D.h:114
GLuint _normAttrSolidColor
Definition: Graphics3D.h:118
void _drawCone(ConeVisualization &cone)
Definition: Graphics3D.cpp:808
float _freeCamMove[3]
Definition: Graphics3D.h:150
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
bool _animating
Definition: Graphics3D.h:106
char infoString[200]
Definition: Graphics3D.h:50
GLuint _matrixUniformColorArray
Definition: Graphics3D.h:111
bool wantTurbo()
Definition: Graphics3D.h:57
double _fps
Definition: Graphics3D.h:48
float _color1[3]
Definition: Graphics3D.h:75