Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

Shadow Mapping in Qt3D 2.0

Shadow mapping in Qt3D. Note the self-shadowing of the plane and of the trefoil knot.

Exaggerated shadow map texture of the very same scene represented above.

import Qt3D 2.0
import Qt3D.Render 2.0

Entity {
    id: sceneRoot

    Camera {
        id: camera
        projectionType: CameraLens.PerspectiveProjection
        fieldOfView: 45
        aspectRatio: _window.width / _window.height
        nearPlane: 0.1
        farPlane: 1000.0
        position: Qt.vector3d(0.0, 10.0, 20.0)
        viewCenter: Qt.vector3d(0.0, 0.0, 0.0)
        upVector: Qt.vector3d(0.0, 1.0, 0.0)
    }

    Configuration  {
        controlledCamera: camera
    }

    Light {
        id: light
    }

    components: [
        ShadowMapFrameGraph {
            id: framegraph
            viewCamera: camera
            lightCamera: light.lightCamera
        }
    ]

    AdsEffect {
        id: shadowMapEffect

        shadowTexture: framegraph.shadowTexture
        light: light
    }

    // Trefoil knot entity
    Trefoil {
        material: AdsMaterial {
            effect: shadowMapEffect
            specularColor: Qt.rgba(0.5, 0.5, 0.5, 1.0)
        }
    }

    // Toyplane entity
    Toyplane {
        material: AdsMaterial {
            effect: shadowMapEffect
            diffuseColor: Qt.rgba(0.9, 0.5, 0.3, 1.0)
            shininess: 75
        }
    }

    // Plane entity
    GroundPlane {
        material: AdsMaterial {
            effect: shadowMapEffect
            diffuseColor: Qt.rgba(0.2, 0.5, 0.3, 1.0)
            specularColor: Qt.rgba(0, 0, 0, 1.0)
        }
    }
}
import Qt3D 2.0
import Qt3D.Render 2.0

Entity {
    id: root

    property vector3d lightPosition: Qt.vector3d(30.0, 30.0, 0.0)
    property vector3d lightIntensity: Qt.vector3d(1.0, 1.0, 1.0)

    readonly property Camera lightCamera: lightCamera
    readonly property matrix4x4 lightViewProjection: lightCamera.projectionMatrix.times(lightCamera.matrix)

    Camera {
        id: lightCamera
        objectName: "lightCameraLens"
        projectionType: CameraLens.PerspectiveProjection
        fieldOfView: 45
        aspectRatio: 1
        nearPlane : 0.1
        farPlane : 200.0
        position: root.lightPosition
        viewCenter: Qt.vector3d(0.0, 0.0, 0.0)
        upVector: Qt.vector3d(0.0, 1.0, 0.0)
    }
}
import Qt3D 2.0
import Qt3D.Render 2.0
import QtQuick 2.2 as QQ2

FrameGraph {
    id: root

    property alias viewCamera: viewCameraSelector.camera
    property alias lightCamera: lightCameraSelector.camera
    readonly property Texture2D shadowTexture: depthTexture

    activeFrameGraph: Viewport {
        rect: Qt.rect(0.0, 0.0, 1.0, 1.0)
        clearColor: Qt.rgba(0.0, 0.4, 0.7, 1.0)

        RenderPassFilter {
            includes: [ Annotation { name: "pass"; value: "shadowmap" } ]

            RenderTargetSelector {
                target: RenderTarget {
                    attachments: [
                        RenderAttachment {
                            name: "depth"
                            type: RenderAttachment.DepthAttachment
                            texture: Texture2D {
                                id: depthTexture
                                width: 1024
                                height: 1024
                                format: Texture.DepthFormat
                                generateMipMaps: false
                                magnificationFilter: Texture.Linear
                                minificationFilter: Texture.Linear
                                wrapMode {
                                    x: WrapMode.ClampToEdge
                                    y: WrapMode.ClampToEdge
                                }
                                comparisonFunction: Texture.CompareLessEqual
                                comparisonMode: Texture.CompareRefToTexture
                            }
                        }
                    ]
                }

                ClearBuffer {
                    buffers: ClearBuffer.DepthBuffer

                    CameraSelector {
                        id: lightCameraSelector
                    }
                }
            }
        }

        RenderPassFilter {
            includes: [ Annotation { name: "pass"; value: "forward" } ]

            ClearBuffer {
                buffers: ClearBuffer.ColorDepthBuffer

                CameraSelector {
                    id: viewCameraSelector
                }
            }
        }
    }
}
import Qt3D 2.0
import Qt3D.Render 2.0

Effect {
    id: root

    property Texture2D shadowTexture
    property Light light

    parameters: [
        Parameter { name: "lightViewProjection"; value: root.light.lightViewProjection },
        Parameter { name: "lightPosition";  value: root.light.lightPosition },
        Parameter { name: "lightIntensity"; value: root.light.lightIntensity },

        Parameter { name: "shadowMapTexture"; value: root.shadowTexture }
    ]

    techniques: [
        Technique {
            openGLFilter {
                api: OpenGLFilter.Desktop
                profile: OpenGLFilter.Core
                majorVersion: 3
                minorVersion: 2
            }

            renderPasses: [
                RenderPass {
                    annotations: [ Annotation { name: "pass"; value: "shadowmap" } ]

                    shaderProgram: ShaderProgram {
                        vertexShaderCode:   loadSource("qrc:/shaders/shadowmap.vert")
                        fragmentShaderCode: loadSource("qrc:/shaders/shadowmap.frag")
                    }

                    renderStates: [
                        PolygonOffset { factor: 4; units: 4 },
                        DepthTest { func: DepthTest.Less }
                    ]
                },

                RenderPass {
                    annotations: [ Annotation { name : "pass"; value : "forward" } ]

                    bindings: [
                        // Uniforms (those provided by the user)
                        ParameterMapping { parameterName: "ambient";  shaderVariableName: "ka"; bindingType: ParameterMapping.Uniform },
                        ParameterMapping { parameterName: "diffuse";  shaderVariableName: "kd"; bindingType: ParameterMapping.Uniform },
                        ParameterMapping { parameterName: "specular"; shaderVariableName: "ks"; bindingType: ParameterMapping.Uniform }
                    ]

                    shaderProgram: ShaderProgram {
                        vertexShaderCode:   loadSource("qrc:/shaders/ads.vert")
                        fragmentShaderCode: loadSource("qrc:/shaders/ads.frag")
                    }
                }
            ]
        }
    ]
}
positionInLightSpace = shadowMatrix * lightViewProjection
        * modelMatrix * vec4(vertexPosition, 1.0);
float shadowMapSample = textureProj(shadowMapTexture, positionInLightSpace);

    vec3 ambient = lightIntensity * ka;
    vec3 result = ambient;

    if (shadowMapSample > 0)
        result += dsModel(position, normalize(normal));

    fragColor = vec4(result, 1.0);

About KDAB


25 Comments

21 - Jan - 2015

Philip

21 - Jan - 2015

Giuseppe D'Angelo

21 - Jan - 2015

Philip

21 - Jan - 2015

Giuseppe D'Angelo

22 - Jan - 2015

Philip

23 - Jan - 2015

Giuseppe D'Angelo

23 - Jan - 2015

Philip

24 - Jan - 2015

Sean Harmer

26 - Jan - 2015

Philip

26 - Jan - 2015

Jim

26 - Jan - 2015

Giuseppe D'Angelo

26 - Jan - 2015

jiangcaiyang

.obj/release/main.o:main.cpp:(.text+0x8a): undefined reference to `_imp___ZN4Qt3D6WindowC1EP7QScreen'
.obj/release/main.o:main.cpp:(.text+0x9e): undefined reference to `_imp___ZN4Qt3D5Quick16QQmlAspectEngineC1EP7QObject'
.obj/release/main.o:main.cpp:(.text+0xbe): undefined reference to `_imp___ZN4Qt3D13QRenderAspectC1EP7QObject'
.obj/release/main.o:main.cpp:(.text+0xc3): undefined reference to `_imp___ZNK4Qt3D5Quick16QQmlAspectEngine12aspectEngineEv'
.obj/release/main.o:main.cpp:(.text+0xdd): undefined reference to `_imp___ZN4Qt3D13QAspectEngine14registerAspectEPNS_15QAbstractAspectE'
.obj/release/main.o:main.cpp:(.text+0xff): undefined reference to `_imp___ZN4Qt3D12QInputAspectC1EP7QObject'
.obj/release/main.o:main.cpp:(.text+0x3d8): undefined reference to `_imp___ZN4Qt3D13QAspectEngine7setDataERK4QMapI7QString8QVariantE'
Makefile.Release:84: recipe for target 'release\playground-qml.exe' failed
.obj/release/main.o:main.cpp:(.text+0x3e9): undefined reference to `_imp___ZN4Qt3D13QAspectEngine10initializeEv'
.obj/release/main.o:main.cpp:(.text+0x427): undefined reference to `_imp___ZN4Qt3D5Quick16QQmlAspectEngine9setSourceERK4QUrl'
.obj/release/main.o:main.cpp:(.text+0x4a7): undefined reference to `_imp___ZTVN4Qt3D5Quick16QQmlAspectEngineE'
.obj/release/main.o:main.cpp:(.text+0x4c1): undefined reference to `_imp___ZN4Qt3D6WindowD1Ev'
.obj/release/main.o:main.cpp:(.text+0x5b5): undefined reference to `_imp___ZN4Qt3D6Window16staticMetaObjectE'
E:/Develop/Qt5.4/Tools/mingw491_32/bin/../lib/gcc/i686-w64-mingw32/4.9.1/../../../../i686-w64-mingw32/bin/ld.exe: .obj/release/main.o: bad reloc address 0x14 in section `.text$_Z17qRegisterMetaTypeIP8QSurfaceEiPKcPT_N9QtPrivate21MetaTypeDefinedHelperIS4_Xaasr12QMetaTypeId2IS4_E7DefinedntsrS9_9IsBuiltInEE11DefinedTypeE[__Z17qRegisterMetaTypeIP8QSurfaceEiPKcPT_N9QtPrivate21MetaTypeDefinedHelperIS4_Xaasr12QMetaTypeId2IS4_E7DefinedntsrS9_9IsBuiltInEE11DefinedTypeE]'

26 - Jan - 2015

jiangcaiyang

26 - Jan - 2015

Sean Harmer

26 - Jan - 2015

jiangcaiyang

#if defined(QT3DCORE_LIBRARY)
#  define QT3DCORESHARED_EXPORT Q_DECL_EXPORT
#else
#  define QT3DCORESHARED_EXPORT Q_DECL_IMPORT
#endif

26 - Jan - 2015

Sean Harmer

26 - Jan - 2015

jiangcaiyang

INCLUDEPATH +=  $$PWD

win32 {
    build_pass {
        CONFIG(debug, debug|release) {
            LIBS += $$shadowed($$PWD)/debug/libexampleresources.$${QMAKE_EXTENSION_STATICLIB}
        } else {
            LIBS += $$shadowed($$PWD)/release/libexampleresources.$${QMAKE_EXTENSION_STATICLIB}
        }
    }
} else {
    LIBS += $$shadowed($$PWD)/libexampleresources.$${QMAKE_EXTENSION_STATICLIB}
}

26 - Jan - 2015

Sean Harmer

27 - Jan - 2015

jiangcaiyang

#if defined(QT_SHARED) || !defined(QT_STATIC)
#  if defined(ENGINIOCLIENT_LIBRARY)
#    define ENGINIOCLIENT_EXPORT Q_DECL_EXPORT
#  else
#    define ENGINIOCLIENT_EXPORT Q_DECL_IMPORT
#  endif
#else
#  define ENGINIOCLIENT_EXPORT
#endif

27 - Jan - 2015

Sean Harmer

5 - Sept - 2015

Marco

7 - Sept - 2015

Giuseppe D'Angelo

24 - Nov - 2015

Dominik

30 - Nov - 2015

Giuseppe D'Angelo

18 - Apr - 2018

Carlos Ranoya

GiuseppeD'Angelo

Giuseppe D’Angelo

Senior Software Engineer