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

Last change on this file since 244 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 2.8 KB
Line 
1//! [0]
2QGLFormat fmt;
3fmt.setAlpha(true);
4fmt.setStereo(true);
5QGLFormat::setDefaultFormat(fmt);
6//! [0]
7
8
9//! [1]
10QGLFormat fmt;
11fmt.setDoubleBuffer(false); // single buffer
12fmt.setDirectRendering(false); // software rendering
13MyGLWidget* myWidget = new MyGLWidget(fmt, ...);
14//! [1]
15
16
17//! [2]
18QGLFormat fmt;
19fmt.setOverlay(true);
20fmt.setStereo(true);
21MyGLWidget* myWidget = new MyGLWidget(fmt, ...);
22if (!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
34MyGLWidget::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]
47QApplication a(argc, argv);
48QGLFormat f;
49f.setDoubleBuffer(false);
50QGLFormat::setDefaultFormat(f);
51//! [4]
52
53
54//! [5]
55QGLFormat f = QGLFormat::defaultOverlayFormat();
56f.setDoubleBuffer(true);
57QGLFormat::setDefaultOverlayFormat(f);
58//! [5]
59
60
61//! [6]
62// ...continued from above
63MyGLWidget* myWidget = new MyGLWidget(QGLFormat(QGL::HasOverlay), ...);
64if (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]
76QGLContext *cx;
77// ...
78QGLFormat f;
79f.setStereo(true);
80cx->setFormat(f);
81if (!cx->create())
82 exit(); // no OpenGL support, or cannot render on the specified paintdevice
83if (!cx->format().stereo())
84 exit(); // could not create stereo context
85//! [7]
86
87
88//! [8]
89class MyGLDrawer : public QGLWidget
90{
91 Q_OBJECT // must include this if you use Qt signals/slots
92
93public:
94 MyGLDrawer(QWidget *parent)
95 : QGLWidget(parent) {}
96
97protected:
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]
Note: See TracBrowser for help on using the repository browser.