| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the documentation of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial Usage | 
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and Nokia. | 
|---|
| 14 | ** | 
|---|
| 15 | ** GNU Lesser General Public License Usage | 
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 19 | ** packaging of this file.  Please review the following information to | 
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 22 | ** | 
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain | 
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL | 
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this | 
|---|
| 26 | ** package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please | 
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | /*! | 
|---|
| 43 | \example opengl/hellogl | 
|---|
| 44 | \title Hello GL Example | 
|---|
| 45 |  | 
|---|
| 46 | The Hello GL example demonstrates the basic use of the OpenGL-related classes | 
|---|
| 47 | provided with Qt. | 
|---|
| 48 |  | 
|---|
| 49 | \image hellogl-example.png | 
|---|
| 50 |  | 
|---|
| 51 | Qt provides the QGLWidget class to enable OpenGL graphics to be rendered within | 
|---|
| 52 | a standard application user interface. By subclassing this class, and providing | 
|---|
| 53 | reimplementations of event handler functions, 3D scenes can be displayed on | 
|---|
| 54 | widgets that can be placed in layouts, connected to other objects using signals | 
|---|
| 55 | and slots, and manipulated like any other widget. | 
|---|
| 56 |  | 
|---|
| 57 | \tableofcontents | 
|---|
| 58 |  | 
|---|
| 59 | \section1 GLWidget Class Definition | 
|---|
| 60 |  | 
|---|
| 61 | The \c GLWidget class contains some standard public definitions for the | 
|---|
| 62 | constructor, destructor, \l{QWidget::sizeHint()}{sizeHint()}, and | 
|---|
| 63 | \l{QWidget::minimumSizeHint()}{minimumSizeHint()} functions: | 
|---|
| 64 |  | 
|---|
| 65 | \snippet examples/opengl/hellogl/glwidget.h 0 | 
|---|
| 66 |  | 
|---|
| 67 | We use a destructor to ensure that any OpenGL-specific data structures | 
|---|
| 68 | are deleted when the widget is no longer needed. | 
|---|
| 69 |  | 
|---|
| 70 | \snippet examples/opengl/hellogl/glwidget.h 1 | 
|---|
| 71 |  | 
|---|
| 72 | The signals and slots are used to allow other objects to interact with the | 
|---|
| 73 | 3D scene. | 
|---|
| 74 |  | 
|---|
| 75 | \snippet examples/opengl/hellogl/glwidget.h 2 | 
|---|
| 76 |  | 
|---|
| 77 | OpenGL initialization, viewport resizing, and painting are handled by | 
|---|
| 78 | reimplementing the QGLWidget::initializeGL(), QGLWidget::resizeGL(), and | 
|---|
| 79 | QGLWidget::paintGL() handler functions. To enable the user to interact | 
|---|
| 80 | directly with the scene using the mouse, we reimplement | 
|---|
| 81 | QWidget::mousePressEvent() and QWidget::mouseMoveEvent(). | 
|---|
| 82 |  | 
|---|
| 83 | \snippet examples/opengl/hellogl/glwidget.h 3 | 
|---|
| 84 |  | 
|---|
| 85 | The rest of the class contains utility functions and variables that are | 
|---|
| 86 | used to construct and hold orientation information for the scene. The | 
|---|
| 87 | \c object variable will be used to hold an identifier for an OpenGL | 
|---|
| 88 | display list. | 
|---|
| 89 |  | 
|---|
| 90 | \section1 GLWidget Class Implementation | 
|---|
| 91 |  | 
|---|
| 92 | In this example, we split the class into groups of functions and describe | 
|---|
| 93 | them separately. This helps to illustrate the differences between subclasses | 
|---|
| 94 | of native widgets (such as QWidget and QFrame) and QGLWidget subclasses. | 
|---|
| 95 |  | 
|---|
| 96 | \section2 Widget Construction and Sizing | 
|---|
| 97 |  | 
|---|
| 98 | The constructor provides default rotation angles for the scene, initializes | 
|---|
| 99 | the variable used for the display list, and sets up some colors for later use. | 
|---|
| 100 |  | 
|---|
| 101 | \snippet examples/opengl/hellogl/glwidget.cpp 0 | 
|---|
| 102 |  | 
|---|
| 103 | We also implement a destructor to release OpenGL-related resources when the | 
|---|
| 104 | widget is deleted: | 
|---|
| 105 |  | 
|---|
| 106 | \snippet examples/opengl/hellogl/glwidget.cpp 1 | 
|---|
| 107 |  | 
|---|
| 108 | The destructor ensures that the display list is deleted properly. | 
|---|
| 109 |  | 
|---|
| 110 | We provide size hint functions to ensure that the widget is shown at a | 
|---|
| 111 | reasonable size: | 
|---|
| 112 |  | 
|---|
| 113 | \snippet examples/opengl/hellogl/glwidget.cpp 2 | 
|---|
| 114 | \codeline | 
|---|
| 115 | \snippet examples/opengl/hellogl/glwidget.cpp 3 | 
|---|
| 116 | \snippet examples/opengl/hellogl/glwidget.cpp 4 | 
|---|
| 117 |  | 
|---|
| 118 | The widget provides three slots that enable other components in the | 
|---|
| 119 | example to change the orientation of the scene: | 
|---|
| 120 |  | 
|---|
| 121 | \snippet examples/opengl/hellogl/glwidget.cpp 5 | 
|---|
| 122 |  | 
|---|
| 123 | In the above slot, the \c xRot variable is updated only if the new angle | 
|---|
| 124 | is different to the old one, the \c xRotationChanged() signal is emitted to | 
|---|
| 125 | allow other components to be updated, and the widget's | 
|---|
| 126 | \l{QGLWidget::updateGL()}{updateGL()} handler function is called. | 
|---|
| 127 |  | 
|---|
| 128 | The \c setYRotation() and \c setZRotation() slots perform the same task for | 
|---|
| 129 | rotations measured by the \c yRot and \c zRot variables. | 
|---|
| 130 |  | 
|---|
| 131 | \section2 OpenGL Initialization | 
|---|
| 132 |  | 
|---|
| 133 | The \l{QGLWidget::initializeGL()}{initializeGL()} function is used to | 
|---|
| 134 | perform useful initialization tasks that are needed to render the 3D scene. | 
|---|
| 135 | These often involve defining colors and materials, enabling and disabling | 
|---|
| 136 | certain rendering flags, and setting other properties used to customize the | 
|---|
| 137 | rendering process. | 
|---|
| 138 |  | 
|---|
| 139 | \snippet examples/opengl/hellogl/glwidget.cpp 6 | 
|---|
| 140 |  | 
|---|
| 141 | In this example, we reimplement the function to set the background color, | 
|---|
| 142 | create a display list containing information about the object we want to | 
|---|
| 143 | display, and set up the rendering process to use a particular shading model | 
|---|
| 144 | and rendering flags: | 
|---|
| 145 |  | 
|---|
| 146 | \section2 Resizing the Viewport | 
|---|
| 147 |  | 
|---|
| 148 | The \l{QGLWidget::resizeGL()}{resizeGL()} function is used to ensure that | 
|---|
| 149 | the OpenGL implementation renders the scene onto a viewport that matches the | 
|---|
| 150 | size of the widget, using the correct transformation from 3D coordinates to | 
|---|
| 151 | 2D viewport coordinates. | 
|---|
| 152 |  | 
|---|
| 153 | The function is called whenever the widget's dimensions change, and is | 
|---|
| 154 | supplied with the new width and height. Here, we define a square viewport | 
|---|
| 155 | based on the length of the smallest side of the widget to ensure that | 
|---|
| 156 | the scene is not distorted if the widget has sides of unequal length: | 
|---|
| 157 |  | 
|---|
| 158 | \snippet examples/opengl/hellogl/glwidget.cpp 8 | 
|---|
| 159 |  | 
|---|
| 160 | A discussion of the projection transformation used is outside the scope of | 
|---|
| 161 | this example. Please consult the OpenGL reference documentation for an | 
|---|
| 162 | explanation of projection matrices. | 
|---|
| 163 |  | 
|---|
| 164 | \section2 Painting the Scene | 
|---|
| 165 |  | 
|---|
| 166 | The \l{QGLWidget::paintGL()}{paintGL()} function is used to paint the | 
|---|
| 167 | contents of the scene onto the widget. For widgets that only need to be | 
|---|
| 168 | decorated with pure OpenGL content, we reimplement QGLWidget::paintGL() | 
|---|
| 169 | \e instead of reimplementing QWidget::paintEvent(): | 
|---|
| 170 |  | 
|---|
| 171 | \snippet examples/opengl/hellogl/glwidget.cpp 7 | 
|---|
| 172 |  | 
|---|
| 173 | In this example, we clear the widget using the background color that | 
|---|
| 174 | we defined in the \l{QGLWidget::initializeGL()}{initializeGL()} function, | 
|---|
| 175 | set up the frame of reference for the object we want to display, and call | 
|---|
| 176 | the display list containing the rendering commands for the object. | 
|---|
| 177 |  | 
|---|
| 178 | \section2 Mouse Handling | 
|---|
| 179 |  | 
|---|
| 180 | Just as in subclasses of native widgets, mouse events are handled by | 
|---|
| 181 | reimplementing functions such as QWidget::mousePressEvent() and | 
|---|
| 182 | QWidget::mouseMoveEvent(). | 
|---|
| 183 |  | 
|---|
| 184 | The \l{QWidget::mousePressEvent()}{mousePressEvent()} function simply | 
|---|
| 185 | records the position of the mouse when a button is initially pressed: | 
|---|
| 186 |  | 
|---|
| 187 | \snippet examples/opengl/hellogl/glwidget.cpp 9 | 
|---|
| 188 |  | 
|---|
| 189 | The \l{QWidget::mouseMoveEvent()}{mouseMoveEvent()} function uses the | 
|---|
| 190 | previous location of the mouse cursor to determine how much the object | 
|---|
| 191 | in the scene should be rotated, and in which direction: | 
|---|
| 192 |  | 
|---|
| 193 | \snippet examples/opengl/hellogl/glwidget.cpp 10 | 
|---|
| 194 |  | 
|---|
| 195 | Since the user is expected to hold down the mouse button and drag the | 
|---|
| 196 | cursor to rotate the object, the cursor's position is updated every time | 
|---|
| 197 | a move event is received. | 
|---|
| 198 |  | 
|---|
| 199 | \section2 Utility Functions | 
|---|
| 200 |  | 
|---|
| 201 | We have omitted the utility functions, \c makeObject(), \c quad(), | 
|---|
| 202 | \c extrude(), and \c normalizeAngle() from our discussion. These can be | 
|---|
| 203 | viewed in the quoted source for \c glwidget.cpp via the link at the | 
|---|
| 204 | start of this document. | 
|---|
| 205 |  | 
|---|
| 206 | \section1 Window Class Definition | 
|---|
| 207 |  | 
|---|
| 208 | The \c Window class is used as a container for the \c GLWidget used to | 
|---|
| 209 | display the scene: | 
|---|
| 210 |  | 
|---|
| 211 | \snippet examples/opengl/hellogl/window.h 0 | 
|---|
| 212 |  | 
|---|
| 213 | In addition, it contains sliders that are used to change the orientation | 
|---|
| 214 | of the object in the scene. | 
|---|
| 215 |  | 
|---|
| 216 | \section1 Window Class Implementation | 
|---|
| 217 |  | 
|---|
| 218 | The constructor constructs an instance of the \c GLWidget class and some | 
|---|
| 219 | sliders to manipulate its contents. | 
|---|
| 220 |  | 
|---|
| 221 | \snippet examples/opengl/hellogl/window.cpp 0 | 
|---|
| 222 |  | 
|---|
| 223 | We connect the \l{QAbstractSlider::valueChanged()}{valueChanged()} signal | 
|---|
| 224 | from each of the sliders to the appropriate slots in \c{glWidget}. | 
|---|
| 225 | This allows the user to change the orientation of the object by dragging | 
|---|
| 226 | the sliders. | 
|---|
| 227 |  | 
|---|
| 228 | We also connect the \c xRotationChanged(), \c yRotationChanged(), and | 
|---|
| 229 | \c zRotationChanged() signals from \c glWidget to the | 
|---|
| 230 | \l{QAbstractSlider::setValue()}{setValue()} slots in the | 
|---|
| 231 | corresponding sliders. | 
|---|
| 232 |  | 
|---|
| 233 | \snippet examples/opengl/hellogl/window.cpp 1 | 
|---|
| 234 |  | 
|---|
| 235 | The sliders are placed horizontally in a layout alongside the \c GLWidget, | 
|---|
| 236 | and initialized with suitable default values. | 
|---|
| 237 |  | 
|---|
| 238 | The \c createSlider() utility function constructs a QSlider, and ensures | 
|---|
| 239 | that it is set up with a suitable range, step value, tick interval, and | 
|---|
| 240 | page step value before returning it to the calling function: | 
|---|
| 241 |  | 
|---|
| 242 | \snippet examples/opengl/hellogl/window.cpp 2 | 
|---|
| 243 |  | 
|---|
| 244 | \section1 Summary | 
|---|
| 245 |  | 
|---|
| 246 | The \c GLWidget class implementation shows how to subclass QGLWidget for | 
|---|
| 247 | the purposes of rendering a 3D scene using OpenGL calls. Since QGLWidget | 
|---|
| 248 | is a subclass of QWidget, subclasses of QGLWidget can be placed in layouts | 
|---|
| 249 | and provided with interactive features just like normal custom widgets. | 
|---|
| 250 |  | 
|---|
| 251 | We ensure that the widget is able to correctly render the scene using OpenGL | 
|---|
| 252 | by reimplementing the following functions: | 
|---|
| 253 |  | 
|---|
| 254 | \list | 
|---|
| 255 | \o QGLWidget::initializeGL() sets up resources needed by the OpenGL implementation | 
|---|
| 256 | to render the scene. | 
|---|
| 257 | \o QGLWidget::resizeGL() resizes the viewport so that the rendered scene fits onto | 
|---|
| 258 | the widget, and sets up a projection matrix to map 3D coordinates to 2D viewport | 
|---|
| 259 | coordinates. | 
|---|
| 260 | \o QGLWidget::paintGL() performs painting operations using OpenGL calls. | 
|---|
| 261 | \endlist | 
|---|
| 262 |  | 
|---|
| 263 | Since QGLWidget is a subclass of QWidget, it can also be used | 
|---|
| 264 | as a normal paint device, allowing 2D graphics to be drawn with QPainter. | 
|---|
| 265 | This use of QGLWidget is discussed in the \l{2D Painting Example}{2D Painting} | 
|---|
| 266 | example. | 
|---|
| 267 |  | 
|---|
| 268 | More advanced users may want to paint over parts of a scene rendered using | 
|---|
| 269 | OpenGL. QGLWidget allows pure OpenGL rendering to be mixed with QPainter | 
|---|
| 270 | calls, but care must be taken to maintain the state of the OpenGL implementation. | 
|---|
| 271 | See the \l{Overpainting Example}{Overpainting} example for more information. | 
|---|
| 272 | */ | 
|---|