|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 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 Qt3D module 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. |
|
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 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "view.h" |
|
43 #include "teapot.h" |
|
44 #include "cube.h" |
|
45 #include <QtCore/qdebug.h> |
|
46 #include <QtCore/qtimer.h> |
|
47 #include <stdio.h> |
|
48 #include "qaccelerometer.h" |
|
49 |
|
50 #if defined(QT_OPENGL_ES) |
|
51 #define USE_BUFFERS 1 |
|
52 #endif |
|
53 |
|
54 View::View(QWidget *parent) |
|
55 : QGLWidget(parent), |
|
56 sensitivity(0.1f), |
|
57 painter(0), |
|
58 showFrameRate(false) |
|
59 { |
|
60 mainCamera = new Camera(this); |
|
61 |
|
62 roomCamera = new Camera(this); |
|
63 roomCamera->setAdjustForAspectRatio(false); |
|
64 |
|
65 sensor = new QAccelerometer(this); |
|
66 connect(sensor, SIGNAL(readingChanged()), this, SLOT(accelerometerTimeout())); |
|
67 sensor->start(); |
|
68 |
|
69 time.start(); |
|
70 |
|
71 vertexBuffer = 0; |
|
72 indexBuffer = 0; |
|
73 } |
|
74 |
|
75 View::~View() |
|
76 { |
|
77 delete painter; |
|
78 } |
|
79 |
|
80 void View::resizeGL(int width, int height) |
|
81 { |
|
82 glViewport(0, 0, width, height); |
|
83 } |
|
84 |
|
85 void View::initializeGL() |
|
86 { |
|
87 #if defined(QT_OPENGL_ES_2) |
|
88 painter = new ShaderPainter(); |
|
89 #else |
|
90 painter = new FixedFunctionPainter(); |
|
91 #endif |
|
92 |
|
93 glEnable(GL_DEPTH_TEST); |
|
94 |
|
95 roomMaterialBack = new Material(); |
|
96 roomMaterialBack->setDiffuseColor(QColor(128, 100, 0)); |
|
97 |
|
98 roomMaterialLeftRight = new Material(); |
|
99 roomMaterialLeftRight->setDiffuseColor(Qt::cyan); |
|
100 |
|
101 roomMaterialTopBottom = new Material(); |
|
102 roomMaterialTopBottom->setDiffuseColor(Qt::yellow); |
|
103 |
|
104 cubeMaterial = new Material(); |
|
105 cubeMaterial->setColor(QColor(170, 202, 0)); |
|
106 |
|
107 teapotMaterial = new Material(); |
|
108 teapotMaterial->setAmbientColor(QColor(192, 150, 128)); |
|
109 teapotMaterial->setSpecularColor(QColor(60, 60, 60)); |
|
110 teapotMaterial->setShininess(128); |
|
111 |
|
112 roomModel = new LightModel(this); |
|
113 roomModel->setAmbientSceneColor(Qt::white); |
|
114 roomModel->setViewerPosition(LightModel::LocalViewer); |
|
115 |
|
116 normalModel = new LightModel(this); |
|
117 |
|
118 Light *light = new Light(this); |
|
119 light->setPosition(QVector3D(0.0f, 0.0f, 3.0f)); |
|
120 painter->setLight(light); |
|
121 |
|
122 texture = bindTexture(QImage(QLatin1String(":/qtlogo.png"))); |
|
123 |
|
124 #ifdef USE_BUFFERS |
|
125 // Upload the teapot data into GL buffers for quicker rendering. |
|
126 glGenBuffers(1, &vertexBuffer); |
|
127 glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); |
|
128 glBufferData(GL_ARRAY_BUFFER, |
|
129 teapotVertexCount * teapotVertexStride * sizeof(float), |
|
130 teapotVertexData, GL_STATIC_DRAW); |
|
131 glBindBuffer(GL_ARRAY_BUFFER, 0); |
|
132 |
|
133 glGenBuffers(1, &indexBuffer); |
|
134 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); |
|
135 glBufferData(GL_ELEMENT_ARRAY_BUFFER, |
|
136 teapotTriangleCount * 3 * sizeof(ushort), |
|
137 teapotTriangleData, GL_STATIC_DRAW); |
|
138 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
|
139 #endif |
|
140 } |
|
141 |
|
142 void View::paintGL() |
|
143 { |
|
144 if (showFrameRate) |
|
145 qWarning("time since last frame: %d ms", time.restart()); |
|
146 |
|
147 qreal aspectRatio = qreal(width()) / qreal(height()); |
|
148 QMatrix4x4 mv, mv2; |
|
149 QMatrix4x4 proj; |
|
150 |
|
151 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
152 |
|
153 mv = roomCamera->modelViewMatrix(); |
|
154 proj = roomCamera->projectionMatrix(aspectRatio); |
|
155 painter->setLightModel(roomModel); |
|
156 painter->setMatrices(mv, proj); |
|
157 |
|
158 painter->selectMaterial(roomMaterialBack); |
|
159 painter->drawQuad(QVector3D(-3.0f, -3.0f, -15.0f), |
|
160 QVector3D( 3.0f, -3.0f, -15.0f), |
|
161 QVector3D( 3.0f, 3.0f, -15.0f), |
|
162 QVector3D(-3.0f, 3.0f, -15.0f), |
|
163 QVector3D(0.0f, 0.0f, 1.0f)); |
|
164 |
|
165 painter->selectMaterial(roomMaterialLeftRight); |
|
166 painter->drawQuad(QVector3D(-3.0f, -3.0f, -15.0f), |
|
167 QVector3D(-3.0f, 3.0f, -15.0f), |
|
168 QVector3D(-3.0f, 3.0f, 0.0f), |
|
169 QVector3D(-3.0f, -3.0f, 0.0f), |
|
170 QVector3D(1.0f, 0.0f, 0.0f)); |
|
171 painter->drawQuad(QVector3D(3.0f, 3.0f, -15.0f), |
|
172 QVector3D(3.0f, -3.0f, -15.0f), |
|
173 QVector3D(3.0f, -3.0f, 0.0f), |
|
174 QVector3D(3.0f, 3.0f, 0.0f), |
|
175 QVector3D(-1.0f, 0.0f, 0.0f)); |
|
176 |
|
177 painter->selectMaterial(roomMaterialTopBottom); |
|
178 painter->drawQuad(QVector3D(-3.0f, 3.0f, -15.0f), |
|
179 QVector3D( 3.0f, 3.0f, -15.0f), |
|
180 QVector3D( 3.0f, 3.0f, 0.0f), |
|
181 QVector3D(-3.0f, 3.0f, 0.0f), |
|
182 QVector3D(0.0f, -1.0f, 0.0f)); |
|
183 painter->drawQuad(QVector3D(-3.0f, -3.0f, -15.0f), |
|
184 QVector3D(-3.0f, -3.0f, 0.0f), |
|
185 QVector3D( 3.0f, -3.0f, 0.0f), |
|
186 QVector3D( 3.0f, -3.0f, -15.0f), |
|
187 QVector3D(0.0f, 1.0f, 0.0f)); |
|
188 |
|
189 mv = mv2 = mainCamera->modelViewMatrix(); |
|
190 proj = mainCamera->projectionMatrix(aspectRatio); |
|
191 mv.translate(1.0f, -0.5f, 0.0f); |
|
192 mv.rotate(45.0f, 1.0f, 1.0f, 1.0f); |
|
193 painter->setMatrices(mv, proj); |
|
194 painter->setLightModel(normalModel); |
|
195 painter->selectTexturedMaterial(cubeMaterial); |
|
196 painter->setVertices(cubeVertices, 8); |
|
197 painter->setNormals(cubeVertices + 3, 8); |
|
198 painter->setTexCoords(cubeVertices + 6, 8); |
|
199 glDrawArrays(GL_TRIANGLES, 0, 36); |
|
200 |
|
201 mv2.translate(-0.8f, -1.5f, -3.0f); |
|
202 painter->setMatrices(mv2, proj); |
|
203 painter->selectMaterial(teapotMaterial); |
|
204 #ifdef USE_BUFFERS |
|
205 glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); |
|
206 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); |
|
207 painter->setVertices(0, 8); |
|
208 painter->setNormals(reinterpret_cast<float *>(3 * sizeof(float)), 8); |
|
209 painter->setTexCoords(reinterpret_cast<float *>(6 * sizeof(float)), 8); |
|
210 glDrawElements(GL_TRIANGLES, teapotTriangleCount * 3, |
|
211 GL_UNSIGNED_SHORT, 0); |
|
212 glBindBuffer(GL_ARRAY_BUFFER, 0); |
|
213 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
|
214 #else |
|
215 painter->setVertices(teapotVertexData, 8); |
|
216 painter->setNormals(teapotVertexData + 3, 8); |
|
217 painter->setTexCoords(teapotVertexData + 6, 8); |
|
218 glDrawElements(GL_TRIANGLES, teapotTriangleCount * 3, |
|
219 GL_UNSIGNED_SHORT, teapotTriangleData); |
|
220 #endif |
|
221 } |
|
222 |
|
223 void View::accelerometerTimeout() |
|
224 { |
|
225 QVector3D g = gravity(); |
|
226 mainCamera->setMotionAdjustment(g); |
|
227 roomCamera->setMotionAdjustment(g); |
|
228 update(); |
|
229 } |
|
230 |
|
231 #define ACCEL_TO_G(v) (v / 9.80665) |
|
232 |
|
233 QVector3D View::gravity() const |
|
234 { |
|
235 qreal x = ACCEL_TO_G(sensor->reading()->x()) * sensitivity; |
|
236 qreal y = ACCEL_TO_G(sensor->reading()->y()) * sensitivity; |
|
237 qreal z = ACCEL_TO_G(sensor->reading()->z()); |
|
238 |
|
239 return QVector3D(x, y, z); |
|
240 } |