1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #include <QtGui>
|
---|
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"
|
---|
|
---|