Introduction-to-Qt3D: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
(Replace outdated instructions with a link to current official docs)
 
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
=Introduction to Qt 3D=
Please see the official documentation at http://doc.qt.io/qt-5/qt3d-index.html
 
''<span class="caps">NOTE</span>: '''Qt 3D''' (along with '''Qt Quick 3D''' project) is undergoing '''heavy development'''. They are '''not yet part of Qt 5''', and the <span class="caps">API</span> shown in this article may be subject to changes.''
 
''<span class="caps">WARNING</span>: The contents of this article were last updated in 2012. This article is heavily outdated.''
 
'''Qt 3D''' is a set of C++ <span class="caps">API</span>s for 3D programming built on top of '''Qt OpenGL'''. The newest project is called '''Qt Quick 3D''' which creates <span class="caps">QML</span> bindings to '''Qt 3D'''.
 
First, we’ll make a comparison of the previous '''OpenGL''' <span class="caps">API</span>s: '''<span class="caps">GLUT</span>''' and '''Qt OpenGL'''.
 
=='''<span class="caps">GLUT</span>''' (OpenGL Utility Toolkit)==
 
'''<span class="caps">GLUT</span>''' was one the first (if not the first) toolkit to provide a portable <span class="caps">API</span> to handle '''window management''' for '''OpenGL''' programs. It uses callbacks to register drawing functions and more. There were also routines provided for drawing geometric primitives (both in solid and wireframe state) such as cubes and spheres.
 
A simple '''OpenGL''' program with just a ''3D cube and some lighting'' could be as long as '''140 lines''' of code when developed with '''<span class="caps">GLUT</span>'''.
 
[[Image:3d-cube-screenshot.png|3D Hello World Cube]]<br />''3D cube drawn using OpenGL.''
 
The code above follows a very basic '''OpenGL''' program structure. It has a '''''initialize''''' function (for doing some initial setup), a '''''resize''''' function (to handle window resizes) and a '''''paint''''' function (to draw the actual 3D objects).
 
=='''Qt OpenGL'''==
 
'''Qt OpenGL''' is a port of the '''OpenGL''' <span class="caps">API</span> to the '''Qt''' toolkit. It does a fairly good job at translating all of '''OpenGL''' basic functions<br /> to a '''Qt''' widget. Now, instead of several functions being registered for callbacks, it is possible to take advantage of the <span class="caps">SIGNAL</span>/SLOT system of '''Qt'''.
 
One of the most commonly used approches for '''Qt OpenGL''' to write a 3D program is to subclass '''<span class="caps">QGLW</span>idget'''. '''<span class="caps">QGLW</span>idget''' provides three convenience methods that you can reimplement to perform the typical OpenGL tasks: '''paintGL''', '''resizeGL''' and '''initializeGL''' (similar to the '''<span class="caps">GLUT</span>''' version of the program).
 
The program below follows the same basic structure of its '''<span class="caps">GLUT</span>''' previous version.
 
This version is still about '''140 lines''' of code. Except that now we have the convenience of using it within a '''Qt''' class. That means, the drawing of the 3D objects can still be done with '''OpenGL''' standard functions, while leaving the window management and input handling part to the '''Qt''' <span class="caps">API</span>.
 
==Using '''Qt 3D'''==
 
'''Qt 3D''' was created to simplify the usage of the '''OpenGL''' standard <span class="caps">API</span> within a '''Qt''' application. It abstracts most of the setup previously required. Camera position, viewing volume, vertices definition and other initial settings become '''much simpler'''. Therefore, reduces the overall code needed for creating a basic 3D program.
 
Similar to '''<span class="caps">QGLW</span>idget''', in '''Qt 3D''' there’s a class named '''<span class="caps">QGLV</span>iew''' which does most of the work regarding this initial settings. All we need to do is subclass it.
 
The code now looks easier to understand than its previous versions. There was no need for setting the cube’s vertices, no aspect ratio calculation, no lighting normals set. The real work is done by the '''<span class="caps">QGLB</span>uilder''' class and '''<span class="caps">QGLP</span>ainter''' class. The first one ('''<span class="caps">QGLB</span>uilder''') creates the geometry of the cube and adds it to the scene, while the second ('''<span class="caps">QGLP</span>ainter''') does the actual drawing of the cube.
 
The basic structure of the program is still there with the '''paintGL''' and '''initializeGL''' methods. There are also three more classes that handle typical '''OpenGL''' tasks: '''<span class="caps">QGLC</span>amera''', '''QGLLightParameters''' and '''<span class="caps">QGLM</span>aterial'''.
 
==References==
 
The source code used in this article was based on examples from the following references:<br />[http://www.glprogramming.com/red/ OpenGL Programming Guide] ''[glprogramming.com]''<br />[http://www.opengl.org/resources/libraries/glut/ <span class="caps">GLUT</span>] ''[opengl.org]''<br />[http://doc.qt.io/qt-5/qtopengl-index.html Qt OpenGL] ''[qt.io]''<br />[http://doc.qt.nokia.com/qt-quick3d-snapshot/ Qt 3D] ''[doc.qt.nokia.com]''
 
'''Qt 3D''' latest snapshot can be obtained here: [http://doc.qt.nokia.com/qt-quick3d-snapshot/ Qt Quick 3D] ''[doc.qt.nokia.com]''<br /> Branch '''qt4''' at the commit '''d5ee2''' was used for the example on this article.
 
===Categories:===
 
* [[:Category:Developing with Qt|Developing_with_Qt]]
** [[:Category:Developing with Qt::Qt3D|Qt3D]]

Latest revision as of 08:56, 10 July 2016

Please see the official documentation at http://doc.qt.io/qt-5/qt3d-index.html