Qt 3D Basics · 2018-11-20 · Demo qt3d/ex-hellodonut-qml p.27 C++ API vs QML API Hello Donut QML...

16
p.1 Qt 3D Basics CERN HSF Visualization workshop, 28/03/2017 Presented by Mike Krus - [email protected] Material based on Qt 5.5, created on March 28, 2017

Transcript of Qt 3D Basics · 2018-11-20 · Demo qt3d/ex-hellodonut-qml p.27 C++ API vs QML API Hello Donut QML...

p.1

Qt3DBasicsCERNHSFVisualizationworkshop,28/03/2017

[email protected]

MaterialbasedonQt5.5,createdonMarch28,2017

p.2

IntroducingQtQuick

IntroducingQtQuick

IntroducingQtQuick

p.3

WhatisQtQuick?

IntroducingQtQuick

Asetoftechnologiesincluding:

Declarativemarkuplanguage:QML

LanguageruntimeintegratedwithQt

QtCreatorIDEsupportfortheQMLlanguage

Graphicaldesigntool

C++APIforintegrationwithQtapplications

p.4

PhilosophyofQtQuick

IntroducingQtQuick

Intuitiveuserinterfaces

Design-oriented

Rapidprototypingandproduction

Easydeployment

p.5

Qt3DBasics

Qt3DBasics

Qt3DBasics

p.6

Qt3DBasics

FeatureSet

FeatureSet

p.7

WhatisQt3D?

FeatureSet

Itisnotabout3D!

Multi-purpose,notjustagameengine

Softreal-timesimulationengine

Designedtobescalable

Extensibleandflexible

p.8

SimulationEngine

FeatureSet

Thecoreisnotinherentlyabout3D

ItcandealwithseveraldomainsatonceAI,logic,audio,etc.Andofcourseitcontainsa3Drenderertoo!

AllyouneedforacomplexsystemsimulationMechanicalsystemsPhysics...andalsogames

p.9

Scalability

FeatureSet

Frontend/backendsplitFrontendislightweightandonthemainthreadBackendexecutedinasecondarythread

Wheretheactualsimulationruns

Non-blockingfrontend/backendcommunication

Backendmaximizesthroughputviaathreadpool

p.10

ExtensibilityandFlexibility

FeatureSet

Domainscanbeaddedviaindependentaspects...onlyifthere'snotsomethingfittingyourneedsalready

ProvidebothC++andQMLAPIs

IntegrateswellwiththerestofQtPullingyoursimulationdatafromadatabaseanyone?

EntityComponentSystemisusedtocombinebehaviorinyourownobjectsNodeepinheritancehierarchy

p.11

Qt3DBasics

EntityComponentSystem?

EntityComponentSystem?

p.12

ECS:Definitions

EntityComponentSystem?

ECSisanarchitecturalpatternPopularingameenginesFavorscompositionoverinheritance

Anentityisageneralpurposeobject

Anentitygetsitsbehaviorbycombiningdata

Datacomesfromtypedcomponents

p.13

CompositionvsInheritance

EntityComponentSystem?

Let'sanalyseafamiliarexample:SpaceInvaders

p.14

CompositionvsInheritancecont'd

EntityComponentSystem?

Typicalinheritancehierarchy

p.15

CompositionvsInheritancecont'd

EntityComponentSystem?

Allfineuntilcustomerrequiresnewfeature:

p.16

CompositionvsInheritancecont'd

EntityComponentSystem?

Typicalsolution:Addfeaturetobaseclass

p.17

CompositionvsInheritancecont'd

EntityComponentSystem?

Doesn'tscale:

p.18

CompositionvsInheritancecont'd

EntityComponentSystem?

Whataboutmultipleinheritance?

p.19

CompositionvsInheritancecont'd

EntityComponentSystem?

Whataboutmix-inmultipleinheritance?

p.20

CompositionvsInheritancecont'd

EntityComponentSystem?

Doesitscale?

p.21

CompositionvsInheritancecont'd

EntityComponentSystem?

Isinheritanceflexibleenough?

p.22

CompositionvsInheritancecont'd

EntityComponentSystem?

Istraditionalfixedcompositionthepanacea?

p.23

EntityComponentSystem

EntityComponentSystem?

TheEntity/ComponentdatasplitgivesflexibilitytomanagetheAPI

TheSystemseparationmovesthebehaviorawayfromdataavoidingdependenciesbetweenComponents

p.24

EntityComponentSystemWrap-up

EntityComponentSystem?

Inheritance:RelationshipsbakedinatdesigntimeComplexinheritancehierarchies:deep,wide,multipleinheritanceFeaturestendtomigratetobaseclass

FixedCompositionRelationshipsstillbakedinatdesigntimeFixedmaximumfeaturescopeLotsoffunctionaldomaindetailsinthesceneobjectIffunctionaldomainobjectscontainbothdataandbehaviortheywillhavelotsofinter-dependencies

EntityComponentSystemAllowschangesatruntimeAvoidsinheritancelimitationsHasadditionalcosts:

MoreQObjectsDifferenttomostOOPdeveloper'sexperience

Wedon'thavetobakeinassumptionstoQt3Dthatwecan'tlaterchangewhenaddingfeatures.

p.25

Qt3DBasics

HelloDonut

HelloDonut

p.26

HelloDonut(QML)

HelloDonut

GoodpracticehavingrootEntitytorepresentthescene

OneEntityper"object"inthescene

Objectsgivenbehaviorbyattachingcomponentsubclasses

ForanEntitytobedrawnitneeds:

AmeshgeometrydescribingitsshapeAmaterialdescribingitssurfaceappearance

Demoqt3d/ex-hellodonut-qml

p.27

C++APIvsQMLAPI

HelloDonut

QMLAPIisamirroroftheC++API

C++classnamesliketherestofQt

QMLelementnamesjustdon'thavetheQinfrontQt3DCore::QNodevsNodeQt3DCore::QEntityvsEntity...

p.28

Qt3DBasics

Qt3DECSExplained

Qt3DECSExplained

p.29

EverythingisaQNode

Qt3DECSExplained

Qt3DCore::QNodeisthebasetypeforeverythingItinheritsfromQObjectandallitsfeaturesInternallyimplementsthefrontend/backendcommunication

Qt3DCore::QEntityItinheritsfromQt3DCore::QNodeItjustaggregatesQt3DCore::QComponents

Qt3DCore::QComponentItinheritsfromQt3DCore::QNodeActualdataisprovidedbyitssubclasses

Qt3DCore::QTransformQt3DRender::QMeshQt3DRender::QMaterial...

p.30

EverythingisaQNodecont'd

Qt3DECSExplained p.31

YouStillNeedaSystem

Qt3DECSExplained

ThesimulationisexecutedbyQt3DCore::QAspectEngine

Qt3DCore::QAbstractAspectsubclassinstancesareregisteredontheengine

BehaviorcomesfromtheaspectsprocessingcomponentdataAspectscontrolthedomainsmanipulatedbyyoursimulation

Qt3DprovidesQt3DRender::QRenderAspectQt3DInput::QInputAspectQt3DLogic::QLogicAspect

NotethataspectshavenoAPIoftheirownItisallprovidedbyQt3DCore::QComponentsubclasses

p.32

Qt3DBasics

TheSceneGraph

TheSceneGraph

p.33

TheSceneGraph

TheSceneGraph

ThescenegraphprovidesthespatialrepresentationofthesimulationQt3DCore::QEntity:whattakespartinthesimulationQt3DCore::QTransform:whereitis,whatscaleitis,whatorientationithas

Hierarchicaltransformsarecontrolledbytheparent/childrelationshipSimilartoQWidget,QQuickItem,etc.

Ifthesceneisrendered,weneedapointofviewonitThisisprovidedbyQt3DRender::QCamera

p.34

Qt3DCore::QTransform

TheSceneGraph

InheritsfromQt3DCore::QComponent

Representsanaffinetransformation

Threewaysofusingit:Throughproperties:scale3D,rotation,translationThroughhelperfunctions:rotateAround()Throughthematrixproperty

Transformationsareapplied:toobjectsinScale/Rotation/TranslationordertocoordinatesystemsinTranslation/Rotation/Scaleorder

Transformationsaremultipliedalongtheparent/childrelationship

p.35

Transformscont'd

TheSceneGraph

1 importQt3D.Core2.023 Entity{4    components:[5        Transform{6            scale3D:Qt.vector3d(1,2,1.5)7            translation:Qt.vector3d(0,0,-1)8        }9    ]

1011    Entity{12        components:[13            Transform{translation:Qt.vector3d(0,1,0)}14        ]15    }16 }

p.36

Geometries

TheSceneGraph

Qt3DRender::QRenderAspectdrawsQt3DCore::QEntityswithashape

Qt3DRender::QGeometryRenderer'sgeometrypropertyspecifiestheshape

Qt3DprovidesconveniencesubclassesofQt3DRender::QGeometryRenderer:

Qt3DExtras::QSphereMeshQt3DExtras::QCuboidMeshQt3DExtras::QPlaneMeshQt3DExtras::QTorusMeshQt3DExtras::QConeMeshQt3DExtras::QCylinderMesh

QtDemoexamples/qt3d/basicshapes-cpp

p.37

MakingyourOwnGeometries

TheSceneGraph

UsingQt3DRender::QBufferwecancreateourownvertices

GeometryRenderercontrolshowbuffersarecombinedandparsed

Usefultomakeyouowngeometriesprogrammatically:FromafunctionFromdatasetsFromuserinteraction

Demoqt3d/ex-surface-function

p.38

Materials

TheSceneGraph

IfaQt3DCore::QEntityonlyhasashapeitwillappearblack

TheQt3DRender::QMaterialcomponentprovidesasurfaceappearance

Qt3DprovidesconveniencesubclassesofQt3DRender::QMaterial:

Qt3DExtras::QPhongMaterialQt3DExtras::QPhongAlphaMaterialQt3DExtras::QDiffuseMapMaterialQt3DExtras::QDiffuseSpecularMapMaterialQt3DExtras::QGoochMaterial...

Demoqt3d/sol-textured-scene

p.39

Lights

TheSceneGraph

Evenwithshapesandmaterialswewouldseenothing

Weneedsomelights...luckilyQt3Dsetsadefaultoneforusifnoneisprovided

Ingeneralwewantsomecontrolofthescenelighting

Qt3Dprovidesthefollowinglighttypes:DirectionalLightPointLightSpotLight

Labqt3d/ex-lights-qml

p.40

CustomMaterialexample

TheSceneGraph

1 importQt3D.Render2.02 ...34 Material{5     parameters:Parameter{name:"colorTint";value:"yellow"}6     effect:Effect{7         techniques:[8             Technique{9                 filterKeys:FilterKey{name:"renderingStyle";value:"forward"}

1011                 graphicsApiFilter{12                     api:GraphicsApiFilter.OpenGL13                     majorVersion:314                     minorVersion:215                     profile:GraphicsApiFilter.CoreProfile16                 }1718                 renderPasses:RenderPass{19                     shaderProgram:ShaderProgram{20                         vertexShaderCode:loadSource("qrc:/customshader.vert")21                         fragmentShaderCode:loadSource("qrc:/customshader.frag")22                     }23                 }24             }25         ]26     }27 }

Demoqt3d/ex-glslp.41

TextureCompositionandFiltering

TheSceneGraph

Possibletosampleseveraltexturesinasinglematerial

Alsoeasytoreusestocklightingmodel

Thenyoucanblendasyouseefitintheshader

Demoqt3d/sol-earth

p.42

Qt3DBasics

Interactingwiththescene

Interactingwiththescene

p.43

Picking

Interactingwiththescene

HighlevelpickingprovidedbyQt3DRender::QObjectPickercomponentImplicitlyassociatedwithmousedeviceUsesray-castbasedpicking

Qt3DRender::QObjectPickeremitssignalsforyoutohandle:pressed(pick), released(pick), clicked(pick)moved(pick)-onlywhendragEnabledistrueentered(), exited()-onlywhenhoverEnabledistrue

ThecontainsMousepropertyprovidesamoredeclarativealternativetoentered(), exited()

ThepickparameterofthesignalsisaQt3DRender::QPickEventpositioninscreenspacelocalIntersectioninmodelspaceworldIntersectioninworldspace

Demoqt3d/ex-object-picker

Demoqt3d/ex-object-picker-qml

p.44

PhysicalDevices

Interactingwiththescene

Tohandleinputwefirstneedtogenerateinputevents

SubclassesofQt3DInput::QAbstractPhysicalDevicerepresentinputdevices

Qt3DInput::QKeyboardDeviceQt3DInput::QMouseDeviceOtherscanbeaddedlater

Onit'sownadevicedoesn'tdomuchInputhandlersexposesignalsemittedinresponsetoevents

p.45

PhysicalDevicesvsLogicalDevices

Interactingwiththescene

Physicaldevicesprovideonlydiscreteevents

Hardtousethemtocontrolavalueovertime

Logicaldeviceprovidesawayto:HaveananalogviewonaphysicaldeviceAggregateseveralphysicaldevicesinaunifieddevice

Combinedwithinputhandler

Demoqt3d/ex-mouse-handler-qml

p.46

PuttingitAllTogether:MovingBoxes

Interactingwiththescene

Focusmanagedusingtab

Focusedboxappearsbigger

Thearrowsmovetheboxontheplane

Pageup/downrotatetheboxonitsYaxis

Boxeslightupwhenonmousehover

Clickingonaboxgivesitthefocus

Boxescanbemovedaroundwiththemouse

Demoqt3d/sol-moving-boxes-qml-step3

p.47

IntegratingwithQtQuickusingScene3D

Interactingwiththescene

ProvidedbytheQtQuick.Scene3Dmodule

TakesanEntityaschildwhichwillbeyourwholescene

Loadedaspectsarecontrolledwiththeaspectsproperty

HovereventsareonlyacceptedifthehoverEnabledpropertyistrue

Demoqt3d/ex-controls-overlay

In5.9,Scene2D,fullyinteractiveQtQuickUImappedonto3Dgeometry

p.48

Qt3DBasics

TheQt3DFrameGraph

TheQt3DFrameGraph

p.49

WhatistheFrameGraphFor?

TheQt3DFrameGraph

Withwhatwehaveseensofar,wecan:DrawgeometryloadedfromdiskorgenerateddynamicallyUsecustommaterialswithshaderstochangesurfaceappearanceMakeuseoftexturestoincreasesurfacedetails

Whataboutshadows?

Whatabouttransparency?

Whataboutpostprocessingeffects?

AlltheseandothersrequirecontroloverhowwerendertheSceneGraph

TheFrameGraphdescribestherenderingalgorithm

p.50

StructureandBehavior

TheQt3DFrameGraph

ThenodesoftheFrameGraphformatree

TheentitiesoftheSceneGraphformatree

TheFrameGraphandSceneGrapharelinearizedintorendercommands

p.51

TheSimplestFrameGraph

TheQt3DFrameGraph

ItisimportanttostructureyourFrameGraphproperlyforperformancereasons

MightleadtodeepandnarrowtreesSimplestcasebeingaonepassforwardrenderer

p.52

SeveralPointsofViewonaScene

TheQt3DFrameGraph

Cameradescribesapointofviewonascene

Viewportallowstosplittherendersurfaceinseveralareas

Theycanbenestedforfurthersplitting

CameraSelectorallowstoselectacameratorenderinaViewport

ClearBuffersdecribeswhichbuffersareclearedduringtherendering

GenerallynecessarytogetanythingonscreenAlsoaneasywaytocontrolbackgroundcolor

ToavoidabranchtotriggerarenderinggiveitaNoDrawelementasleaf

Demoqt3d/ex-viewports

p.53

ShowingDifferentScenesinViewports

TheQt3DFrameGraph

OurViewportsalldisplaythesamescene...

Buttheycandisplaydifferentsubsetsofthesceneusinglayers

AttacheachentitytoaLayer

HaveeachViewportdisplayasubsetoftheentitiesusingLayerFilter

Demoqt3d/ex-viewports-and-layers

p.54

Image-BaseTechniques

TheQt3DFrameGraph

Renderingtoatexture,supportforframebuffers

Multiplepasses

Post-ProcessingEffects

Demoqt3d/ex-gaussian-blur

Demoqt3d/ex-multiple-effects

p.55

Qt3DBasics

BeyondtheTipoftheIceberg

BeyondtheTipoftheIceberg

p.56

Andmore...

BeyondtheTipoftheIceberg

Texturemipmaps

CubeMaps

PortabilityofyourcodeaccrossseveralOpenGLversions

Completecontrolovertherenderingalgorithm

Loadingcompleteobjectsorscenesfromfiles(3ds,collada,qml...)

Post-processingeffects(singleormulti-pass)

Instancedrendering

etc.

Demoqt3d/sol-asteroids

Demoqt3d/ex-instanced-geometry

p.57

Qt3DBasics

TheFutureofQt3D

TheFutureofQt3D

p.58

WhatdoesthefutureholdforQt3D?

TheFutureofQt3D

Qt3DCoreEfficiencyimprovemmentsBackendthreadpoolandjobhandlingimprovements-jobsspawningjobs

Qt3DRenderUseQtQuickorQPaintertorenderintoatextureEmbedQtQuickintoQt3Dincludinginputhandling(5.9)LevelofDetail(LOD)supportformeshes(5.9)Billboards-camerafacingentitiesTextsupport-2Dand3D(5.9)AdditionalmaterialssuchasPhysicsBasedRendering(PBR)materialsParticlesystems

Qt3DInputAxisinputsthatapplycumulativeaxisvaluesasposition,velocityoraccelerationAdditionalinputdevicesupport

3Dmousecontrollers,gamecontrollersEnumeratedinputssuchas8-waybuttons,hatswitchesordials

p.59

WhatdoesthefutureholdforQt3D?

TheFutureofQt3D

Newaspects:CollisionDetectionAspect

Allowstodetectwhenentitiescollideorenter/exitvolumesinspaceAnimationAspect

Keyframeanimation(5.9TP)SkeletalanimationMorphtargetanimationRemovesanimationworkloadfrommainthread

PhysicsAspectRigidbodyandsoftbodyphysicssimulation

AIAspect,3DPositionalAudioAspect...

Tooling:Designtimetooling-sceneeditorQt3DStudioBuildtimetooling-assetconditionersformeshes,texturesetc.

p.60

WhatdoesthefutureholdforQt3D?

TheFutureofQt3D

Qt3DandtherestofQt:DataVis,MappingarelikelytobebasedonQt3DWorkonunifyingrenderingtoolset

Singlerenderer,Vulkan,D12,Metalbackends