-
Notifications
You must be signed in to change notification settings - Fork 63
/
camera_keyboard.qml
167 lines (145 loc) · 3.59 KB
/
camera_keyboard.qml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import Qt3D.Core 2.0
import Qt3D.Input 2.0
import VirtualKey 1.0
import "Components"
Scene2 {
id: scene
children: VirtualKeys {
target: scene
gameButtonsEnabled: false
color: "transparent"
}
Entity {
id: root
RenderSettings1 {}
InputSettings {}
KeyboardDevice {
id: keyboardDevice
}
property var keys: {
"up":false, "down":false, "left":false, "right": false
}
KeyboardHandler {
id: keyboardHandler
sourceDevice: keyboardDevice
focus: true
onPressed: {
switch (event.key) {
case Qt.key_W:
case 'W'.charCodeAt(0): // BUG ???
case Qt.Key_Up:
root.keys.up = true;
break;
case Qt.key_S:
case 'S'.charCodeAt(0):
case Qt.Key_Down:
root.keys.down = true;
break;
case Qt.key_A:
case 'A'.charCodeAt(0):
case Qt.Key_Left:
root.keys.left = true;
break;
case Qt.key_D:
case 'D'.charCodeAt(0):
case Qt.Key_Right:
root.keys.right = true;
break;
}
}
onReleased: {
switch (event.key) {
case Qt.key_W:
case 'W'.charCodeAt(0):
case Qt.Key_Up:
root.keys.up = false;
break;
case Qt.key_S:
case 'S'.charCodeAt(0):
case Qt.Key_Down:
root.keys.down = false;
break;
case Qt.key_A:
case 'A'.charCodeAt(0):
case Qt.Key_Left:
root.keys.left = false;
break;
case Qt.key_D:
case 'D'.charCodeAt(0):
case Qt.Key_Right:
root.keys.right = false;
break;
}
}
}
FrameSwap {
/*
FrameAction: The event between frames change
in triggered() method, dt is provided by Qt for delta calculations
*/
property real cameraSpeed: .05
onTriggered: {
if (root.keys.up)
camera.position = camera.position.plus(camera.frontVector.times(cameraSpeed));
if (root.keys.down)
camera.position = camera.position.minus(camera.frontVector.times(cameraSpeed));
if (root.keys.right)
camera.position = camera.position.plus(camera.rightVector.times(cameraSpeed));
if (root.keys.left)
camera.position = camera.position.minus(camera.rightVector.times(cameraSpeed));
}
}
Entity {
id: camera
property vector3d position: "0,0,3"
property vector3d viewCenter: position.plus(frontVector)
property vector3d upVector: "0,1,0"
property vector3d frontVector: "0,0,-1"
property vector3d rightVector: frontVector.crossProduct(upVector).normalized()
property matrix4x4 viewMatrix: {
var m = Qt.matrix4x4();
m.lookAt(position, viewCenter, upVector);
return m;
}
property matrix4x4 projectionMatrix: {
var fov = 45;
var aspect = scene.width / scene.height;
var zNear = .1;
var zFar = 100.;
var h = Math.tan(fov * Math.PI / 360) * zNear;
var w = h * aspect;
var m = Qt.matrix4x4();
m.m11 = zNear / w;
m.m22 = zNear / h;
m.m33 = - (zNear + zFar) / (zFar - zNear);
m.m34 = -2 * zNear * zFar / (zFar - zNear);
m.m43 = -1;
m.m44 = 0;
return m;
}
}
property var cubePositions: [
Qt.vector3d( 0.0, 0.0, 0.0),
Qt.vector3d( 2.0, 5.0, -15.0),
Qt.vector3d(-1.5, -2.2, -2.5),
Qt.vector3d(-3.8, -2.0, -12.3),
Qt.vector3d( 2.4, -0.4, -3.5),
Qt.vector3d(-1.7, 3.0, -7.5),
Qt.vector3d( 1.3, -2.0, -2.5),
Qt.vector3d( 1.5, 2.0, -2.5),
Qt.vector3d( 1.5, 0.2, -1.5),
Qt.vector3d(-1.3, 1.0, -1.5),
]
NodeInstantiator {
model: root.cubePositions
delegate: TextureCube0 {
transform: Transform {
translation: modelData
rotation: fromAxisAndAngle(Qt.vector3d(0.5, 1.0, 0.0), 20 * index)
}
viewMatrix: camera.viewMatrix
projectionMatrix: camera.projectionMatrix
}
}
}
}