diff options
author | Rhys Weatherley <rhys.weatherley@nokia.com> | 2009-09-11 06:37:53 (GMT) |
---|---|---|
committer | Rhys Weatherley <rhys.weatherley@nokia.com> | 2009-09-11 06:37:53 (GMT) |
commit | 4b1a48f212c7624e9c1976c9e7c92a08b4fb307d (patch) | |
tree | 662398b64c195bbc5f0b32b38a138875376a6d9d /examples | |
parent | f9243aac1e26ccf9f46cc1d7de5f57063a053211 (diff) | |
download | Qt-4b1a48f212c7624e9c1976c9e7c92a08b4fb307d.zip Qt-4b1a48f212c7624e9c1976c9e7c92a08b4fb307d.tar.gz Qt-4b1a48f212c7624e9c1976c9e7c92a08b4fb307d.tar.bz2 |
Port the textures example to OpenGL/ES 2.0
Reviewed-by: trustme
Diffstat (limited to 'examples')
-rw-r--r-- | examples/opengl/opengl.pro | 6 | ||||
-rw-r--r-- | examples/opengl/textures/glwidget.cpp | 110 | ||||
-rw-r--r-- | examples/opengl/textures/glwidget.h | 10 |
3 files changed, 91 insertions, 35 deletions
diff --git a/examples/opengl/opengl.pro b/examples/opengl/opengl.pro index b86e0ba..2cb8227 100644 --- a/examples/opengl/opengl.pro +++ b/examples/opengl/opengl.pro @@ -5,9 +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 - } + } + !contains(QT_CONFIG, opengles1cl) { + SUBDIRS += textures } } else { SUBDIRS = 2dpainting \ diff --git a/examples/opengl/textures/glwidget.cpp b/examples/opengl/textures/glwidget.cpp index 024133b..6efd31a 100644 --- a/examples/opengl/textures/glwidget.cpp +++ b/examples/opengl/textures/glwidget.cpp @@ -44,16 +44,6 @@ #include "glwidget.h" -class CubeObject -{ -public: - GLuint textures[6]; - QVector<QVector3D> vertices; - QVector<QVector2D> texCoords; - - void draw(); -}; - GLWidget::GLWidget(QWidget *parent, QGLWidget *shareWidget) : QGLWidget(parent, shareWidget) { @@ -61,12 +51,13 @@ GLWidget::GLWidget(QWidget *parent, QGLWidget *shareWidget) xRot = 0; yRot = 0; zRot = 0; - cube = 0; +#ifdef QT_OPENGL_ES_2 + program = 0; +#endif } GLWidget::~GLWidget() { - delete cube; } QSize GLWidget::minimumSizeHint() const @@ -99,19 +90,90 @@ void GLWidget::initializeGL() glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); +#ifndef QT_OPENGL_ES_2 glEnable(GL_TEXTURE_2D); +#endif + +#ifdef QT_OPENGL_ES_2 + +#define PROGRAM_VERTEX_ATTRIBUTE 0 +#define PROGRAM_TEXCOORD_ATTRIBUTE 1 + + QGLShader *vshader = new QGLShader(QGLShader::VertexShader, this); + const char *vsrc = + "attribute highp vec4 vertex;\n" + "attribute mediump vec4 texCoord;\n" + "varying mediump vec4 texc;\n" + "uniform mediump mat4 matrix;\n" + "void main(void)\n" + "{\n" + " gl_Position = matrix * vertex;\n" + " texc = texCoord;\n" + "}\n"; + vshader->compile(vsrc); + + QGLShader *fshader = new QGLShader(QGLShader::FragmentShader, this); + const char *fsrc = + "uniform sampler2D texture;\n" + "varying mediump vec4 texc;\n" + "void main(void)\n" + "{\n" + " gl_FragColor = texture2D(texture, texc.st);\n" + "}\n"; + fshader->compile(fsrc); + + program = new QGLShaderProgram(this); + program->addShader(vshader); + program->addShader(fshader); + program->bindAttributeLocation("vertex", PROGRAM_VERTEX_ATTRIBUTE); + program->bindAttributeLocation("texCoord", PROGRAM_TEXCOORD_ATTRIBUTE); + program->link(); + + program->enable(); + program->setUniformValue("texture", 0); + +#endif } void GLWidget::paintGL() { qglClearColor(clearColor); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + +#if !defined(QT_OPENGL_ES_2) + glLoadIdentity(); 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(); + + glVertexPointer(3, GL_FLOAT, 0, vertices.constData()); + glTexCoordPointer(2, GL_FLOAT, 0, texCoords.constData()); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + +#else + + QMatrix4x4 m; + m.ortho(-0.5f, +0.5f, +0.5f, -0.5f, 4.0f, 15.0f); + m.translate(0.0f, 0.0f, -10.0f); + m.rotate(xRot / 16.0f, 1.0f, 0.0f, 0.0f); + m.rotate(yRot / 16.0f, 0.0f, 1.0f, 0.0f); + m.rotate(zRot / 16.0f, 0.0f, 0.0f, 1.0f); + + program->setUniformValue("matrix", m); + program->setAttributeArray + (PROGRAM_VERTEX_ATTRIBUTE, vertices.constData()); + program->setAttributeArray + (PROGRAM_TEXCOORD_ATTRIBUTE, texCoords.constData()); + +#endif + + for (int i = 0; i < 6; ++i) { + glBindTexture(GL_TEXTURE_2D, textures[i]); + glDrawArrays(GL_TRIANGLE_FAN, i * 4, 4); + } } void GLWidget::resizeGL(int width, int height) @@ -119,6 +181,7 @@ void GLWidget::resizeGL(int width, int height) int side = qMin(width, height); glViewport((width - side) / 2, (height - side) / 2, side, side); +#if !defined(QT_OPENGL_ES_2) glMatrixMode(GL_PROJECTION); glLoadIdentity(); #ifndef QT_OPENGL_ES @@ -127,6 +190,7 @@ void GLWidget::resizeGL(int width, int height) glOrthof(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0); #endif glMatrixMode(GL_MODELVIEW); +#endif } void GLWidget::mousePressEvent(QMouseEvent *event) @@ -163,32 +227,18 @@ void GLWidget::makeObject() { { -1, -1, +1 }, { +1, -1, +1 }, { +1, +1, +1 }, { -1, +1, +1 } } }; - cube = new CubeObject(); - for (int j=0; j < 6; ++j) { - cube->textures[j] = bindTexture + textures[j] = bindTexture (QPixmap(QString(":/images/side%1.png").arg(j + 1)), GL_TEXTURE_2D); } for (int i = 0; i < 6; ++i) { for (int j = 0; j < 4; ++j) { - cube->texCoords.append + texCoords.append (QVector2D(j == 0 || j == 3, j == 0 || j == 1)); - cube->vertices.append + vertices.append (QVector3D(0.2 * coords[i][j][0], 0.2 * coords[i][j][1], 0.2 * coords[i][j][2])); } } } - -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 46507ce..4dcff3b 100644 --- a/examples/opengl/textures/glwidget.h +++ b/examples/opengl/textures/glwidget.h @@ -42,9 +42,10 @@ #ifndef GLWIDGET_H #define GLWIDGET_H +#include <QtGui> #include <QGLWidget> -class CubeObject; +class QGLShaderProgram; class GLWidget : public QGLWidget { @@ -78,7 +79,12 @@ private: int xRot; int yRot; int zRot; - CubeObject *cube; + GLuint textures[6]; + QVector<QVector3D> vertices; + QVector<QVector2D> texCoords; +#ifdef QT_OPENGL_ES_2 + QGLShaderProgram *program; +#endif }; #endif |