From 79ad2c1d4f5205245c929be1c0db2c678d185038 Mon Sep 17 00:00:00 2001 From: Rhys Weatherley Date: Tue, 8 Sep 2009 12:23:31 +1000 Subject: Port examples/opengl/textures to OpenGL/ES 1.1 Reviewed-by: Sarah Smith --- examples/opengl/opengl.pro | 3 ++ examples/opengl/textures/glwidget.cpp | 74 ++++++++++++++++++++--------------- examples/opengl/textures/glwidget.h | 8 ++-- examples/opengl/textures/window.cpp | 4 +- 4 files changed, 51 insertions(+), 38 deletions(-) (limited to 'examples/opengl') diff --git a/examples/opengl/opengl.pro b/examples/opengl/opengl.pro index 567eb7bda6..b86e0ba28c 100644 --- a/examples/opengl/opengl.pro +++ b/examples/opengl/opengl.pro @@ -5,6 +5,9 @@ contains(QT_CONFIG, opengles1)|contains(QT_CONFIG, opengles1cl)|contains(QT_CONF SUBDIRS = hellogl_es2 } else { SUBDIRS = hellogl_es + !contains(QT_CONFIG, opengles1cl) { + SUBDIRS += textures + } } } else { SUBDIRS = 2dpainting \ diff --git a/examples/opengl/textures/glwidget.cpp b/examples/opengl/textures/glwidget.cpp index cfb99b034f..9c6d5eabcd 100644 --- a/examples/opengl/textures/glwidget.cpp +++ b/examples/opengl/textures/glwidget.cpp @@ -42,12 +42,17 @@ #include #include -#include - #include "glwidget.h" -GLuint GLWidget::sharedObject = 0; -int GLWidget::refCount = 0; +class CubeObject +{ +public: + GLuint textures[6]; + QVector vertices; + QVector 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); + } } diff --git a/examples/opengl/textures/glwidget.h b/examples/opengl/textures/glwidget.h index 68be8bc6c3..f793d6d12a 100644 --- a/examples/opengl/textures/glwidget.h +++ b/examples/opengl/textures/glwidget.h @@ -44,6 +44,8 @@ #include +class CubeObject; + class GLWidget : public QGLWidget { Q_OBJECT @@ -69,16 +71,14 @@ protected: void mouseReleaseEvent(QMouseEvent *event); private: - GLuint makeObject(); + void makeObject(); QColor clearColor; QPoint lastPos; int xRot; int yRot; int zRot; - - static GLuint sharedObject; - static int refCount; + CubeObject *cube; }; #endif diff --git a/examples/opengl/textures/window.cpp b/examples/opengl/textures/window.cpp index ea645127d9..9bd7931f1c 100644 --- a/examples/opengl/textures/window.cpp +++ b/examples/opengl/textures/window.cpp @@ -48,8 +48,6 @@ Window::Window() { QGridLayout *mainLayout = new QGridLayout; - glWidgets[0][0] = 0; - for (int i = 0; i < NumRows; ++i) { for (int j = 0; j < NumColumns; ++j) { QColor clearColor; @@ -57,7 +55,7 @@ Window::Window() / (NumRows * NumColumns - 1), 255, 63); - glWidgets[i][j] = new GLWidget(0, glWidgets[0][0]); + glWidgets[i][j] = new GLWidget(0, 0); glWidgets[i][j]->setClearColor(clearColor); glWidgets[i][j]->rotateBy(+42 * 16, +42 * 16, -21 * 16); mainLayout->addWidget(glWidgets[i][j], i, j); -- cgit v1.2.3