|
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. |
|
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 "glwidget.h" |
|
43 #include <QtGui/QImage> |
|
44 |
|
45 #include <math.h> |
|
46 |
|
47 static GLint cubeArray[][3] = { |
|
48 {0, 0, 0}, {0, 1, 0}, {1, 1, 0}, {1, 0, 0}, |
|
49 {0, 0, 1}, {1, 0, 1}, {1, 1, 1}, {0, 1, 1}, |
|
50 {0, 0, 0}, {1, 0, 0}, {1, 0, 1}, {0, 0, 1}, |
|
51 {0, 1, 0}, {0, 1, 1}, {1, 1, 1}, {1, 1, 0}, |
|
52 {0, 1, 0}, {0, 0, 0}, {0, 0, 1}, {0, 1, 1}, |
|
53 {1, 0, 0}, {1, 1, 0}, {1, 1, 1}, {1, 0, 1} |
|
54 }; |
|
55 |
|
56 static GLint cubeTextureArray[][2] = { |
|
57 {0, 0}, {1, 0}, {1, 1}, {0, 1}, |
|
58 {0, 0}, {0, 1}, {1, 1}, {1, 0}, |
|
59 {0, 0}, {1, 0}, {1, 1}, {0, 1}, |
|
60 {1, 0}, {0, 0}, {0, 1}, {1, 1}, |
|
61 {0, 0}, {1, 0}, {1, 1}, {0, 1}, |
|
62 {1, 0}, {0, 0}, {0, 1}, {1, 1} |
|
63 }; |
|
64 |
|
65 static GLint faceArray[][2] = { |
|
66 {1, -1}, {1, 1}, {-1, 1}, {-1, -1} |
|
67 }; |
|
68 |
|
69 static GLubyte colorArray[][4] = { |
|
70 {102, 176, 54, 255}, |
|
71 {81, 141, 41, 255}, |
|
72 {62, 108, 32, 255}, |
|
73 {45, 79, 23, 255} |
|
74 }; |
|
75 |
|
76 GLWidget::GLWidget(QWidget *parent) |
|
77 : QGLWidget(QGLFormat(QGL::SampleBuffers), parent) |
|
78 { |
|
79 // create the framebuffer object - make sure to have a current |
|
80 // context before creating it |
|
81 makeCurrent(); |
|
82 fbo = new QGLFramebufferObject(512, 512); |
|
83 timerId = startTimer(20); |
|
84 setWindowTitle(tr("OpenGL framebuffer objects 2")); |
|
85 } |
|
86 |
|
87 GLWidget::~GLWidget() |
|
88 { |
|
89 glDeleteLists(pbufferList, 1); |
|
90 delete fbo; |
|
91 } |
|
92 |
|
93 void GLWidget::initializeGL() |
|
94 { |
|
95 glMatrixMode(GL_MODELVIEW); |
|
96 |
|
97 glEnable(GL_CULL_FACE); |
|
98 |
|
99 glEnableClientState(GL_VERTEX_ARRAY); |
|
100 glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
|
101 glVertexPointer(3, GL_INT, 0, cubeArray); |
|
102 glTexCoordPointer(2, GL_INT, 0, cubeTextureArray); |
|
103 glColorPointer(4, GL_UNSIGNED_BYTE, 0, colorArray); |
|
104 |
|
105 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
|
106 glEnable(GL_BLEND); |
|
107 glEnable(GL_TEXTURE_2D); |
|
108 glEnable(GL_DEPTH_TEST); |
|
109 |
|
110 glClearColor(1.0f, 1.0f, 1.0f, 1.0f); |
|
111 pbufferList = glGenLists(1); |
|
112 glNewList(pbufferList, GL_COMPILE); |
|
113 { |
|
114 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); |
|
115 |
|
116 // draw cube background |
|
117 glPushMatrix(); |
|
118 glLoadIdentity(); |
|
119 glTranslatef(0.5f, 0.5f, -2.0f); |
|
120 glDisable(GL_TEXTURE_2D); |
|
121 glEnableClientState(GL_COLOR_ARRAY); |
|
122 glVertexPointer(2, GL_INT, 0, faceArray); |
|
123 glDrawArrays(GL_QUADS, 0, 4); |
|
124 glVertexPointer(3, GL_INT, 0, cubeArray); |
|
125 glDisableClientState(GL_COLOR_ARRAY); |
|
126 glEnable(GL_TEXTURE_2D); |
|
127 glPopMatrix(); |
|
128 |
|
129 // draw cube |
|
130 glTranslatef(0.5f, 0.5f, 0.5f); |
|
131 glRotatef(3.0f, 1.0f, 1.0f, 1.0f); |
|
132 glTranslatef(-0.5f, -0.5f, -0.5f); |
|
133 glColor4f(0.9f, 0.9f, 0.9f, 1.0f); |
|
134 glDrawArrays(GL_QUADS, 0, 24); |
|
135 |
|
136 glPushMatrix(); // this state is popped back in the paintGL() function |
|
137 } |
|
138 glEndList(); |
|
139 |
|
140 for (int i = 0; i < 3; ++i) { |
|
141 yOffs[i] = 0.0f; |
|
142 xInc[i] = 0.005f; |
|
143 rot[i] = 0.0f; |
|
144 } |
|
145 xOffs[0]= 0.0f; |
|
146 xOffs[1]= 0.5f; |
|
147 xOffs[2]= 1.0f; |
|
148 |
|
149 cubeTexture = bindTexture(QImage(":res/cubelogo.png")); |
|
150 |
|
151 glPushMatrix(); // push to avoid stack underflow in the first paintGL() call |
|
152 } |
|
153 |
|
154 void GLWidget::resizeGL(int w, int h) |
|
155 { |
|
156 glViewport(0, 0, w, h); |
|
157 glMatrixMode(GL_PROJECTION); |
|
158 glLoadIdentity(); |
|
159 float aspect = w/(float)(h ? h : 1); |
|
160 glFrustum(-aspect, aspect, -1, 1, 10, 100); |
|
161 glTranslatef(-0.5f, -0.5f, -0.5f); |
|
162 glTranslatef(0.0f, 0.0f, -15.0f); |
|
163 } |
|
164 |
|
165 void GLWidget::paintGL() |
|
166 { |
|
167 glPopMatrix(); // pop the matrix pushed in the pbuffer list |
|
168 |
|
169 // push the projection matrix and the entire GL state before |
|
170 // doing any rendering into our framebuffer object |
|
171 glPushAttrib(GL_ALL_ATTRIB_BITS); |
|
172 glMatrixMode(GL_PROJECTION); |
|
173 glPushMatrix(); |
|
174 |
|
175 glViewport(0, 0, fbo->size().width(), fbo->size().height()); |
|
176 glMatrixMode(GL_PROJECTION); |
|
177 glLoadIdentity(); |
|
178 glOrtho(-1, 1, -1, 1, -99, 99); |
|
179 glTranslatef(-0.5f, -0.5f, 0.0f); |
|
180 glMatrixMode(GL_MODELVIEW); |
|
181 |
|
182 // render to the framebuffer object |
|
183 fbo->bind(); |
|
184 glBindTexture(GL_TEXTURE_2D, cubeTexture); |
|
185 glCallList(pbufferList); |
|
186 fbo->release(); |
|
187 |
|
188 // pop the projection matrix and GL state back for rendering |
|
189 // to the actual widget |
|
190 glPopAttrib(); |
|
191 glMatrixMode(GL_PROJECTION); |
|
192 glPopMatrix(); |
|
193 |
|
194 glBindTexture(GL_TEXTURE_2D, fbo->texture()); |
|
195 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
196 |
|
197 // draw the background |
|
198 glMatrixMode(GL_MODELVIEW); |
|
199 glPushMatrix(); |
|
200 glLoadIdentity(); |
|
201 glMatrixMode(GL_PROJECTION); |
|
202 glPushMatrix(); |
|
203 glLoadIdentity(); |
|
204 |
|
205 glVertexPointer(2, GL_INT, 0, faceArray); |
|
206 glTranslatef(-1.2f, -0.8f, 0.0f); |
|
207 glScalef(0.2f, 0.2f, 0.2f); |
|
208 for (int y = 0; y < 5; ++y) { |
|
209 for (int x = 0; x < 5; ++x) { |
|
210 glTranslatef(2.0f, 0, 0); |
|
211 glColor4f(0.8f, 0.8f, 0.8f, 1.0f); |
|
212 glDrawArrays(GL_QUADS, 0, 4); |
|
213 } |
|
214 glTranslatef(-10.0f, 2.0f, 0); |
|
215 } |
|
216 glVertexPointer(3, GL_INT, 0, cubeArray); |
|
217 |
|
218 glPopMatrix(); |
|
219 glMatrixMode(GL_MODELVIEW); |
|
220 |
|
221 // draw the bouncing cubes |
|
222 drawCube(0, 0.0f, 1.5f, 2.5f, 1.5f); |
|
223 drawCube(1, 1.0f, 2.0f, 2.5f, 2.0f); |
|
224 drawCube(2, 2.0f, 3.5f, 2.5f, 2.5f); |
|
225 glPopMatrix(); |
|
226 } |
|
227 |
|
228 void GLWidget::drawCube(int i, GLfloat z, GLfloat rotation, GLfloat jmp, GLfloat amp) |
|
229 { |
|
230 glMatrixMode(GL_MODELVIEW); |
|
231 glLoadIdentity(); |
|
232 glTranslatef(xOffs[i], yOffs[i], z); |
|
233 glTranslatef(0.5f, 0.5f, 0.5f); |
|
234 GLfloat scale = 0.75 + i*(0.25f/2); |
|
235 glScalef(scale, scale, scale); |
|
236 glRotatef(rot[i], 1.0f, 1.0f, 1.0f); |
|
237 glTranslatef(-0.5f, -0.5f, -0.5f); |
|
238 |
|
239 glColor4f(1.0f, 1.0f, 1.0f, 0.8f); |
|
240 glDrawArrays(GL_QUADS, 0, 24); |
|
241 |
|
242 if (xOffs[i] > 1.0f || xOffs[i] < -1.0f) { |
|
243 xInc[i] = -xInc[i]; |
|
244 xOffs[i] = xOffs[i] > 1.0f ? 1.0f : -1.0f; |
|
245 } |
|
246 xOffs[i] += xInc[i]; |
|
247 yOffs[i] = qAbs(cos((-3.141592f * jmp) * xOffs[i]) * amp) - 1; |
|
248 rot[i] += rotation; |
|
249 } |