source: trunk/examples/opengl/hellogl/glwidget.cpp@ 651

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 4.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
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.
23**
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.
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 have questions regarding the use of this file, please contact
37** Nokia 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#include "qtlogo.h"
49
50#ifndef GL_MULTISAMPLE
51#define GL_MULTISAMPLE 0x809D
52#endif
53
54//! [0]
55GLWidget::GLWidget(QWidget *parent)
56 : QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
57{
58 logo = 0;
59 xRot = 0;
60 yRot = 0;
61 zRot = 0;
62
63 qtGreen = QColor::fromCmykF(0.40, 0.0, 1.0, 0.0);
64 qtPurple = QColor::fromCmykF(0.39, 0.39, 0.0, 0.0);
65}
66//! [0]
67
68//! [1]
69GLWidget::~GLWidget()
70{
71}
72//! [1]
73
74//! [2]
75QSize GLWidget::minimumSizeHint() const
76{
77 return QSize(50, 50);
78}
79//! [2]
80
81//! [3]
82QSize GLWidget::sizeHint() const
83//! [3] //! [4]
84{
85 return QSize(400, 400);
86}
87//! [4]
88
89static void qNormalizeAngle(int &angle)
90{
91 while (angle < 0)
92 angle += 360 * 16;
93 while (angle > 360 * 16)
94 angle -= 360 * 16;
95}
96
97//! [5]
98void GLWidget::setXRotation(int angle)
99{
100 qNormalizeAngle(angle);
101 if (angle != xRot) {
102 xRot = angle;
103 emit xRotationChanged(angle);
104 updateGL();
105 }
106}
107//! [5]
108
109void GLWidget::setYRotation(int angle)
110{
111 qNormalizeAngle(angle);
112 if (angle != yRot) {
113 yRot = angle;
114 emit yRotationChanged(angle);
115 updateGL();
116 }
117}
118
119void GLWidget::setZRotation(int angle)
120{
121 qNormalizeAngle(angle);
122 if (angle != zRot) {
123 zRot = angle;
124 emit zRotationChanged(angle);
125 updateGL();
126 }
127}
128
129//! [6]
130void GLWidget::initializeGL()
131{
132 qglClearColor(qtPurple.dark());
133
134 logo = new QtLogo(this, 64);
135 logo->setColor(qtGreen.dark());
136
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);
145}
146//! [6]
147
148//! [7]
149void GLWidget::paintGL()
150{
151 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
152 glLoadIdentity();
153 glTranslatef(0.0, 0.0, -10.0);
154 glRotatef(xRot / 16.0, 1.0, 0.0, 0.0);
155 glRotatef(yRot / 16.0, 0.0, 1.0, 0.0);
156 glRotatef(zRot / 16.0, 0.0, 0.0, 1.0);
157 logo->draw();
158}
159//! [7]
160
161//! [8]
162void GLWidget::resizeGL(int width, int height)
163{
164 int side = qMin(width, height);
165 glViewport((width - side) / 2, (height - side) / 2, side, side);
166
167 glMatrixMode(GL_PROJECTION);
168 glLoadIdentity();
169#ifdef QT_OPENGL_ES_1
170 glOrthof(-0.5, +0.5, -0.5, +0.5, 4.0, 15.0);
171#else
172 glOrtho(-0.5, +0.5, -0.5, +0.5, 4.0, 15.0);
173#endif
174 glMatrixMode(GL_MODELVIEW);
175}
176//! [8]
177
178//! [9]
179void GLWidget::mousePressEvent(QMouseEvent *event)
180{
181 lastPos = event->pos();
182}
183//! [9]
184
185//! [10]
186void GLWidget::mouseMoveEvent(QMouseEvent *event)
187{
188 int dx = event->x() - lastPos.x();
189 int dy = event->y() - lastPos.y();
190
191 if (event->buttons() & Qt::LeftButton) {
192 setXRotation(xRot + 8 * dy);
193 setYRotation(yRot + 8 * dx);
194 } else if (event->buttons() & Qt::RightButton) {
195 setXRotation(xRot + 8 * dy);
196 setZRotation(zRot + 8 * dx);
197 }
198 lastPos = event->pos();
199}
200//! [10]
Note: See TracBrowser for help on using the repository browser.