| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#define GL_GLEXT_PROTOTYPES |
| 5 |
|
| 6 |
#include "openglwidget.h" |
| 7 |
#include <QtWidgets/private/qwidget_p.h> |
| 8 |
#include <QOpenGLFramebufferObject> |
| 9 |
#include <QWindow> |
| 10 |
#include <qpa/qplatformwindow.h> |
| 11 |
#include <QDebug> |
| 12 |
#include <QTimer> |
| 13 |
|
| 14 |
#include <QtGui/QOpenGLFunctions> |
| 15 |
#include <QtGui/QGuiApplication> |
| 16 |
#include <QtGui/QMatrix4x4> |
| 17 |
#include <QOpenGLShaderProgram> |
| 18 |
#include <QtGui/QScreen> |
| 19 |
|
| 20 |
#include <QtCore/qmath.h> |
| 21 |
#include <qopengl.h> |
| 22 |
|
| 23 |
class OpenGLWidgetPrivate : protected QOpenGLFunctions |
| 24 |
{ |
| 25 |
public: |
| 26 |
OpenGLWidgetPrivate(QWidget *q) |
| 27 |
: m_program(0), m_frame(0), q(q) |
| 28 |
{ |
| 29 |
|
| 30 |
} |
| 31 |
|
| 32 |
void initialize(); |
| 33 |
void render(); |
| 34 |
|
| 35 |
|
| 36 |
int width() {return w;} |
| 37 |
int height() {return h;} |
| 38 |
|
| 39 |
GLuint m_posAttr; |
| 40 |
GLuint m_colAttr; |
| 41 |
GLuint m_matrixUniform; |
| 42 |
|
| 43 |
QOpenGLShaderProgram *m_program; |
| 44 |
int m_frame; |
| 45 |
|
| 46 |
int w,h; |
| 47 |
QWidget *q; |
| 48 |
|
| 49 |
int m_interval; |
| 50 |
QVector3D m_rotAxis; |
| 51 |
|
| 52 |
float clearColor[3]; |
| 53 |
}; |
| 54 |
|
| 55 |
|
| 56 |
OpenGLWidget::OpenGLWidget(int interval, const QVector3D &rotAxis, QWidget *parent) |
| 57 |
: QOpenGLWidget(parent) |
| 58 |
{ |
| 59 |
d.reset(new OpenGLWidgetPrivate(this)); |
| 60 |
d->clearColor[0] = d->clearColor[1] = d->clearColor[2] = 0.0f; |
| 61 |
d->m_interval = interval; |
| 62 |
d->m_rotAxis = rotAxis; |
| 63 |
if (interval > 0) { |
| 64 |
QTimer *timer = new QTimer(this); |
| 65 |
connect(timer, SIGNAL(timeout()), this, SLOT(update())); |
| 66 |
timer->start(interval); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
OpenGLWidget::~OpenGLWidget() |
| 71 |
{ |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
void OpenGLWidget::initializeGL() |
| 76 |
{ |
| 77 |
// qDebug("*initializeGL*"); |
| 78 |
d->initialize(); |
| 79 |
} |
| 80 |
|
| 81 |
void OpenGLWidget::resizeGL(int w, int h) |
| 82 |
{ |
| 83 |
// qDebug("*resizeGL*"); |
| 84 |
d->w = w; |
| 85 |
d->h = h; |
| 86 |
} |
| 87 |
void OpenGLWidget::paintGL() |
| 88 |
{ |
| 89 |
// qDebug("*paintGL* %d", d->m_frame); |
| 90 |
d->render(); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
static const char *vertexShaderSource = |
| 95 |
"attribute highp vec4 posAttr;\n" |
| 96 |
"attribute lowp vec4 colAttr;\n" |
| 97 |
"varying lowp vec4 col;\n" |
| 98 |
"uniform highp mat4 matrix;\n" |
| 99 |
"void main() {\n" |
| 100 |
" col = colAttr;\n" |
| 101 |
" gl_Position = matrix * posAttr;\n" |
| 102 |
"}\n"; |
| 103 |
|
| 104 |
static const char *fragmentShaderSource = |
| 105 |
"varying lowp vec4 col;\n" |
| 106 |
"void main() {\n" |
| 107 |
" gl_FragColor = col;\n" |
| 108 |
"}\n"; |
| 109 |
|
| 110 |
void OpenGLWidgetPrivate::initialize() |
| 111 |
{ |
| 112 |
initializeOpenGLFunctions(); |
| 113 |
m_program = new QOpenGLShaderProgram; |
| 114 |
m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource); |
| 115 |
m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource); |
| 116 |
m_program->link(); |
| 117 |
m_posAttr = m_program->attributeLocation("posAttr"); |
| 118 |
m_colAttr = m_program->attributeLocation("colAttr"); |
| 119 |
m_matrixUniform = m_program->uniformLocation("matrix"); |
| 120 |
} |
| 121 |
|
| 122 |
void OpenGLWidgetPrivate::render() |
| 123 |
{ |
| 124 |
const qreal retinaScale = q->devicePixelRatio(); |
| 125 |
glViewport(0, 0, width() * retinaScale, height() * retinaScale); |
| 126 |
|
| 127 |
glClearColor(clearColor[0], clearColor[1], clearColor[2], 1.0f); |
| 128 |
glClear(GL_COLOR_BUFFER_BIT); |
| 129 |
|
| 130 |
m_program->bind(); |
| 131 |
|
| 132 |
QMatrix4x4 matrix; |
| 133 |
matrix.perspective(60.0f, 4.0f/3.0f, 0.1f, 100.0f); |
| 134 |
matrix.translate(0, 0, -2); |
| 135 |
const qreal angle = 100.0f * m_frame / 30; |
| 136 |
matrix.rotate(angle, m_rotAxis); |
| 137 |
|
| 138 |
m_program->setUniformValue(m_matrixUniform, matrix); |
| 139 |
|
| 140 |
GLfloat vertices[] = { |
| 141 |
0.0f, 0.707f, |
| 142 |
-0.5f, -0.5f, |
| 143 |
0.5f, -0.5f |
| 144 |
}; |
| 145 |
|
| 146 |
GLfloat colors[] = { |
| 147 |
1.0f, 0.0f, 0.0f, |
| 148 |
0.0f, 1.0f, 0.0f, |
| 149 |
0.0f, 0.0f, 1.0f |
| 150 |
}; |
| 151 |
|
| 152 |
glVertexAttribPointer(m_posAttr, 2, GL_FLOAT, GL_FALSE, 0, vertices); |
| 153 |
glVertexAttribPointer(m_colAttr, 3, GL_FLOAT, GL_FALSE, 0, colors); |
| 154 |
|
| 155 |
glEnableVertexAttribArray(0); |
| 156 |
glEnableVertexAttribArray(1); |
| 157 |
|
| 158 |
glDrawArrays(GL_TRIANGLES, 0, 3); |
| 159 |
|
| 160 |
glDisableVertexAttribArray(1); |
| 161 |
glDisableVertexAttribArray(0); |
| 162 |
|
| 163 |
m_program->release(); |
| 164 |
|
| 165 |
++m_frame; |
| 166 |
|
| 167 |
if (m_interval <= 0) |
| 168 |
q->update(); |
| 169 |
} |
| 170 |
|
| 171 |
void OpenGLWidget::setClearColor(const float *c) |
| 172 |
{ |
| 173 |
d->clearColor[0] = c[0]; |
| 174 |
d->clearColor[1] = c[1]; |
| 175 |
d->clearColor[2] = c[2]; |
| 176 |
} |