| 1 |
// Copyright (C) 2022 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause |
| 3 |
|
| 4 |
#include "glwidget.h" |
| 5 |
#include <QPainter> |
| 6 |
#include <QPaintEngine> |
| 7 |
#include <QOpenGLShaderProgram> |
| 8 |
#include <QOpenGLTexture> |
| 9 |
#include <QRandomGenerator> |
| 10 |
#include <QCoreApplication> |
| 11 |
#include <QFileDialog> |
| 12 |
#include <qmath.h> |
| 13 |
|
| 14 |
GLWidget::GLWidget(const QColor &background) |
| 15 |
: m_background(background) |
| 16 |
{ |
| 17 |
setMinimumSize(300, 250); |
| 18 |
} |
| 19 |
|
| 20 |
GLWidget::~GLWidget() |
| 21 |
{ |
| 22 |
reset(); |
| 23 |
} |
| 24 |
|
| 25 |
void GLWidget::saveImage(TargetBuffer targetBuffer) |
| 26 |
{ |
| 27 |
QImage img = grabFramebuffer(targetBuffer); |
| 28 |
if (img.isNull()) { |
| 29 |
qFatal("Failed to grab framebuffer"); |
| 30 |
} |
| 31 |
|
| 32 |
const char *fn = |
| 33 |
targetBuffer == TargetBuffer::LeftBuffer |
| 34 |
? "leftBuffer.png" : "rightBuffer.png"; |
| 35 |
|
| 36 |
QFileDialog fd(this); |
| 37 |
fd.setAcceptMode(QFileDialog::AcceptSave); |
| 38 |
fd.setDefaultSuffix("png"); |
| 39 |
fd.selectFile(fn); |
| 40 |
if (fd.exec() == QDialog::Accepted) |
| 41 |
img.save(fd.selectedFiles().first()); |
| 42 |
} |
| 43 |
|
| 44 |
void GLWidget::reset() |
| 45 |
{ |
| 46 |
// And now release all OpenGL resources. |
| 47 |
makeCurrent(); |
| 48 |
delete m_program; |
| 49 |
m_program = nullptr; |
| 50 |
delete m_vshader; |
| 51 |
m_vshader = nullptr; |
| 52 |
delete m_fshader; |
| 53 |
m_fshader = nullptr; |
| 54 |
m_vbo.destroy(); |
| 55 |
doneCurrent(); |
| 56 |
|
| 57 |
// We are done with the current QOpenGLContext, forget it. If there is a |
| 58 |
// subsequent initialize(), that will then connect to the new context. |
| 59 |
QObject::disconnect(m_contextWatchConnection); |
| 60 |
} |
| 61 |
void GLWidget::initializeGL() |
| 62 |
{ |
| 63 |
initializeOpenGLFunctions(); |
| 64 |
|
| 65 |
m_vshader = new QOpenGLShader(QOpenGLShader::Vertex); |
| 66 |
const char *vsrc1 = |
| 67 |
"attribute highp vec4 vertex;\n" |
| 68 |
"attribute mediump vec3 normal;\n" |
| 69 |
"uniform mediump mat4 matrix;\n" |
| 70 |
"varying mediump vec4 color;\n" |
| 71 |
"void main(void)\n" |
| 72 |
"{\n" |
| 73 |
" vec3 toLight = normalize(vec3(0.0, 0.3, 1.0));\n" |
| 74 |
" float angle = max(dot(normal, toLight), 0.0);\n" |
| 75 |
" vec3 col = vec3(0.40, 1.0, 0.0);\n" |
| 76 |
" color = vec4(col * 0.2 + col * 0.8 * angle, 1.0);\n" |
| 77 |
" color = clamp(color, 0.0, 1.0);\n" |
| 78 |
" gl_Position = matrix * vertex;\n" |
| 79 |
"}\n"; |
| 80 |
m_vshader->compileSourceCode(vsrc1); |
| 81 |
|
| 82 |
m_fshader = new QOpenGLShader(QOpenGLShader::Fragment); |
| 83 |
const char *fsrc1 = |
| 84 |
"varying mediump vec4 color;\n" |
| 85 |
"void main(void)\n" |
| 86 |
"{\n" |
| 87 |
" gl_FragColor = color;\n" |
| 88 |
"}\n"; |
| 89 |
m_fshader->compileSourceCode(fsrc1); |
| 90 |
|
| 91 |
m_program = new QOpenGLShaderProgram; |
| 92 |
m_program->addShader(m_vshader); |
| 93 |
m_program->addShader(m_fshader); |
| 94 |
m_program->link(); |
| 95 |
|
| 96 |
|
| 97 |
m_vertexAttr = m_program->attributeLocation("vertex"); |
| 98 |
m_normalAttr = m_program->attributeLocation("normal"); |
| 99 |
m_matrixUniform = m_program->uniformLocation("matrix"); |
| 100 |
|
| 101 |
createGeometry(); |
| 102 |
|
| 103 |
m_vbo.create(); |
| 104 |
m_vbo.bind(); |
| 105 |
const int vertexCount = m_vertices.count(); |
| 106 |
QList<GLfloat> buf; |
| 107 |
buf.resize(vertexCount * 3 * 2); |
| 108 |
GLfloat *p = buf.data(); |
| 109 |
for (int i = 0; i < vertexCount; ++i) { |
| 110 |
*p++ = m_vertices[i].x(); |
| 111 |
*p++ = m_vertices[i].y(); |
| 112 |
*p++ = m_vertices[i].z(); |
| 113 |
*p++ = m_normals[i].x(); |
| 114 |
*p++ = m_normals[i].y(); |
| 115 |
*p++ = m_normals[i].z(); |
| 116 |
} |
| 117 |
m_vbo.allocate(buf.constData(), (int)buf.count() * sizeof(GLfloat)); |
| 118 |
m_vbo.release(); |
| 119 |
|
| 120 |
m_contextWatchConnection = QObject::connect(context(), &QOpenGLContext::aboutToBeDestroyed, context(), [this] { reset(); }); |
| 121 |
|
| 122 |
glFrontFace(GL_CW); |
| 123 |
glCullFace(GL_FRONT); |
| 124 |
glEnable(GL_CULL_FACE); |
| 125 |
glEnable(GL_DEPTH_TEST); |
| 126 |
} |
| 127 |
|
| 128 |
void GLWidget::paintGL() |
| 129 |
{ |
| 130 |
// When QSurfaceFormat::StereoBuffers is enabled, this function is called twice. |
| 131 |
// Once where currentTargetBuffer() == QOpenGLWidget::LeftBuffer, |
| 132 |
// and once where currentTargetBuffer() == QOpenGLWidget::RightBuffer. |
| 133 |
|
| 134 |
glClearColor(m_background.redF(), m_background.greenF(), m_background.blueF(), 1.0f); |
| 135 |
|
| 136 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 137 |
|
| 138 |
//! [1] |
| 139 |
// Slightly translate the model, so that there's a visible difference in each buffer. |
| 140 |
QMatrix4x4 modelview; |
| 141 |
if (currentTargetBuffer() == QOpenGLWidget::LeftBuffer) |
| 142 |
modelview.translate(-0.4f, 0.0f, 0.0f); |
| 143 |
else if (currentTargetBuffer() == QOpenGLWidget::RightBuffer) |
| 144 |
modelview.translate(0.4f, 0.0f, 0.0f); |
| 145 |
//! [1] |
| 146 |
|
| 147 |
m_program->bind(); |
| 148 |
m_program->setUniformValue(m_matrixUniform, modelview); |
| 149 |
m_program->enableAttributeArray(m_vertexAttr); |
| 150 |
m_program->enableAttributeArray(m_normalAttr); |
| 151 |
|
| 152 |
m_vbo.bind(); |
| 153 |
m_program->setAttributeBuffer(m_vertexAttr, GL_FLOAT, 0, 3, 6 * sizeof(GLfloat)); |
| 154 |
m_program->setAttributeBuffer(m_normalAttr, GL_FLOAT, 3 * sizeof(GLfloat), 3, 6 * sizeof(GLfloat)); |
| 155 |
m_vbo.release(); |
| 156 |
|
| 157 |
glDrawArrays(GL_TRIANGLES, 0, m_vertices.size()); |
| 158 |
|
| 159 |
m_program->disableAttributeArray(m_normalAttr); |
| 160 |
m_program->disableAttributeArray(m_vertexAttr); |
| 161 |
m_program->release(); |
| 162 |
update(); |
| 163 |
} |
| 164 |
|
| 165 |
void GLWidget::createGeometry() |
| 166 |
{ |
| 167 |
m_vertices.clear(); |
| 168 |
m_normals.clear(); |
| 169 |
|
| 170 |
qreal x1 = +0.06f; |
| 171 |
qreal y1 = -0.14f; |
| 172 |
qreal x2 = +0.14f; |
| 173 |
qreal y2 = -0.06f; |
| 174 |
qreal x3 = +0.08f; |
| 175 |
qreal y3 = +0.00f; |
| 176 |
qreal x4 = +0.30f; |
| 177 |
qreal y4 = +0.22f; |
| 178 |
|
| 179 |
quad(x1, y1, x2, y2, y2, x2, y1, x1); |
| 180 |
quad(x3, y3, x4, y4, y4, x4, y3, x3); |
| 181 |
|
| 182 |
extrude(x1, y1, x2, y2); |
| 183 |
extrude(x2, y2, y2, x2); |
| 184 |
extrude(y2, x2, y1, x1); |
| 185 |
extrude(y1, x1, x1, y1); |
| 186 |
extrude(x3, y3, x4, y4); |
| 187 |
extrude(x4, y4, y4, x4); |
| 188 |
extrude(y4, x4, y3, x3); |
| 189 |
|
| 190 |
const int NumSectors = 100; |
| 191 |
const qreal sectorAngle = 2 * qreal(M_PI) / NumSectors; |
| 192 |
|
| 193 |
for (int i = 0; i < NumSectors; ++i) { |
| 194 |
qreal angle = i * sectorAngle; |
| 195 |
qreal x5 = 0.30 * sin(angle); |
| 196 |
qreal y5 = 0.30 * cos(angle); |
| 197 |
qreal x6 = 0.20 * sin(angle); |
| 198 |
qreal y6 = 0.20 * cos(angle); |
| 199 |
|
| 200 |
angle += sectorAngle; |
| 201 |
qreal x7 = 0.20 * sin(angle); |
| 202 |
qreal y7 = 0.20 * cos(angle); |
| 203 |
qreal x8 = 0.30 * sin(angle); |
| 204 |
qreal y8 = 0.30 * cos(angle); |
| 205 |
|
| 206 |
quad(x5, y5, x6, y6, x7, y7, x8, y8); |
| 207 |
|
| 208 |
extrude(x6, y6, x7, y7); |
| 209 |
extrude(x8, y8, x5, y5); |
| 210 |
} |
| 211 |
|
| 212 |
for (int i = 0;i < m_vertices.size();i++) |
| 213 |
m_vertices[i] *= 2.0f; |
| 214 |
} |
| 215 |
|
| 216 |
void GLWidget::quad(qreal x1, qreal y1, qreal x2, qreal y2, qreal x3, qreal y3, qreal x4, qreal y4) |
| 217 |
{ |
| 218 |
m_vertices << QVector3D(x1, y1, -0.05f); |
| 219 |
m_vertices << QVector3D(x2, y2, -0.05f); |
| 220 |
m_vertices << QVector3D(x4, y4, -0.05f); |
| 221 |
|
| 222 |
m_vertices << QVector3D(x3, y3, -0.05f); |
| 223 |
m_vertices << QVector3D(x4, y4, -0.05f); |
| 224 |
m_vertices << QVector3D(x2, y2, -0.05f); |
| 225 |
|
| 226 |
QVector3D n = QVector3D::normal |
| 227 |
(QVector3D(x2 - x1, y2 - y1, 0.0f), QVector3D(x4 - x1, y4 - y1, 0.0f)); |
| 228 |
|
| 229 |
m_normals << n; |
| 230 |
m_normals << n; |
| 231 |
m_normals << n; |
| 232 |
|
| 233 |
m_normals << n; |
| 234 |
m_normals << n; |
| 235 |
m_normals << n; |
| 236 |
|
| 237 |
m_vertices << QVector3D(x4, y4, 0.05f); |
| 238 |
m_vertices << QVector3D(x2, y2, 0.05f); |
| 239 |
m_vertices << QVector3D(x1, y1, 0.05f); |
| 240 |
|
| 241 |
m_vertices << QVector3D(x2, y2, 0.05f); |
| 242 |
m_vertices << QVector3D(x4, y4, 0.05f); |
| 243 |
m_vertices << QVector3D(x3, y3, 0.05f); |
| 244 |
|
| 245 |
n = QVector3D::normal |
| 246 |
(QVector3D(x2 - x4, y2 - y4, 0.0f), QVector3D(x1 - x4, y1 - y4, 0.0f)); |
| 247 |
|
| 248 |
m_normals << n; |
| 249 |
m_normals << n; |
| 250 |
m_normals << n; |
| 251 |
|
| 252 |
m_normals << n; |
| 253 |
m_normals << n; |
| 254 |
m_normals << n; |
| 255 |
} |
| 256 |
|
| 257 |
void GLWidget::extrude(qreal x1, qreal y1, qreal x2, qreal y2) |
| 258 |
{ |
| 259 |
m_vertices << QVector3D(x1, y1, +0.05f); |
| 260 |
m_vertices << QVector3D(x2, y2, +0.05f); |
| 261 |
m_vertices << QVector3D(x1, y1, -0.05f); |
| 262 |
|
| 263 |
m_vertices << QVector3D(x2, y2, -0.05f); |
| 264 |
m_vertices << QVector3D(x1, y1, -0.05f); |
| 265 |
m_vertices << QVector3D(x2, y2, +0.05f); |
| 266 |
|
| 267 |
QVector3D n = QVector3D::normal |
| 268 |
(QVector3D(x2 - x1, y2 - y1, 0.0f), QVector3D(0.0f, 0.0f, -0.1f)); |
| 269 |
|
| 270 |
m_normals << n; |
| 271 |
m_normals << n; |
| 272 |
m_normals << n; |
| 273 |
|
| 274 |
m_normals << n; |
| 275 |
m_normals << n; |
| 276 |
m_normals << n; |
| 277 |
} |