Debian Codegraph @

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

raw symbols SCIP
1 // Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB).
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include <QGuiApplication>
5 #include <QTimer>
6
7 #include <Qt3DCore/QEntity>
8 #include <Qt3DRender/QCamera>
9 #include <Qt3DRender/QCameraLens>
10 #include <Qt3DCore/QTransform>
11 #include <Qt3DCore/QAspectEngine>
12 #include <Qt3DCore/QGeometry>
13 #include <Qt3DCore/QAttribute>
14 #include <Qt3DCore/QBuffer>
15
16 #include <Qt3DInput/QInputAspect>
17
18 #include <Qt3DRender/QRenderStateSet>
19 #include <Qt3DRender/QRenderAspect>
20 #include <Qt3DExtras/QForwardRenderer>
21 #include <Qt3DExtras/QPerVertexColorMaterial>
22
23 #include <Qt3DRender/QGeometryRenderer>
24
25 #include <QPropertyAnimation>
26 #include <Qt3DExtras/qt3dwindow.h>
27 #include <Qt3DExtras/qfirstpersoncameracontroller.h>
28 #include <qmath.h>
29
30 QByteArray vertexBufferData;
31 Qt3DCore::QBuffer *vertexDataBuffer;
32
33 class TimerObject: public QObject {
34 Q_OBJECT
35
36 public:
37 TimerObject(QObject *parent = 0): QObject(parent) {
38 }
39 ~TimerObject() {
40 }
41 private slots:
42 void timeout();
43 private:
44 float angle = 0;
45 };
46
47 int main(int argc, char* argv[])
48 {
49 QGuiApplication app(argc, argv);
50 Qt3DExtras::Qt3DWindow view;
51 view.defaultFrameGraph()->setClearColor(QColor::fromRgbF(0.0, 0.5, 1.0, 1.0));
52
53 // Root entity
54 Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity();
55
56 // Camera
57 Qt3DRender::QCamera *cameraEntity = view.camera();
58
59 cameraEntity->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f);
60 cameraEntity->setPosition(QVector3D(50.0f, 20.0f, 40.0f));
61 cameraEntity->setUpVector(QVector3D(0, 1, 0));
62 cameraEntity->setViewCenter(QVector3D(0, 0, 0));
63
64 // For camera controls
65 Qt3DExtras::QFirstPersonCameraController *camController = new Qt3DExtras::QFirstPersonCameraController(rootEntity);
66 camController->setCamera(cameraEntity);
67
68 // Material
69 Qt3DRender::QMaterial *material = new Qt3DExtras::QPerVertexColorMaterial(rootEntity);
70
71 // Torus
72 Qt3DCore::QEntity *customMeshEntity = new Qt3DCore::QEntity(rootEntity);
73
74 // Transform
75 Qt3DCore::QTransform *transform = new Qt3DCore::QTransform;
76 transform->setScale(20.0f);
77
78 // Custom Mesh (TetraHedron)
79 Qt3DRender::QGeometryRenderer *customMeshRenderer = new Qt3DRender::QGeometryRenderer;
80 Qt3DCore::QGeometry *customGeometry = new Qt3DCore::QGeometry(customMeshRenderer);
81
82 vertexDataBuffer = new Qt3DCore::QBuffer(customGeometry);
83 Qt3DCore::QBuffer *indexDataBuffer = new Qt3DCore::QBuffer(customGeometry);
84
85 // vec3 for position
86 // vec3 for colors
87 // vec3 for normals
88
89 /* 2
90 /|\
91 / | \
92 / /3\ \
93 0/___\ 1
94 */
95
96 // 4 distinct vertices
97 vertexBufferData.resize(4 * (3 + 3 + 3) * sizeof(float));
98
99 // Vertices
100 QVector3D v0(-1.0f, 0.0f, -1.0f);
101 QVector3D v1(1.0f, 0.0f, -1.0f);
102 QVector3D v2(0.0f, 1.0f, 0.0f);
103 QVector3D v3(0.0f, 0.0f, 1.0f);
104
105 // Faces Normals
106 QVector3D n023 = QVector3D::normal(v0, v2, v3);
107 QVector3D n012 = QVector3D::normal(v0, v1, v2);
108 QVector3D n310 = QVector3D::normal(v3, v1, v0);
109 QVector3D n132 = QVector3D::normal(v1, v3, v2);
110
111 // Vector Normals
112 QVector3D n0 = QVector3D(n023 + n012 + n310).normalized();
113 QVector3D n1 = QVector3D(n132 + n012 + n310).normalized();
114 QVector3D n2 = QVector3D(n132 + n012 + n023).normalized();
115 QVector3D n3 = QVector3D(n132 + n310 + n023).normalized();
116
117 // Colors
118 QVector3D red(1.0f, 0.0f, 0.0f);
119 QVector3D green(0.0f, 1.0f, 0.0f);
120 QVector3D blue(0.0f, 0.0f, 1.0f);
121 QVector3D white(1.0f, 1.0f, 1.0f);
122
123 const QList<QVector3D> vertices = { v0, n0, red,
124 v1, n1, blue,
125 v2, n2, green,
126 v3, n3, white };
127
128 float *rawVertexArray = reinterpret_cast<float *>(vertexBufferData.data());
129 int idx = 0;
130
131 for (const QVector3D &v : vertices) {
132 rawVertexArray[idx++] = v.x();
133 rawVertexArray[idx++] = v.y();
134 rawVertexArray[idx++] = v.z();
135 }
136
137 // Indices (12)
138 QByteArray indexBufferData;
139 indexBufferData.resize(4 * 3 * sizeof(ushort));
140 ushort *rawIndexArray = reinterpret_cast<ushort *>(indexBufferData.data());
141
142 // Front
143 rawIndexArray[0] = 0;
144 rawIndexArray[1] = 1;
145 rawIndexArray[2] = 2;
146 // Bottom
147 rawIndexArray[3] = 3;
148 rawIndexArray[4] = 1;
149 rawIndexArray[5] = 0;
150 // Left
151 rawIndexArray[6] = 0;
152 rawIndexArray[7] = 2;
153 rawIndexArray[8] = 3;
154 // Right
155 rawIndexArray[9] = 1;
156 rawIndexArray[10] = 3;
157 rawIndexArray[11] = 2;
158
159 vertexDataBuffer->setData(vertexBufferData);
160 indexDataBuffer->setData(indexBufferData);
161
162 // Attributes
163 Qt3DCore::QAttribute *positionAttribute = new Qt3DCore::QAttribute();
164 positionAttribute->setAttributeType(Qt3DCore::QAttribute::VertexAttribute);
165 positionAttribute->setBuffer(vertexDataBuffer);
166 positionAttribute->setVertexSize(3);
167 positionAttribute->setByteOffset(0);
168 positionAttribute->setByteStride(9 * sizeof(float));
169 positionAttribute->setCount(4);
170 positionAttribute->setName(Qt3DCore::QAttribute::defaultPositionAttributeName());
171
172 Qt3DCore::QAttribute *normalAttribute = new Qt3DCore::QAttribute();
173 normalAttribute->setAttributeType(Qt3DCore::QAttribute::VertexAttribute);
174 normalAttribute->setBuffer(vertexDataBuffer);
175 normalAttribute->setVertexSize(3);
176 normalAttribute->setByteOffset(3 * sizeof(float));
177 normalAttribute->setByteStride(9 * sizeof(float));
178 normalAttribute->setCount(4);
179 normalAttribute->setName(Qt3DCore::QAttribute::defaultNormalAttributeName());
180
181 Qt3DCore::QAttribute *colorAttribute = new Qt3DCore::QAttribute();
182 colorAttribute->setAttributeType(Qt3DCore::QAttribute::VertexAttribute);
183 colorAttribute->setBuffer(vertexDataBuffer);
184 colorAttribute->setVertexSize(3);
185 colorAttribute->setByteOffset(6 * sizeof(float));
186 colorAttribute->setByteStride(9 * sizeof(float));
187 colorAttribute->setCount(4);
188 colorAttribute->setName(Qt3DCore::QAttribute::defaultColorAttributeName());
189
190 Qt3DCore::QAttribute *indexAttribute = new Qt3DCore::QAttribute();
191 indexAttribute->setAttributeType(Qt3DCore::QAttribute::IndexAttribute);
192 indexAttribute->setBuffer(indexDataBuffer);
193 indexAttribute->setVertexBaseType(Qt3DCore::QAttribute::UnsignedShort);
194 indexAttribute->setVertexSize(1);
195 indexAttribute->setByteOffset(0);
196 indexAttribute->setByteStride(0);
197 indexAttribute->setCount(12);
198
199 customGeometry->addAttribute(positionAttribute);
200 customGeometry->addAttribute(normalAttribute);
201 customGeometry->addAttribute(colorAttribute);
202 customGeometry->addAttribute(indexAttribute);
203
204 customMeshRenderer->setInstanceCount(1);
205 customMeshRenderer->setIndexOffset(0);
206 customMeshRenderer->setFirstInstance(0);
207 customMeshRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles);
208 customMeshRenderer->setGeometry(customGeometry);
209 // 4 faces of 3 points
210 customMeshRenderer->setVertexCount(12);
211
212 customMeshEntity->addComponent(customMeshRenderer);
213 customMeshEntity->addComponent(transform);
214 customMeshEntity->addComponent(material);
215
216 view.setRootEntity(rootEntity);
217 view.show();
218
219 QTimer *timer = new QTimer();
220 TimerObject *timerObject = new TimerObject();
221 QObject::connect(timer, SIGNAL(timeout()), timerObject, SLOT(timeout()));
222 timer->start(10);
223
224 return app.exec();
225 }
226
227 void TimerObject::timeout()
228 {
229 angle += qDegreesToRadians(0.5f);
230
231 QByteArray updateData;
232 updateData.resize(3*sizeof(float));
233
234 // Colors
235 QVector3D c1(qFabs(qCos(angle + M_PI_4)), qFabs(qSin(angle)), qFabs(qCos(angle)));
236 QVector3D c2(qFabs(qSin(angle)), qFabs(qCos(angle + M_PI_4)), qFabs(qSin(angle + M_PI_4)));
237 QVector3D c3(qFabs(qSin(angle + M_PI_4)), qFabs(qSin(angle)), qFabs(qCos(angle)));
238
239 const QList<QVector3D> colors = { c1, c2, c3 };
240
241 float *rawVertexArray = reinterpret_cast<float *>(updateData.data());
242
243 int pos = 6 * sizeof(float); //color offset
244 for (const QVector3D &v : colors) {
245 rawVertexArray[0] = v.x();
246 rawVertexArray[1] = v.y();
247 rawVertexArray[2] = v.z();
248 vertexDataBuffer->updateData(pos,updateData);
249 pos += 9 * sizeof(float); //stride
250 }
251 }
252
253 #include "main.moc"