1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
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:GPL-EXCEPT$
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** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <QtTest/QTest>
30#include <qbackendnodetester.h>
31#include <Qt3DRender/qgeometryrenderer.h>
32#include <Qt3DRender/qbuffer.h>
33#include <private/trianglesvisitor_p.h>
34#include <private/nodemanagers_p.h>
35#include <private/managers_p.h>
36#include <private/geometryrenderer_p.h>
37#include <private/geometryrenderermanager_p.h>
38#include <private/buffermanager_p.h>
39#include "testrenderer.h"
40
41using namespace Qt3DRender::Render;
42
43class TestVisitor : public TrianglesVisitor
44{
45public:
46 TestVisitor(NodeManagers *manager)
47 : TrianglesVisitor(manager)
48 {
49
50 }
51
52 virtual void visit(uint andx, const Vector3D &a, uint bndx, const Vector3D &b, uint cndx, const Vector3D &c)
53 {
54 m_triangles.push_back(t: TestTriangle(andx, a, bndx, b, cndx, c));
55 }
56
57 NodeManagers *nodeManagers() const
58 {
59 return m_manager;
60 }
61
62 Qt3DCore::QNodeId nodeId() const
63 {
64 return m_nodeId;
65 }
66
67 uint triangleCount() const
68 {
69 return m_triangles.size();
70 }
71
72 bool verifyTriangle(uint triangle, uint andx, uint bndx, uint cndx, Vector3D a, Vector3D b, Vector3D c) const
73 {
74 if (triangle >= uint(m_triangles.size()))
75 return false;
76 if (andx != m_triangles[triangle].abcndx[0]
77 || bndx != m_triangles[triangle].abcndx[1]
78 || cndx != m_triangles[triangle].abcndx[2])
79 return false;
80
81 if (!qFuzzyCompare(v1: a, v2: m_triangles[triangle].abc[0])
82 || !qFuzzyCompare(v1: b, v2: m_triangles[triangle].abc[1])
83 || !qFuzzyCompare(v1: c, v2: m_triangles[triangle].abc[2]))
84 return false;
85
86 return true;
87 }
88private:
89 struct TestTriangle
90 {
91 uint abcndx[3];
92 Vector3D abc[3];
93 TestTriangle()
94 {
95 abcndx[0] = abcndx[1] = abcndx[2] = uint(-1);
96 }
97
98 TestTriangle(uint andx, const Vector3D &a, uint bndx, const Vector3D &b, uint cndx, const Vector3D &c)
99 {
100 abcndx[0] = andx;
101 abcndx[1] = bndx;
102 abcndx[2] = cndx;
103 abc[0] = a;
104 abc[1] = b;
105 abc[2] = c;
106 }
107 };
108 QVector<TestTriangle> m_triangles;
109};
110
111class tst_TriangleVisitor : public Qt3DCore::QBackendNodeTester
112{
113 Q_OBJECT
114
115private Q_SLOTS:
116
117 void checkInitialize()
118 {
119 // WHEN
120 QScopedPointer<NodeManagers> nodeManagers(new NodeManagers());
121 TestVisitor visitor(nodeManagers.data());
122
123 // THEN
124 QCOMPARE(visitor.nodeManagers(), nodeManagers.data());
125 }
126
127 void checkApplyEntity()
128 {
129 QScopedPointer<NodeManagers> nodeManagers(new NodeManagers());
130 QScopedPointer<Qt3DCore::QEntity> entity(new Qt3DCore::QEntity());
131 TestVisitor visitor(nodeManagers.data());
132
133 // WHEN
134 visitor.apply(entity: entity.data());
135
136 // THEN
137 QCOMPARE(visitor.nodeId(), entity->id());
138 QVERIFY(visitor.triangleCount() == 0);
139 }
140
141 void checkApplyGeometryRenderer()
142 {
143 QScopedPointer<NodeManagers> nodeManagers(new NodeManagers());
144 QScopedPointer<GeometryRenderer> geometryRenderer(new GeometryRenderer());
145 TestVisitor visitor(nodeManagers.data());
146
147 // WHEN
148 visitor.apply(renderer: geometryRenderer.data(), id: Qt3DCore::QNodeId());
149
150 // THEN
151 // tadaa, nothing should happen
152 }
153
154 void testVisitTriangles()
155 {
156 QScopedPointer<NodeManagers> nodeManagers(new NodeManagers());
157 Qt3DRender::QGeometry *geometry = new Qt3DRender::QGeometry();
158 QScopedPointer<Qt3DRender::QGeometryRenderer> geometryRenderer(new Qt3DRender::QGeometryRenderer());
159 QScopedPointer<Qt3DRender::QAttribute> positionAttribute(new Qt3DRender::QAttribute());
160 QScopedPointer<Qt3DRender::QBuffer> dataBuffer(new Qt3DRender::QBuffer());
161 TestVisitor visitor(nodeManagers.data());
162 TestRenderer renderer;
163
164 QByteArray data;
165 data.resize(size: sizeof(float) * 3 * 3 * 2);
166 float *dataPtr = reinterpret_cast<float *>(data.data());
167 dataPtr[0] = 0;
168 dataPtr[1] = 0;
169 dataPtr[2] = 1.0f;
170 dataPtr[3] = 1.0f;
171 dataPtr[4] = 0;
172 dataPtr[5] = 0;
173 dataPtr[6] = 0;
174 dataPtr[7] = 1.0f;
175 dataPtr[8] = 0;
176
177 dataPtr[9] = 0;
178 dataPtr[10] = 0;
179 dataPtr[11] = 1.0f;
180 dataPtr[12] = 1.0f;
181 dataPtr[13] = 0;
182 dataPtr[14] = 0;
183 dataPtr[15] = 0;
184 dataPtr[16] = 1.0f;
185 dataPtr[17] = 0;
186 dataBuffer->setData(data);
187 Buffer *backendBuffer = nodeManagers->bufferManager()->getOrCreateResource(id: dataBuffer->id());
188 backendBuffer->setRenderer(&renderer);
189 backendBuffer->setManager(nodeManagers->bufferManager());
190 simulateInitializationSync(frontend: dataBuffer.data(), backend: backendBuffer);
191
192 positionAttribute->setBuffer(dataBuffer.data());
193 positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
194 positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
195 positionAttribute->setVertexSize(3);
196 positionAttribute->setCount(6);
197 positionAttribute->setByteStride(0);
198 positionAttribute->setByteOffset(0);
199 positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
200 geometry->addAttribute(attribute: positionAttribute.data());
201
202 geometryRenderer->setGeometry(geometry);
203 geometryRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles);
204
205 Attribute *backendAttribute = nodeManagers->attributeManager()->getOrCreateResource(id: positionAttribute->id());
206 backendAttribute->setRenderer(&renderer);
207 simulateInitializationSync(frontend: positionAttribute.data(), backend: backendAttribute);
208
209 Geometry *backendGeometry = nodeManagers->geometryManager()->getOrCreateResource(id: geometry->id());
210 backendGeometry->setRenderer(&renderer);
211 simulateInitializationSync(frontend: geometry, backend: backendGeometry);
212
213 GeometryRenderer *backendRenderer = nodeManagers->geometryRendererManager()->getOrCreateResource(id: geometryRenderer->id());
214 backendRenderer->setRenderer(&renderer);
215 backendRenderer->setManager(nodeManagers->geometryRendererManager());
216 simulateInitializationSync(frontend: geometryRenderer.data(), backend: backendRenderer);
217
218 // WHEN
219 visitor.apply(renderer: backendRenderer, id: Qt3DCore::QNodeId());
220
221 // THEN
222 QVERIFY(visitor.triangleCount() == 2);
223 QVERIFY(visitor.verifyTriangle(0, 2,1,0, Vector3D(0,1,0), Vector3D(1,0,0), Vector3D(0,0,1)));
224 QVERIFY(visitor.verifyTriangle(1, 5,4,3, Vector3D(0,1,0), Vector3D(1,0,0), Vector3D(0,0,1)));
225 }
226
227 void testVisitTrianglesIndexed()
228 {
229 QScopedPointer<NodeManagers> nodeManagers(new NodeManagers());
230 Qt3DRender::QGeometry *geometry = new Qt3DRender::QGeometry();
231 QScopedPointer<Qt3DRender::QGeometryRenderer> geometryRenderer(new Qt3DRender::QGeometryRenderer());
232 QScopedPointer<Qt3DRender::QAttribute> positionAttribute(new Qt3DRender::QAttribute());
233 QScopedPointer<Qt3DRender::QAttribute> indexAttribute(new Qt3DRender::QAttribute());
234 QScopedPointer<Qt3DRender::QBuffer> dataBuffer(new Qt3DRender::QBuffer());
235 QScopedPointer<Qt3DRender::QBuffer> indexDataBuffer(new Qt3DRender::QBuffer());
236 TestVisitor visitor(nodeManagers.data());
237 TestRenderer renderer;
238
239 QByteArray data;
240 data.resize(size: sizeof(float) * 3 * 3 * 2);
241 float *dataPtr = reinterpret_cast<float *>(data.data());
242 dataPtr[0] = 0;
243 dataPtr[1] = 0;
244 dataPtr[2] = 1.0f;
245 dataPtr[3] = 1.0f;
246 dataPtr[4] = 0;
247 dataPtr[5] = 0;
248 dataPtr[6] = 0;
249 dataPtr[7] = 1.0f;
250 dataPtr[8] = 0;
251
252 dataPtr[9] = 0;
253 dataPtr[10] = 0;
254 dataPtr[11] = 1.0f;
255 dataPtr[12] = 1.0f;
256 dataPtr[13] = 0;
257 dataPtr[14] = 0;
258 dataPtr[15] = 0;
259 dataPtr[16] = 1.0f;
260 dataPtr[17] = 0;
261 dataBuffer->setData(data);
262 Buffer *backendBuffer = nodeManagers->bufferManager()->getOrCreateResource(id: dataBuffer->id());
263 backendBuffer->setRenderer(&renderer);
264 backendBuffer->setManager(nodeManagers->bufferManager());
265 simulateInitializationSync(frontend: dataBuffer.data(), backend: backendBuffer);
266
267 QByteArray indexData;
268 indexData.resize(size: sizeof(uint) * 3 * 5);
269 uint *iDataPtr = reinterpret_cast<uint *>(indexData.data());
270 iDataPtr[0] = 0;
271 iDataPtr[1] = 1;
272 iDataPtr[2] = 2;
273 iDataPtr[3] = 3;
274 iDataPtr[4] = 4;
275 iDataPtr[5] = 5;
276 iDataPtr[6] = 5;
277 iDataPtr[7] = 1;
278 iDataPtr[8] = 0;
279 iDataPtr[9] = 4;
280 iDataPtr[10] = 3;
281 iDataPtr[11] = 2;
282 iDataPtr[12] = 0;
283 iDataPtr[13] = 2;
284 iDataPtr[14] = 4;
285 indexDataBuffer->setData(indexData);
286
287 Buffer *backendIndexBuffer = nodeManagers->bufferManager()->getOrCreateResource(id: indexDataBuffer->id());
288 backendIndexBuffer->setRenderer(&renderer);
289 backendIndexBuffer->setManager(nodeManagers->bufferManager());
290 simulateInitializationSync(frontend: indexDataBuffer.data(), backend: backendIndexBuffer);
291
292 positionAttribute->setBuffer(dataBuffer.data());
293 positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
294 positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
295 positionAttribute->setVertexSize(3);
296 positionAttribute->setCount(6);
297 positionAttribute->setByteStride(3*sizeof(float));
298 positionAttribute->setByteOffset(0);
299 positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
300
301 indexAttribute->setBuffer(indexDataBuffer.data());
302 indexAttribute->setVertexBaseType(Qt3DRender::QAttribute::UnsignedInt);
303 indexAttribute->setCount(3*5);
304 indexAttribute->setAttributeType(Qt3DRender::QAttribute::IndexAttribute);
305
306 geometry->addAttribute(attribute: positionAttribute.data());
307 geometry->addAttribute(attribute: indexAttribute.data());
308
309 geometryRenderer->setGeometry(geometry);
310 geometryRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles);
311
312 Attribute *backendAttribute = nodeManagers->attributeManager()->getOrCreateResource(id: positionAttribute->id());
313 backendAttribute->setRenderer(&renderer);
314 simulateInitializationSync(frontend: positionAttribute.data(), backend: backendAttribute);
315
316 Attribute *backendIndexAttribute = nodeManagers->attributeManager()->getOrCreateResource(id: indexAttribute->id());
317 backendIndexAttribute->setRenderer(&renderer);
318 simulateInitializationSync(frontend: indexAttribute.data(), backend: backendIndexAttribute);
319
320 Geometry *backendGeometry = nodeManagers->geometryManager()->getOrCreateResource(id: geometry->id());
321 backendGeometry->setRenderer(&renderer);
322 simulateInitializationSync(frontend: geometry, backend: backendGeometry);
323
324 GeometryRenderer *backendRenderer = nodeManagers->geometryRendererManager()->getOrCreateResource(id: geometryRenderer->id());
325 backendRenderer->setRenderer(&renderer);
326 backendRenderer->setManager(nodeManagers->geometryRendererManager());
327 simulateInitializationSync(frontend: geometryRenderer.data(), backend: backendRenderer);
328
329 // WHEN
330 visitor.apply(renderer: backendRenderer, id: Qt3DCore::QNodeId());
331
332 // THEN
333 QVERIFY(visitor.triangleCount() == 5);
334 QVERIFY(visitor.verifyTriangle(0, 2,1,0, Vector3D(0,1,0), Vector3D(1,0,0), Vector3D(0,0,1)));
335 QVERIFY(visitor.verifyTriangle(1, 5,4,3, Vector3D(0,1,0), Vector3D(1,0,0), Vector3D(0,0,1)));
336 QVERIFY(visitor.verifyTriangle(2, 0,1,5, Vector3D(0,0,1), Vector3D(1,0,0), Vector3D(0,1,0)));
337 QVERIFY(visitor.verifyTriangle(3, 2,3,4, Vector3D(0,1,0), Vector3D(0,0,1), Vector3D(1,0,0)));
338 QVERIFY(visitor.verifyTriangle(4, 4,2,0, Vector3D(1,0,0), Vector3D(0,1,0), Vector3D(0,0,1)));
339 }
340
341 void testVisitTriangleStrip()
342 {
343 QScopedPointer<NodeManagers> nodeManagers(new NodeManagers());
344 Qt3DRender::QGeometry *geometry = new Qt3DRender::QGeometry();
345 QScopedPointer<Qt3DRender::QGeometryRenderer> geometryRenderer(new Qt3DRender::QGeometryRenderer());
346 QScopedPointer<Qt3DRender::QAttribute> positionAttribute(new Qt3DRender::QAttribute());
347 QScopedPointer<Qt3DRender::QBuffer> dataBuffer(new Qt3DRender::QBuffer());
348 TestVisitor visitor(nodeManagers.data());
349 TestRenderer renderer;
350
351 QByteArray data;
352 data.resize(size: sizeof(float) * 3 * 3 * 2);
353 float *dataPtr = reinterpret_cast<float *>(data.data());
354 dataPtr[0] = 0;
355 dataPtr[1] = 0;
356 dataPtr[2] = 1.0f;
357 dataPtr[3] = 1.0f;
358 dataPtr[4] = 0;
359 dataPtr[5] = 0;
360 dataPtr[6] = 0;
361 dataPtr[7] = 1.0f;
362 dataPtr[8] = 0;
363
364 dataPtr[9] = 0;
365 dataPtr[10] = 0;
366 dataPtr[11] = 1.0f;
367 dataPtr[12] = 1.0f;
368 dataPtr[13] = 0;
369 dataPtr[14] = 0;
370 dataPtr[15] = 0;
371 dataPtr[16] = 1.0f;
372 dataPtr[17] = 0;
373 dataBuffer->setData(data);
374 Buffer *backendBuffer = nodeManagers->bufferManager()->getOrCreateResource(id: dataBuffer->id());
375 backendBuffer->setRenderer(&renderer);
376 backendBuffer->setManager(nodeManagers->bufferManager());
377 simulateInitializationSync(frontend: dataBuffer.data(), backend: backendBuffer);
378
379 positionAttribute->setBuffer(dataBuffer.data());
380 positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
381 positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
382 positionAttribute->setVertexSize(3);
383 positionAttribute->setCount(6);
384 positionAttribute->setByteStride(3*4);
385 positionAttribute->setByteOffset(0);
386 positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
387 geometry->addAttribute(attribute: positionAttribute.data());
388
389 geometryRenderer->setGeometry(geometry);
390 geometryRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::TriangleStrip);
391
392 Attribute *backendAttribute = nodeManagers->attributeManager()->getOrCreateResource(id: positionAttribute->id());
393 backendAttribute->setRenderer(&renderer);
394 simulateInitializationSync(frontend: positionAttribute.data(), backend: backendAttribute);
395
396 Geometry *backendGeometry = nodeManagers->geometryManager()->getOrCreateResource(id: geometry->id());
397 backendGeometry->setRenderer(&renderer);
398 simulateInitializationSync(frontend: geometry, backend: backendGeometry);
399
400 GeometryRenderer *backendRenderer = nodeManagers->geometryRendererManager()->getOrCreateResource(id: geometryRenderer->id());
401 backendRenderer->setRenderer(&renderer);
402 backendRenderer->setManager(nodeManagers->geometryRendererManager());
403 simulateInitializationSync(frontend: geometryRenderer.data(), backend: backendRenderer);
404
405 // WHEN
406 visitor.apply(renderer: backendRenderer, id: Qt3DCore::QNodeId());
407
408 // THEN
409 QVERIFY(visitor.triangleCount() == 4);
410 QVERIFY(visitor.verifyTriangle(0, 2,1,0, Vector3D(0,1,0), Vector3D(1,0,0), Vector3D(0,0,1)));
411 QVERIFY(visitor.verifyTriangle(1, 3,2,1, Vector3D(0,0,1), Vector3D(0,1,0), Vector3D(1,0,0)));
412 QVERIFY(visitor.verifyTriangle(2, 4,3,2, Vector3D(1,0,0), Vector3D(0,0,1), Vector3D(0,1,0)));
413 QVERIFY(visitor.verifyTriangle(3, 5,4,3, Vector3D(0,1,0), Vector3D(1,0,0), Vector3D(0,0,1)));
414 }
415
416 void testVisitTriangleStripIndexed()
417 {
418 QScopedPointer<NodeManagers> nodeManagers(new NodeManagers());
419 Qt3DRender::QGeometry *geometry = new Qt3DRender::QGeometry();
420 QScopedPointer<Qt3DRender::QGeometryRenderer> geometryRenderer(new Qt3DRender::QGeometryRenderer());
421 QScopedPointer<Qt3DRender::QAttribute> positionAttribute(new Qt3DRender::QAttribute());
422 QScopedPointer<Qt3DRender::QAttribute> indexAttribute(new Qt3DRender::QAttribute());
423 QScopedPointer<Qt3DRender::QBuffer> dataBuffer(new Qt3DRender::QBuffer());
424 QScopedPointer<Qt3DRender::QBuffer> indexDataBuffer(new Qt3DRender::QBuffer());
425 TestVisitor visitor(nodeManagers.data());
426 TestRenderer renderer;
427
428 QByteArray data;
429 data.resize(size: sizeof(float) * 3 * 3 * 2);
430 float *dataPtr = reinterpret_cast<float *>(data.data());
431 dataPtr[0] = 0;
432 dataPtr[1] = 0;
433 dataPtr[2] = 1.0f;
434 dataPtr[3] = 1.0f;
435 dataPtr[4] = 0;
436 dataPtr[5] = 0;
437 dataPtr[6] = 0;
438 dataPtr[7] = 1.0f;
439 dataPtr[8] = 0;
440
441 dataPtr[9] = 0;
442 dataPtr[10] = 0;
443 dataPtr[11] = 1.0f;
444 dataPtr[12] = 1.0f;
445 dataPtr[13] = 0;
446 dataPtr[14] = 0;
447 dataPtr[15] = 0;
448 dataPtr[16] = 1.0f;
449 dataPtr[17] = 0;
450 dataBuffer->setData(data);
451 Buffer *backendBuffer = nodeManagers->bufferManager()->getOrCreateResource(id: dataBuffer->id());
452 backendBuffer->setRenderer(&renderer);
453 backendBuffer->setManager(nodeManagers->bufferManager());
454 simulateInitializationSync(frontend: dataBuffer.data(), backend: backendBuffer);
455
456 QByteArray indexData;
457 indexData.resize(size: sizeof(uint) * 4 * 4);
458 uint *iDataPtr = reinterpret_cast<uint *>(indexData.data());
459 iDataPtr[0] = 0;
460 iDataPtr[1] = 1;
461 iDataPtr[2] = 2;
462 iDataPtr[3] = 3;
463 iDataPtr[4] = 4;
464 iDataPtr[5] = 5;
465 iDataPtr[6] = 5;
466 iDataPtr[7] = 1;
467 iDataPtr[8] = 0;
468 iDataPtr[9] = 4;
469 iDataPtr[10] = 3;
470 iDataPtr[11] = 2;
471 iDataPtr[12] = static_cast<uint>(-1);
472 iDataPtr[13] = 0;
473 iDataPtr[14] = 1;
474 iDataPtr[15] = 2;
475 indexDataBuffer->setData(indexData);
476
477 Buffer *backendIndexBuffer = nodeManagers->bufferManager()->getOrCreateResource(id: indexDataBuffer->id());
478 backendIndexBuffer->setRenderer(&renderer);
479 backendIndexBuffer->setManager(nodeManagers->bufferManager());
480 simulateInitializationSync(frontend: indexDataBuffer.data(), backend: backendIndexBuffer);
481
482 positionAttribute->setBuffer(dataBuffer.data());
483 positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
484 positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
485 positionAttribute->setVertexSize(3);
486 positionAttribute->setCount(6);
487 positionAttribute->setByteStride(3*sizeof(float));
488 positionAttribute->setByteOffset(0);
489 positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
490
491 indexAttribute->setBuffer(indexDataBuffer.data());
492 indexAttribute->setVertexBaseType(Qt3DRender::QAttribute::UnsignedInt);
493 indexAttribute->setCount(4*4);
494 indexAttribute->setAttributeType(Qt3DRender::QAttribute::IndexAttribute);
495
496 geometry->addAttribute(attribute: positionAttribute.data());
497 geometry->addAttribute(attribute: indexAttribute.data());
498
499 geometryRenderer->setGeometry(geometry);
500 geometryRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::TriangleStrip);
501 geometryRenderer->setPrimitiveRestartEnabled(true);
502 geometryRenderer->setRestartIndexValue(-1);
503
504 Attribute *backendAttribute = nodeManagers->attributeManager()->getOrCreateResource(id: positionAttribute->id());
505 backendAttribute->setRenderer(&renderer);
506 simulateInitializationSync(frontend: positionAttribute.data(), backend: backendAttribute);
507
508 Attribute *backendIndexAttribute = nodeManagers->attributeManager()->getOrCreateResource(id: indexAttribute->id());
509 backendIndexAttribute->setRenderer(&renderer);
510 simulateInitializationSync(frontend: indexAttribute.data(), backend: backendIndexAttribute);
511
512 Geometry *backendGeometry = nodeManagers->geometryManager()->getOrCreateResource(id: geometry->id());
513 backendGeometry->setRenderer(&renderer);
514 simulateInitializationSync(frontend: geometry, backend: backendGeometry);
515
516 GeometryRenderer *backendRenderer = nodeManagers->geometryRendererManager()->getOrCreateResource(id: geometryRenderer->id());
517 backendRenderer->setRenderer(&renderer);
518 backendRenderer->setManager(nodeManagers->geometryRendererManager());
519 simulateInitializationSync(frontend: geometryRenderer.data(), backend: backendRenderer);
520
521 // WHEN
522 visitor.apply(renderer: backendRenderer, id: Qt3DCore::QNodeId());
523
524 // THEN
525 QCOMPARE(visitor.triangleCount(), 9U);
526 QVERIFY(visitor.verifyTriangle(0, 2,1,0, Vector3D(0,1,0), Vector3D(1,0,0), Vector3D(0,0,1)));
527 QVERIFY(visitor.verifyTriangle(1, 3,2,1, Vector3D(0,0,1), Vector3D(0,1,0), Vector3D(1,0,0)));
528 QVERIFY(visitor.verifyTriangle(2, 4,3,2, Vector3D(1,0,0), Vector3D(0,0,1), Vector3D(0,1,0)));
529 QVERIFY(visitor.verifyTriangle(3, 5,4,3, Vector3D(0,1,0), Vector3D(1,0,0), Vector3D(0,0,1)));
530 QVERIFY(visitor.verifyTriangle(4, 0,1,5, Vector3D(0,0,1), Vector3D(1,0,0), Vector3D(0,1,0)));
531 QVERIFY(visitor.verifyTriangle(5, 4,0,1, Vector3D(1,0,0), Vector3D(0,0,1), Vector3D(1,0,0)));
532 QVERIFY(visitor.verifyTriangle(6, 3,4,0, Vector3D(0,0,1), Vector3D(1,0,0), Vector3D(0,0,1)));
533 QVERIFY(visitor.verifyTriangle(7, 2,3,4, Vector3D(0,1,0), Vector3D(0,0,1), Vector3D(1,0,0)));
534 QVERIFY(visitor.verifyTriangle(8, 2,1,0, Vector3D(0,1,0), Vector3D(1,0,0), Vector3D(0,0,1)));
535 }
536
537 void testVisitTriangleFan()
538 {
539 QScopedPointer<NodeManagers> nodeManagers(new NodeManagers());
540 Qt3DRender::QGeometry *geometry = new Qt3DRender::QGeometry();
541 QScopedPointer<Qt3DRender::QGeometryRenderer> geometryRenderer(new Qt3DRender::QGeometryRenderer());
542 QScopedPointer<Qt3DRender::QAttribute> positionAttribute(new Qt3DRender::QAttribute());
543 QScopedPointer<Qt3DRender::QBuffer> dataBuffer(new Qt3DRender::QBuffer());
544 TestVisitor visitor(nodeManagers.data());
545 TestRenderer renderer;
546
547 QByteArray data;
548 data.resize(size: sizeof(float) * 3 * 3 * 2);
549 float *dataPtr = reinterpret_cast<float *>(data.data());
550 dataPtr[0] = 0;
551 dataPtr[1] = 0;
552 dataPtr[2] = 1.0f;
553 dataPtr[3] = 1.0f;
554 dataPtr[4] = 0;
555 dataPtr[5] = 0;
556 dataPtr[6] = 0;
557 dataPtr[7] = 1.0f;
558 dataPtr[8] = 0;
559
560 dataPtr[9] = 0;
561 dataPtr[10] = 0;
562 dataPtr[11] = 1.0f;
563 dataPtr[12] = 1.0f;
564 dataPtr[13] = 0;
565 dataPtr[14] = 0;
566 dataPtr[15] = 0;
567 dataPtr[16] = 1.0f;
568 dataPtr[17] = 0;
569 dataBuffer->setData(data);
570 Buffer *backendBuffer = nodeManagers->bufferManager()->getOrCreateResource(id: dataBuffer->id());
571 backendBuffer->setRenderer(&renderer);
572 backendBuffer->setManager(nodeManagers->bufferManager());
573 simulateInitializationSync(frontend: dataBuffer.data(), backend: backendBuffer);
574
575 positionAttribute->setBuffer(dataBuffer.data());
576 positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
577 positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
578 positionAttribute->setVertexSize(3);
579 positionAttribute->setCount(6);
580 positionAttribute->setByteStride(3*4);
581 positionAttribute->setByteOffset(0);
582 positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
583 geometry->addAttribute(attribute: positionAttribute.data());
584
585 geometryRenderer->setGeometry(geometry);
586 geometryRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::TriangleFan);
587
588 Attribute *backendAttribute = nodeManagers->attributeManager()->getOrCreateResource(id: positionAttribute->id());
589 backendAttribute->setRenderer(&renderer);
590 simulateInitializationSync(frontend: positionAttribute.data(), backend: backendAttribute);
591
592 Geometry *backendGeometry = nodeManagers->geometryManager()->getOrCreateResource(id: geometry->id());
593 backendGeometry->setRenderer(&renderer);
594 simulateInitializationSync(frontend: geometry, backend: backendGeometry);
595
596 GeometryRenderer *backendRenderer = nodeManagers->geometryRendererManager()->getOrCreateResource(id: geometryRenderer->id());
597 backendRenderer->setRenderer(&renderer);
598 backendRenderer->setManager(nodeManagers->geometryRendererManager());
599 simulateInitializationSync(frontend: geometryRenderer.data(), backend: backendRenderer);
600
601 // WHEN
602 visitor.apply(renderer: backendRenderer, id: Qt3DCore::QNodeId());
603
604 // THEN
605 QVERIFY(visitor.triangleCount() == 4);
606 QVERIFY(visitor.verifyTriangle(0, 2,1,0, Vector3D(0,1,0), Vector3D(1,0,0), Vector3D(0,0,1)));
607 QVERIFY(visitor.verifyTriangle(1, 3,2,0, Vector3D(0,0,1), Vector3D(0,1,0), Vector3D(0,0,1)));
608 QVERIFY(visitor.verifyTriangle(2, 4,3,0, Vector3D(1,0,0), Vector3D(0,0,1), Vector3D(0,0,1)));
609 QVERIFY(visitor.verifyTriangle(3, 5,4,0, Vector3D(0,1,0), Vector3D(1,0,0), Vector3D(0,0,1)));
610 }
611
612 void testVisitTriangleFanIndexed()
613 {
614 QScopedPointer<NodeManagers> nodeManagers(new NodeManagers());
615 Qt3DRender::QGeometry *geometry = new Qt3DRender::QGeometry();
616 QScopedPointer<Qt3DRender::QGeometryRenderer> geometryRenderer(new Qt3DRender::QGeometryRenderer());
617 QScopedPointer<Qt3DRender::QAttribute> positionAttribute(new Qt3DRender::QAttribute());
618 QScopedPointer<Qt3DRender::QAttribute> indexAttribute(new Qt3DRender::QAttribute());
619 QScopedPointer<Qt3DRender::QBuffer> dataBuffer(new Qt3DRender::QBuffer());
620 QScopedPointer<Qt3DRender::QBuffer> indexDataBuffer(new Qt3DRender::QBuffer());
621 TestVisitor visitor(nodeManagers.data());
622 TestRenderer renderer;
623
624 QByteArray data;
625 data.resize(size: sizeof(float) * 3 * 3 * 2);
626 float *dataPtr = reinterpret_cast<float *>(data.data());
627 dataPtr[0] = 0;
628 dataPtr[1] = 0;
629 dataPtr[2] = 1.0f;
630 dataPtr[3] = 1.0f;
631 dataPtr[4] = 0;
632 dataPtr[5] = 0;
633 dataPtr[6] = 0;
634 dataPtr[7] = 1.0f;
635 dataPtr[8] = 0;
636
637 dataPtr[9] = 0;
638 dataPtr[10] = 0;
639 dataPtr[11] = 1.0f;
640 dataPtr[12] = 1.0f;
641 dataPtr[13] = 0;
642 dataPtr[14] = 0;
643 dataPtr[15] = 0;
644 dataPtr[16] = 1.0f;
645 dataPtr[17] = 0;
646 dataBuffer->setData(data);
647 Buffer *backendBuffer = nodeManagers->bufferManager()->getOrCreateResource(id: dataBuffer->id());
648 backendBuffer->setRenderer(&renderer);
649 backendBuffer->setManager(nodeManagers->bufferManager());
650 simulateInitializationSync(frontend: dataBuffer.data(), backend: backendBuffer);
651
652 QByteArray indexData;
653 indexData.resize(size: sizeof(uint) * 10);
654 uint *iDataPtr = reinterpret_cast<uint *>(indexData.data());
655 iDataPtr[0] = 0;
656 iDataPtr[1] = 1;
657 iDataPtr[2] = 2;
658 iDataPtr[3] = 3;
659 iDataPtr[4] = 4;
660 iDataPtr[5] = 5;
661 iDataPtr[6] = static_cast<uint>(-1);
662 iDataPtr[7] = 0;
663 iDataPtr[8] = 1;
664 iDataPtr[9] = 2;
665 indexDataBuffer->setData(indexData);
666
667 Buffer *backendIndexBuffer = nodeManagers->bufferManager()->getOrCreateResource(id: indexDataBuffer->id());
668 backendIndexBuffer->setRenderer(&renderer);
669 backendIndexBuffer->setManager(nodeManagers->bufferManager());
670 simulateInitializationSync(frontend: indexDataBuffer.data(), backend: backendIndexBuffer);
671
672 positionAttribute->setBuffer(dataBuffer.data());
673 positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
674 positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
675 positionAttribute->setVertexSize(3);
676 positionAttribute->setCount(6);
677 positionAttribute->setByteStride(3*sizeof(float));
678 positionAttribute->setByteOffset(0);
679 positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
680
681 indexAttribute->setBuffer(indexDataBuffer.data());
682 indexAttribute->setVertexBaseType(Qt3DRender::QAttribute::UnsignedInt);
683 indexAttribute->setCount(10);
684 indexAttribute->setAttributeType(Qt3DRender::QAttribute::IndexAttribute);
685
686 geometry->addAttribute(attribute: positionAttribute.data());
687 geometry->addAttribute(attribute: indexAttribute.data());
688
689 geometryRenderer->setGeometry(geometry);
690 geometryRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::TriangleFan);
691 geometryRenderer->setPrimitiveRestartEnabled(true);
692 geometryRenderer->setRestartIndexValue(-1);
693
694 Attribute *backendAttribute = nodeManagers->attributeManager()->getOrCreateResource(id: positionAttribute->id());
695 backendAttribute->setRenderer(&renderer);
696 simulateInitializationSync(frontend: positionAttribute.data(), backend: backendAttribute);
697
698 Attribute *backendIndexAttribute = nodeManagers->attributeManager()->getOrCreateResource(id: indexAttribute->id());
699 backendIndexAttribute->setRenderer(&renderer);
700 simulateInitializationSync(frontend: indexAttribute.data(), backend: backendIndexAttribute);
701
702 Geometry *backendGeometry = nodeManagers->geometryManager()->getOrCreateResource(id: geometry->id());
703 backendGeometry->setRenderer(&renderer);
704 simulateInitializationSync(frontend: geometry, backend: backendGeometry);
705
706 GeometryRenderer *backendRenderer = nodeManagers->geometryRendererManager()->getOrCreateResource(id: geometryRenderer->id());
707 backendRenderer->setRenderer(&renderer);
708 backendRenderer->setManager(nodeManagers->geometryRendererManager());
709 simulateInitializationSync(frontend: geometryRenderer.data(), backend: backendRenderer);
710
711 // WHEN
712 visitor.apply(renderer: backendRenderer, id: Qt3DCore::QNodeId());
713
714 // THEN
715 QCOMPARE(visitor.triangleCount(), 5U);
716 QVERIFY(visitor.verifyTriangle(0, 2,1,0, Vector3D(0,1,0), Vector3D(1,0,0), Vector3D(0,0,1)));
717 QVERIFY(visitor.verifyTriangle(1, 3,2,0, Vector3D(0,0,1), Vector3D(0,1,0), Vector3D(0,0,1)));
718 QVERIFY(visitor.verifyTriangle(2, 4,3,0, Vector3D(1,0,0), Vector3D(0,0,1), Vector3D(0,0,1)));
719 QVERIFY(visitor.verifyTriangle(3, 5,4,0, Vector3D(0,1,0), Vector3D(1,0,0), Vector3D(0,0,1)));
720 QVERIFY(visitor.verifyTriangle(4, 2,1,0, Vector3D(0,1,0), Vector3D(1,0,0), Vector3D(0,0,1)));
721 }
722
723 void testVisitTrianglesAdjacency()
724 {
725 QScopedPointer<NodeManagers> nodeManagers(new NodeManagers());
726 Qt3DRender::QGeometry *geometry = new Qt3DRender::QGeometry();
727 QScopedPointer<Qt3DRender::QGeometryRenderer> geometryRenderer(new Qt3DRender::QGeometryRenderer());
728 QScopedPointer<Qt3DRender::QAttribute> positionAttribute(new Qt3DRender::QAttribute());
729 QScopedPointer<Qt3DRender::QBuffer> dataBuffer(new Qt3DRender::QBuffer());
730 TestVisitor visitor(nodeManagers.data());
731 TestRenderer renderer;
732
733 QByteArray data;
734 data.resize(size: sizeof(float) * 3 * 3 * 2);
735 float *dataPtr = reinterpret_cast<float *>(data.data());
736 dataPtr[0] = 0;
737 dataPtr[1] = 0;
738 dataPtr[2] = 1.0f;
739 dataPtr[3] = 1.0f;
740 dataPtr[4] = 0;
741 dataPtr[5] = 0;
742 dataPtr[6] = 0;
743 dataPtr[7] = 1.0f;
744 dataPtr[8] = 0;
745
746 dataPtr[9] = 0;
747 dataPtr[10] = 0;
748 dataPtr[11] = 1.0f;
749 dataPtr[12] = 1.0f;
750 dataPtr[13] = 0;
751 dataPtr[14] = 0;
752 dataPtr[15] = 0;
753 dataPtr[16] = 1.0f;
754 dataPtr[17] = 0;
755 dataBuffer->setData(data);
756 Buffer *backendBuffer = nodeManagers->bufferManager()->getOrCreateResource(id: dataBuffer->id());
757 backendBuffer->setRenderer(&renderer);
758 backendBuffer->setManager(nodeManagers->bufferManager());
759 simulateInitializationSync(frontend: dataBuffer.data(), backend: backendBuffer);
760
761 positionAttribute->setBuffer(dataBuffer.data());
762 positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
763 positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
764 positionAttribute->setVertexSize(3);
765 positionAttribute->setCount(6);
766 positionAttribute->setByteStride(3*4);
767 positionAttribute->setByteOffset(0);
768 positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
769 geometry->addAttribute(attribute: positionAttribute.data());
770
771 geometryRenderer->setGeometry(geometry);
772 geometryRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::TrianglesAdjacency);
773
774 Attribute *backendAttribute = nodeManagers->attributeManager()->getOrCreateResource(id: positionAttribute->id());
775 backendAttribute->setRenderer(&renderer);
776 simulateInitializationSync(frontend: positionAttribute.data(), backend: backendAttribute);
777
778 Geometry *backendGeometry = nodeManagers->geometryManager()->getOrCreateResource(id: geometry->id());
779 backendGeometry->setRenderer(&renderer);
780 simulateInitializationSync(frontend: geometry, backend: backendGeometry);
781
782 GeometryRenderer *backendRenderer = nodeManagers->geometryRendererManager()->getOrCreateResource(id: geometryRenderer->id());
783 backendRenderer->setRenderer(&renderer);
784 backendRenderer->setManager(nodeManagers->geometryRendererManager());
785 simulateInitializationSync(frontend: geometryRenderer.data(), backend: backendRenderer);
786
787 // WHEN
788 visitor.apply(renderer: backendRenderer, id: Qt3DCore::QNodeId());
789
790 // THEN
791 QVERIFY(visitor.triangleCount() == 1);
792 QVERIFY(visitor.verifyTriangle(0, 4,2,0, Vector3D(1,0,0), Vector3D(0,1,0), Vector3D(0,0,1)));
793 }
794
795 void testVisitTrianglesAdjacencyIndexed()
796 {
797 QScopedPointer<NodeManagers> nodeManagers(new NodeManagers());
798 Qt3DRender::QGeometry *geometry = new Qt3DRender::QGeometry();
799 QScopedPointer<Qt3DRender::QGeometryRenderer> geometryRenderer(new Qt3DRender::QGeometryRenderer());
800 QScopedPointer<Qt3DRender::QAttribute> positionAttribute(new Qt3DRender::QAttribute());
801 QScopedPointer<Qt3DRender::QAttribute> indexAttribute(new Qt3DRender::QAttribute());
802 QScopedPointer<Qt3DRender::QBuffer> dataBuffer(new Qt3DRender::QBuffer());
803 QScopedPointer<Qt3DRender::QBuffer> indexDataBuffer(new Qt3DRender::QBuffer());
804 TestVisitor visitor(nodeManagers.data());
805 TestRenderer renderer;
806
807 QByteArray data;
808 data.resize(size: sizeof(float) * 3 * 3 * 2);
809 float *dataPtr = reinterpret_cast<float *>(data.data());
810 dataPtr[0] = 0;
811 dataPtr[1] = 0;
812 dataPtr[2] = 1.0f;
813 dataPtr[3] = 1.0f;
814 dataPtr[4] = 0;
815 dataPtr[5] = 0;
816 dataPtr[6] = 0;
817 dataPtr[7] = 1.0f;
818 dataPtr[8] = 0;
819
820 dataPtr[9] = 0;
821 dataPtr[10] = 0;
822 dataPtr[11] = 1.0f;
823 dataPtr[12] = 1.0f;
824 dataPtr[13] = 0;
825 dataPtr[14] = 0;
826 dataPtr[15] = 0;
827 dataPtr[16] = 1.0f;
828 dataPtr[17] = 0;
829 dataBuffer->setData(data);
830 Buffer *backendBuffer = nodeManagers->bufferManager()->getOrCreateResource(id: dataBuffer->id());
831 backendBuffer->setRenderer(&renderer);
832 backendBuffer->setManager(nodeManagers->bufferManager());
833 simulateInitializationSync(frontend: dataBuffer.data(), backend: backendBuffer);
834
835 QByteArray indexData;
836 indexData.resize(size: sizeof(uint) * 3 * 4);
837 uint *iDataPtr = reinterpret_cast<uint *>(indexData.data());
838 iDataPtr[0] = 0;
839 iDataPtr[1] = 1;
840 iDataPtr[2] = 2;
841 iDataPtr[3] = 3;
842 iDataPtr[4] = 4;
843 iDataPtr[5] = 5;
844
845 iDataPtr[6] = 5;
846 iDataPtr[7] = 1;
847 iDataPtr[8] = 0;
848 iDataPtr[9] = 4;
849 iDataPtr[10] = 3;
850 iDataPtr[11] = 2;
851 indexDataBuffer->setData(indexData);
852
853 Buffer *backendIndexBuffer = nodeManagers->bufferManager()->getOrCreateResource(id: indexDataBuffer->id());
854 backendIndexBuffer->setRenderer(&renderer);
855 backendIndexBuffer->setManager(nodeManagers->bufferManager());
856 simulateInitializationSync(frontend: indexDataBuffer.data(), backend: backendIndexBuffer);
857
858 positionAttribute->setBuffer(dataBuffer.data());
859 positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
860 positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
861 positionAttribute->setVertexSize(3);
862 positionAttribute->setCount(6);
863 positionAttribute->setByteStride(3*sizeof(float));
864 positionAttribute->setByteOffset(0);
865 positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
866
867 indexAttribute->setBuffer(indexDataBuffer.data());
868 indexAttribute->setVertexBaseType(Qt3DRender::QAttribute::UnsignedInt);
869 indexAttribute->setCount(3*4);
870 indexAttribute->setAttributeType(Qt3DRender::QAttribute::IndexAttribute);
871
872 geometry->addAttribute(attribute: positionAttribute.data());
873 geometry->addAttribute(attribute: indexAttribute.data());
874
875 geometryRenderer->setGeometry(geometry);
876 geometryRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::TrianglesAdjacency);
877
878 Attribute *backendAttribute = nodeManagers->attributeManager()->getOrCreateResource(id: positionAttribute->id());
879 backendAttribute->setRenderer(&renderer);
880 simulateInitializationSync(frontend: positionAttribute.data(), backend: backendAttribute);
881
882 Attribute *backendIndexAttribute = nodeManagers->attributeManager()->getOrCreateResource(id: indexAttribute->id());
883 backendIndexAttribute->setRenderer(&renderer);
884 simulateInitializationSync(frontend: indexAttribute.data(), backend: backendIndexAttribute);
885
886 Geometry *backendGeometry = nodeManagers->geometryManager()->getOrCreateResource(id: geometry->id());
887 backendGeometry->setRenderer(&renderer);
888 simulateInitializationSync(frontend: geometry, backend: backendGeometry);
889
890 GeometryRenderer *backendRenderer = nodeManagers->geometryRendererManager()->getOrCreateResource(id: geometryRenderer->id());
891 backendRenderer->setRenderer(&renderer);
892 backendRenderer->setManager(nodeManagers->geometryRendererManager());
893 simulateInitializationSync(frontend: geometryRenderer.data(), backend: backendRenderer);
894
895 // WHEN
896 visitor.apply(renderer: backendRenderer, id: Qt3DCore::QNodeId());
897
898 // THEN
899 QVERIFY(visitor.triangleCount() == 2);
900 QVERIFY(visitor.verifyTriangle(0, 4,2,0, Vector3D(1,0,0), Vector3D(0,1,0), Vector3D(0,0,1)));
901 QVERIFY(visitor.verifyTriangle(1, 3,0,5, Vector3D(0,0,1), Vector3D(0,0,1), Vector3D(0,1,0)));
902 }
903
904 void testVisitTriangleStripAdjacency()
905 {
906 QSKIP("TriangleStripAdjacency not implemented");
907 QScopedPointer<NodeManagers> nodeManagers(new NodeManagers());
908 Qt3DRender::QGeometry *geometry = new Qt3DRender::QGeometry();
909 QScopedPointer<Qt3DRender::QGeometryRenderer> geometryRenderer(new Qt3DRender::QGeometryRenderer());
910 QScopedPointer<Qt3DRender::QAttribute> positionAttribute(new Qt3DRender::QAttribute());
911 QScopedPointer<Qt3DRender::QBuffer> dataBuffer(new Qt3DRender::QBuffer());
912 TestVisitor visitor(nodeManagers.data());
913 TestRenderer renderer;
914
915 QByteArray data;
916 data.resize(size: sizeof(float) * 3 * 8);
917 float *dataPtr = reinterpret_cast<float *>(data.data());
918 dataPtr[0] = 0;
919 dataPtr[1] = 0;
920 dataPtr[2] = 1.0f;
921
922 dataPtr[3] = 1.0f;
923 dataPtr[4] = 0;
924 dataPtr[5] = 0;
925
926 dataPtr[6] = 0;
927 dataPtr[7] = 1.0f;
928 dataPtr[8] = 0;
929
930 dataPtr[9] = 0;
931 dataPtr[10] = 0;
932 dataPtr[11] = 1.0f;
933
934 dataPtr[12] = 1.0f;
935 dataPtr[13] = 0;
936 dataPtr[14] = 0;
937
938 dataPtr[15] = 0;
939 dataPtr[16] = 1.0f;
940 dataPtr[17] = 0;
941
942 dataPtr[18] = 1.0f;
943 dataPtr[19] = 1.0f;
944 dataPtr[20] = 1.0f;
945
946 dataPtr[21] = 0;
947 dataPtr[22] = 0;
948 dataPtr[22] = 0;
949 dataBuffer->setData(data);
950 Buffer *backendBuffer = nodeManagers->bufferManager()->getOrCreateResource(id: dataBuffer->id());
951 backendBuffer->setRenderer(&renderer);
952 backendBuffer->setManager(nodeManagers->bufferManager());
953 simulateInitializationSync(frontend: dataBuffer.data(), backend: backendBuffer);
954
955 positionAttribute->setBuffer(dataBuffer.data());
956 positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
957 positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
958 positionAttribute->setVertexSize(3);
959 positionAttribute->setCount(8);
960 positionAttribute->setByteStride(3*4);
961 positionAttribute->setByteOffset(0);
962 positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
963 geometry->addAttribute(attribute: positionAttribute.data());
964
965 geometryRenderer->setGeometry(geometry);
966 geometryRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::TriangleStripAdjacency);
967
968 Attribute *backendAttribute = nodeManagers->attributeManager()->getOrCreateResource(id: positionAttribute->id());
969 backendAttribute->setRenderer(&renderer);
970 simulateInitializationSync(frontend: positionAttribute.data(), backend: backendAttribute);
971
972 Geometry *backendGeometry = nodeManagers->geometryManager()->getOrCreateResource(id: geometry->id());
973 backendGeometry->setRenderer(&renderer);
974 simulateInitializationSync(frontend: geometry, backend: backendGeometry);
975
976 GeometryRenderer *backendRenderer = nodeManagers->geometryRendererManager()->getOrCreateResource(id: geometryRenderer->id());
977 backendRenderer->setRenderer(&renderer);
978 backendRenderer->setManager(nodeManagers->geometryRendererManager());
979 simulateInitializationSync(frontend: geometryRenderer.data(), backend: backendRenderer);
980
981 // WHEN
982 visitor.apply(renderer: backendRenderer, id: Qt3DCore::QNodeId());
983
984 // THEN
985 QVERIFY(visitor.triangleCount() == 2);
986 QVERIFY(visitor.verifyTriangle(0, 4,2,0, Vector3D(1,0,0), Vector3D(0,1,0), Vector3D(0,0,1)));
987 QVERIFY(visitor.verifyTriangle(1, 6,4,2, Vector3D(1,1,1), Vector3D(1,0,0), Vector3D(0,1,0)));
988 }
989
990 void testVisitTriangleStripAdjacencyIndexed()
991 {
992 QSKIP("TriangleStripAdjacency not implemented");
993 QScopedPointer<NodeManagers> nodeManagers(new NodeManagers());
994 Qt3DRender::QGeometry *geometry = new Qt3DRender::QGeometry();
995 QScopedPointer<Qt3DRender::QGeometryRenderer> geometryRenderer(new Qt3DRender::QGeometryRenderer());
996 QScopedPointer<Qt3DRender::QAttribute> positionAttribute(new Qt3DRender::QAttribute());
997 QScopedPointer<Qt3DRender::QAttribute> indexAttribute(new Qt3DRender::QAttribute());
998 QScopedPointer<Qt3DRender::QBuffer> dataBuffer(new Qt3DRender::QBuffer());
999 QScopedPointer<Qt3DRender::QBuffer> indexDataBuffer(new Qt3DRender::QBuffer());
1000 TestVisitor visitor(nodeManagers.data());
1001 TestRenderer renderer;
1002
1003 QByteArray data;
1004 data.resize(size: sizeof(float) * 3 * 3 * 2);
1005 float *dataPtr = reinterpret_cast<float *>(data.data());
1006 dataPtr[0] = 0;
1007 dataPtr[1] = 0;
1008 dataPtr[2] = 1.0f;
1009 dataPtr[3] = 1.0f;
1010 dataPtr[4] = 0;
1011 dataPtr[5] = 0;
1012 dataPtr[6] = 0;
1013 dataPtr[7] = 1.0f;
1014 dataPtr[8] = 0;
1015
1016 dataPtr[9] = 0;
1017 dataPtr[10] = 0;
1018 dataPtr[11] = 1.0f;
1019 dataPtr[12] = 1.0f;
1020 dataPtr[13] = 0;
1021 dataPtr[14] = 0;
1022 dataPtr[15] = 0;
1023 dataPtr[16] = 1.0f;
1024 dataPtr[17] = 0;
1025 dataBuffer->setData(data);
1026 Buffer *backendBuffer = nodeManagers->bufferManager()->getOrCreateResource(id: dataBuffer->id());
1027 backendBuffer->setRenderer(&renderer);
1028 backendBuffer->setManager(nodeManagers->bufferManager());
1029 simulateInitializationSync(frontend: dataBuffer.data(), backend: backendBuffer);
1030
1031 QByteArray indexData;
1032 indexData.resize(size: sizeof(uint) * 8);
1033 uint *iDataPtr = reinterpret_cast<uint *>(indexData.data());
1034 iDataPtr[0] = 0;
1035 iDataPtr[1] = 1;
1036 iDataPtr[2] = 2;
1037 iDataPtr[3] = 3;
1038 iDataPtr[4] = 4;
1039 iDataPtr[5] = 5;
1040 iDataPtr[6] = 5;
1041 iDataPtr[7] = 1;
1042 indexDataBuffer->setData(indexData);
1043
1044 Buffer *backendIndexBuffer = nodeManagers->bufferManager()->getOrCreateResource(id: indexDataBuffer->id());
1045 backendIndexBuffer->setRenderer(&renderer);
1046 backendIndexBuffer->setManager(nodeManagers->bufferManager());
1047 simulateInitializationSync(frontend: indexDataBuffer.data(), backend: backendIndexBuffer);
1048
1049 positionAttribute->setBuffer(dataBuffer.data());
1050 positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
1051 positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
1052 positionAttribute->setVertexSize(3);
1053 positionAttribute->setCount(6);
1054 positionAttribute->setByteStride(3*sizeof(float));
1055 positionAttribute->setByteOffset(0);
1056 positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
1057
1058 indexAttribute->setBuffer(indexDataBuffer.data());
1059 indexAttribute->setVertexBaseType(Qt3DRender::QAttribute::UnsignedInt);
1060 indexAttribute->setCount(8);
1061 indexAttribute->setAttributeType(Qt3DRender::QAttribute::IndexAttribute);
1062
1063 geometry->addAttribute(attribute: positionAttribute.data());
1064 geometry->addAttribute(attribute: indexAttribute.data());
1065
1066 geometryRenderer->setGeometry(geometry);
1067 geometryRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::TriangleStrip);
1068
1069 Attribute *backendAttribute = nodeManagers->attributeManager()->getOrCreateResource(id: positionAttribute->id());
1070 backendAttribute->setRenderer(&renderer);
1071 simulateInitializationSync(frontend: positionAttribute.data(), backend: backendAttribute);
1072
1073 Attribute *backendIndexAttribute = nodeManagers->attributeManager()->getOrCreateResource(id: indexAttribute->id());
1074 backendIndexAttribute->setRenderer(&renderer);
1075 simulateInitializationSync(frontend: indexAttribute.data(), backend: backendIndexAttribute);
1076
1077 Geometry *backendGeometry = nodeManagers->geometryManager()->getOrCreateResource(id: geometry->id());
1078 backendGeometry->setRenderer(&renderer);
1079 simulateInitializationSync(frontend: geometry, backend: backendGeometry);
1080
1081 GeometryRenderer *backendRenderer = nodeManagers->geometryRendererManager()->getOrCreateResource(id: geometryRenderer->id());
1082 backendRenderer->setRenderer(&renderer);
1083 backendRenderer->setManager(nodeManagers->geometryRendererManager());
1084 simulateInitializationSync(frontend: geometryRenderer.data(), backend: backendRenderer);
1085
1086 // WHEN
1087 visitor.apply(renderer: backendRenderer, id: Qt3DCore::QNodeId());
1088
1089 // THEN
1090 QVERIFY(visitor.triangleCount() == 2);
1091 QVERIFY(visitor.verifyTriangle(0, 4,2,0, Vector3D(1,0,0), Vector3D(0,1,0), Vector3D(0,0,1)));
1092 QVERIFY(visitor.verifyTriangle(1, 5,4,2, Vector3D(0,1,0), Vector3D(1,0,0), Vector3D(0,1,0)));
1093 }
1094};
1095
1096QTEST_MAIN(tst_TriangleVisitor)
1097
1098#include "tst_trianglevisitor.moc"
1099

source code of qt3d/tests/auto/render/trianglevisitor/tst_trianglevisitor.cpp