1/****************************************************************************
2**
3** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB).
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt3D module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include <QGuiApplication>
52#include <QTimer>
53
54#include <Qt3DCore/QEntity>
55#include <Qt3DRender/QCamera>
56#include <Qt3DRender/QCameraLens>
57#include <Qt3DCore/QTransform>
58#include <Qt3DCore/QAspectEngine>
59
60#include <Qt3DInput/QInputAspect>
61
62#include <Qt3DRender/QRenderStateSet>
63#include <Qt3DRender/QRenderAspect>
64#include <Qt3DExtras/QForwardRenderer>
65#include <Qt3DExtras/QPerVertexColorMaterial>
66
67#include <Qt3DRender/QGeometryRenderer>
68#include <Qt3DRender/QGeometry>
69#include <Qt3DRender/QAttribute>
70#include <Qt3DRender/QBuffer>
71
72#include <QPropertyAnimation>
73#include <Qt3DExtras/qt3dwindow.h>
74#include <Qt3DExtras/qfirstpersoncameracontroller.h>
75#include <qmath.h>
76
77QByteArray vertexBufferData;
78Qt3DRender::QBuffer *vertexDataBuffer;
79
80class TimerObject: public QObject {
81 Q_OBJECT
82
83public:
84 TimerObject(QObject *parent = 0): QObject(parent) {
85 }
86 ~TimerObject() {
87 }
88private slots:
89 void timeout();
90private:
91 float angle = 0;
92};
93
94int main(int argc, char* argv[])
95{
96 QGuiApplication app(argc, argv);
97 Qt3DExtras::Qt3DWindow view;
98 view.defaultFrameGraph()->setClearColor(QColor::fromRgbF(r: 0.0, g: 0.5, b: 1.0, a: 1.0));
99
100 // Root entity
101 Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity();
102
103 // Camera
104 Qt3DRender::QCamera *cameraEntity = view.camera();
105
106 cameraEntity->lens()->setPerspectiveProjection(fieldOfView: 45.0f, aspect: 16.0f/9.0f, nearPlane: 0.1f, farPlane: 1000.0f);
107 cameraEntity->setPosition(QVector3D(50.0f, 20.0f, 40.0f));
108 cameraEntity->setUpVector(QVector3D(0, 1, 0));
109 cameraEntity->setViewCenter(QVector3D(0, 0, 0));
110
111 // For camera controls
112 Qt3DExtras::QFirstPersonCameraController *camController = new Qt3DExtras::QFirstPersonCameraController(rootEntity);
113 camController->setCamera(cameraEntity);
114
115 // Material
116 Qt3DRender::QMaterial *material = new Qt3DExtras::QPerVertexColorMaterial(rootEntity);
117
118 // Torus
119 Qt3DCore::QEntity *customMeshEntity = new Qt3DCore::QEntity(rootEntity);
120
121 // Transform
122 Qt3DCore::QTransform *transform = new Qt3DCore::QTransform;
123 transform->setScale(20.0f);
124
125 // Custom Mesh (TetraHedron)
126 Qt3DRender::QGeometryRenderer *customMeshRenderer = new Qt3DRender::QGeometryRenderer;
127 Qt3DRender::QGeometry *customGeometry = new Qt3DRender::QGeometry(customMeshRenderer);
128
129 vertexDataBuffer = new Qt3DRender::QBuffer(customGeometry);
130 Qt3DRender::QBuffer *indexDataBuffer = new Qt3DRender::QBuffer(customGeometry);
131
132 // vec3 for position
133 // vec3 for colors
134 // vec3 for normals
135
136 /* 2
137 /|\
138 / | \
139 / /3\ \
140 0/___\ 1
141 */
142
143 // 4 distinct vertices
144 vertexBufferData.resize(size: 4 * (3 + 3 + 3) * sizeof(float));
145
146 // Vertices
147 QVector3D v0(-1.0f, 0.0f, -1.0f);
148 QVector3D v1(1.0f, 0.0f, -1.0f);
149 QVector3D v2(0.0f, 1.0f, 0.0f);
150 QVector3D v3(0.0f, 0.0f, 1.0f);
151
152 // Faces Normals
153 QVector3D n023 = QVector3D::normal(v1: v0, v2, v3);
154 QVector3D n012 = QVector3D::normal(v1: v0, v2: v1, v3: v2);
155 QVector3D n310 = QVector3D::normal(v1: v3, v2: v1, v3: v0);
156 QVector3D n132 = QVector3D::normal(v1, v2: v3, v3: v2);
157
158 // Vector Normals
159 QVector3D n0 = QVector3D(n023 + n012 + n310).normalized();
160 QVector3D n1 = QVector3D(n132 + n012 + n310).normalized();
161 QVector3D n2 = QVector3D(n132 + n012 + n023).normalized();
162 QVector3D n3 = QVector3D(n132 + n310 + n023).normalized();
163
164 // Colors
165 QVector3D red(1.0f, 0.0f, 0.0f);
166 QVector3D green(0.0f, 1.0f, 0.0f);
167 QVector3D blue(0.0f, 0.0f, 1.0f);
168 QVector3D white(1.0f, 1.0f, 1.0f);
169
170 const QVector<QVector3D> vertices = QVector<QVector3D>()
171 << v0 << n0 << red
172 << v1 << n1 << blue
173 << v2 << n2 << green
174 << v3 << n3 << white;
175
176 float *rawVertexArray = reinterpret_cast<float *>(vertexBufferData.data());
177 int idx = 0;
178
179 for (const QVector3D &v : vertices) {
180 rawVertexArray[idx++] = v.x();
181 rawVertexArray[idx++] = v.y();
182 rawVertexArray[idx++] = v.z();
183 }
184
185 // Indices (12)
186 QByteArray indexBufferData;
187 indexBufferData.resize(size: 4 * 3 * sizeof(ushort));
188 ushort *rawIndexArray = reinterpret_cast<ushort *>(indexBufferData.data());
189
190 // Front
191 rawIndexArray[0] = 0;
192 rawIndexArray[1] = 1;
193 rawIndexArray[2] = 2;
194 // Bottom
195 rawIndexArray[3] = 3;
196 rawIndexArray[4] = 1;
197 rawIndexArray[5] = 0;
198 // Left
199 rawIndexArray[6] = 0;
200 rawIndexArray[7] = 2;
201 rawIndexArray[8] = 3;
202 // Right
203 rawIndexArray[9] = 1;
204 rawIndexArray[10] = 3;
205 rawIndexArray[11] = 2;
206
207 vertexDataBuffer->setData(vertexBufferData);
208 indexDataBuffer->setData(indexBufferData);
209
210 // Attributes
211 Qt3DRender::QAttribute *positionAttribute = new Qt3DRender::QAttribute();
212 positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
213 positionAttribute->setBuffer(vertexDataBuffer);
214 positionAttribute->setVertexSize(3);
215 positionAttribute->setByteOffset(0);
216 positionAttribute->setByteStride(9 * sizeof(float));
217 positionAttribute->setCount(4);
218 positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
219
220 Qt3DRender::QAttribute *normalAttribute = new Qt3DRender::QAttribute();
221 normalAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
222 normalAttribute->setBuffer(vertexDataBuffer);
223 normalAttribute->setVertexSize(3);
224 normalAttribute->setByteOffset(3 * sizeof(float));
225 normalAttribute->setByteStride(9 * sizeof(float));
226 normalAttribute->setCount(4);
227 normalAttribute->setName(Qt3DRender::QAttribute::defaultNormalAttributeName());
228
229 Qt3DRender::QAttribute *colorAttribute = new Qt3DRender::QAttribute();
230 colorAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
231 colorAttribute->setBuffer(vertexDataBuffer);
232 colorAttribute->setVertexSize(3);
233 colorAttribute->setByteOffset(6 * sizeof(float));
234 colorAttribute->setByteStride(9 * sizeof(float));
235 colorAttribute->setCount(4);
236 colorAttribute->setName(Qt3DRender::QAttribute::defaultColorAttributeName());
237
238 Qt3DRender::QAttribute *indexAttribute = new Qt3DRender::QAttribute();
239 indexAttribute->setAttributeType(Qt3DRender::QAttribute::IndexAttribute);
240 indexAttribute->setBuffer(indexDataBuffer);
241 indexAttribute->setVertexBaseType(Qt3DRender::QAttribute::UnsignedShort);
242 indexAttribute->setVertexSize(1);
243 indexAttribute->setByteOffset(0);
244 indexAttribute->setByteStride(0);
245 indexAttribute->setCount(12);
246
247 customGeometry->addAttribute(attribute: positionAttribute);
248 customGeometry->addAttribute(attribute: normalAttribute);
249 customGeometry->addAttribute(attribute: colorAttribute);
250 customGeometry->addAttribute(attribute: indexAttribute);
251
252 customMeshRenderer->setInstanceCount(1);
253 customMeshRenderer->setIndexOffset(0);
254 customMeshRenderer->setFirstInstance(0);
255 customMeshRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles);
256 customMeshRenderer->setGeometry(customGeometry);
257 // 4 faces of 3 points
258 customMeshRenderer->setVertexCount(12);
259
260 customMeshEntity->addComponent(comp: customMeshRenderer);
261 customMeshEntity->addComponent(comp: transform);
262 customMeshEntity->addComponent(comp: material);
263
264 view.setRootEntity(rootEntity);
265 view.show();
266
267 QTimer *timer = new QTimer();
268 TimerObject *timerObject = new TimerObject();
269 QObject::connect(sender: timer, SIGNAL(timeout()), receiver: timerObject, SLOT(timeout()));
270 timer->start(msec: 10);
271
272 return app.exec();
273}
274
275void TimerObject::timeout()
276{
277 angle += qDegreesToRadians(degrees: 0.5f);
278
279 QByteArray updateData;
280 updateData.resize(size: 3*sizeof(float));
281
282 // Colors
283 QVector3D c1(qFabs(v: qCos(v: angle + M_PI_4)), qFabs(v: qSin(v: angle)), qFabs(v: qCos(v: angle)));
284 QVector3D c2(qFabs(v: qSin(v: angle)), qFabs(v: qCos(v: angle + M_PI_4)), qFabs(v: qSin(v: angle + M_PI_4)));
285 QVector3D c3(qFabs(v: qSin(v: angle + M_PI_4)), qFabs(v: qSin(v: angle)), qFabs(v: qCos(v: angle)));
286
287 const QVector<QVector3D> colors = QVector<QVector3D>() << c1 << c2 << c3;
288
289 float *rawVertexArray = reinterpret_cast<float *>(updateData.data());
290
291 int pos = 6 * sizeof(float); //color offset
292 for (const QVector3D &v : colors) {
293 rawVertexArray[0] = v.x();
294 rawVertexArray[1] = v.y();
295 rawVertexArray[2] = v.z();
296 vertexDataBuffer->updateData(offset: pos,bytes: updateData);
297 pos += 9 * sizeof(float); //stride
298 }
299}
300
301#include "main.moc"
302

source code of qt3d/tests/manual/custom-mesh-update-data-cpp/main.cpp