From 8339121e0942e3e3b81af551a47be7b4a347608c Mon Sep 17 00:00:00 2001 From: Jason Barron Date: Wed, 1 Jun 2011 10:50:56 +0200 Subject: Add the new 'glhypnotizer' demo. Introduce a new demo to demonstrate the usage of OpenGL from a separate thread. Reviewed-by: Samuel --- demos/demos.pro | 4 + demos/glhypnotizer/glhypnotizer.pro | 21 +++ demos/glhypnotizer/hypnotizer.qrc | 6 + demos/glhypnotizer/main.cpp | 315 +++++++++++++++++++++++++++++++++++ demos/glhypnotizer/qt-logo.png | Bin 0 -> 2493 bytes demos/glhypnotizer/spiral.svg | 100 +++++++++++ doc/src/demos/glhypnotizer.qdoc | 38 +++++ doc/src/images/glhypnotizer-demo.png | Bin 0 -> 46888 bytes 8 files changed, 484 insertions(+) create mode 100644 demos/glhypnotizer/glhypnotizer.pro create mode 100644 demos/glhypnotizer/hypnotizer.qrc create mode 100644 demos/glhypnotizer/main.cpp create mode 100644 demos/glhypnotizer/qt-logo.png create mode 100644 demos/glhypnotizer/spiral.svg create mode 100644 doc/src/demos/glhypnotizer.qdoc create mode 100644 doc/src/images/glhypnotizer-demo.png diff --git a/demos/demos.pro b/demos/demos.pro index f1d5b00..ed18446 100644 --- a/demos/demos.pro +++ b/demos/demos.pro @@ -44,6 +44,9 @@ wince*: SUBDIRS = \ contains(QT_CONFIG, opengl):!contains(QT_CONFIG, opengles1):!contains(QT_CONFIG, opengles2):{ SUBDIRS += demos_boxes } +contains(QT_CONFIG, opengl):contains(QT_CONFIG, svg){ +SUBDIRS += demos_glhypnotizer +} mac* && !qpa: SUBDIRS += demos_macmainwindow wince*|symbian|embedded|x11: SUBDIRS += demos_embedded @@ -103,6 +106,7 @@ demos_browser.subdir = browser demos_boxes.subdir = boxes demos_sub-attaq.subdir = sub-attaq demos_spectrum.subdir = spectrum +demos_glhypnotizer.subdir = glhypnotizer #CONFIG += ordered !ordered { diff --git a/demos/glhypnotizer/glhypnotizer.pro b/demos/glhypnotizer/glhypnotizer.pro new file mode 100644 index 0000000..a7fdf5e --- /dev/null +++ b/demos/glhypnotizer/glhypnotizer.pro @@ -0,0 +1,21 @@ +TEMPLATE = app +CONFIG -= moc +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +QT += opengl svg +RESOURCES = hypnotizer.qrc + +build_all:!build_pass { + CONFIG -= build_all + CONFIG += release +} + +# install +target.path = $$[QT_INSTALL_DEMOS]/glhypnotizer +sources.files = $$SOURCES $$HEADERS $$RESOURCES *.png *.pro *.svg +sources.path = $$[QT_INSTALL_DEMOS]/glhypnotizer +INSTALLS += target sources + +symbian: include($$QT_SOURCE_TREE/demos/symbianpkgrules.pri) diff --git a/demos/glhypnotizer/hypnotizer.qrc b/demos/glhypnotizer/hypnotizer.qrc new file mode 100644 index 0000000..97ba159 --- /dev/null +++ b/demos/glhypnotizer/hypnotizer.qrc @@ -0,0 +1,6 @@ + + + qt-logo.png + spiral.svg + + \ No newline at end of file diff --git a/demos/glhypnotizer/main.cpp b/demos/glhypnotizer/main.cpp new file mode 100644 index 0000000..d6f3dc9 --- /dev/null +++ b/demos/glhypnotizer/main.cpp @@ -0,0 +1,315 @@ +#include +#include +#include + +#define NUM_SWIRLY_ITEMS 10 + +class GLPainter : public QObject +{ + Q_OBJECT +public: + GLPainter(QGLWidget *widget); + void stop(); + void resizeViewport(const QSize &size); + +public slots: + void start(); + +protected: + void timerEvent(QTimerEvent *event); + void paint(); + void initSwirlyItems(); + void updateSwirlyItems(); + void drawSuggestiveMessages(QPainter *p); + +private: + QMutex mutex; + QGLWidget *glWidget; + int viewportWidth; + int viewportHeight; + bool doRendering; + qreal rotationAngle; + bool swirlClockwise; + QSvgRenderer svgRenderer; + QPixmap logo; + QPainter::PixmapFragment swirlyItems[NUM_SWIRLY_ITEMS]; + int swirlyCounter; + int textCounter; + int messageYPos; + qreal scaleFactor; +}; + + +GLPainter::GLPainter(QGLWidget *widget) + : glWidget(widget) + , doRendering(true) + , rotationAngle(rand() % 360) + , swirlClockwise((rand() % 2) == 1) + , svgRenderer(QLatin1String(":/spiral.svg"), this) + , logo(QLatin1String(":/qt-logo.png")) +{ +} + +void GLPainter::start() +{ + glWidget->makeCurrent(); + startTimer(20); +} + +void GLPainter::stop() +{ + QMutexLocker locker(&mutex); + doRendering = false; +} + +void GLPainter::resizeViewport(const QSize &size) +{ + QMutexLocker locker(&mutex); + viewportWidth = size.width(); + viewportHeight = size.height(); + initSwirlyItems(); + textCounter = 0; + messageYPos = -1; +} + +void GLPainter::timerEvent(QTimerEvent *event) +{ + QMutexLocker locker(&mutex); + if (!doRendering) { + killTimer(event->timerId()); + QThread::currentThread()->quit(); + return; + } + updateSwirlyItems(); + paint(); +} + +void GLPainter::paint() +{ + QPainter p(glWidget); + p.fillRect(QRect(0, 0, viewportWidth, viewportHeight), Qt::white); + p.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); + p.translate(viewportWidth / 2, viewportHeight / 2); + p.rotate(rotationAngle * (swirlClockwise ? 1 : -1)); + p.scale(svgRenderer.viewBox().width() / 200.0 * 0.65, + svgRenderer.viewBox().height() / 200.0 * 0.65); + p.translate(-viewportWidth / 2, -viewportHeight / 2); + svgRenderer.render(&p); + p.resetTransform(); + p.drawPixmapFragments(swirlyItems, NUM_SWIRLY_ITEMS, logo); + drawSuggestiveMessages(&p); + p.end(); + rotationAngle += 1.2; +} + +void GLPainter::drawSuggestiveMessages(QPainter *p) +{ + const int numSuggestiveMessages = 7; + const char *texts[numSuggestiveMessages] = {" You feel relaxed.. ", + " Let your mind wander.. ", + " Look deep into the swirls.. ", + " Even deeper.. ", + " Qt is good! ", + " Qt is good for you! ", + " You MUST use Qt! "}; + QFont font(p->font()); + font.setPointSizeF(font.pointSizeF() * viewportWidth/200.0); + p->setFont(font); + QFontMetrics fm(font); + int messageNo = textCounter/314; + if (messageNo > 6 || messageNo < 0) { + qFatal("This should not happen: %d - %d", messageNo, textCounter); + } + QLatin1String text(texts[textCounter / 314]); + qreal textWidth = fm.width(text); + int alpha = 255 * qAbs(qSin(textCounter * 0.01)); + if (messageYPos < 0 || (textCounter % 314 == 0)) { + messageYPos = qBound(fm.height(), rand() % viewportHeight, + viewportHeight - fm.height()); + } + p->setPen(QColor(255, 255, 255, alpha)); + p->setBackground(QColor(50, 50, 50, qBound(0, alpha, 50))); + p->setBackgroundMode(Qt::OpaqueMode); + p->drawText((viewportWidth / 2) - (textWidth/ 2), messageYPos, text); + ++textCounter; + if (textCounter >= (314 * numSuggestiveMessages)) + textCounter = 0; +} + +void GLPainter::initSwirlyItems() +{ + swirlyCounter = swirlClockwise ? 0 : 360; + scaleFactor = viewportWidth / 200.0; + QRectF logoRect(0, 0, logo.width(), logo.height()); + + for (int i=0; i 0 && factor <= 360) { + swirlyItems[i].x = viewportWidth / 2.0 + qSin(factor * 0.05) * (viewportWidth / 2.0) * (100.0 / factor); + swirlyItems[i].y = viewportHeight / 2.0 + qCos(factor * 0.05) * (viewportHeight / 2.0) * (100.0 / factor); + swirlyItems[i].rotation += -swirlyCounter * 0.01; + swirlyItems[i].scaleX += swirlClockwise ? -scaleFactor * 1 / 360.0 : scaleFactor * 1 / 360.0; + if (swirlClockwise) { + if (swirlyItems[i].scaleX < 0) + swirlyItems[i].scaleX = scaleFactor; + } else { + if (swirlyItems[i].scaleX > scaleFactor) + swirlyItems[i].scaleX = scaleFactor * 1 / 360.0; + } + swirlyItems[i].scaleY = swirlyItems[i].scaleX; + } else { + swirlyItems[i].scaleX = swirlyItems[i].scaleY = 0; + } + } + if (swirlClockwise) { + if (swirlyCounter > (360 + NUM_SWIRLY_ITEMS * 20)) + swirlyCounter = 0; + } else { + if (swirlyCounter < -NUM_SWIRLY_ITEMS * 20) + swirlyCounter = 360; + } +} + +class GLWidget : public QGLWidget +{ +public: + GLWidget(QWidget *parent, QGLWidget *shareWidget = 0); + ~GLWidget(); + void startRendering(); + void stopRendering(); + +protected: + void resizeEvent(QResizeEvent *event); + void paintEvent(QPaintEvent *event); + QSize sizeHint() const { return QSize(200, 200); } + +private: + GLPainter glPainter; + QThread glThread; +}; + +GLWidget::GLWidget(QWidget *parent, QGLWidget *share) + : QGLWidget(QGLFormat(QGL::SampleBuffers), parent, share) + , glPainter(this) + , glThread(this) +{ + setAttribute(Qt::WA_PaintOutsidePaintEvent); + setAttribute(Qt::WA_DeleteOnClose); +} + +GLWidget::~GLWidget() +{ + stopRendering(); +} + +void GLWidget::startRendering() +{ + glPainter.moveToThread(&glThread); + connect(&glThread, SIGNAL(started()), &glPainter, SLOT(start())); + glThread.start(); +} + +void GLWidget::stopRendering() +{ + glPainter.stop(); + glThread.wait(); +} + +void GLWidget::resizeEvent(QResizeEvent *event) +{ + glPainter.resizeViewport(event->size()); +} + +void GLWidget::paintEvent(QPaintEvent *) +{ + // Handled by GLPainter. +} + +class MainWindow : public QMainWindow +{ + Q_OBJECT +public: + MainWindow(); + +private slots: + void newThread(); + void killThread(); + +private: + QMdiArea mdiArea; + QGLWidget shareWidget; +}; + +MainWindow::MainWindow() + : QMainWindow(0) + , mdiArea(this) + , shareWidget(this) +{ + setWindowTitle("Qt GL Hypnotizer"); + QMenu *menu = menuBar()->addMenu("&Hypnotizers"); + menu->addAction("&New hypnotizer thread", this, SLOT(newThread()), QKeySequence("Ctrl+N")); + menu->addAction("&End current hypnotizer thread", this, SLOT(killThread()), QKeySequence("Ctrl+K")); + menu->addSeparator(); + menu->addAction("E&xit", qApp, SLOT(quit()), QKeySequence("Ctrl+Q")); + + setCentralWidget(&mdiArea); + shareWidget.resize(1, 1); + newThread(); +} + +void MainWindow::newThread() +{ + static int windowCount = 1; + if (mdiArea.subWindowList().count() > 9) + return; + GLWidget *widget = new GLWidget(&mdiArea, &shareWidget); + mdiArea.addSubWindow(widget); + widget->setWindowTitle("Thread #" + QString::number(windowCount++)); + widget->show(); + widget->startRendering(); +} + +void MainWindow::killThread() +{ + delete mdiArea.activeSubWindow(); +} + +int main(int argc, char *argv[]) +{ + // Make Xlib and GLX thread safe under X11 + QApplication::setAttribute(Qt::AA_X11InitThreads); + QApplication application(argc, argv); + + // Using QPainter to draw into QGLWidgets is only supported with GL 2.0 + if (!((QGLFormat::openGLVersionFlags() & QGLFormat::OpenGL_Version_2_0) || + (QGLFormat::openGLVersionFlags() & QGLFormat::OpenGL_ES_Version_2_0))) { + QMessageBox::information(0, "Qt GL Hypnotizer", + "This system does not support OpenGL 2.0 or OpenGL ES 2.0, " + "which is required for this example to work."); + return 0; + } + + MainWindow mainWindow; + mainWindow.show(); + return application.exec(); +} + +#include "main.moc" diff --git a/demos/glhypnotizer/qt-logo.png b/demos/glhypnotizer/qt-logo.png new file mode 100644 index 0000000..eaea344 Binary files /dev/null and b/demos/glhypnotizer/qt-logo.png differ diff --git a/demos/glhypnotizer/spiral.svg b/demos/glhypnotizer/spiral.svg new file mode 100644 index 0000000..ff1f047 --- /dev/null +++ b/demos/glhypnotizer/spiral.svg @@ -0,0 +1,100 @@ + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + diff --git a/doc/src/demos/glhypnotizer.qdoc b/doc/src/demos/glhypnotizer.qdoc new file mode 100644 index 0000000..97b3c3b --- /dev/null +++ b/doc/src/demos/glhypnotizer.qdoc @@ -0,0 +1,38 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** 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 Technology Preview License Agreement accompanying +** this package. +** +** GNU Free Documentation License +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of this +** file. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \example demos/glhypnotizer + \title GL Hypnotizer + + This demo shows how to use OpenGL from a seperate thread. + + \image glhypnotizer-demo.png + + New MDI windows can be added by pressing Ctrl-N and each new child + window renders in it's own thread. +*/ diff --git a/doc/src/images/glhypnotizer-demo.png b/doc/src/images/glhypnotizer-demo.png new file mode 100644 index 0000000..5b7a1ae Binary files /dev/null and b/doc/src/images/glhypnotizer-demo.png differ -- cgit v0.12