source: trunk/doc/src/snippets/code/src_opengl_qgl.cpp

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 4.8 KB
Line 
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]
42QGLFormat fmt;
43fmt.setAlpha(true);
44fmt.setStereo(true);
45QGLFormat::setDefaultFormat(fmt);
46//! [0]
47
48
49//! [1]
50QGLFormat fmt;
51fmt.setDoubleBuffer(false); // single buffer
52fmt.setDirectRendering(false); // software rendering
53MyGLWidget* myWidget = new MyGLWidget(fmt, ...);
54//! [1]
55
56
57//! [2]
58QGLFormat fmt;
59fmt.setOverlay(true);
60fmt.setStereo(true);
61MyGLWidget* myWidget = new MyGLWidget(fmt, ...);
62if (!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
74MyGLWidget::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]
87QApplication a(argc, argv);
88QGLFormat f;
89f.setDoubleBuffer(false);
90QGLFormat::setDefaultFormat(f);
91//! [4]
92
93
94//! [5]
95QGLFormat f = QGLFormat::defaultOverlayFormat();
96f.setDoubleBuffer(true);
97QGLFormat::setDefaultOverlayFormat(f);
98//! [5]
99
100
101//! [6]
102// ...continued from above
103MyGLWidget* myWidget = new MyGLWidget(QGLFormat(QGL::HasOverlay), ...);
104if (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]
116QGLContext *cx;
117// ...
118QGLFormat f;
119f.setStereo(true);
120cx->setFormat(f);
121if (!cx->create())
122 exit(); // no OpenGL support, or cannot render on the specified paintdevice
123if (!cx->format().stereo())
124 exit(); // could not create stereo context
125//! [7]
126
127
128//! [8]
129class MyGLDrawer : public QGLWidget
130{
131 Q_OBJECT // must include this if you use Qt signals/slots
132
133public:
134 MyGLDrawer(QWidget *parent)
135 : QGLWidget(parent) {}
136
137protected:
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]
Note: See TracBrowser for help on using the repository browser.