1 /****************************************************************************
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the examples of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
50 #ifndef GL_MULTISAMPLE
51 #define GL_MULTISAMPLE 0x809D
55 GLWidget::GLWidget(QWidget
*parent
)
56 : QGLWidget(QGLFormat(QGL::SampleBuffers
), parent
)
63 qtGreen
= QColor::fromCmykF(0.40, 0.0, 1.0, 0.0);
64 qtPurple
= QColor::fromCmykF(0.39, 0.39, 0.0, 0.0);
75 QSize
GLWidget::minimumSizeHint() const
82 QSize
GLWidget::sizeHint() const
85 return QSize(400, 400);
89 static void qNormalizeAngle(int &angle
)
93 while (angle
> 360 * 16)
98 void GLWidget::setXRotation(int angle
)
100 qNormalizeAngle(angle
);
103 emit
xRotationChanged(angle
);
109 void GLWidget::setYRotation(int angle
)
111 qNormalizeAngle(angle
);
114 emit
yRotationChanged(angle
);
119 void GLWidget::setZRotation(int angle
)
121 qNormalizeAngle(angle
);
124 emit
zRotationChanged(angle
);
130 void GLWidget::initializeGL()
132 qglClearColor(qtPurple
.dark());
134 logo
= new QtLogo(this, 64);
135 logo
->setColor(qtGreen
.dark());
137 glEnable(GL_DEPTH_TEST
);
138 glEnable(GL_CULL_FACE
);
139 glShadeModel(GL_SMOOTH
);
140 glEnable(GL_LIGHTING
);
142 glEnable(GL_MULTISAMPLE
);
143 static GLfloat lightPosition
[4] = { 0.5, 5.0, 7.0, 1.0 };
144 glLightfv(GL_LIGHT0
, GL_POSITION
, lightPosition
);
149 void GLWidget::paintGL()
151 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
153 glTranslated(0.0, 0.0, -10.0);
154 glRotated(xRot
/ 16.0, 1.0, 0.0, 0.0);
155 glRotated(yRot
/ 16.0, 0.0, 1.0, 0.0);
156 glRotated(zRot
/ 16.0, 0.0, 0.0, 1.0);
162 void GLWidget::resizeGL(int width
, int height
)
164 int side
= qMin(width
, height
);
165 glViewport((width
- side
) / 2, (height
- side
) / 2, side
, side
);
167 glMatrixMode(GL_PROJECTION
);
169 glOrtho(-0.5, +0.5, -0.5, +0.5, 4.0, 15.0);
170 glMatrixMode(GL_MODELVIEW
);
175 void GLWidget::mousePressEvent(QMouseEvent
*event
)
177 lastPos
= event
->pos();
182 void GLWidget::mouseMoveEvent(QMouseEvent
*event
)
184 int dx
= event
->x() - lastPos
.x();
185 int dy
= event
->y() - lastPos
.y();
187 if (event
->buttons() & Qt::LeftButton
) {
188 setXRotation(xRot
+ 8 * dy
);
189 setYRotation(yRot
+ 8 * dx
);
190 } else if (event
->buttons() & Qt::RightButton
) {
191 setXRotation(xRot
+ 8 * dy
);
192 setZRotation(zRot
+ 8 * dx
);
194 lastPos
= event
->pos();