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