Qt 5 introduced a new set of OpenGL classes in Qt Gui and a new rendering pipeline for Qt Quick with the scenegraph. As awesome as these are, they were based on the newly introduced QWindow, making it very hard to use them in existing applications.
To remedy this problem, Qt 5.1 introduces the function QWidget::createWindowContainer(). A function that creates a QWidget wrapper for an existing QWindow, allowing it to live inside a QWidget-based application. Using QQuickView or QOpenGLContext together with widgets is now possible.
The window container works by forcing the use of native child widgets inside the widgets hierarchy and will reparent the window in the windowing system. After that, the container will manage the window’s geometry and visibility. The rendering of the window happens directly in the window without any interference from the widgets, resulting in optimal performance.
As can be seen from the code-snippet above, the container can also receive focus.
Embedding the “Other Way”
This feature covers the use case where an application wants to either port an existing view to either Qt Quick or the new OpenGL classes in Qt Gui. What about the use case where an application’s mainview is written with widgets, say QGraphicsView, and the application wants to keep this and rewrite the surrounding UI with Qt Quick? Well, this is doable by keeping the main application QWidget-based and making each of the big UI blocks a QQuickView embedded in its own container.
With Qt 5 we are making the bold move of making OpenGL 2.0 a requirement for using QtQuick2. There are several challenges with this new requirement, many of which have been discussed on the blogs and mailing lists already, but today I want to talk about one issue in particular and that’s OpenGL on Windows.
Microsoft has included OpenGL in Windows since Windows 98 (or earlier?), however the initial version was a software implementation and only supported version 1.1 of the API. Over time, the implementation has been improved so that today it is implemented as a wrapper on top of Direct3D. This is great, but unfortunately the version of the API that is supported has remained the same and that means it is not sufficient for running QtQuick2 or even Qt’s GL paint engine. You can insert your own conspiracy theory about why Microsoft has let OpenGL stagnate on Windows, but every theory will likely involve Direct3D. D3D is a component of DirectX that offers similar functionality to OpenGL. It is most commonly used by game developers to target desktop Windows and also XBox, but is also used by CAD applications.
Typically when a developer wants to run or develop something that requires OpenGL on Windows, the first place they go is to their hardware vendor’s website and download the latest drivers from Intel, NVidia, AMD or whoever.. This is fine, but can be somewhat inconvenient for end users of OpenGL applications. We wanted to avoid this problem with Qt applications so deployment would essentially be the same as with Qt 4 (but with possibly more DLLs). Luckily for us, we weren’t the first people to experience this problem. Most browsers these days (except IE) are supporting WebGL out of the box. WebGL is a 3D canvas API available through Javascript. The API is based on OpenGL ES 2.0 and the shader language is GLSL ES. So how can browsers offer an OpenGL 2 API without requiring new drivers? The solution is called the ANGLE (Almost Native Graphics Layer Engine) project.
ANGLE is a BSD licensed project that implements the OpenGL ES 2.0 API on top of DirectX 9.0c. In addition to this, it also implements the EGL API and contains a shader cross compiler that takes GLSL ES code and converts it to HLSL which is the shader language used in DirectX. So ANGLE allows us to essentially run the Qt Scene Graph on top of DirectX without modifying any of our code. Friedemann has already done most of the hard work by adding support for GLES2 and EGL into the Windows platform plugin and as a result it has been possible to use Qt 5 with ANGLE for quite some time now. However, to make this experience more painless, we are including ANGLE in Qt’s 3rdparty directory and making it the default OpenGL configuration on Windows.
For QML applications and lightweight OpenGL apps, you can use this new configuration to deploy your apps without requiring your end users to install new graphics drivers. On the other hand if your application requires full desktop OpenGL, then it’s simply a matter of configuring Qt with “-opengl desktop” and you get the same behavior as before.
One additional benefit of using ANGLE on Windows is that our graphics pipeline and multimedia pipeline are now based on the same underlying technology (DirectX) so we can take some shortcuts to get video frames into the Scene Graph more efficiently. Taking this one step further, Windows Vista introduced a new driver model called WDDM and one of the features here is that DirectX surfaces can now be shared across processes. This little feature will come in handy for our WebKit2 work which has seperate processes for the web renderer and the ui. All of this goodness might not make it into the 5.0 release, but it’s all in progress so hopefully it makes an appearance soon!
Well its taken 3+ years for Qt 3D to be an overnight success, but here it is: we are now part of Qt5 and we’ll be front and centre in the exciting picture that will be powering a new generation of QML enhanced apps on a whole range of platforms. As a Qt5 Essential Qt 3D will be running on all those supported Qt5 platforms providing OpenGL accelerated 3D content defined in QML. Its great news for us in the team, and good news for those of you using Qt 3D or planning on using it for your projects.
If you’re a Qt 3D fan and have been following the project for a while you’ll know that as a labs project we had to do our own releases, carefully making sure that packages we created matched the current supported versions of Qt 4. We have in the past released easy-to-use packages for Symbian^3, N900 and the N9 platforms, as well as for Windows desktop; and supported Linux and Mac with source packages. We got great feedback from those package releases, and saw some interesting projects built on Qt 3D.
As part of Qt5 the SDK team and release program will make Qt 3D available through official Qt5 release activities. This will free us up to spend more time on performance, bug-fixing and best of all the features that we’ve been asked for. This is fantastic news for us since packaging consumed quite a bit of our resources, and now we can focus that on improving Qt 3D.
Before we move away from Labs status we are making one more Qt 3D labs release: and this is it. This follows our Qt 3D TP1 and TP2 releases with a 1.0 release as part of our Qt 4.x programme. In this release are a number of important bug fixes, some new stuff like threaded texture and model loading, and the new Qt 3D Asset Viewer, which allows you to visually configure the models you load into your Qt 3D applications.
Today we announce our Qt 3D 1.0 for Qt 4.8.1 with only a small amount of fanfare: this will be our last 4.x major version release.
For N9 developers we no longer create a package. The publicly available Qt SDK can build these packages for you from our source download using the Qt 4.7.x Harmattan support, that is built right in to your SDK. Just follow the special N9 instructions in our build documentation. For Symbian likewise you will need to create your own packages. For folks using a MinGW based Qt on Windows, you will need to use the source package also.
Why are we not supplying N9 or Symbian packages for Qt 3D 1.0 against Qt 4.x? Since Qt 3D 1.0 for 4.x is a labs project, our previous device packages were not official Nokia system packages, and just functioned as a convenience to the developer. This is why we’re asking that you create your own packages to underscore the fact that Nokia does not supply Qt 3D system packages as part of Qt 4.x, and you’ll need to do your own work to include any Qt 3D functionality in your device apps. Apologies for this, but its the way it is for our 4.x labs status programme. Qt 3D team remains available on IRC and via our email lists (see below) to support you as far as possible when including Qt 3D in your apps, but unfortunately for Qt 4.x we cannot supply Nokia system packages for these platforms.
What about Qt5? That is where the excitement lies, with a vastly improved QML rendering engine, which has the benefit for Qt 3D of guaranteed OpenGL support. If you have QML2 then you have OpenGL and that means Qt 3D is ready to go on any Qt5 platform. We’d love you to try out Qt 3D on Qt5 – go and check out the Qt5 Alpha release which has just been announced, and tell us what you think. If you want to try out the bleeding edge, you can also get Qt 3D in Qt5 by building from Git.
Published Wednesday 29, 2012 | by Gareth Stockwell
Introduction
A topic which has been receiving quite a bit of attention recently is the use of shader programs to apply effects within Qt Quick applications. Once you have grasped the basics of the shader programming language, embedding the shaders in your application is made really easy by Qt Quick, with the result that stunning visual effects can be achieved from a surprisingly small amount of code.
What hasn’t been mentioned previously in this blog is that applying shader effects to multimedia content (video playback, or a camera viewfinder) is just as easy as transforming any other QML element. This post shows some of the results you can achieve by combining shader programming with QtMultimedia.
The best way to demonstrate some of the effects you can achieve is … with a demo, so that’s where we’ll start.
qmlvideofx demo running on Qt 5, on desktop Linux
The videos above show a Qt Quick 2 demo app, running on a desktop Linux machine. Aside from some ancillary C++ code (listening to scene graph signals in order to calculate the frame rate, and reading shader programs from the file system), the app is entirely written in QML.
Implementation details
As previously described, Qt Quick 2 comes with built-in support for shader effects via the ShaderEffect QML element. Using this to apply an effect to video or viewfinder content really isn’t any more difficult than applying the same effect to any other QML element. To illustrate this, the code snippet below shows a time-varying wobble effect being applied to a video clip which is played from a file.
import QtQuick 2.0
import QtMultimedia 5.0
Rectangle {
width: 600
height: 400
MediaPlayer {
id: mediaPlayer
autoplay: true
source: "test.ogg"
}
VideoOutput {
id: videoOutput
anchors.fill: parent
source: mediaPlayer
}
ShaderEffect {
anchors.fill: parent
// Properties which will be passed into the shader as uniforms
property real amplitude: 0.02
property real frequency: 20
property real time: 0
NumberAnimation on time {
loops: Animation.Infinite
from: 0
to: Math.PI * 2
duration: 600
}
property variant source: ShaderEffectSource {
sourceItem: videoOutput
hideSource: true
}
fragmentShader: "
uniform highp float amplitude;
uniform highp float frequency;
uniform highp float time;
uniform sampler2D source;
uniform lowp float qt_Opacity;
varying highp vec2 qt_TexCoord0;
void main() {
highp vec2 p = sin(time + frequency * qt_TexCoord0);
highp vec2 tc = qt_TexCoord0 + amplitude * vec2(p.y, -p.x);
gl_FragColor = qt_Opacity * texture2D(source, tc);
}
"
}
}
Applying the effect to a viewfinder stream rather than to video playback is simply a matter of replacing the Video element with a Camera element.
In the qmlvideofx demo, each effect is implemented as a QML element which inherits from ShaderEffect; these elements are dynamically loaded and applied to the video content when the user selects the corresponding effect from the menu. Similarly, the different inputs (image, video and camera) are represented by QML elements which are dynamically loaded when required.
The set of parameters supported by each effect (e.g. ‘granularity’ for Pixelate; ‘radius and diffraction’ for Magnify) is exposed as a ListModel which is used to construct sliders via which the parameter values can be adjusted.
The code (Qt 5.x)
The source for the qmlvideofx demo can be found in the qtmultimedia repository here:
Until Qt 5.0 is released, you’ll have to build it from source in order to build and run the demo. Instructions for how to do that can be found on the wiki here. The required subset of Qt modules can be cloned as follows:
While Qt Quick 1 does not have built-in support for shader effects, it is provided by the Qt.labs.shaders plugin, which is shipped with Qt from version 4.7.4 onwards. A Qt 4 version of the demo is available here:
In addition to running on desktop platforms (tested on Linux and Windows), the Qt 4 version of the demo will also run on mobile devices – the video below shows it running on a Symbian device (Nokia C-701).
qmlvideofx demo running on Qt 4, on Symbian (Nokia C-701)
Keen-eyed viewers will notice that the app has a slightly different UI to the desktop version – thanks to the flexibility of Qt Quick, that is achieved by substituting a single QML file which describes the layout.
It should be noted that the app will not run on currently available versions of the Symbian platform. The reason is that it requires the output of the video decoder to be available to the OpenGLES engine as a texture. An EGL extension which allows the video and graphics stacks to cooperate in this way (EGL_NOK_image_endpoint2) will be added in a future Symbian release. Once this is available, QtMultimediaKit will automatically make use of it (see QTMOBILITY-1818), and the demo will start working.
You can, however, run the app today on the Nokia N9 – as shown by the video below.
qmlvideofx demo running on Qt 4, on MeeGo Harmattan (Nokia N9)
Further information
If you weren’t already excited about the potential of shader effects, hopefully you are now. Below are some links to other examples of their use in conjunction with Qt/QML.
The Qt Graphical Effects project is on its way to offer a set of design neutral visual effects for Qt Quick 2.0.
Over twenty ready-made QML graphical effect elements are currently available. The effects include code for blending, masking, blurring, coloring, and much more. There are still areas to improve and extend — all ideas, feedback, proposals and even concrete contributions are welcome!
The Effects
Using the effects
Using the graphical effect elements should be straightforward for developers and technically oriented designers as you only need to know basic Qt Quick/QML to get started.
Any QML item can be used as a source item for an effect. You can add a drop shadow to an image, for instance, as follows:
git clone git@gitorious.org:qt-labs/qtgraphicaleffects.git
cd qtgraphicaleffects
qmake
make install
To see the effects in action, run the code examples from the graphical effects doc/src/snippets folder:
qmlscene doc/src/snippets/DropShadow-example.qml
or launch the Testbed application:
qmlscene tests/manual/testbed/testBed.qml
The Testbed application is a convenient way to browse the effects and their properties as well as to visualise the results. Select an effect from the left, adjust its properties on the right, and see the result in the center in realtime:
API and documentation
The API for each effect is a set of QML properties. The effect properties can be animated just like any other QML properties. The documention contains property descriptions and basic usage examples. To generate the documentation, execute the following commands in the qtgraphicaleffects projects folder:
qmake
make docs
Open the generated doc/html/qml-graphicaleffects-index.html in a browser to view the documentation.
Implementation details
QML ShaderEffect is a built-in element in Qt Quick 2.0. This powerful QML element allows combining OpenGL Shading Language (GLSL) and QML, and makes it easy to implement visually impressive custom effects. Under the hood, all the effects in this project are based on ShaderEffect elements. Only QML and GLSL have been used, and there are no additional C++ APIs. If you are familiar with GLSL, you can easily study how the existing effects have been implemented. You can then create custom effects by modifying and combining the basic effects.
It is also quite ripe for testing if you are into that kind of thing. This is my personally recommended approach to testing QtQuick 2 on your n9(50) at this point in time and I have to stress that these steps are not officialy sanctioned. I don’t like chroot environments, and since my builds are restricted to Qt which uses the ever rational qmake build system (lone vocal fanboi here) which respects build (qmake profile) sovereignty, I don’t feel I need one. This is an alternate build approach to the webkit team’s approach linked to below and if you would rather follow in the footsteps of wisdom you might want to tail them.
The Webkit team were gracious enough to jot down these instructions:
This mkspec clearly has to be adjusted to reflect your local dev paths.
0) Realize this is dangerous and might require the reflashing of your device/loss of user data
1) Install the dependencies (as documented in the webkit teams instructions above) in your HARMATTAN_ARMEL target under scratchbox
2) Replace any fully qualified symlinks under $HARMATTAN_ARMEL/usr/lib with relative ones
3) set PKG_CONFIG_SYSROOT_DIR and PKG_CONFIG_PATH in relation to your SYSROOT
4) Run configure directly from qtbase
5) Use the resulting qmake to build the qtdeclarative module
6) Deploy Qt and the required xcb libs to the device (you will need to be root, supplement the existing files don’t override exist files)
You can use:
objdump -x ./plugins/platforms/libxcb.so
to establish what dependencies need to be fulfilled. Up until every required library is present, Qt is gonna tell you that the XCB backend does not exist.
I personally use shadow builds (build out of source) since I am targetting a wide range of devices and discard my builds with fair insane regularity. I have no hit any issues using them in Qt5 when targetting qtbase/qtdeclarative.
Similar steps would clearly probably work for the n900 but, alas, the version of xcb packaged as part of the Fremantle SDK is too old to be used with the XCB QPA backend as it stands. It will take a braver man than me with more time to kill to get that turkey airborne at this point in time. (I also managed to render my Meego 1.2.9 CE unbootable by trying to install the build dependencies, so I don’t see any really convenient avenue for QtQuick 2 experimentation on the n900. Prove me wrong)
Bon appetizer:
Graphic proof of flight, incase you are faring poorly and suspecting that I am a fibber.
Update
The Webkit guys (who tend to have their soup together) have created packages for Qt 5 (along with packing up all the xcb dependencies)
We’ve been doing lots of interesting stuff around accelerated compositing in QtWebKit in the last year since my last blog post, and several people have asked for information about it. So here goes a brain-dump, hope some people find it useful
If you want to know more about the basics of accelerated compositing, please read Ariya’s blog first (or this one, also from Ariya), as this post is maybe a bit more advanced. Please proceed if you’re interested in QtWebKit internals regarding accelerated graphics…
The raspberry pi initiative is very compelling and (forgive me for the hype) just got even more compelling now that we have Qt 5 running with full acceleration on the target. A tagline of “An ARM Linux box for $25. Take a byte!” sums it up rather nicely and should get many a geeks pulse racing. The Mer community have been good about getting Qt up and running on the Raspberry PI and we appreciate their drive and rapid work. We will now proceed to document how to build Qt in order to have full OpenGL ES 2 (tested) and OpenVG support (untested)
The mkspecs are here with associated patch(es).
The modified simplegl driver (required for QWS support in Qt 4.8) is here.
I have tested Qt 4.8 QWS with the simplegl driver, Qt 4.8 QPA (lighthouse) trunk with the EGLFS driver and Qt 5 trunk with the EGLFS driver. We are looking great on all fronts. (Videos to follow, but the performance is great at 1360×768) The required changes were a couple lines of Broadcom specific initialization code which is added to the simplegl connect method and the QEglFSScreen constructor respectively for QWS/QPA.
We clearly can’t distribute the libraries/headers the above mkspecs reference. (Qt Embedded/QPA are both largely self contained and only require external headers/libs for EGL/GLES2/OpenVG support)
I am using the Code Sourcery 2010q1 compiler.
I am using a sd-card (read snapshot) with all the required libraries (and the EGL/GLES2/VG headers required to compile Qt)
I was happy to see how easy it was to get Qt 5 cross compiled and running on the device. I restricted my build efforts to qtbase and qtdeclarative, but this built rapidly and without fault.
Caveat!: Before proceeding, my instructions are home brewed, pragmatic and get the job done. If you have better/functional instructions on getting the Qt 5 repo to a buildable state, then please don’t walk off a cliff with me.
Once this was built and you have your trusty qt 5 relevant qmake, creating a qtdeclarative directory within your shadow built qt, and running:
qmake /opt/dev/src/qt5/qtdeclarative/qtdeclarative.pro
make
make install
Gets you Qt Quick 2 (Scenegraph) enriched and merrily on your road to riches.
What remains to be done:
1) The device has OpenMax support which we could use for Audio/Video/Image decoding. It would be nice to see whether we can utilize this in Qt’s image loading plugin mechanism.
2) Get qtwebkit compiled, deployed and running on the target (Python related hijinxs stemming from the build-webkit script postponed inclusion in the done list above)
3) Turtles the whole way down (There is no shortage of board specific customization we could potentially indulge in)
I look forward to the deluge of videos showing the capabilities of accelerated Qt (4 or 5) on this fine hardware. We will be demoing this device in the demo pavilion at this year’s Qt Developer Day event in Munich and San Francisco and I will personally be on hand to answer any posting/cross compilation questions you may happen to have.
Since someone said “hey, let’s throw in some 3D stuff” back in 2009, what was initially a simple set of OpenGL bindings for Qt has grown to become a fully fledged Qt module. The path from R&D to being a “real” part of the Qt world has been a long one, and so it’s with great happiness that we share our second Technology Preview of Qt Quick 3D.
This preview follows our earlier public showing back in May, and we have taken what we learned from preview and used it to make Qt Quick 3D more streamlined, more complete, and generally (in our opinion at least) better all around.
We would like to say a huge thank-you to the community for your constant and valuable feedback. It is this interaction with users which has driven the development of Qt Quick 3D, and will continue to do so into the future. In the last four months or so we have been fixing a lot of the bugs which you all reported to us, and have worked to stabilise and extend make the 3D framework across all of our key platforms. Right now we support:
Symbian
Meego 1.2 Harmattan
Maemo
The usual suspects among desktop operating systems
Along with the bug fixes we’ve had time to throw in a number of new features, including:
Where you could once only load assets from the local file system, you can now load them directly from the internet. Specifically you can specify a URL for a texture or 3D model, which will then be downloaded and created as a 3D object in your application automatically.
While Qt3D could do some exciting things it seemed clear that the limitations of a static set of 3D objects was far from ideal. As such we decided to support dynamic object creation. It is now possible to create and destroy arbitrary 3D items on-the-fly. This capability allows for a whole new set of use-cases to be fulfilled more easily than ever before.
Resource files have long been an easy and convenient way of packaging up application resource files such as images for ease of distribution. This excellent legacy led us to support proper loading of 3D assets from .qrc files. Using resource files you can now package up 3D models, textures, QML files… in fact pretty much anything Qt Quick 3D supports, and load it from your app.
So, having whet your appetites for more 3D goodness, here are the precompiled libraries and examples for Qt Quick3D:
I thought it’s time for a new Quick3D-HowTo-Video. It’s been some time since the last one was posted.
This time the video is more technical and less religious
I’ve nothing against Monkeyism, but let’s be honest, devoted penguins are one thing, though the real stuff is cars, cars, cars!
I stumbled across a few bugs and missing features, namely QTBUG-20729, QTBUG-20731, QTBUG-20460.
However, all of them could be easily worked around by slicing the 3D model into smaller portions.
The video shows exactly how to do this right!
Another issue: models aren’t always rendered as they should be.
For example colors are totally wrong, textures missing or parts of the car aren’t where they should be.
Admittedly, this is more about the world being a flawed place but anything else.
This could be observed in Blender as well as in Qt Quick 3D – and is always a problem when formats aren’t fully standardized.
Yet after finding and slicing a suitable model, the rest was definitely a smooth ride – and great fun to develop!
The Qt Blog provides you with one area for all Qt development posts from our Qt engineering experts. It includes information on projects in the works, tips and tricks, technical release information and more from our pool of very clever Qt developers.