1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the examples of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include <QtGui>
|
---|
43 | #include <QtOpenGL>
|
---|
44 |
|
---|
45 | #include <math.h>
|
---|
46 |
|
---|
47 | #include "glwidget.h"
|
---|
48 |
|
---|
49 | GLuint GLWidget::sharedObject = 0;
|
---|
50 | int GLWidget::refCount = 0;
|
---|
51 |
|
---|
52 | GLWidget::GLWidget(QWidget *parent, QGLWidget *shareWidget)
|
---|
53 | : QGLWidget(parent, shareWidget)
|
---|
54 | {
|
---|
55 | clearColor = Qt::black;
|
---|
56 | xRot = 0;
|
---|
57 | yRot = 0;
|
---|
58 | zRot = 0;
|
---|
59 | }
|
---|
60 |
|
---|
61 | GLWidget::~GLWidget()
|
---|
62 | {
|
---|
63 | if (--refCount == 0) {
|
---|
64 | makeCurrent();
|
---|
65 | glDeleteLists(sharedObject, 1);
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | QSize GLWidget::minimumSizeHint() const
|
---|
70 | {
|
---|
71 | return QSize(50, 50);
|
---|
72 | }
|
---|
73 |
|
---|
74 | QSize GLWidget::sizeHint() const
|
---|
75 | {
|
---|
76 | return QSize(200, 200);
|
---|
77 | }
|
---|
78 |
|
---|
79 | void GLWidget::rotateBy(int xAngle, int yAngle, int zAngle)
|
---|
80 | {
|
---|
81 | xRot += xAngle;
|
---|
82 | yRot += yAngle;
|
---|
83 | zRot += zAngle;
|
---|
84 | updateGL();
|
---|
85 | }
|
---|
86 |
|
---|
87 | void GLWidget::setClearColor(const QColor &color)
|
---|
88 | {
|
---|
89 | clearColor = color;
|
---|
90 | updateGL();
|
---|
91 | }
|
---|
92 |
|
---|
93 | void GLWidget::initializeGL()
|
---|
94 | {
|
---|
95 | if (!sharedObject)
|
---|
96 | sharedObject = makeObject();
|
---|
97 | ++refCount;
|
---|
98 |
|
---|
99 | glEnable(GL_DEPTH_TEST);
|
---|
100 | glEnable(GL_CULL_FACE);
|
---|
101 | glEnable(GL_TEXTURE_2D);
|
---|
102 | }
|
---|
103 |
|
---|
104 | void GLWidget::paintGL()
|
---|
105 | {
|
---|
106 | qglClearColor(clearColor);
|
---|
107 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
108 | glLoadIdentity();
|
---|
109 | glTranslated(0.0, 0.0, -10.0);
|
---|
110 | glRotated(xRot / 16.0, 1.0, 0.0, 0.0);
|
---|
111 | glRotated(yRot / 16.0, 0.0, 1.0, 0.0);
|
---|
112 | glRotated(zRot / 16.0, 0.0, 0.0, 1.0);
|
---|
113 | glCallList(sharedObject);
|
---|
114 | }
|
---|
115 |
|
---|
116 | void GLWidget::resizeGL(int width, int height)
|
---|
117 | {
|
---|
118 | int side = qMin(width, height);
|
---|
119 | glViewport((width - side) / 2, (height - side) / 2, side, side);
|
---|
120 |
|
---|
121 | glMatrixMode(GL_PROJECTION);
|
---|
122 | glLoadIdentity();
|
---|
123 | glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0);
|
---|
124 | glMatrixMode(GL_MODELVIEW);
|
---|
125 | }
|
---|
126 |
|
---|
127 | void GLWidget::mousePressEvent(QMouseEvent *event)
|
---|
128 | {
|
---|
129 | lastPos = event->pos();
|
---|
130 | }
|
---|
131 |
|
---|
132 | void GLWidget::mouseMoveEvent(QMouseEvent *event)
|
---|
133 | {
|
---|
134 | int dx = event->x() - lastPos.x();
|
---|
135 | int dy = event->y() - lastPos.y();
|
---|
136 |
|
---|
137 | if (event->buttons() & Qt::LeftButton) {
|
---|
138 | rotateBy(8 * dy, 8 * dx, 0);
|
---|
139 | } else if (event->buttons() & Qt::RightButton) {
|
---|
140 | rotateBy(8 * dy, 0, 8 * dx);
|
---|
141 | }
|
---|
142 | lastPos = event->pos();
|
---|
143 | }
|
---|
144 |
|
---|
145 | void GLWidget::mouseReleaseEvent(QMouseEvent * /* event */)
|
---|
146 | {
|
---|
147 | emit clicked();
|
---|
148 | }
|
---|
149 |
|
---|
150 | GLuint GLWidget::makeObject()
|
---|
151 | {
|
---|
152 | static const int coords[6][4][3] = {
|
---|
153 | { { +1, -1, -1 }, { -1, -1, -1 }, { -1, +1, -1 }, { +1, +1, -1 } },
|
---|
154 | { { +1, +1, -1 }, { -1, +1, -1 }, { -1, +1, +1 }, { +1, +1, +1 } },
|
---|
155 | { { +1, -1, +1 }, { +1, -1, -1 }, { +1, +1, -1 }, { +1, +1, +1 } },
|
---|
156 | { { -1, -1, -1 }, { -1, -1, +1 }, { -1, +1, +1 }, { -1, +1, -1 } },
|
---|
157 | { { +1, -1, +1 }, { -1, -1, +1 }, { -1, -1, -1 }, { +1, -1, -1 } },
|
---|
158 | { { -1, -1, +1 }, { +1, -1, +1 }, { +1, +1, +1 }, { -1, +1, +1 } }
|
---|
159 | };
|
---|
160 |
|
---|
161 |
|
---|
162 | GLuint textures[6];
|
---|
163 | for (int j=0; j < 6; ++j)
|
---|
164 | textures[j] = bindTexture(QPixmap(QString(":/images/side%1.png").arg(j + 1)),
|
---|
165 | GL_TEXTURE_2D);
|
---|
166 |
|
---|
167 | GLuint list = glGenLists(1);
|
---|
168 | glNewList(list, GL_COMPILE);
|
---|
169 | for (int i = 0; i < 6; ++i) {
|
---|
170 | glBindTexture(GL_TEXTURE_2D, textures[i]);
|
---|
171 | glBegin(GL_QUADS);
|
---|
172 | for (int j = 0; j < 4; ++j) {
|
---|
173 | glTexCoord2d(j == 0 || j == 3, j == 0 || j == 1);
|
---|
174 | glVertex3d(0.2 * coords[i][j][0], 0.2 * coords[i][j][1],
|
---|
175 | 0.2 * coords[i][j][2]);
|
---|
176 | }
|
---|
177 | glEnd();
|
---|
178 | }
|
---|
179 |
|
---|
180 | glEndList();
|
---|
181 | return list;
|
---|
182 | }
|
---|