-
Notifications
You must be signed in to change notification settings - Fork 63
/
coordinate_systems_with_depth.qml
278 lines (247 loc) · 6.45 KB
/
coordinate_systems_with_depth.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import Qt3D.Core 2.0
import Qt3D.Render 2.0
import Qt3D.Input 2.0
import VirtualKey 1.0
import "Components"
Scene2 {
id: scene
children: VirtualKeys {
target: scene
targetHandler: keyboardHandler
padEnabled: false
gameButtonsEnabled: false
color: "transparent"
centerItem: RowKeys {
keys: [
{text:"Space", key:Qt.Key_Space},
{text:"Return", key:Qt.Key_Return},
]
}
}
Entity {
id: root
RenderSettings {
activeFrameGraph: ClearBuffers {
buffers: ClearBuffers.ColorDepthBuffer
clearColor: Qt.rgba(0.2, 0.3, 0.3, 1.0)
RenderSurfaceSelector {
RenderStateSet {
id: renderStateSet
renderStates: DepthTest {
/*
Let's explicitly enable depth test now
Use depthFunction to decide which compared result to be shown
Refer: qthelp://org.qt-project.qt3d.570/qt3d/qt3drender-qdepthtest.html
*/
depthFunction: DepthTest.Less
}
}
}
}
}
InputSettings {}
KeyboardDevice {
id: keyboardDevice
}
property bool useQtTransform: false
property bool enableDepthTest: true
property var storeDepthTest
KeyboardHandler {
/*
Space to toggle depth test
Return to toggle the usage of Qt3D Transform
*/
id: keyboardHandler
focus: true
onSpacePressed: {
root.enableDepthTest = !root.enableDepthTest;
if (root.enableDepthTest) {
renderStateSet.renderStates = [root.storeDepthTest];
} else {
root.storeDepthTest = renderStateSet.renderStates[0];
renderStateSet.renderStates = [];
}
console.log("enableDepthTest:", root.enableDepthTest);
}
onReturnPressed: {
root.useQtTransform = !root.useQtTransform;
console.log("useQtTransform:", root.useQtTransform);
}
sourceDevice: keyboardDevice
}
FrameSwap {}
Entity {
id: cube
GeometryRenderer {
id: geometry
geometry: Geometry {
boundingVolumePositionAttribute: position
Attribute {
id: position
attributeType: Attribute.VertexAttribute
vertexBaseType: Attribute.Float
vertexSize: 3
count: 36
byteOffset: 0
byteStride: 4 * 5
name: "position"
buffer: vertexBuffer
}
Attribute {
attributeType: Attribute.VertexAttribute
vertexBaseType: Attribute.Float
vertexSize: 2
count: 36
byteOffset: 4 * 3
byteStride: 4 * 5
name: "texCoord"
buffer: vertexBuffer
}
}
Buffer {
id: vertexBuffer
type: Buffer.VertexBuffer
data: new Float32Array([
-0.5, -0.5, -0.5, 0.0, 0.0,
0.5, -0.5, -0.5, 1.0, 0.0,
0.5, 0.5, -0.5, 1.0, 1.0,
0.5, 0.5, -0.5, 1.0, 1.0,
-0.5, 0.5, -0.5, 0.0, 1.0,
-0.5, -0.5, -0.5, 0.0, 0.0,
-0.5, -0.5, 0.5, 0.0, 0.0,
0.5, -0.5, 0.5, 1.0, 0.0,
0.5, 0.5, 0.5, 1.0, 1.0,
0.5, 0.5, 0.5, 1.0, 1.0,
-0.5, 0.5, 0.5, 0.0, 1.0,
-0.5, -0.5, 0.5, 0.0, 0.0,
-0.5, 0.5, 0.5, 1.0, 0.0,
-0.5, 0.5, -0.5, 1.0, 1.0,
-0.5, -0.5, -0.5, 0.0, 1.0,
-0.5, -0.5, -0.5, 0.0, 1.0,
-0.5, -0.5, 0.5, 0.0, 0.0,
-0.5, 0.5, 0.5, 1.0, 0.0,
0.5, 0.5, 0.5, 1.0, 0.0,
0.5, 0.5, -0.5, 1.0, 1.0,
0.5, -0.5, -0.5, 0.0, 1.0,
0.5, -0.5, -0.5, 0.0, 1.0,
0.5, -0.5, 0.5, 0.0, 0.0,
0.5, 0.5, 0.5, 1.0, 0.0,
-0.5, -0.5, -0.5, 0.0, 1.0,
0.5, -0.5, -0.5, 1.0, 1.0,
0.5, -0.5, 0.5, 1.0, 0.0,
0.5, -0.5, 0.5, 1.0, 0.0,
-0.5, -0.5, 0.5, 0.0, 0.0,
-0.5, -0.5, -0.5, 0.0, 1.0,
-0.5, 0.5, -0.5, 0.0, 1.0,
0.5, 0.5, -0.5, 1.0, 1.0,
0.5, 0.5, 0.5, 1.0, 0.0,
0.5, 0.5, 0.5, 1.0, 0.0,
-0.5, 0.5, 0.5, 0.0, 0.0,
-0.5, 0.5, -0.5, 0.0, 1.0,
])
}
}
Transform {
/*
This is Qt3D internal transform QML
It is what "model" is in renderer
With several Transform cascaded in QML
the final modelMatrix is the product of all transform matrix in order,
so you need no longer multiple them by yourself
The internal uniform names can be found in:
qt3d/src/render/backend/renderview.cpp:137
*/
id: transform
matrix: { // modelMatrix
var m = Qt.matrix4x4();
m.rotate(time.value % 360 * 50, Qt.vector3d(0.5, 1.0, 0.0));
return m;
}
Time {
id: time
}
}
Material {
id: material
effect: Effect {
techniques: AutoTechnique {
renderPasses: RenderPass {
shaderProgram: AutoShaderProgram {
vertName: root.useQtTransform?
"coordinate_systems_qt3d_transform":
"coordinate_systems"
fragName: "textures_combined"
}
parameters: [
Parameter {
name: "ourTexture1"
value: Texture2D {
generateMipMaps: true
minificationFilter: Texture.Linear
magnificationFilter: Texture.Linear
wrapMode {
x: WrapMode.Repeat
y: WrapMode.Repeat
}
TextureImage {
mipLevel: 0
source: Resources.texture("container.jpg")
}
}
},
Parameter {
name: "ourTexture2"
value: Texture2D {
generateMipMaps: true
minificationFilter: Texture.Linear
magnificationFilter: Texture.Linear
wrapMode {
x: WrapMode.Repeat
y: WrapMode.Repeat
}
TextureImage {
mipLevel: 0
source: Resources.texture("awesomeface.png")
}
}
},
Parameter {
name: "model"
value: transform.matrix // vs Qt3D Transform
},
Parameter {
name: "view"
value: {
var m = Qt.matrix4x4();
m.translate(0, 0, -3);
return m;
}
},
Parameter {
name: "projection"
value: {
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;
}
}
]
}
}
}
}
components: [geometry, transform, material]
}
}
}