Cleaning up usage of examples/{examplebase,symbianpkgrules}.pri
[qt-netbsd.git] / examples / opengl / hellogl / glwidget.cpp
blob282f21f8cf5209abbd7d0fcd47a98a2691d0905c
1 /****************************************************************************
2 **
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the examples of the Qt Toolkit.
8 **
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
14 ** this package.
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.
38 ** $QT_END_LICENSE$
40 ****************************************************************************/
42 #include <QtGui>
43 #include <QtOpenGL>
45 #include <math.h>
47 #include "glwidget.h"
48 #include "qtlogo.h"
50 #ifndef GL_MULTISAMPLE
51 #define GL_MULTISAMPLE 0x809D
52 #endif
54 //! [0]
55 GLWidget::GLWidget(QWidget *parent)
56 : QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
58 logo = 0;
59 xRot = 0;
60 yRot = 0;
61 zRot = 0;
63 qtGreen = QColor::fromCmykF(0.40, 0.0, 1.0, 0.0);
64 qtPurple = QColor::fromCmykF(0.39, 0.39, 0.0, 0.0);
66 //! [0]
68 //! [1]
69 GLWidget::~GLWidget()
72 //! [1]
74 //! [2]
75 QSize GLWidget::minimumSizeHint() const
77 return QSize(50, 50);
79 //! [2]
81 //! [3]
82 QSize GLWidget::sizeHint() const
83 //! [3] //! [4]
85 return QSize(400, 400);
87 //! [4]
89 static void qNormalizeAngle(int &angle)
91 while (angle < 0)
92 angle += 360 * 16;
93 while (angle > 360 * 16)
94 angle -= 360 * 16;
97 //! [5]
98 void GLWidget::setXRotation(int angle)
100 qNormalizeAngle(angle);
101 if (angle != xRot) {
102 xRot = angle;
103 emit xRotationChanged(angle);
104 updateGL();
107 //! [5]
109 void GLWidget::setYRotation(int angle)
111 qNormalizeAngle(angle);
112 if (angle != yRot) {
113 yRot = angle;
114 emit yRotationChanged(angle);
115 updateGL();
119 void GLWidget::setZRotation(int angle)
121 qNormalizeAngle(angle);
122 if (angle != zRot) {
123 zRot = angle;
124 emit zRotationChanged(angle);
125 updateGL();
129 //! [6]
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);
141 glEnable(GL_LIGHT0);
142 glEnable(GL_MULTISAMPLE);
143 static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 1.0 };
144 glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
146 //! [6]
148 //! [7]
149 void GLWidget::paintGL()
151 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
152 glLoadIdentity();
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);
157 logo->draw();
159 //! [7]
161 //! [8]
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);
168 glLoadIdentity();
169 glOrtho(-0.5, +0.5, -0.5, +0.5, 4.0, 15.0);
170 glMatrixMode(GL_MODELVIEW);
172 //! [8]
174 //! [9]
175 void GLWidget::mousePressEvent(QMouseEvent *event)
177 lastPos = event->pos();
179 //! [9]
181 //! [10]
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();
196 //! [10]