| 1 |
// Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
import Qt3D.Core 2.0 |
| 5 |
import Qt3D.Render 2.0 |
| 6 |
import Qardboard 1.0 |
| 7 |
|
| 8 |
Entity { |
| 9 |
id: root |
| 10 |
property real convergence: 2000.0 |
| 11 |
property real eyeSeparation: 35.0 |
| 12 |
property real aspectRatio: _window.width / _window.height |
| 13 |
property real fieldOfView: 60.0 |
| 14 |
property real nearPlane: 1. |
| 15 |
property real farPlane: 1000.0 |
| 16 |
|
| 17 |
property vector3d position: Qt.vector3d(0.0, 0.0, 0.0) |
| 18 |
property vector3d viewDirection: Qt.vector3d(0.0, 0.0, 1.0) |
| 19 |
property vector3d viewUp: Qt.vector3d(0.0, 1.0, 0.0) |
| 20 |
property alias viewMatrix: eyeTransform.matrix |
| 21 |
|
| 22 |
readonly property real _fov2: Math.tan(fieldOfView * Math.PI / 180 * 0.5) |
| 23 |
readonly property real top: nearPlane * _fov2 |
| 24 |
readonly property real a: aspectRatio * _fov2 * convergence |
| 25 |
|
| 26 |
DeviceOrientation { |
| 27 |
id: orientation |
| 28 |
enabled: true |
| 29 |
} |
| 30 |
|
| 31 |
CameraLens { |
| 32 |
id: leftEyeLens |
| 33 |
projectionType: CameraLens.FrustumProjection |
| 34 |
nearPlane : root.nearPlane |
| 35 |
farPlane : root.farPlane |
| 36 |
left: -(a - eyeSeparation * 0.5) * nearPlane / convergence |
| 37 |
right: (a + eyeSeparation * 0.5) * nearPlane / convergence |
| 38 |
top: root.top |
| 39 |
bottom: -root.top |
| 40 |
} |
| 41 |
|
| 42 |
CameraLens { |
| 43 |
id: rightEyeLens |
| 44 |
projectionType: CameraLens.FrustumProjection |
| 45 |
nearPlane : root.nearPlane |
| 46 |
farPlane : root.farPlane |
| 47 |
left: -(a + eyeSeparation * 0.5) * nearPlane / convergence |
| 48 |
right: (a - eyeSeparation * 0.5) * nearPlane / convergence |
| 49 |
top: root.top |
| 50 |
bottom: -root.top |
| 51 |
} |
| 52 |
|
| 53 |
Transform { |
| 54 |
id: eyeTransform |
| 55 |
|
| 56 |
function computeMatrix(roll, pitch, yaw) { |
| 57 |
var n = Qt.matrix4x4() |
| 58 |
n.rotate(-roll, Qt.vector3d(1, 0, 0)) |
| 59 |
n.rotate(yaw, Qt.vector3d(0, 1, 0)) |
| 60 |
var vd = n.times(stereoCamera.viewDirection) |
| 61 |
var vu = n.times(stereoCamera.viewUp) |
| 62 |
var vc = stereoCamera.position.minus(vd) |
| 63 |
|
| 64 |
var m = Qt.matrix4x4(); |
| 65 |
m.translate(stereoCamera.position) |
| 66 |
var zAxis = stereoCamera.position.minus(vc).normalized() |
| 67 |
var xAxis = vu.crossProduct(zAxis).normalized(); |
| 68 |
var yAxis = zAxis.crossProduct(xAxis); |
| 69 |
var r = Qt.matrix4x4(xAxis.x, yAxis.x, zAxis.x, 0, |
| 70 |
xAxis.y, yAxis.y, zAxis.y, 0, |
| 71 |
xAxis.z, yAxis.z, zAxis.z, 0, |
| 72 |
0, 0, 0, 1) |
| 73 |
return m.times(r); |
| 74 |
} |
| 75 |
|
| 76 |
matrix: computeMatrix(orientation.roll, orientation.pitch, orientation.yaw) |
| 77 |
} |
| 78 |
|
| 79 |
components: [ eyeTransform ] |
| 80 |
|
| 81 |
property Entity leftCamera: Entity { |
| 82 |
components: [ leftEyeLens ] |
| 83 |
} |
| 84 |
|
| 85 |
property Entity rightCamera: Entity { |
| 86 |
id: rightCameraEntity |
| 87 |
components: [ rightEyeLens ] |
| 88 |
} |
| 89 |
} |