~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to examples/opengl/textures/glwidget.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the examples of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:BSD$
 
9
** You may use this file under the terms of the BSD license as follows:
 
10
**
 
11
** "Redistribution and use in source and binary forms, with or without
 
12
** modification, are permitted provided that the following conditions are
 
13
** met:
 
14
**   * Redistributions of source code must retain the above copyright
 
15
**     notice, this list of conditions and the following disclaimer.
 
16
**   * Redistributions in binary form must reproduce the above copyright
 
17
**     notice, this list of conditions and the following disclaimer in
 
18
**     the documentation and/or other materials provided with the
 
19
**     distribution.
 
20
**   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
 
21
**     of its contributors may be used to endorse or promote products derived
 
22
**     from this software without specific prior written permission.
 
23
**
 
24
**
 
25
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
26
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
27
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
28
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
29
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
30
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
31
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
32
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
33
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
34
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
35
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
 
36
**
 
37
** $QT_END_LICENSE$
 
38
**
 
39
****************************************************************************/
 
40
 
 
41
#include <QtWidgets>
 
42
#include <QtOpenGL>
 
43
 
 
44
#include "glwidget.h"
 
45
 
 
46
GLWidget::GLWidget(QWidget *parent, QGLWidget *shareWidget)
 
47
    : QGLWidget(parent, shareWidget)
 
48
{
 
49
    clearColor = Qt::black;
 
50
    xRot = 0;
 
51
    yRot = 0;
 
52
    zRot = 0;
 
53
#ifdef QT_OPENGL_ES_2
 
54
    program = 0;
 
55
#endif
 
56
}
 
57
 
 
58
GLWidget::~GLWidget()
 
59
{
 
60
}
 
61
 
 
62
QSize GLWidget::minimumSizeHint() const
 
63
{
 
64
    return QSize(50, 50);
 
65
}
 
66
 
 
67
QSize GLWidget::sizeHint() const
 
68
{
 
69
    return QSize(200, 200);
 
70
}
 
71
 
 
72
void GLWidget::rotateBy(int xAngle, int yAngle, int zAngle)
 
73
{
 
74
    xRot += xAngle;
 
75
    yRot += yAngle;
 
76
    zRot += zAngle;
 
77
    updateGL();
 
78
}
 
79
 
 
80
void GLWidget::setClearColor(const QColor &color)
 
81
{
 
82
    clearColor = color;
 
83
    updateGL();
 
84
}
 
85
 
 
86
void GLWidget::initializeGL()
 
87
{
 
88
    makeObject();
 
89
 
 
90
    glEnable(GL_DEPTH_TEST);
 
91
    glEnable(GL_CULL_FACE);
 
92
#ifndef QT_OPENGL_ES_2
 
93
    glEnable(GL_TEXTURE_2D);
 
94
#endif
 
95
 
 
96
#ifdef QT_OPENGL_ES_2
 
97
 
 
98
#define PROGRAM_VERTEX_ATTRIBUTE 0
 
99
#define PROGRAM_TEXCOORD_ATTRIBUTE 1
 
100
 
 
101
    QGLShader *vshader = new QGLShader(QGLShader::Vertex, this);
 
102
    const char *vsrc =
 
103
        "attribute highp vec4 vertex;\n"
 
104
        "attribute mediump vec4 texCoord;\n"
 
105
        "varying mediump vec4 texc;\n"
 
106
        "uniform mediump mat4 matrix;\n"
 
107
        "void main(void)\n"
 
108
        "{\n"
 
109
        "    gl_Position = matrix * vertex;\n"
 
110
        "    texc = texCoord;\n"
 
111
        "}\n";
 
112
    vshader->compileSourceCode(vsrc);
 
113
 
 
114
    QGLShader *fshader = new QGLShader(QGLShader::Fragment, this);
 
115
    const char *fsrc =
 
116
        "uniform sampler2D texture;\n"
 
117
        "varying mediump vec4 texc;\n"
 
118
        "void main(void)\n"
 
119
        "{\n"
 
120
        "    gl_FragColor = texture2D(texture, texc.st);\n"
 
121
        "}\n";
 
122
    fshader->compileSourceCode(fsrc);
 
123
 
 
124
    program = new QGLShaderProgram(this);
 
125
    program->addShader(vshader);
 
126
    program->addShader(fshader);
 
127
    program->bindAttributeLocation("vertex", PROGRAM_VERTEX_ATTRIBUTE);
 
128
    program->bindAttributeLocation("texCoord", PROGRAM_TEXCOORD_ATTRIBUTE);
 
129
    program->link();
 
130
 
 
131
    program->bind();
 
132
    program->setUniformValue("texture", 0);
 
133
 
 
134
#endif
 
135
}
 
136
 
 
137
void GLWidget::paintGL()
 
138
{
 
139
    qglClearColor(clearColor);
 
140
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
141
 
 
142
#if !defined(QT_OPENGL_ES_2)
 
143
 
 
144
    glLoadIdentity();
 
145
    glTranslatef(0.0f, 0.0f, -10.0f);
 
146
    glRotatef(xRot / 16.0f, 1.0f, 0.0f, 0.0f);
 
147
    glRotatef(yRot / 16.0f, 0.0f, 1.0f, 0.0f);
 
148
    glRotatef(zRot / 16.0f, 0.0f, 0.0f, 1.0f);
 
149
 
 
150
    glVertexPointer(3, GL_FLOAT, 0, vertices.constData());
 
151
    glTexCoordPointer(2, GL_FLOAT, 0, texCoords.constData());
 
152
    glEnableClientState(GL_VERTEX_ARRAY);
 
153
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 
154
 
 
155
#else
 
156
 
 
157
    QMatrix4x4 m;
 
158
    m.ortho(-0.5f, +0.5f, +0.5f, -0.5f, 4.0f, 15.0f);
 
159
    m.translate(0.0f, 0.0f, -10.0f);
 
160
    m.rotate(xRot / 16.0f, 1.0f, 0.0f, 0.0f);
 
161
    m.rotate(yRot / 16.0f, 0.0f, 1.0f, 0.0f);
 
162
    m.rotate(zRot / 16.0f, 0.0f, 0.0f, 1.0f);
 
163
 
 
164
    program->setUniformValue("matrix", m);
 
165
    program->enableAttributeArray(PROGRAM_VERTEX_ATTRIBUTE);
 
166
    program->enableAttributeArray(PROGRAM_TEXCOORD_ATTRIBUTE);
 
167
    program->setAttributeArray
 
168
        (PROGRAM_VERTEX_ATTRIBUTE, vertices.constData());
 
169
    program->setAttributeArray
 
170
        (PROGRAM_TEXCOORD_ATTRIBUTE, texCoords.constData());
 
171
 
 
172
#endif
 
173
 
 
174
    for (int i = 0; i < 6; ++i) {
 
175
        glBindTexture(GL_TEXTURE_2D, textures[i]);
 
176
        glDrawArrays(GL_TRIANGLE_FAN, i * 4, 4);
 
177
    }
 
178
}
 
179
 
 
180
void GLWidget::resizeGL(int width, int height)
 
181
{
 
182
    int side = qMin(width, height);
 
183
    glViewport((width - side) / 2, (height - side) / 2, side, side);
 
184
 
 
185
#if !defined(QT_OPENGL_ES_2)
 
186
    glMatrixMode(GL_PROJECTION);
 
187
    glLoadIdentity();
 
188
#ifndef QT_OPENGL_ES
 
189
    glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0);
 
190
#else
 
191
    glOrthof(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0);
 
192
#endif
 
193
    glMatrixMode(GL_MODELVIEW);
 
194
#endif
 
195
}
 
196
 
 
197
void GLWidget::mousePressEvent(QMouseEvent *event)
 
198
{
 
199
    lastPos = event->pos();
 
200
}
 
201
 
 
202
void GLWidget::mouseMoveEvent(QMouseEvent *event)
 
203
{
 
204
    int dx = event->x() - lastPos.x();
 
205
    int dy = event->y() - lastPos.y();
 
206
 
 
207
    if (event->buttons() & Qt::LeftButton) {
 
208
        rotateBy(8 * dy, 8 * dx, 0);
 
209
    } else if (event->buttons() & Qt::RightButton) {
 
210
        rotateBy(8 * dy, 0, 8 * dx);
 
211
    }
 
212
    lastPos = event->pos();
 
213
}
 
214
 
 
215
void GLWidget::mouseReleaseEvent(QMouseEvent * /* event */)
 
216
{
 
217
    emit clicked();
 
218
}
 
219
 
 
220
void GLWidget::makeObject()
 
221
{
 
222
    static const int coords[6][4][3] = {
 
223
        { { +1, -1, -1 }, { -1, -1, -1 }, { -1, +1, -1 }, { +1, +1, -1 } },
 
224
        { { +1, +1, -1 }, { -1, +1, -1 }, { -1, +1, +1 }, { +1, +1, +1 } },
 
225
        { { +1, -1, +1 }, { +1, -1, -1 }, { +1, +1, -1 }, { +1, +1, +1 } },
 
226
        { { -1, -1, -1 }, { -1, -1, +1 }, { -1, +1, +1 }, { -1, +1, -1 } },
 
227
        { { +1, -1, +1 }, { -1, -1, +1 }, { -1, -1, -1 }, { +1, -1, -1 } },
 
228
        { { -1, -1, +1 }, { +1, -1, +1 }, { +1, +1, +1 }, { -1, +1, +1 } }
 
229
    };
 
230
 
 
231
    for (int j=0; j < 6; ++j) {
 
232
        textures[j] = bindTexture
 
233
            (QPixmap(QString(":/images/side%1.png").arg(j + 1)), GL_TEXTURE_2D);
 
234
    }
 
235
 
 
236
    for (int i = 0; i < 6; ++i) {
 
237
        for (int j = 0; j < 4; ++j) {
 
238
            texCoords.append
 
239
                (QVector2D(j == 0 || j == 3, j == 0 || j == 1));
 
240
            vertices.append
 
241
                (QVector3D(0.2 * coords[i][j][0], 0.2 * coords[i][j][1],
 
242
                           0.2 * coords[i][j][2]));
 
243
        }
 
244
    }
 
245
}