Debian Codegraph @

examples/opengl/legacy/hellogl/glwidget.cpp

raw
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 <QtWidgets>
52 #include <QtOpenGL>
53
54 #include <math.h>
55
56 #include "glwidget.h"
57 #include "qtlogo.h"
58
59 #ifndef GL_MULTISAMPLE
60 #define GL_MULTISAMPLE 0x809D
61 #endif
62
63 //! [0]
64 GLWidget::GLWidget(QWidget *parent)
65 : QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
66 {
67 logo = 0;
68 xRot = 0;
69 yRot = 0;
70 zRot = 0;
71
72 qtGreen = QColor::fromCmykF(0.40, 0.0, 1.0, 0.0);
73 qtPurple = QColor::fromCmykF(0.39, 0.39, 0.0, 0.0);
74 }
75 //! [0]
76
77 //! [1]
78 GLWidget::~GLWidget()
79 {
80 }
81 //! [1]
82
83 //! [2]
84 QSize GLWidget::minimumSizeHint() const
85 {
86 return QSize(50, 50);
87 }
88 //! [2]
89
90 //! [3]
91 QSize GLWidget::sizeHint() const
92 //! [3] //! [4]
93 {
94 return QSize(400, 400);
95 }
96 //! [4]
97
98 static void qNormalizeAngle(int &angle)
99 {
100 while (angle < 0)
101 angle += 360 * 16;
102 while (angle > 360 * 16)
103 angle -= 360 * 16;
104 }
105
106 //! [5]
107 void GLWidget::setXRotation(int angle)
108 {
109 qNormalizeAngle(angle);
110 if (angle != xRot) {
111 xRot = angle;
112 emit xRotationChanged(angle);
113 update();
114 }
115 }
116 //! [5]
117
118 void GLWidget::setYRotation(int angle)
119 {
120 qNormalizeAngle(angle);
121 if (angle != yRot) {
122 yRot = angle;
123 emit yRotationChanged(angle);
124 update();
125 }
126 }
127
128 void GLWidget::setZRotation(int angle)
129 {
130 qNormalizeAngle(angle);
131 if (angle != zRot) {
132 zRot = angle;
133 emit zRotationChanged(angle);
134 update();
135 }
136 }
137
138 //! [6]
139 void GLWidget::initializeGL()
140 {
141 initializeOpenGLFunctions();
142
143 qglClearColor(qtPurple.darker());
144
145 logo = new QtLogo(this, 64);
146 logo->setColor(qtGreen.darker());
147
148 glEnable(GL_DEPTH_TEST);
149 glEnable(GL_CULL_FACE);
150 glShadeModel(GL_SMOOTH);
151 glEnable(GL_LIGHTING);
152 glEnable(GL_LIGHT0);
153 glEnable(GL_MULTISAMPLE);
154 static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 1.0 };
155 glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
156 }
157 //! [6]
158
159 //! [7]
160 void GLWidget::paintGL()
161 {
162 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
163 glLoadIdentity();
164 glTranslatef(0.0, 0.0, -10.0);
165 glRotatef(xRot / 16.0, 1.0, 0.0, 0.0);
166 glRotatef(yRot / 16.0, 0.0, 1.0, 0.0);
167 glRotatef(zRot / 16.0, 0.0, 0.0, 1.0);
168 logo->draw(static_cast<QOpenGLFunctions_1_1 *>(this));
169 }
170 //! [7]
171
172 //! [8]
173 void GLWidget::resizeGL(int width, int height)
174 {
175 int side = qMin(width, height);
176 glViewport((width - side) / 2, (height - side) / 2, side, side);
177
178 glMatrixMode(GL_PROJECTION);
179 glLoadIdentity();
180 glOrtho(-0.5, +0.5, -0.5, +0.5, 4.0, 15.0);
181 glMatrixMode(GL_MODELVIEW);
182 }
183 //! [8]
184
185 //! [9]
186 void GLWidget::mousePressEvent(QMouseEvent *event)
187 {
188 lastPos = event->pos();
189 }
190 //! [9]
191
192 //! [10]
193 void GLWidget::mouseMoveEvent(QMouseEvent *event)
194 {
195 int dx = event->x() - lastPos.x();
196 int dy = event->y() - lastPos.y();
197
198 if (event->buttons() & Qt::LeftButton) {
199 setXRotation(xRot + 8 * dy);
200 setYRotation(yRot + 8 * dx);
201 } else if (event->buttons() & Qt::RightButton) {
202 setXRotation(xRot + 8 * dy);
203 setZRotation(zRot + 8 * dx);
204 }
205 lastPos = event->pos();
206 }
207 //! [10]