Changeset 561 for trunk/examples/opengl/pbuffers/glwidget.cpp
- Timestamp:
- Feb 11, 2010, 11:19:06 PM (15 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
-
Property svn:mergeinfo
set to (toggle deleted branches)
/branches/vendor/nokia/qt/4.6.1 merged eligible /branches/vendor/nokia/qt/current merged eligible /branches/vendor/trolltech/qt/current 3-149
-
Property svn:mergeinfo
set to (toggle deleted branches)
-
trunk/examples/opengl/pbuffers/glwidget.cpp
r2 r561 2 2 ** 3 3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). 4 ** Contact: Qt Software Information ([email protected]) 4 ** All rights reserved. 5 ** Contact: Nokia Corporation ([email protected]) 5 6 ** 6 7 ** This file is part of the examples of the Qt Toolkit. … … 21 22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 22 23 ** 23 ** In addition, as a special exception, Nokia gives you certain 24 ** additional rights. These rights are described in the Nokia Qt LGPL 25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this 26 ** package. 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 27 ** 28 28 ** GNU General Public License Usage … … 34 34 ** met: http://www.gnu.org/copyleft/gpl.html. 35 35 ** 36 ** If you are unsure which license is appropriate for your use, please37 ** contact the sales department at qt-sales@nokia.com.36 ** If you 37 ** @nokia.com. 38 38 ** $QT_END_LICENSE$ 39 39 ** … … 41 41 42 42 #include "glwidget.h" 43 #include <QtGui/QImage>44 45 43 #include <math.h> 46 44 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} 45 #include "cube.h" 46 47 #include <QGLPixelBuffer> 48 49 #ifndef GL_MULTISAMPLE 50 #define GL_MULTISAMPLE 0x809D 51 #endif 52 53 static GLfloat colorArray[][4] = { 54 {0.243f, 0.423f, 0.125f, 1.0f}, 55 {0.176f, 0.31f, 0.09f, 1.0f}, 56 {0.4f, 0.69f, 0.212f, 1.0f}, 57 {0.317f, 0.553f, 0.161f, 1.0f} 54 58 }; 55 59 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 60 GLWidget::GLWidget(QWidget *parent) 77 : QGLWidget(QGLFormat(QGL::SampleBuffers), parent) 61 : QGLWidget(QGLFormat(QGL::SampleBuffers), parent) 62 , geom(0) 63 , cube(0) 78 64 { 79 65 // create the pbuffer 80 66 pbuffer = new QGLPixelBuffer(QSize(512, 512), format(), this); 81 timerId = startTimer(20);82 67 setWindowTitle(tr("OpenGL pbuffers")); 68 83 69 } 84 70 … … 87 73 pbuffer->releaseFromDynamicTexture(); 88 74 glDeleteTextures(1, &dynamicTexture); 89 glDeleteLists(pbufferList, 1);90 75 delete pbuffer; 76 77 78 79 91 80 } 92 81 93 82 void GLWidget::initializeGL() 94 83 { 95 glMatrixMode(GL_MODELVIEW); 96 84 initCommon(); 85 glShadeModel(GL_SMOOTH); 86 glEnable(GL_LIGHTING); 87 glEnable(GL_LIGHT0); 88 static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 1.0 }; 89 glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); 90 initPbuffer(); 91 cube->startAnimation(); 92 connect(cube, SIGNAL(changed()), this, SLOT(update())); 93 for (int i = 0; i < 3; ++i) 94 { 95 cubes[i]->startAnimation(); 96 connect(cubes[i], SIGNAL(changed()), this, SLOT(update())); 97 } 98 } 99 100 void GLWidget::paintGL() 101 { 102 pbuffer->makeCurrent(); 103 drawPbuffer(); 104 // On direct render platforms, drawing onto the pbuffer context above 105 // automatically updates the dynamic texture. For cases where rendering 106 // directly to a texture is not supported, explicitly copy. 107 if (!hasDynamicTextureUpdate) 108 pbuffer->updateDynamicTexture(dynamicTexture); 109 makeCurrent(); 110 111 // Use the pbuffer as a texture to render the scene 112 glBindTexture(GL_TEXTURE_2D, dynamicTexture); 113 114 // set up to render the scene 115 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 116 glLoadIdentity(); 117 glTranslatef(0.0f, 0.0f, -10.0f); 118 119 // draw the background 120 glPushMatrix(); 121 glScalef(aspect, 1.0f, 1.0f); 122 for (int i = 0; i < tiles.count(); ++i) 123 tiles[i]->draw(); 124 glPopMatrix(); 125 126 // draw the bouncing cubes 127 for (int i = 0; i < cubes.count(); ++i) 128 cubes[i]->draw(); 129 } 130 131 void GLWidget::initializeGeometry() 132 { 133 geom = new Geometry(); 134 CubeBuilder cBuilder(geom, 0.5); 135 cBuilder.setColor(QColor(255, 255, 255, 212)); 136 // build the 3 bouncing, spinning cubes 137 for (int i = 0; i < 3; ++i) 138 cubes.append(cBuilder.newCube(QVector3D((float)(i-1), -1.5f, 5 - i))); 139 140 // build the spinning cube which goes in the dynamic texture 141 cube = cBuilder.newCube(); 142 cube->removeBounce(); 143 144 // build the background tiles 145 TileBuilder tBuilder(geom); 146 tBuilder.setColor(QColor(Qt::white)); 147 for (int c = -2; c <= +2; ++c) 148 for (int r = -2; r <= +2; ++r) 149 tiles.append(tBuilder.newTile(QVector3D(c, r, 0))); 150 151 // graded backdrop for the pbuffer scene 152 TileBuilder bBuilder(geom, 0.0f, 2.0f); 153 bBuilder.setColor(QColor(102, 176, 54, 210)); 154 backdrop = bBuilder.newTile(QVector3D(0.0f, 0.0f, -1.5f)); 155 backdrop->setColors(colorArray); 156 } 157 158 void GLWidget::initCommon() 159 { 160 qglClearColor(QColor(Qt::darkGray)); 161 162 glEnable(GL_DEPTH_TEST); 97 163 glEnable(GL_CULL_FACE); 98 initCommon(); 99 initPbuffer(); 100 101 for (int i = 0; i < 3; ++i) { 102 yOffs[i] = 0.0f; 103 xInc[i] = 0.005f; 104 rot[i] = 0.0f; 105 } 106 xOffs[0]= 0.0f; 107 xOffs[1]= 0.5f; 108 xOffs[2]= 1.0f; 109 110 cubeTexture = bindTexture(QImage(":res/cubelogo.png")); 111 } 112 113 void GLWidget::resizeGL(int w, int h) 114 { 115 glViewport(0, 0, w, h); 164 glEnable(GL_MULTISAMPLE); 165 166 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 167 glEnable(GL_BLEND); 168 169 glEnable(GL_TEXTURE_2D); 170 171 geom->loadArrays(); 172 } 173 174 void GLWidget::perspectiveProjection() 175 { 116 176 glMatrixMode(GL_PROJECTION); 117 177 glLoadIdentity(); 118 float aspect = w/(float)(h ? h : 1); 119 glFrustum(-aspect, aspect, -1, 1, 10, 100); 120 glTranslatef(-0.5f, -0.5f, -0.5f); 121 glTranslatef(0.0f, 0.0f, -15.0f); 122 } 123 124 void GLWidget::paintGL() 125 { 126 // draw a spinning cube into the pbuffer.. 127 pbuffer->makeCurrent(); 128 glBindTexture(GL_TEXTURE_2D, cubeTexture); 129 glCallList(pbufferList); 130 glFlush(); 131 132 // rendering directly to a texture is not supported on X11 and 133 // some Windows implementations, unfortunately 134 if (!hasDynamicTextureUpdate) 135 pbuffer->updateDynamicTexture(dynamicTexture); 136 137 // ..and use the pbuffer contents as a texture when rendering the 138 // background and the bouncing cubes 139 makeCurrent(); 140 glBindTexture(GL_TEXTURE_2D, dynamicTexture); 141 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 142 143 // draw the background 178 #ifdef QT_OPENGL_ES 179 glFrustumf(-aspect, +aspect, -1.0, +1.0, 4.0, 15.0); 180 #else 181 glFrustum(-aspect, +aspect, -1.0, +1.0, 4.0, 15.0); 182 #endif 144 183 glMatrixMode(GL_MODELVIEW); 145 glPushMatrix(); 146 glLoadIdentity(); 147 glMatrixMode(GL_PROJECTION); 148 glPushMatrix(); 149 glLoadIdentity(); 150 151 glVertexPointer(2, GL_INT, 0, faceArray); 152 glTranslatef(-1.2f, -0.8f, 0.0f); 153 glScalef(0.2f, 0.2f, 0.2f); 154 for (int y = 0; y < 5; ++y) { 155 for (int x = 0; x < 5; ++x) { 156 glTranslatef(2.0f, 0, 0); 157 glColor4f(0.8, 0.8, 0.8, 1.0); 158 glDrawArrays(GL_QUADS, 0, 4); 159 } 160 glTranslatef(-10.0f, 2.0f, 0); 161 } 162 glVertexPointer(3, GL_INT, 0, cubeArray); 163 164 glPopMatrix(); 165 glMatrixMode(GL_MODELVIEW); 166 glPopMatrix(); 167 168 // draw the bouncing cubes 169 drawCube(0, 0.0f, 1.5f, 2.5f, 1.5f); 170 drawCube(1, 1.0f, 2.0f, 2.5f, 2.0f); 171 drawCube(2, 2.0f, 3.5f, 2.5f, 2.5f); 172 } 173 174 void GLWidget::drawCube(int i, GLfloat z, GLfloat rotation, GLfloat jmp, GLfloat amp) 175 { 176 glMatrixMode(GL_MODELVIEW); 177 glLoadIdentity(); 178 glTranslatef(xOffs[i], yOffs[i], z); 179 glTranslatef(0.5f, 0.5f, 0.5f); 180 GLfloat scale = 0.75 + i*(0.25f/2); 181 glScalef(scale, scale, scale); 182 glRotatef(rot[i], 1.0f, 1.0f, 1.0f); 183 glTranslatef(-0.5f, -0.5f, -0.5f); 184 185 glColor4f(1.0f, 1.0f, 1.0f, 0.8f); 186 glDrawArrays(GL_QUADS, 0, 24); 187 188 if (xOffs[i] > 1.0f || xOffs[i] < -1.0f) { 189 xInc[i] = -xInc[i]; 190 xOffs[i] = xOffs[i] > 1.0f ? 1.0f : -1.0f; 191 } 192 xOffs[i] += xInc[i]; 193 yOffs[i] = qAbs(cos((-3.141592f * jmp) * xOffs[i]) * amp) - 1; 194 rot[i] += rotation; 195 } 196 197 void GLWidget::initCommon() 198 { 199 glEnableClientState(GL_VERTEX_ARRAY); 200 glEnableClientState(GL_TEXTURE_COORD_ARRAY); 201 glVertexPointer(3, GL_INT, 0, cubeArray); 202 glTexCoordPointer(2, GL_INT, 0, cubeTextureArray); 203 glColorPointer(4, GL_UNSIGNED_BYTE, 0, colorArray); 204 205 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 206 glEnable(GL_BLEND); 207 glEnable(GL_TEXTURE_2D); 208 glEnable(GL_DEPTH_TEST); 209 210 glClearColor(1.0f, 1.0f, 1.0f, 1.0f); 211 } 212 213 void GLWidget::initPbuffer() 214 { 215 // set up the pbuffer context 216 pbuffer->makeCurrent(); 217 initCommon(); 218 219 glViewport(0, 0, pbuffer->size().width(), pbuffer->size().height()); 184 } 185 186 void GLWidget::orthographicProjection() 187 { 220 188 glMatrixMode(GL_PROJECTION); 221 189 glLoadIdentity(); 222 glOrtho(-1, 1, -1, 1, -99, 99); 223 glTranslatef(-0.5f, -0.5f, 0.0f); 190 #ifdef QT_OPENGL_ES 191 glOrthof(-1.0, +1.0, -1.0, +1.0, -90.0, +90.0); 192 #else 193 glOrtho(-1.0, +1.0, -1.0, +1.0, -90.0, +90.0); 194 #endif 224 195 glMatrixMode(GL_MODELVIEW); 225 glLoadIdentity(); 226 227 pbufferList = glGenLists(1); 228 glNewList(pbufferList, GL_COMPILE); 229 { 230 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); 231 232 // draw cube background 233 glPushMatrix(); 234 glLoadIdentity(); 235 glTranslatef(0.5f, 0.5f, -2.0f); 236 glDisable(GL_TEXTURE_2D); 237 glEnableClientState(GL_COLOR_ARRAY); 238 glVertexPointer(2, GL_INT, 0, faceArray); 239 glDrawArrays(GL_QUADS, 0, 4); 240 glVertexPointer(3, GL_INT, 0, cubeArray); 241 glDisableClientState(GL_COLOR_ARRAY); 242 glEnable(GL_TEXTURE_2D); 243 glPopMatrix(); 244 245 // draw cube 246 glTranslatef(0.5f, 0.5f, 0.5f); 247 glRotatef(3.0f, 1.0f, 1.0f, 1.0f); 248 glTranslatef(-0.5f, -0.5f, -0.5f); 249 glColor4f(0.9f, 0.9f, 0.9f, 1.0f); 250 glDrawArrays(GL_QUADS, 0, 24); 251 } 252 glEndList(); 196 } 197 198 void GLWidget::resizeGL(int width, int height) 199 { 200 glViewport(0, 0, width, height); 201 aspect = (qreal)width / (qreal)(height ? height : 1); 202 perspectiveProjection(); 203 } 204 205 void GLWidget::drawPbuffer() 206 { 207 orthographicProjection(); 208 209 glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 210 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 211 212 glDisable(GL_TEXTURE_2D); 213 backdrop->draw(); 214 glEnable(GL_TEXTURE_2D); 215 216 glBindTexture(GL_TEXTURE_2D, cubeTexture); 217 glDisable(GL_CULL_FACE); 218 cube->draw(); 219 glEnable(GL_CULL_FACE); 220 221 glFlush(); 222 } 223 224 void GLWidget::initPbuffer() 225 { 226 pbuffer->makeCurrent(); 227 228 cubeTexture = bindTexture(QImage(":res/cubelogo.png")); 229 230 initCommon(); 231 253 232 // generate a texture that has the same size/format as the pbuffer 254 233 dynamicTexture = pbuffer->generateDynamicTexture(); … … 259 238 } 260 239 240 241 242 243 244 245
Note:
See TracChangeset
for help on using the changeset viewer.