1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the examples 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 "logorenderer.h" |
52 | #include <QPainter> |
53 | #include <QPaintEngine> |
54 | #include <qmath.h> |
55 | |
56 | LogoRenderer::LogoRenderer() |
57 | { |
58 | } |
59 | |
60 | LogoRenderer::~LogoRenderer() |
61 | { |
62 | } |
63 | |
64 | |
65 | void LogoRenderer::paintQtLogo() |
66 | { |
67 | program1.enableAttributeArray(location: normalAttr1); |
68 | program1.enableAttributeArray(location: vertexAttr1); |
69 | program1.setAttributeArray(location: vertexAttr1, values: vertices.constData()); |
70 | program1.setAttributeArray(location: normalAttr1, values: normals.constData()); |
71 | glDrawArrays(GL_TRIANGLES, first: 0, count: vertices.size()); |
72 | program1.disableAttributeArray(location: normalAttr1); |
73 | program1.disableAttributeArray(location: vertexAttr1); |
74 | } |
75 | |
76 | |
77 | void LogoRenderer::initialize() |
78 | { |
79 | initializeOpenGLFunctions(); |
80 | |
81 | glClearColor(red: 0.1f, green: 0.1f, blue: 0.2f, alpha: 1.0f); |
82 | |
83 | const char *vsrc1 = |
84 | "attribute highp vec4 vertex;\n" |
85 | "attribute mediump vec3 normal;\n" |
86 | "uniform mediump mat4 matrix;\n" |
87 | "varying mediump vec4 color;\n" |
88 | "void main(void)\n" |
89 | "{\n" |
90 | " vec3 toLight = normalize(vec3(0.0, 0.3, 1.0));\n" |
91 | " float angle = max(dot(normal, toLight), 0.0);\n" |
92 | " vec3 col = vec3(0.40, 1.0, 0.0);\n" |
93 | " color = vec4(col * 0.2 + col * 0.8 * angle, 1.0);\n" |
94 | " color = clamp(color, 0.0, 1.0);\n" |
95 | " gl_Position = matrix * vertex;\n" |
96 | "}\n" ; |
97 | |
98 | const char *fsrc1 = |
99 | "varying mediump vec4 color;\n" |
100 | "void main(void)\n" |
101 | "{\n" |
102 | " gl_FragColor = color;\n" |
103 | "}\n" ; |
104 | |
105 | program1.addCacheableShaderFromSourceCode(type: QOpenGLShader::Vertex, source: vsrc1); |
106 | program1.addCacheableShaderFromSourceCode(type: QOpenGLShader::Fragment, source: fsrc1); |
107 | program1.link(); |
108 | |
109 | vertexAttr1 = program1.attributeLocation(name: "vertex" ); |
110 | normalAttr1 = program1.attributeLocation(name: "normal" ); |
111 | matrixUniform1 = program1.uniformLocation(name: "matrix" ); |
112 | |
113 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); |
114 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); |
115 | |
116 | m_fAngle = 0; |
117 | m_fScale = 1; |
118 | createGeometry(); |
119 | } |
120 | |
121 | void LogoRenderer::render() |
122 | { |
123 | glDepthMask(flag: true); |
124 | |
125 | glClearColor(red: 0.5f, green: 0.5f, blue: 0.7f, alpha: 1.0f); |
126 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
127 | |
128 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); |
129 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); |
130 | |
131 | glFrontFace(GL_CW); |
132 | glCullFace(GL_FRONT); |
133 | glEnable(GL_CULL_FACE); |
134 | glEnable(GL_DEPTH_TEST); |
135 | |
136 | QMatrix4x4 modelview; |
137 | modelview.rotate(angle: m_fAngle, x: 0.0f, y: 1.0f, z: 0.0f); |
138 | modelview.rotate(angle: m_fAngle, x: 1.0f, y: 0.0f, z: 0.0f); |
139 | modelview.rotate(angle: m_fAngle, x: 0.0f, y: 0.0f, z: 1.0f); |
140 | modelview.scale(factor: m_fScale); |
141 | modelview.translate(x: 0.0f, y: -0.2f, z: 0.0f); |
142 | |
143 | program1.bind(); |
144 | program1.setUniformValue(location: matrixUniform1, value: modelview); |
145 | paintQtLogo(); |
146 | program1.release(); |
147 | |
148 | glDisable(GL_DEPTH_TEST); |
149 | glDisable(GL_CULL_FACE); |
150 | |
151 | m_fAngle += 1.0f; |
152 | } |
153 | |
154 | void LogoRenderer::createGeometry() |
155 | { |
156 | vertices.clear(); |
157 | normals.clear(); |
158 | |
159 | qreal x1 = +0.06f; |
160 | qreal y1 = -0.14f; |
161 | qreal x2 = +0.14f; |
162 | qreal y2 = -0.06f; |
163 | qreal x3 = +0.08f; |
164 | qreal y3 = +0.00f; |
165 | qreal x4 = +0.30f; |
166 | qreal y4 = +0.22f; |
167 | |
168 | quad(x1, y1, x2, y2, x3: y2, y3: x2, x4: y1, y4: x1); |
169 | quad(x1: x3, y1: y3, x2: x4, y2: y4, x3: y4, y3: x4, x4: y3, y4: x3); |
170 | |
171 | extrude(x1, y1, x2, y2); |
172 | extrude(x1: x2, y1: y2, x2: y2, y2: x2); |
173 | extrude(x1: y2, y1: x2, x2: y1, y2: x1); |
174 | extrude(x1: y1, y1: x1, x2: x1, y2: y1); |
175 | extrude(x1: x3, y1: y3, x2: x4, y2: y4); |
176 | extrude(x1: x4, y1: y4, x2: y4, y2: x4); |
177 | extrude(x1: y4, y1: x4, x2: y3, y2: x3); |
178 | |
179 | const qreal Pi = M_PI; |
180 | const int NumSectors = 100; |
181 | |
182 | for (int i = 0; i < NumSectors; ++i) { |
183 | qreal angle1 = (i * 2 * Pi) / NumSectors; |
184 | qreal x5 = 0.30 * sin(x: angle1); |
185 | qreal y5 = 0.30 * cos(x: angle1); |
186 | qreal x6 = 0.20 * sin(x: angle1); |
187 | qreal y6 = 0.20 * cos(x: angle1); |
188 | |
189 | qreal angle2 = ((i + 1) * 2 * Pi) / NumSectors; |
190 | qreal x7 = 0.20 * sin(x: angle2); |
191 | qreal y7 = 0.20 * cos(x: angle2); |
192 | qreal x8 = 0.30 * sin(x: angle2); |
193 | qreal y8 = 0.30 * cos(x: angle2); |
194 | |
195 | quad(x1: x5, y1: y5, x2: x6, y2: y6, x3: x7, y3: y7, x4: x8, y4: y8); |
196 | |
197 | extrude(x1: x6, y1: y6, x2: x7, y2: y7); |
198 | extrude(x1: x8, y1: y8, x2: x5, y2: y5); |
199 | } |
200 | |
201 | for (int i = 0;i < vertices.size();i++) |
202 | vertices[i] *= 2.0f; |
203 | } |
204 | |
205 | void LogoRenderer::quad(qreal x1, qreal y1, qreal x2, qreal y2, qreal x3, qreal y3, qreal x4, qreal y4) |
206 | { |
207 | vertices << QVector3D(x1, y1, -0.05f); |
208 | vertices << QVector3D(x2, y2, -0.05f); |
209 | vertices << QVector3D(x4, y4, -0.05f); |
210 | |
211 | vertices << QVector3D(x3, y3, -0.05f); |
212 | vertices << QVector3D(x4, y4, -0.05f); |
213 | vertices << QVector3D(x2, y2, -0.05f); |
214 | |
215 | QVector3D n = QVector3D::normal |
216 | (v1: QVector3D(x2 - x1, y2 - y1, 0.0f), v2: QVector3D(x4 - x1, y4 - y1, 0.0f)); |
217 | |
218 | normals << n; |
219 | normals << n; |
220 | normals << n; |
221 | |
222 | normals << n; |
223 | normals << n; |
224 | normals << n; |
225 | |
226 | vertices << QVector3D(x4, y4, 0.05f); |
227 | vertices << QVector3D(x2, y2, 0.05f); |
228 | vertices << QVector3D(x1, y1, 0.05f); |
229 | |
230 | vertices << QVector3D(x2, y2, 0.05f); |
231 | vertices << QVector3D(x4, y4, 0.05f); |
232 | vertices << QVector3D(x3, y3, 0.05f); |
233 | |
234 | n = QVector3D::normal |
235 | (v1: QVector3D(x2 - x4, y2 - y4, 0.0f), v2: QVector3D(x1 - x4, y1 - y4, 0.0f)); |
236 | |
237 | normals << n; |
238 | normals << n; |
239 | normals << n; |
240 | |
241 | normals << n; |
242 | normals << n; |
243 | normals << n; |
244 | } |
245 | |
246 | void LogoRenderer::extrude(qreal x1, qreal y1, qreal x2, qreal y2) |
247 | { |
248 | vertices << QVector3D(x1, y1, +0.05f); |
249 | vertices << QVector3D(x2, y2, +0.05f); |
250 | vertices << QVector3D(x1, y1, -0.05f); |
251 | |
252 | vertices << QVector3D(x2, y2, -0.05f); |
253 | vertices << QVector3D(x1, y1, -0.05f); |
254 | vertices << QVector3D(x2, y2, +0.05f); |
255 | |
256 | QVector3D n = QVector3D::normal |
257 | (v1: QVector3D(x2 - x1, y2 - y1, 0.0f), v2: QVector3D(0.0f, 0.0f, -0.1f)); |
258 | |
259 | normals << n; |
260 | normals << n; |
261 | normals << n; |
262 | |
263 | normals << n; |
264 | normals << n; |
265 | normals << n; |
266 | } |
267 | |