Debian Codegraph @

tests/manual/custom-mesh-update-data-qml/main.qml

raw
1 // Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB).
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 import QtQuick 2.2 as QQ2
5 import Qt3D.Core 2.0
6 import Qt3D.Render 2.0
7 import Qt3D.Input 2.0
8 import Qt3D.Extras 2.0
9
10 Entity {
11 id: sceneRoot
12
13 Camera {
14 id: camera
15 projectionType: CameraLens.PerspectiveProjection
16 fieldOfView: 45
17 aspectRatio: 16/9
18 nearPlane : 0.1
19 farPlane : 1000.0
20 position: Qt.vector3d( 0.0, 0.0, 40.0 )
21 upVector: Qt.vector3d( 0.0, 1.0, 0.0 )
22 viewCenter: Qt.vector3d( 0.0, 0.0, 0.0 )
23 }
24
25 OrbitCameraController { camera: camera }
26
27 components: [
28 RenderSettings {
29 activeFrameGraph: ForwardRenderer {
30 clearColor: Qt.rgba(0.5, 0.5, 1, 1)
31 camera: camera
32 }
33 },
34 // Event Source will be set by the Qt3DQuickWindow
35 InputSettings { }
36 ]
37
38 PerVertexColorMaterial {
39 id: material
40 }
41
42 function buildVertexBufferData()
43 {
44 // Vertices
45 var v0 = Qt.vector3d(-1.0, 0.0, -1.0)
46 var v1 = Qt.vector3d(1.0, 0.0, -1.0)
47 var v2 = Qt.vector3d(0.0, 1.0, 0.0)
48 var v3 = Qt.vector3d(0.0, 0.0, 1.0)
49
50 // Face Normals
51 function normal(v0, v1, v2) {
52 return v1.minus(v0).crossProduct(v2.minus(v0)).normalized();
53 }
54 var n023 = normal(v0, v2, v3)
55 var n012 = normal(v0, v1, v2)
56 var n310 = normal(v3, v1, v0)
57 var n132 = normal(v1, v3, v2)
58
59 // Vector normals
60 var n0 = n023.plus(n012).plus(n310).normalized()
61 var n1 = n132.plus(n012).plus(n310).normalized()
62 var n2 = n132.plus(n012).plus(n023).normalized()
63 var n3 = n132.plus(n310).plus(n023).normalized()
64
65 // Colors
66 var red = Qt.vector3d(1.0, 0.0, 0.0)
67 var green = Qt.vector3d(0.0, 1.0, 0.0)
68 var blue = Qt.vector3d(0.0, 0.0, 1.0)
69 var white = Qt.vector3d(1.0, 1.0, 1.0)
70
71 var vertices = [
72 v0, n0, red,
73 v1, n1, blue,
74 v2, n2, green,
75 v3, n3, white
76 ]
77
78 var vertexArray = new Float32Array(4 * (3 + 3 + 3));
79 var i = 0;
80
81 vertices.forEach(function(vec3) {
82 vertexArray[i++] = vec3.x;
83 vertexArray[i++] = vec3.y;
84 vertexArray[i++] = vec3.z;
85 });
86
87 return vertexArray;
88 }
89
90 function buildIndexBufferData()
91 {
92 var indexArray = new Uint16Array(12);
93
94 // Front
95 indexArray[0] = 0;
96 indexArray[1] = 1;
97 indexArray[2] = 2;
98 // Bottom
99 indexArray[3] = 3;
100 indexArray[4] = 1;
101 indexArray[5] = 0;
102 // Left
103 indexArray[6] = 0;
104 indexArray[7] = 2;
105 indexArray[8] = 3;
106 // Right
107 indexArray[9] = 1;
108 indexArray[10] = 3;
109 indexArray[11] = 2;
110
111 return indexArray;
112 }
113
114 function updateVertexColor()
115 {
116 var angle = colorBufferTimerUpdate.angle
117
118 var c1 = Qt.vector3d(Math.abs(Math.cos(angle + Math.PI * 0.25)),
119 Math.abs(Math.sin(angle)),
120 Math.abs(Math.cos(angle)));
121 var c2 = Qt.vector3d(Math.abs(Math.sin(angle)),
122 Math.abs(Math.cos(angle + Math.PI * 0.25)),
123 Math.abs(Math.sin(angle + Math.PI * 0.25)));
124 var c3 = Qt.vector3d(Math.abs(Math.sin(angle + Math.PI * 0.25)),
125 Math.abs(Math.sin(angle)),
126 Math.abs(Math.cos(angle)));
127
128 var cols = [c1, c2, c3];
129
130 var byteOffset = 6.0 * 4.0 // 6 * sizeof(float)
131 var stride = 9.0 * 4.0 // 9 * sizeof(float)
132 var colorArray = new Float32Array(3)
133 cols.forEach(function(vec3Col) {
134 colorArray[0] = vec3Col.x
135 colorArray[1] = vec3Col.y
136 colorArray[2] = vec3Col.z
137 vertexBuffer.updateData(byteOffset, colorArray)
138 byteOffset += stride
139 });
140
141 colorBufferTimerUpdate.angle += Math.PI / 360.0;
142 }
143
144 QQ2.Timer {
145 id: colorBufferTimerUpdate
146 interval: 16
147 repeat: true
148 running: true
149 property real angle: 0
150 onTriggered: {
151 updateVertexColor()
152 }
153 }
154
155 GeometryRenderer {
156 id: customMesh
157 instanceCount: 1
158 indexOffset: 0
159 firstInstance: 0
160 primitiveType: GeometryRenderer.Triangles
161 Buffer {
162 id: vertexBuffer
163 data: buildVertexBufferData()
164 }
165
166 Buffer {
167 id: indexBuffer
168 data: buildIndexBufferData()
169 }
170 geometry: Geometry {
171 Attribute {
172 attributeType: Attribute.VertexAttribute
173 vertexBaseType: Attribute.Float
174 vertexSize: 3
175 byteOffset: 0
176 byteStride: 9 * 4
177 count: 4
178 name: defaultPositionAttributeName
179 buffer: vertexBuffer
180 }
181
182 Attribute {
183 attributeType: Attribute.VertexAttribute
184 vertexBaseType: Attribute.Float
185 vertexSize: 3
186 byteOffset: 3 * 4
187 byteStride: 9 * 4
188 count: 4
189 name: defaultNormalAttributeName
190 buffer: vertexBuffer
191 }
192
193 Attribute {
194 attributeType: Attribute.VertexAttribute
195 vertexBaseType: Attribute.Float
196 vertexSize: 3
197 byteOffset: 6 * 4
198 byteStride: 9 * 4
199 count: 4
200 name: defaultColorAttributeName
201 buffer: vertexBuffer
202 }
203
204 Attribute {
205 attributeType: Attribute.IndexAttribute
206 vertexBaseType: Attribute.UnsignedShort
207 vertexSize: 1
208 byteOffset: 0
209 byteStride: 0
210 count: 12
211 buffer: indexBuffer
212 }
213 }
214 }
215
216 Transform {
217 id: meshTransform
218 property real userAngle: 0.0
219 scale: 10
220 rotation: fromAxisAndAngle(Qt.vector3d(0, 1, 0), userAngle)
221 }
222
223 QQ2.NumberAnimation {
224 target: meshTransform
225 property: "userAngle"
226 duration: 10000
227 from: 0
228 to: 360
229
230 loops: QQ2.Animation.Infinite
231 running: true
232 }
233
234 Entity {
235 id: sphereEntity
236 components: [ customMesh, material, meshTransform ]
237 }
238 }