From ebff027b83f9c0a48439fa49b52cbebd07f62cce Mon Sep 17 00:00:00 2001 From: Paul Lemire Date: Mon, 9 Mar 2015 11:12:31 +0100 Subject: anaglyph-3d example Note: you need red/cyan glasses to fully enjoy the experience Change-Id: Ic823831eea0f4658c2ab9116eb1505893847b6e8 Reviewed-by: Sean Harmer --- examples/qt3d/anaglyph-rendering/StereoCamera.qml | 102 +++++++++ .../qt3d/anaglyph-rendering/StereoFrameGraph.qml | 76 +++++++ .../qt3d/anaglyph-rendering/anaglyph-rendering.pro | 18 ++ examples/qt3d/anaglyph-rendering/main.cpp | 68 ++++++ examples/qt3d/anaglyph-rendering/main.qml | 227 +++++++++++++++++++++ examples/qt3d/anaglyph-rendering/resources.qrc | 7 + examples/qt3d/qt3d.pro | 3 +- examples/qt3d/skybox/Skybox.qml | 66 +----- examples/qt3d/skybox/main.qml | 4 +- 9 files changed, 510 insertions(+), 61 deletions(-) create mode 100644 examples/qt3d/anaglyph-rendering/StereoCamera.qml create mode 100644 examples/qt3d/anaglyph-rendering/StereoFrameGraph.qml create mode 100644 examples/qt3d/anaglyph-rendering/anaglyph-rendering.pro create mode 100644 examples/qt3d/anaglyph-rendering/main.cpp create mode 100644 examples/qt3d/anaglyph-rendering/main.qml create mode 100644 examples/qt3d/anaglyph-rendering/resources.qrc diff --git a/examples/qt3d/anaglyph-rendering/StereoCamera.qml b/examples/qt3d/anaglyph-rendering/StereoCamera.qml new file mode 100644 index 000000000..c91d81ddf --- /dev/null +++ b/examples/qt3d/anaglyph-rendering/StereoCamera.qml @@ -0,0 +1,102 @@ +/**************************************************************************** +** +** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import Qt3D 2.0 +import Qt3D.Render 2.0 + +Entity { + id: root + property real convergence: 2000.0 + property real eyeSeparation: 35.0 + property real aspectRatio: _window.width / _window.height + property real fieldOfView: 60.0 + property real nearPlane: 10.0 + property real farPlane: 10000.0 + + property alias viewCenter: eyeLookAt.viewCenter + property alias position: eyeLookAt.position + + readonly property real _fov2: Math.tan(fieldOfView * Math.PI / 180 * 0.5) + readonly property real top: nearPlane * _fov2 + readonly property real a: aspectRatio * _fov2 * convergence + + CameraLens { + id: leftEyeLens + projectionType: CameraLens.FrustumProjection + nearPlane : root.nearPlane + farPlane : root.farPlane + left: -(a - eyeSeparation * 0.5) * nearPlane / convergence + right: (a + eyeSeparation * 0.5) * nearPlane / convergence + top: root.top + bottom: -root.top + } + + CameraLens { + id: rightEyeLens + projectionType: CameraLens.FrustumProjection + nearPlane : root.nearPlane + farPlane : root.farPlane + left: -(a + eyeSeparation * 0.5) * nearPlane / convergence + right: (a - eyeSeparation * 0.5) * nearPlane / convergence + top: root.top + bottom: -root.top + } + + Transform { + id: eyeTransform + LookAt { + id: eyeLookAt + upVector: Qt.vector3d(0.0, 1.0, 0.0) + viewCenter: root.viewCenter + position: root.position + } + } + + property Entity leftCamera: Entity { + components: [ + leftEyeLens, + eyeTransform + ] + } + + property Entity rightCamera: Entity { + id: rightCameraEntity + components: [ + rightEyeLens, + eyeTransform + ] + } +} diff --git a/examples/qt3d/anaglyph-rendering/StereoFrameGraph.qml b/examples/qt3d/anaglyph-rendering/StereoFrameGraph.qml new file mode 100644 index 000000000..46164fc60 --- /dev/null +++ b/examples/qt3d/anaglyph-rendering/StereoFrameGraph.qml @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import Qt3D 2.0 +import Qt3D.Render 2.0 + +Viewport { + + property alias leftCamera: leftCameraSelector.camera + property alias rightCamera: rightCameraSelector.camera + + // ColorMask is reset by default + // By default reset to the default if not specified + ClearBuffer { + buffers: ClearBuffer.ColorDepthBuffer + NoDraw {} // We just want to clear the buffers + } + + // Draw with left eye + CameraSelector { + id: leftCameraSelector + StateSet { + renderStates: [ + ColorMask { red: true; green: false; blue: false; alpha: false }, + DepthTest { func: DepthTest.Less } + ] + } + } + + // Draw with right eye + ClearBuffer { + buffers: ClearBuffer.DepthBuffer + CameraSelector { + id: rightCameraSelector + StateSet { + renderStates: [ + ColorMask { red: false; green: true; blue: true; alpha: false }, + DepthTest { func: DepthTest.Less } + ] + } + } + } +} diff --git a/examples/qt3d/anaglyph-rendering/anaglyph-rendering.pro b/examples/qt3d/anaglyph-rendering/anaglyph-rendering.pro new file mode 100644 index 000000000..e57436172 --- /dev/null +++ b/examples/qt3d/anaglyph-rendering/anaglyph-rendering.pro @@ -0,0 +1,18 @@ +TEMPLATE = app + +SOURCE += main.cpp + +QT += qml quick 3dcore 3drenderer 3dinput 3dquick + +OTHER_FILES += *.qml + +SOURCES += \ + main.cpp + +include("../exampleresources/exampleresources.pri") + +RESOURCES += \ + resources.qrc + +DISTFILES += \ + StereoFrameGraph.qml diff --git a/examples/qt3d/anaglyph-rendering/main.cpp b/examples/qt3d/anaglyph-rendering/main.cpp new file mode 100644 index 000000000..621217c61 --- /dev/null +++ b/examples/qt3d/anaglyph-rendering/main.cpp @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include +#include +#include +#include + +#include +#include + +int main(int argc, char* argv[]) +{ + QGuiApplication app(argc, argv); + + initializeAssetResources("../exampleresources/example-assets.qrb"); + + Qt3D::Window view; + Qt3D::Quick::QQmlAspectEngine engine; + + engine.aspectEngine()->registerAspect(new Qt3D::QRenderAspect()); + engine.aspectEngine()->registerAspect(new Qt3D::QInputAspect()); + QVariantMap data; + data.insert(QStringLiteral("surface"), QVariant::fromValue(static_cast(&view))); + data.insert(QStringLiteral("eventSource"), QVariant::fromValue(&view)); + engine.aspectEngine()->setData(data); + engine.aspectEngine()->initialize(); + engine.qmlEngine()->rootContext()->setContextProperty("_window", &view); + engine.setSource(QUrl("qrc:/main.qml")); + view.show(); + + return app.exec(); +} diff --git a/examples/qt3d/anaglyph-rendering/main.qml b/examples/qt3d/anaglyph-rendering/main.qml new file mode 100644 index 000000000..bf1dd211c --- /dev/null +++ b/examples/qt3d/anaglyph-rendering/main.qml @@ -0,0 +1,227 @@ +/**************************************************************************** +** +** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import Qt3D 2.0 +import Qt3D.Render 2.0 +import QtQuick 2.4 as QQ2 + +Entity { + id: root + + components: FrameGraph { + StereoFrameGraph { + id: stereoFrameGraph + leftCamera: stereoCamera.leftCamera + rightCamera: stereoCamera.rightCamera + } + } + + // Camera + StereoCamera { + id: stereoCamera + property real circleRotation: 0 + readonly property real cameraRadius: obstaclesRepeater.radius - 50 + readonly property vector3d circlePosition: Qt.vector3d(cameraRadius * Math.cos(circleRotation), 0.0, cameraRadius * Math.sin(circleRotation)) + readonly property vector3d tan: circlePosition.crossProduct(Qt.vector3d(0, 1, 0).normalized()) + viewCenter: planeTranslation.translation + position: circlePosition.plus(Qt.vector3d(0, 45 * Math.sin(circleRotation * 2), 0)).plus(tan.times(-2)) + + QQ2.NumberAnimation { + target: stereoCamera + property: "circleRotation" + from: 0; to: Math.PI * 2 + duration: 10000 + loops: QQ2.Animation.Infinite + running: true + } + } + + // Skybox + SkyboxEntity { + cameraPosition: stereoCamera.position + baseName: "qrc:/assets/cubemaps/miramar/miramar" + extension: ".webp" + } + + // Cylinder + Entity { + property CylinderMesh cylinder: CylinderMesh { + radius: 1 + length: 3 + rings: 100 + slices: 20 + } + property Transform transform: Transform { + Scale { id: cylinderScale; scale: 5 } + Rotate { + id: cylinderRotationTheta + angle: 45 + axis: Qt.vector3d(1, 0, 0) + } + Rotate { + id: cylinderRotationPhi + angle: 30 + axis: Qt.vector3d(1, 0, 0) + } + } + property Material phong: PhongMaterial {} + + QQ2.ParallelAnimation { + loops: QQ2.Animation.Infinite + running: true + QQ2.SequentialAnimation { + QQ2.NumberAnimation { + target: cylinderScale + property: "scale" + from: 5; to: 45 + duration: 2000 + easing.type: QQ2.Easing.OutInQuad + } + QQ2.NumberAnimation { + target: cylinderScale + property: "scale" + from: 45; to: 5 + duration: 2000 + easing.type: QQ2.Easing.InOutQuart + } + } + QQ2.NumberAnimation { + target: cylinderRotationPhi + property: "angle" + from: 0; to: 360 + duration: 4000 + } + QQ2.NumberAnimation { + target: cylinderRotationTheta + property: "angle" + from: 0; to: 720 + duration: 4000 + } + } + + components: [cylinder, transform, phong] + } + + // AirPlane + Entity { + components: [ + Mesh { + source: "assets/obj/toyplane.obj" + }, + Transform { + Rotate { // roll + id: roll + axis : Qt.vector3d(1, 0, 0) + angle : 30 + } + Rotate { + axis: Qt.vector3d(0, 1, 0) + angle: stereoCamera.circleRotation * -2 * 180 / Math.PI + 180 + } + Translate { + id: planeTranslation + dx: Math.sin(stereoCamera.circleRotation * -2) * obstaclesRepeater.radius + dy: 0 + dz: Math.cos(stereoCamera.circleRotation * -2) * obstaclesRepeater.radius + } + }, + PhongMaterial { + shininess: 20.0 + diffuse: "#ba1a02" // Inferno Orange + } + ] + + QQ2.SequentialAnimation { + running: true + loops: QQ2.Animation.Infinite + + QQ2.NumberAnimation { + target: roll + property: "angle" + from: 30; to: 45 + duration: 750 + } + QQ2.NumberAnimation { + target: roll + property: "angle" + from: 45; to: 25 + duration: 500 + } + QQ2.NumberAnimation { + target: roll + property: "angle" + from: 25; to: 390 + duration: 800 + } + } + } + + // Torus obsctacles + NodeInstantiator { + id: obstaclesRepeater + model: 4 + readonly property real radius: 130.0; + readonly property real det: 1.0 / model + delegate: Entity { + components: [ + TorusMesh { + radius: 35 + minorRadius: 5 + rings: 100 + slices: 20 + }, + Transform { + id: transform + readonly property real angle: Math.PI * 2.0 * index * obstaclesRepeater.det + Rotate { + angle: transform.angle * 180 / Math.PI + axis: Qt.vector3d(0.0, 1.0, 0.0) + } + Translate { + dx: obstaclesRepeater.radius * Math.cos(transform.angle) + dz: obstaclesRepeater.radius * Math.sin(transform.angle) + } + }, + PhongMaterial { + diffuse: Qt.rgba(Math.abs(Math.cos(transform.angle)), 204 / 255, 75 / 255, 1) + specular: "white" + shininess: 20.0 + } + ] + } + } +} + diff --git a/examples/qt3d/anaglyph-rendering/resources.qrc b/examples/qt3d/anaglyph-rendering/resources.qrc new file mode 100644 index 000000000..e217f138c --- /dev/null +++ b/examples/qt3d/anaglyph-rendering/resources.qrc @@ -0,0 +1,7 @@ + + + main.qml + StereoCamera.qml + StereoFrameGraph.qml + + diff --git a/examples/qt3d/qt3d.pro b/examples/qt3d/qt3d.pro index 1f0db6160..d8979dabd 100644 --- a/examples/qt3d/qt3d.pro +++ b/examples/qt3d/qt3d.pro @@ -29,7 +29,8 @@ SUBDIRS += \ scene3d \ enabled-qml \ skybox \ - controls + controls \ + anaglyph-rendering # TODO Port the old examples to new APIs #SUBDIRS += qt3d diff --git a/examples/qt3d/skybox/Skybox.qml b/examples/qt3d/skybox/Skybox.qml index e8dfc29d1..49d269bf5 100644 --- a/examples/qt3d/skybox/Skybox.qml +++ b/examples/qt3d/skybox/Skybox.qml @@ -40,7 +40,7 @@ import Qt3D.Render 2.0 Entity { property alias cameraPosition: cameraTranslate.translation; - property string sourceDirectory: "qrc:/assets/cubemaps/miramar/miramar"; + property string sourceDirectory: ""; property string extension: ".webp" property TextureCubeMap skyboxTexture: TextureCubeMap { @@ -61,63 +61,14 @@ Entity { ShaderProgram { id: gl3SkyboxShader - - vertexShaderCode: " - #version 140 - - in vec3 vertexPosition; - out vec3 texCoord0; - - uniform mat4 mvp; - uniform mat4 inverseProjectionMatrix; - uniform mat4 inverseModelView; - - void main() - { - texCoord0 = vertexPosition.xyz; - gl_Position = vec4(mvp * vec4(vertexPosition, 1.0)).xyww; - } - " - fragmentShaderCode: " - #version 140 - - in vec3 texCoord0; - out vec4 fragColor; - uniform samplerCube skyboxTexture; - - void main() - { - fragColor = texture(skyboxTexture, texCoord0); - } - " + vertexShaderCode: loadSource("qrc:/shaders/gl3/skybox.vert") + fragmentShaderCode: loadSource("qrc:/shaders/gl3/skybox.frag") } ShaderProgram { id: gl2es2SkyboxShader - - vertexShaderCode: " - attribute vec3 vertexPosition; - varying vec3 texCoord0; - - uniform mat4 mvp; - uniform mat4 inverseProjectionMatrix; - uniform mat4 inverseModelView; - - void main() - { - texCoord0 = vertexPosition.xyz; - gl_Position = vec4(mvp * vec4(vertexPosition, 1.0)).xyww; - } - " - fragmentShaderCode: " - varying highp vec3 texCoord0; - uniform samplerCube skyboxTexture; - - void main() - { - gl_FragColor = textureCube(skyboxTexture, texCoord0); - } - " + vertexShaderCode: loadSource("qrc:/shaders/es2/skybox.vert") + fragmentShaderCode: loadSource("qrc:/shaders/es2/skybox.frag") } CuboidMesh { @@ -129,15 +80,12 @@ Entity { Transform { id: transform - Translate { id: cameraTranslate; } + Translate { id: cameraTranslate } } Material { id: skyboxMaterial - - parameters: [ - Parameter { name: "skyboxTexture"; value: skyboxTexture} - ] + parameters: Parameter { name: "skyboxTexture"; value: skyboxTexture} effect: Effect { techniques: [ diff --git a/examples/qt3d/skybox/main.qml b/examples/qt3d/skybox/main.qml index 83bb663cf..8c995e94f 100644 --- a/examples/qt3d/skybox/main.qml +++ b/examples/qt3d/skybox/main.qml @@ -60,8 +60,10 @@ Entity { } // So that the camera is rendered always at the same position as the camera - Skybox { + SkyboxEntity { cameraPosition: basicCamera.position + sourceDirectory: "qrc:/assets/cubemaps/miramar/miramar" + extension: ".webp" } Configuration { -- cgit v1.2.3