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 documentation 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 | //! [0]
|
---|
42 | QGLFormat fmt;
|
---|
43 | fmt.setAlpha(true);
|
---|
44 | fmt.setStereo(true);
|
---|
45 | QGLFormat::setDefaultFormat(fmt);
|
---|
46 | //! [0]
|
---|
47 |
|
---|
48 |
|
---|
49 | //! [1]
|
---|
50 | QGLFormat fmt;
|
---|
51 | fmt.setDoubleBuffer(false); // single buffer
|
---|
52 | fmt.setDirectRendering(false); // software rendering
|
---|
53 | MyGLWidget* myWidget = new MyGLWidget(fmt, ...);
|
---|
54 | //! [1]
|
---|
55 |
|
---|
56 |
|
---|
57 | //! [2]
|
---|
58 | QGLFormat fmt;
|
---|
59 | fmt.setOverlay(true);
|
---|
60 | fmt.setStereo(true);
|
---|
61 | MyGLWidget* myWidget = new MyGLWidget(fmt, ...);
|
---|
62 | if (!myWidget->format().stereo()) {
|
---|
63 | // ok, goggles off
|
---|
64 | if (!myWidget->format().hasOverlay()) {
|
---|
65 | qFatal("Cool hardware required");
|
---|
66 | }
|
---|
67 | }
|
---|
68 | //! [2]
|
---|
69 |
|
---|
70 |
|
---|
71 | //! [3]
|
---|
72 | // The rendering in MyGLWidget depends on using
|
---|
73 | // stencil buffer and alpha channel
|
---|
74 | MyGLWidget::MyGLWidget(QWidget* parent)
|
---|
75 | : QGLWidget(QGLFormat(QGL::StencilBuffer | QGL::AlphaChannel), parent)
|
---|
76 | {
|
---|
77 | if (!format().stencil())
|
---|
78 | qWarning("Could not get stencil buffer; results will be suboptimal");
|
---|
79 | if (!format().alpha())
|
---|
80 | qWarning("Could not get alpha channel; results will be suboptimal");
|
---|
81 | ...
|
---|
82 | }
|
---|
83 | //! [3]
|
---|
84 |
|
---|
85 |
|
---|
86 | //! [4]
|
---|
87 | QApplication a(argc, argv);
|
---|
88 | QGLFormat f;
|
---|
89 | f.setDoubleBuffer(false);
|
---|
90 | QGLFormat::setDefaultFormat(f);
|
---|
91 | //! [4]
|
---|
92 |
|
---|
93 |
|
---|
94 | //! [5]
|
---|
95 | QGLFormat f = QGLFormat::defaultOverlayFormat();
|
---|
96 | f.setDoubleBuffer(true);
|
---|
97 | QGLFormat::setDefaultOverlayFormat(f);
|
---|
98 | //! [5]
|
---|
99 |
|
---|
100 |
|
---|
101 | //! [6]
|
---|
102 | // ...continued from above
|
---|
103 | MyGLWidget* myWidget = new MyGLWidget(QGLFormat(QGL::HasOverlay), ...);
|
---|
104 | if (myWidget->format().hasOverlay()) {
|
---|
105 | // Yes, we got an overlay, let's check _its_ format:
|
---|
106 | QGLContext* olContext = myWidget->overlayContext();
|
---|
107 | if (olContext->format().doubleBuffer())
|
---|
108 | ; // yes, we got a double buffered overlay
|
---|
109 | else
|
---|
110 | ; // no, only single buffered overlays are available
|
---|
111 | }
|
---|
112 | //! [6]
|
---|
113 |
|
---|
114 |
|
---|
115 | //! [7]
|
---|
116 | QGLContext *cx;
|
---|
117 | // ...
|
---|
118 | QGLFormat f;
|
---|
119 | f.setStereo(true);
|
---|
120 | cx->setFormat(f);
|
---|
121 | if (!cx->create())
|
---|
122 | exit(); // no OpenGL support, or cannot render on the specified paintdevice
|
---|
123 | if (!cx->format().stereo())
|
---|
124 | exit(); // could not create stereo context
|
---|
125 | //! [7]
|
---|
126 |
|
---|
127 |
|
---|
128 | //! [8]
|
---|
129 | class MyGLDrawer : public QGLWidget
|
---|
130 | {
|
---|
131 | Q_OBJECT // must include this if you use Qt signals/slots
|
---|
132 |
|
---|
133 | public:
|
---|
134 | MyGLDrawer(QWidget *parent)
|
---|
135 | : QGLWidget(parent) {}
|
---|
136 |
|
---|
137 | protected:
|
---|
138 |
|
---|
139 | void initializeGL()
|
---|
140 | {
|
---|
141 | // Set up the rendering context, define display lists etc.:
|
---|
142 | ...
|
---|
143 | glClearColor(0.0, 0.0, 0.0, 0.0);
|
---|
144 | glEnable(GL_DEPTH_TEST);
|
---|
145 | ...
|
---|
146 | }
|
---|
147 |
|
---|
148 | void resizeGL(int w, int h)
|
---|
149 | {
|
---|
150 | // setup viewport, projection etc.:
|
---|
151 | glViewport(0, 0, (GLint)w, (GLint)h);
|
---|
152 | ...
|
---|
153 | glFrustum(...);
|
---|
154 | ...
|
---|
155 | }
|
---|
156 |
|
---|
157 | void paintGL()
|
---|
158 | {
|
---|
159 | // draw the scene:
|
---|
160 | ...
|
---|
161 | glRotatef(...);
|
---|
162 | glMaterialfv(...);
|
---|
163 | glBegin(GL_QUADS);
|
---|
164 | glVertex3f(...);
|
---|
165 | glVertex3f(...);
|
---|
166 | ...
|
---|
167 | glEnd();
|
---|
168 | ...
|
---|
169 | }
|
---|
170 |
|
---|
171 | };
|
---|
172 | //! [8]
|
---|