Buran Motion Planning Framework
qt_gl_widget.h
1 #pragma once
2 
3 #include <QOpenGLWidget>
4 #include <QMouseEvent>
5 #include <QOpenGLFunctions>
6 #include <QOpenGLVertexArrayObject>
7 #include <QOpenGLBuffer>
8 #include <QMatrix4x4>
9 #include <QtWidgets/QSlider>
10 #include <QOpenGLShaderProgram>
11 
12 namespace bmpf {
13 
24  class QTGLWidget : public QOpenGLWidget, protected QOpenGLFunctions {
25  public:
26 
31  explicit QTGLWidget(QWidget *parent = nullptr);
32 
36  virtual void cleanup();
37 
42  QSize minimumSizeHint() const Q_DECL_OVERRIDE;
43 
48  QSize sizeHint() const Q_DECL_OVERRIDE;
49 
54  virtual void setXRotation(int angle) = 0;
55 
60  virtual void setYRotation(int angle) = 0;
61 
66  virtual void setZRotation(int angle) = 0;
67 
68  protected:
69 
73  virtual void init() = 0;
74 
78  virtual void paint() = 0;
79 
83  void initializeGL() Q_DECL_OVERRIDE;
84 
88  void paintGL() Q_DECL_OVERRIDE;
89 
94  void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
95 
100  void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
101 
105  int xAngle;
109  int yAngle;
113  int zAngle;
114 
122  bool _core;
123 
131  QMatrix4x4 _worldTransform;
135  QOpenGLShaderProgram *_program;
139  QWidget *_parent;
159  QMatrix4x4 _camera;
163  std::string _caption;
164  };
165 
166 
170  static const char *vertexShaderSourceCore =
171  "#version 150\n"
172  "in vec4 vertex;\n"
173  "in vec3 normal;\n"
174  "out vec3 vert;\n"
175  "out vec3 vertNormal;\n"
176  "uniform mat4 projMatrix;\n"
177  "uniform mat4 mvMatrix;\n"
178  "uniform mat3 normalMatrix;\n"
179  "void main() {\n"
180  " vert = vertex.xyz;\n"
181  " vertNormal = normalMatrix * normal;\n"
182  " gl_Position = projMatrix * mvMatrix * vertex;\n"
183  "}\n";
184 
188  static const char *fragmentShaderSourceCore =
189  "#version 150\n"
190  "in highp vec3 vert;\n"
191  "in highp vec3 vertNormal;\n"
192  "out highp vec4 fragColor;\n"
193  "uniform highp vec3 lightPos;\n"
194  "void main() {\n"
195  " highp vec3 L = normalize(lightPos - vert);\n"
196  " highp float NL = max(dot(normalize(vertNormal), L), 0.0);\n"
197  " highp vec3 color = vec3(0.39, 1.0, 0.0);\n"
198  " highp vec3 col = clamp(color * 0.2 + color * 0.8 * NL, 0.0, 1.0);\n"
199  " fragColor = vec4(col, 1.0);\n"
200  "}\n";
201 
205  static const char *vertexShaderSource =
206  "attribute vec4 vertex;\n"
207  "attribute vec3 normal;\n"
208  "varying vec3 vert;\n"
209  "varying vec3 vertNormal;\n"
210  "uniform mat4 projMatrix;\n"
211  "uniform mat4 mvMatrix;\n"
212  "uniform mat3 normalMatrix;\n"
213  "void main() {\n"
214  " vert = vertex.xyz;\n"
215  " vertNormal = normalMatrix * normal;\n"
216  " gl_Position = projMatrix * mvMatrix * vertex;\n"
217  "}\n";
218 
222  static const char *fragmentShaderSource =
223  "varying highp vec3 vert;\n"
224  "varying highp vec3 vertNormal;\n"
225  "uniform highp vec3 lightPos;\n"
226  "void main() {\n"
227  " highp vec3 L = normalize(lightPos - vert);\n"
228  " highp float NL = max(dot(normalize(vertNormal), L), 0.0);\n"
229  " highp vec3 color = vec3(0.39, 1.0, 0.0);\n"
230  " highp vec3 col = clamp(color * 0.2 + color * 0.8 * NL, 0.0, 1.0);\n"
231  " gl_FragColor = vec4(col, 1.0);\n"
232  "}\n";
233 
234 
239  static void qNormalizeAngle(int &angle) {
240  while (angle < 0)
241  angle += 360 * 16;
242  while (angle > 360 * 16)
243  angle -= 360 * 16;
244  }
245 
246 }
bmpf::QTGLWidget::yAngle
int yAngle
Definition: qt_gl_widget.h:109
bmpf::QTGLWidget::_program
QOpenGLShaderProgram * _program
Definition: qt_gl_widget.h:135
bmpf::QTGLWidget::cleanup
virtual void cleanup()
Definition: qt_gl_widget.cpp:30
bmpf::QTGLWidget::_core
bool _core
Definition: qt_gl_widget.h:122
bmpf::QTGLWidget::setYRotation
virtual void setYRotation(int angle)=0
bmpf::QTGLWidget::init
virtual void init()=0
bmpf::QTGLWidget::_lastMousePos
QPoint _lastMousePos
Definition: qt_gl_widget.h:127
bmpf::QTGLWidget::_mvMatrixLoc
int _mvMatrixLoc
Definition: qt_gl_widget.h:147
bmpf::QTGLWidget::_parent
QWidget * _parent
Definition: qt_gl_widget.h:139
bmpf::QTGLWidget::paint
virtual void paint()=0
bmpf::QTGLWidget::zAngle
int zAngle
Definition: qt_gl_widget.h:113
bmpf::QTGLWidget::setXRotation
virtual void setXRotation(int angle)=0
bmpf::QTGLWidget::_worldTransform
QMatrix4x4 _worldTransform
Definition: qt_gl_widget.h:131
bmpf::QTGLWidget::_caption
std::string _caption
Definition: qt_gl_widget.h:163
bmpf::QTGLWidget::_normalMatrixLoc
int _normalMatrixLoc
Definition: qt_gl_widget.h:151
bmpf::QTGLWidget::mouseMoveEvent
void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE
Definition: qt_gl_widget.cpp:133
bmpf::QTGLWidget::minimumSizeHint
QSize minimumSizeHint() const Q_DECL_OVERRIDE
Definition: qt_gl_widget.cpp:109
bmpf::QTGLWidget::_camera
QMatrix4x4 _camera
Definition: qt_gl_widget.h:159
bmpf::QTGLWidget::QTGLWidget
QTGLWidget(QWidget *parent=nullptr)
Definition: qt_gl_widget.cpp:11
bmpf::QTGLWidget::initializeGL
void initializeGL() Q_DECL_OVERRIDE
Definition: qt_gl_widget.cpp:40
bmpf::QTGLWidget::mousePressEvent
void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE
Definition: qt_gl_widget.cpp:125
bmpf::QTGLWidget
Базовый класс для QT виджетов и использованием OpenGL
Definition: qt_gl_widget.h:24
bmpf::QTGLWidget::_projMatrixLoc
int _projMatrixLoc
Definition: qt_gl_widget.h:143
bmpf::QTGLWidget::setZRotation
virtual void setZRotation(int angle)=0
bmpf::QTGLWidget::_isTransparent
bool _isTransparent
Definition: qt_gl_widget.h:118
bmpf::QTGLWidget::sizeHint
QSize sizeHint() const Q_DECL_OVERRIDE
Definition: qt_gl_widget.cpp:117
bmpf::QTGLWidget::_lightPosLoc
int _lightPosLoc
Definition: qt_gl_widget.h:155
bmpf::QTGLWidget::xAngle
int xAngle
Definition: qt_gl_widget.h:105
bmpf::QTGLWidget::paintGL
void paintGL() Q_DECL_OVERRIDE
Definition: qt_gl_widget.cpp:88