Qt 3D: Shadow Map QML Example

A Qt 3D QML application that illustrates how to render a scene in Qt 3D with shadows.

../_images/shadowmapping-qt3d.png

Qt 3D Shadow Map illustrates how to configure the renderer in order to accommodate custom rendering techniques. The example application displays a self-shadowed plane and trefoil knot.

We implement shadow mapping using a two pass rendering. In the first pass, we generate the shadow information. In the second pass, we generate the scene using the forward rendering technique with Phong shading, while at the same time using the information gathered in the first pass to draw the shadows.

The entire rendering is configured using QML, but it is possible to use C++ to achieve the very same result.

Running the Example

To run the example from Qt Creator , open the Welcome mode and select the example from Examples . For more information, visit Building and Running an Example.

Setting Up the Scene

We set up the entire scene in the main.qml file.

To be able to use the types in the Q3D and Q3D Renderer modules, we must import the modules:

The first entities we create are a Camera , which represents the camera used for the final rendering, and a Configuration , which allows us to control this camera using the keyboard or the mouse:

We then create a Light custom entity, which represents our light. It is a directional spotlight, placed somewhere above the plane and looking down at the scene’s origin:

This light entity is used by our custom frame graph, ShadowMapFrameGraph, and our rendering effect, AdsEffect, whose instances are created just after the light:

Last, we create three entities for the meshes in the scene: a trefoil knot, a toy plane, and a ground plane. They aggregate a mesh, a transformation, and a material that uses the AdsEffect. The toy plane and the trefoil knot transformations are animated:

Specifying the Light

We specify the Light custom entity in ShadowMapLight.qml .

Again, we import the necessary modules:

We then use an Entity type as the root element of the custom QML type. The light is a directional spotlight that exposes as properties a position, intensity, and a 4×4 transformation matrix:

In the first rendering pass, we use the light as a camera, and therefore we use a Camera entity within the light and expose it as a property:

Configuring the Framegraph

In Qt 3D, the frame graph is the data-driven configuration for the rendering. We implement the frame graph in the ShadowMapFrameGraph.qml file.

In addition to the Qt 3D and Qt 3D Render modules, we also import the Qt Quick module:

The code defines a RenderSettings node that has a tree of nodes as the active frame graph:

RenderSettings {
    activeFrameGraph: Viewport {...}
}

Any path from the leaves of this tree to the root is a viable frame graph configuration. Filter entities can enable or disable such paths, and selector entities can alter the configuration.

In our case, the tree looks like this:

Viewport
    RenderSurfaceSelector
        RenderPassFilter
            RenderTargetSelector
                ClearBuffers
                    CameraSelector
        RenderPassFilter
            ClearBuffers
                CameraSelector

So we have two paths from the topmost Viewport entity. Each path corresponds to a pass, or phase, of the shadow map technique. The paths are enabled and disabled using a RenderPassFilter , a node that can filter depending on arbitrary values defined in a given render pass. In this example, it is a string:

The actual passes are not defined within the frame graph. Instead the available passes are declared in the Materials used in the scene graph. The frame graph is only used to select which passes are used when rendering.

Generating the Shadow Map

In the shadow map generation pass, we must render to an offscreen surface (Framebuffer Object) which has a depth texture attachment. In Qt 3D, it is represented by the RenderTarget entity, which has a number of attachments.

In this example, we need only a depth attachment. We define it as a RenderAttachment entity using the RenderAttachment.DepthAttachment type that stores the depth and a Texture2D entity that actually configures the exture storage used to store the depth information:

Moreover, in this first pass, we must render using the light’s camera. Therefore, we have a CameraSelector entity that sets the camera to the one exported by the Light:

The second pass is more straightforward, because we simply render to the screen using the main camera:

Using Effects

The bulk of the magic happens in the AdsEffect.qml file, where our main Effect is defined. It implements the Ambient, Diffuse and Specular (ADS) Lighting Model using Phong shading with the addition of shadow mapping.

An effect contains the implementation of a particular rendering strategy. In this example, shadow mapping using two passes:

The parameters list defines some default values for the effect. The values will get mapped to shader program uniform variables, so that in the shaders we can access their values. In this example, we expose some information from the Light entity (position, intensity, view or projection matrix defined by the internal camera) and the shadow map texture exposed by the frame graph:

It is possible to put such parameters all the way down, from a Material , to its Effect , to one of the effect’s Techniques and a RenderPass within a Technique . This allows a Material instance to override defaults in an Effect , Technique or RenderPass .

To adapt the implementation to different hardware or OpenGL versions, we could use one or more Technique elements. In this example, only one technique is provided, targeting OpenGL 3.2 Core, or later:

Inside the technique, we finally have the definition of our two rendering passes. We tag each pass with a FilterKey object, matching the ones we specified in the frame graph configuration, so that each pass will have different rendering settings:

The first pass is the shadow map generation. We load a suitable set of GLSL shaders, which are actually extremely simple. They do only MVP (Model, View, Projection) to bring meshes from their model space into clip space (and, remember, in this first pass, the light is the camera). The fragment shader is totally empty, because there is no color to be generated, and the depth will be automatically captured for us by OpenGL:

In this first pass, we also set some custom OpenGL state in the form of a polygon offset and depth testing mode:

Rendering Using Phong Shading

The second pass is a normal forward rendering using Phong shading. The code in the effect entity is extremely simple. We simply configure some parameters and load a pair of shaders which will be used when drawing.

The first part of the shadow mapping happens in the vertex shader defined in ads.vert file, where we output towards the fragment shader the coordinates of each vertex in light space:

Actually, the coordinates get adjusted a little to allow us to easily sample the shadow map texture.

The second part happens in the fragment shader defined in the ads.frag file, where we sample the shadow map. If the currently processed fragment is behind the one closest to the light, then the current fragment is in shadow (and only gets ambient contribution). Otherwise, it gets full Phong shading:

Example project @ code.qt.io