Added OpenGL example and documentation.
Change-Id: I2d9d4e52caf0a39fef9648d8a9e83a0c1328f650 Reviewed-by: Paul Olav Tvete <paul.tvete@nokia.com>
This commit is contained in:
parent
49c251e054
commit
f3b45ffa61
156
doc/src/examples/openglwindow.qdoc
Normal file
156
doc/src/examples/openglwindow.qdoc
Normal file
@ -0,0 +1,156 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
**
|
||||||
|
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
|
||||||
|
** Contact: http://www.qt-project.org/
|
||||||
|
**
|
||||||
|
** This file is part of the documentation of the Qt Toolkit.
|
||||||
|
**
|
||||||
|
** $QT_BEGIN_LICENSE:FDL$
|
||||||
|
** GNU Free Documentation License
|
||||||
|
** 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.
|
||||||
|
**
|
||||||
|
** Other Usage
|
||||||
|
** Alternatively, this file may be used in accordance with the terms
|
||||||
|
** and conditions contained in a signed written agreement between you
|
||||||
|
** and Nokia.
|
||||||
|
**
|
||||||
|
**
|
||||||
|
**
|
||||||
|
**
|
||||||
|
**
|
||||||
|
** $QT_END_LICENSE$
|
||||||
|
**
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\example gui/openglwindow
|
||||||
|
\title OpenGL Window Example
|
||||||
|
|
||||||
|
This example shows how to create a minimal QWindow based application
|
||||||
|
for the purpose of using OpenGL.
|
||||||
|
|
||||||
|
\image openglwindow-example.png Screenshot of the OpenGLWindow example
|
||||||
|
|
||||||
|
\section1 OpenGLWindow super class
|
||||||
|
|
||||||
|
Our OpenGLWindow class acts as an API which is then subclassed to do the
|
||||||
|
actual rendering. It has functions to make a request for render() to be
|
||||||
|
called, either immediately with renderNow() or as soon as the event loop
|
||||||
|
has finished processing the current batch of events with renderLater().
|
||||||
|
The OpenGLWindow subclass can either reimplement render() for OpenGL based
|
||||||
|
rendering, or render(QPainter *) for rendering with a QPainter. Use
|
||||||
|
OpenGLWindow::setAnimating(true) for render() to be called at the vertical
|
||||||
|
refresh rate, assuming vertical sync is enabled in the underlying OpenGL
|
||||||
|
drivers.
|
||||||
|
|
||||||
|
In the class that does the OpenGL rendering you will typically want to
|
||||||
|
inherit from QOpenGLFunctions, as our OpenGLWindow does, in order to get
|
||||||
|
platform independent access to OpenGL ES 2.0 functions. By inheriting from
|
||||||
|
QOpenGLFunctions the OpenGL functions it contains will get precedence, and
|
||||||
|
you will not have to worry about resolving those functions if you want your
|
||||||
|
application to work with OpenGL as well as OpenGL ES 2.0.
|
||||||
|
|
||||||
|
\snippet gui/openglwindow/openglwindow.h 1
|
||||||
|
|
||||||
|
The window's surface type must be set to QSurface::OpenGLSurface to
|
||||||
|
indicate that the window is to be used for OpenGL rendering and not for
|
||||||
|
rendering raster content with QPainter using a QBackingStore.
|
||||||
|
|
||||||
|
\snippet gui/openglwindow/openglwindow.cpp 1
|
||||||
|
|
||||||
|
Any OpenGL initialization needed can be done by overriding the initialize()
|
||||||
|
function, which is called once before the first call to render(), with a
|
||||||
|
valid current QOpenGLContext. As can be seen in the following code snippet,
|
||||||
|
the default render(QPainter *) and initialize() implementations are empty,
|
||||||
|
whereas the default render() implementation initializes a
|
||||||
|
QOpenGLPaintDevice and then calls into render(QPainter *).
|
||||||
|
|
||||||
|
\snippet gui/openglwindow/openglwindow.cpp 2
|
||||||
|
|
||||||
|
The renderLater() function simply puts an update request event on
|
||||||
|
the event loop, which leads to renderNow() being called once the event
|
||||||
|
gets processed.
|
||||||
|
|
||||||
|
We also call renderNow() when we get an expose event. The exposeEvent() is
|
||||||
|
the notification to the window that its exposure, meaning visibility, on
|
||||||
|
the screen has changed. When the expose event is received you can query
|
||||||
|
QWindow::isExposed() to find out whether or not the window is currently
|
||||||
|
exposed. Do not render to or call QOpenGLContext::swapBuffers() on a window
|
||||||
|
before it has received its first expose event, as before then its final
|
||||||
|
size might be unknown, and in addition what is rendered might not even end
|
||||||
|
up on the screen.
|
||||||
|
|
||||||
|
\snippet gui/openglwindow/openglwindow.cpp 3
|
||||||
|
|
||||||
|
In renderNow() we return if we are not currently exposed, in which case
|
||||||
|
rendering is delayed until we actually get an expose event. If we have not
|
||||||
|
yet done so, we create the QOpenGLContext with the same QSurfaceFormat as
|
||||||
|
was set on the OpenGLWindow, and call initialize() for the sake of the sub
|
||||||
|
class, and initializeOpenGLFunctions() in order for the QOpenGLFunctions
|
||||||
|
super class to be associated with the correct QOpenGLContext. In any case
|
||||||
|
we make the context current by calling QOpenGLContext::makeCurrent(), call
|
||||||
|
render() to do the actual rendering, and finally we schedule for the
|
||||||
|
rendered contents to be made visible by calling
|
||||||
|
QOpenGLContext::swapBuffers() with the OpenGLWindow as parameter.
|
||||||
|
|
||||||
|
Once the rendering of a frame using an OpenGL context is initiated by
|
||||||
|
calling QOpenGLContext::makeCurrent(), giving the surface on which to
|
||||||
|
render as a parameter, OpenGL commands can be issued. The commands can be
|
||||||
|
issued either directly by including <qopengl.h>, which also includes the
|
||||||
|
system's OpenGL headers, or as by using QOpenGLFunctions, which can
|
||||||
|
either be inherited from for convenience, or accessed using
|
||||||
|
QOpenGLContext::functions(). QOpenGLFunctions gives access to all the
|
||||||
|
OpenGL ES 2.0 level OpenGL calls that are not already standard in both
|
||||||
|
OpenGL ES 2.0 and desktop OpenGL. For more information about the OpenGL and
|
||||||
|
OpenGL ES APIs, refer to the official \l{OpenGL Registry} and
|
||||||
|
\l{Khronos OpenGL ES API Registry}.
|
||||||
|
|
||||||
|
If animation has been enabled with OpenGLWindow::setAnimating(true), we
|
||||||
|
call renderLater() to put another update request on the event loop.
|
||||||
|
|
||||||
|
\snippet gui/openglwindow/openglwindow.cpp 4
|
||||||
|
|
||||||
|
Enabling animation also triggers an update request as shown in the
|
||||||
|
following code snippet.
|
||||||
|
|
||||||
|
\snippet gui/openglwindow/openglwindow.cpp 5
|
||||||
|
|
||||||
|
\section1 Example OpenGL rendering sub class
|
||||||
|
|
||||||
|
Here we sub class OpenGLWindow to show how to do OpenGL to render a
|
||||||
|
rotating triangle. By indirectly sub classing QOpenGLFunctions we gain
|
||||||
|
access to all OpenGL ES 2.0 level functionality.
|
||||||
|
|
||||||
|
\snippet gui/openglwindow/main.cpp 1
|
||||||
|
|
||||||
|
In our main function we initialize QGuiApplication and instantiate our
|
||||||
|
TriangleOpenGLWindow. We give it a QSurfaceFormat specifying that we want
|
||||||
|
four samples of multisample antialiasing, as well as a default geometry.
|
||||||
|
Since we want to have animation we call the above mentioned setAnimating()
|
||||||
|
function with an argument of true.
|
||||||
|
|
||||||
|
\snippet gui/openglwindow/main.cpp 2
|
||||||
|
|
||||||
|
The following code snippet shows the OpenGL shader program used in this
|
||||||
|
example. The vertex and fragment shaders are relatively simple, doing
|
||||||
|
vertex transformation and interpolated vertex coloring.
|
||||||
|
|
||||||
|
\snippet gui/openglwindow/main.cpp 3
|
||||||
|
|
||||||
|
Here is the code that loads the shaders and initializes the shader program
|
||||||
|
By using QOpenGLShaderProgram instead of raw OpenGL we get the convenience
|
||||||
|
that strips out the highp, mediump, and lowp qualifiers on desktop OpenGL,
|
||||||
|
where they are not part of the standard. We store the attribute and uniform
|
||||||
|
locations in member variables to avoid having to do the location lookup
|
||||||
|
each frame.
|
||||||
|
|
||||||
|
\snippet gui/openglwindow/main.cpp 4
|
||||||
|
|
||||||
|
Finally, here is our render() function, where we use OpenGL to set up the
|
||||||
|
viewport, clear the background, and render a rotating triangle.
|
||||||
|
|
||||||
|
\snippet gui/openglwindow/main.cpp 5
|
||||||
|
*/
|
BIN
doc/src/images/openglwindow-example.png
Normal file
BIN
doc/src/images/openglwindow-example.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 20 KiB |
@ -2,6 +2,7 @@ TEMPLATE = subdirs
|
|||||||
CONFIG += no_docs_target
|
CONFIG += no_docs_target
|
||||||
|
|
||||||
SUBDIRS = \
|
SUBDIRS = \
|
||||||
|
gui \
|
||||||
network \
|
network \
|
||||||
threads \
|
threads \
|
||||||
xml \
|
xml \
|
||||||
|
@ -2,4 +2,4 @@ TEMPLATE = subdirs
|
|||||||
|
|
||||||
SUBDIRS += analogclock
|
SUBDIRS += analogclock
|
||||||
SUBDIRS += rasterwindow
|
SUBDIRS += rasterwindow
|
||||||
|
SUBDIRS += openglwindow
|
||||||
|
179
examples/gui/openglwindow/main.cpp
Normal file
179
examples/gui/openglwindow/main.cpp
Normal file
@ -0,0 +1,179 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
**
|
||||||
|
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
|
||||||
|
** Contact: http://www.qt-project.org/
|
||||||
|
**
|
||||||
|
** This file is part of the documentation of the Qt Toolkit.
|
||||||
|
**
|
||||||
|
** $QT_BEGIN_LICENSE:BSD$
|
||||||
|
** You may use this file under the terms of the BSD license as follows:
|
||||||
|
**
|
||||||
|
** "Redistribution and use in source and binary forms, with or without
|
||||||
|
** modification, are permitted provided that the following conditions are
|
||||||
|
** met:
|
||||||
|
** * Redistributions of source code must retain the above copyright
|
||||||
|
** notice, this list of conditions and the following disclaimer.
|
||||||
|
** * Redistributions in binary form must reproduce the above copyright
|
||||||
|
** notice, this list of conditions and the following disclaimer in
|
||||||
|
** the documentation and/or other materials provided with the
|
||||||
|
** distribution.
|
||||||
|
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
||||||
|
** the names of its contributors may be used to endorse or promote
|
||||||
|
** products derived from this software without specific prior written
|
||||||
|
** permission.
|
||||||
|
**
|
||||||
|
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
||||||
|
**
|
||||||
|
** $QT_END_LICENSE$
|
||||||
|
**
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include "openglwindow.h"
|
||||||
|
|
||||||
|
#include <QtGui/QGuiApplication>
|
||||||
|
#include <QtGui/QMatrix4x4>
|
||||||
|
#include <QtGui/QOpenGLShaderProgram>
|
||||||
|
#include <QtGui/QScreen>
|
||||||
|
|
||||||
|
#include <QtCore/qmath.h>
|
||||||
|
|
||||||
|
//! [1]
|
||||||
|
class TriangleWindow : public OpenGLWindow
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
TriangleWindow();
|
||||||
|
|
||||||
|
void initialize();
|
||||||
|
void render();
|
||||||
|
|
||||||
|
private:
|
||||||
|
GLuint loadShader(GLenum type, const char *source);
|
||||||
|
|
||||||
|
GLuint m_posAttr;
|
||||||
|
GLuint m_colAttr;
|
||||||
|
GLuint m_matrixUniform;
|
||||||
|
|
||||||
|
QOpenGLShaderProgram *m_program;
|
||||||
|
int m_frame;
|
||||||
|
};
|
||||||
|
|
||||||
|
TriangleWindow::TriangleWindow()
|
||||||
|
: m_program(0)
|
||||||
|
, m_frame(0)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
//! [1]
|
||||||
|
|
||||||
|
//! [2]
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
QGuiApplication app(argc, argv);
|
||||||
|
|
||||||
|
QSurfaceFormat format;
|
||||||
|
format.setSamples(4);
|
||||||
|
|
||||||
|
TriangleWindow window;
|
||||||
|
window.setFormat(format);
|
||||||
|
window.resize(640, 480);
|
||||||
|
window.show();
|
||||||
|
|
||||||
|
window.setAnimating(true);
|
||||||
|
|
||||||
|
return app.exec();
|
||||||
|
}
|
||||||
|
//! [2]
|
||||||
|
|
||||||
|
|
||||||
|
//! [3]
|
||||||
|
static const char *vertexShaderSource =
|
||||||
|
"attribute highp vec4 posAttr;\n"
|
||||||
|
"attribute lowp vec4 colAttr;\n"
|
||||||
|
"varying lowp vec4 col;\n"
|
||||||
|
"uniform highp mat4 matrix;\n"
|
||||||
|
"void main() {\n"
|
||||||
|
" col = colAttr;\n"
|
||||||
|
" gl_Position = matrix * posAttr;\n"
|
||||||
|
"}\n";
|
||||||
|
|
||||||
|
static const char *fragmentShaderSource =
|
||||||
|
"varying lowp vec4 col;\n"
|
||||||
|
"void main() {\n"
|
||||||
|
" gl_FragColor = col;\n"
|
||||||
|
"}\n";
|
||||||
|
//! [3]
|
||||||
|
|
||||||
|
//! [4]
|
||||||
|
GLuint TriangleWindow::loadShader(GLenum type, const char *source)
|
||||||
|
{
|
||||||
|
GLuint shader = glCreateShader(type);
|
||||||
|
glShaderSource(shader, 1, &source, 0);
|
||||||
|
glCompileShader(shader);
|
||||||
|
return shader;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TriangleWindow::initialize()
|
||||||
|
{
|
||||||
|
m_program = new QOpenGLShaderProgram(this);
|
||||||
|
m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
|
||||||
|
m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
|
||||||
|
m_program->link();
|
||||||
|
m_posAttr = m_program->attributeLocation("posAttr");
|
||||||
|
m_colAttr = m_program->attributeLocation("colAttr");
|
||||||
|
m_matrixUniform = m_program->uniformLocation("matrix");
|
||||||
|
}
|
||||||
|
//! [4]
|
||||||
|
|
||||||
|
//! [5]
|
||||||
|
void TriangleWindow::render()
|
||||||
|
{
|
||||||
|
glViewport(0, 0, width(), height());
|
||||||
|
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
|
m_program->bind();
|
||||||
|
|
||||||
|
QMatrix4x4 matrix;
|
||||||
|
matrix.perspective(60, 4.0/3.0, 0.1, 100.0);
|
||||||
|
matrix.translate(0, 0, -2);
|
||||||
|
matrix.rotate(100.0f * m_frame / screen()->refreshRate(), 0, 1, 0);
|
||||||
|
|
||||||
|
m_program->setUniformValue(m_matrixUniform, matrix);
|
||||||
|
|
||||||
|
GLfloat vertices[] = {
|
||||||
|
0.0f, 0.707f,
|
||||||
|
-0.5f, -0.5f,
|
||||||
|
0.5f, -0.5f
|
||||||
|
};
|
||||||
|
|
||||||
|
GLfloat colors[] = {
|
||||||
|
1.0f, 0.0f, 0.0f,
|
||||||
|
0.0f, 1.0f, 0.0f,
|
||||||
|
0.0f, 0.0f, 1.0f
|
||||||
|
};
|
||||||
|
|
||||||
|
glVertexAttribPointer(m_posAttr, 2, GL_FLOAT, GL_FALSE, 0, vertices);
|
||||||
|
glVertexAttribPointer(m_colAttr, 3, GL_FLOAT, GL_FALSE, 0, colors);
|
||||||
|
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
|
glEnableVertexAttribArray(1);
|
||||||
|
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||||
|
|
||||||
|
glDisableVertexAttribArray(1);
|
||||||
|
glDisableVertexAttribArray(0);
|
||||||
|
|
||||||
|
m_program->release();
|
||||||
|
|
||||||
|
++m_frame;
|
||||||
|
}
|
||||||
|
//! [5]
|
169
examples/gui/openglwindow/openglwindow.cpp
Normal file
169
examples/gui/openglwindow/openglwindow.cpp
Normal file
@ -0,0 +1,169 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
**
|
||||||
|
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
|
||||||
|
** Contact: http://www.qt-project.org/
|
||||||
|
**
|
||||||
|
** This file is part of the documentation of the Qt Toolkit.
|
||||||
|
**
|
||||||
|
** $QT_BEGIN_LICENSE:BSD$
|
||||||
|
** You may use this file under the terms of the BSD license as follows:
|
||||||
|
**
|
||||||
|
** "Redistribution and use in source and binary forms, with or without
|
||||||
|
** modification, are permitted provided that the following conditions are
|
||||||
|
** met:
|
||||||
|
** * Redistributions of source code must retain the above copyright
|
||||||
|
** notice, this list of conditions and the following disclaimer.
|
||||||
|
** * Redistributions in binary form must reproduce the above copyright
|
||||||
|
** notice, this list of conditions and the following disclaimer in
|
||||||
|
** the documentation and/or other materials provided with the
|
||||||
|
** distribution.
|
||||||
|
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
||||||
|
** the names of its contributors may be used to endorse or promote
|
||||||
|
** products derived from this software without specific prior written
|
||||||
|
** permission.
|
||||||
|
**
|
||||||
|
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
||||||
|
**
|
||||||
|
** $QT_END_LICENSE$
|
||||||
|
**
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include "openglwindow.h"
|
||||||
|
|
||||||
|
#include <QtCore/QCoreApplication>
|
||||||
|
|
||||||
|
#include <QtGui/QOpenGLContext>
|
||||||
|
#include <QtGui/QOpenGLPaintDevice>
|
||||||
|
#include <QtGui/QPainter>
|
||||||
|
|
||||||
|
//! [1]
|
||||||
|
OpenGLWindow::OpenGLWindow(QWindow *parent)
|
||||||
|
: QWindow(parent)
|
||||||
|
, m_update_pending(false)
|
||||||
|
, m_animating(false)
|
||||||
|
, m_context(0)
|
||||||
|
, m_device(0)
|
||||||
|
{
|
||||||
|
setSurfaceType(QWindow::OpenGLSurface);
|
||||||
|
}
|
||||||
|
//! [1]
|
||||||
|
|
||||||
|
OpenGLWindow::~OpenGLWindow()
|
||||||
|
{
|
||||||
|
delete m_device;
|
||||||
|
}
|
||||||
|
//! [2]
|
||||||
|
void OpenGLWindow::render(QPainter *painter)
|
||||||
|
{
|
||||||
|
Q_UNUSED(painter);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpenGLWindow::initialize()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpenGLWindow::render()
|
||||||
|
{
|
||||||
|
if (!m_device)
|
||||||
|
m_device = new QOpenGLPaintDevice;
|
||||||
|
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
||||||
|
|
||||||
|
m_device->setSize(size());
|
||||||
|
|
||||||
|
QPainter painter(m_device);
|
||||||
|
render(&painter);
|
||||||
|
}
|
||||||
|
//! [2]
|
||||||
|
|
||||||
|
//! [3]
|
||||||
|
void OpenGLWindow::renderLater()
|
||||||
|
{
|
||||||
|
if (!m_update_pending) {
|
||||||
|
m_update_pending = true;
|
||||||
|
QCoreApplication::postEvent(this, new QEvent(QEvent::UpdateRequest));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool OpenGLWindow::event(QEvent *event)
|
||||||
|
{
|
||||||
|
switch (event->type()) {
|
||||||
|
case QEvent::UpdateRequest:
|
||||||
|
renderNow();
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
return QWindow::event(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpenGLWindow::exposeEvent(QExposeEvent *event)
|
||||||
|
{
|
||||||
|
Q_UNUSED(event);
|
||||||
|
|
||||||
|
if (isExposed())
|
||||||
|
renderNow();
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpenGLWindow::resizeEvent(QResizeEvent *event)
|
||||||
|
{
|
||||||
|
Q_UNUSED(event);
|
||||||
|
|
||||||
|
if (isExposed())
|
||||||
|
renderNow();
|
||||||
|
}
|
||||||
|
//! [3]
|
||||||
|
|
||||||
|
//! [4]
|
||||||
|
void OpenGLWindow::renderNow()
|
||||||
|
{
|
||||||
|
if (!isExposed())
|
||||||
|
return;
|
||||||
|
|
||||||
|
m_update_pending = false;
|
||||||
|
|
||||||
|
bool needsInitialize = false;
|
||||||
|
|
||||||
|
if (!m_context) {
|
||||||
|
m_context = new QOpenGLContext(this);
|
||||||
|
m_context->setFormat(requestedFormat());
|
||||||
|
m_context->create();
|
||||||
|
|
||||||
|
needsInitialize = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_context->makeCurrent(this);
|
||||||
|
|
||||||
|
if (needsInitialize) {
|
||||||
|
initializeOpenGLFunctions();
|
||||||
|
initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
render();
|
||||||
|
|
||||||
|
m_context->swapBuffers(this);
|
||||||
|
|
||||||
|
if (m_animating)
|
||||||
|
renderLater();
|
||||||
|
}
|
||||||
|
//! [4]
|
||||||
|
|
||||||
|
//! [5]
|
||||||
|
void OpenGLWindow::setAnimating(bool animating)
|
||||||
|
{
|
||||||
|
m_animating = animating;
|
||||||
|
|
||||||
|
if (animating)
|
||||||
|
renderLater();
|
||||||
|
}
|
||||||
|
//! [5]
|
||||||
|
|
83
examples/gui/openglwindow/openglwindow.h
Normal file
83
examples/gui/openglwindow/openglwindow.h
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
**
|
||||||
|
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
|
||||||
|
** Contact: http://www.qt-project.org/
|
||||||
|
**
|
||||||
|
** This file is part of the documentation of the Qt Toolkit.
|
||||||
|
**
|
||||||
|
** $QT_BEGIN_LICENSE:BSD$
|
||||||
|
** You may use this file under the terms of the BSD license as follows:
|
||||||
|
**
|
||||||
|
** "Redistribution and use in source and binary forms, with or without
|
||||||
|
** modification, are permitted provided that the following conditions are
|
||||||
|
** met:
|
||||||
|
** * Redistributions of source code must retain the above copyright
|
||||||
|
** notice, this list of conditions and the following disclaimer.
|
||||||
|
** * Redistributions in binary form must reproduce the above copyright
|
||||||
|
** notice, this list of conditions and the following disclaimer in
|
||||||
|
** the documentation and/or other materials provided with the
|
||||||
|
** distribution.
|
||||||
|
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
||||||
|
** the names of its contributors may be used to endorse or promote
|
||||||
|
** products derived from this software without specific prior written
|
||||||
|
** permission.
|
||||||
|
**
|
||||||
|
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
||||||
|
**
|
||||||
|
** $QT_END_LICENSE$
|
||||||
|
**
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <QtGui/QWindow>
|
||||||
|
#include <QtGui/QOpenGLFunctions>
|
||||||
|
|
||||||
|
QT_BEGIN_NAMESPACE
|
||||||
|
class QPainter;
|
||||||
|
class QOpenGLContext;
|
||||||
|
class QOpenGLPaintDevice;
|
||||||
|
QT_END_NAMESPACE
|
||||||
|
|
||||||
|
//! [1]
|
||||||
|
class OpenGLWindow : public QWindow, protected QOpenGLFunctions
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
explicit OpenGLWindow(QWindow *parent = 0);
|
||||||
|
~OpenGLWindow();
|
||||||
|
|
||||||
|
virtual void render(QPainter *painter);
|
||||||
|
virtual void render();
|
||||||
|
|
||||||
|
virtual void initialize();
|
||||||
|
|
||||||
|
void setAnimating(bool animating);
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
void renderLater();
|
||||||
|
void renderNow();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
bool event(QEvent *event);
|
||||||
|
|
||||||
|
void exposeEvent(QExposeEvent *event);
|
||||||
|
void resizeEvent(QResizeEvent *event);
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool m_update_pending;
|
||||||
|
bool m_animating;
|
||||||
|
|
||||||
|
QOpenGLContext *m_context;
|
||||||
|
QOpenGLPaintDevice *m_device;
|
||||||
|
};
|
||||||
|
//! [1]
|
||||||
|
|
3
examples/gui/openglwindow/openglwindow.pri
Normal file
3
examples/gui/openglwindow/openglwindow.pri
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
INCLUDEPATH += $$PWD
|
||||||
|
SOURCES += $$PWD/openglwindow.cpp
|
||||||
|
HEADERS += $$PWD/openglwindow.h
|
7
examples/gui/openglwindow/openglwindow.pro
Normal file
7
examples/gui/openglwindow/openglwindow.pro
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
include(openglwindow.pri)
|
||||||
|
|
||||||
|
OTHER_FILES += \
|
||||||
|
openglwindow.pri
|
||||||
|
|
||||||
|
SOURCES += \
|
||||||
|
main.cpp
|
@ -114,10 +114,19 @@
|
|||||||
QWindow supports rendering using desktop OpenGL, OpenGL ES 1.1 and
|
QWindow supports rendering using desktop OpenGL, OpenGL ES 1.1 and
|
||||||
OpenGL ES 2.0, depending on what the platform supports. OpenGL
|
OpenGL ES 2.0, depending on what the platform supports. OpenGL
|
||||||
rendering is enabled by setting the QWindow's surface type to
|
rendering is enabled by setting the QWindow's surface type to
|
||||||
QSurface::OpenGLSurface, then creating a QOpenGLContext to manage
|
QSurface::OpenGLSurface, choosing the format attributes with
|
||||||
the native OpenGL context.
|
QSurfaceFormat, and then creating a QOpenGLContext to manage
|
||||||
|
the native OpenGL context. In addition, Qt has QOpenGLPaintDevice,
|
||||||
|
which enables the use of OpenGL accelerated QPainter rendering, as well as
|
||||||
|
convenience classes that simplify the writing of OpenGL code and hides the
|
||||||
|
complexities of extension handling and the differences between OpenGL ES 2
|
||||||
|
and desktop OpenGL. The convenience classes include QOpenGLFunctions that
|
||||||
|
lets an application use all the OpenGL ES 2 functions on desktop OpenGL
|
||||||
|
without having to manually resolve the OpenGL function pointers and some
|
||||||
|
classes that wrap native OpenGL resources in a simpler Qt API:
|
||||||
|
QOpenGLBuffer, QOpenGLFramebufferObject, and QOpenGLShaderProgram.
|
||||||
|
|
||||||
For more information, see \l {OpenGL Enablers}.
|
For more information, see the \l {OpenGL Window Example}.
|
||||||
|
|
||||||
The Qt GUI module also contains a few math classes to aid with the
|
The Qt GUI module also contains a few math classes to aid with the
|
||||||
most common mathmatical operations related to 3D graphics. These
|
most common mathmatical operations related to 3D graphics. These
|
||||||
|
Loading…
x
Reference in New Issue
Block a user