| 1 |
// Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only |
| 3 |
|
| 4 |
/*! |
| 5 |
\example shadow-map-qml |
| 6 |
\title Qt 3D: Shadow Map QML Example |
| 7 |
\ingroup qt3d-examples-qml |
| 8 |
|
| 9 |
\brief A Qt 3D QML application that illustrates how to render a scene in Qt 3D |
| 10 |
with shadows. |
| 11 |
|
| 12 |
\image shadowmapping-qt3d.png {A 3D rendered scene showing a metallic knot |
| 13 |
hovering above a green plane and an orange plane flying in the |
| 14 |
background} |
| 15 |
|
| 16 |
\e {Qt 3D Shadow Map} illustrates how to configure the renderer in order to |
| 17 |
accommodate custom rendering techniques. The example application displays a |
| 18 |
self-shadowed plane and trefoil knot. |
| 19 |
|
| 20 |
We implement \l{Shadow Mapping}{shadow mapping} using a two pass rendering. |
| 21 |
In the first pass, we generate the shadow information. In the second pass, |
| 22 |
we generate the scene using the forward rendering technique with Phong |
| 23 |
shading, while at the same time using the information gathered in the first |
| 24 |
pass to draw the shadows. |
| 25 |
|
| 26 |
The entire rendering is configured using QML, but it is possible to use C++ |
| 27 |
to achieve the very same result. |
| 28 |
|
| 29 |
\include examples-run.qdocinc |
| 30 |
|
| 31 |
\section1 Setting Up the Scene |
| 32 |
|
| 33 |
We set up the entire scene in the \e main.qml file. |
| 34 |
|
| 35 |
To be able to use the types in the Q3D and Q3D Renderer modules, we must |
| 36 |
import the modules: |
| 37 |
|
| 38 |
\quotefromfile shadow-map-qml/main.qml |
| 39 |
\skipto import Qt3D.Core |
| 40 |
\printuntil Render 2.0 |
| 41 |
|
| 42 |
The first entities we create are a \l Camera, which represents the camera |
| 43 |
used for the final rendering, and a \l Configuration, which allows us to |
| 44 |
control this camera using the keyboard or the mouse: |
| 45 |
|
| 46 |
\printuntil } |
| 47 |
\printuntil } |
| 48 |
|
| 49 |
We then create a Light custom entity, which represents our light. It is a |
| 50 |
directional spotlight, placed somewhere above the plane and looking down at |
| 51 |
the scene’s origin: |
| 52 |
|
| 53 |
\printuntil } |
| 54 |
|
| 55 |
This light entity is used by our custom frame graph, ShadowMapFrameGraph, |
| 56 |
and our rendering effect, AdsEffect, whose instances are created just after |
| 57 |
the light: |
| 58 |
|
| 59 |
\printuntil ] |
| 60 |
\printuntil } |
| 61 |
|
| 62 |
Last, we create three entities for the meshes in the scene: a trefoil knot, |
| 63 |
a toy plane, and a ground plane. They aggregate a mesh, a transformation, |
| 64 |
and a material that uses the AdsEffect. The toy plane and the trefoil knot |
| 65 |
transformations are animated: |
| 66 |
|
| 67 |
\printuntil /^\}/ |
| 68 |
|
| 69 |
\section1 Specifying the Light |
| 70 |
|
| 71 |
We specify the Light custom entity in \e ShadowMapLight.qml. |
| 72 |
|
| 73 |
Again, we import the necessary modules: |
| 74 |
|
| 75 |
\quotefromfile shadow-map-qml/ShadowMapLight.qml |
| 76 |
\skipto import Qt3D |
| 77 |
\printuntil Qt3D.Render |
| 78 |
|
| 79 |
We then use an \l Entity type as the root element of the custom QML type. |
| 80 |
The light is a directional spotlight that exposes as properties a position, |
| 81 |
intensity, and a 4×4 transformation matrix: |
| 82 |
|
| 83 |
\printuntil matrix4x4 |
| 84 |
|
| 85 |
In the first rendering pass, we use the light as a camera, and therefore we |
| 86 |
use a \l Camera entity within the light and expose it as a property: |
| 87 |
|
| 88 |
\printuntil /^\}/ |
| 89 |
|
| 90 |
\section1 Configuring the Framegraph |
| 91 |
|
| 92 |
In Qt 3D, the frame graph is the data-driven configuration for the rendering. |
| 93 |
We implement the frame graph in the \e ShadowMapFrameGraph.qml file. |
| 94 |
|
| 95 |
In addition to the Qt 3D and Qt 3D Render modules, we also import the |
| 96 |
Qt Quick module: |
| 97 |
|
| 98 |
\quotefromfile shadow-map-qml/ShadowMapFrameGraph.qml |
| 99 |
\skipto import QtQuick |
| 100 |
\printuntil Render 2.0 |
| 101 |
|
| 102 |
The code defines a \l RenderSettings node that has a tree of nodes as the |
| 103 |
active frame graph: |
| 104 |
|
| 105 |
\badcode |
| 106 |
RenderSettings { |
| 107 |
activeFrameGraph: Viewport {...} |
| 108 |
} |
| 109 |
\endcode |
| 110 |
|
| 111 |
Any path from the leaves of this tree to the root is a viable frame graph |
| 112 |
configuration. Filter entities can enable or disable such paths, and |
| 113 |
selector entities can alter the configuration. |
| 114 |
|
| 115 |
In our case, the tree looks like this: |
| 116 |
|
| 117 |
\badcode |
| 118 |
Viewport |
| 119 |
RenderSurfaceSelector |
| 120 |
RenderPassFilter |
| 121 |
RenderTargetSelector |
| 122 |
ClearBuffers |
| 123 |
CameraSelector |
| 124 |
RenderPassFilter |
| 125 |
ClearBuffers |
| 126 |
CameraSelector |
| 127 |
\endcode |
| 128 |
|
| 129 |
So we have two paths from the topmost \l Viewport entity. Each path |
| 130 |
corresponds to a pass, or phase, of the shadow map technique. The paths are |
| 131 |
enabled and disabled using a RenderPassFilter, a node that can filter |
| 132 |
depending on arbitrary values defined in a given render pass. In this |
| 133 |
example, it is a string: |
| 134 |
|
| 135 |
\skipto RenderPassFilter |
| 136 |
\printuntil ] |
| 137 |
|
| 138 |
The actual passes are not defined within the frame graph. Instead the |
| 139 |
available passes are declared in the Materials used in the scene graph. The |
| 140 |
frame graph is only used to select which passes are used when rendering. |
| 141 |
|
| 142 |
\section1 Generating the Shadow Map |
| 143 |
|
| 144 |
In the shadow map generation pass, we must render to an offscreen surface |
| 145 |
(Framebuffer Object) which has a depth texture attachment. In Qt 3D, it is |
| 146 |
represented by the RenderTarget entity, which has a number of attachments. |
| 147 |
|
| 148 |
In this example, we need only a depth attachment. We define it as a |
| 149 |
RenderAttachment entity using the RenderAttachment.DepthAttachment \c type |
| 150 |
that stores the depth and a Texture2D entity that actually configures the |
| 151 |
exture storage used to store the depth information: |
| 152 |
|
| 153 |
\printuntil ] |
| 154 |
\printuntil } |
| 155 |
|
| 156 |
Moreover, in this first pass, we must render using the light’s camera. |
| 157 |
Therefore, we have a CameraSelector entity that sets the camera to the one |
| 158 |
exported by the Light: |
| 159 |
|
| 160 |
\skipto CameraSelector |
| 161 |
\printuntil } |
| 162 |
|
| 163 |
The second pass is more straightforward, because we simply render to the |
| 164 |
screen using the main camera: |
| 165 |
|
| 166 |
\skipto RenderPassFilter |
| 167 |
\printuntil } |
| 168 |
\printuntil } |
| 169 |
\printuntil } |
| 170 |
\printuntil } |
| 171 |
|
| 172 |
\section1 Using Effects |
| 173 |
|
| 174 |
The bulk of the magic happens in the \e AdsEffect.qml file, where our main |
| 175 |
\l Effect is defined. It implements the Ambient, Diffuse and Specular |
| 176 |
(ADS) Lighting Model using Phong shading with the addition of shadow mapping. |
| 177 |
|
| 178 |
An effect contains the implementation of a particular rendering strategy. In |
| 179 |
this example, shadow mapping using two passes: |
| 180 |
|
| 181 |
\quotefromfile shadow-map-qml/AdsEffect.qml |
| 182 |
\skipto Effect |
| 183 |
\printuntil Light |
| 184 |
|
| 185 |
The \c parameters list defines some default values for the effect. The |
| 186 |
values will get mapped to shader program uniform variables, so that in the |
| 187 |
shaders we can access their values. In this example, we expose some information from |
| 188 |
the Light entity (position, intensity, view or projection matrix defined by |
| 189 |
the internal camera) and the shadow map texture exposed by the frame graph: |
| 190 |
|
| 191 |
\skipto parameters: |
| 192 |
\printuntil ] |
| 193 |
|
| 194 |
It is possible to put such parameters all the way down, from a \l Material, |
| 195 |
to its \l Effect, to one of the effect’s \l {Technique}{Techniques} and a |
| 196 |
\l RenderPass within a \l Technique. This allows a \l Material instance to |
| 197 |
override defaults in an \l Effect, \l Technique or \l RenderPass. |
| 198 |
|
| 199 |
To adapt the implementation to different hardware or OpenGL versions, we |
| 200 |
could use one or more \l Technique elements. In this example, only one |
| 201 |
technique is provided, targeting OpenGL 3.2 Core, or later: |
| 202 |
|
| 203 |
\quotefromfile shadow-map-qml/AdsEffect.qml |
| 204 |
\skipto techniques: |
| 205 |
\printuntil } |
| 206 |
|
| 207 |
Inside the technique, we finally have the definition of our two rendering |
| 208 |
passes. We \e tag each pass with a \l FilterKey object, matching the ones |
| 209 |
we specified in the frame graph configuration, so that each pass will have |
| 210 |
different rendering settings: |
| 211 |
|
| 212 |
\printuntil ] |
| 213 |
|
| 214 |
The first pass is the shadow map generation. We load a suitable set of GLSL |
| 215 |
shaders, which are actually extremely simple. They do only MVP (Model, View, |
| 216 |
Projection) to bring meshes from their model space into clip space (and, |
| 217 |
remember, in this first pass, the light is the camera). The fragment shader |
| 218 |
is totally empty, because there is no color to be generated, and the depth |
| 219 |
will be automatically captured for us by OpenGL: |
| 220 |
|
| 221 |
\printuntil } |
| 222 |
|
| 223 |
In this first pass, we also set some custom OpenGL state in the form of a |
| 224 |
polygon offset and depth testing mode: |
| 225 |
|
| 226 |
\printuntil ] |
| 227 |
|
| 228 |
\section1 Rendering Using Phong Shading |
| 229 |
|
| 230 |
The second pass is a normal forward rendering using Phong shading. The code |
| 231 |
in the effect entity is extremely simple. We simply configure some |
| 232 |
parameters and load a pair of shaders which will be used when drawing. |
| 233 |
|
| 234 |
The first part of the shadow mapping happens in the vertex shader defined in |
| 235 |
\e ads.vert file, where we output towards the fragment shader the |
| 236 |
coordinates of each vertex in light space: |
| 237 |
|
| 238 |
\quotefromfile shadow-map-qml/shaders/ads.vert |
| 239 |
\skipto mat4( |
| 240 |
\skipto positionInLightSpace |
| 241 |
\printuntil ; |
| 242 |
|
| 243 |
Actually, the coordinates get adjusted a little to allow us to easily sample |
| 244 |
the shadow map texture. |
| 245 |
|
| 246 |
The second part happens in the fragment shader defined in the \e ads.frag |
| 247 |
file, where we sample the shadow map. If the currently processed fragment is |
| 248 |
behind the one closest to the light, then the current fragment is in shadow |
| 249 |
(and only gets ambient contribution). Otherwise, it gets full Phong shading: |
| 250 |
|
| 251 |
\quotefromfile shadow-map-qml/shaders/ads.frag |
| 252 |
\skipto main |
| 253 |
\printuntil } |
| 254 |
*/ |