| 1 |
// Copyright (C) 2022 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include "mygraphicsview.h" |
| 5 |
|
| 6 |
#include <QResizeEvent> |
| 7 |
#include <QFileDialog> |
| 8 |
|
| 9 |
#include <QtGui/qquaternion.h> |
| 10 |
|
| 11 |
Q_OPENGL_EXPORT QImage qt_gl_read_framebuffer(const QSize &size, bool alpha_format, bool include_alpha); |
| 12 |
|
| 13 |
MyGraphicsView::MyGraphicsView(QWidget *parent) : |
| 14 |
QGraphicsView(parent), |
| 15 |
m_indexBuf(QOpenGLBuffer::IndexBuffer) |
| 16 |
{ |
| 17 |
|
| 18 |
} |
| 19 |
|
| 20 |
MyGraphicsView::MyGraphicsView(QGraphicsScene *scene, QWidget *parent) : |
| 21 |
QGraphicsView(scene, parent) |
| 22 |
{ |
| 23 |
|
| 24 |
} |
| 25 |
|
| 26 |
void MyGraphicsView::saveImage(QOpenGLWidget::TargetBuffer targetBuffer) |
| 27 |
{ |
| 28 |
QOpenGLWidget *w = static_cast<QOpenGLWidget*>(viewport()); |
| 29 |
Q_ASSERT(w); |
| 30 |
|
| 31 |
w->makeCurrent(targetBuffer); |
| 32 |
draw(targetBuffer); |
| 33 |
|
| 34 |
QImage img = qt_gl_read_framebuffer(w->size() * w->devicePixelRatio(), true, true); |
| 35 |
|
| 36 |
if (img.isNull()) { |
| 37 |
qFatal("Failed to grab framebuffer"); |
| 38 |
} |
| 39 |
|
| 40 |
const char *fn = |
| 41 |
targetBuffer == QOpenGLWidget::LeftBuffer |
| 42 |
? "leftBuffer.png" : "rightBuffer.png"; |
| 43 |
|
| 44 |
QFileDialog fd(this); |
| 45 |
fd.setAcceptMode(QFileDialog::AcceptSave); |
| 46 |
fd.setDefaultSuffix("png"); |
| 47 |
fd.selectFile(fn); |
| 48 |
if (fd.exec() == QDialog::Accepted) |
| 49 |
img.save(fd.selectedFiles().first()); |
| 50 |
} |
| 51 |
|
| 52 |
void MyGraphicsView::drawBackground(QPainter *painter, const QRectF &rect) |
| 53 |
{ |
| 54 |
if (!m_initialized) |
| 55 |
init(); |
| 56 |
|
| 57 |
QOpenGLWidget *w = static_cast<QOpenGLWidget*>(viewport()); |
| 58 |
|
| 59 |
painter->beginNativePainting(); |
| 60 |
|
| 61 |
draw(w->currentTargetBuffer()); |
| 62 |
|
| 63 |
painter->endNativePainting(); |
| 64 |
|
| 65 |
|
| 66 |
m_yaw += 0.5; |
| 67 |
if (m_yaw > 360) |
| 68 |
m_yaw = 0; |
| 69 |
} |
| 70 |
|
| 71 |
void MyGraphicsView::resizeEvent(QResizeEvent *event) |
| 72 |
{ |
| 73 |
QGraphicsView::resizeEvent(event); |
| 74 |
|
| 75 |
resize(event->size().width(), event->size().height()); |
| 76 |
} |
| 77 |
|
| 78 |
void MyGraphicsView::init() |
| 79 |
{ |
| 80 |
if (m_initialized) |
| 81 |
return; |
| 82 |
|
| 83 |
initializeOpenGLFunctions(); |
| 84 |
|
| 85 |
initShaders(); |
| 86 |
initCube(); |
| 87 |
|
| 88 |
resize(viewport()->width(), viewport()->height()); |
| 89 |
|
| 90 |
m_initialized = true; |
| 91 |
} |
| 92 |
|
| 93 |
void MyGraphicsView::initShaders() |
| 94 |
{ |
| 95 |
static const char *vertexShaderSource = |
| 96 |
"#ifdef GL_ES\n" |
| 97 |
"// Set default precision to medium\n" |
| 98 |
"precision mediump int;\n" |
| 99 |
"precision mediump float;\n" |
| 100 |
"#endif\n" |
| 101 |
"attribute vec4 a_position;\n" |
| 102 |
"uniform mat4 u_mvp;\n" |
| 103 |
"void main() {\n" |
| 104 |
"gl_Position = u_mvp * a_position;\n" |
| 105 |
"}\n"; |
| 106 |
|
| 107 |
static const char *fragmentShaderSource = |
| 108 |
"#ifdef GL_ES\n" |
| 109 |
"// Set default precision to medium\n" |
| 110 |
"precision mediump int;\n" |
| 111 |
"precision mediump float;\n" |
| 112 |
"#endif\n" |
| 113 |
"void main() {\n" |
| 114 |
"gl_FragColor = vec4(0.7, 0.1, 0.0, 1.0);\n" |
| 115 |
"}\n"; |
| 116 |
|
| 117 |
if (!m_program.addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource)) |
| 118 |
close(); |
| 119 |
|
| 120 |
if (!m_program.addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource)) |
| 121 |
close(); |
| 122 |
|
| 123 |
if (!m_program.link()) |
| 124 |
close(); |
| 125 |
|
| 126 |
if (!m_program.bind()) |
| 127 |
close(); |
| 128 |
} |
| 129 |
|
| 130 |
void MyGraphicsView::initCube() |
| 131 |
{ |
| 132 |
m_arrayBuf.create(); |
| 133 |
m_indexBuf.create(); |
| 134 |
|
| 135 |
QVector3D vertices[] = { |
| 136 |
QVector3D(-1.0f, -1.0f, 1.0f), |
| 137 |
QVector3D( 1.0f, -1.0f, 1.0f), |
| 138 |
QVector3D(-1.0f, 1.0f, 1.0f), |
| 139 |
QVector3D( 1.0f, 1.0f, 1.0f), |
| 140 |
QVector3D( 1.0f, -1.0f, 1.0f), |
| 141 |
QVector3D( 1.0f, -1.0f, -1.0f), |
| 142 |
QVector3D( 1.0f, 1.0f, 1.0f), |
| 143 |
QVector3D( 1.0f, 1.0f, -1.0f), |
| 144 |
QVector3D( 1.0f, -1.0f, -1.0f), |
| 145 |
QVector3D(-1.0f, -1.0f, -1.0f), |
| 146 |
QVector3D( 1.0f, 1.0f, -1.0f), |
| 147 |
QVector3D(-1.0f, 1.0f, -1.0f), |
| 148 |
QVector3D(-1.0f, -1.0f, -1.0f), |
| 149 |
QVector3D(-1.0f, -1.0f, 1.0f), |
| 150 |
QVector3D(-1.0f, 1.0f, -1.0f), |
| 151 |
QVector3D(-1.0f, 1.0f, 1.0f), |
| 152 |
QVector3D(-1.0f, -1.0f, -1.0f), |
| 153 |
QVector3D( 1.0f, -1.0f, -1.0f), |
| 154 |
QVector3D(-1.0f, -1.0f, 1.0f), |
| 155 |
QVector3D( 1.0f, -1.0f, 1.0f), |
| 156 |
QVector3D(-1.0f, 1.0f, 1.0f), |
| 157 |
QVector3D( 1.0f, 1.0f, 1.0f), |
| 158 |
QVector3D(-1.0f, 1.0f, -1.0f), |
| 159 |
QVector3D( 1.0f, 1.0f, -1.0f) |
| 160 |
}; |
| 161 |
|
| 162 |
GLushort indices[] = { |
| 163 |
0, 1, 2, 3, 3, |
| 164 |
4, 4, 5, 6, 7, 7, |
| 165 |
8, 8, 9, 10, 11, 11, |
| 166 |
12, 12, 13, 14, 15, 15, |
| 167 |
16, 16, 17, 18, 19, 19, |
| 168 |
20, 20, 21, 22, 23 |
| 169 |
}; |
| 170 |
|
| 171 |
|
| 172 |
m_arrayBuf.bind(); |
| 173 |
m_arrayBuf.allocate(vertices, 24 * sizeof(QVector3D)); |
| 174 |
|
| 175 |
m_indexBuf.bind(); |
| 176 |
m_indexBuf.allocate(indices, 34 * sizeof(GLushort)); |
| 177 |
} |
| 178 |
|
| 179 |
void MyGraphicsView::draw(QOpenGLWidget::TargetBuffer targetBuffer) |
| 180 |
{ |
| 181 |
glClearColor(0.0f, 0.7f, 0.0f, 1.0f); |
| 182 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 183 |
|
| 184 |
m_program.bind(); |
| 185 |
m_arrayBuf.bind(); |
| 186 |
m_indexBuf.bind(); |
| 187 |
|
| 188 |
QMatrix4x4 matrix; |
| 189 |
matrix.translate(targetBuffer == QOpenGLWidget::LeftBuffer ? -2.0 : 2.0, |
| 190 |
0.0, |
| 191 |
-10.0); |
| 192 |
matrix.scale(2.0); |
| 193 |
matrix.rotate(QQuaternion::fromEulerAngles(10, m_yaw, 0)); |
| 194 |
m_program.setUniformValue("u_mvp", m_projection * matrix); |
| 195 |
|
| 196 |
int vertexLocation = m_program.attributeLocation("a_position"); |
| 197 |
m_program.enableAttributeArray(vertexLocation); |
| 198 |
m_program.setAttributeBuffer(vertexLocation, GL_FLOAT, 0, 3, sizeof(QVector3D)); |
| 199 |
|
| 200 |
glDrawElements(GL_TRIANGLE_STRIP, 34, GL_UNSIGNED_SHORT, nullptr); |
| 201 |
} |
| 202 |
|
| 203 |
void MyGraphicsView::resize(int w, int h) |
| 204 |
{ |
| 205 |
qreal aspect = qreal(w) / qreal(h ? h : 1); |
| 206 |
const qreal zNear = 0.0, zFar = 100.0, fov = 45.0; |
| 207 |
m_projection.setToIdentity(); |
| 208 |
m_projection.perspective(fov, aspect, zNear, zFar); |
| 209 |
|
| 210 |
if (m_initialized) |
| 211 |
glViewport(0, 0, w, h); |
| 212 |
} |