129 lines
4.8 KiB
Plaintext
129 lines
4.8 KiB
Plaintext
/****************************************************************************
|
|
**
|
|
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
|
|
** Contact: http://www.qt-project.org/legal
|
|
**
|
|
** This file is part of the documentation of the Qt Toolkit.
|
|
**
|
|
** $QT_BEGIN_LICENSE:FDL$
|
|
** Commercial License Usage
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
** accordance with the commercial license agreement provided with the
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
|
**
|
|
** GNU Free Documentation License Usage
|
|
** Alternatively, this file may be used under the terms of the GNU Free
|
|
** Documentation License version 1.3 as published by the Free Software
|
|
** Foundation and appearing in the file included in the packaging of
|
|
** this file. Please review the following information to ensure
|
|
** the GNU Free Documentation License version 1.3 requirements
|
|
** will be met: http://www.gnu.org/copyleft/fdl.html.
|
|
** $QT_END_LICENSE$
|
|
**
|
|
****************************************************************************/
|
|
|
|
/*!
|
|
\example hellogl_es
|
|
\title Hello GL ES Example
|
|
|
|
\brief The Hello GL ES example is the \l{Hello GL Example} ported to OpenGL ES.
|
|
It also included some effects from the OpenGL \l{Overpainting Example}.
|
|
|
|
\image hellogl-es-example.png
|
|
|
|
A complete introduction to OpenGL ES and a description of all differences
|
|
between OpenGL and OpenGL ES is out of the scope of this document; but
|
|
we will describe some of the major issues and differences.
|
|
|
|
Since Hello GL ES is a direct port of standard OpenGL code, it is a fairly
|
|
good example for porting OpenGL code to OpenGL ES.
|
|
|
|
\tableofcontents
|
|
|
|
\section1 Using QGLWidget
|
|
|
|
QGLWidget can be used for OpenGL ES similar to the way it is used with
|
|
standard OpenGL; but there are some differences. We use EGL 1.0 to embedd
|
|
the OpenGL ES window within the native window manager. In
|
|
QGLWidget::initializeGL() we initialize OpenGL ES.
|
|
|
|
\section1 Porting OpenGL to OpenGL ES
|
|
|
|
Since OpenGL ES is missing the immediate mode and does not support quads,
|
|
we have to create triangle arrays.
|
|
|
|
We create a quad by adding vertices to a QList of vertices. We create both
|
|
sides of the quad and hardcode a distance of 0.05f. We also compute the
|
|
correct normal for each face and store them in another QList.
|
|
|
|
\snippet hellogl_es/glwidget.cpp 0
|
|
|
|
And then we convert the complete list of vertexes and the list of normals
|
|
into the native OpenGL ES format that we can use with the OpenGL ES API.
|
|
|
|
\snippet hellogl_es/glwidget.cpp 1
|
|
|
|
In \c paintQtLogo() we draw the triangle array using OpenGL ES. We use
|
|
q_vertexTypeEnum to abstract the fact that our vertex and normal arrays
|
|
are either in float or in fixed point format.
|
|
|
|
\snippet hellogl_es/glwidget.cpp 2
|
|
|
|
\section1 Using QGLPainter
|
|
|
|
Since the \c QGLPainter is slower for OpenGL ES we paint the bubbles with
|
|
the rasterizer and cache them in a QImage. This happends only once during
|
|
the initialiazation.
|
|
|
|
\snippet hellogl_es/bubble.cpp 0
|
|
|
|
For each bubble this QImage is then drawn to the QGLWidget by using the
|
|
according QPainter with transparency enabled.
|
|
|
|
\snippet hellogl_es/bubble.cpp 1
|
|
|
|
Another difference beetwen OpenGL and OpenGL ES is that OpenGL ES does not
|
|
support glPushAttrib(GL_ALL_ATTRIB_BITS). So we have to restore all the
|
|
OpenGL states ourselves, after we created the QPainter in
|
|
GLWidget::paintGL().
|
|
|
|
\snippet hellogl_es/glwidget.cpp 3
|
|
|
|
Setting up up the model view matrix and setting the right OpenGL states is
|
|
done in the same way as for standard OpenGL.
|
|
|
|
\snippet hellogl_es/glwidget.cpp 4
|
|
|
|
Now we have to restore the OpenGL state for the QPainter. This is not done
|
|
automatically for OpenGL ES.
|
|
|
|
\snippet hellogl_es/glwidget.cpp 5
|
|
|
|
Now we use the QPainter to draw the transparent bubbles.
|
|
|
|
\snippet hellogl_es/glwidget.cpp 6
|
|
|
|
In the end, we calculate the framerate and display it using the QPainter
|
|
again.
|
|
|
|
\snippet hellogl_es/glwidget.cpp 7
|
|
|
|
After we finished all the drawing operations we swap the screen buffer.
|
|
|
|
\snippet hellogl_es/glwidget.cpp 8
|
|
|
|
\section1 Summary
|
|
|
|
Similar to the \l{Hello GL Example}, we subclass QGLWidget to render
|
|
a 3D scene using OpenGL ES calls. QGLWidget is a subclass of QWidget.
|
|
Hence, its \l{QGLWidget}'s subclasses can be placed in layouts and
|
|
provided with interactive features just like normal custom widgets.
|
|
|
|
QGLWidget allows pure OpenGL ES rendering to be mixed with QPainter calls,
|
|
but care must be taken to maintain the state of the OpenGL ES
|
|
implementation.
|
|
*/
|