diff options
author | Lars Knoll <lars.knoll@nokia.com> | 2009-03-23 09:34:13 (GMT) |
---|---|---|
committer | Simon Hausmann <simon.hausmann@nokia.com> | 2009-03-23 09:34:13 (GMT) |
commit | 67ad0519fd165acee4a4d2a94fa502e9e4847bd0 (patch) | |
tree | 1dbf50b3dff8d5ca7e9344733968c72704eb15ff /examples/opengl | |
download | Qt-67ad0519fd165acee4a4d2a94fa502e9e4847bd0.zip Qt-67ad0519fd165acee4a4d2a94fa502e9e4847bd0.tar.gz Qt-67ad0519fd165acee4a4d2a94fa502e9e4847bd0.tar.bz2 |
Long live Qt!
Diffstat (limited to 'examples/opengl')
94 files changed, 8175 insertions, 0 deletions
diff --git a/examples/opengl/2dpainting/2dpainting.pro b/examples/opengl/2dpainting/2dpainting.pro new file mode 100644 index 0000000..c45b764 --- /dev/null +++ b/examples/opengl/2dpainting/2dpainting.pro @@ -0,0 +1,17 @@ +QT += opengl + +HEADERS = glwidget.h \ + helper.h \ + widget.h \ + window.h +SOURCES = glwidget.cpp \ + helper.cpp \ + main.cpp \ + widget.cpp \ + window.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/opengl/2dpainting +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS 2dpainting.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/opengl/2dpainting +INSTALLS += target sources diff --git a/examples/opengl/2dpainting/glwidget.cpp b/examples/opengl/2dpainting/glwidget.cpp new file mode 100644 index 0000000..d5f63d2 --- /dev/null +++ b/examples/opengl/2dpainting/glwidget.cpp @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> +#include "glwidget.h" +#include "helper.h" + +//! [0] +GLWidget::GLWidget(Helper *helper, QWidget *parent) + : QGLWidget(QGLFormat(QGL::SampleBuffers), parent), helper(helper) +{ + elapsed = 0; + setFixedSize(200, 200); + setAutoFillBackground(false); +} +//! [0] + +//! [1] +void GLWidget::animate() +{ + elapsed = (elapsed + qobject_cast<QTimer*>(sender())->interval()) % 1000; + repaint(); +} +//! [1] + +//! [2] +void GLWidget::paintEvent(QPaintEvent *event) +{ + QPainter painter; + painter.begin(this); + painter.setRenderHint(QPainter::Antialiasing); + helper->paint(&painter, event, elapsed); + painter.end(); +} +//! [2] diff --git a/examples/opengl/2dpainting/glwidget.h b/examples/opengl/2dpainting/glwidget.h new file mode 100644 index 0000000..f3a9bd5 --- /dev/null +++ b/examples/opengl/2dpainting/glwidget.h @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef GLWIDGET_H +#define GLWIDGET_H + +#include <QGLWidget> + +//! [0] +class Helper; +QT_BEGIN_NAMESPACE +class QPaintEvent; +class QWidget; +QT_END_NAMESPACE + +class GLWidget : public QGLWidget +{ + Q_OBJECT + +public: + GLWidget(Helper *helper, QWidget *parent); + +public slots: + void animate(); + +protected: + void paintEvent(QPaintEvent *event); + +private: + Helper *helper; + int elapsed; +}; +//! [0] + +#endif diff --git a/examples/opengl/2dpainting/helper.cpp b/examples/opengl/2dpainting/helper.cpp new file mode 100644 index 0000000..8796d87 --- /dev/null +++ b/examples/opengl/2dpainting/helper.cpp @@ -0,0 +1,91 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> +#include "helper.h" + +//! [0] +Helper::Helper() +{ + QLinearGradient gradient(QPointF(50, -20), QPointF(80, 20)); + gradient.setColorAt(0.0, Qt::white); + gradient.setColorAt(1.0, QColor(0xa6, 0xce, 0x39)); + + background = QBrush(QColor(64, 32, 64)); + circleBrush = QBrush(gradient); + circlePen = QPen(Qt::black); + circlePen.setWidth(1); + textPen = QPen(Qt::white); + textFont.setPixelSize(50); +} +//! [0] + +//! [1] +void Helper::paint(QPainter *painter, QPaintEvent *event, int elapsed) +{ + painter->fillRect(event->rect(), background); + painter->translate(100, 100); +//! [1] + +//! [2] + painter->save(); + painter->setBrush(circleBrush); + painter->setPen(circlePen); + painter->rotate(elapsed * 0.030); + + qreal r = elapsed/1000.0; + int n = 30; + for (int i = 0; i < n; ++i) { + painter->rotate(30); + qreal radius = 0 + 120.0*((i+r)/n); + qreal circleRadius = 1 + ((i+r)/n)*20; + painter->drawEllipse(QRectF(radius, -circleRadius, + circleRadius*2, circleRadius*2)); + } + painter->restore(); +//! [2] + +//! [3] + painter->setPen(textPen); + painter->setFont(textFont); + painter->drawText(QRect(-50, -50, 100, 100), Qt::AlignCenter, "Qt"); +} +//! [3] diff --git a/examples/opengl/2dpainting/helper.h b/examples/opengl/2dpainting/helper.h new file mode 100644 index 0000000..1d63cc9 --- /dev/null +++ b/examples/opengl/2dpainting/helper.h @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef HELPER_H +#define HELPER_H + +#include <QBrush> +#include <QFont> +#include <QPen> + +QT_BEGIN_NAMESPACE +class QPainter; +class QPaintEvent; +QT_END_NAMESPACE + +//! [0] +class Helper +{ +public: + Helper(); + +public: + void paint(QPainter *painter, QPaintEvent *event, int elapsed); + +private: + QBrush background; + QBrush circleBrush; + QFont textFont; + QPen circlePen; + QPen textPen; +}; +//! [0] + +#endif diff --git a/examples/opengl/2dpainting/main.cpp b/examples/opengl/2dpainting/main.cpp new file mode 100644 index 0000000..58c9705 --- /dev/null +++ b/examples/opengl/2dpainting/main.cpp @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> +#include "window.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + Window window; + window.show(); + return app.exec(); +} diff --git a/examples/opengl/2dpainting/widget.cpp b/examples/opengl/2dpainting/widget.cpp new file mode 100644 index 0000000..89d4608 --- /dev/null +++ b/examples/opengl/2dpainting/widget.cpp @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> +#include "widget.h" +#include "helper.h" + + +//! [0] +Widget::Widget(Helper *helper, QWidget *parent) + : QWidget(parent), helper(helper) +{ + elapsed = 0; + setFixedSize(200, 200); +} +//! [0] + +//! [1] +void Widget::animate() +{ + elapsed = (elapsed + qobject_cast<QTimer*>(sender())->interval()) % 1000; + repaint(); +} +//! [1] + +//! [2] +void Widget::paintEvent(QPaintEvent *event) +{ + QPainter painter; + painter.begin(this); + painter.setRenderHint(QPainter::Antialiasing); + helper->paint(&painter, event, elapsed); + painter.end(); +} +//! [2] diff --git a/examples/opengl/2dpainting/widget.h b/examples/opengl/2dpainting/widget.h new file mode 100644 index 0000000..f88c676 --- /dev/null +++ b/examples/opengl/2dpainting/widget.h @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef WIDGET_H +#define WIDGET_H + +#include <QWidget> + +//! [0] +class Helper; +QT_BEGIN_NAMESPACE +class QPaintEvent; +QT_END_NAMESPACE + +class Widget : public QWidget +{ + Q_OBJECT + +public: + Widget(Helper *helper, QWidget *parent); + +public slots: + void animate(); + +protected: + void paintEvent(QPaintEvent *event); + +private: + Helper *helper; + int elapsed; +}; +//! [0] + +#endif diff --git a/examples/opengl/2dpainting/window.cpp b/examples/opengl/2dpainting/window.cpp new file mode 100644 index 0000000..f55e90e --- /dev/null +++ b/examples/opengl/2dpainting/window.cpp @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> +#include "glwidget.h" +#include "widget.h" +#include "window.h" + +//! [0] +Window::Window() + : QWidget() +{ + Widget *native = new Widget(&helper, this); + GLWidget *openGL = new GLWidget(&helper, this); + QLabel *nativeLabel = new QLabel(tr("Native")); + nativeLabel->setAlignment(Qt::AlignHCenter); + QLabel *openGLLabel = new QLabel(tr("OpenGL")); + openGLLabel->setAlignment(Qt::AlignHCenter); + + QGridLayout *layout = new QGridLayout; + layout->addWidget(native, 0, 0); + layout->addWidget(openGL, 0, 1); + layout->addWidget(nativeLabel, 1, 0); + layout->addWidget(openGLLabel, 1, 1); + setLayout(layout); + + QTimer *timer = new QTimer(this); + connect(timer, SIGNAL(timeout()), native, SLOT(animate())); + connect(timer, SIGNAL(timeout()), openGL, SLOT(animate())); + timer->start(50); + + setWindowTitle(tr("2D Painting on Native and OpenGL Widgets")); +} +//! [0] diff --git a/examples/opengl/2dpainting/window.h b/examples/opengl/2dpainting/window.h new file mode 100644 index 0000000..84a4440 --- /dev/null +++ b/examples/opengl/2dpainting/window.h @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef WINDOW_H +#define WINDOW_H + +#include <QWidget> + +#include "helper.h" + +QT_BEGIN_NAMESPACE +class QLabel; +class QWidget; +QT_END_NAMESPACE + +//! [0] +class Window : public QWidget +{ + Q_OBJECT + +public: + Window(); + +private: + Helper helper; +}; +//! [0] + +#endif diff --git a/examples/opengl/README b/examples/opengl/README new file mode 100644 index 0000000..69bd134 --- /dev/null +++ b/examples/opengl/README @@ -0,0 +1,41 @@ +Qt provides support for integration with OpenGL implementations on all +platforms, giving developers the opportunity to display hardware accelerated +3D graphics alongside a more conventional user interface. + +These examples demonstrate the basic techniques used to take advantage of +OpenGL in Qt applications. + + +The example launcher provided with Qt can be used to explore each of the +examples in this directory. + +Documentation for these examples can be found via the Tutorial and Examples +link in the main Qt documentation. + + +Finding the Qt Examples and Demos launcher +========================================== + +On Windows: + +The launcher can be accessed via the Windows Start menu. Select the menu +entry entitled "Qt Examples and Demos" entry in the submenu containing +the Qt tools. + +On Mac OS X: + +For the binary distribution, the qtdemo executable is installed in the +/Developer/Applications/Qt directory. For the source distribution, it is +installed alongside the other Qt tools on the path specified when Qt is +configured. + +On Unix/Linux: + +The qtdemo executable is installed alongside the other Qt tools on the path +specified when Qt is configured. + +On all platforms: + +The source code for the launcher can be found in the demos/qtdemo directory +in the Qt package. This example is built at the same time as the Qt libraries, +tools, examples, and demonstrations. diff --git a/examples/opengl/framebufferobject/bubbles.svg b/examples/opengl/framebufferobject/bubbles.svg new file mode 100644 index 0000000..65867da --- /dev/null +++ b/examples/opengl/framebufferobject/bubbles.svg @@ -0,0 +1,215 @@ +<?xml version="1.0" standalone="no"?> +<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> +<svg width="20cm" height="15cm" viewBox="0 0 800 600" + xmlns="http://www.w3.org/2000/svg" + xmlns:xlink="http://www.w3.org/1999/xlink/" + baseProfile="tiny" version="1.2"> + <title>Spheres</title> + <desc>Semi-transparent bubbles on a colored background.</desc> + <defs> + <!-- Create radial gradients for each bubble. --> + <radialGradient id="blueBubble" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="-50"> + <stop offset="0%" stop-color="white" stop-opacity="1" /> + <stop offset="25%" stop-color="#cdcdff" stop-opacity=".65" /> + <stop offset="100%" stop-color="#cdaacd" stop-opacity=".75" /> + </radialGradient> + <radialGradient id="redBubble" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="-50"> + <stop offset="0%" stop-color="white" stop-opacity="1" /> + <stop offset="25%" stop-color="#ffcdcd" stop-opacity=".65" /> + <stop offset="100%" stop-color="#bbbb99" stop-opacity=".75" /> + </radialGradient> + <radialGradient id="greenBubble" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="-50"> + <stop offset="0%" stop-color="white" stop-opacity="1" /> + <stop offset="25%" stop-color="#cdffcd" stop-opacity=".65" /> + <stop offset="100%" stop-color="#99aaaa" stop-opacity=".75" /> + </radialGradient> + <radialGradient id="yellowBubble" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="-50"> + <stop offset="0%" stop-color="white" stop-opacity="1" /> + <stop offset="25%" stop-color="#ffffcd" stop-opacity=".65" /> + <stop offset="100%" stop-color="#bbbbaa" stop-opacity=".75" /> + </radialGradient> + <radialGradient id="background" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="400" fx="250" fy="250"> + <stop offset="0%" stop-color="#ffffee" /> + <stop offset="100%" stop-color="#ccccaa" /> + </radialGradient> + <linearGradient id="surface" gradientUnits="userSpaceOnUse" + x1="-100" y1="200" x2="400" y2="200"> + <stop offset="0%" stop-color="#ffffcc" /> + <stop offset="100%" stop-color="#bbbb88" /> + </linearGradient> + + <!-- Create radial gradients for each circle to make them look like + spheres. --> + <radialGradient id="blueSphere" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="-50"> + <stop offset="0%" stop-color="white" /> + <stop offset="75%" stop-color="blue" /> + <stop offset="100%" stop-color="#222244" /> + </radialGradient> + <radialGradient id="redSphere" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="-50"> + <stop offset="0%" stop-color="white" /> + <stop offset="75%" stop-color="red" /> + <stop offset="100%" stop-color="#442222" /> + </radialGradient> + <radialGradient id="greenSphere" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="-50"> + <stop offset="0%" stop-color="white" /> + <stop offset="75%" stop-color="green" /> + <stop offset="100%" stop-color="#113311" /> + </radialGradient> + <radialGradient id="yellowSphere" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="-50"> + <stop offset="0%" stop-color="white" /> + <stop offset="75%" stop-color="yellow" /> + <stop offset="100%" stop-color="#444422" /> + </radialGradient> + <radialGradient id="shadowGrad" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="50"> + <stop offset="0%" stop-color="black" stop-opacity="1.0" /> + <stop offset="100%" stop-color="white" stop-opacity="0.0" /> + </radialGradient> + + <!-- Define a shadow for each sphere. --> + <circle id="shadow" fill="url(#shadowGrad)" cx="0" cy="0" r="100" /> + <g id="bubble"> + <circle fill="black" cx="0" cy="0" r="50" /> + <circle fill="#a6ce39" cx="0" cy="0" r="33" /> + <path fill="black" d="M 37,50 L 50,37 L 12,-1 L 22,-11 L 10,-24 L -24,10 + L -11,22 L -1,12 Z" /> + <circle cx="0" cy="0" r="100" /> + </g> + </defs> + <g> + <rect fill="url(#background)" x="0" y="0" width="800" height="600" /> + </g> + + <g transform="translate(200,700)"> + <use xlink:href="#bubble" fill="url(#blueBubble)" /> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="1s" dur="10s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(315,700)"> + <g transform="scale(0.5,0.5)"> + <use xlink:href="#bubble" fill="url(#redBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="3s" dur="7s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(80,700)"> + <g transform="scale(0.65,0.65)"> + <use xlink:href="#bubble" fill="url(#greenBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="5s" dur="9s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(255,700)"> + <g transform="scale(0.3,0.3)"> + <use xlink:href="#bubble" fill="url(#yellowBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="2s" dur="6s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(565,700)"> + <g transform="scale(0.4,0.4)"> + <use xlink:href="#bubble" fill="url(#blueBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="4s" dur="8s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(715,700)"> + <g transform="scale(0.6,0.6)"> + <use xlink:href="#bubble" fill="url(#redBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="1s" dur="4s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(645,700)"> + <g transform="scale(0.375,0.375)"> + <use xlink:href="#bubble" fill="url(#greenBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="0s" dur="11s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(555,700)"> + <g transform="scale(0.9,0.9)"> + <use xlink:href="#bubble" fill="url(#yellowBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="3s" dur="7.5s" fill="freeze" repeatCount="indefinite" /> + </g> + + <g transform="translate(360,700)"> + <g transform="scale(0.5,0.5)"> + <use xlink:href="#bubble" fill="url(#blueBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="3s" dur="6s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(215,700)"> + <g transform="scale(0.45,0.45)"> + <use xlink:href="#bubble" fill="url(#redBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="5.5s" dur="7s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(420,700)"> + <g transform="scale(0.75,0.75)"> + <use xlink:href="#bubble" fill="url(#greenBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="1s" dur="9s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(815,700)"> + <g transform="scale(0.6,0.6)"> + <use xlink:href="#bubble" fill="url(#yellowBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="2s" dur="9.5s" fill="freeze" repeatCount="indefinite" /> + </g> + + <g transform="translate(225,375)" > + <g transform="scale(1.0,0.5)" > + <path d="M 0 0 L 350 0 L 450 450 L -100 450 z" + fill="url(#surface)" stroke="none" /> + </g> + </g> + <g transform="translate(200,0)" > + <g transform="translate(200,490) scale(2.0,1.0) rotate(45)" > + <rect fill="#a6ce39" x="-69" y="-69" width="138" height="138" /> + <circle fill="black" cx="0" cy="0" r="50" /> + <circle fill="#a6ce39" cx="0" cy="0" r="33" /> + <path fill="black" d="M 37,50 L 50,37 L 12,-1 L 22,-11 L 10,-24 L -24,10 + L -11,22 L -1,12 Z" /> + <animateTransform attributeName="transform" type="rotate" values="0; 360" + begin="0s" dur="10s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(200,375)"> + <use xlink:href="#shadow" transform="translate(25,55) scale(1.0,0.5)" /> + <circle fill="url(#blueSphere)" cx="0" cy="0" r="100" /> + </g> + <g transform="translate(315,440)"> + <g transform="scale(0.5,0.5)"> + <use xlink:href="#shadow" transform="translate(25,55) scale(1.0,0.5)" /> + <circle fill="url(#redSphere)" cx="0" cy="0" r="100" /> + </g> + </g> + <g transform="translate(80,475)"> + <g transform="scale(0.65,0.65)"> + <use xlink:href="#shadow" transform="translate(25,55) scale(1.0,0.5)" /> + <circle fill="url(#greenSphere)" cx="0" cy="0" r="100" /> + </g> + </g> + <g transform="translate(255,525)"> + <g transform="scale(0.3,0.3)"> + <use xlink:href="#shadow" transform="translate(25,55) scale(1.0,0.5)" /> + <circle fill="url(#yellowSphere)" cx="0" cy="0" r="100" /> + </g> + </g> + </g> +</svg> diff --git a/examples/opengl/framebufferobject/designer.png b/examples/opengl/framebufferobject/designer.png Binary files differnew file mode 100644 index 0000000..4f32d57 --- /dev/null +++ b/examples/opengl/framebufferobject/designer.png diff --git a/examples/opengl/framebufferobject/framebufferobject.pro b/examples/opengl/framebufferobject/framebufferobject.pro new file mode 100644 index 0000000..4bc667c --- /dev/null +++ b/examples/opengl/framebufferobject/framebufferobject.pro @@ -0,0 +1,22 @@ +###################################################################### +# Automatically generated by qmake (2.01a) Fri May 12 17:15:46 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +QT += opengl svg + +# Input +HEADERS += glwidget.h +SOURCES += glwidget.cpp main.cpp +RESOURCES += framebufferobject.qrc + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/opengl/framebufferobject +sources.files = $$SOURCES $$HEADERS $$RESOURCES *.pro *.png *.svg +sources.path = $$[QT_INSTALL_EXAMPLES]/opengl/framebufferobject +INSTALLS += target sources + diff --git a/examples/opengl/framebufferobject/framebufferobject.qrc b/examples/opengl/framebufferobject/framebufferobject.qrc new file mode 100644 index 0000000..986f1ca --- /dev/null +++ b/examples/opengl/framebufferobject/framebufferobject.qrc @@ -0,0 +1,6 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource prefix="/res"> + <file>bubbles.svg</file> + <file>designer.png</file> +</qresource> +</RCC> diff --git a/examples/opengl/framebufferobject/glwidget.cpp b/examples/opengl/framebufferobject/glwidget.cpp new file mode 100644 index 0000000..d3591d6 --- /dev/null +++ b/examples/opengl/framebufferobject/glwidget.cpp @@ -0,0 +1,309 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui/QImage> +#include "glwidget.h" + +#include <math.h> + +#ifndef GL_MULTISAMPLE +#define GL_MULTISAMPLE 0x809D +#endif + +GLWidget::GLWidget(QWidget *parent) + : QGLWidget(QGLFormat(QGL::SampleBuffers|QGL::AlphaChannel), parent) +{ + setWindowTitle(tr("OpenGL framebuffer objects")); + makeCurrent(); + fbo = new QGLFramebufferObject(1024, 1024); + rot_x = rot_y = rot_z = 0.0f; + scale = 0.1f; + anim = new QTimeLine(750, this); + anim->setUpdateInterval(20); + connect(anim, SIGNAL(valueChanged(qreal)), SLOT(animate(qreal))); + connect(anim, SIGNAL(finished()), SLOT(animFinished())); + + svg_renderer = new QSvgRenderer(QLatin1String(":/res/bubbles.svg"), this); + connect(svg_renderer, SIGNAL(repaintNeeded()), this, SLOT(draw())); + + logo = QImage(":/res/designer.png"); + logo = logo.convertToFormat(QImage::Format_ARGB32); + + tile_list = glGenLists(1); + glNewList(tile_list, GL_COMPILE); + glBegin(GL_QUADS); + { + glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); + glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); + glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); + glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); + + glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); + glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); + glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); + glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); + + glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); + glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); + glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); + glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); + + glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); + glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); + glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); + glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); + + glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); + glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); + glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); + glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); + + glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); + glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); + glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); + glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); + } + glEnd(); + glEndList(); + + wave = new GLfloat[logo.width()*logo.height()]; + memset(wave, 0, logo.width()*logo.height()); + startTimer(30); // wave timer +} + +GLWidget::~GLWidget() +{ + delete[] wave; + glDeleteLists(tile_list, 1); + delete fbo; +} + +void GLWidget::paintEvent(QPaintEvent *) +{ + draw(); +} + +void GLWidget::draw() +{ + QPainter p(this); // used for text overlay + + // save the GL state set for QPainter + saveGLState(); + + // render the 'bubbles.svg' file into our framebuffer object + QPainter fbo_painter(fbo); + svg_renderer->render(&fbo_painter); + fbo_painter.end(); + + // draw into the GL widget + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glFrustum(-1, 1, -1, 1, 10, 100); + glTranslatef(0.0f, 0.0f, -15.0f); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glViewport(0, 0, width(), height()); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + glBindTexture(GL_TEXTURE_2D, fbo->texture()); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glEnable(GL_TEXTURE_2D); + glEnable(GL_MULTISAMPLE); + glEnable(GL_CULL_FACE); + + // draw background + glPushMatrix(); + glScalef(1.7f, 1.7f, 1.7f); + glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + glCallList(tile_list); + glPopMatrix(); + + const int w = logo.width(); + const int h = logo.height(); + + glRotatef(rot_x, 1.0f, 0.0f, 0.0f); + glRotatef(rot_y, 0.0f, 1.0f, 0.0f); + glRotatef(rot_z, 0.0f, 0.0f, 1.0f); + glScalef(scale/w, scale/w, scale/w); + + glDepthFunc(GL_LESS); + glEnable(GL_DEPTH_TEST); + // draw the Qt icon + glTranslatef(-w+1, -h+1, 0.0f); + for (int y=h-1; y>=0; --y) { + uint *p = (uint*) logo.scanLine(y); + uint *end = p + w; + int x = 0; + while (p < end) { + glColor4ub(qRed(*p), qGreen(*p), qBlue(*p), uchar(qAlpha(*p)*.9)); + glTranslatef(0.0f, 0.0f, wave[y*w+x]); + if (qAlpha(*p) > 128) + glCallList(tile_list); + glTranslatef(0.0f, 0.0f, -wave[y*w+x]); + glTranslatef(2.0f, 0.0f, 0.0f); + ++x; + ++p; + } + glTranslatef(-w*2.0f, 2.0f, 0.0f); + } + + // restore the GL state that QPainter expects + restoreGLState(); + + // draw the overlayed text using QPainter + p.setPen(QColor(197, 197, 197, 157)); + p.setBrush(QColor(197, 197, 197, 127)); + p.drawRect(QRect(0, 0, width(), 50)); + p.setPen(Qt::black); + p.setBrush(Qt::NoBrush); + const QString str1(tr("A simple OpenGL framebuffer object example.")); + const QString str2(tr("Use the mouse wheel to zoom, press buttons and move mouse to rotate, double-click to flip.")); + QFontMetrics fm(p.font()); + p.drawText(width()/2 - fm.width(str1)/2, 20, str1); + p.drawText(width()/2 - fm.width(str2)/2, 20 + fm.lineSpacing(), str2); +} + +void GLWidget::mousePressEvent(QMouseEvent *e) +{ + anchor = e->pos(); +} + +void GLWidget::mouseMoveEvent(QMouseEvent *e) +{ + QPoint diff = e->pos() - anchor; + if (e->buttons() & Qt::LeftButton) { + rot_x += diff.y()/5.0f; + rot_y += diff.x()/5.0f; + } else if (e->buttons() & Qt::RightButton) { + rot_z += diff.x()/5.0f; + } + + anchor = e->pos(); + draw(); +} + +void GLWidget::wheelEvent(QWheelEvent *e) +{ + e->delta() > 0 ? scale += scale*0.1f : scale -= scale*0.1f; + draw(); +} + +void GLWidget::mouseDoubleClickEvent(QMouseEvent *) +{ + anim->start(); +} + +void GLWidget::animate(qreal val) +{ + rot_y = val * 180; + draw(); +} + +void GLWidget::animFinished() +{ + if (anim->direction() == QTimeLine::Forward) + anim->setDirection(QTimeLine::Backward); + else + anim->setDirection(QTimeLine::Forward); +} + +void GLWidget::saveGLState() +{ + glPushAttrib(GL_ALL_ATTRIB_BITS); + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); +} + +void GLWidget::restoreGLState() +{ + glMatrixMode(GL_PROJECTION); + glPopMatrix(); + glMatrixMode(GL_MODELVIEW); + glPopMatrix(); + glPopAttrib(); +} + +#define PI 3.14159 + +void GLWidget::timerEvent(QTimerEvent *) +{ + if (QApplication::mouseButtons() != 0) + return; + + static bool scale_in = true; + + if (scale_in && scale > 35.0f) + scale_in = false; + else if (!scale_in && scale < .5f) + scale_in = true; + + scale = scale_in ? scale + scale*0.01f : scale-scale*0.01f; + rot_z += 0.3f; + rot_x += 0.1f; + + int dx, dy; // disturbance point + float s, v, W, t; + int i, j; + static float wt[128][128]; + const int width = logo.width(); + const int AMP = 5; + + dx = dy = width >> 1; + + W = .3f; + v = -4; // wave speed + + for (i = 0; i < width; ++i) { + for ( j = 0; j < width; ++j) { + s = sqrt((double) ((j - dx) * (j - dx) + (i - dy) * (i - dy))); + wt[i][j] += 0.1f; + t = s / v; + if (s != 0) + wave[i*width + j] = AMP * sin(2 * PI * W * (wt[i][j] + t)) / (0.2*(s + 2)); + else + wave[i*width + j] = AMP * sin(2 * PI * W * (wt[i][j] + t)); + } + } +} diff --git a/examples/opengl/framebufferobject/glwidget.h b/examples/opengl/framebufferobject/glwidget.h new file mode 100644 index 0000000..b64cfa8 --- /dev/null +++ b/examples/opengl/framebufferobject/glwidget.h @@ -0,0 +1,82 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtOpenGL> +#include <QImage> +#include <QTimeLine> +#include <QSvgRenderer> + +class GLWidget : public QGLWidget +{ + Q_OBJECT + +public: + GLWidget(QWidget *parent); + ~GLWidget(); + + void saveGLState(); + void restoreGLState(); + + void paintEvent(QPaintEvent *); + void mousePressEvent(QMouseEvent *); + void mouseDoubleClickEvent(QMouseEvent *); + void mouseMoveEvent(QMouseEvent *); + void timerEvent(QTimerEvent *); + void wheelEvent(QWheelEvent *); + +public slots: + void animate(qreal); + void animFinished(); + void draw(); + +private: + QPoint anchor; + float scale; + float rot_x, rot_y, rot_z; + GLuint tile_list; + GLfloat *wave; + + QImage logo; + QTimeLine *anim; + QSvgRenderer *svg_renderer; + QGLFramebufferObject *fbo; +}; + diff --git a/examples/opengl/framebufferobject/main.cpp b/examples/opengl/framebufferobject/main.cpp new file mode 100644 index 0000000..f9af3f9 --- /dev/null +++ b/examples/opengl/framebufferobject/main.cpp @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui/QApplication> +#include <QtGui/QMessageBox> +#include "glwidget.h" + +int main(int argc, char **argv) +{ + Q_INIT_RESOURCE(framebufferobject); + + QApplication a(argc, argv); + if (!QGLFormat::hasOpenGL() || !QGLFramebufferObject::hasOpenGLFramebufferObjects()) { + QMessageBox::information(0, "OpenGL framebuffer objects", + "This system does not support OpenGL/framebuffer objects."); + return -1; + } + + GLWidget widget(0); + widget.resize(640, 480); + widget.show(); + return a.exec(); +} + diff --git a/examples/opengl/framebufferobject2/cubelogo.png b/examples/opengl/framebufferobject2/cubelogo.png Binary files differnew file mode 100644 index 0000000..3ae0f9b --- /dev/null +++ b/examples/opengl/framebufferobject2/cubelogo.png diff --git a/examples/opengl/framebufferobject2/framebufferobject2.pro b/examples/opengl/framebufferobject2/framebufferobject2.pro new file mode 100644 index 0000000..9f1644c --- /dev/null +++ b/examples/opengl/framebufferobject2/framebufferobject2.pro @@ -0,0 +1,11 @@ +HEADERS += glwidget.h +SOURCES += glwidget.cpp main.cpp +RESOURCES += framebufferobject2.qrc + +QT += opengl + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/opengl/framebufferobject2 +sources.files = $$SOURCES $$HEADERS $$RESOURCES framebufferobject2.pro *.png +sources.path = $$[QT_INSTALL_EXAMPLES]/opengl/framebufferobject2 +INSTALLS += target sources diff --git a/examples/opengl/framebufferobject2/framebufferobject2.qrc b/examples/opengl/framebufferobject2/framebufferobject2.qrc new file mode 100644 index 0000000..9fc1a56 --- /dev/null +++ b/examples/opengl/framebufferobject2/framebufferobject2.qrc @@ -0,0 +1,5 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource prefix="/res"> + <file>cubelogo.png</file> +</qresource> +</RCC> diff --git a/examples/opengl/framebufferobject2/glwidget.cpp b/examples/opengl/framebufferobject2/glwidget.cpp new file mode 100644 index 0000000..24bc2ef --- /dev/null +++ b/examples/opengl/framebufferobject2/glwidget.cpp @@ -0,0 +1,249 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "glwidget.h" +#include <QtGui/QImage> + +#include <math.h> + +static GLint cubeArray[][3] = { + {0, 0, 0}, {0, 1, 0}, {1, 1, 0}, {1, 0, 0}, + {0, 0, 1}, {1, 0, 1}, {1, 1, 1}, {0, 1, 1}, + {0, 0, 0}, {1, 0, 0}, {1, 0, 1}, {0, 0, 1}, + {0, 1, 0}, {0, 1, 1}, {1, 1, 1}, {1, 1, 0}, + {0, 1, 0}, {0, 0, 0}, {0, 0, 1}, {0, 1, 1}, + {1, 0, 0}, {1, 1, 0}, {1, 1, 1}, {1, 0, 1} +}; + +static GLint cubeTextureArray[][2] = { + {0, 0}, {1, 0}, {1, 1}, {0, 1}, + {0, 0}, {0, 1}, {1, 1}, {1, 0}, + {0, 0}, {1, 0}, {1, 1}, {0, 1}, + {1, 0}, {0, 0}, {0, 1}, {1, 1}, + {0, 0}, {1, 0}, {1, 1}, {0, 1}, + {1, 0}, {0, 0}, {0, 1}, {1, 1} +}; + +static GLint faceArray[][2] = { + {1, -1}, {1, 1}, {-1, 1}, {-1, -1} +}; + +static GLubyte colorArray[][4] = { + {102, 176, 54, 255}, + {81, 141, 41, 255}, + {62, 108, 32, 255}, + {45, 79, 23, 255} +}; + +GLWidget::GLWidget(QWidget *parent) + : QGLWidget(QGLFormat(QGL::SampleBuffers), parent) +{ + // create the framebuffer object - make sure to have a current + // context before creating it + makeCurrent(); + fbo = new QGLFramebufferObject(512, 512); + timerId = startTimer(20); + setWindowTitle(tr("OpenGL framebuffer objects 2")); +} + +GLWidget::~GLWidget() +{ + glDeleteLists(pbufferList, 1); + delete fbo; +} + +void GLWidget::initializeGL() +{ + glMatrixMode(GL_MODELVIEW); + + glEnable(GL_CULL_FACE); + + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glVertexPointer(3, GL_INT, 0, cubeArray); + glTexCoordPointer(2, GL_INT, 0, cubeTextureArray); + glColorPointer(4, GL_UNSIGNED_BYTE, 0, colorArray); + + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glEnable(GL_BLEND); + glEnable(GL_TEXTURE_2D); + glEnable(GL_DEPTH_TEST); + + glClearColor(1.0f, 1.0f, 1.0f, 1.0f); + pbufferList = glGenLists(1); + glNewList(pbufferList, GL_COMPILE); + { + glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); + + // draw cube background + glPushMatrix(); + glLoadIdentity(); + glTranslatef(0.5f, 0.5f, -2.0f); + glDisable(GL_TEXTURE_2D); + glEnableClientState(GL_COLOR_ARRAY); + glVertexPointer(2, GL_INT, 0, faceArray); + glDrawArrays(GL_QUADS, 0, 4); + glVertexPointer(3, GL_INT, 0, cubeArray); + glDisableClientState(GL_COLOR_ARRAY); + glEnable(GL_TEXTURE_2D); + glPopMatrix(); + + // draw cube + glTranslatef(0.5f, 0.5f, 0.5f); + glRotatef(3.0f, 1.0f, 1.0f, 1.0f); + glTranslatef(-0.5f, -0.5f, -0.5f); + glColor4f(0.9f, 0.9f, 0.9f, 1.0f); + glDrawArrays(GL_QUADS, 0, 24); + + glPushMatrix(); // this state is popped back in the paintGL() function + } + glEndList(); + + for (int i = 0; i < 3; ++i) { + yOffs[i] = 0.0f; + xInc[i] = 0.005f; + rot[i] = 0.0f; + } + xOffs[0]= 0.0f; + xOffs[1]= 0.5f; + xOffs[2]= 1.0f; + + cubeTexture = bindTexture(QImage(":res/cubelogo.png")); + + glPushMatrix(); // push to avoid stack underflow in the first paintGL() call +} + +void GLWidget::resizeGL(int w, int h) +{ + glViewport(0, 0, w, h); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + float aspect = w/(float)(h ? h : 1); + glFrustum(-aspect, aspect, -1, 1, 10, 100); + glTranslatef(-0.5f, -0.5f, -0.5f); + glTranslatef(0.0f, 0.0f, -15.0f); +} + +void GLWidget::paintGL() +{ + glPopMatrix(); // pop the matrix pushed in the pbuffer list + + // push the projection matrix and the entire GL state before + // doing any rendering into our framebuffer object + glPushAttrib(GL_ALL_ATTRIB_BITS); + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + + glViewport(0, 0, fbo->size().width(), fbo->size().height()); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(-1, 1, -1, 1, -99, 99); + glTranslatef(-0.5f, -0.5f, 0.0f); + glMatrixMode(GL_MODELVIEW); + + // render to the framebuffer object + fbo->bind(); + glBindTexture(GL_TEXTURE_2D, cubeTexture); + glCallList(pbufferList); + fbo->release(); + + // pop the projection matrix and GL state back for rendering + // to the actual widget + glPopAttrib(); + glMatrixMode(GL_PROJECTION); + glPopMatrix(); + + glBindTexture(GL_TEXTURE_2D, fbo->texture()); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + // draw the background + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glLoadIdentity(); + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); + + glVertexPointer(2, GL_INT, 0, faceArray); + glTranslatef(-1.2f, -0.8f, 0.0f); + glScalef(0.2f, 0.2f, 0.2f); + for (int y = 0; y < 5; ++y) { + for (int x = 0; x < 5; ++x) { + glTranslatef(2.0f, 0, 0); + glColor4f(0.8, 0.8, 0.8, 1.0); + glDrawArrays(GL_QUADS, 0, 4); + } + glTranslatef(-10.0f, 2.0f, 0); + } + glVertexPointer(3, GL_INT, 0, cubeArray); + + glPopMatrix(); + glMatrixMode(GL_MODELVIEW); + + // draw the bouncing cubes + drawCube(0, 0.0f, 1.5f, 2.5f, 1.5f); + drawCube(1, 1.0f, 2.0f, 2.5f, 2.0f); + drawCube(2, 2.0f, 3.5f, 2.5f, 2.5f); + glPopMatrix(); +} + +void GLWidget::drawCube(int i, GLfloat z, GLfloat rotation, GLfloat jmp, GLfloat amp) +{ + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(xOffs[i], yOffs[i], z); + glTranslatef(0.5f, 0.5f, 0.5f); + GLfloat scale = 0.75 + i*(0.25f/2); + glScalef(scale, scale, scale); + glRotatef(rot[i], 1.0f, 1.0f, 1.0f); + glTranslatef(-0.5f, -0.5f, -0.5f); + + glColor4f(1.0f, 1.0f, 1.0f, 0.8f); + glDrawArrays(GL_QUADS, 0, 24); + + if (xOffs[i] > 1.0f || xOffs[i] < -1.0f) { + xInc[i] = -xInc[i]; + xOffs[i] = xOffs[i] > 1.0f ? 1.0f : -1.0f; + } + xOffs[i] += xInc[i]; + yOffs[i] = qAbs(cos((-3.141592f * jmp) * xOffs[i]) * amp) - 1; + rot[i] += rotation; +} diff --git a/examples/opengl/framebufferobject2/glwidget.h b/examples/opengl/framebufferobject2/glwidget.h new file mode 100644 index 0000000..e200ac2 --- /dev/null +++ b/examples/opengl/framebufferobject2/glwidget.h @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtOpenGL> + +class GLWidget : public QGLWidget +{ +public: + GLWidget(QWidget *parent); + ~GLWidget(); + void initializeGL(); + void resizeGL(int w, int h); + void paintGL(); + void timerEvent(QTimerEvent *) { update(); } + void mousePressEvent(QMouseEvent *) { killTimer(timerId); } + void mouseReleaseEvent(QMouseEvent *) { timerId = startTimer(20); } + + void drawCube(int i, GLfloat z, GLfloat ri, GLfloat jmp, GLfloat amp); + +private: + GLfloat rot[3], xOffs[3], yOffs[3], xInc[3]; + GLuint pbufferList; + GLuint cubeTexture; + int timerId; + + QGLFramebufferObject *fbo; +}; + diff --git a/examples/opengl/framebufferobject2/main.cpp b/examples/opengl/framebufferobject2/main.cpp new file mode 100644 index 0000000..912f4ec --- /dev/null +++ b/examples/opengl/framebufferobject2/main.cpp @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui/QApplication> +#include <QtGui/QMessageBox> +#include "glwidget.h" + +int main(int argc, char **argv) +{ + Q_INIT_RESOURCE(framebufferobject2); + + QApplication a(argc, argv); + if (!QGLFormat::hasOpenGL() || !QGLFramebufferObject::hasOpenGLFramebufferObjects()) { + QMessageBox::information(0, "OpenGL framebuffer objects 2", + "This system does not support OpenGL/framebuffer objects."); + return -1; + } + + GLWidget widget(0); + widget.resize(640, 480); + widget.show(); + return a.exec(); +} + diff --git a/examples/opengl/grabber/glwidget.cpp b/examples/opengl/grabber/glwidget.cpp new file mode 100644 index 0000000..71550ac --- /dev/null +++ b/examples/opengl/grabber/glwidget.cpp @@ -0,0 +1,284 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> +#include <QtOpenGL> + +#include <math.h> + +#include "glwidget.h" + +GLWidget::GLWidget(QWidget *parent) + : QGLWidget(parent) +{ + gear1 = 0; + gear2 = 0; + gear3 = 0; + xRot = 0; + yRot = 0; + zRot = 0; + gear1Rot = 0; + + QTimer *timer = new QTimer(this); + connect(timer, SIGNAL(timeout()), this, SLOT(advanceGears())); + timer->start(20); +} + +GLWidget::~GLWidget() +{ + makeCurrent(); + glDeleteLists(gear1, 1); + glDeleteLists(gear2, 1); + glDeleteLists(gear3, 1); +} + +void GLWidget::setXRotation(int angle) +{ + normalizeAngle(&angle); + if (angle != xRot) { + xRot = angle; + emit xRotationChanged(angle); + updateGL(); + } +} + +void GLWidget::setYRotation(int angle) +{ + normalizeAngle(&angle); + if (angle != yRot) { + yRot = angle; + emit yRotationChanged(angle); + updateGL(); + } +} + +void GLWidget::setZRotation(int angle) +{ + normalizeAngle(&angle); + if (angle != zRot) { + zRot = angle; + emit zRotationChanged(angle); + updateGL(); + } +} + +void GLWidget::initializeGL() +{ + static const GLfloat lightPos[4] = { 5.0f, 5.0f, 10.0f, 1.0f }; + static const GLfloat reflectance1[4] = { 0.8f, 0.1f, 0.0f, 1.0f }; + static const GLfloat reflectance2[4] = { 0.0f, 0.8f, 0.2f, 1.0f }; + static const GLfloat reflectance3[4] = { 0.2f, 0.2f, 1.0f, 1.0f }; + + glLightfv(GL_LIGHT0, GL_POSITION, lightPos); + glEnable(GL_LIGHTING); + glEnable(GL_LIGHT0); + glEnable(GL_DEPTH_TEST); + + gear1 = makeGear(reflectance1, 1.0, 4.0, 1.0, 0.7, 20); + gear2 = makeGear(reflectance2, 0.5, 2.0, 2.0, 0.7, 10); + gear3 = makeGear(reflectance3, 1.3, 2.0, 0.5, 0.7, 10); + + glEnable(GL_NORMALIZE); + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); +} + +void GLWidget::paintGL() +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glPushMatrix(); + glRotated(xRot / 16.0, 1.0, 0.0, 0.0); + glRotated(yRot / 16.0, 0.0, 1.0, 0.0); + glRotated(zRot / 16.0, 0.0, 0.0, 1.0); + + drawGear(gear1, -3.0, -2.0, 0.0, gear1Rot / 16.0); + drawGear(gear2, +3.1, -2.0, 0.0, -2.0 * (gear1Rot / 16.0) - 9.0); + + glRotated(+90.0, 1.0, 0.0, 0.0); + drawGear(gear3, -3.1, -1.8, -2.2, +2.0 * (gear1Rot / 16.0) - 2.0); + + glPopMatrix(); +} + +void GLWidget::resizeGL(int width, int height) +{ + int side = qMin(width, height); + glViewport((width - side) / 2, (height - side) / 2, side, side); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glFrustum(-1.0, +1.0, -1.0, 1.0, 5.0, 60.0); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslated(0.0, 0.0, -40.0); +} + +void GLWidget::mousePressEvent(QMouseEvent *event) +{ + lastPos = event->pos(); +} + +void GLWidget::mouseMoveEvent(QMouseEvent *event) +{ + int dx = event->x() - lastPos.x(); + int dy = event->y() - lastPos.y(); + + if (event->buttons() & Qt::LeftButton) { + setXRotation(xRot + 8 * dy); + setYRotation(yRot + 8 * dx); + } else if (event->buttons() & Qt::RightButton) { + setXRotation(xRot + 8 * dy); + setZRotation(zRot + 8 * dx); + } + lastPos = event->pos(); +} + +void GLWidget::advanceGears() +{ + gear1Rot += 2 * 16; + updateGL(); +} + +GLuint GLWidget::makeGear(const GLfloat *reflectance, GLdouble innerRadius, + GLdouble outerRadius, GLdouble thickness, + GLdouble toothSize, GLint toothCount) +{ + const double Pi = 3.14159265358979323846; + + GLuint list = glGenLists(1); + glNewList(list, GL_COMPILE); + glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, reflectance); + + GLdouble r0 = innerRadius; + GLdouble r1 = outerRadius - toothSize / 2.0; + GLdouble r2 = outerRadius + toothSize / 2.0; + GLdouble delta = (2.0 * Pi / toothCount) / 4.0; + GLdouble z = thickness / 2.0; + int i, j; + + glShadeModel(GL_FLAT); + + for (i = 0; i < 2; ++i) { + GLdouble sign = (i == 0) ? +1.0 : -1.0; + + glNormal3d(0.0, 0.0, sign); + + glBegin(GL_QUAD_STRIP); + for (j = 0; j <= toothCount; ++j) { + GLdouble angle = 2.0 * Pi * j / toothCount; + glVertex3d(r0 * cos(angle), r0 * sin(angle), sign * z); + glVertex3d(r1 * cos(angle), r1 * sin(angle), sign * z); + glVertex3d(r0 * cos(angle), r0 * sin(angle), sign * z); + glVertex3d(r1 * cos(angle + 3 * delta), r1 * sin(angle + 3 * delta), + sign * z); + } + glEnd(); + + glBegin(GL_QUADS); + for (j = 0; j < toothCount; ++j) { + GLdouble angle = 2.0 * Pi * j / toothCount; + glVertex3d(r1 * cos(angle), r1 * sin(angle), sign * z); + glVertex3d(r2 * cos(angle + delta), r2 * sin(angle + delta), + sign * z); + glVertex3d(r2 * cos(angle + 2 * delta), r2 * sin(angle + 2 * delta), + sign * z); + glVertex3d(r1 * cos(angle + 3 * delta), r1 * sin(angle + 3 * delta), + sign * z); + } + glEnd(); + } + + glBegin(GL_QUAD_STRIP); + for (i = 0; i < toothCount; ++i) { + for (j = 0; j < 2; ++j) { + GLdouble angle = 2.0 * Pi * (i + (j / 2.0)) / toothCount; + GLdouble s1 = r1; + GLdouble s2 = r2; + if (j == 1) + qSwap(s1, s2); + + glNormal3d(cos(angle), sin(angle), 0.0); + glVertex3d(s1 * cos(angle), s1 * sin(angle), +z); + glVertex3d(s1 * cos(angle), s1 * sin(angle), -z); + + glNormal3d(s2 * sin(angle + delta) - s1 * sin(angle), + s1 * cos(angle) - s2 * cos(angle + delta), 0.0); + glVertex3d(s2 * cos(angle + delta), s2 * sin(angle + delta), +z); + glVertex3d(s2 * cos(angle + delta), s2 * sin(angle + delta), -z); + } + } + glVertex3d(r1, 0.0, +z); + glVertex3d(r1, 0.0, -z); + glEnd(); + + glShadeModel(GL_SMOOTH); + + glBegin(GL_QUAD_STRIP); + for (i = 0; i <= toothCount; ++i) { + GLdouble angle = i * 2.0 * Pi / toothCount; + glNormal3d(-cos(angle), -sin(angle), 0.0); + glVertex3d(r0 * cos(angle), r0 * sin(angle), +z); + glVertex3d(r0 * cos(angle), r0 * sin(angle), -z); + } + glEnd(); + + glEndList(); + + return list; +} + +void GLWidget::drawGear(GLuint gear, GLdouble dx, GLdouble dy, GLdouble dz, + GLdouble angle) +{ + glPushMatrix(); + glTranslated(dx, dy, dz); + glRotated(angle, 0.0, 0.0, 1.0); + glCallList(gear); + glPopMatrix(); +} + +void GLWidget::normalizeAngle(int *angle) +{ + while (*angle < 0) + *angle += 360 * 16; + while (*angle > 360 * 16) + *angle -= 360 * 16; +} diff --git a/examples/opengl/grabber/glwidget.h b/examples/opengl/grabber/glwidget.h new file mode 100644 index 0000000..4e66b1f --- /dev/null +++ b/examples/opengl/grabber/glwidget.h @@ -0,0 +1,98 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef GLWIDGET_H +#define GLWIDGET_H + +#include <QGLWidget> + +class GLWidget : public QGLWidget +{ + Q_OBJECT + +public: + GLWidget(QWidget *parent = 0); + ~GLWidget(); + + int xRotation() const { return xRot; } + int yRotation() const { return yRot; } + int zRotation() const { return zRot; } + +public slots: + void setXRotation(int angle); + void setYRotation(int angle); + void setZRotation(int angle); + +signals: + void xRotationChanged(int angle); + void yRotationChanged(int angle); + void zRotationChanged(int angle); + +protected: + void initializeGL(); + void paintGL(); + void resizeGL(int width, int height); + void mousePressEvent(QMouseEvent *event); + void mouseMoveEvent(QMouseEvent *event); + +private slots: + void advanceGears(); + +private: + GLuint makeGear(const GLfloat *reflectance, GLdouble innerRadius, + GLdouble outerRadius, GLdouble thickness, + GLdouble toothSize, GLint toothCount); + void drawGear(GLuint gear, GLdouble dx, GLdouble dy, GLdouble dz, + GLdouble angle); + void normalizeAngle(int *angle); + + GLuint gear1; + GLuint gear2; + GLuint gear3; + int xRot; + int yRot; + int zRot; + int gear1Rot; + + QPoint lastPos; +}; + +#endif diff --git a/examples/opengl/grabber/grabber.pro b/examples/opengl/grabber/grabber.pro new file mode 100644 index 0000000..15eaf02 --- /dev/null +++ b/examples/opengl/grabber/grabber.pro @@ -0,0 +1,12 @@ +HEADERS = glwidget.h \ + mainwindow.h +SOURCES = glwidget.cpp \ + main.cpp \ + mainwindow.cpp +QT += opengl + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/opengl/grabber +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS grabber.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/opengl/grabber +INSTALLS += target sources diff --git a/examples/opengl/grabber/main.cpp b/examples/opengl/grabber/main.cpp new file mode 100644 index 0000000..4856752 --- /dev/null +++ b/examples/opengl/grabber/main.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> + +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + MainWindow mainWin; + mainWin.show(); + return app.exec(); +} diff --git a/examples/opengl/grabber/mainwindow.cpp b/examples/opengl/grabber/mainwindow.cpp new file mode 100644 index 0000000..7e878f9 --- /dev/null +++ b/examples/opengl/grabber/mainwindow.cpp @@ -0,0 +1,207 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> +#include <QtOpenGL> + +#include "glwidget.h" +#include "mainwindow.h" + +MainWindow::MainWindow() +{ + centralWidget = new QWidget; + setCentralWidget(centralWidget); + + glWidget = new GLWidget; + pixmapLabel = new QLabel; + + glWidgetArea = new QScrollArea; + glWidgetArea->setWidget(glWidget); + glWidgetArea->setWidgetResizable(true); + glWidgetArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + glWidgetArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + glWidgetArea->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); + glWidgetArea->setMinimumSize(50, 50); + + pixmapLabelArea = new QScrollArea; + pixmapLabelArea->setWidget(pixmapLabel); + pixmapLabelArea->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); + pixmapLabelArea->setMinimumSize(50, 50); + + xSlider = createSlider(SIGNAL(xRotationChanged(int)), + SLOT(setXRotation(int))); + ySlider = createSlider(SIGNAL(yRotationChanged(int)), + SLOT(setYRotation(int))); + zSlider = createSlider(SIGNAL(zRotationChanged(int)), + SLOT(setZRotation(int))); + + createActions(); + createMenus(); + + QGridLayout *centralLayout = new QGridLayout; + centralLayout->addWidget(glWidgetArea, 0, 0); + centralLayout->addWidget(pixmapLabelArea, 0, 1); + centralLayout->addWidget(xSlider, 1, 0, 1, 2); + centralLayout->addWidget(ySlider, 2, 0, 1, 2); + centralLayout->addWidget(zSlider, 3, 0, 1, 2); + centralWidget->setLayout(centralLayout); + + xSlider->setValue(15 * 16); + ySlider->setValue(345 * 16); + zSlider->setValue(0 * 16); + + setWindowTitle(tr("Grabber")); + resize(400, 300); +} + +void MainWindow::renderIntoPixmap() +{ + QSize size = getSize(); + if (size.isValid()) { + QPixmap pixmap = glWidget->renderPixmap(size.width(), size.height()); + setPixmap(pixmap); + } +} + +void MainWindow::grabFrameBuffer() +{ + QImage image = glWidget->grabFrameBuffer(); + setPixmap(QPixmap::fromImage(image)); +} + +void MainWindow::clearPixmap() +{ + setPixmap(QPixmap()); +} + +void MainWindow::about() +{ + QMessageBox::about(this, tr("About Grabber"), + tr("The <b>Grabber</b> example demonstrates two approaches for " + "rendering OpenGL into a Qt pixmap.")); +} + +void MainWindow::createActions() +{ + renderIntoPixmapAct = new QAction(tr("&Render into Pixmap..."), this); + renderIntoPixmapAct->setShortcut(tr("Ctrl+R")); + connect(renderIntoPixmapAct, SIGNAL(triggered()), + this, SLOT(renderIntoPixmap())); + + grabFrameBufferAct = new QAction(tr("&Grab Frame Buffer"), this); + grabFrameBufferAct->setShortcut(tr("Ctrl+G")); + connect(grabFrameBufferAct, SIGNAL(triggered()), + this, SLOT(grabFrameBuffer())); + + clearPixmapAct = new QAction(tr("&Clear Pixmap"), this); + clearPixmapAct->setShortcut(tr("Ctrl+L")); + connect(clearPixmapAct, SIGNAL(triggered()), this, SLOT(clearPixmap())); + + exitAct = new QAction(tr("E&xit"), this); + exitAct->setShortcut(tr("Ctrl+Q")); + connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); + + aboutAct = new QAction(tr("&About"), this); + connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); + + aboutQtAct = new QAction(tr("About &Qt"), this); + connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); +} + +void MainWindow::createMenus() +{ + fileMenu = menuBar()->addMenu(tr("&File")); + fileMenu->addAction(renderIntoPixmapAct); + fileMenu->addAction(grabFrameBufferAct); + fileMenu->addAction(clearPixmapAct); + fileMenu->addSeparator(); + fileMenu->addAction(exitAct); + + helpMenu = menuBar()->addMenu(tr("&Help")); + helpMenu->addAction(aboutAct); + helpMenu->addAction(aboutQtAct); +} + +QSlider *MainWindow::createSlider(const char *changedSignal, + const char *setterSlot) +{ + QSlider *slider = new QSlider(Qt::Horizontal); + slider->setRange(0, 360 * 16); + slider->setSingleStep(16); + slider->setPageStep(15 * 16); + slider->setTickInterval(15 * 16); + slider->setTickPosition(QSlider::TicksRight); + connect(slider, SIGNAL(valueChanged(int)), glWidget, setterSlot); + connect(glWidget, changedSignal, slider, SLOT(setValue(int))); + return slider; +} + +void MainWindow::setPixmap(const QPixmap &pixmap) +{ + pixmapLabel->setPixmap(pixmap); + QSize size = pixmap.size(); + if (size - QSize(1, 0) == pixmapLabelArea->maximumViewportSize()) + size -= QSize(1, 0); + pixmapLabel->resize(size); +} + +QSize MainWindow::getSize() +{ + bool ok; + QString text = QInputDialog::getText(this, tr("Grabber"), + tr("Enter pixmap size:"), + QLineEdit::Normal, + tr("%1 x %2").arg(glWidget->width()) + .arg(glWidget->height()), + &ok); + if (!ok) + return QSize(); + + QRegExp regExp(tr("([0-9]+) *x *([0-9]+)")); + if (regExp.exactMatch(text)) { + int width = regExp.cap(1).toInt(); + int height = regExp.cap(2).toInt(); + if (width > 0 && width < 2048 && height > 0 && height < 2048) + return QSize(width, height); + } + + return glWidget->size(); +} diff --git a/examples/opengl/grabber/mainwindow.h b/examples/opengl/grabber/mainwindow.h new file mode 100644 index 0000000..8f77747 --- /dev/null +++ b/examples/opengl/grabber/mainwindow.h @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QMainWindow> + +QT_BEGIN_NAMESPACE +class QAction; +class QLabel; +class QMenu; +class QScrollArea; +class QSlider; +QT_END_NAMESPACE +class GLWidget; + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); + +private slots: + void renderIntoPixmap(); + void grabFrameBuffer(); + void clearPixmap(); + void about(); + +private: + void createActions(); + void createMenus(); + QSlider *createSlider(const char *changedSignal, const char *setterSlot); + void setPixmap(const QPixmap &pixmap); + QSize getSize(); + + QWidget *centralWidget; + QScrollArea *glWidgetArea; + QScrollArea *pixmapLabelArea; + GLWidget *glWidget; + QLabel *pixmapLabel; + QSlider *xSlider; + QSlider *ySlider; + QSlider *zSlider; + + QMenu *fileMenu; + QMenu *helpMenu; + QAction *grabFrameBufferAct; + QAction *renderIntoPixmapAct; + QAction *clearPixmapAct; + QAction *exitAct; + QAction *aboutAct; + QAction *aboutQtAct; +}; + +#endif diff --git a/examples/opengl/hellogl/glwidget.cpp b/examples/opengl/hellogl/glwidget.cpp new file mode 100644 index 0000000..d81e7e6 --- /dev/null +++ b/examples/opengl/hellogl/glwidget.cpp @@ -0,0 +1,266 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> +#include <QtOpenGL> + +#include <math.h> + +#include "glwidget.h" + +//! [0] +GLWidget::GLWidget(QWidget *parent) + : QGLWidget(parent) +{ + object = 0; + xRot = 0; + yRot = 0; + zRot = 0; + + trolltechGreen = QColor::fromCmykF(0.40, 0.0, 1.0, 0.0); + trolltechPurple = QColor::fromCmykF(0.39, 0.39, 0.0, 0.0); +} +//! [0] + +//! [1] +GLWidget::~GLWidget() +{ + makeCurrent(); + glDeleteLists(object, 1); +} +//! [1] + +//! [2] +QSize GLWidget::minimumSizeHint() const +{ + return QSize(50, 50); +} +//! [2] + +//! [3] +QSize GLWidget::sizeHint() const +//! [3] //! [4] +{ + return QSize(400, 400); +} +//! [4] + +//! [5] +void GLWidget::setXRotation(int angle) +{ + normalizeAngle(&angle); + if (angle != xRot) { + xRot = angle; + emit xRotationChanged(angle); + updateGL(); + } +} +//! [5] + +void GLWidget::setYRotation(int angle) +{ + normalizeAngle(&angle); + if (angle != yRot) { + yRot = angle; + emit yRotationChanged(angle); + updateGL(); + } +} + +void GLWidget::setZRotation(int angle) +{ + normalizeAngle(&angle); + if (angle != zRot) { + zRot = angle; + emit zRotationChanged(angle); + updateGL(); + } +} + +//! [6] +void GLWidget::initializeGL() +{ + qglClearColor(trolltechPurple.dark()); + object = makeObject(); + glShadeModel(GL_FLAT); + glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); +} +//! [6] + +//! [7] +void GLWidget::paintGL() +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glLoadIdentity(); + glTranslated(0.0, 0.0, -10.0); + glRotated(xRot / 16.0, 1.0, 0.0, 0.0); + glRotated(yRot / 16.0, 0.0, 1.0, 0.0); + glRotated(zRot / 16.0, 0.0, 0.0, 1.0); + glCallList(object); +} +//! [7] + +//! [8] +void GLWidget::resizeGL(int width, int height) +{ + int side = qMin(width, height); + glViewport((width - side) / 2, (height - side) / 2, side, side); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0); + glMatrixMode(GL_MODELVIEW); +} +//! [8] + +//! [9] +void GLWidget::mousePressEvent(QMouseEvent *event) +{ + lastPos = event->pos(); +} +//! [9] + +//! [10] +void GLWidget::mouseMoveEvent(QMouseEvent *event) +{ + int dx = event->x() - lastPos.x(); + int dy = event->y() - lastPos.y(); + + if (event->buttons() & Qt::LeftButton) { + setXRotation(xRot + 8 * dy); + setYRotation(yRot + 8 * dx); + } else if (event->buttons() & Qt::RightButton) { + setXRotation(xRot + 8 * dy); + setZRotation(zRot + 8 * dx); + } + lastPos = event->pos(); +} +//! [10] + +GLuint GLWidget::makeObject() +{ + GLuint list = glGenLists(1); + glNewList(list, GL_COMPILE); + + glBegin(GL_QUADS); + + GLdouble x1 = +0.06; + GLdouble y1 = -0.14; + GLdouble x2 = +0.14; + GLdouble y2 = -0.06; + GLdouble x3 = +0.08; + GLdouble y3 = +0.00; + GLdouble x4 = +0.30; + GLdouble y4 = +0.22; + + quad(x1, y1, x2, y2, y2, x2, y1, x1); + quad(x3, y3, x4, y4, y4, x4, y3, x3); + + extrude(x1, y1, x2, y2); + extrude(x2, y2, y2, x2); + extrude(y2, x2, y1, x1); + extrude(y1, x1, x1, y1); + extrude(x3, y3, x4, y4); + extrude(x4, y4, y4, x4); + extrude(y4, x4, y3, x3); + + const double Pi = 3.14159265358979323846; + const int NumSectors = 200; + + for (int i = 0; i < NumSectors; ++i) { + double angle1 = (i * 2 * Pi) / NumSectors; + GLdouble x5 = 0.30 * sin(angle1); + GLdouble y5 = 0.30 * cos(angle1); + GLdouble x6 = 0.20 * sin(angle1); + GLdouble y6 = 0.20 * cos(angle1); + + double angle2 = ((i + 1) * 2 * Pi) / NumSectors; + GLdouble x7 = 0.20 * sin(angle2); + GLdouble y7 = 0.20 * cos(angle2); + GLdouble x8 = 0.30 * sin(angle2); + GLdouble y8 = 0.30 * cos(angle2); + + quad(x5, y5, x6, y6, x7, y7, x8, y8); + + extrude(x6, y6, x7, y7); + extrude(x8, y8, x5, y5); + } + + glEnd(); + + glEndList(); + return list; +} + +void GLWidget::quad(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2, + GLdouble x3, GLdouble y3, GLdouble x4, GLdouble y4) +{ + qglColor(trolltechGreen); + + glVertex3d(x1, y1, -0.05); + glVertex3d(x2, y2, -0.05); + glVertex3d(x3, y3, -0.05); + glVertex3d(x4, y4, -0.05); + + glVertex3d(x4, y4, +0.05); + glVertex3d(x3, y3, +0.05); + glVertex3d(x2, y2, +0.05); + glVertex3d(x1, y1, +0.05); +} + +void GLWidget::extrude(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2) +{ + qglColor(trolltechGreen.dark(250 + int(100 * x1))); + + glVertex3d(x1, y1, +0.05); + glVertex3d(x2, y2, +0.05); + glVertex3d(x2, y2, -0.05); + glVertex3d(x1, y1, -0.05); +} + +void GLWidget::normalizeAngle(int *angle) +{ + while (*angle < 0) + *angle += 360 * 16; + while (*angle > 360 * 16) + *angle -= 360 * 16; +} diff --git a/examples/opengl/hellogl/glwidget.h b/examples/opengl/hellogl/glwidget.h new file mode 100644 index 0000000..a5f73ee --- /dev/null +++ b/examples/opengl/hellogl/glwidget.h @@ -0,0 +1,99 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef GLWIDGET_H +#define GLWIDGET_H + +#include <QGLWidget> + +//! [0] +class GLWidget : public QGLWidget +{ + Q_OBJECT + +public: + GLWidget(QWidget *parent = 0); + ~GLWidget(); + + QSize minimumSizeHint() const; + QSize sizeHint() const; +//! [0] + +//! [1] +public slots: + void setXRotation(int angle); + void setYRotation(int angle); + void setZRotation(int angle); + +signals: + void xRotationChanged(int angle); + void yRotationChanged(int angle); + void zRotationChanged(int angle); +//! [1] + +//! [2] +protected: + void initializeGL(); + void paintGL(); + void resizeGL(int width, int height); + void mousePressEvent(QMouseEvent *event); + void mouseMoveEvent(QMouseEvent *event); +//! [2] + +//! [3] +private: + GLuint makeObject(); + void quad(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2, + GLdouble x3, GLdouble y3, GLdouble x4, GLdouble y4); + void extrude(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2); + void normalizeAngle(int *angle); + + GLuint object; + int xRot; + int yRot; + int zRot; + QPoint lastPos; + QColor trolltechGreen; + QColor trolltechPurple; +}; +//! [3] + +#endif diff --git a/examples/opengl/hellogl/hellogl.pro b/examples/opengl/hellogl/hellogl.pro new file mode 100644 index 0000000..ce9c8e8 --- /dev/null +++ b/examples/opengl/hellogl/hellogl.pro @@ -0,0 +1,12 @@ +HEADERS = glwidget.h \ + window.h +SOURCES = glwidget.cpp \ + main.cpp \ + window.cpp +QT += opengl + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/opengl/hellogl +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS hellogl.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/opengl/hellogl +INSTALLS += target sources diff --git a/examples/opengl/hellogl/main.cpp b/examples/opengl/hellogl/main.cpp new file mode 100644 index 0000000..fa8b0ab --- /dev/null +++ b/examples/opengl/hellogl/main.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> + +#include "window.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + Window window; + window.show(); + return app.exec(); +} diff --git a/examples/opengl/hellogl/window.cpp b/examples/opengl/hellogl/window.cpp new file mode 100644 index 0000000..82031ee --- /dev/null +++ b/examples/opengl/hellogl/window.cpp @@ -0,0 +1,90 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#include "glwidget.h" +#include "window.h" + +//! [0] +Window::Window() +{ + glWidget = new GLWidget; + + xSlider = createSlider(); + ySlider = createSlider(); + zSlider = createSlider(); + + connect(xSlider, SIGNAL(valueChanged(int)), glWidget, SLOT(setXRotation(int))); + connect(glWidget, SIGNAL(xRotationChanged(int)), xSlider, SLOT(setValue(int))); + connect(ySlider, SIGNAL(valueChanged(int)), glWidget, SLOT(setYRotation(int))); + connect(glWidget, SIGNAL(yRotationChanged(int)), ySlider, SLOT(setValue(int))); + connect(zSlider, SIGNAL(valueChanged(int)), glWidget, SLOT(setZRotation(int))); + connect(glWidget, SIGNAL(zRotationChanged(int)), zSlider, SLOT(setValue(int))); +//! [0] + +//! [1] + QHBoxLayout *mainLayout = new QHBoxLayout; + mainLayout->addWidget(glWidget); + mainLayout->addWidget(xSlider); + mainLayout->addWidget(ySlider); + mainLayout->addWidget(zSlider); + setLayout(mainLayout); + + xSlider->setValue(15 * 16); + ySlider->setValue(345 * 16); + zSlider->setValue(0 * 16); + setWindowTitle(tr("Hello GL")); +} +//! [1] + +//! [2] +QSlider *Window::createSlider() +{ + QSlider *slider = new QSlider(Qt::Vertical); + slider->setRange(0, 360 * 16); + slider->setSingleStep(16); + slider->setPageStep(15 * 16); + slider->setTickInterval(15 * 16); + slider->setTickPosition(QSlider::TicksRight); + return slider; +} +//! [2] diff --git a/examples/opengl/hellogl/window.h b/examples/opengl/hellogl/window.h new file mode 100644 index 0000000..b20f8a8 --- /dev/null +++ b/examples/opengl/hellogl/window.h @@ -0,0 +1,70 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef WINDOW_H +#define WINDOW_H + +#include <QWidget> + +QT_BEGIN_NAMESPACE +class QSlider; +QT_END_NAMESPACE +//! [0] +class GLWidget; + +class Window : public QWidget +{ + Q_OBJECT + +public: + Window(); + +private: + QSlider *createSlider(); + + GLWidget *glWidget; + QSlider *xSlider; + QSlider *ySlider; + QSlider *zSlider; +}; +//! [0] + +#endif diff --git a/examples/opengl/hellogl_es/bubble.cpp b/examples/opengl/hellogl_es/bubble.cpp new file mode 100644 index 0000000..691d314 --- /dev/null +++ b/examples/opengl/hellogl_es/bubble.cpp @@ -0,0 +1,140 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#include "bubble.h" + +Bubble::Bubble(const QPointF &position, qreal radius, const QPointF &velocity) + : position(position), vel(velocity), radius(radius) +{ + innerColor = randomColor(); + outerColor = randomColor(); + cache = 0; + updateBrush(); +} + +//! [0] +void Bubble::updateCache() +{ + if (cache) + delete cache; + cache = new QImage(qRound(radius * 2 + 2), qRound(radius * 2 + 2), QImage::Format_ARGB32); + cache->fill(0x00000000); + QPainter p(cache); + p.setRenderHint(QPainter::HighQualityAntialiasing); + QPen pen(Qt::white); + pen.setWidth(2); + p.setPen(pen); + p.setBrush(brush); + p.drawEllipse(0, 0, int(2*radius), int(2*radius)); +} +//! [0] + +Bubble::~Bubble() +{ + if (cache) + delete cache; +} + +void Bubble::updateBrush() +{ + QRadialGradient gradient(QPointF(radius, radius), radius, + QPointF(radius*0.5, radius*0.5)); + + gradient.setColorAt(0, QColor(255, 255, 255, 255)); + gradient.setColorAt(0.25, innerColor); + gradient.setColorAt(1, outerColor); + brush = QBrush(gradient); + updateCache(); +} + +//! [1] +void Bubble::drawBubble(QPainter *painter) +{ + painter->save(); + painter->translate(position.x() - radius, position.y() - radius); + painter->setOpacity(0.8); + painter->drawImage(0, 0, *cache); + painter->restore(); +} +//! [1] + +QColor Bubble::randomColor() +{ + int red = int(185 + 70.0*qrand()/(RAND_MAX+1.0)); + int green = int(185 + 70.0*qrand()/(RAND_MAX+1.0)); + int blue = int(205 + 50.0*qrand()/(RAND_MAX+1.0)); + int alpha = int(91 + 100.0*qrand()/(RAND_MAX+1.0)); + + return QColor(red, green, blue, alpha); +} + +void Bubble::move(const QRect &bbox) +{ + position += vel; + qreal leftOverflow = position.x() - radius - bbox.left(); + qreal rightOverflow = position.x() + radius - bbox.right(); + qreal topOverflow = position.y() - radius - bbox.top(); + qreal bottomOverflow = position.y() + radius - bbox.bottom(); + + if (leftOverflow < 0.0) { + position.setX(position.x() - 2 * leftOverflow); + vel.setX(-vel.x()); + } else if (rightOverflow > 0.0) { + position.setX(position.x() - 2 * rightOverflow); + vel.setX(-vel.x()); + } + + if (topOverflow < 0.0) { + position.setY(position.y() - 2 * topOverflow); + vel.setY(-vel.y()); + } else if (bottomOverflow > 0.0) { + position.setY(position.y() - 2 * bottomOverflow); + vel.setY(-vel.y()); + } +} + +QRectF Bubble::rect() +{ + return QRectF(position.x() - radius, position.y() - radius, + 2 * radius, 2 * radius); +} diff --git a/examples/opengl/hellogl_es/bubble.h b/examples/opengl/hellogl_es/bubble.h new file mode 100644 index 0000000..a32754f --- /dev/null +++ b/examples/opengl/hellogl_es/bubble.h @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef BUBBLE_H +#define BUBBLE_H + +#include <QBrush> +#include <QColor> +#include <QPointF> +#include <QRect> +#include <QRectF> + +class QPainter; + +class Bubble +{ +public: + Bubble(const QPointF &position, qreal radius, const QPointF &velocity); + ~Bubble(); + + void drawBubble(QPainter *painter); + void updateBrush(); + void move(const QRect &bbox); + void updateCache(); + QRectF rect(); + +private: + QColor randomColor(); + + QBrush brush; + QPointF position; + QPointF vel; + qreal radius; + QColor innerColor; + QColor outerColor; + QImage *cache; +}; + +#endif diff --git a/examples/opengl/hellogl_es/cl_helper.h b/examples/opengl/hellogl_es/cl_helper.h new file mode 100644 index 0000000..416c9bb --- /dev/null +++ b/examples/opengl/hellogl_es/cl_helper.h @@ -0,0 +1,133 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifdef QT_OPENGL_ES_1_CL + +//! [0] +#define FLOAT2X(f) ((int) ( (f) * (65536))) +#define X2FLOAT(x) ((float)(x) / 65536.0f) + +#define f2vt(f) FLOAT2X(f) +#define vt2f(x) X2FLOAT(x) + +#define q_vertexType GLfixed +#define q_vertexTypeEnum GL_FIXED + +#define q_glFog glFogx +#define q_glFogv glFogxv +//! [0] + +#define q_glLight glLightx +#define q_glLightv glLightxv +#define q_glLightModel glLightModelx +#define q_glLightModelv glLightModelxv + +#define q_glAlphaFunc glAlphaFuncx + +#define q_glMaterial glMaterialx +#define q_glMaterialv glMaterialxv +#define q_glColor4 glColor4x + +#define q_glTexParameter glTexParameterx +#define q_glTexEnv glTexEnvx + +#define q_glOrtho glOrthox +#define q_glFrustum glFrustumx + +#define q_glTranslate glTranslatex +#define q_glScale glScalex +#define q_glRotate glRotatex +#define q_glLoadMatrix glLoadMatrixx + +#define q_glClearColor glClearColorx + +#define q_glMultMatrix glMultMatrixx + +#define q_glNormal3 glNormal3x + +#define q_glPolygonOffset glPolygonOffsetx +#define q_glPointSize glPointSizex + +//! [1] +#else + +#define f2vt(f) (f) +#define vt2f(x) (x) + +#define q_vertexType GLfloat +#define q_vertexTypeEnum GL_FLOAT + +#define q_glFog glFogf +#define q_glFogv glFogfv +//! [1] + +#define q_glLight glLightf +#define q_glLightv glLightfv +#define q_glLightModel glLightModelf +#define q_glLightModelv glLightModelfv + +#define q_glAlphaFunc glAlphaFuncf + +#define q_glMaterial glMaterialf +#define q_glMaterialv glMaterialfv +#define q_glColor4 glColor4f + +#define q_glTexParameter glTexParameterf +#define q_glTexEnv glTexEnvf + +#define q_glOrtho glOrthof +#define q_glFrustum glFrustumf + +#define q_glTranslate glTranslatef +#define q_glScale glScalef +#define q_glRotate glRotatef +#define q_glLoadMatrix glLoadMatrixf + +#define q_glClearColor glClearColor + +#define q_glMultMatrix glMultMatrixf + +#define q_glNormal3 glNormal3f + +#define q_glPolygonOffset glPolygonOffsetf +#define q_glPointSize glPointSizef + +#endif diff --git a/examples/opengl/hellogl_es/glwidget.cpp b/examples/opengl/hellogl_es/glwidget.cpp new file mode 100644 index 0000000..6098c99 --- /dev/null +++ b/examples/opengl/hellogl_es/glwidget.cpp @@ -0,0 +1,457 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "glwidget.h" +#include <QPainter> +#include <math.h> + +#include "bubble.h" +#include "cl_helper.h" + + +const int bubbleNum = 8; + +inline void CrossProduct(qreal &xOut, qreal &yOut, qreal &zOut, qreal x1, qreal y1, qreal z1, qreal x2, qreal y2, qreal z2) +{ + xOut = y1 * z2 - z1 * y2; + yOut = z1 * x2 - x1 * z2; + zOut = x1 * y2 - y1 * x2; +} + +inline void Normalize(qreal &x, qreal &y, qreal &z) +{ + qreal l = sqrt(x*x + y*y + z*z); + x = x / l; + y = y / l; + z = z / l; +} + +GLWidget::GLWidget(QWidget *parent) + : QGLWidget(parent) +{ + qtLogo = true; + createdVertices = 0; + createdNormals = 0; + m_vertexNumber = 0; + frames = 0; + setAttribute(Qt::WA_PaintOnScreen); + setAttribute(Qt::WA_NoSystemBackground); + setAutoBufferSwap(false); + m_showBubbles = true; +} + +GLWidget::~GLWidget() +{ + if (createdVertices) + delete[] createdVertices; + if (createdNormals) + delete[] createdNormals; +} + +void GLWidget::setScaling(int scale) { + + if (scale > 50) + m_fScale = 1 + qreal(scale -50) / 50 * 0.5; + else if (scale < 50) + m_fScale = 1- (qreal(50 - scale) / 50 * 1/2); + else + m_fScale = 1; +} + +void GLWidget::setLogo() { + qtLogo = true; +} + +void GLWidget::setTexture() { + qtLogo = false; +} + +void GLWidget::showBubbles(bool bubbles) +{ + m_showBubbles = bubbles; +} + +//! [2] +void GLWidget::paintQtLogo() +{ + glDisable(GL_TEXTURE_2D); + glEnableClientState(GL_VERTEX_ARRAY); + glVertexPointer(3,q_vertexTypeEnum,0, createdVertices); + glEnableClientState(GL_NORMAL_ARRAY); + glNormalPointer(q_vertexTypeEnum,0,createdNormals); + glDrawArrays(GL_TRIANGLES, 0, m_vertexNumber / 3); +} +//! [2] + +void GLWidget::paintTexturedCube() +{ + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, m_uiTexture); + q_vertexType afVertices[] = { + f2vt(-0.5), f2vt(0.5), f2vt(0.5), f2vt(0.5),f2vt(-0.5),f2vt(0.5),f2vt(-0.5),f2vt(-0.5),f2vt(0.5), + f2vt(0.5), f2vt(-0.5), f2vt(0.5), f2vt(-0.5),f2vt(0.5),f2vt(0.5),f2vt(0.5),f2vt(0.5),f2vt(0.5), + f2vt(-0.5), f2vt(-0.5), f2vt(-0.5), f2vt(0.5),f2vt(-0.5),f2vt(-0.5),f2vt(-0.5),f2vt(0.5),f2vt(-0.5), + f2vt(0.5), f2vt(0.5), f2vt(-0.5), f2vt(-0.5),f2vt(0.5),f2vt(-0.5),f2vt(0.5),f2vt(-0.5),f2vt(-0.5), + + f2vt(0.5), f2vt(-0.5), f2vt(-0.5), f2vt(0.5),f2vt(-0.5),f2vt(0.5),f2vt(0.5),f2vt(0.5),f2vt(-0.5), + f2vt(0.5), f2vt(0.5), f2vt(0.5), f2vt(0.5),f2vt(0.5),f2vt(-0.5),f2vt(0.5),f2vt(-0.5),f2vt(0.5), + f2vt(-0.5), f2vt(0.5), f2vt(-0.5), f2vt(-0.5),f2vt(-0.5),f2vt(0.5),f2vt(-0.5),f2vt(-0.5),f2vt(-0.5), + f2vt(-0.5), f2vt(-0.5), f2vt(0.5), f2vt(-0.5),f2vt(0.5),f2vt(-0.5),f2vt(-0.5),f2vt(0.5),f2vt(0.5), + + f2vt(0.5), f2vt(0.5), f2vt(-0.5), f2vt(-0.5), f2vt(0.5), f2vt(0.5), f2vt(-0.5), f2vt(0.5), f2vt(-0.5), + f2vt(-0.5), f2vt(0.5), f2vt(0.5), f2vt(0.5), f2vt(0.5), f2vt(-0.5), f2vt(0.5), f2vt(0.5), f2vt(0.5), + f2vt(-0.5), f2vt(-0.5), f2vt(-0.5), f2vt(-0.5), f2vt(-0.5), f2vt(0.5), f2vt(0.5), f2vt(-0.5), f2vt(-0.5), + f2vt(0.5), f2vt(-0.5), f2vt(0.5), f2vt(0.5), f2vt(-0.5), f2vt(-0.5), f2vt(-0.5), f2vt(-0.5), f2vt(0.5) + }; + glEnableClientState(GL_VERTEX_ARRAY); + glVertexPointer(3,q_vertexTypeEnum,0,afVertices); + + q_vertexType afTexCoord[] = { + f2vt(0.0f),f2vt(0.0f), f2vt(1.0f),f2vt(1.0f), f2vt(1.0f),f2vt(0.0f), + f2vt(1.0f),f2vt(1.0f), f2vt(0.0f),f2vt(0.0f), f2vt(0.0f),f2vt(1.0f), + f2vt(1.0f),f2vt(1.0f), f2vt(1.0f),f2vt(0.0f), f2vt(0.0f),f2vt(1.0f), + f2vt(0.0f),f2vt(0.0f), f2vt(0.0f),f2vt(1.0f), f2vt(1.0f),f2vt(0.0f), + + f2vt(1.0f),f2vt(1.0f), f2vt(1.0f),f2vt(0.0f), f2vt(0.0f),f2vt(1.0f), + f2vt(0.0f),f2vt(0.0f), f2vt(0.0f),f2vt(1.0f), f2vt(1.0f),f2vt(0.0f), + f2vt(0.0f),f2vt(0.0f), f2vt(1.0f),f2vt(1.0f), f2vt(1.0f),f2vt(0.0f), + f2vt(1.0f),f2vt(1.0f), f2vt(0.0f),f2vt(0.0f), f2vt(0.0f),f2vt(1.0f), + + f2vt(0.0f),f2vt(1.0f), f2vt(1.0f),f2vt(0.0f), f2vt(1.0f),f2vt(1.0f), + f2vt(1.0f),f2vt(0.0f), f2vt(0.0f),f2vt(1.0f), f2vt(0.0f),f2vt(0.0f), + f2vt(1.0f),f2vt(0.0f), f2vt(1.0f),f2vt(1.0f), f2vt(0.0f),f2vt(0.0f), + f2vt(0.0f),f2vt(1.0f), f2vt(0.0f),f2vt(0.0f), f2vt(1.0f),f2vt(1.0f) + }; + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glTexCoordPointer(2,q_vertexTypeEnum,0,afTexCoord); + + q_vertexType afNormals[] = { + + f2vt(0),f2vt(0),f2vt(-1), f2vt(0),f2vt(0),f2vt(-1), f2vt(0),f2vt(0),f2vt(-1), + f2vt(0),f2vt(0),f2vt(-1), f2vt(0),f2vt(0),f2vt(-1), f2vt(0),f2vt(0),f2vt(-1), + f2vt(0),f2vt(0),f2vt(1), f2vt(0),f2vt(0),f2vt(1), f2vt(0),f2vt(0),f2vt(1), + f2vt(0),f2vt(0),f2vt(1), f2vt(0),f2vt(0),f2vt(1), f2vt(0),f2vt(0),f2vt(1), + + f2vt(-1),f2vt(0),f2vt(0), f2vt(-1),f2vt(0),f2vt(0), f2vt(-1),f2vt(0),f2vt(0), + f2vt(-1),f2vt(0),f2vt(0), f2vt(-1),f2vt(0),f2vt(0), f2vt(-1),f2vt(0),f2vt(0), + f2vt(1),f2vt(0),f2vt(0), f2vt(1),f2vt(0),f2vt(0), f2vt(1),f2vt(0),f2vt(0), + f2vt(1),f2vt(0),f2vt(0), f2vt(1),f2vt(0),f2vt(0), f2vt(1),f2vt(0),f2vt(0), + + f2vt(0),f2vt(-1),f2vt(0), f2vt(0),f2vt(-1),f2vt(0), f2vt(0),f2vt(-1),f2vt(0), + f2vt(0),f2vt(-1),f2vt(0), f2vt(0),f2vt(-1),f2vt(0), f2vt(0),f2vt(-1),f2vt(0), + f2vt(0),f2vt(1),f2vt(0), f2vt(0),f2vt(1),f2vt(0), f2vt(0),f2vt(1),f2vt(0), + f2vt(0),f2vt(1),f2vt(0), f2vt(0),f2vt(1),f2vt(0), f2vt(0),f2vt(1),f2vt(0) + }; + glEnableClientState(GL_NORMAL_ARRAY); + glNormalPointer(q_vertexTypeEnum,0,afNormals); + + glDrawArrays(GL_TRIANGLES, 0, 36); +} + +void GLWidget::initializeGL () +{ + q_glClearColor(f2vt(0.1f), f2vt(0.1f), f2vt(0.2f), f2vt(1.0f)); + + glEnable(GL_TEXTURE_2D); + glGenTextures(1, &m_uiTexture); + m_uiTexture = bindTexture(QImage(":/qt.png")); + + q_glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); + q_glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); + glEnable(GL_LIGHTING); + glEnable(GL_LIGHT0); + + q_vertexType aLightPosition[] = {f2vt(0.0f),f2vt(0.3f),f2vt(1.0f),f2vt(0.0f)}; + + q_glLightv(GL_LIGHT0, GL_SPOT_DIRECTION, aLightPosition); + m_fAngle = 0; + m_fScale = 1; + createGeometry(); + createBubbles(bubbleNum - bubbles.count()); +} + +void GLWidget::paintGL() +{ + createBubbles(bubbleNum - bubbles.count()); + +//! [3] + QPainter painter; + painter.begin(this); + + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); +//! [3] + +//! [4] + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glMatrixMode(GL_TEXTURE); + glPushMatrix(); + + //Since OpenGL ES does not support glPush/PopAttrib(GL_ALL_ATTRIB_BITS) + //we have to take care of the states ourselves + + q_glClearColor(f2vt(0.1f), f2vt(0.1f), f2vt(0.2f), f2vt(1.0f)); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glEnable(GL_TEXTURE_2D); + + q_glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); + q_glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); + glEnable(GL_LIGHTING); + glEnable(GL_LIGHT0); + + glShadeModel(GL_FLAT); + glFrontFace(GL_CW); + glCullFace(GL_FRONT); + glEnable(GL_CULL_FACE); + glEnable(GL_DEPTH_TEST); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + q_glRotate(f2vt(m_fAngle), f2vt(0.0), f2vt(1.0), f2vt(0.0)); + q_glRotate(f2vt(m_fAngle), f2vt(1.0), f2vt(0.0), f2vt(0.0)); + q_glRotate(f2vt(m_fAngle), f2vt(0.0), f2vt(0.0), f2vt(1.0)); + q_glScale(f2vt(m_fScale), f2vt(m_fScale),f2vt(m_fScale)); + q_glTranslate(f2vt(0),f2vt(-0.2),f2vt(0)); + + q_vertexType matDiff[] = {f2vt(0.40), f2vt(1.0), f2vt(0.0), f2vt(1.0)}; + q_glMaterialv(GL_FRONT_AND_BACK, GL_DIFFUSE, matDiff); + + if (qtLogo) + paintQtLogo(); + else + paintTexturedCube(); +//! [4] + +//![5] + glMatrixMode(GL_MODELVIEW); + glPopMatrix(); + glMatrixMode(GL_PROJECTION); + glPopMatrix(); + glMatrixMode(GL_TEXTURE); + glPopMatrix(); + + glDisable(GL_LIGHTING); + glDisable(GL_LIGHT0); + + glDisable(GL_DEPTH_TEST); + glDisable(GL_CULL_FACE); +//![5] + +//! [6] + if (m_showBubbles) + foreach (Bubble *bubble, bubbles) { + bubble->drawBubble(&painter); + } +//! [6] + +//! [7] + QString framesPerSecond; + framesPerSecond.setNum(frames /(time.elapsed() / 1000.0), 'f', 2); + + painter.setPen(Qt::white); + + painter.drawText(20, 40, framesPerSecond + " fps"); + + painter.end(); +//! [7] + +//! [8] + swapBuffers(); +//! [8] + + QMutableListIterator<Bubble*> iter(bubbles); + + while (iter.hasNext()) { + Bubble *bubble = iter.next(); + bubble->move(rect()); + } + if (!(frames % 100)) { + time.start(); + frames = 0; + } + m_fAngle += 1.0f; + frames ++; +} + +void GLWidget::createBubbles(int number) +{ + for (int i = 0; i < number; ++i) { + QPointF position(width()*(0.1 + (0.8*qrand()/(RAND_MAX+1.0))), + height()*(0.1 + (0.8*qrand()/(RAND_MAX+1.0)))); + qreal radius = qMin(width(), height())*(0.0175 + 0.0875*qrand()/(RAND_MAX+1.0)); + QPointF velocity(width()*0.0175*(-0.5 + qrand()/(RAND_MAX+1.0)), + height()*0.0175*(-0.5 + qrand()/(RAND_MAX+1.0))); + + bubbles.append(new Bubble(position, radius, velocity)); + } +} + +void GLWidget::createGeometry() +{ + vertices.clear(); + normals.clear(); + + qreal x1 = +0.06f; + qreal y1 = -0.14f; + qreal x2 = +0.14f; + qreal y2 = -0.06f; + qreal x3 = +0.08f; + qreal y3 = +0.00f; + qreal x4 = +0.30f; + qreal y4 = +0.22f; + + quad(x1, y1, x2, y2, y2, x2, y1, x1); + quad(x3, y3, x4, y4, y4, x4, y3, x3); + + extrude(x1, y1, x2, y2); + extrude(x2, y2, y2, x2); + extrude(y2, x2, y1, x1); + extrude(y1, x1, x1, y1); + extrude(x3, y3, x4, y4); + extrude(x4, y4, y4, x4); + extrude(y4, x4, y3, x3); + + const qreal Pi = 3.14159f; + const int NumSectors = 100; + + for (int i = 0; i < NumSectors; ++i) { + qreal angle1 = (i * 2 * Pi) / NumSectors; + qreal x5 = 0.30 * sin(angle1); + qreal y5 = 0.30 * cos(angle1); + qreal x6 = 0.20 * sin(angle1); + qreal y6 = 0.20 * cos(angle1); + + qreal angle2 = ((i + 1) * 2 * Pi) / NumSectors; + qreal x7 = 0.20 * sin(angle2); + qreal y7 = 0.20 * cos(angle2); + qreal x8 = 0.30 * sin(angle2); + qreal y8 = 0.30 * cos(angle2); + + quad(x5, y5, x6, y6, x7, y7, x8, y8); + + extrude(x6, y6, x7, y7); + extrude(x8, y8, x5, y5); + } + +//! [1] + m_vertexNumber = vertices.size(); + createdVertices = new q_vertexType[m_vertexNumber]; + createdNormals = new q_vertexType[m_vertexNumber]; + for (int i = 0;i < m_vertexNumber;i++) { + createdVertices[i] = f2vt(vertices.at(i) * 2); + createdNormals[i] = f2vt(normals.at(i)); + } + vertices.clear(); + normals.clear(); +} +//! [1] + +//! [0] +void GLWidget::quad(qreal x1, qreal y1, qreal x2, qreal y2, qreal x3, qreal y3, qreal x4, qreal y4) +{ + qreal nx, ny, nz; + + vertices << x1 << y1 << -0.05f; + vertices << x2 << y2 << -0.05f; + vertices << x4 << y4 << -0.05f; + + vertices << x3 << y3 << -0.05f; + vertices << x4 << y4 << -0.05f; + vertices << x2 << y2 << -0.05f; + + CrossProduct(nx, ny, nz, x2 - x1, y2 - y1, 0, x4 - x1, y4 - y1, 0); + Normalize(nx, ny, nz); + + normals << nx << ny << nz; + normals << nx << ny << nz; + normals << nx << ny << nz; + + normals << nx << ny << nz; + normals << nx << ny << nz; + normals << nx << ny << nz; + + vertices << x4 << y4 << 0.05f; + vertices << x2 << y2 << 0.05f; + vertices << x1 << y1 << 0.05f; + + vertices << x2 << y2 << 0.05f; + vertices << x4 << y4 << 0.05f; + vertices << x3 << y3 << 0.05f; + + CrossProduct(nx, ny, nz, x2 - x4, y2 - y4, 0, x1 - x4, y1 - y4, 0); + Normalize(nx, ny, nz); + + normals << nx << ny << nz; + normals << nx << ny << nz; + normals << nx << ny << nz; + + normals << nx << ny << nz; + normals << nx << ny << nz; + normals << nx << ny << nz; +} +//! [0] + +void GLWidget::extrude(qreal x1, qreal y1, qreal x2, qreal y2) +{ + qreal nx, ny, nz; + + vertices << x1 << y1 << +0.05f; + vertices << x2 << y2 << +0.05f; + vertices << x1 << y1 << -0.05f; + + vertices << x2 << y2 << -0.05f; + vertices << x1 << y1 << -0.05f; + vertices << x2 << y2 << +0.05f; + + CrossProduct(nx, ny, nz, x2 - x1, y2 - y1, 0.0f, 0.0f, 0.0f, -0.1f); + Normalize(nx, ny, nz); + + normals << nx << ny << nz; + normals << nx << ny << nz; + normals << nx << ny << nz; + + normals << nx << ny << nz; + normals << nx << ny << nz; + normals << nx << ny << nz; +} diff --git a/examples/opengl/hellogl_es/glwidget.h b/examples/opengl/hellogl_es/glwidget.h new file mode 100644 index 0000000..954342e --- /dev/null +++ b/examples/opengl/hellogl_es/glwidget.h @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef GLWIDGET_H +#define GLWIDGET_H + +#include <QGLWidget> +#include <QTime> + +#include "cl_helper.h" + + +class Bubble; +class GLWidget : public QGLWidget { + + Q_OBJECT +public: + GLWidget(QWidget *parent = 0); + ~GLWidget(); +public slots: + void setScaling(int scale); + void setLogo(); + void setTexture(); + void showBubbles(bool); +protected: + void paintGL (); + void initializeGL (); +private: + GLuint m_uiTexture; + qreal m_fAngle; + qreal m_fScale; + bool m_showBubbles; + void paintTexturedCube(); + void paintQtLogo(); + void createGeometry(); + void createBubbles(int number); + void quad(qreal x1, qreal y1, qreal x2, qreal y2, qreal x3, qreal y3, qreal x4, qreal y4); + void extrude(qreal x1, qreal y1, qreal x2, qreal y2); + QList<qreal> vertices; + QList<qreal> normals; + q_vertexType *createdVertices; + q_vertexType *createdNormals; + int m_vertexNumber; + bool qtLogo; + QList<Bubble*> bubbles; + int frames; + QTime time; +}; +#endif diff --git a/examples/opengl/hellogl_es/hellogl_es.pro b/examples/opengl/hellogl_es/hellogl_es.pro new file mode 100644 index 0000000..7459456 --- /dev/null +++ b/examples/opengl/hellogl_es/hellogl_es.pro @@ -0,0 +1,34 @@ +###################################################################### +# Automatically generated by qmake (2.01a) Thu Oct 4 19:01:12 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +SOURCES += glwidget.cpp +SOURCES += mainwindow.cpp +SOURCES += bubble.cpp + +HEADERS += glwidget.h +HEADERS += mainwindow.h +HEADERS += bubble.h + +RESOURCES += texture.qrc +QT += opengl + +contains(QT_CONFIG,opengles1) { + QMAKE_LIBS += "libGLES_CM.lib" +} +contains(QT_CONFIG,opengles1cl) { + QMAKE_LIBS += "libGLES_CL.lib" +} + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/opengl/hellogl_es +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS hellogl_es.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/opengl/hellogl_es +INSTALLS += target sources diff --git a/examples/opengl/hellogl_es/main.cpp b/examples/opengl/hellogl_es/main.cpp new file mode 100644 index 0000000..feff054 --- /dev/null +++ b/examples/opengl/hellogl_es/main.cpp @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> +#include <QMainWindow> +#include "mainwindow.h" + +int main( int argc, char ** argv ) +{ + Q_INIT_RESOURCE(texture); + QApplication a( argc, argv ); + MainWindow mw; + mw.showMaximized(); + return a.exec(); +} diff --git a/examples/opengl/hellogl_es/mainwindow.cpp b/examples/opengl/hellogl_es/mainwindow.cpp new file mode 100644 index 0000000..26fbeaa --- /dev/null +++ b/examples/opengl/hellogl_es/mainwindow.cpp @@ -0,0 +1,108 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "mainwindow.h" + +#include <QApplication> +#include <QMenuBar> +#include <QGroupBox> +#include <QGridLayout> +#include <QSlider> +#include <QLabel> +#include <QTimer> + +#include "glwidget.h" + +MainWindow::MainWindow() +{ + GLWidget *glwidget = new GLWidget(); + QLabel *label = new QLabel(this); + QTimer *timer = new QTimer(this); + QSlider *slider = new QSlider(this); + slider->setOrientation(Qt::Horizontal); + + slider->setRange(0, 100); + slider->setSliderPosition(50); + timer->setInterval(10); + label->setText("A QGlWidget with OpenGl ES"); + label->setAlignment(Qt::AlignHCenter); + + QGroupBox * groupBox = new QGroupBox(this); + setCentralWidget(groupBox); + groupBox->setTitle("OpenGL ES Example"); + + QGridLayout *layout = new QGridLayout(groupBox); + + layout->addWidget(glwidget,1,0,8,1); + layout->addWidget(label,9,0,1,1); + layout->addWidget(slider, 11,0,1,1); + + groupBox->setLayout(layout); + + QMenu *fileMenu = new QMenu("File"); + QMenu *helpMenu = new QMenu("Help"); + QMenu *showMenu = new QMenu("Show"); + menuBar()->addMenu(fileMenu); + menuBar()->addMenu(showMenu); + menuBar()->addMenu(helpMenu); + QAction *exit = new QAction("Exit", fileMenu); + QAction *aboutQt = new QAction("AboutQt", helpMenu); + QAction *showLogo = new QAction("Show 3D Logo", showMenu); + QAction *showTexture = new QAction("Show 2D Texture", showMenu); + QAction *showBubbles = new QAction("Show bubbles", showMenu); + showBubbles->setCheckable(true); + showBubbles->setChecked(true); + fileMenu->addAction(exit); + helpMenu->addAction(aboutQt); + showMenu->addAction(showLogo); + showMenu->addAction(showTexture); + showMenu->addAction(showBubbles); + + QObject::connect(timer, SIGNAL(timeout()), glwidget, SLOT(updateGL())); + QObject::connect(exit, SIGNAL(triggered(bool)), this, SLOT(close())); + QObject::connect(aboutQt, SIGNAL(triggered(bool)), qApp, SLOT(aboutQt())); + + QObject::connect(showLogo, SIGNAL(triggered(bool)), glwidget, SLOT(setLogo())); + QObject::connect(showTexture, SIGNAL(triggered(bool)), glwidget, SLOT(setTexture())); + QObject::connect(showBubbles, SIGNAL(triggered(bool)), glwidget, SLOT(showBubbles(bool))); + QObject::connect(slider, SIGNAL(valueChanged(int)), glwidget, SLOT(setScaling(int))); + timer->start(); +} diff --git a/examples/opengl/hellogl_es/mainwindow.h b/examples/opengl/hellogl_es/mainwindow.h new file mode 100644 index 0000000..72b723c --- /dev/null +++ b/examples/opengl/hellogl_es/mainwindow.h @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QMainWindow> + +class QSlider; +class GLWidget; + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); + +private: +}; + +#endif diff --git a/examples/opengl/hellogl_es/qt.png b/examples/opengl/hellogl_es/qt.png Binary files differnew file mode 100644 index 0000000..79e383c --- /dev/null +++ b/examples/opengl/hellogl_es/qt.png diff --git a/examples/opengl/hellogl_es/texture.qrc b/examples/opengl/hellogl_es/texture.qrc new file mode 100644 index 0000000..ff1d0e5 --- /dev/null +++ b/examples/opengl/hellogl_es/texture.qrc @@ -0,0 +1,5 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> + <file>qt.png</file> +</qresource> +</RCC> diff --git a/examples/opengl/hellogl_es2/bubble.cpp b/examples/opengl/hellogl_es2/bubble.cpp new file mode 100644 index 0000000..691d314 --- /dev/null +++ b/examples/opengl/hellogl_es2/bubble.cpp @@ -0,0 +1,140 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#include "bubble.h" + +Bubble::Bubble(const QPointF &position, qreal radius, const QPointF &velocity) + : position(position), vel(velocity), radius(radius) +{ + innerColor = randomColor(); + outerColor = randomColor(); + cache = 0; + updateBrush(); +} + +//! [0] +void Bubble::updateCache() +{ + if (cache) + delete cache; + cache = new QImage(qRound(radius * 2 + 2), qRound(radius * 2 + 2), QImage::Format_ARGB32); + cache->fill(0x00000000); + QPainter p(cache); + p.setRenderHint(QPainter::HighQualityAntialiasing); + QPen pen(Qt::white); + pen.setWidth(2); + p.setPen(pen); + p.setBrush(brush); + p.drawEllipse(0, 0, int(2*radius), int(2*radius)); +} +//! [0] + +Bubble::~Bubble() +{ + if (cache) + delete cache; +} + +void Bubble::updateBrush() +{ + QRadialGradient gradient(QPointF(radius, radius), radius, + QPointF(radius*0.5, radius*0.5)); + + gradient.setColorAt(0, QColor(255, 255, 255, 255)); + gradient.setColorAt(0.25, innerColor); + gradient.setColorAt(1, outerColor); + brush = QBrush(gradient); + updateCache(); +} + +//! [1] +void Bubble::drawBubble(QPainter *painter) +{ + painter->save(); + painter->translate(position.x() - radius, position.y() - radius); + painter->setOpacity(0.8); + painter->drawImage(0, 0, *cache); + painter->restore(); +} +//! [1] + +QColor Bubble::randomColor() +{ + int red = int(185 + 70.0*qrand()/(RAND_MAX+1.0)); + int green = int(185 + 70.0*qrand()/(RAND_MAX+1.0)); + int blue = int(205 + 50.0*qrand()/(RAND_MAX+1.0)); + int alpha = int(91 + 100.0*qrand()/(RAND_MAX+1.0)); + + return QColor(red, green, blue, alpha); +} + +void Bubble::move(const QRect &bbox) +{ + position += vel; + qreal leftOverflow = position.x() - radius - bbox.left(); + qreal rightOverflow = position.x() + radius - bbox.right(); + qreal topOverflow = position.y() - radius - bbox.top(); + qreal bottomOverflow = position.y() + radius - bbox.bottom(); + + if (leftOverflow < 0.0) { + position.setX(position.x() - 2 * leftOverflow); + vel.setX(-vel.x()); + } else if (rightOverflow > 0.0) { + position.setX(position.x() - 2 * rightOverflow); + vel.setX(-vel.x()); + } + + if (topOverflow < 0.0) { + position.setY(position.y() - 2 * topOverflow); + vel.setY(-vel.y()); + } else if (bottomOverflow > 0.0) { + position.setY(position.y() - 2 * bottomOverflow); + vel.setY(-vel.y()); + } +} + +QRectF Bubble::rect() +{ + return QRectF(position.x() - radius, position.y() - radius, + 2 * radius, 2 * radius); +} diff --git a/examples/opengl/hellogl_es2/bubble.h b/examples/opengl/hellogl_es2/bubble.h new file mode 100644 index 0000000..a32754f --- /dev/null +++ b/examples/opengl/hellogl_es2/bubble.h @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef BUBBLE_H +#define BUBBLE_H + +#include <QBrush> +#include <QColor> +#include <QPointF> +#include <QRect> +#include <QRectF> + +class QPainter; + +class Bubble +{ +public: + Bubble(const QPointF &position, qreal radius, const QPointF &velocity); + ~Bubble(); + + void drawBubble(QPainter *painter); + void updateBrush(); + void move(const QRect &bbox); + void updateCache(); + QRectF rect(); + +private: + QColor randomColor(); + + QBrush brush; + QPointF position; + QPointF vel; + qreal radius; + QColor innerColor; + QColor outerColor; + QImage *cache; +}; + +#endif diff --git a/examples/opengl/hellogl_es2/glwidget.cpp b/examples/opengl/hellogl_es2/glwidget.cpp new file mode 100644 index 0000000..213c5b2 --- /dev/null +++ b/examples/opengl/hellogl_es2/glwidget.cpp @@ -0,0 +1,642 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "glwidget.h" +#include <QPainter> +#include <math.h> + +#include "bubble.h" + + +const int bubbleNum = 8; + +inline void CrossProduct(qreal &xOut, qreal &yOut, qreal &zOut, qreal x1, qreal y1, qreal z1, qreal x2, qreal y2, qreal z2) +{ + xOut = y1 * z2 - z1 * y2; + yOut = z1 * x2 - x1 * z2; + zOut = x1 * y2 - y1 * x2; +} + +inline void Normalize(qreal &x, qreal &y, qreal &z) +{ + qreal l = sqrt(x*x + y*y + z*z); + x = x / l; + y = y / l; + z = z / l; +} + +inline void IdentityMatrix(GLfloat *m) +{ + m[0 * 4 + 0] = 1.0f; + m[1 * 4 + 0] = 0.0f; + m[2 * 4 + 0] = 0.0f; + m[3 * 4 + 0] = 0.0f; + m[0 * 4 + 1] = 0.0f; + m[1 * 4 + 1] = 1.0f; + m[2 * 4 + 1] = 0.0f; + m[3 * 4 + 1] = 0.0f; + m[0 * 4 + 2] = 0.0f; + m[1 * 4 + 2] = 0.0f; + m[2 * 4 + 2] = 1.0f; + m[3 * 4 + 2] = 0.0f; + m[0 * 4 + 3] = 0.0f; + m[1 * 4 + 3] = 0.0f; + m[2 * 4 + 3] = 0.0f; + m[3 * 4 + 3] = 1.0f; +} + +// Adjust a 4x4 matrix to apply a scale. +inline void ScaleMatrix(GLfloat *m, GLfloat scalex, GLfloat scaley, GLfloat scalez) +{ + m[0 * 4 + 0] *= scalex; + m[0 * 4 + 1] *= scalex; + m[0 * 4 + 2] *= scalex; + m[0 * 4 + 3] *= scalex; + m[1 * 4 + 0] *= scaley; + m[1 * 4 + 1] *= scaley; + m[1 * 4 + 2] *= scaley; + m[1 * 4 + 3] *= scaley; + m[2 * 4 + 0] *= scalez; + m[2 * 4 + 1] *= scalez; + m[2 * 4 + 2] *= scalez; + m[2 * 4 + 3] *= scalez; +} + +// Adjust a 4x4 matrix to apply a translation. +inline void TranslateMatrix(GLfloat *m, GLfloat translatex, GLfloat translatey, GLfloat translatez) +{ + m[3 * 4 + 0] += m[0 * 4 + 0] * translatex + m[1 * 4 + 0] * translatey + m[2 * 4 + 0] * translatez; + m[3 * 4 + 1] += m[0 * 4 + 1] * translatex + m[1 * 4 + 1] * translatey + m[2 * 4 + 1] * translatez; + m[3 * 4 + 2] += m[0 * 4 + 2] * translatex + m[1 * 4 + 2] * translatey + m[2 * 4 + 2] * translatez; + m[3 * 4 + 3] += m[0 * 4 + 3] * translatex + m[1 * 4 + 3] * translatey + m[2 * 4 + 3] * translatez; +} + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +// Adjust a 4x4 matrix to apply a rotation. +inline void RotateMatrix(GLfloat *m, GLfloat angle, GLfloat vx, GLfloat vy, GLfloat vz) +{ + GLfloat len = sqrt(vx * vx + vy * vy + vz * vz); + if (len != 0) { + vx /= len; + vy /= len; + vz /= len; + } + + GLfloat c, s, ic; + c = cos(angle * M_PI / 180.0); + s = sin(angle * M_PI / 180.0); + ic = 1.0f - c; + + GLfloat rot[16]; + rot[0 * 4 + 0] = vx * vx * ic + c; + rot[1 * 4 + 0] = vx * vy * ic - vz * s; + rot[2 * 4 + 0] = vx * vz * ic + vy * s; + rot[3 * 4 + 0] = 0.0f; + rot[0 * 4 + 1] = vy * vx * ic + vz * s; + rot[1 * 4 + 1] = vy * vy * ic + c; + rot[2 * 4 + 1] = vy * vz * ic - vx * s; + rot[3 * 4 + 1] = 0.0f; + rot[0 * 4 + 2] = vx * vz * ic - vy * s; + rot[1 * 4 + 2] = vy * vz * ic + vx * s; + rot[2 * 4 + 2] = vz * vz * ic + c; + rot[3 * 4 + 2] = 0.0f; + rot[0 * 4 + 3] = 0.0f; + rot[1 * 4 + 3] = 0.0f; + rot[2 * 4 + 3] = 0.0f; + rot[3 * 4 + 3] = 1.0f; + + GLfloat temp[16]; + for (int i = 0; i < 4; ++i) { + for (int j = 0; j < 4; ++j) { + temp[j * 4 + i] = 0.0f; + for (int k = 0; k < 4; ++k) { + temp[j * 4 + i] += m[k * 4 + i] * rot[j * 4 + k]; + } + } + } + + qMemCopy(m, temp, sizeof(temp)); +} + +GLWidget::GLWidget(QWidget *parent) + : QGLWidget(parent) +{ + qtLogo = true; + createdVertices = 0; + createdNormals = 0; + m_vertexNumber = 0; + frames = 0; + setAttribute(Qt::WA_PaintOnScreen); + setAttribute(Qt::WA_NoSystemBackground); + setAutoBufferSwap(false); + m_showBubbles = true; +#ifndef Q_WS_QWS + setMinimumSize(300, 250); +#endif +} + +GLWidget::~GLWidget() +{ + if (createdVertices) + delete[] createdVertices; + if (createdNormals) + delete[] createdNormals; +} + +void GLWidget::setScaling(int scale) { + + if (scale > 50) + m_fScale = 1 + qreal(scale -50) / 50 * 0.5; + else if (scale < 50) + m_fScale = 1- (qreal(50 - scale) / 50 * 1/2); + else + m_fScale = 1; +} + +void GLWidget::setLogo() { + qtLogo = true; +} + +void GLWidget::setTexture() { + qtLogo = false; +} + +void GLWidget::showBubbles(bool bubbles) +{ + m_showBubbles = bubbles; +} + +void GLWidget::paintQtLogo() +{ + glDisable(GL_TEXTURE_2D); + glVertexAttribPointer(vertexAttr1, 3, GL_FLOAT, GL_FALSE, 0, createdVertices); + glEnableVertexAttribArray(vertexAttr1); + glVertexAttribPointer(normalAttr1, 3, GL_FLOAT, GL_FALSE, 0, createdNormals); + glEnableVertexAttribArray(normalAttr1); + glDrawArrays(GL_TRIANGLES, 0, m_vertexNumber / 3); + glDisableVertexAttribArray(normalAttr1); + glDisableVertexAttribArray(vertexAttr1); +} + +void GLWidget::paintTexturedCube() +{ + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, m_uiTexture); + GLfloat afVertices[] = { + -0.5, 0.5, 0.5, 0.5,-0.5,0.5,-0.5,-0.5,0.5, + 0.5, -0.5, 0.5, -0.5,0.5,0.5,0.5,0.5,0.5, + -0.5, -0.5, -0.5, 0.5,-0.5,-0.5,-0.5,0.5,-0.5, + 0.5, 0.5, -0.5, -0.5,0.5,-0.5,0.5,-0.5,-0.5, + + 0.5, -0.5, -0.5, 0.5,-0.5,0.5,0.5,0.5,-0.5, + 0.5, 0.5, 0.5, 0.5,0.5,-0.5,0.5,-0.5,0.5, + -0.5, 0.5, -0.5, -0.5,-0.5,0.5,-0.5,-0.5,-0.5, + -0.5, -0.5, 0.5, -0.5,0.5,-0.5,-0.5,0.5,0.5, + + 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5, -0.5, + -0.5, 0.5, 0.5, 0.5, 0.5, -0.5, 0.5, 0.5, 0.5, + -0.5, -0.5, -0.5, -0.5, -0.5, 0.5, 0.5, -0.5, -0.5, + 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, -0.5, -0.5, 0.5 + }; + glVertexAttribPointer(vertexAttr2, 3, GL_FLOAT, GL_FALSE, 0, afVertices); + glEnableVertexAttribArray(vertexAttr2); + + GLfloat afTexCoord[] = { + 0.0f,0.0f, 1.0f,1.0f, 1.0f,0.0f, + 1.0f,1.0f, 0.0f,0.0f, 0.0f,1.0f, + 1.0f,1.0f, 1.0f,0.0f, 0.0f,1.0f, + 0.0f,0.0f, 0.0f,1.0f, 1.0f,0.0f, + + 1.0f,1.0f, 1.0f,0.0f, 0.0f,1.0f, + 0.0f,0.0f, 0.0f,1.0f, 1.0f,0.0f, + 0.0f,0.0f, 1.0f,1.0f, 1.0f,0.0f, + 1.0f,1.0f, 0.0f,0.0f, 0.0f,1.0f, + + 0.0f,1.0f, 1.0f,0.0f, 1.0f,1.0f, + 1.0f,0.0f, 0.0f,1.0f, 0.0f,0.0f, + 1.0f,0.0f, 1.0f,1.0f, 0.0f,0.0f, + 0.0f,1.0f, 0.0f,0.0f, 1.0f,1.0f + }; + glVertexAttribPointer(texCoordAttr2, 2, GL_FLOAT, GL_FALSE, 0, afTexCoord); + glEnableVertexAttribArray(texCoordAttr2); + + GLfloat afNormals[] = { + + 0,0,-1, 0,0,-1, 0,0,-1, + 0,0,-1, 0,0,-1, 0,0,-1, + 0,0,1, 0,0,1, 0,0,1, + 0,0,1, 0,0,1, 0,0,1, + + -1,0,0, -1,0,0, -1,0,0, + -1,0,0, -1,0,0, -1,0,0, + 1,0,0, 1,0,0, 1,0,0, + 1,0,0, 1,0,0, 1,0,0, + + 0,-1,0, 0,-1,0, 0,-1,0, + 0,-1,0, 0,-1,0, 0,-1,0, + 0,1,0, 0,1,0, 0,1,0, + 0,1,0, 0,1,0, 0,1,0 + }; + glVertexAttribPointer(normalAttr2, 3, GL_FLOAT, GL_FALSE, 0, afNormals); + glEnableVertexAttribArray(normalAttr2); + + glUniform1i(textureUniform2, 0); // use texture unit 0 + + glDrawArrays(GL_TRIANGLES, 0, 36); + + glDisableVertexAttribArray(vertexAttr2); + glDisableVertexAttribArray(normalAttr2); + glDisableVertexAttribArray(texCoordAttr2); +} + +static void reportCompileErrors(GLuint shader, const char *src) +{ + GLint value = 0; + glGetShaderiv(shader, GL_COMPILE_STATUS, &value); + bool compiled = (value != 0); + value = 0; + glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &value); + if (!compiled && value > 1) { + char *log = new char [value]; + GLint len; + glGetShaderInfoLog(shader, value, &len, log); + qWarning("%s\n", log); + qWarning("when compiling:\n%s\n", src); + delete [] log; + } +} + +static void reportLinkErrors(GLuint program, const char *vsrc, const char *fsrc) +{ + GLint value = 0; + glGetProgramiv(program, GL_LINK_STATUS, &value); + bool linked = (value != 0); + value = 0; + glGetProgramiv(program, GL_INFO_LOG_LENGTH, &value); + if (!linked && value > 1) { + char *log = new char [value]; + GLint len; + glGetProgramInfoLog(program, value, &len, log); + qWarning("%s\n", log); + qWarning("when linking:\n%s\nwith:\n%s\n", vsrc, fsrc); + delete [] log; + } +} + +void GLWidget::initializeGL () +{ + glClearColor(0.1f, 0.1f, 0.2f, 1.0f); + + glEnable(GL_TEXTURE_2D); + glGenTextures(1, &m_uiTexture); + m_uiTexture = bindTexture(QImage(":/qt.png")); + + GLuint vshader1 = glCreateShader(GL_VERTEX_SHADER); + const char *vsrc1[1] = { + "attribute highp vec4 vertex;\n" + "attribute mediump vec3 normal;\n" + "uniform mediump mat4 matrix;\n" + "varying mediump vec4 color;\n" + "void main(void)\n" + "{\n" + " vec3 toLight = normalize(vec3(0.0, 0.3, 1.0));\n" + " float angle = max(dot(normal, toLight), 0.0);\n" + " vec3 col = vec3(0.40, 1.0, 0.0);\n" + " color = vec4(col * 0.2 + col * 0.8 * angle, 1.0);\n" + " color = clamp(color, 0.0, 1.0);\n" + " gl_Position = matrix * vertex;\n" + "}\n" + }; + glShaderSource(vshader1, 1, vsrc1, 0); + glCompileShader(vshader1); + reportCompileErrors(vshader1, vsrc1[0]); + + GLuint fshader1 = glCreateShader(GL_FRAGMENT_SHADER); + const char *fsrc1[1] = { + "varying mediump vec4 color;\n" + "void main(void)\n" + "{\n" + " gl_FragColor = color;\n" + "}\n" + }; + glShaderSource(fshader1, 1, fsrc1, 0); + glCompileShader(fshader1); + reportCompileErrors(fshader1, fsrc1[0]); + + program1 = glCreateProgram(); + glAttachShader(program1, vshader1); + glAttachShader(program1, fshader1); + glLinkProgram(program1); + reportLinkErrors(program1, vsrc1[0], fsrc1[0]); + + vertexAttr1 = glGetAttribLocation(program1, "vertex"); + normalAttr1 = glGetAttribLocation(program1, "normal"); + matrixUniform1 = glGetUniformLocation(program1, "matrix"); + + GLuint vshader2 = glCreateShader(GL_VERTEX_SHADER); + const char *vsrc2[1] = { + "attribute highp vec4 vertex;\n" + "attribute highp vec4 texCoord;\n" + "attribute mediump vec3 normal;\n" + "uniform mediump mat4 matrix;\n" + "varying highp vec4 texc;\n" + "varying mediump float angle;\n" + "void main(void)\n" + "{\n" + " vec3 toLight = normalize(vec3(0.0, 0.3, 1.0));\n" + " angle = max(dot(normal, toLight), 0.0);\n" + " gl_Position = matrix * vertex;\n" + " texc = texCoord;\n" + "}\n" + }; + glShaderSource(vshader2, 1, vsrc2, 0); + glCompileShader(vshader2); + reportCompileErrors(vshader2, vsrc2[0]); + + GLuint fshader2 = glCreateShader(GL_FRAGMENT_SHADER); + const char *fsrc2[1] = { + "varying highp vec4 texc;\n" + "uniform sampler2D tex;\n" + "varying mediump float angle;\n" + "void main(void)\n" + "{\n" + " highp vec3 color = texture2D(tex, texc.st).rgb;\n" + " color = color * 0.2 + color * 0.8 * angle;\n" + " gl_FragColor = vec4(clamp(color, 0.0, 1.0), 1.0);\n" + "}\n" + }; + glShaderSource(fshader2, 1, fsrc2, 0); + glCompileShader(fshader2); + reportCompileErrors(fshader2, fsrc2[0]); + + program2 = glCreateProgram(); + glAttachShader(program2, vshader2); + glAttachShader(program2, fshader2); + glLinkProgram(program2); + reportLinkErrors(program2, vsrc2[0], fsrc2[0]); + + vertexAttr2 = glGetAttribLocation(program2, "vertex"); + normalAttr2 = glGetAttribLocation(program2, "normal"); + texCoordAttr2 = glGetAttribLocation(program2, "texCoord"); + matrixUniform2 = glGetUniformLocation(program2, "matrix"); + textureUniform2 = glGetUniformLocation(program2, "tex"); + + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); + + m_fAngle = 0; + m_fScale = 1; + createGeometry(); + createBubbles(bubbleNum - bubbles.count()); +} + +void GLWidget::paintGL() +{ + createBubbles(bubbleNum - bubbles.count()); + + QPainter painter; + painter.begin(this); + + glClearColor(0.1f, 0.1f, 0.2f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glEnable(GL_TEXTURE_2D); + + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); + + glFrontFace(GL_CW); + glCullFace(GL_FRONT); + glEnable(GL_CULL_FACE); + glEnable(GL_DEPTH_TEST); + + GLfloat modelview[16]; + IdentityMatrix(modelview); + RotateMatrix(modelview, m_fAngle, 0.0, 1.0, 0.0); + RotateMatrix(modelview, m_fAngle, 1.0, 0.0, 0.0); + RotateMatrix(modelview, m_fAngle, 0.0, 0.0, 1.0); + ScaleMatrix(modelview, m_fScale, m_fScale, m_fScale); + TranslateMatrix(modelview, 0, -0.2, 0); + + if (qtLogo) { + glUseProgram(program1); + glUniformMatrix4fv(matrixUniform1, 1, GL_FALSE, modelview); + paintQtLogo(); + glUseProgram(0); + } else { + glUseProgram(program2); + glUniformMatrix4fv(matrixUniform2, 1, GL_FALSE, modelview); + paintTexturedCube(); + glUseProgram(0); + } + + glDisable(GL_DEPTH_TEST); + glDisable(GL_CULL_FACE); + + if (m_showBubbles) + foreach (Bubble *bubble, bubbles) { + bubble->drawBubble(&painter); + } + + QString framesPerSecond; + framesPerSecond.setNum(frames /(time.elapsed() / 1000.0), 'f', 2); + + painter.setPen(Qt::white); + + painter.drawText(20, 40, framesPerSecond + " fps"); + + painter.end(); + + swapBuffers(); + + QMutableListIterator<Bubble*> iter(bubbles); + + while (iter.hasNext()) { + Bubble *bubble = iter.next(); + bubble->move(rect()); + } + if (!(frames % 100)) { + time.start(); + frames = 0; + } + m_fAngle += 1.0f; + frames ++; +} + +void GLWidget::createBubbles(int number) +{ + for (int i = 0; i < number; ++i) { + QPointF position(width()*(0.1 + (0.8*qrand()/(RAND_MAX+1.0))), + height()*(0.1 + (0.8*qrand()/(RAND_MAX+1.0)))); + qreal radius = qMin(width(), height())*(0.0175 + 0.0875*qrand()/(RAND_MAX+1.0)); + QPointF velocity(width()*0.0175*(-0.5 + qrand()/(RAND_MAX+1.0)), + height()*0.0175*(-0.5 + qrand()/(RAND_MAX+1.0))); + + bubbles.append(new Bubble(position, radius, velocity)); + } +} + +void GLWidget::createGeometry() +{ + vertices.clear(); + normals.clear(); + + qreal x1 = +0.06f; + qreal y1 = -0.14f; + qreal x2 = +0.14f; + qreal y2 = -0.06f; + qreal x3 = +0.08f; + qreal y3 = +0.00f; + qreal x4 = +0.30f; + qreal y4 = +0.22f; + + quad(x1, y1, x2, y2, y2, x2, y1, x1); + quad(x3, y3, x4, y4, y4, x4, y3, x3); + + extrude(x1, y1, x2, y2); + extrude(x2, y2, y2, x2); + extrude(y2, x2, y1, x1); + extrude(y1, x1, x1, y1); + extrude(x3, y3, x4, y4); + extrude(x4, y4, y4, x4); + extrude(y4, x4, y3, x3); + + const qreal Pi = 3.14159f; + const int NumSectors = 100; + + for (int i = 0; i < NumSectors; ++i) { + qreal angle1 = (i * 2 * Pi) / NumSectors; + qreal x5 = 0.30 * sin(angle1); + qreal y5 = 0.30 * cos(angle1); + qreal x6 = 0.20 * sin(angle1); + qreal y6 = 0.20 * cos(angle1); + + qreal angle2 = ((i + 1) * 2 * Pi) / NumSectors; + qreal x7 = 0.20 * sin(angle2); + qreal y7 = 0.20 * cos(angle2); + qreal x8 = 0.30 * sin(angle2); + qreal y8 = 0.30 * cos(angle2); + + quad(x5, y5, x6, y6, x7, y7, x8, y8); + + extrude(x6, y6, x7, y7); + extrude(x8, y8, x5, y5); + } + + m_vertexNumber = vertices.size(); + createdVertices = new GLfloat[m_vertexNumber]; + createdNormals = new GLfloat[m_vertexNumber]; + for (int i = 0;i < m_vertexNumber;i++) { + createdVertices[i] = vertices.at(i) * 2; + createdNormals[i] = normals.at(i); + } + vertices.clear(); + normals.clear(); +} + +void GLWidget::quad(qreal x1, qreal y1, qreal x2, qreal y2, qreal x3, qreal y3, qreal x4, qreal y4) +{ + qreal nx, ny, nz; + + vertices << x1 << y1 << -0.05f; + vertices << x2 << y2 << -0.05f; + vertices << x4 << y4 << -0.05f; + + vertices << x3 << y3 << -0.05f; + vertices << x4 << y4 << -0.05f; + vertices << x2 << y2 << -0.05f; + + CrossProduct(nx, ny, nz, x2 - x1, y2 - y1, 0, x4 - x1, y4 - y1, 0); + Normalize(nx, ny, nz); + + normals << nx << ny << nz; + normals << nx << ny << nz; + normals << nx << ny << nz; + + normals << nx << ny << nz; + normals << nx << ny << nz; + normals << nx << ny << nz; + + vertices << x4 << y4 << 0.05f; + vertices << x2 << y2 << 0.05f; + vertices << x1 << y1 << 0.05f; + + vertices << x2 << y2 << 0.05f; + vertices << x4 << y4 << 0.05f; + vertices << x3 << y3 << 0.05f; + + CrossProduct(nx, ny, nz, x2 - x4, y2 - y4, 0, x1 - x4, y1 - y4, 0); + Normalize(nx, ny, nz); + + normals << nx << ny << nz; + normals << nx << ny << nz; + normals << nx << ny << nz; + + normals << nx << ny << nz; + normals << nx << ny << nz; + normals << nx << ny << nz; +} + +void GLWidget::extrude(qreal x1, qreal y1, qreal x2, qreal y2) +{ + qreal nx, ny, nz; + + vertices << x1 << y1 << +0.05f; + vertices << x2 << y2 << +0.05f; + vertices << x1 << y1 << -0.05f; + + vertices << x2 << y2 << -0.05f; + vertices << x1 << y1 << -0.05f; + vertices << x2 << y2 << +0.05f; + + CrossProduct(nx, ny, nz, x2 - x1, y2 - y1, 0.0f, 0.0f, 0.0f, -0.1f); + Normalize(nx, ny, nz); + + normals << nx << ny << nz; + normals << nx << ny << nz; + normals << nx << ny << nz; + + normals << nx << ny << nz; + normals << nx << ny << nz; + normals << nx << ny << nz; +} diff --git a/examples/opengl/hellogl_es2/glwidget.h b/examples/opengl/hellogl_es2/glwidget.h new file mode 100644 index 0000000..9a7823a --- /dev/null +++ b/examples/opengl/hellogl_es2/glwidget.h @@ -0,0 +1,94 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef GLWIDGET_H +#define GLWIDGET_H + +#include <QGLWidget> +#include <QTime> + +class Bubble; +class GLWidget : public QGLWidget { + + Q_OBJECT +public: + GLWidget(QWidget *parent = 0); + ~GLWidget(); +public slots: + void setScaling(int scale); + void setLogo(); + void setTexture(); + void showBubbles(bool); +protected: + void paintGL (); + void initializeGL (); +private: + GLuint m_uiTexture; + qreal m_fAngle; + qreal m_fScale; + bool m_showBubbles; + void paintTexturedCube(); + void paintQtLogo(); + void createGeometry(); + void createBubbles(int number); + void quad(qreal x1, qreal y1, qreal x2, qreal y2, qreal x3, qreal y3, qreal x4, qreal y4); + void extrude(qreal x1, qreal y1, qreal x2, qreal y2); + QList<qreal> vertices; + QList<qreal> normals; + GLfloat *createdVertices; + GLfloat *createdNormals; + int m_vertexNumber; + bool qtLogo; + QList<Bubble*> bubbles; + int frames; + QTime time; + GLuint program1; + GLuint program2; + GLuint vertexAttr1; + GLuint normalAttr1; + GLuint matrixUniform1; + GLuint vertexAttr2; + GLuint normalAttr2; + GLuint texCoordAttr2; + GLuint matrixUniform2; + GLuint textureUniform2; +}; +#endif diff --git a/examples/opengl/hellogl_es2/hellogl_es2.pro b/examples/opengl/hellogl_es2/hellogl_es2.pro new file mode 100644 index 0000000..92b4224 --- /dev/null +++ b/examples/opengl/hellogl_es2/hellogl_es2.pro @@ -0,0 +1,27 @@ +###################################################################### +# Automatically generated by qmake (2.01a) Thu Oct 4 19:01:12 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +SOURCES += glwidget.cpp +SOURCES += mainwindow.cpp +SOURCES += bubble.cpp + +HEADERS += glwidget.h +HEADERS += mainwindow.h +HEADERS += bubble.h + +RESOURCES += texture.qrc +QT += opengl + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/opengl/hellogl_es2 +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS hellogl_es2.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/opengl/hellogl_es2 +INSTALLS += target sources diff --git a/examples/opengl/hellogl_es2/main.cpp b/examples/opengl/hellogl_es2/main.cpp new file mode 100644 index 0000000..feff054 --- /dev/null +++ b/examples/opengl/hellogl_es2/main.cpp @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> +#include <QMainWindow> +#include "mainwindow.h" + +int main( int argc, char ** argv ) +{ + Q_INIT_RESOURCE(texture); + QApplication a( argc, argv ); + MainWindow mw; + mw.showMaximized(); + return a.exec(); +} diff --git a/examples/opengl/hellogl_es2/mainwindow.cpp b/examples/opengl/hellogl_es2/mainwindow.cpp new file mode 100644 index 0000000..26fbeaa --- /dev/null +++ b/examples/opengl/hellogl_es2/mainwindow.cpp @@ -0,0 +1,108 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "mainwindow.h" + +#include <QApplication> +#include <QMenuBar> +#include <QGroupBox> +#include <QGridLayout> +#include <QSlider> +#include <QLabel> +#include <QTimer> + +#include "glwidget.h" + +MainWindow::MainWindow() +{ + GLWidget *glwidget = new GLWidget(); + QLabel *label = new QLabel(this); + QTimer *timer = new QTimer(this); + QSlider *slider = new QSlider(this); + slider->setOrientation(Qt::Horizontal); + + slider->setRange(0, 100); + slider->setSliderPosition(50); + timer->setInterval(10); + label->setText("A QGlWidget with OpenGl ES"); + label->setAlignment(Qt::AlignHCenter); + + QGroupBox * groupBox = new QGroupBox(this); + setCentralWidget(groupBox); + groupBox->setTitle("OpenGL ES Example"); + + QGridLayout *layout = new QGridLayout(groupBox); + + layout->addWidget(glwidget,1,0,8,1); + layout->addWidget(label,9,0,1,1); + layout->addWidget(slider, 11,0,1,1); + + groupBox->setLayout(layout); + + QMenu *fileMenu = new QMenu("File"); + QMenu *helpMenu = new QMenu("Help"); + QMenu *showMenu = new QMenu("Show"); + menuBar()->addMenu(fileMenu); + menuBar()->addMenu(showMenu); + menuBar()->addMenu(helpMenu); + QAction *exit = new QAction("Exit", fileMenu); + QAction *aboutQt = new QAction("AboutQt", helpMenu); + QAction *showLogo = new QAction("Show 3D Logo", showMenu); + QAction *showTexture = new QAction("Show 2D Texture", showMenu); + QAction *showBubbles = new QAction("Show bubbles", showMenu); + showBubbles->setCheckable(true); + showBubbles->setChecked(true); + fileMenu->addAction(exit); + helpMenu->addAction(aboutQt); + showMenu->addAction(showLogo); + showMenu->addAction(showTexture); + showMenu->addAction(showBubbles); + + QObject::connect(timer, SIGNAL(timeout()), glwidget, SLOT(updateGL())); + QObject::connect(exit, SIGNAL(triggered(bool)), this, SLOT(close())); + QObject::connect(aboutQt, SIGNAL(triggered(bool)), qApp, SLOT(aboutQt())); + + QObject::connect(showLogo, SIGNAL(triggered(bool)), glwidget, SLOT(setLogo())); + QObject::connect(showTexture, SIGNAL(triggered(bool)), glwidget, SLOT(setTexture())); + QObject::connect(showBubbles, SIGNAL(triggered(bool)), glwidget, SLOT(showBubbles(bool))); + QObject::connect(slider, SIGNAL(valueChanged(int)), glwidget, SLOT(setScaling(int))); + timer->start(); +} diff --git a/examples/opengl/hellogl_es2/mainwindow.h b/examples/opengl/hellogl_es2/mainwindow.h new file mode 100644 index 0000000..72b723c --- /dev/null +++ b/examples/opengl/hellogl_es2/mainwindow.h @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QMainWindow> + +class QSlider; +class GLWidget; + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); + +private: +}; + +#endif diff --git a/examples/opengl/hellogl_es2/qt.png b/examples/opengl/hellogl_es2/qt.png Binary files differnew file mode 100644 index 0000000..79e383c --- /dev/null +++ b/examples/opengl/hellogl_es2/qt.png diff --git a/examples/opengl/hellogl_es2/texture.qrc b/examples/opengl/hellogl_es2/texture.qrc new file mode 100644 index 0000000..ff1d0e5 --- /dev/null +++ b/examples/opengl/hellogl_es2/texture.qrc @@ -0,0 +1,5 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> + <file>qt.png</file> +</qresource> +</RCC> diff --git a/examples/opengl/opengl.pro b/examples/opengl/opengl.pro new file mode 100644 index 0000000..a4c2a22 --- /dev/null +++ b/examples/opengl/opengl.pro @@ -0,0 +1,29 @@ +TEMPLATE = subdirs + +contains(QT_CONFIG, opengles1)|contains(QT_CONFIG, opengles1cl)|contains(QT_CONFIG, opengles2){ + contains(QT_CONFIG, opengles2) { + SUBDIRS = hellogl_es2 + } else { + SUBDIRS = hellogl_es + } +} else { + SUBDIRS = 2dpainting \ + grabber \ + hellogl \ + overpainting \ + pbuffers \ + framebufferobject2 \ + samplebuffers \ + textures + + contains(QT_CONFIG, svg) { + SUBDIRS += framebufferobject \ + pbuffers2 + } +} + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/opengl +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS opengl.pro README +sources.path = $$[QT_INSTALL_EXAMPLES]/opengl +INSTALLS += target sources diff --git a/examples/opengl/overpainting/bubble.cpp b/examples/opengl/overpainting/bubble.cpp new file mode 100644 index 0000000..8bbe9b0 --- /dev/null +++ b/examples/opengl/overpainting/bubble.cpp @@ -0,0 +1,113 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#include "bubble.h" + +Bubble::Bubble(const QPointF &position, qreal radius, const QPointF &velocity) + : position(position), vel(velocity), radius(radius) +{ + innerColor = randomColor(); + outerColor = randomColor(); + updateBrush(); +} + +void Bubble::updateBrush() +{ + QRadialGradient gradient(QPointF(radius, radius), radius, + QPointF(radius*0.5, radius*0.5)); + + gradient.setColorAt(0, QColor(255, 255, 255, 255)); + gradient.setColorAt(0.25, innerColor); + gradient.setColorAt(1, outerColor); + brush = QBrush(gradient); +} + +void Bubble::drawBubble(QPainter *painter) +{ + painter->save(); + painter->translate(position.x() - radius, position.y() - radius); + painter->setBrush(brush); + painter->drawEllipse(0, 0, int(2*radius), int(2*radius)); + painter->restore(); +} + +QColor Bubble::randomColor() +{ + int red = int(205 + 50.0*qrand()/(RAND_MAX+1.0)); + int green = int(205 + 50.0*qrand()/(RAND_MAX+1.0)); + int blue = int(205 + 50.0*qrand()/(RAND_MAX+1.0)); + int alpha = int(91 + 100.0*qrand()/(RAND_MAX+1.0)); + + return QColor(red, green, blue, alpha); +} + +void Bubble::move(const QRect &bbox) +{ + position += vel; + qreal leftOverflow = position.x() - radius - bbox.left(); + qreal rightOverflow = position.x() + radius - bbox.right(); + qreal topOverflow = position.y() - radius - bbox.top(); + qreal bottomOverflow = position.y() + radius - bbox.bottom(); + + if (leftOverflow < 0.0) { + position.setX(position.x() - 2 * leftOverflow); + vel.setX(-vel.x()); + } else if (rightOverflow > 0.0) { + position.setX(position.x() - 2 * rightOverflow); + vel.setX(-vel.x()); + } + + if (topOverflow < 0.0) { + position.setY(position.y() - 2 * topOverflow); + vel.setY(-vel.y()); + } else if (bottomOverflow > 0.0) { + position.setY(position.y() - 2 * bottomOverflow); + vel.setY(-vel.y()); + } +} + +QRectF Bubble::rect() +{ + return QRectF(position.x() - radius, position.y() - radius, + 2 * radius, 2 * radius); +} diff --git a/examples/opengl/overpainting/bubble.h b/examples/opengl/overpainting/bubble.h new file mode 100644 index 0000000..7e71550 --- /dev/null +++ b/examples/opengl/overpainting/bubble.h @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef BUBBLE_H +#define BUBBLE_H + +#include <QBrush> +#include <QColor> +#include <QPointF> +#include <QRect> +#include <QRectF> + +QT_BEGIN_NAMESPACE +class QPainter; +QT_END_NAMESPACE + +class Bubble +{ +public: + Bubble(const QPointF &position, qreal radius, const QPointF &velocity); + + void drawBubble(QPainter *painter); + void updateBrush(); + void move(const QRect &bbox); + QRectF rect(); + +private: + QColor randomColor(); + + QBrush brush; + QPointF position; + QPointF vel; + qreal radius; + QColor innerColor; + QColor outerColor; +}; + +#endif diff --git a/examples/opengl/overpainting/glwidget.cpp b/examples/opengl/overpainting/glwidget.cpp new file mode 100644 index 0000000..e3a7b1e --- /dev/null +++ b/examples/opengl/overpainting/glwidget.cpp @@ -0,0 +1,360 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> +#include <QtOpenGL> +#include <stdlib.h> + +#include <math.h> + +#include "bubble.h" +#include "glwidget.h" + +#ifndef GL_MULTISAMPLE +#define GL_MULTISAMPLE 0x809D +#endif + +//! [0] +GLWidget::GLWidget(QWidget *parent) + : QGLWidget(QGLFormat(QGL::SampleBuffers), parent) +{ + QTime midnight(0, 0, 0); + qsrand(midnight.secsTo(QTime::currentTime())); + + object = 0; + xRot = 0; + yRot = 0; + zRot = 0; + + trolltechGreen = QColor::fromCmykF(0.40, 0.0, 1.0, 0.0); + trolltechPurple = QColor::fromCmykF(0.39, 0.39, 0.0, 0.0); + + animationTimer.setSingleShot(false); + connect(&animationTimer, SIGNAL(timeout()), this, SLOT(animate())); + animationTimer.start(25); + + setAutoFillBackground(false); + setMinimumSize(200, 200); + setWindowTitle(tr("Overpainting a Scene")); +} +//! [0] + +//! [1] +GLWidget::~GLWidget() +{ + makeCurrent(); + glDeleteLists(object, 1); +} +//! [1] + +void GLWidget::setXRotation(int angle) +{ + normalizeAngle(&angle); + if (angle != xRot) + xRot = angle; +} + +void GLWidget::setYRotation(int angle) +{ + normalizeAngle(&angle); + if (angle != yRot) + yRot = angle; +} + +void GLWidget::setZRotation(int angle) +{ + normalizeAngle(&angle); + if (angle != zRot) + zRot = angle; +} + +//! [2] +void GLWidget::initializeGL() +{ + object = makeObject(); +} +//! [2] + +void GLWidget::mousePressEvent(QMouseEvent *event) +{ + lastPos = event->pos(); +} + +void GLWidget::mouseMoveEvent(QMouseEvent *event) +{ + int dx = event->x() - lastPos.x(); + int dy = event->y() - lastPos.y(); + + if (event->buttons() & Qt::LeftButton) { + setXRotation(xRot + 8 * dy); + setYRotation(yRot + 8 * dx); + } else if (event->buttons() & Qt::RightButton) { + setXRotation(xRot + 8 * dy); + setZRotation(zRot + 8 * dx); + } + lastPos = event->pos(); +} + +void GLWidget::paintEvent(QPaintEvent *event) +{ + makeCurrent(); +//! [4] + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); +//! [4] + +//! [6] + qglClearColor(trolltechPurple.dark()); + glShadeModel(GL_SMOOTH); + glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); + glEnable(GL_LIGHTING); + glEnable(GL_LIGHT0); + glEnable(GL_MULTISAMPLE); + static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 1.0 }; + glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); + + setupViewport(width(), height()); +//! [6] + +//! [7] + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glLoadIdentity(); + glTranslated(0.0, 0.0, -10.0); + glRotated(xRot / 16.0, 1.0, 0.0, 0.0); + glRotated(yRot / 16.0, 0.0, 1.0, 0.0); + glRotated(zRot / 16.0, 0.0, 0.0, 1.0); + glCallList(object); +//! [7] + +//! [8] + glMatrixMode(GL_MODELVIEW); + glPopMatrix(); +//! [8] + +//! [10] + QPainter painter(this); + painter.setRenderHint(QPainter::Antialiasing); + foreach (Bubble *bubble, bubbles) { + if (bubble->rect().intersects(event->rect())) + bubble->drawBubble(&painter); + } + drawInstructions(&painter); + painter.end(); +} +//! [10] + +//! [11] +void GLWidget::resizeGL(int width, int height) +{ + setupViewport(width, height); +} +//! [11] + +//! [12] +void GLWidget::showEvent(QShowEvent *event) +{ + Q_UNUSED(event); + createBubbles(20 - bubbles.count()); +} +//! [12] + +QSize GLWidget::sizeHint() const +{ + return QSize(400, 400); +} + +GLuint GLWidget::makeObject() +{ + GLuint list = glGenLists(1); + glNewList(list, GL_COMPILE); + + glEnable(GL_NORMALIZE); + glBegin(GL_QUADS); + + static GLfloat logoDiffuseColor[4] = {trolltechGreen.red()/255.0, + trolltechGreen.green()/255.0, + trolltechGreen.blue()/255.0, 1.0}; + glMaterialfv(GL_FRONT, GL_DIFFUSE, logoDiffuseColor); + + GLdouble x1 = +0.06; + GLdouble y1 = -0.14; + GLdouble x2 = +0.14; + GLdouble y2 = -0.06; + GLdouble x3 = +0.08; + GLdouble y3 = +0.00; + GLdouble x4 = +0.30; + GLdouble y4 = +0.22; + + quad(x1, y1, x2, y2, y2, x2, y1, x1); + quad(x3, y3, x4, y4, y4, x4, y3, x3); + + extrude(x1, y1, x2, y2); + extrude(x2, y2, y2, x2); + extrude(y2, x2, y1, x1); + extrude(y1, x1, x1, y1); + extrude(x3, y3, x4, y4); + extrude(x4, y4, y4, x4); + extrude(y4, x4, y3, x3); + + const double Pi = 3.14159265358979323846; + const int NumSectors = 200; + + for (int i = 0; i < NumSectors; ++i) { + double angle1 = (i * 2 * Pi) / NumSectors; + GLdouble x5 = 0.30 * sin(angle1); + GLdouble y5 = 0.30 * cos(angle1); + GLdouble x6 = 0.20 * sin(angle1); + GLdouble y6 = 0.20 * cos(angle1); + + double angle2 = ((i + 1) * 2 * Pi) / NumSectors; + GLdouble x7 = 0.20 * sin(angle2); + GLdouble y7 = 0.20 * cos(angle2); + GLdouble x8 = 0.30 * sin(angle2); + GLdouble y8 = 0.30 * cos(angle2); + + quad(x5, y5, x6, y6, x7, y7, x8, y8); + + extrude(x6, y6, x7, y7); + extrude(x8, y8, x5, y5); + } + + glEnd(); + + glEndList(); + return list; +} + +void GLWidget::quad(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2, + GLdouble x3, GLdouble y3, GLdouble x4, GLdouble y4) +{ + glNormal3d(0.0, 0.0, -1.0); + glVertex3d(x1, y1, -0.05); + glVertex3d(x2, y2, -0.05); + glVertex3d(x3, y3, -0.05); + glVertex3d(x4, y4, -0.05); + + glNormal3d(0.0, 0.0, 1.0); + glVertex3d(x4, y4, +0.05); + glVertex3d(x3, y3, +0.05); + glVertex3d(x2, y2, +0.05); + glVertex3d(x1, y1, +0.05); +} + +void GLWidget::extrude(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2) +{ + qglColor(trolltechGreen.dark(250 + int(100 * x1))); + + glNormal3d((x1 + x2)/2.0, (y1 + y2)/2.0, 0.0); + glVertex3d(x1, y1, +0.05); + glVertex3d(x2, y2, +0.05); + glVertex3d(x2, y2, -0.05); + glVertex3d(x1, y1, -0.05); +} + +void GLWidget::normalizeAngle(int *angle) +{ + while (*angle < 0) + *angle += 360 * 16; + while (*angle > 360 * 16) + *angle -= 360 * 16; +} + +void GLWidget::createBubbles(int number) +{ + for (int i = 0; i < number; ++i) { + QPointF position(width()*(0.1 + (0.8*qrand()/(RAND_MAX+1.0))), + height()*(0.1 + (0.8*qrand()/(RAND_MAX+1.0)))); + qreal radius = qMin(width(), height())*(0.0125 + 0.0875*qrand()/(RAND_MAX+1.0)); + QPointF velocity(width()*0.0125*(-0.5 + qrand()/(RAND_MAX+1.0)), + height()*0.0125*(-0.5 + qrand()/(RAND_MAX+1.0))); + + bubbles.append(new Bubble(position, radius, velocity)); + } +} + +//! [13] +void GLWidget::animate() +{ + QMutableListIterator<Bubble*> iter(bubbles); + + while (iter.hasNext()) { + Bubble *bubble = iter.next(); + bubble->move(rect()); + } + update(); +} +//! [13] + +//! [14] +void GLWidget::setupViewport(int width, int height) +{ + int side = qMin(width, height); + glViewport((width - side) / 2, (height - side) / 2, side, side); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0); + glMatrixMode(GL_MODELVIEW); +} +//! [14] + +//! [15] +void GLWidget::drawInstructions(QPainter *painter) +{ + QString text = tr("Click and drag with the left mouse button " + "to rotate the Qt logo."); + QFontMetrics metrics = QFontMetrics(font()); + int border = qMax(4, metrics.leading()); + + QRect rect = metrics.boundingRect(0, 0, width() - 2*border, int(height()*0.125), + Qt::AlignCenter | Qt::TextWordWrap, text); + painter->setRenderHint(QPainter::TextAntialiasing); + painter->fillRect(QRect(0, 0, width(), rect.height() + 2*border), + QColor(0, 0, 0, 127)); + painter->setPen(Qt::white); + painter->fillRect(QRect(0, 0, width(), rect.height() + 2*border), + QColor(0, 0, 0, 127)); + painter->drawText((width() - rect.width())/2, border, + rect.width(), rect.height(), + Qt::AlignCenter | Qt::TextWordWrap, text); +} +//! [15] diff --git a/examples/opengl/overpainting/glwidget.h b/examples/opengl/overpainting/glwidget.h new file mode 100644 index 0000000..f1b49c1 --- /dev/null +++ b/examples/opengl/overpainting/glwidget.h @@ -0,0 +1,114 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef GLWIDGET_H +#define GLWIDGET_H + +#include <QBrush> +#include <QFont> +#include <QImage> +#include <QPen> +#include <QGLWidget> +#include <QTimer> + +class Bubble; +QT_BEGIN_NAMESPACE +class QPaintEvent; +class QWidget; +QT_END_NAMESPACE + +//! [0] +class GLWidget : public QGLWidget +{ + Q_OBJECT + +public: + GLWidget(QWidget *parent = 0); + ~GLWidget(); +//! [0] + + QSize sizeHint() const; + int xRotation() const { return xRot; } + int yRotation() const { return yRot; } + int zRotation() const { return zRot; } + +public slots: + void setXRotation(int angle); + void setYRotation(int angle); + void setZRotation(int angle); + +//! [1] +protected: + void initializeGL(); + void paintEvent(QPaintEvent *event); + void resizeGL(int width, int height); + void mousePressEvent(QMouseEvent *event); + void mouseMoveEvent(QMouseEvent *event); + void showEvent(QShowEvent *event); + +private slots: + void animate(); + +private: + GLuint makeObject(); + void createBubbles(int number); + void drawInstructions(QPainter *painter); +//! [1] + void extrude(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2); + void normalizeAngle(int *angle); + void quad(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2, + GLdouble x3, GLdouble y3, GLdouble x4, GLdouble y4); + void setupViewport(int width, int height); + + GLuint object; + int xRot; + int yRot; + int zRot; + QPoint lastPos; + QColor trolltechGreen; + QColor trolltechPurple; +//! [4] + QList<Bubble*> bubbles; + QTimer animationTimer; +}; +//! [4] + +#endif diff --git a/examples/opengl/overpainting/main.cpp b/examples/opengl/overpainting/main.cpp new file mode 100644 index 0000000..c1b17df --- /dev/null +++ b/examples/opengl/overpainting/main.cpp @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> +#include "glwidget.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + GLWidget window; + window.show(); + return app.exec(); +} diff --git a/examples/opengl/overpainting/overpainting.pro b/examples/opengl/overpainting/overpainting.pro new file mode 100644 index 0000000..b8e79eb --- /dev/null +++ b/examples/opengl/overpainting/overpainting.pro @@ -0,0 +1,13 @@ +QT += opengl + +HEADERS = bubble.h \ + glwidget.h +SOURCES = bubble.cpp \ + glwidget.cpp \ + main.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/opengl/overpainting +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS overpainting.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/opengl/overpainting +INSTALLS += target sources diff --git a/examples/opengl/pbuffers/cubelogo.png b/examples/opengl/pbuffers/cubelogo.png Binary files differnew file mode 100644 index 0000000..3ae0f9b --- /dev/null +++ b/examples/opengl/pbuffers/cubelogo.png diff --git a/examples/opengl/pbuffers/glwidget.cpp b/examples/opengl/pbuffers/glwidget.cpp new file mode 100644 index 0000000..4dfbcf4 --- /dev/null +++ b/examples/opengl/pbuffers/glwidget.cpp @@ -0,0 +1,260 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "glwidget.h" +#include <QtGui/QImage> + +#include <math.h> + +static GLint cubeArray[][3] = { + {0, 0, 0}, {0, 1, 0}, {1, 1, 0}, {1, 0, 0}, + {0, 0, 1}, {1, 0, 1}, {1, 1, 1}, {0, 1, 1}, + {0, 0, 0}, {1, 0, 0}, {1, 0, 1}, {0, 0, 1}, + {0, 1, 0}, {0, 1, 1}, {1, 1, 1}, {1, 1, 0}, + {0, 1, 0}, {0, 0, 0}, {0, 0, 1}, {0, 1, 1}, + {1, 0, 0}, {1, 1, 0}, {1, 1, 1}, {1, 0, 1} +}; + +static GLint cubeTextureArray[][2] = { + {0, 0}, {1, 0}, {1, 1}, {0, 1}, + {0, 0}, {0, 1}, {1, 1}, {1, 0}, + {0, 0}, {1, 0}, {1, 1}, {0, 1}, + {1, 0}, {0, 0}, {0, 1}, {1, 1}, + {0, 0}, {1, 0}, {1, 1}, {0, 1}, + {1, 0}, {0, 0}, {0, 1}, {1, 1} +}; + +static GLint faceArray[][2] = { + {1, -1}, {1, 1}, {-1, 1}, {-1, -1} +}; + +static GLubyte colorArray[][4] = { + {102, 176, 54, 255}, + {81, 141, 41, 255}, + {62, 108, 32, 255}, + {45, 79, 23, 255} +}; + +GLWidget::GLWidget(QWidget *parent) + : QGLWidget(QGLFormat(QGL::SampleBuffers), parent) +{ + // create the pbuffer + pbuffer = new QGLPixelBuffer(QSize(512, 512), format(), this); + timerId = startTimer(20); + setWindowTitle(tr("OpenGL pbuffers")); +} + +GLWidget::~GLWidget() +{ + pbuffer->releaseFromDynamicTexture(); + glDeleteTextures(1, &dynamicTexture); + glDeleteLists(pbufferList, 1); + delete pbuffer; +} + +void GLWidget::initializeGL() +{ + glMatrixMode(GL_MODELVIEW); + + glEnable(GL_CULL_FACE); + initCommon(); + initPbuffer(); + + for (int i = 0; i < 3; ++i) { + yOffs[i] = 0.0f; + xInc[i] = 0.005f; + rot[i] = 0.0f; + } + xOffs[0]= 0.0f; + xOffs[1]= 0.5f; + xOffs[2]= 1.0f; + + cubeTexture = bindTexture(QImage(":res/cubelogo.png")); +} + +void GLWidget::resizeGL(int w, int h) +{ + glViewport(0, 0, w, h); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + float aspect = w/(float)(h ? h : 1); + glFrustum(-aspect, aspect, -1, 1, 10, 100); + glTranslatef(-0.5f, -0.5f, -0.5f); + glTranslatef(0.0f, 0.0f, -15.0f); +} + +void GLWidget::paintGL() +{ + // draw a spinning cube into the pbuffer.. + pbuffer->makeCurrent(); + glBindTexture(GL_TEXTURE_2D, cubeTexture); + glCallList(pbufferList); + glFlush(); + + // rendering directly to a texture is not supported on X11 and + // some Windows implementations, unfortunately + if (!hasDynamicTextureUpdate) + pbuffer->updateDynamicTexture(dynamicTexture); + + // ..and use the pbuffer contents as a texture when rendering the + // background and the bouncing cubes + makeCurrent(); + glBindTexture(GL_TEXTURE_2D, dynamicTexture); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + // draw the background + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glLoadIdentity(); + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); + + glVertexPointer(2, GL_INT, 0, faceArray); + glTranslatef(-1.2f, -0.8f, 0.0f); + glScalef(0.2f, 0.2f, 0.2f); + for (int y = 0; y < 5; ++y) { + for (int x = 0; x < 5; ++x) { + glTranslatef(2.0f, 0, 0); + glColor4f(0.8, 0.8, 0.8, 1.0); + glDrawArrays(GL_QUADS, 0, 4); + } + glTranslatef(-10.0f, 2.0f, 0); + } + glVertexPointer(3, GL_INT, 0, cubeArray); + + glPopMatrix(); + glMatrixMode(GL_MODELVIEW); + glPopMatrix(); + + // draw the bouncing cubes + drawCube(0, 0.0f, 1.5f, 2.5f, 1.5f); + drawCube(1, 1.0f, 2.0f, 2.5f, 2.0f); + drawCube(2, 2.0f, 3.5f, 2.5f, 2.5f); +} + +void GLWidget::drawCube(int i, GLfloat z, GLfloat rotation, GLfloat jmp, GLfloat amp) +{ + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(xOffs[i], yOffs[i], z); + glTranslatef(0.5f, 0.5f, 0.5f); + GLfloat scale = 0.75 + i*(0.25f/2); + glScalef(scale, scale, scale); + glRotatef(rot[i], 1.0f, 1.0f, 1.0f); + glTranslatef(-0.5f, -0.5f, -0.5f); + + glColor4f(1.0f, 1.0f, 1.0f, 0.8f); + glDrawArrays(GL_QUADS, 0, 24); + + if (xOffs[i] > 1.0f || xOffs[i] < -1.0f) { + xInc[i] = -xInc[i]; + xOffs[i] = xOffs[i] > 1.0f ? 1.0f : -1.0f; + } + xOffs[i] += xInc[i]; + yOffs[i] = qAbs(cos((-3.141592f * jmp) * xOffs[i]) * amp) - 1; + rot[i] += rotation; +} + +void GLWidget::initCommon() +{ + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glVertexPointer(3, GL_INT, 0, cubeArray); + glTexCoordPointer(2, GL_INT, 0, cubeTextureArray); + glColorPointer(4, GL_UNSIGNED_BYTE, 0, colorArray); + + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glEnable(GL_BLEND); + glEnable(GL_TEXTURE_2D); + glEnable(GL_DEPTH_TEST); + + glClearColor(1.0f, 1.0f, 1.0f, 1.0f); +} + +void GLWidget::initPbuffer() +{ + // set up the pbuffer context + pbuffer->makeCurrent(); + initCommon(); + + glViewport(0, 0, pbuffer->size().width(), pbuffer->size().height()); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(-1, 1, -1, 1, -99, 99); + glTranslatef(-0.5f, -0.5f, 0.0f); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + pbufferList = glGenLists(1); + glNewList(pbufferList, GL_COMPILE); + { + glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); + + // draw cube background + glPushMatrix(); + glLoadIdentity(); + glTranslatef(0.5f, 0.5f, -2.0f); + glDisable(GL_TEXTURE_2D); + glEnableClientState(GL_COLOR_ARRAY); + glVertexPointer(2, GL_INT, 0, faceArray); + glDrawArrays(GL_QUADS, 0, 4); + glVertexPointer(3, GL_INT, 0, cubeArray); + glDisableClientState(GL_COLOR_ARRAY); + glEnable(GL_TEXTURE_2D); + glPopMatrix(); + + // draw cube + glTranslatef(0.5f, 0.5f, 0.5f); + glRotatef(3.0f, 1.0f, 1.0f, 1.0f); + glTranslatef(-0.5f, -0.5f, -0.5f); + glColor4f(0.9f, 0.9f, 0.9f, 1.0f); + glDrawArrays(GL_QUADS, 0, 24); + } + glEndList(); + // generate a texture that has the same size/format as the pbuffer + dynamicTexture = pbuffer->generateDynamicTexture(); + + // bind the dynamic texture to the pbuffer - this is a no-op under X11 + hasDynamicTextureUpdate = pbuffer->bindToDynamicTexture(dynamicTexture); + makeCurrent(); +} + diff --git a/examples/opengl/pbuffers/glwidget.h b/examples/opengl/pbuffers/glwidget.h new file mode 100644 index 0000000..9f2e86c --- /dev/null +++ b/examples/opengl/pbuffers/glwidget.h @@ -0,0 +1,70 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtOpenGL> + +class GLWidget : public QGLWidget +{ +public: + GLWidget(QWidget *parent); + ~GLWidget(); + void initializeGL(); + void resizeGL(int w, int h); + void paintGL(); + void timerEvent(QTimerEvent *) { update(); } + void mousePressEvent(QMouseEvent *) { killTimer(timerId); } + void mouseReleaseEvent(QMouseEvent *) { timerId = startTimer(20); } + + void drawCube(int i, GLfloat z, GLfloat ri, GLfloat jmp, GLfloat amp); + void initCommon(); + void initPbuffer(); + +private: + GLfloat rot[3], xOffs[3], yOffs[3], xInc[3]; + GLuint pbufferList; + GLuint dynamicTexture; + GLuint cubeTexture; + int timerId; + bool hasDynamicTextureUpdate; + + QGLPixelBuffer *pbuffer; +}; + diff --git a/examples/opengl/pbuffers/main.cpp b/examples/opengl/pbuffers/main.cpp new file mode 100644 index 0000000..91799e0 --- /dev/null +++ b/examples/opengl/pbuffers/main.cpp @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui/QApplication> +#include <QtGui/QMessageBox> +#include "glwidget.h" + +int main(int argc, char **argv) +{ + Q_INIT_RESOURCE(pbuffers); + + QApplication a(argc, argv); + if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) { + QMessageBox::information(0, "OpenGL pbuffers", + "This system does not support OpenGL/pbuffers."); + return -1; + } + + GLWidget widget(0); + widget.resize(640, 480); + widget.show(); + return a.exec(); +} + diff --git a/examples/opengl/pbuffers/pbuffers.pro b/examples/opengl/pbuffers/pbuffers.pro new file mode 100644 index 0000000..c1060e2 --- /dev/null +++ b/examples/opengl/pbuffers/pbuffers.pro @@ -0,0 +1,11 @@ +HEADERS += glwidget.h +SOURCES += glwidget.cpp main.cpp +RESOURCES += pbuffers.qrc + +QT += opengl + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/opengl/pbuffers +sources.files = $$SOURCES $$HEADERS $$RESOURCES pbuffers.pro *.png +sources.path = $$[QT_INSTALL_EXAMPLES]/opengl/pbuffers +INSTALLS += target sources diff --git a/examples/opengl/pbuffers/pbuffers.qrc b/examples/opengl/pbuffers/pbuffers.qrc new file mode 100644 index 0000000..9fc1a56 --- /dev/null +++ b/examples/opengl/pbuffers/pbuffers.qrc @@ -0,0 +1,5 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource prefix="/res"> + <file>cubelogo.png</file> +</qresource> +</RCC> diff --git a/examples/opengl/pbuffers2/bubbles.svg b/examples/opengl/pbuffers2/bubbles.svg new file mode 100644 index 0000000..65867da --- /dev/null +++ b/examples/opengl/pbuffers2/bubbles.svg @@ -0,0 +1,215 @@ +<?xml version="1.0" standalone="no"?> +<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> +<svg width="20cm" height="15cm" viewBox="0 0 800 600" + xmlns="http://www.w3.org/2000/svg" + xmlns:xlink="http://www.w3.org/1999/xlink/" + baseProfile="tiny" version="1.2"> + <title>Spheres</title> + <desc>Semi-transparent bubbles on a colored background.</desc> + <defs> + <!-- Create radial gradients for each bubble. --> + <radialGradient id="blueBubble" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="-50"> + <stop offset="0%" stop-color="white" stop-opacity="1" /> + <stop offset="25%" stop-color="#cdcdff" stop-opacity=".65" /> + <stop offset="100%" stop-color="#cdaacd" stop-opacity=".75" /> + </radialGradient> + <radialGradient id="redBubble" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="-50"> + <stop offset="0%" stop-color="white" stop-opacity="1" /> + <stop offset="25%" stop-color="#ffcdcd" stop-opacity=".65" /> + <stop offset="100%" stop-color="#bbbb99" stop-opacity=".75" /> + </radialGradient> + <radialGradient id="greenBubble" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="-50"> + <stop offset="0%" stop-color="white" stop-opacity="1" /> + <stop offset="25%" stop-color="#cdffcd" stop-opacity=".65" /> + <stop offset="100%" stop-color="#99aaaa" stop-opacity=".75" /> + </radialGradient> + <radialGradient id="yellowBubble" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="-50"> + <stop offset="0%" stop-color="white" stop-opacity="1" /> + <stop offset="25%" stop-color="#ffffcd" stop-opacity=".65" /> + <stop offset="100%" stop-color="#bbbbaa" stop-opacity=".75" /> + </radialGradient> + <radialGradient id="background" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="400" fx="250" fy="250"> + <stop offset="0%" stop-color="#ffffee" /> + <stop offset="100%" stop-color="#ccccaa" /> + </radialGradient> + <linearGradient id="surface" gradientUnits="userSpaceOnUse" + x1="-100" y1="200" x2="400" y2="200"> + <stop offset="0%" stop-color="#ffffcc" /> + <stop offset="100%" stop-color="#bbbb88" /> + </linearGradient> + + <!-- Create radial gradients for each circle to make them look like + spheres. --> + <radialGradient id="blueSphere" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="-50"> + <stop offset="0%" stop-color="white" /> + <stop offset="75%" stop-color="blue" /> + <stop offset="100%" stop-color="#222244" /> + </radialGradient> + <radialGradient id="redSphere" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="-50"> + <stop offset="0%" stop-color="white" /> + <stop offset="75%" stop-color="red" /> + <stop offset="100%" stop-color="#442222" /> + </radialGradient> + <radialGradient id="greenSphere" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="-50"> + <stop offset="0%" stop-color="white" /> + <stop offset="75%" stop-color="green" /> + <stop offset="100%" stop-color="#113311" /> + </radialGradient> + <radialGradient id="yellowSphere" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="-50"> + <stop offset="0%" stop-color="white" /> + <stop offset="75%" stop-color="yellow" /> + <stop offset="100%" stop-color="#444422" /> + </radialGradient> + <radialGradient id="shadowGrad" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="50"> + <stop offset="0%" stop-color="black" stop-opacity="1.0" /> + <stop offset="100%" stop-color="white" stop-opacity="0.0" /> + </radialGradient> + + <!-- Define a shadow for each sphere. --> + <circle id="shadow" fill="url(#shadowGrad)" cx="0" cy="0" r="100" /> + <g id="bubble"> + <circle fill="black" cx="0" cy="0" r="50" /> + <circle fill="#a6ce39" cx="0" cy="0" r="33" /> + <path fill="black" d="M 37,50 L 50,37 L 12,-1 L 22,-11 L 10,-24 L -24,10 + L -11,22 L -1,12 Z" /> + <circle cx="0" cy="0" r="100" /> + </g> + </defs> + <g> + <rect fill="url(#background)" x="0" y="0" width="800" height="600" /> + </g> + + <g transform="translate(200,700)"> + <use xlink:href="#bubble" fill="url(#blueBubble)" /> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="1s" dur="10s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(315,700)"> + <g transform="scale(0.5,0.5)"> + <use xlink:href="#bubble" fill="url(#redBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="3s" dur="7s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(80,700)"> + <g transform="scale(0.65,0.65)"> + <use xlink:href="#bubble" fill="url(#greenBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="5s" dur="9s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(255,700)"> + <g transform="scale(0.3,0.3)"> + <use xlink:href="#bubble" fill="url(#yellowBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="2s" dur="6s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(565,700)"> + <g transform="scale(0.4,0.4)"> + <use xlink:href="#bubble" fill="url(#blueBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="4s" dur="8s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(715,700)"> + <g transform="scale(0.6,0.6)"> + <use xlink:href="#bubble" fill="url(#redBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="1s" dur="4s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(645,700)"> + <g transform="scale(0.375,0.375)"> + <use xlink:href="#bubble" fill="url(#greenBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="0s" dur="11s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(555,700)"> + <g transform="scale(0.9,0.9)"> + <use xlink:href="#bubble" fill="url(#yellowBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="3s" dur="7.5s" fill="freeze" repeatCount="indefinite" /> + </g> + + <g transform="translate(360,700)"> + <g transform="scale(0.5,0.5)"> + <use xlink:href="#bubble" fill="url(#blueBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="3s" dur="6s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(215,700)"> + <g transform="scale(0.45,0.45)"> + <use xlink:href="#bubble" fill="url(#redBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="5.5s" dur="7s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(420,700)"> + <g transform="scale(0.75,0.75)"> + <use xlink:href="#bubble" fill="url(#greenBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="1s" dur="9s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(815,700)"> + <g transform="scale(0.6,0.6)"> + <use xlink:href="#bubble" fill="url(#yellowBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="2s" dur="9.5s" fill="freeze" repeatCount="indefinite" /> + </g> + + <g transform="translate(225,375)" > + <g transform="scale(1.0,0.5)" > + <path d="M 0 0 L 350 0 L 450 450 L -100 450 z" + fill="url(#surface)" stroke="none" /> + </g> + </g> + <g transform="translate(200,0)" > + <g transform="translate(200,490) scale(2.0,1.0) rotate(45)" > + <rect fill="#a6ce39" x="-69" y="-69" width="138" height="138" /> + <circle fill="black" cx="0" cy="0" r="50" /> + <circle fill="#a6ce39" cx="0" cy="0" r="33" /> + <path fill="black" d="M 37,50 L 50,37 L 12,-1 L 22,-11 L 10,-24 L -24,10 + L -11,22 L -1,12 Z" /> + <animateTransform attributeName="transform" type="rotate" values="0; 360" + begin="0s" dur="10s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(200,375)"> + <use xlink:href="#shadow" transform="translate(25,55) scale(1.0,0.5)" /> + <circle fill="url(#blueSphere)" cx="0" cy="0" r="100" /> + </g> + <g transform="translate(315,440)"> + <g transform="scale(0.5,0.5)"> + <use xlink:href="#shadow" transform="translate(25,55) scale(1.0,0.5)" /> + <circle fill="url(#redSphere)" cx="0" cy="0" r="100" /> + </g> + </g> + <g transform="translate(80,475)"> + <g transform="scale(0.65,0.65)"> + <use xlink:href="#shadow" transform="translate(25,55) scale(1.0,0.5)" /> + <circle fill="url(#greenSphere)" cx="0" cy="0" r="100" /> + </g> + </g> + <g transform="translate(255,525)"> + <g transform="scale(0.3,0.3)"> + <use xlink:href="#shadow" transform="translate(25,55) scale(1.0,0.5)" /> + <circle fill="url(#yellowSphere)" cx="0" cy="0" r="100" /> + </g> + </g> + </g> +</svg> diff --git a/examples/opengl/pbuffers2/designer.png b/examples/opengl/pbuffers2/designer.png Binary files differnew file mode 100644 index 0000000..4f32d57 --- /dev/null +++ b/examples/opengl/pbuffers2/designer.png diff --git a/examples/opengl/pbuffers2/glwidget.cpp b/examples/opengl/pbuffers2/glwidget.cpp new file mode 100644 index 0000000..fa37946 --- /dev/null +++ b/examples/opengl/pbuffers2/glwidget.cpp @@ -0,0 +1,326 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui/QImage> +#include "glwidget.h" + +#include <math.h> + +#ifndef GL_MULTISAMPLE +#define GL_MULTISAMPLE 0x809D +#endif + +GLWidget::GLWidget(QWidget *parent) + : QGLWidget(QGLFormat(QGL::SampleBuffers), parent) +{ + setWindowTitle(tr("OpenGL pbuffers 2")); + + pbuffer = new QGLPixelBuffer(1024, 1024, format(), this); + + rot_x = rot_y = rot_z = 0.0f; + scale = 0.1f; + anim = new QTimeLine(750, this); + anim->setUpdateInterval(20); + connect(anim, SIGNAL(valueChanged(qreal)), SLOT(animate(qreal))); + connect(anim, SIGNAL(finished()), SLOT(animFinished())); + + svg_renderer = new QSvgRenderer(QLatin1String(":/res/bubbles.svg"), this); + connect(svg_renderer, SIGNAL(repaintNeeded()), this, SLOT(draw())); + + logo = QImage(":/res/designer.png"); + logo = logo.convertToFormat(QImage::Format_ARGB32); + + makeCurrent(); // need a current context to create the display list + tile_list = glGenLists(1); + glNewList(tile_list, GL_COMPILE); + glBegin(GL_QUADS); + { + glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); + glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); + glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); + glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); + + glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); + glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); + glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); + glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); + + glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); + glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); + glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); + glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); + + glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); + glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); + glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); + glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); + + glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); + glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); + glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); + glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); + + glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); + glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); + glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); + glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); +} + glEnd(); + glEndList(); + + wave = new GLfloat[logo.width()*logo.height()]; + memset(wave, 0, logo.width()*logo.height()); + startTimer(30); // wave timer + + pbuffer->makeCurrent(); + dynamicTexture = pbuffer->generateDynamicTexture(); + + // bind the dynamic texture to the pbuffer - this is a no-op under X11 + hasDynamicTextureUpdate = pbuffer->bindToDynamicTexture(dynamicTexture); +} + +GLWidget::~GLWidget() +{ + delete[] wave; + glDeleteLists(tile_list, 1); + pbuffer->releaseFromDynamicTexture(); + glDeleteTextures(1, &dynamicTexture); + delete pbuffer; +} + +void GLWidget::paintEvent(QPaintEvent *) +{ + draw(); +} + +void GLWidget::draw() +{ + QPainter p(this); // used for text overlay + + // save the GL state set for QPainter + saveGLState(); + + // render the 'bubbles.svg' file into our pbuffer + QPainter pbuffer_painter(pbuffer); + svg_renderer->render(&pbuffer_painter); + pbuffer_painter.end(); + glFlush(); + + // rendering directly to a texture is not supported on X11 and + // some Windows implementations, unfortunately + if (!hasDynamicTextureUpdate) + pbuffer->updateDynamicTexture(dynamicTexture); + + makeCurrent(); + // draw into the GL widget + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glFrustum(-1, 1, -1, 1, 10, 100); + glTranslatef(0.0f, 0.0f, -15.0f); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glViewport(0, 0, width(), height()); + + glBindTexture(GL_TEXTURE_2D, dynamicTexture); + glEnable(GL_TEXTURE_2D); + glEnable(GL_MULTISAMPLE); + glEnable(GL_CULL_FACE); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + // draw background + glPushMatrix(); + glScalef(1.7f, 1.7f, 1.7f); + glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + glCallList(tile_list); + glPopMatrix(); + + const int w = logo.width(); + const int h = logo.height(); + + glRotatef(rot_x, 1.0f, 0.0f, 0.0f); + glRotatef(rot_y, 0.0f, 1.0f, 0.0f); + glRotatef(rot_z, 0.0f, 0.0f, 1.0f); + glScalef(scale/w, scale/w, scale/w); + + glDepthFunc(GL_LESS); + glEnable(GL_DEPTH_TEST); + + // draw the Qt icon + glTranslatef(-w+1, -h+1, 0.0f); + for (int y=h-1; y>=0; --y) { + uint *p = (uint*) logo.scanLine(y); + uint *end = p + w; + int x = 0; + while (p < end) { + glColor4ub(qRed(*p), qGreen(*p), qBlue(*p), uchar(qAlpha(*p)*.9)); + glTranslatef(0.0f, 0.0f, wave[y*w+x]); + if (qAlpha(*p) > 128) + glCallList(tile_list); + glTranslatef(0.0f, 0.0f, -wave[y*w+x]); + glTranslatef(2.0f, 0.0f, 0.0f); + ++x; + ++p; + } + glTranslatef(-w*2.0f, 2.0f, 0.0f); + } + + // restore the GL state that QPainter expects + restoreGLState(); + + // draw the overlayed text using QPainter + p.setPen(QColor(197, 197, 197, 157)); + p.setBrush(QColor(197, 197, 197, 127)); + p.drawRect(QRect(0, 0, width(), 50)); + p.setPen(Qt::black); + p.setBrush(Qt::NoBrush); + const QString str1(tr("A simple OpenGL pbuffer example.")); + const QString str2(tr("Use the mouse wheel to zoom, press buttons and move mouse to rotate, double-click to flip.")); + QFontMetrics fm(p.font()); + p.drawText(width()/2 - fm.width(str1)/2, 20, str1); + p.drawText(width()/2 - fm.width(str2)/2, 20 + fm.lineSpacing(), str2); +} + +void GLWidget::mousePressEvent(QMouseEvent *e) +{ + anchor = e->pos(); +} + +void GLWidget::mouseMoveEvent(QMouseEvent *e) +{ + QPoint diff = e->pos() - anchor; + if (e->buttons() & Qt::LeftButton) { + rot_x += diff.y()/5.0f; + rot_y += diff.x()/5.0f; + } else if (e->buttons() & Qt::RightButton) { + rot_z += diff.x()/5.0f; + } + + anchor = e->pos(); + draw(); +} + +void GLWidget::wheelEvent(QWheelEvent *e) +{ + e->delta() > 0 ? scale += scale*0.1f : scale -= scale*0.1f; + draw(); +} + +void GLWidget::mouseDoubleClickEvent(QMouseEvent *) +{ + anim->start(); +} + +void GLWidget::animate(qreal val) +{ + rot_y = val * 180; + draw(); +} + +void GLWidget::animFinished() +{ + if (anim->direction() == QTimeLine::Forward) + anim->setDirection(QTimeLine::Backward); + else + anim->setDirection(QTimeLine::Forward); +} + +void GLWidget::saveGLState() +{ + glPushAttrib(GL_ALL_ATTRIB_BITS); + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); +} + +void GLWidget::restoreGLState() +{ + glMatrixMode(GL_PROJECTION); + glPopMatrix(); + glMatrixMode(GL_MODELVIEW); + glPopMatrix(); + glPopAttrib(); +} + +#define PI 3.14159 + +void GLWidget::timerEvent(QTimerEvent *) +{ + if (QApplication::mouseButtons() != 0) + return; + + static bool scale_in = true; + + if (scale_in && scale > 35.0f) + scale_in = false; + else if (!scale_in && scale < .5f) + scale_in = true; + + scale = scale_in ? scale + scale*0.01f : scale-scale*0.01f; + rot_z += 0.3f; + rot_x += 0.1f; + + int dx, dy; // disturbance point + float s, v, W, t; + int i, j; + static float wt[128][128]; + const int width = logo.width(); + const int AMP = 5; + + dx = dy = width >> 1; + + W = .3f; + v = -4; // wave speed + + for (i = 0; i < width; ++i) { + for ( j = 0; j < width; ++j) { + s = sqrt((double) ((j - dx) * (j - dx) + (i - dy) * (i - dy))); + wt[i][j] += 0.1f; + t = s / v; + if (s != 0) + wave[i*width + j] = AMP * sin(2 * PI * W * (wt[i][j] + t)) / (0.2*(s + 2)); + else + wave[i*width + j] = AMP * sin(2 * PI * W * (wt[i][j] + t)); + } + } +} diff --git a/examples/opengl/pbuffers2/glwidget.h b/examples/opengl/pbuffers2/glwidget.h new file mode 100644 index 0000000..8058a41 --- /dev/null +++ b/examples/opengl/pbuffers2/glwidget.h @@ -0,0 +1,85 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtOpenGL> +#include <QImage> +#include <QTimeLine> +#include <QSvgRenderer> + +class GLWidget : public QGLWidget +{ + Q_OBJECT + +public: + GLWidget(QWidget *parent); + ~GLWidget(); + + void saveGLState(); + void restoreGLState(); + + void paintEvent(QPaintEvent *); + void mousePressEvent(QMouseEvent *); + void mouseDoubleClickEvent(QMouseEvent *); + void mouseMoveEvent(QMouseEvent *); + void timerEvent(QTimerEvent *); + void wheelEvent(QWheelEvent *); + +public slots: + void animate(qreal); + void animFinished(); + void draw(); + +private: + QPoint anchor; + float scale; + float rot_x, rot_y, rot_z; + GLuint tile_list; + GLfloat *wave; + bool hasDynamicTextureUpdate; + + QImage logo; + QTimeLine *anim; + QSvgRenderer *svg_renderer; + + GLuint dynamicTexture; + QGLPixelBuffer *pbuffer; +}; + diff --git a/examples/opengl/pbuffers2/main.cpp b/examples/opengl/pbuffers2/main.cpp new file mode 100644 index 0000000..9d03beb --- /dev/null +++ b/examples/opengl/pbuffers2/main.cpp @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui/QApplication> +#include <QtGui/QMessageBox> +#include "glwidget.h" + +int main(int argc, char **argv) +{ + Q_INIT_RESOURCE(pbuffers2); + + QApplication a(argc, argv); + if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) { + QMessageBox::information(0, "OpenGL pbuffers 2", + "This system does not support OpenGL/pbuffers."); + return -1; + } + + GLWidget widget(0); + widget.resize(640, 480); + widget.show(); + return a.exec(); +} + diff --git a/examples/opengl/pbuffers2/pbuffers2.pro b/examples/opengl/pbuffers2/pbuffers2.pro new file mode 100644 index 0000000..cbd0cb5 --- /dev/null +++ b/examples/opengl/pbuffers2/pbuffers2.pro @@ -0,0 +1,21 @@ +###################################################################### +# Automatically generated by qmake (2.01a) Fri May 12 17:15:46 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +QT += opengl svg + +# Input +HEADERS += glwidget.h +SOURCES += glwidget.cpp main.cpp +RESOURCES += pbuffers2.qrc + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/opengl/pbuffers2 +sources.files = $$SOURCES $$HEADERS $$RESOURCES pbuffers2.pro *.png *.svg +sources.path = $$[QT_INSTALL_EXAMPLES]/opengl/pbuffers2 +INSTALLS += target sources diff --git a/examples/opengl/pbuffers2/pbuffers2.qrc b/examples/opengl/pbuffers2/pbuffers2.qrc new file mode 100644 index 0000000..986f1ca --- /dev/null +++ b/examples/opengl/pbuffers2/pbuffers2.qrc @@ -0,0 +1,6 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource prefix="/res"> + <file>bubbles.svg</file> + <file>designer.png</file> +</qresource> +</RCC> diff --git a/examples/opengl/samplebuffers/glwidget.cpp b/examples/opengl/samplebuffers/glwidget.cpp new file mode 100644 index 0000000..381ff95 --- /dev/null +++ b/examples/opengl/samplebuffers/glwidget.cpp @@ -0,0 +1,165 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "glwidget.h" +#include <math.h> + +#ifndef GL_MULTISAMPLE +#define GL_MULTISAMPLE 0x809D +#endif + +GLWidget::GLWidget(QWidget *parent) + : QGLWidget(QGLFormat(QGL::SampleBuffers), parent) +{ + startTimer(40); + setWindowTitle(tr("Sample Buffers")); +} + +void GLWidget::initializeGL() +{ + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(-.5, .5, .5, -.5, -1000, 1000); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glClearColor(1.0f, 1.0f, 1.0f, 1.0f); + + makeObject(); +} + +void GLWidget::resizeGL(int w, int h) +{ + glViewport(0, 0, w, h); +} + +void GLWidget::paintGL() +{ + static float rot = 0.0; + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glEnable(GL_MULTISAMPLE); + glTranslatef(-0.25f, -0.10f, 0.0f); + glScalef(0.75f, 1.15f, 0.0f); + glRotatef(rot, 0.0f, 0.f, 1.0f); + glCallList(list); + glPopMatrix(); + + glPushMatrix(); + glDisable(GL_MULTISAMPLE); + glTranslatef(0.25f, -0.10f, 0.0f); + glScalef(0.75f, 1.15f, 0.0f); + glRotatef(rot, 0.0f, 0.0f, 1.0f); + glCallList(list); + glPopMatrix(); + + rot += 0.2f; + + qglColor(Qt::black); + renderText(-0.35, 0.4, 0.0, "Multisampling enabled"); + renderText(0.15, 0.4, 0.0, "Multisampling disabled"); +} + +void GLWidget::timerEvent(QTimerEvent *) +{ + update(); +} + +void GLWidget::makeObject() +{ + QColor trolltechGreen(QColor::fromCmykF(0.40, 0.0, 1.0, 0.0)); + const double Pi = 3.14159265358979323846; + const int NumSectors = 15; + GLdouble x1 = +0.06; + GLdouble y1 = -0.14; + GLdouble x2 = +0.14; + GLdouble y2 = -0.06; + GLdouble x3 = +0.08; + GLdouble y3 = +0.00; + GLdouble x4 = +0.30; + GLdouble y4 = +0.22; + + list = glGenLists(1); + glNewList(list, GL_COMPILE); + { + for (int i = 0; i < NumSectors; ++i) { + double angle1 = (i * 2 * Pi) / NumSectors; + GLdouble x5 = 0.30 * sin(angle1); + GLdouble y5 = 0.30 * cos(angle1); + GLdouble x6 = 0.20 * sin(angle1); + GLdouble y6 = 0.20 * cos(angle1); + + double angle2 = ((i + 1) * 2 * Pi) / NumSectors; + GLdouble x7 = 0.20 * sin(angle2); + GLdouble y7 = 0.20 * cos(angle2); + GLdouble x8 = 0.30 * sin(angle2); + GLdouble y8 = 0.30 * cos(angle2); + + qglColor(trolltechGreen); + quad(GL_QUADS, x5, y5, x6, y6, x7, y7, x8, y8); + qglColor(Qt::black); + quad(GL_LINE_LOOP, x5, y5, x6, y6, x7, y7, x8, y8); + } + + qglColor(trolltechGreen); + quad(GL_QUADS, x1, y1, x2, y2, y2, x2, y1, x1); + quad(GL_QUADS, x3, y3, x4, y4, y4, x4, y3, x3); + + qglColor(Qt::black); + quad(GL_LINE_LOOP, x1, y1, x2, y2, y2, x2, y1, x1); + quad(GL_LINE_LOOP, x3, y3, x4, y4, y4, x4, y3, x3); + } + glEndList(); +} + +void GLWidget::quad(GLenum primitive, GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2, + GLdouble x3, GLdouble y3, GLdouble x4, GLdouble y4) +{ + glBegin(primitive); + { + glVertex2d(x1, y1); + glVertex2d(x2, y2); + glVertex2d(x3, y3); + glVertex2d(x4, y4); + } + glEnd(); +} diff --git a/examples/opengl/samplebuffers/glwidget.h b/examples/opengl/samplebuffers/glwidget.h new file mode 100644 index 0000000..dee739e --- /dev/null +++ b/examples/opengl/samplebuffers/glwidget.h @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtOpenGL> + +class GLWidget : public QGLWidget +{ +public: + GLWidget(QWidget *parent); + +protected: + void initializeGL(); + void resizeGL(int w, int h); + void paintGL(); + void timerEvent(QTimerEvent *); + + void makeObject(); + void quad(GLenum primitive, GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2, + GLdouble x3, GLdouble y3, GLdouble x4, GLdouble y4); + +private: + GLuint list; +}; + diff --git a/examples/opengl/samplebuffers/main.cpp b/examples/opengl/samplebuffers/main.cpp new file mode 100644 index 0000000..88a37b4 --- /dev/null +++ b/examples/opengl/samplebuffers/main.cpp @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui/QApplication> +#include <QtGui/QMessageBox> +#include "glwidget.h" + +int main(int argc, char **argv) +{ + QApplication a(argc, argv); + + QGLFormat f = QGLFormat::defaultFormat(); + f.setSampleBuffers(true); + QGLFormat::setDefaultFormat(f); + if (!QGLFormat::hasOpenGL()) { + QMessageBox::information(0, "OpenGL samplebuffers", + "This system does not support OpenGL."); + return 0; + } + + GLWidget widget(0); + + if (!widget.format().sampleBuffers()) { + QMessageBox::information(0, "OpenGL samplebuffers", + "This system does not have sample buffer support."); + return 0; + } + + widget.resize(640, 480); + widget.show(); + + return a.exec(); +} + diff --git a/examples/opengl/samplebuffers/samplebuffers.pro b/examples/opengl/samplebuffers/samplebuffers.pro new file mode 100644 index 0000000..9eb5f58 --- /dev/null +++ b/examples/opengl/samplebuffers/samplebuffers.pro @@ -0,0 +1,10 @@ +HEADERS += glwidget.h +SOURCES += glwidget.cpp main.cpp + +QT += opengl + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/opengl/samplebuffers +sources.files = $$SOURCES $$HEADERS samplebuffers.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/opengl/samplebuffers +INSTALLS += target sources diff --git a/examples/opengl/textures/glwidget.cpp b/examples/opengl/textures/glwidget.cpp new file mode 100644 index 0000000..5882373 --- /dev/null +++ b/examples/opengl/textures/glwidget.cpp @@ -0,0 +1,182 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> +#include <QtOpenGL> + +#include <math.h> + +#include "glwidget.h" + +GLuint GLWidget::sharedObject = 0; +int GLWidget::refCount = 0; + +GLWidget::GLWidget(QWidget *parent, QGLWidget *shareWidget) + : QGLWidget(parent, shareWidget) +{ + clearColor = Qt::black; + xRot = 0; + yRot = 0; + zRot = 0; +} + +GLWidget::~GLWidget() +{ + if (--refCount == 0) { + makeCurrent(); + glDeleteLists(sharedObject, 1); + } +} + +QSize GLWidget::minimumSizeHint() const +{ + return QSize(50, 50); +} + +QSize GLWidget::sizeHint() const +{ + return QSize(200, 200); +} + +void GLWidget::rotateBy(int xAngle, int yAngle, int zAngle) +{ + xRot += xAngle; + yRot += yAngle; + zRot += zAngle; + updateGL(); +} + +void GLWidget::setClearColor(const QColor &color) +{ + clearColor = color; + updateGL(); +} + +void GLWidget::initializeGL() +{ + if (!sharedObject) + sharedObject = makeObject(); + ++refCount; + + glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); + glEnable(GL_TEXTURE_2D); +} + +void GLWidget::paintGL() +{ + qglClearColor(clearColor); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glLoadIdentity(); + glTranslated(0.0, 0.0, -10.0); + glRotated(xRot / 16.0, 1.0, 0.0, 0.0); + glRotated(yRot / 16.0, 0.0, 1.0, 0.0); + glRotated(zRot / 16.0, 0.0, 0.0, 1.0); + glCallList(sharedObject); +} + +void GLWidget::resizeGL(int width, int height) +{ + int side = qMin(width, height); + glViewport((width - side) / 2, (height - side) / 2, side, side); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0); + glMatrixMode(GL_MODELVIEW); +} + +void GLWidget::mousePressEvent(QMouseEvent *event) +{ + lastPos = event->pos(); +} + +void GLWidget::mouseMoveEvent(QMouseEvent *event) +{ + int dx = event->x() - lastPos.x(); + int dy = event->y() - lastPos.y(); + + if (event->buttons() & Qt::LeftButton) { + rotateBy(8 * dy, 8 * dx, 0); + } else if (event->buttons() & Qt::RightButton) { + rotateBy(8 * dy, 0, 8 * dx); + } + lastPos = event->pos(); +} + +void GLWidget::mouseReleaseEvent(QMouseEvent * /* event */) +{ + emit clicked(); +} + +GLuint GLWidget::makeObject() +{ + static const int coords[6][4][3] = { + { { +1, -1, -1 }, { -1, -1, -1 }, { -1, +1, -1 }, { +1, +1, -1 } }, + { { +1, +1, -1 }, { -1, +1, -1 }, { -1, +1, +1 }, { +1, +1, +1 } }, + { { +1, -1, +1 }, { +1, -1, -1 }, { +1, +1, -1 }, { +1, +1, +1 } }, + { { -1, -1, -1 }, { -1, -1, +1 }, { -1, +1, +1 }, { -1, +1, -1 } }, + { { +1, -1, +1 }, { -1, -1, +1 }, { -1, -1, -1 }, { +1, -1, -1 } }, + { { -1, -1, +1 }, { +1, -1, +1 }, { +1, +1, +1 }, { -1, +1, +1 } } + }; + + + GLuint textures[6]; + for (int j=0; j < 6; ++j) + textures[j] = bindTexture(QPixmap(QString(":/images/side%1.png").arg(j + 1)), + GL_TEXTURE_2D); + + GLuint list = glGenLists(1); + glNewList(list, GL_COMPILE); + for (int i = 0; i < 6; ++i) { + glBindTexture(GL_TEXTURE_2D, textures[i]); + glBegin(GL_QUADS); + for (int j = 0; j < 4; ++j) { + glTexCoord2d(j == 0 || j == 3, j == 0 || j == 1); + glVertex3d(0.2 * coords[i][j][0], 0.2 * coords[i][j][1], + 0.2 * coords[i][j][2]); + } + glEnd(); + } + + glEndList(); + return list; +} diff --git a/examples/opengl/textures/glwidget.h b/examples/opengl/textures/glwidget.h new file mode 100644 index 0000000..623b301 --- /dev/null +++ b/examples/opengl/textures/glwidget.h @@ -0,0 +1,84 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef GLWIDGET_H +#define GLWIDGET_H + +#include <QGLWidget> + +class GLWidget : public QGLWidget +{ + Q_OBJECT + +public: + GLWidget(QWidget *parent = 0, QGLWidget *shareWidget = 0); + ~GLWidget(); + + QSize minimumSizeHint() const; + QSize sizeHint() const; + void rotateBy(int xAngle, int yAngle, int zAngle); + void setClearColor(const QColor &color); + +signals: + void clicked(); + +protected: + void initializeGL(); + void paintGL(); + void resizeGL(int width, int height); + void mousePressEvent(QMouseEvent *event); + void mouseMoveEvent(QMouseEvent *event); + void mouseReleaseEvent(QMouseEvent *event); + +private: + GLuint makeObject(); + + QColor clearColor; + QPoint lastPos; + int xRot; + int yRot; + int zRot; + + static GLuint sharedObject; + static int refCount; +}; + +#endif diff --git a/examples/opengl/textures/images/side1.png b/examples/opengl/textures/images/side1.png Binary files differnew file mode 100644 index 0000000..2e6b471 --- /dev/null +++ b/examples/opengl/textures/images/side1.png diff --git a/examples/opengl/textures/images/side2.png b/examples/opengl/textures/images/side2.png Binary files differnew file mode 100644 index 0000000..d1acf1b --- /dev/null +++ b/examples/opengl/textures/images/side2.png diff --git a/examples/opengl/textures/images/side3.png b/examples/opengl/textures/images/side3.png Binary files differnew file mode 100644 index 0000000..a638613 --- /dev/null +++ b/examples/opengl/textures/images/side3.png diff --git a/examples/opengl/textures/images/side4.png b/examples/opengl/textures/images/side4.png Binary files differnew file mode 100644 index 0000000..ebbc1e3 --- /dev/null +++ b/examples/opengl/textures/images/side4.png diff --git a/examples/opengl/textures/images/side5.png b/examples/opengl/textures/images/side5.png Binary files differnew file mode 100644 index 0000000..fa2f709 --- /dev/null +++ b/examples/opengl/textures/images/side5.png diff --git a/examples/opengl/textures/images/side6.png b/examples/opengl/textures/images/side6.png Binary files differnew file mode 100644 index 0000000..97141e4 --- /dev/null +++ b/examples/opengl/textures/images/side6.png diff --git a/examples/opengl/textures/main.cpp b/examples/opengl/textures/main.cpp new file mode 100644 index 0000000..5129cda --- /dev/null +++ b/examples/opengl/textures/main.cpp @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> + +#include "window.h" + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(textures); + + QApplication app(argc, argv); + Window window; + window.show(); + return app.exec(); +} diff --git a/examples/opengl/textures/textures.pro b/examples/opengl/textures/textures.pro new file mode 100644 index 0000000..cd98a68 --- /dev/null +++ b/examples/opengl/textures/textures.pro @@ -0,0 +1,13 @@ +HEADERS = glwidget.h \ + window.h +SOURCES = glwidget.cpp \ + main.cpp \ + window.cpp +RESOURCES = textures.qrc +QT += opengl + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/opengl/textures +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS textures.pro images +sources.path = $$[QT_INSTALL_EXAMPLES]/opengl/textures +INSTALLS += target sources diff --git a/examples/opengl/textures/textures.qrc b/examples/opengl/textures/textures.qrc new file mode 100644 index 0000000..efa9e9c --- /dev/null +++ b/examples/opengl/textures/textures.qrc @@ -0,0 +1,10 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> + <file>images/side1.png</file> + <file>images/side2.png</file> + <file>images/side3.png</file> + <file>images/side4.png</file> + <file>images/side5.png</file> + <file>images/side6.png</file> +</qresource> +</RCC> diff --git a/examples/opengl/textures/window.cpp b/examples/opengl/textures/window.cpp new file mode 100644 index 0000000..a974e68 --- /dev/null +++ b/examples/opengl/textures/window.cpp @@ -0,0 +1,89 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#include "glwidget.h" +#include "window.h" + +Window::Window() +{ + QGridLayout *mainLayout = new QGridLayout; + + glWidgets[0][0] = 0; + + for (int i = 0; i < NumRows; ++i) { + for (int j = 0; j < NumColumns; ++j) { + QColor clearColor; + clearColor.setHsv(((i * NumColumns) + j) * 255 + / (NumRows * NumColumns - 1), + 255, 63); + + glWidgets[i][j] = new GLWidget(0, glWidgets[0][0]); + glWidgets[i][j]->setClearColor(clearColor); + glWidgets[i][j]->rotateBy(+42 * 16, +42 * 16, -21 * 16); + mainLayout->addWidget(glWidgets[i][j], i, j); + + connect(glWidgets[i][j], SIGNAL(clicked()), + this, SLOT(setCurrentGlWidget())); + } + } + setLayout(mainLayout); + + currentGlWidget = glWidgets[0][0]; + + QTimer *timer = new QTimer(this); + connect(timer, SIGNAL(timeout()), this, SLOT(rotateOneStep())); + timer->start(20); + + setWindowTitle(tr("Textures")); +} + +void Window::setCurrentGlWidget() +{ + currentGlWidget = qobject_cast<GLWidget *>(sender()); +} + +void Window::rotateOneStep() +{ + if (currentGlWidget) + currentGlWidget->rotateBy(+2 * 16, +2 * 16, -1 * 16); +} diff --git a/examples/opengl/textures/window.h b/examples/opengl/textures/window.h new file mode 100644 index 0000000..50bb7f0 --- /dev/null +++ b/examples/opengl/textures/window.h @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef WINDOW_H +#define WINDOW_H + +#include <QWidget> + +class GLWidget; + +class Window : public QWidget +{ + Q_OBJECT + +public: + Window(); + +private slots: + void setCurrentGlWidget(); + void rotateOneStep(); + +private: + enum { NumRows = 2, NumColumns = 3 }; + + GLWidget *glWidgets[NumRows][NumColumns]; + GLWidget *currentGlWidget; +}; + +#endif |