1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the documentation of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | #include "openglwindow.h" |
52 | |
53 | #include <QGuiApplication> |
54 | #include <QMatrix4x4> |
55 | #include <QOpenGLShaderProgram> |
56 | #include <QScreen> |
57 | #include <QtMath> |
58 | |
59 | |
60 | //! [1] |
61 | class TriangleWindow : public OpenGLWindow |
62 | { |
63 | public: |
64 | using OpenGLWindow::OpenGLWindow; |
65 | |
66 | void initialize() override; |
67 | void render() override; |
68 | |
69 | private: |
70 | GLint m_posAttr = 0; |
71 | GLint m_colAttr = 0; |
72 | GLint m_matrixUniform = 0; |
73 | |
74 | QOpenGLShaderProgram *m_program = nullptr; |
75 | int m_frame = 0; |
76 | }; |
77 | //! [1] |
78 | |
79 | //! [2] |
80 | int main(int argc, char **argv) |
81 | { |
82 | QGuiApplication app(argc, argv); |
83 | |
84 | QSurfaceFormat format; |
85 | format.setSamples(16); |
86 | |
87 | TriangleWindow window; |
88 | window.setFormat(format); |
89 | window.resize(640, 480); |
90 | window.show(); |
91 | |
92 | window.setAnimating(true); |
93 | |
94 | return app.exec(); |
95 | } |
96 | //! [2] |
97 | |
98 | |
99 | //! [3] |
100 | static const char *vertexShaderSource = |
101 | "attribute highp vec4 posAttr;\n" |
102 | "attribute lowp vec4 colAttr;\n" |
103 | "varying lowp vec4 col;\n" |
104 | "uniform highp mat4 matrix;\n" |
105 | "void main() {\n" |
106 | " col = colAttr;\n" |
107 | " gl_Position = matrix * posAttr;\n" |
108 | "}\n" ; |
109 | |
110 | static const char *fragmentShaderSource = |
111 | "varying lowp vec4 col;\n" |
112 | "void main() {\n" |
113 | " gl_FragColor = col;\n" |
114 | "}\n" ; |
115 | //! [3] |
116 | |
117 | //! [4] |
118 | void TriangleWindow::initialize() |
119 | { |
120 | m_program = new QOpenGLShaderProgram(this); |
121 | m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource); |
122 | m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource); |
123 | m_program->link(); |
124 | m_posAttr = m_program->attributeLocation("posAttr" ); |
125 | Q_ASSERT(m_posAttr != -1); |
126 | m_colAttr = m_program->attributeLocation("colAttr" ); |
127 | Q_ASSERT(m_colAttr != -1); |
128 | m_matrixUniform = m_program->uniformLocation("matrix" ); |
129 | Q_ASSERT(m_matrixUniform != -1); |
130 | } |
131 | //! [4] |
132 | |
133 | //! [5] |
134 | void TriangleWindow::render() |
135 | { |
136 | const qreal retinaScale = devicePixelRatio(); |
137 | glViewport(0, 0, width() * retinaScale, height() * retinaScale); |
138 | |
139 | glClear(GL_COLOR_BUFFER_BIT); |
140 | |
141 | m_program->bind(); |
142 | |
143 | QMatrix4x4 matrix; |
144 | matrix.perspective(60.0f, 4.0f / 3.0f, 0.1f, 100.0f); |
145 | matrix.translate(0, 0, -2); |
146 | matrix.rotate(100.0f * m_frame / screen()->refreshRate(), 0, 1, 0); |
147 | |
148 | m_program->setUniformValue(m_matrixUniform, matrix); |
149 | |
150 | static const GLfloat vertices[] = { |
151 | 0.0f, 0.707f, |
152 | -0.5f, -0.5f, |
153 | 0.5f, -0.5f |
154 | }; |
155 | |
156 | static const GLfloat colors[] = { |
157 | 1.0f, 0.0f, 0.0f, |
158 | 0.0f, 1.0f, 0.0f, |
159 | 0.0f, 0.0f, 1.0f |
160 | }; |
161 | |
162 | glVertexAttribPointer(m_posAttr, 2, GL_FLOAT, GL_FALSE, 0, vertices); |
163 | glVertexAttribPointer(m_colAttr, 3, GL_FLOAT, GL_FALSE, 0, colors); |
164 | |
165 | glEnableVertexAttribArray(m_posAttr); |
166 | glEnableVertexAttribArray(m_colAttr); |
167 | |
168 | glDrawArrays(GL_TRIANGLES, 0, 3); |
169 | |
170 | glDisableVertexAttribArray(m_colAttr); |
171 | glDisableVertexAttribArray(m_posAttr); |
172 | |
173 | m_program->release(); |
174 | |
175 | ++m_frame; |
176 | } |
177 | //! [5] |
178 | |