glpixwidget.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2005-2007 Trolltech ASA. All rights reserved.
00004 **
00005 ** This file is part of the example classes of the Qt Toolkit.
00006 **
00007 ** This file may be used under the terms of the GNU General Public
00008 ** License version 2.0 as published by the Free Software Foundation
00009 ** and appearing in the file LICENSE.GPL included in the packaging of
00010 ** this file.  Please review the following information to ensure GNU
00011 ** General Public Licensing requirements will be met:
00012 ** http://www.trolltech.com/products/qt/opensource.html
00013 **
00014 ** If you are unsure which license is appropriate for your use, please
00015 ** review the following information:
00016 ** http://www.trolltech.com/products/qt/licensing.html or contact the
00017 ** sales department at sales@trolltech.com.
00018 **
00019 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00020 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021 **
00022 ****************************************************************************/
00023 
00024 #include "glpixwidget.h"
00025 
00026 #include <QtGui/QImage>
00027 
00028 #include <math.h>
00029 
00030 static GLint cubeArray[][3] = {
00031     {0, 0, 0}, {0, 1, 0}, {1, 1, 0}, {1, 0, 0},
00032     {0, 0, 1}, {1, 0, 1}, {1, 1, 1}, {0, 1, 1},
00033     {0, 0, 0}, {1, 0, 0}, {1, 0, 1}, {0, 0, 1},
00034     {0, 1, 0}, {0, 1, 1}, {1, 1, 1}, {1, 1, 0},
00035     {0, 1, 0}, {0, 0, 0}, {0, 0, 1}, {0, 1, 1},
00036     {1, 0, 0}, {1, 1, 0}, {1, 1, 1}, {1, 0, 1}
00037 };
00038 
00039 static GLint cubeTextureArray[][2] = {
00040     {0, 0}, {1, 0}, {1, 1}, {0, 1},
00041     {0, 0}, {0, 1}, {1, 1}, {1, 0},
00042     {0, 0}, {1, 0}, {1, 1}, {0, 1},
00043     {1, 0}, {0, 0}, {0, 1}, {1, 1},
00044     {0, 0}, {1, 0}, {1, 1}, {0, 1},
00045     {1, 0}, {0, 0}, {0, 1}, {1, 1}
00046 };
00047 
00048 static GLint faceArray[][2] = {
00049     {1, -1}, {1, 1}, {-1, 1}, {-1, -1}
00050 };
00051 
00052 static GLubyte colorArray[][4] = {
00053     {170, 202, 0, 255},
00054     {120, 143, 0, 255},
00055     {83, 102, 0, 255},
00056     {120, 143, 0, 255}
00057 };
00058 
00059 GLPixelWidget::GLPixelWidget(QWidget *parent)
00060     : QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
00061 {
00062     // create the pbuffer
00063     pbuffer = new QGLPixelBuffer(QSize(512, 512), format(), this);
00064     timerId = startTimer(20);
00065     setWindowTitle(tr("OpenGL pbuffers"));
00066 }
00067 
00068 GLPixelWidget::~GLPixelWidget()
00069 {
00070     pbuffer->releaseFromDynamicTexture();
00071     glDeleteTextures(1, &dynamicTexture);
00072     glDeleteLists(pbufferList, 1);
00073     delete pbuffer;
00074 }
00075 
00076 void GLPixelWidget::initializeGL()
00077 {
00078     glMatrixMode(GL_MODELVIEW);
00079 
00080     glEnable(GL_CULL_FACE);
00081     initCommon();
00082     initPbuffer();
00083 
00084     for (int i = 0; i < 3; ++i) {
00085         yOffs[i] = 0.0f;
00086         xInc[i] = 0.005f;
00087         rot[i] = 0.0f;
00088     }
00089     xOffs[0]= 0.0f;
00090     xOffs[1]= 0.5f;
00091     xOffs[2]= 1.0f;
00092 
00093     cubeTexture = bindTexture(QImage(":res/cubelogo.png"));
00094 }
00095 
00096 void GLPixelWidget::resizeGL(int w, int h)
00097 {
00098     glViewport(0, 0, w, h);
00099     glMatrixMode(GL_PROJECTION);
00100     glLoadIdentity();
00101     float aspect = w/(float)(h ? h : 1);
00102     glFrustum(-aspect, aspect, -1, 1, 10, 100);
00103     glTranslatef(-0.5f, -0.5f, -0.5f);
00104     glTranslatef(0.0f, 0.0f, -15.0f);
00105 }
00106 
00107 void GLPixelWidget::paintGL()
00108 {
00109     // draw a spinning cube into the pbuffer..
00110     pbuffer->makeCurrent();
00111     glBindTexture(GL_TEXTURE_2D, cubeTexture);
00112     glCallList(pbufferList);
00113     glFlush();
00114 
00115 #if  defined(Q_WS_X11)
00116     // rendering directly to a texture is not supported on X11, unfortunately
00117     pbuffer->updateDynamicTexture(dynamicTexture);
00118 #endif
00119     // ..and use the pbuffer contents as a texture when rendering the
00120     // background and the bouncing cubes
00121     makeCurrent();
00122     glBindTexture(GL_TEXTURE_2D, dynamicTexture);
00123     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00124 
00125     // draw the background
00126     glMatrixMode(GL_MODELVIEW);
00127     glPushMatrix();
00128     glLoadIdentity();
00129     glMatrixMode(GL_PROJECTION);
00130     glPushMatrix();
00131     glLoadIdentity();
00132 
00133     glVertexPointer(2, GL_INT, 0, faceArray);
00134     glTranslatef(-1.2f, -0.8f, 0.0f);
00135     glScalef(0.2f, 0.2f, 0.2f);
00136     for (int y = 0; y < 5; ++y)
00137     {
00138         for (int x = 0; x < 5; ++x)
00139         {
00140             glTranslatef(2.0f, 0, 0);
00141             glColor4f(0.5, 0.5, 0.5, 1.0);
00142             glDrawArrays(GL_QUADS, 0, 4);
00143         }
00144         glTranslatef(-10.0f, 2.0f, 0);
00145     }
00146     glVertexPointer(3, GL_INT, 0, cubeArray);
00147 
00148     glPopMatrix();
00149     glMatrixMode(GL_MODELVIEW);
00150     glPopMatrix();
00151 
00152     // draw the bouncing cubes
00153     drawCube(0, 0.0f, 1.5f, 2.5f, 1.5f);
00154     drawCube(1, 1.0f, 2.0f, 2.5f, 2.0f);
00155     drawCube(2, 2.0f, 3.5f, 2.5f, 2.5f);
00156 }
00157 
00158 void GLPixelWidget::drawCube(int i, GLfloat z, GLfloat rotation, GLfloat jmp, GLfloat amp)
00159 {
00160     glMatrixMode(GL_MODELVIEW);
00161     glLoadIdentity();
00162     glTranslatef(xOffs[i], yOffs[i], z);
00163     glTranslatef(0.5f, 0.5f, 0.5f);
00164     GLfloat scale = 0.75 + i*(0.25f/2);
00165     glScalef(scale, scale, scale);
00166     glRotatef(rot[i], 1.0f, 1.0f, 1.0f);
00167     glTranslatef(-0.5f, -0.5f, -0.5f);
00168 
00169     glColor4f(1.0f, 1.0f, 1.0f, 0.8f);
00170     glDrawArrays(GL_QUADS, 0, 24);
00171 
00172     if (xOffs[i] > 1.0f || xOffs[i] < -1.0f)
00173     {
00174         xInc[i] = -xInc[i];
00175         xOffs[i] = xOffs[i] > 1.0f ? 1.0f : -1.0f;
00176     }
00177     xOffs[i] += xInc[i];
00178     yOffs[i] = qAbs(cos((-3.141592f * jmp) * xOffs[i]) * amp) - 1;
00179     rot[i] += rotation;
00180 }
00181 
00182 void GLPixelWidget::initCommon()
00183 {
00184     glEnableClientState(GL_VERTEX_ARRAY);
00185     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
00186     glVertexPointer(3, GL_INT, 0, cubeArray);
00187     glTexCoordPointer(2, GL_INT, 0, cubeTextureArray);
00188     glColorPointer(4, GL_UNSIGNED_BYTE, 0, colorArray);
00189 
00190     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00191     glEnable(GL_BLEND);
00192     glEnable(GL_TEXTURE_2D);
00193     glEnable(GL_DEPTH_TEST);
00194 
00195     glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
00196 }
00197 
00198 void GLPixelWidget::initPbuffer()
00199 {
00200     // set up the pbuffer context
00201     pbuffer->makeCurrent();
00202     initCommon();
00203 
00204     glViewport(0, 0, pbuffer->size().width(), pbuffer->size().height());
00205     glMatrixMode(GL_PROJECTION);
00206     glLoadIdentity();
00207     glOrtho(-1, 1, -1, 1, -99, 99);
00208     glTranslatef(-0.5f, -0.5f, 0.0f);
00209     glMatrixMode(GL_MODELVIEW);
00210     glLoadIdentity();
00211 
00212     pbufferList = glGenLists(1);
00213     glNewList(pbufferList, GL_COMPILE);
00214     {
00215         glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
00216 
00217         // draw cube background
00218         glPushMatrix();
00219         glLoadIdentity();
00220         glTranslatef(0.5f, 0.5f, -2.0f);
00221         glDisable(GL_TEXTURE_2D);
00222         glEnableClientState(GL_COLOR_ARRAY);
00223         glVertexPointer(2, GL_INT, 0, faceArray);
00224         glDrawArrays(GL_QUADS, 0, 4);
00225         glVertexPointer(3, GL_INT, 0, cubeArray);
00226         glDisableClientState(GL_COLOR_ARRAY);
00227         glEnable(GL_TEXTURE_2D);
00228         glPopMatrix();
00229 
00230         // draw cube
00231         glTranslatef(0.5f, 0.5f, 0.5f);
00232         glRotatef(3.0f, 1.0f, 1.0f, 1.0f);
00233         glTranslatef(-0.5f, -0.5f, -0.5f);
00234         glColor4f(0.9f, 0.9f, 0.9f, 1.0f);
00235         glDrawArrays(GL_QUADS, 0, 24);
00236     }
00237     glEndList();
00238     // generate a texture that has the same size/format as the pbuffer
00239     dynamicTexture = pbuffer->generateDynamicTexture();
00240 
00241     // bind the dynamic texture to the pbuffer - this is a no-op under X11
00242     pbuffer->bindToDynamicTexture(dynamicTexture);
00243     makeCurrent();
00244 }
00245 

Generated on Sun Oct 28 22:51:27 2007 for BBoB by  doxygen 1.5.3