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 "logo.h" |
52 | #include <qmath.h> |
53 | |
54 | Logo::Logo() |
55 | : m_count(0) |
56 | { |
57 | m_data.resize(asize: 2500 * 6); |
58 | |
59 | const GLfloat x1 = +0.06f; |
60 | const GLfloat y1 = -0.14f; |
61 | const GLfloat x2 = +0.14f; |
62 | const GLfloat y2 = -0.06f; |
63 | const GLfloat x3 = +0.08f; |
64 | const GLfloat y3 = +0.00f; |
65 | const GLfloat x4 = +0.30f; |
66 | const GLfloat y4 = +0.22f; |
67 | |
68 | quad(x1, y1, x2, y2, x3: y2, y3: x2, x4: y1, y4: x1); |
69 | quad(x1: x3, y1: y3, x2: x4, y2: y4, x3: y4, y3: x4, x4: y3, y4: x3); |
70 | |
71 | extrude(x1, y1, x2, y2); |
72 | extrude(x1: x2, y1: y2, x2: y2, y2: x2); |
73 | extrude(x1: y2, y1: x2, x2: y1, y2: x1); |
74 | extrude(x1: y1, y1: x1, x2: x1, y2: y1); |
75 | extrude(x1: x3, y1: y3, x2: x4, y2: y4); |
76 | extrude(x1: x4, y1: y4, x2: y4, y2: x4); |
77 | extrude(x1: y4, y1: x4, x2: y3, y2: x3); |
78 | |
79 | const int NumSectors = 100; |
80 | |
81 | for (int i = 0; i < NumSectors; ++i) { |
82 | GLfloat angle = (i * 2 * M_PI) / NumSectors; |
83 | GLfloat angleSin = qSin(v: angle); |
84 | GLfloat angleCos = qCos(v: angle); |
85 | const GLfloat x5 = 0.30f * angleSin; |
86 | const GLfloat y5 = 0.30f * angleCos; |
87 | const GLfloat x6 = 0.20f * angleSin; |
88 | const GLfloat y6 = 0.20f * angleCos; |
89 | |
90 | angle = ((i + 1) * 2 * M_PI) / NumSectors; |
91 | angleSin = qSin(v: angle); |
92 | angleCos = qCos(v: angle); |
93 | const GLfloat x7 = 0.20f * angleSin; |
94 | const GLfloat y7 = 0.20f * angleCos; |
95 | const GLfloat x8 = 0.30f * angleSin; |
96 | const GLfloat y8 = 0.30f * angleCos; |
97 | |
98 | quad(x1: x5, y1: y5, x2: x6, y2: y6, x3: x7, y3: y7, x4: x8, y4: y8); |
99 | |
100 | extrude(x1: x6, y1: y6, x2: x7, y2: y7); |
101 | extrude(x1: x8, y1: y8, x2: x5, y2: y5); |
102 | } |
103 | } |
104 | |
105 | void Logo::add(const QVector3D &v, const QVector3D &n) |
106 | { |
107 | GLfloat *p = m_data.data() + m_count; |
108 | *p++ = v.x(); |
109 | *p++ = v.y(); |
110 | *p++ = v.z(); |
111 | *p++ = n.x(); |
112 | *p++ = n.y(); |
113 | *p++ = n.z(); |
114 | m_count += 6; |
115 | } |
116 | |
117 | void Logo::quad(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2, GLfloat x3, GLfloat y3, GLfloat x4, GLfloat y4) |
118 | { |
119 | QVector3D n = QVector3D::normal(v1: QVector3D(x4 - x1, y4 - y1, 0.0f), v2: QVector3D(x2 - x1, y2 - y1, 0.0f)); |
120 | |
121 | add(v: QVector3D(x1, y1, -0.05f), n); |
122 | add(v: QVector3D(x4, y4, -0.05f), n); |
123 | add(v: QVector3D(x2, y2, -0.05f), n); |
124 | |
125 | add(v: QVector3D(x3, y3, -0.05f), n); |
126 | add(v: QVector3D(x2, y2, -0.05f), n); |
127 | add(v: QVector3D(x4, y4, -0.05f), n); |
128 | |
129 | n = QVector3D::normal(v1: QVector3D(x1 - x4, y1 - y4, 0.0f), v2: QVector3D(x2 - x4, y2 - y4, 0.0f)); |
130 | |
131 | add(v: QVector3D(x4, y4, 0.05f), n); |
132 | add(v: QVector3D(x1, y1, 0.05f), n); |
133 | add(v: QVector3D(x2, y2, 0.05f), n); |
134 | |
135 | add(v: QVector3D(x2, y2, 0.05f), n); |
136 | add(v: QVector3D(x3, y3, 0.05f), n); |
137 | add(v: QVector3D(x4, y4, 0.05f), n); |
138 | } |
139 | |
140 | void Logo::extrude(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2) |
141 | { |
142 | QVector3D n = QVector3D::normal(v1: QVector3D(0.0f, 0.0f, -0.1f), v2: QVector3D(x2 - x1, y2 - y1, 0.0f)); |
143 | |
144 | add(v: QVector3D(x1, y1, +0.05f), n); |
145 | add(v: QVector3D(x1, y1, -0.05f), n); |
146 | add(v: QVector3D(x2, y2, +0.05f), n); |
147 | |
148 | add(v: QVector3D(x2, y2, -0.05f), n); |
149 | add(v: QVector3D(x2, y2, +0.05f), n); |
150 | add(v: QVector3D(x1, y1, -0.05f), n); |
151 | } |
152 | |