1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 **   * Redistributions of source code must retain the above copyright
25 **     notice, this list of conditions and the following disclaimer.
26 **   * Redistributions in binary form must reproduce the above copyright
27 **     notice, this list of conditions and the following disclaimer in
28 **     the documentation and/or other materials provided with the
29 **     distribution.
30 **   * Neither the name of The Qt Company Ltd nor the names of its
31 **     contributors may be used to endorse or promote products derived
32 **     from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
51 //! [0]
52 QGLFormat fmt;
53 fmt.setAlpha(true);
54 fmt.setStereo(true);
55 QGLFormat::setDefaultFormat(fmt);
56 //! [0]
57 
58 
59 //! [1]
60 QGLFormat fmt;
61 fmt.setDoubleBuffer(false);                 // single buffer
62 fmt.setDirectRendering(false);              // software rendering
63 MyGLWidget* myWidget = new MyGLWidget(fmt, ...);
64 //! [1]
65 
66 
67 //! [2]
68 QGLFormat fmt;
69 fmt.setOverlay(true);
70 fmt.setStereo(true);
71 MyGLWidget* myWidget = new MyGLWidget(fmt, ...);
72 if (!myWidget->format().stereo()) {
73     // ok, goggles off
74     if (!myWidget->format().hasOverlay()) {
75         qFatal("Cool hardware required");
76     }
77 }
78 //! [2]
79 
80 
81 //! [3]
82 // The rendering in MyGLWidget depends on using
83 // stencil buffer and alpha channel
MyGLWidget(QWidget * parent)84 MyGLWidget::MyGLWidget(QWidget* parent)
85     : QGLWidget(QGLFormat(QGL::StencilBuffer | QGL::AlphaChannel), parent)
86 {
87     if (!format().stencil())
88         qWarning("Could not get stencil buffer; results will be suboptimal");
89     if (!format().alpha())
90         qWarning("Could not get alpha channel; results will be suboptimal");
91     ...
92 }
93 //! [3]
94 
95 
96 //! [4]
97 QApplication a(argc, argv);
98 QGLFormat f;
99 f.setDoubleBuffer(false);
100 QGLFormat::setDefaultFormat(f);
101 //! [4]
102 
103 
104 //! [5]
105 QGLFormat f = QGLFormat::defaultOverlayFormat();
106 f.setDoubleBuffer(true);
107 QGLFormat::setDefaultOverlayFormat(f);
108 //! [5]
109 
110 
111 //! [6]
112 // ...continued from above
113 MyGLWidget* myWidget = new MyGLWidget(QGLFormat(QGL::HasOverlay), ...);
114 if (myWidget->format().hasOverlay()) {
115     // Yes, we got an overlay, let's check _its_ format:
116     QGLContext* olContext = myWidget->overlayContext();
117     if (olContext->format().doubleBuffer())
118         ; // yes, we got a double buffered overlay
119     else
120         ; // no, only single buffered overlays are available
121 }
122 //! [6]
123 
124 
125 //! [7]
126 QGLContext *cx;
127 //  ...
128 QGLFormat f;
129 f.setStereo(true);
130 cx->setFormat(f);
131 if (!cx->create())
132     exit(); // no OpenGL support, or cannot render on the specified paintdevice
133 if (!cx->format().stereo())
134     exit(); // could not create stereo context
135 //! [7]
136 
137 
138 //! [8]
139 class MyGLDrawer : public QGLWidget
140 {
141     Q_OBJECT        // must include this if you use Qt signals/slots
142 
143 public:
MyGLDrawer(QWidget * parent)144     MyGLDrawer(QWidget *parent)
145         : QGLWidget(parent) {}
146 
147 protected:
148 
initializeGL()149     void initializeGL() override
150     {
151         // Set up the rendering context, define display lists etc.:
152         ...
153         glClearColor(0.0, 0.0, 0.0, 0.0);
154         glEnable(GL_DEPTH_TEST);
155         ...
156     }
157 
resizeGL(int w,int h)158     void resizeGL(int w, int h) override
159     {
160         // setup viewport, projection etc.:
161         glViewport(0, 0, (GLint)w, (GLint)h);
162         ...
163         glFrustum(...);
164         ...
165     }
166 
paintGL()167     void paintGL() override
168     {
169         // draw the scene:
170         ...
171         glRotatef(...);
172         glMaterialfv(...);
173         glBegin(GL_QUADS);
174         glVertex3f(...);
175         glVertex3f(...);
176         ...
177         glEnd();
178         ...
179     }
180 
181 };
182 //! [8]
183