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 "glwidget.h"
|
---|
42 | #include <QtGui/QImage>
|
---|
43 |
|
---|
44 | #include <math.h>
|
---|
45 |
|
---|
46 | static GLint cubeArray[][3] = {
|
---|
47 | {0, 0, 0}, {0, 1, 0}, {1, 1, 0}, {1, 0, 0},
|
---|
48 | {0, 0, 1}, {1, 0, 1}, {1, 1, 1}, {0, 1, 1},
|
---|
49 | {0, 0, 0}, {1, 0, 0}, {1, 0, 1}, {0, 0, 1},
|
---|
50 | {0, 1, 0}, {0, 1, 1}, {1, 1, 1}, {1, 1, 0},
|
---|
51 | {0, 1, 0}, {0, 0, 0}, {0, 0, 1}, {0, 1, 1},
|
---|
52 | {1, 0, 0}, {1, 1, 0}, {1, 1, 1}, {1, 0, 1}
|
---|
53 | };
|
---|
54 |
|
---|
55 | static GLint cubeTextureArray[][2] = {
|
---|
56 | {0, 0}, {1, 0}, {1, 1}, {0, 1},
|
---|
57 | {0, 0}, {0, 1}, {1, 1}, {1, 0},
|
---|
58 | {0, 0}, {1, 0}, {1, 1}, {0, 1},
|
---|
59 | {1, 0}, {0, 0}, {0, 1}, {1, 1},
|
---|
60 | {0, 0}, {1, 0}, {1, 1}, {0, 1},
|
---|
61 | {1, 0}, {0, 0}, {0, 1}, {1, 1}
|
---|
62 | };
|
---|
63 |
|
---|
64 | static GLint faceArray[][2] = {
|
---|
65 | {1, -1}, {1, 1}, {-1, 1}, {-1, -1}
|
---|
66 | };
|
---|
67 |
|
---|
68 | static GLubyte colorArray[][4] = {
|
---|
69 | {102, 176, 54, 255},
|
---|
70 | {81, 141, 41, 255},
|
---|
71 | {62, 108, 32, 255},
|
---|
72 | {45, 79, 23, 255}
|
---|
73 | };
|
---|
74 |
|
---|
75 | GLWidget::GLWidget(QWidget *parent)
|
---|
76 | : QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
|
---|
77 | {
|
---|
78 | // create the framebuffer object - make sure to have a current
|
---|
79 | // context before creating it
|
---|
80 | makeCurrent();
|
---|
81 | fbo = new QGLFramebufferObject(512, 512);
|
---|
82 | timerId = startTimer(20);
|
---|
83 | setWindowTitle(tr("OpenGL framebuffer objects 2"));
|
---|
84 | }
|
---|
85 |
|
---|
86 | GLWidget::~GLWidget()
|
---|
87 | {
|
---|
88 | glDeleteLists(pbufferList, 1);
|
---|
89 | delete fbo;
|
---|
90 | }
|
---|
91 |
|
---|
92 | void GLWidget::initializeGL()
|
---|
93 | {
|
---|
94 | glMatrixMode(GL_MODELVIEW);
|
---|
95 |
|
---|
96 | glEnable(GL_CULL_FACE);
|
---|
97 |
|
---|
98 | glEnableClientState(GL_VERTEX_ARRAY);
|
---|
99 | glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
100 | glVertexPointer(3, GL_INT, 0, cubeArray);
|
---|
101 | glTexCoordPointer(2, GL_INT, 0, cubeTextureArray);
|
---|
102 | glColorPointer(4, GL_UNSIGNED_BYTE, 0, colorArray);
|
---|
103 |
|
---|
104 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
---|
105 | glEnable(GL_BLEND);
|
---|
106 | glEnable(GL_TEXTURE_2D);
|
---|
107 | glEnable(GL_DEPTH_TEST);
|
---|
108 |
|
---|
109 | glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
|
---|
110 | pbufferList = glGenLists(1);
|
---|
111 | glNewList(pbufferList, GL_COMPILE);
|
---|
112 | {
|
---|
113 | glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
|
---|
114 |
|
---|
115 | // draw cube background
|
---|
116 | glPushMatrix();
|
---|
117 | glLoadIdentity();
|
---|
118 | glTranslatef(0.5f, 0.5f, -2.0f);
|
---|
119 | glDisable(GL_TEXTURE_2D);
|
---|
|
---|