The Hello GL example demonstrates the basic use of the OpenGL-related classes provided with Qt.
Qt provides the QGLWidget class to enable OpenGL graphics to be rendered within a standard application user interface. By subclassing this class, and providing reimplementations of event handler functions, 3D scenes can be displayed on widgets that can be placed in layouts, connected to other objects using signals and slots, and manipulated like any other widget.
The rest of the class contains utility functions and variables that are used to construct and hold orientation information for the scene. The object variable will be used to hold an identifier for an OpenGL display list.
GLWidget Class Implementation
In this example, we split the class into groups of functions and describe them separately. This helps to illustrate the differences between subclasses of native widgets (such as QWidget and QFrame) and QGLWidget subclasses.
Widget Construction and Sizing
The constructor provides default rotation angles for the scene, initializes the variable used for the display list, and sets up some colors for later use.
In the above slot, the xRot variable is updated only if the new angle is different to the old one, the xRotationChanged() signal is emitted to allow other components to be updated, and the widget's updateGL() handler function is called.
The setYRotation() and setZRotation() slots perform the same task for rotations measured by the yRot and zRot variables.
OpenGL Initialization
The initializeGL() function is used to perform useful initialization tasks that are needed to render the 3D scene. These often involve defining colors and materials, enabling and disabling certain rendering flags, and setting other properties used to customize the rendering process.
In this example, we reimplement the function to set the background color, create a display list containing information about the object we want to display, and set up the rendering process to use a particular shading model and rendering flags:
Resizing the Viewport
The resizeGL() function is used to ensure that the OpenGL implementation renders the scene onto a viewport that matches the size of the widget, using the correct transformation from 3D coordinates to 2D viewport coordinates.
The function is called whenever the widget's dimensions change, and is supplied with the new width and height. Here, we define a square viewport based on the length of the smallest side of the widget to ensure that the scene is not distorted if the widget has sides of unequal length:
A discussion of the projection transformation used is outside the scope of this example. Please consult the OpenGL reference documentation for an explanation of projection matrices.
Painting the Scene
The paintGL() function is used to paint the contents of the scene onto the widget. For widgets that only need to be decorated with pure OpenGL content, we reimplement QGLWidget::paintGL() instead of reimplementing QWidget::paintEvent():
In this example, we clear the widget using the background color that we defined in the initializeGL() function, set up the frame of reference for the object we want to display, and call the display list containing the rendering commands for the object.
The mouseMoveEvent() function uses the previous location of the mouse cursor to determine how much the object in the scene should be rotated, and in which direction:
Since the user is expected to hold down the mouse button and drag the cursor to rotate the object, the cursor's position is updated every time a move event is received.
Utility Functions
We have omitted the utility functions, makeObject(), quad(), extrude(), and normalizeAngle() from our discussion. These can be viewed in the quoted source for glwidget.cpp via the link at the start of this document.
Window Class Definition
The Window class is used as a container for the GLWidget used to display the scene:
class GLWidget;
class Window : public QWidget
{
Q_OBJECT
public:
Window();
private:
QSlider *createSlider();
GLWidget *glWidget;
QSlider *xSlider;
QSlider *ySlider;
QSlider *zSlider;
};
In addition, it contains sliders that are used to change the orientation of the object in the scene.
Window Class Implementation
The constructor constructs an instance of the GLWidget class and some sliders to manipulate its contents.
We connect the valueChanged() signal from each of the sliders to the appropriate slots in glWidget. This allows the user to change the orientation of the object by dragging the sliders.
We also connect the xRotationChanged(), yRotationChanged(), and zRotationChanged() signals from glWidget to the setValue() slots in the corresponding sliders.
The sliders are placed horizontally in a layout alongside the GLWidget, and initialized with suitable default values.
The createSlider() utility function constructs a QSlider, and ensures that it is set up with a suitable range, step value, tick interval, and page step value before returning it to the calling function:
The GLWidget class implementation shows how to subclass QGLWidget for the purposes of rendering a 3D scene using OpenGL calls. Since QGLWidget is a subclass of QWidget, subclasses of QGLWidget can be placed in layouts and provided with interactive features just like normal custom widgets.
We ensure that the widget is able to correctly render the scene using OpenGL by reimplementing the following functions:
QGLWidget::initializeGL() sets up resources needed by the OpenGL implementation to render the scene.
QGLWidget::resizeGL() resizes the viewport so that the rendered scene fits onto the widget, and sets up a projection matrix to map 3D coordinates to 2D viewport coordinates.
Since QGLWidget is a subclass of QWidget, it can also be used as a normal paint device, allowing 2D graphics to be drawn with QPainter. This use of QGLWidget is discussed in the 2D Painting example.
More advanced users may want to paint over parts of a scene rendered using OpenGL. QGLWidget allows pure OpenGL rendering to be mixed with QPainter calls, but care must be taken to maintain the state of the OpenGL implementation. See the Overpainting example for more information.
Qt Quarterly est la revue trimestrielle proposée par Nokia et à destination des développeurs Qt. Ces articles d'une grande qualité technique sont rédigés par des experts Qt. Lire l'article.
Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).
Qt dans le magazine
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter
ou par MP !