ebook img

Developing an arcade game with Qt3D PDF

59 Pages·2013·0.87 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Developing an arcade game with Qt3D

Developing an arcade game with Qt3D Release 0.1 (default) DIgia, Qt Learning February 28, 2013 Contents 1 AboutthisGuide 1 1.1 Whyshouldyoureadthisguide? . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 Getthesourcecodeandtheguideindifferentformats . . . . . . . . . . . . . 2 1.3 License . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 2 HowweProceed 4 3 Overviewofthe“SpaceBurger”Game 5 3.1 Applicationidea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 3.2 Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 3.3 Gamemenu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 4 Qt3DBasics 6 4.1 Beforeyoutrythefirstexample . . . . . . . . . . . . . . . . . . . . . . . . . 7 5 Hello worldinQt3D 8 5.1 Loadingamodel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 5.2 Wheretoget3Dmodels? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 6 UsingaCamera 11 6.1 AnimatingtheCamera’sposition . . . . . . . . . . . . . . . . . . . . . . . . 11 7 Skybox 13 8 PlayerMovement 15 8.1 Update-timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 8.2 Keyinput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 8.3 Basicmotionequations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 8.4 Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 9 Movingtargets 22 9.1 OnionRings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 i 9.2 Collision-detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 9.3 DynamicObjectCreation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 10 States 28 11 GameMenu 31 11.1 Head-updisplay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 11.2 Gamemenu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 12 Bossenemy 40 12.1 Cameramovement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 12.2 Movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 12.3 Weaponfire . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 13 Shaders 49 13.1 Bulletshader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 13.2 Explosion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 14 Finalizingthegame 56 ii 1 CHAPTER About this Guide 1.1 Why should you read this guide? The Qt3D module is a set of APIs that use OpenGL and aim on making 3D development easier and more platform independent. It includes features like asset loading, shapes, texture managementandshaders. Qt3Dhasboth,aC++andaQtQuickAPI. The main aim of the C++ API is to make 3D programming platform independent. You don’t havetoworryanymorewhat3Darchitectureinuse. Itshouldbenomatterwhetheritisdesktop orembeddedOpenGLorwhetherafixedordynamicpipelineisused. TheQt3DQtQuickmoduleprovidesmoreabstractionandmakesitpossibletowritecomplete 3D applications in Qt Quick only. Using the Qt3D Qt Quick module, you can very easily mix 2D and 3D elements, implement head up displays overlaying a 3D scene, or even embed 3D elementsina2DQtQuickuserinterface. Qt3Dwasinitiallydevelopedasanadd-onprojectinQt4. Today,itisapartofQt5. Itisagood timetotakeacloserlookonthisinanhands-onexampleandgetreadytouseitinthefuture. The purpose of this guide is to give a brief overview of the Qt3D Qt Quick by walking the readerthroughthedevelopmentofanarcadegame: Thisguidewillshowhowthe2Dand3Dworldscanbecombinedinoneapplication. Addition- ally we will talk about basic lighting, material and texture topics. While developing the game, 1 Developing an arcade game with Qt3D, Release 0.1 (default) youwillgetanoverviewofmajorQtQuickelements1 providedbytheQt3Dmoduleandlearn howtousethem. ThisguideusesQMLonlywithoutanyC++code. AsolidQtQuickknowledgeisaprerequisite forreadingthisguide. Additionally,somebasicunderstandingofcomputergraphics,OpenGL2 in general, OpenGL shaders and GLSL (OpenGL Shading Language) is required. If you do not have sufficient knowledge in OpenGL, you should strongly consider reading the OpenGL tutorial3 first. Inthisguide,wewillfocuson3Dandgraphicaspects. Genericaspectsofapplicationdevelop- mentfordesktopandmobileareinfocusinotherguides. Ifyouareinterestedinthis,consider readingthemfirst4. After completion of this guide, you should have a good understanding of how the Qt3D Qt Quick API works as well as how to develop basic desktop and mobile applications with this technology. 1.2 Get the source code and the guide in different for- mats A.zipfilethatcontainsthesourcecodeofeachchapterisprovidedhere: Sourcecode5 Theguideisavailableinthefollowingformats: • PDF6 • ePub7 forebookreaders. • QtHelp8 forQtAssistantandQtCreator. 1.3 License Copyright(C)2012DigiaPlcand/oritssubsidiary(-ies). Allrightsreserved. This work, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-ShareAlike2.5. The full license document is available from http://creativecommons.org/licenses/by- sa/2.5/legalcode. 1http://qt-project.org/doc/qt-5.0/qt3d-qml3d.html 2http://www.opengl.org 3http://qt-project.org/wiki/Developer-Guides/index.html 4http://qt-project.org/wiki/Developer-Guides/ 5http://releases.qt-project.org/learning/developerguides/gamedevelopmentqt3d/completegame_src.zip 6http://releases.qt-project.org/learning/developerguides/gamedevelopmentqt3d/GameDevelopmentQt3D.pdf 7http://releases.qt-project.org/learning/developerguides/gamedevelopmentqt3d/GameDevelopmentQt3D.epub 8http://releases.qt-project.org/learning/developerguides/gamedevelopmentqt3d/GameDevelopmentQt3D.qch 1.2. Get the source code and the guide in different formats 2 Developing an arcade game with Qt3D, Release 0.1 (default) Qt and the Qt logo is a registered trade mark of Digia plc and/or its subsidiaries and is used pursuant to a license from Digia plc and/or its subsidiaries. All other trademarks are property oftheirrespectiveowners. What’s Next? Inthenextchapterwewillexplaintheapplicationconceptandlookatsmallfirstexamples. 1.3. License 3 2 CHAPTER How we Proceed In the following chapters, we will start with the development of our game. We will proceed step-by-step and take a deeper look on Qt3D Qt Qtuick API and related technologies. The sourcecodearchiveaccompanyingthisguidecontainsthefinalversionoftheapplication. Since this guide in on an advanced level, we will discuss relevant code samples directly taken from thisversionandomitmakingsmallapplicationsforeachtopic. First, we will cover basics of the Qt Quick Qt3D API. After this, we learn how to load models and textures. We will also cover dynamic model creation, making Head up Displays, states, a gamemenu,andtheusageofGLSLshaders. In the course of the guide, we will explain basics of Qt Quick Qt3D API. Nevertheless, you shouldreadthedetaileddescriptionprovidedintheQtdocumentation1. Aspreviouslymentioned,weuseonlyQMLinthedevelopmentinordertostayfocusedonQt Quick. This is sufficient for many basic use cases. Qt3D’s C++ API provides more features andabettercontrolovertheelementsimportedinQtQuick. Whendevelopingamorecomplex gameorareal3Dapplication,youwillsoonerorlaterneedtodefineyourownmodulesinC++ andexposingthemtoQtQuick. Thisisamoreadvancedtopicoutofscopeofthisguide. 1http://qt-project.org/doc/qt-5.0/qt3d-reference.html 4 3 CHAPTER Overview of the “SpaceBurger” Game 3.1 Application idea OurgamewillbecalledSpaceBurger. Playingthisgame,yourmissionistosteerahamburger through the darkness of the space and to try to hit as many onion rings as possible. Each onion ring brings additional scope. During the flight, it is possible increase maneuverability and firepower by hitting small power-ups. The player has to pass multiple levels including a special challenge at the end of every level: a fight against a boss* enemy. Once the boss has beendefeated,thenextlevelbegins. 3.2 Controls While having to hit several targets along the way, the hamburger is shown from behind. It can be controlled using the A (left), S (down), D (right) and W (up) keys. We will use basic movementequationstoachieverealisticflightbehavior. The fight against the boss enemy at the end of every level is shown from the top view. During thefight,thehamburgercanthenonlybemovedleftandright. Weaponscanbefiredusingthe spacekey. 3.3 Game menu Whenstartingtheapplication,agamemenuwillbedisplayed. Theplayercanstartanewgame andreviewtheachievedresultsinahighscoretable. 5 4 CHAPTER Qt3D Basics In order to starting using Qt3D API in Qt Quick you need to import it in your application with thestatement: import Qt3D 1.0 Afterthis,youcanloaditemsprovidedbytheQt3Dmodule. Mostimportantfromthemare: Viewport* The Viewport1 element specifies a viewport for the whole scene. It is the root element and defines the camera and (scene-)lights as well as rendering parameters. Itisusuallytheoutermostelementina3Dscene. Camera*TheCamera2 elementisassignedtothecamerapropertyoftheviewport. It defines a viewing position and direction as well as the projection. Furthermore stereoprojectionsaresupported. Item3D* The Item3D3 is used for creating visible 3D objects in a scene. It defines basic parameters and methods for manipulating an object. To create a visible ob- ject, a mesh has to be specified for the Item3D. Futhermore tree structures can be built out of Item3Ds which allows the creation of logical groups. Children of an Item3Dareplacedrelativelytotheirparentobjectinthe3Dscene. I.e. iftheparent objectismovedorrotated,allthechildrenwillalsoberotated. Mesh* The Mesh4 is used to load geometry in such a way that it can be used in Qt3D. File loading is done automatically after a filename is specified. The mesh element chooses the appropriate model loader from the file ending. The supported modelformatsaree.g. 3ds,dae,bezandobj. Effect*AnEffect5 definesaverybasicandsimplewayonhowanitemisrendered onthescreen. Withitsimplelighting,materialandtextureeffectscanbeachieved. ShaderProgram* The ShaderProgram6 element is derived from Effect and gives 1http://qt-project.org/doc/qt-5.0/qml-viewport.html 2http://qt-project.org/doc/qt-5.0/qml-camera.html 3http://qt-project.org/doc/qt-5.0/qml-item3d.html 4http://qt-project.org/doc/qt-5.0/qml-mesh.html 5http://qt-project.org/doc/qt-5.0/qml-effect.html 6http://qt-project.org/doc/qt-5.0/qml-shaderprogram.html 6 Developing an arcade game with Qt3D, Release 0.1 (default) theuserthemeansforcreatingcustomshaderprogramsinGLSL.Youcanspecify afragmentandavertexshader. ThetexturepropertyinheritedfromtheEffectele- mentwillbemapedtotheqt_Texture0intheshaderprogramcode. TheShaderPro- gramautomaticallybindscustompropertiestoyourfragmentandvertexshadersif they exist under the same name. If you want e.g. to specify more then one texture youmaydosobyaddingastringpropertywiththepathtoyourtexture. Material* Materials7 provide some information for an effect, like lighting proper- tiesandcolors. Transformations* There are currently four transformations available in Qt3D: Ro- tation3D8, Translation3D9, Scale3D10 and LookAtTransform11. All of these can be applied to an Item3D and rotate, translate, scale or change the orientation of an item. The order in which the transformations are specified are very important for the result. It makes a difference if an item is rotated first and then translated or the otherwayround. 4.1 Before you try the first example Qt3D is available for Qt 4.8 as an add-on. Qt5 is the first Qt version where Qt3D is available as an Essential Module12. In this guide, we will use Qt5 only. If required, the game can be ported to Qt4.8 with a minimal effort. If you already have installed Qt5, please make sure that OpenGL is supported. More information where to download Qt5 and how get it installed is availableontheQtProjecthomepage13. In-line examples and the final game are available as QML files. Please use the program qmlscenefromtheqtbase/bindirectorytotrythecode. What’s Next? Nextwewillbeusingtheelementsexplainedaboveinaverysimpleexample. 7http://qt-project.org/doc/qt-5.0/qml-material.html 8http://qt-project.org/doc/qt-5.0/qml-rotation3d.html 9http://qt-project.org/doc/qt-5.0/qml-translation3d.html 10http://qt-project.org/doc/qt-5.0/qml-scale3d.html 11http://qt-project.org/doc/qt-5.0/qml-lookattransform.html 12http://qt-project.org/wiki/Qt-Essentials-Modules 13http://qt-project.org/wiki/Qt_5.0 4.1. Before you try the first example 7

Description:
DIgia, Qt Learning . through the darkness of the space and to try to hit as many onion rings as possible http://sketchup.google.com/3dwarehouse/ : The Google 3D warehouse is probably .. specific setting for alpha blending.
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.