Debian Codegraph @

tests/manual/custom-mesh-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 GeometryRenderer {
115 id: customMesh
116 instanceCount: 1
117 indexOffset: 0
118 firstInstance: 0
119 primitiveType: GeometryRenderer.Triangles
120 Buffer {
121 id: vertexBuffer
122 data: buildVertexBufferData()
123 }
124
125 Buffer {
126 id: indexBuffer
127 data: buildIndexBufferData()
128 }
129 geometry: Geometry {
130 Attribute {
131 attributeType: Attribute.VertexAttribute
132 vertexBaseType: Attribute.Float
133 vertexSize: 3
134 byteOffset: 0
135 byteStride: 9 * 4
136 count: 4
137 name: defaultPositionAttributeName
138 buffer: vertexBuffer
139 }
140
141 Attribute {
142 attributeType: Attribute.VertexAttribute
143 vertexBaseType: Attribute.Float
144 vertexSize: 3
145 byteOffset: 3 * 4
146 byteStride: 9 * 4
147 count: 4
148 name: defaultNormalAttributeName
149 buffer: vertexBuffer
150 }
151
152 Attribute {
153 attributeType: Attribute.VertexAttribute
154 vertexBaseType: Attribute.Float
155 vertexSize: 3
156 byteOffset: 6 * 4
157 byteStride: 9 * 4
158 count: 4
159 name: defaultColorAttributeName
160 buffer: vertexBuffer
161 }
162
163 Attribute {
164 attributeType: Attribute.IndexAttribute
165 vertexBaseType: Attribute.UnsignedShort
166 vertexSize: 1
167 byteOffset: 0
168 byteStride: 0
169 count: 12
170 buffer: indexBuffer
171 }
172 }
173 }
174
175 Transform {
176 id: meshTransform
177 property real userAngle: 0.0
178 scale: 10
179 rotation: fromAxisAndAngle(Qt.vector3d(0, 1, 0), userAngle)
180 }
181
182 QQ2.NumberAnimation {
183 target: meshTransform
184 property: "userAngle"
185 duration: 10000
186 from: 0
187 to: 360
188
189 loops: QQ2.Animation.Infinite
190 running: true
191 }
192
193 Entity {
194 id: sphereEntity
195 components: [ customMesh, material, meshTransform ]
196 }
197 }