Debian Codegraph @

tests/manual/examples/opengl/contextinfo/renderwindow.cpp

raw
1 // Copyright (C) 2016 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4 #include "renderwindow.h"
5 #include <QTimer>
6 #include <QMatrix4x4>
7 #include <QOpenGLContext>
8 #include <QtOpenGL/QOpenGLShaderProgram>
9 #include <QOpenGLFunctions>
10
11 RenderWindow::RenderWindow(const QSurfaceFormat &format)
12 : m_context(nullptr),
13 m_initialized(false),
14 m_forceGLSL110(false),
15 m_angle(0.0f)
16 {
17 setSurfaceType(QWindow::OpenGLSurface);
18 setFormat(format);
19 m_context = new QOpenGLContext(this);
20 m_context->setFormat(requestedFormat());
21 if (!m_context->create()) {
22 delete m_context;
23 m_context = nullptr;
24 }
25 }
26
27 void RenderWindow::exposeEvent(QExposeEvent *)
28 {
29 if (isExposed())
30 render();
31 }
32
33 // ES needs the precision qualifiers.
34 // On desktop GL QOpenGLShaderProgram inserts dummy defines for highp/mediump/lowp.
35 static const char *vertexShaderSource110 =
36 "attribute highp vec4 posAttr;\n"
37 "attribute lowp vec4 colAttr;\n"
38 "varying lowp vec4 col;\n"
39 "uniform highp mat4 matrix;\n"
40 "void main() {\n"
41 " col = colAttr;\n"
42 " gl_Position = matrix * posAttr;\n"
43 "}\n";
44
45 static const char *fragmentShaderSource110 =
46 "varying lowp vec4 col;\n"
47 "void main() {\n"
48 " gl_FragColor = col;\n"
49 "}\n";
50
51 static const char *vertexShaderSource =
52 "#version 150\n"
53 "in vec4 posAttr;\n"
54 "in vec4 colAttr;\n"
55 "out vec4 col;\n"
56 "uniform mat4 matrix;\n"
57 "void main() {\n"
58 " col = colAttr;\n"
59 " gl_Position = matrix * posAttr;\n"
60 "}\n";
61
62 static const char *fragmentShaderSource =
63 "#version 150\n"
64 "in vec4 col;\n"
65 "out vec4 fragColor;\n"
66 "void main() {\n"
67 " fragColor = col;\n"
68 "}\n";
69
70 static GLfloat vertices[] = {
71 0.0f, 0.707f,
72 -0.5f, -0.5f,
73 0.5f, -0.5f
74 };
75
76 static GLfloat colors[] = {
77 1.0f, 0.0f, 0.0f,
78 0.0f, 1.0f, 0.0f,
79 0.0f, 0.0f, 1.0f
80 };
81
82 void RenderWindow::init()
83 {
84 m_program = new QOpenGLShaderProgram(this);
85
86 QSurfaceFormat format = m_context->format();
87 bool useNewStyleShader = format.profile() == QSurfaceFormat::CoreProfile;
88 // Try to handle 3.0 & 3.1 that do not have the core/compatibility profile concept 3.2+ has.
89 // This may still fail since version 150 (3.2) is specified in the sources but it's worth a try.
90 if (format.renderableType() == QSurfaceFormat::OpenGL && format.majorVersion() == 3 && format.minorVersion() <= 1)
91 useNewStyleShader = !format.testOption(QSurfaceFormat::DeprecatedFunctions);
92 if (m_forceGLSL110)
93 useNewStyleShader = false;
94
95 const char *vsrc = useNewStyleShader ? vertexShaderSource : vertexShaderSource110;
96 const char *fsrc = useNewStyleShader ? fragmentShaderSource : fragmentShaderSource110;
97 qDebug("Using version %s shader", useNewStyleShader ? "150" : "110");
98
99 if (!m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vsrc)) {
100 emit error(m_program->log());
101 return;
102 }
103 if (!m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fsrc)) {
104 emit error(m_program->log());
105 return;
106 }
107 if (!m_program->link()) {
108 emit error(m_program->log());
109 return;
110 }
111
112 m_posAttr = m_program->attributeLocation("posAttr");
113 m_colAttr = m_program->attributeLocation("colAttr");
114 m_matrixUniform = m_program->uniformLocation("matrix");
115
116 m_vbo.create();
117 m_vbo.bind();
118 m_vbo.allocate(vertices, sizeof(vertices) + sizeof(colors));
119 m_vbo.write(sizeof(vertices), colors, sizeof(colors));
120 m_vbo.release();
121
122 QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao);
123 if (m_vao.isCreated()) // have VAO support, use it
124 setupVertexAttribs();
125 }
126
127 void RenderWindow::setupVertexAttribs()
128 {
129 m_vbo.bind();
130 m_program->setAttributeBuffer(m_posAttr, GL_FLOAT, 0, 2);
131 m_program->setAttributeBuffer(m_colAttr, GL_FLOAT, sizeof(vertices), 3);
132 m_program->enableAttributeArray(m_posAttr);
133 m_program->enableAttributeArray(m_colAttr);
134 m_vbo.release();
135 }
136
137 bool RenderWindow::event(QEvent *ev)
138 {
139 if (ev->type() == QEvent::UpdateRequest)
140 render();
141 return QWindow::event(ev);
142 }
143
144 void RenderWindow::render()
145 {
146 if (!m_context->makeCurrent(this)) {
147 emit error(tr("makeCurrent() failed"));
148 return;
149 }
150
151 QOpenGLFunctions *f = m_context->functions();
152 if (!m_initialized) {
153 m_initialized = true;
154 f->glEnable(GL_DEPTH_TEST);
155 f->glClearColor(0, 0, 0, 1);
156 init();
157 emit ready();
158 }
159
160 if (!m_vbo.isCreated()) // init() failed, don't bother with trying to render
161 return;
162
163 const qreal retinaScale = devicePixelRatio();
164 f->glViewport(0, 0, width() * retinaScale, height() * retinaScale);
165 f->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
166
167 m_program->bind();
168 QMatrix4x4 matrix;
169 matrix.perspective(60.0f, 4.0f / 3.0f, 0.1f, 100.0f);
170 matrix.translate(0.0f, 0.0f, -2.0f);
171 matrix.rotate(m_angle, 0.0f, 1.0f, 0.0f);
172 m_program->setUniformValue(m_matrixUniform, matrix);
173
174 if (m_vao.isCreated())
175 m_vao.bind();
176 else // no VAO support, set the vertex attribute arrays now
177 setupVertexAttribs();
178
179 f->glDrawArrays(GL_TRIANGLES, 0, 3);
180
181 m_vao.release();
182 m_program->release();
183
184 // swapInterval is 1 by default which means that swapBuffers() will (hopefully) block
185 // and wait for vsync.
186 m_context->swapBuffers(this);
187
188 m_angle += 1.0f;
189
190 requestUpdate();
191 }