1 | //! [0]
|
---|
2 | QGLFormat fmt;
|
---|
3 | fmt.setAlpha(true);
|
---|
4 | fmt.setStereo(true);
|
---|
5 | QGLFormat::setDefaultFormat(fmt);
|
---|
6 | //! [0]
|
---|
7 |
|
---|
8 |
|
---|
9 | //! [1]
|
---|
10 | QGLFormat fmt;
|
---|
11 | fmt.setDoubleBuffer(false); // single buffer
|
---|
12 | fmt.setDirectRendering(false); // software rendering
|
---|
13 | MyGLWidget* myWidget = new MyGLWidget(fmt, ...);
|
---|
14 | //! [1]
|
---|
15 |
|
---|
16 |
|
---|
17 | //! [2]
|
---|
18 | QGLFormat fmt;
|
---|
19 | fmt.setOverlay(true);
|
---|
20 | fmt.setStereo(true);
|
---|
21 | MyGLWidget* myWidget = new MyGLWidget(fmt, ...);
|
---|
22 | if (!myWidget->format().stereo()) {
|
---|
23 | // ok, goggles off
|
---|
24 | if (!myWidget->format().hasOverlay()) {
|
---|
25 | qFatal("Cool hardware required");
|
---|
26 | }
|
---|
27 | }
|
---|
28 | //! [2]
|
---|
29 |
|
---|
30 |
|
---|
31 | //! [3]
|
---|
32 | // The rendering in MyGLWidget depends on using
|
---|
33 | // stencil buffer and alpha channel
|
---|
34 | MyGLWidget::MyGLWidget(QWidget* parent)
|
---|
35 | : QGLWidget(QGLFormat(QGL::StencilBuffer | QGL::AlphaChannel), parent)
|
---|
36 | {
|
---|
37 | if (!format().stencil())
|
---|
38 | qWarning("Could not get stencil buffer; results will be suboptimal");
|
---|
39 | if (!format().alpha())
|
---|
40 | qWarning("Could not get alpha channel; results will be suboptimal");
|
---|
41 | ...
|
---|
42 | }
|
---|
43 | //! [3]
|
---|
44 |
|
---|
45 |
|
---|
46 | //! [4]
|
---|
47 | QApplication a(argc, argv);
|
---|
48 | QGLFormat f;
|
---|
49 | f.setDoubleBuffer(false);
|
---|
50 | QGLFormat::setDefaultFormat(f);
|
---|
51 | //! [4]
|
---|
52 |
|
---|
53 |
|
---|
54 | //! [5]
|
---|
55 | QGLFormat f = QGLFormat::defaultOverlayFormat();
|
---|
56 | f.setDoubleBuffer(true);
|
---|
57 | QGLFormat::setDefaultOverlayFormat(f);
|
---|
58 | //! [5]
|
---|
59 |
|
---|
60 |
|
---|
61 | //! [6]
|
---|
62 | // ...continued from above
|
---|
63 | MyGLWidget* myWidget = new MyGLWidget(QGLFormat(QGL::HasOverlay), ...);
|
---|
64 | if (myWidget->format().hasOverlay()) {
|
---|
65 | // Yes, we got an overlay, let's check _its_ format:
|
---|
66 | QGLContext* olContext = myWidget->overlayContext();
|
---|
67 | if (olContext->format().doubleBuffer())
|
---|
68 | ; // yes, we got a double buffered overlay
|
---|
69 | else
|
---|
70 | ; // no, only single buffered overlays are available
|
---|
71 | }
|
---|
72 | //! [6]
|
---|
73 |
|
---|
74 |
|
---|
75 | //! [7]
|
---|
76 | QGLContext *cx;
|
---|
77 | // ...
|
---|
78 | QGLFormat f;
|
---|
79 | f.setStereo(true);
|
---|
80 | cx->setFormat(f);
|
---|
81 | if (!cx->create())
|
---|
82 | exit(); // no OpenGL support, or cannot render on the specified paintdevice
|
---|
83 | if (!cx->format().stereo())
|
---|
84 | exit(); // could not create stereo context
|
---|
85 | //! [7]
|
---|
86 |
|
---|
87 |
|
---|
88 | //! [8]
|
---|
89 | class MyGLDrawer : public QGLWidget
|
---|
90 | {
|
---|
91 | Q_OBJECT // must include this if you use Qt signals/slots
|
---|
92 |
|
---|
93 | public:
|
---|
94 | MyGLDrawer(QWidget *parent)
|
---|
95 | : QGLWidget(parent) {}
|
---|
96 |
|
---|
97 | protected:
|
---|
98 |
|
---|
99 | void initializeGL()
|
---|
100 | {
|
---|
101 | // Set up the rendering context, define display lists etc.:
|
---|
102 | ...
|
---|
103 | glClearColor(0.0, 0.0, 0.0, 0.0);
|
---|
104 | glEnable(GL_DEPTH_TEST);
|
---|
105 | ...
|
---|
106 | }
|
---|
107 |
|
---|
108 | void resizeGL(int w, int h)
|
---|
109 | {
|
---|
110 | // setup viewport, projection etc.:
|
---|
111 | glViewport(0, 0, (GLint)w, (GLint)h);
|
---|
112 | ...
|
---|
113 | glFrustum(...);
|
---|
114 | ...
|
---|
115 | }
|
---|
116 |
|
---|
117 | void paintGL()
|
---|
118 | {
|
---|
119 | // draw the scene:
|
---|
120 | ...
|
---|
121 | glRotatef(...);
|
---|
122 | glMaterialfv(...);
|
---|
123 | glBegin(GL_QUADS);
|
---|
124 | glVertex3f(...);
|
---|
125 | glVertex3f(...);
|
---|
126 | ...
|
---|
127 | glEnd();
|
---|
128 | ...
|
---|
129 | }
|
---|
130 |
|
---|
131 | };
|
---|
132 | //! [8]
|
---|