diff options
Diffstat (limited to 'examples/opengl/textures/glwidget.cpp')
-rw-r--r-- | examples/opengl/textures/glwidget.cpp | 82 |
1 files changed, 47 insertions, 35 deletions
diff --git a/examples/opengl/textures/glwidget.cpp b/examples/opengl/textures/glwidget.cpp index 83757aa..9c6d5ea 100644 --- a/examples/opengl/textures/glwidget.cpp +++ b/examples/opengl/textures/glwidget.cpp @@ -1,7 +1,6 @@ /**************************************************************************** ** ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. ** Contact: Nokia Corporation (qt-info@nokia.com) ** ** This file is part of the examples of the Qt Toolkit. @@ -21,9 +20,10 @@ ** ensure the GNU Lesser General Public License version 2.1 requirements ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.1, included in the file LGPL_EXCEPTION.txt in this +** package. ** ** If you have questions regarding the use of this file, please contact ** Nokia at qt-info@nokia.com. @@ -42,12 +42,17 @@ #include <QtGui> #include <QtOpenGL> -#include <math.h> - #include "glwidget.h" -GLuint GLWidget::sharedObject = 0; -int GLWidget::refCount = 0; +class CubeObject +{ +public: + GLuint textures[6]; + QVector<QVector3D> vertices; + QVector<QVector2D> texCoords; + + void draw(); +}; GLWidget::GLWidget(QWidget *parent, QGLWidget *shareWidget) : QGLWidget(parent, shareWidget) @@ -56,14 +61,12 @@ GLWidget::GLWidget(QWidget *parent, QGLWidget *shareWidget) xRot = 0; yRot = 0; zRot = 0; + cube = 0; } GLWidget::~GLWidget() { - if (--refCount == 0) { - makeCurrent(); - glDeleteLists(sharedObject, 1); - } + delete cube; } QSize GLWidget::minimumSizeHint() const @@ -92,9 +95,7 @@ void GLWidget::setClearColor(const QColor &color) void GLWidget::initializeGL() { - if (!sharedObject) - sharedObject = makeObject(); - ++refCount; + makeObject(); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); @@ -106,11 +107,11 @@ void GLWidget::paintGL() qglClearColor(clearColor); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); - glTranslated(0.0, 0.0, -10.0); - glRotated(xRot / 16.0, 1.0, 0.0, 0.0); - glRotated(yRot / 16.0, 0.0, 1.0, 0.0); - glRotated(zRot / 16.0, 0.0, 0.0, 1.0); - glCallList(sharedObject); + glTranslatef(0.0f, 0.0f, -10.0f); + glRotatef(xRot / 16.0f, 1.0f, 0.0f, 0.0f); + glRotatef(yRot / 16.0f, 0.0f, 1.0f, 0.0f); + glRotatef(zRot / 16.0f, 0.0f, 0.0f, 1.0f); + cube->draw(); } void GLWidget::resizeGL(int width, int height) @@ -120,7 +121,11 @@ void GLWidget::resizeGL(int width, int height) glMatrixMode(GL_PROJECTION); glLoadIdentity(); +#ifndef QT_OPENGL_ES glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0); +#else + glOrthof(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0); +#endif glMatrixMode(GL_MODELVIEW); } @@ -147,7 +152,7 @@ void GLWidget::mouseReleaseEvent(QMouseEvent * /* event */) emit clicked(); } -GLuint GLWidget::makeObject() +void GLWidget::makeObject() { static const int coords[6][4][3] = { { { +1, -1, -1 }, { -1, -1, -1 }, { -1, +1, -1 }, { +1, +1, -1 } }, @@ -158,25 +163,32 @@ GLuint GLWidget::makeObject() { { -1, -1, +1 }, { +1, -1, +1 }, { +1, +1, +1 }, { -1, +1, +1 } } }; + cube = new CubeObject(); - GLuint textures[6]; - for (int j=0; j < 6; ++j) - textures[j] = bindTexture(QPixmap(QString(":/images/side%1.png").arg(j + 1)), - GL_TEXTURE_2D); + for (int j=0; j < 6; ++j) { + cube->textures[j] = bindTexture + (QPixmap(QString(":/images/side%1.png").arg(j + 1)), GL_TEXTURE_2D); + } - GLuint list = glGenLists(1); - glNewList(list, GL_COMPILE); for (int i = 0; i < 6; ++i) { - glBindTexture(GL_TEXTURE_2D, textures[i]); - glBegin(GL_QUADS); for (int j = 0; j < 4; ++j) { - glTexCoord2d(j == 0 || j == 3, j == 0 || j == 1); - glVertex3d(0.2 * coords[i][j][0], 0.2 * coords[i][j][1], - 0.2 * coords[i][j][2]); + cube->texCoords.append + (QVector2D(j == 0 || j == 3, j == 0 || j == 1)); + cube->vertices.append + (QVector3D(0.2 * coords[i][j][0], 0.2 * coords[i][j][1], + 0.2 * coords[i][j][2])); } - glEnd(); } +} - glEndList(); - return list; +void CubeObject::draw() +{ + glVertexPointer(3, GL_FLOAT, 0, vertices.constData()); + glTexCoordPointer(2, GL_FLOAT, 0, texCoords.constData()); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + for (int i = 0; i < 6; ++i) { + glBindTexture(GL_TEXTURE_2D, textures[i]); + glDrawArrays(GL_TRIANGLE_FAN, i * 4, 4); + } } |