1/****************************************************************************
2**
3** Copyright (C) 2017 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: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/segmentsvisitor_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 SegmentsVisitor
44{
45public:
46 TestVisitor(NodeManagers *manager)
47 : SegmentsVisitor(manager)
48 {
49
50 }
51
52 virtual void visit(uint andx, const Vector3D &a, uint bndx, const Vector3D &b)
53 {
54 m_segments.push_back(t: TestSegment(andx, a, bndx, b));
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 segmentCount() const
68 {
69 return m_segments.size();
70 }
71
72 bool verifySegment(uint segment, uint andx, uint bndx, Vector3D a, Vector3D b) const
73 {
74 if (segment >= uint(m_segments.size()))
75 return false;
76 if (andx != m_segments[segment].abcndx[0]
77 || bndx != m_segments[segment].abcndx[1])
78 return false;
79
80 if (!qFuzzyCompare(v1: a, v2: m_segments[segment].abc[0])
81 || !qFuzzyCompare(v1: b, v2: m_segments[segment].abc[1]))
82 return false;
83
84 return true;
85 }
86private:
87 struct TestSegment
88 {
89 uint abcndx[2];
90 Vector3D abc[2];
91 TestSegment()
92 {
93 abcndx[0] = abcndx[1] = uint(-1);
94 }
95
96 TestSegment(uint andx, const Vector3D &a, uint bndx, const Vector3D &b)
97 {
98 abcndx[0] = andx;
99 abcndx[1] = bndx;
100 abc[0] = a;
101 abc[1] = b;
102 }
103 };
104 QVector<TestSegment> m_segments;
105};
106
107class tst_SegmentVisitor : public Qt3DCore::QBackendNodeTester
108{
109 Q_OBJECT
110
111private Q_SLOTS:
112
113 void checkInitialize()
114 {
115 // WHEN
116 QScopedPointer<NodeManagers> nodeManagers(new NodeManagers());
117 TestVisitor visitor(nodeManagers.data());
118
119 // THEN
120 QCOMPARE(visitor.nodeManagers(), nodeManagers.data());
121 }
122
123 void checkApplyEntity()
124 {
125 QScopedPointer<NodeManagers> nodeManagers(new NodeManagers());
126 QScopedPointer<Qt3DCore::QEntity> entity(new Qt3DCore::QEntity());
127 TestVisitor visitor(nodeManagers.data());
128
129 // WHEN
130 visitor.apply(entity: entity.data());
131
132 // THEN
133 QCOMPARE(visitor.nodeId(), entity->id());
134 QCOMPARE(visitor.segmentCount(), uint(0));
135 }
136
137 void checkApplyGeometryRenderer()
138 {
139 QScopedPointer<NodeManagers> nodeManagers(new NodeManagers());
140 QScopedPointer<GeometryRenderer> geometryRenderer(new GeometryRenderer());
141 TestVisitor visitor(nodeManagers.data());
142
143 // WHEN
144 visitor.apply(renderer: geometryRenderer.data(), id: Qt3DCore::QNodeId());
145
146 // THEN
147 // tadaa, nothing should happen
148 }
149
150 void testVisitSegments()
151 {
152 QScopedPointer<NodeManagers> nodeManagers(new NodeManagers());
153 Qt3DRender::QGeometry *geometry = new Qt3DRender::QGeometry();
154 QScopedPointer<Qt3DRender::QGeometryRenderer> geometryRenderer(new Qt3DRender::QGeometryRenderer());
155 QScopedPointer<Qt3DRender::QAttribute> positionAttribute(new Qt3DRender::QAttribute());
156 QScopedPointer<Qt3DRender::QBuffer> dataBuffer(new Qt3DRender::QBuffer());
157 TestVisitor visitor(nodeManagers.data());
158 TestRenderer renderer;
159
160 QByteArray data;
161 data.resize(size: sizeof(float) * 3 * 2 * 3);
162 float *dataPtr = reinterpret_cast<float *>(data.data());
163 dataPtr[0] = 0;
164 dataPtr[1] = 0;
165 dataPtr[2] = 1.0f;
166 dataPtr[3] = 1.0f;
167 dataPtr[4] = 0;
168 dataPtr[5] = 0;
169
170 dataPtr[6] = 0;
171 dataPtr[7] = 1.0f;
172 dataPtr[8] = 0;
173 dataPtr[9] = 0;
174 dataPtr[10] = 0;
175 dataPtr[11] = 1.0f;
176
177 dataPtr[12] = 1.0f;
178 dataPtr[13] = 0;
179 dataPtr[14] = 0;
180 dataPtr[15] = 0;
181 dataPtr[16] = 1.0f;
182 dataPtr[17] = 0;
183
184 dataBuffer->setData(data);
185 Buffer *backendBuffer = nodeManagers->bufferManager()->getOrCreateResource(id: dataBuffer->id());
186 backendBuffer->setRenderer(&renderer);
187 backendBuffer->setManager(nodeManagers->bufferManager());
188 simulateInitializationSync(frontend: dataBuffer.data(), backend: backendBuffer);
189
190 positionAttribute->setBuffer(dataBuffer.data());
191 positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
192 positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
193 positionAttribute->setVertexSize(3);
194 positionAttribute->setCount(6);
195 positionAttribute->setByteStride(3*4);
196 positionAttribute->setByteOffset(0);
197 positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
198 geometry->addAttribute(attribute: positionAttribute.data());
199
200 geometryRenderer->setGeometry(geometry);
201 geometryRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Lines);
202
203 Attribute *backendAttribute = nodeManagers->attributeManager()->getOrCreateResource(id: positionAttribute->id());
204 backendAttribute->setRenderer(&renderer);
205 simulateInitializationSync(frontend: positionAttribute.data(), backend: backendAttribute);
206
207 Geometry *backendGeometry = nodeManagers->geometryManager()->getOrCreateResource(id: geometry->id());
208 backendGeometry->setRenderer(&renderer);
209 simulateInitializationSync(frontend: geometry, backend: backendGeometry);
210
211 GeometryRenderer *backendRenderer = nodeManagers->geometryRendererManager()->getOrCreateResource(id: geometryRenderer->id());
212 backendRenderer->setRenderer(&renderer);
213 backendRenderer->setManager(nodeManagers->geometryRendererManager());
214 simulateInitializationSync(frontend: geometryRenderer.data(), backend: backendRenderer);
215
216 // WHEN
217 visitor.apply(renderer: backendRenderer, id: Qt3DCore::QNodeId());
218
219 // THEN
220 QCOMPARE(visitor.segmentCount(), uint(3));
221 QVERIFY(visitor.verifySegment(0, 0,1, Vector3D(0,0,1), Vector3D(1,0,0)));
222 QVERIFY(visitor.verifySegment(1, 2,3, Vector3D(0,1,0), Vector3D(0,0,1)));
223 QVERIFY(visitor.verifySegment(2, 4,5, Vector3D(1,0,0), Vector3D(0,1,0)));
224 }
225
226 void testVisitSegmentsIndexed()
227 {
228 QScopedPointer<NodeManagers> nodeManagers(new NodeManagers());
229 Qt3DRender::QGeometry *geometry = new Qt3DRender::QGeometry();
230 QScopedPointer<Qt3DRender::QGeometryRenderer> geometryRenderer(new Qt3DRender::QGeometryRenderer());
231 QScopedPointer<Qt3DRender::QAttribute> positionAttribute(new Qt3DRender::QAttribute());
232 QScopedPointer<Qt3DRender::QAttribute> indexAttribute(new Qt3DRender::QAttribute());
233 QScopedPointer<Qt3DRender::QBuffer> dataBuffer(new Qt3DRender::QBuffer());
234 QScopedPointer<Qt3DRender::QBuffer> indexDataBuffer(new Qt3DRender::QBuffer());
235 TestVisitor visitor(nodeManagers.data());
236 TestRenderer renderer;
237
238 QByteArray data;
239 data.resize(size: sizeof(float) * 3 * 4);
240 float *dataPtr = reinterpret_cast<float *>(data.data());
241 dataPtr[0] = 0;
242 dataPtr[1] = 0;
243 dataPtr[2] = 0.0f;
244 dataPtr[3] = 1.0f;
245 dataPtr[4] = 0;
246 dataPtr[5] = 0;
247 dataPtr[6] = 1.0;
248 dataPtr[7] = 1.0f;
249 dataPtr[8] = 0;
250 dataPtr[9] = 0;
251 dataPtr[10] = 1.0f;
252 dataPtr[11] = 0;
253 dataBuffer->setData(data);
254 Buffer *backendBuffer = nodeManagers->bufferManager()->getOrCreateResource(id: dataBuffer->id());
255 backendBuffer->setRenderer(&renderer);
256 backendBuffer->setManager(nodeManagers->bufferManager());
257 simulateInitializationSync(frontend: dataBuffer.data(), backend: backendBuffer);
258
259 QByteArray indexData;
260 indexData.resize(size: sizeof(uint) * 2 * 5);
261 uint *iDataPtr = reinterpret_cast<uint *>(indexData.data());
262 iDataPtr[0] = 0;
263 iDataPtr[1] = 1;
264 iDataPtr[2] = 1;
265 iDataPtr[3] = 2;
266 iDataPtr[4] = 2;
267 iDataPtr[5] = 3;
268 iDataPtr[6] = 0;
269 iDataPtr[7] = 2;
270 iDataPtr[8] = 1;
271 iDataPtr[9] = 3;
272 indexDataBuffer->setData(indexData);
273
274 Buffer *backendIndexBuffer = nodeManagers->bufferManager()->getOrCreateResource(id: indexDataBuffer->id());
275 backendIndexBuffer->setRenderer(&renderer);
276 backendIndexBuffer->setManager(nodeManagers->bufferManager());
277 simulateInitializationSync(frontend: indexDataBuffer.data(), backend: backendIndexBuffer);
278
279 positionAttribute->setBuffer(dataBuffer.data());
280 positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
281 positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
282 positionAttribute->setVertexSize(3);
283 positionAttribute->setCount(4);
284 positionAttribute->setByteStride(3*sizeof(float));
285 positionAttribute->setByteOffset(0);
286 positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
287
288 indexAttribute->setBuffer(indexDataBuffer.data());
289 indexAttribute->setVertexBaseType(Qt3DRender::QAttribute::UnsignedInt);
290 indexAttribute->setCount(2*5);
291 indexAttribute->setAttributeType(Qt3DRender::QAttribute::IndexAttribute);
292
293 geometry->addAttribute(attribute: positionAttribute.data());
294 geometry->addAttribute(attribute: indexAttribute.data());
295
296 geometryRenderer->setGeometry(geometry);
297 geometryRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Lines);
298
299 Attribute *backendAttribute = nodeManagers->attributeManager()->getOrCreateResource(id: positionAttribute->id());
300 backendAttribute->setRenderer(&renderer);
301 simulateInitializationSync(frontend: positionAttribute.data(), backend: backendAttribute);
302
303 Attribute *backendIndexAttribute = nodeManagers->attributeManager()->getOrCreateResource(id: indexAttribute->id());
304 backendIndexAttribute->setRenderer(&renderer);
305 simulateInitializationSync(frontend: indexAttribute.data(), backend: backendIndexAttribute);
306
307 Geometry *backendGeometry = nodeManagers->geometryManager()->getOrCreateResource(id: geometry->id());
308 backendGeometry->setRenderer(&renderer);
309 simulateInitializationSync(frontend: geometry, backend: backendGeometry);
310
311 GeometryRenderer *backendRenderer = nodeManagers->geometryRendererManager()->getOrCreateResource(id: geometryRenderer->id());
312 backendRenderer->setRenderer(&renderer);
313 backendRenderer->setManager(nodeManagers->geometryRendererManager());
314 simulateInitializationSync(frontend: geometryRenderer.data(), backend: backendRenderer);
315
316 // WHEN
317 visitor.apply(renderer: backendRenderer, id: Qt3DCore::QNodeId());
318
319 // THEN
320 QCOMPARE(visitor.segmentCount(), uint(5));
321 QVERIFY(visitor.verifySegment(0, 0,1, Vector3D(0,0,0), Vector3D(1,0,0)));
322 QVERIFY(visitor.verifySegment(1, 1,2, Vector3D(1,0,0), Vector3D(1,1,0)));
323 QVERIFY(visitor.verifySegment(2, 2,3, Vector3D(1,1,0), Vector3D(0,1,0)));
324 QVERIFY(visitor.verifySegment(3, 0,2, Vector3D(0,0,0), Vector3D(1,1,0)));
325 QVERIFY(visitor.verifySegment(4, 1,3, Vector3D(1,0,0), Vector3D(0,1,0)));
326 }
327
328 void testVisitLineStrip()
329 {
330 QScopedPointer<NodeManagers> nodeManagers(new NodeManagers());
331 Qt3DRender::QGeometry *geometry = new Qt3DRender::QGeometry();
332 QScopedPointer<Qt3DRender::QGeometryRenderer> geometryRenderer(new Qt3DRender::QGeometryRenderer());
333 QScopedPointer<Qt3DRender::QAttribute> positionAttribute(new Qt3DRender::QAttribute());
334 QScopedPointer<Qt3DRender::QBuffer> dataBuffer(new Qt3DRender::QBuffer());
335 TestVisitor visitor(nodeManagers.data());
336 TestRenderer renderer;
337
338 QByteArray data;
339 data.resize(size: sizeof(float) * 3 * 4);
340 float *dataPtr = reinterpret_cast<float *>(data.data());
341 dataPtr[0] = 0;
342 dataPtr[1] = 0;
343 dataPtr[2] = 0.0f;
344 dataPtr[3] = 1.0f;
345 dataPtr[4] = 0;
346 dataPtr[5] = 0;
347 dataPtr[6] = 1.0f;
348 dataPtr[7] = 1.0f;
349 dataPtr[8] = 0;
350 dataPtr[9] = 0;
351 dataPtr[10] = 1.0f;
352 dataPtr[11] = 0;
353 dataBuffer->setData(data);
354 Buffer *backendBuffer = nodeManagers->bufferManager()->getOrCreateResource(id: dataBuffer->id());
355 backendBuffer->setRenderer(&renderer);
356 backendBuffer->setManager(nodeManagers->bufferManager());
357 simulateInitializationSync(frontend: dataBuffer.data(), backend: backendBuffer);
358
359 positionAttribute->setBuffer(dataBuffer.data());
360 positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
361 positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
362 positionAttribute->setVertexSize(3);
363 positionAttribute->setCount(4);
364 positionAttribute->setByteStride(3*4);
365 positionAttribute->setByteOffset(0);
366 positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
367 geometry->addAttribute(attribute: positionAttribute.data());
368
369 geometryRenderer->setGeometry(geometry);
370 geometryRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::LineStrip);
371
372 Attribute *backendAttribute = nodeManagers->attributeManager()->getOrCreateResource(id: positionAttribute->id());
373 backendAttribute->setRenderer(&renderer);
374 simulateInitializationSync(frontend: positionAttribute.data(), backend: backendAttribute);
375
376 Geometry *backendGeometry = nodeManagers->geometryManager()->getOrCreateResource(id: geometry->id());
377 backendGeometry->setRenderer(&renderer);
378 simulateInitializationSync(frontend: geometry, backend: backendGeometry);
379
380 GeometryRenderer *backendRenderer = nodeManagers->geometryRendererManager()->getOrCreateResource(id: geometryRenderer->id());
381 backendRenderer->setRenderer(&renderer);
382 backendRenderer->setManager(nodeManagers->geometryRendererManager());
383 simulateInitializationSync(frontend: geometryRenderer.data(), backend: backendRenderer);
384
385 // WHEN
386 visitor.apply(renderer: backendRenderer, id: Qt3DCore::QNodeId());
387
388 // THEN
389 QCOMPARE(visitor.segmentCount(), uint(3));
390 QVERIFY(visitor.verifySegment(0, 0,1, Vector3D(0,0,0), Vector3D(1,0,0)));
391 QVERIFY(visitor.verifySegment(1, 1,2, Vector3D(1,0,0), Vector3D(1,1,0)));
392 QVERIFY(visitor.verifySegment(2, 2,3, Vector3D(1,1,0), Vector3D(0,1,0)));
393 }
394
395 void testVisitListStripIndexed()
396 {
397 QScopedPointer<NodeManagers> nodeManagers(new NodeManagers());
398 Qt3DRender::QGeometry *geometry = new Qt3DRender::QGeometry();
399 QScopedPointer<Qt3DRender::QGeometryRenderer> geometryRenderer(new Qt3DRender::QGeometryRenderer());
400 QScopedPointer<Qt3DRender::QAttribute> positionAttribute(new Qt3DRender::QAttribute());
401 QScopedPointer<Qt3DRender::QAttribute> indexAttribute(new Qt3DRender::QAttribute());
402 QScopedPointer<Qt3DRender::QBuffer> dataBuffer(new Qt3DRender::QBuffer());
403 QScopedPointer<Qt3DRender::QBuffer> indexDataBuffer(new Qt3DRender::QBuffer());
404 TestVisitor visitor(nodeManagers.data());
405 TestRenderer renderer;
406
407 QByteArray data;
408 data.resize(size: sizeof(float) * 3 * 4);
409 float *dataPtr = reinterpret_cast<float *>(data.data());
410 dataPtr[0] = 0;
411 dataPtr[1] = 0;
412 dataPtr[2] = 0.0f;
413 dataPtr[3] = 1.0f;
414 dataPtr[4] = 0;
415 dataPtr[5] = 0;
416 dataPtr[6] = 1.0;
417 dataPtr[7] = 1.0f;
418 dataPtr[8] = 0;
419 dataPtr[9] = 0;
420 dataPtr[10] = 1.0f;
421 dataPtr[11] = 0;
422 dataBuffer->setData(data);
423 Buffer *backendBuffer = nodeManagers->bufferManager()->getOrCreateResource(id: dataBuffer->id());
424 backendBuffer->setRenderer(&renderer);
425 backendBuffer->setManager(nodeManagers->bufferManager());
426 simulateInitializationSync(frontend: dataBuffer.data(), backend: backendBuffer);
427
428 QByteArray indexData;
429 indexData.resize(size: sizeof(uint) * 7);
430 uint *iDataPtr = reinterpret_cast<uint *>(indexData.data());
431 iDataPtr[0] = 0;
432 iDataPtr[1] = 1;
433 iDataPtr[2] = 2;
434 iDataPtr[3] = 3;
435 iDataPtr[4] = static_cast<uint>(-1);
436 iDataPtr[5] = 0;
437 iDataPtr[6] = 1;
438 indexDataBuffer->setData(indexData);
439
440 Buffer *backendIndexBuffer = nodeManagers->bufferManager()->getOrCreateResource(id: indexDataBuffer->id());
441 backendIndexBuffer->setRenderer(&renderer);
442 backendIndexBuffer->setManager(nodeManagers->bufferManager());
443 simulateInitializationSync(frontend: indexDataBuffer.data(), backend: backendIndexBuffer);
444
445 positionAttribute->setBuffer(dataBuffer.data());
446 positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
447 positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
448 positionAttribute->setVertexSize(3);
449 positionAttribute->setCount(4);
450 positionAttribute->setByteStride(3*sizeof(float));
451 positionAttribute->setByteOffset(0);
452 positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
453
454 indexAttribute->setBuffer(indexDataBuffer.data());
455 indexAttribute->setVertexBaseType(Qt3DRender::QAttribute::UnsignedInt);
456 indexAttribute->setCount(7);
457 indexAttribute->setAttributeType(Qt3DRender::QAttribute::IndexAttribute);
458
459 geometry->addAttribute(attribute: positionAttribute.data());
460 geometry->addAttribute(attribute: indexAttribute.data());
461
462 geometryRenderer->setGeometry(geometry);
463 geometryRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::LineStrip);
464 geometryRenderer->setPrimitiveRestartEnabled(true);
465 geometryRenderer->setRestartIndexValue(-1);
466
467 Attribute *backendAttribute = nodeManagers->attributeManager()->getOrCreateResource(id: positionAttribute->id());
468 backendAttribute->setRenderer(&renderer);
469 simulateInitializationSync(frontend: positionAttribute.data(), backend: backendAttribute);
470
471 Attribute *backendIndexAttribute = nodeManagers->attributeManager()->getOrCreateResource(id: indexAttribute->id());
472 backendIndexAttribute->setRenderer(&renderer);
473 simulateInitializationSync(frontend: indexAttribute.data(), backend: backendIndexAttribute);
474
475 Geometry *backendGeometry = nodeManagers->geometryManager()->getOrCreateResource(id: geometry->id());
476 backendGeometry->setRenderer(&renderer);
477 simulateInitializationSync(frontend: geometry, backend: backendGeometry);
478
479 GeometryRenderer *backendRenderer = nodeManagers->geometryRendererManager()->getOrCreateResource(id: geometryRenderer->id());
480 backendRenderer->setRenderer(&renderer);
481 backendRenderer->setManager(nodeManagers->geometryRendererManager());
482 simulateInitializationSync(frontend: geometryRenderer.data(), backend: backendRenderer);
483
484 // WHEN
485 visitor.apply(renderer: backendRenderer, id: Qt3DCore::QNodeId());
486
487 // THEN
488 QCOMPARE(visitor.segmentCount(), uint(4));
489 QVERIFY(visitor.verifySegment(0, 0,1, Vector3D(0,0,0), Vector3D(1,0,0)));
490 QVERIFY(visitor.verifySegment(1, 1,2, Vector3D(1,0,0), Vector3D(1,1,0)));
491 QVERIFY(visitor.verifySegment(2, 2,3, Vector3D(1,1,0), Vector3D(0,1,0)));
492 QVERIFY(visitor.verifySegment(3, 0,1, Vector3D(0,0,0), Vector3D(1,0,0)));
493 }
494
495 void testVisitLineLoop()
496 {
497 QScopedPointer<NodeManagers> nodeManagers(new NodeManagers());
498 Qt3DRender::QGeometry *geometry = new Qt3DRender::QGeometry();
499 QScopedPointer<Qt3DRender::QGeometryRenderer> geometryRenderer(new Qt3DRender::QGeometryRenderer());
500 QScopedPointer<Qt3DRender::QAttribute> positionAttribute(new Qt3DRender::QAttribute());
501 QScopedPointer<Qt3DRender::QBuffer> dataBuffer(new Qt3DRender::QBuffer());
502 TestVisitor visitor(nodeManagers.data());
503 TestRenderer renderer;
504
505 QByteArray data;
506 data.resize(size: sizeof(float) * 3 * 4);
507 float *dataPtr = reinterpret_cast<float *>(data.data());
508 dataPtr[0] = 0;
509 dataPtr[1] = 0;
510 dataPtr[2] = 0.0f;
511 dataPtr[3] = 1.0f;
512 dataPtr[4] = 0;
513 dataPtr[5] = 0;
514 dataPtr[6] = 1.0f;
515 dataPtr[7] = 1.0f;
516 dataPtr[8] = 0;
517 dataPtr[9] = 0;
518 dataPtr[10] = 1.0f;
519 dataPtr[11] = 0;
520 dataBuffer->setData(data);
521 Buffer *backendBuffer = nodeManagers->bufferManager()->getOrCreateResource(id: dataBuffer->id());
522 backendBuffer->setRenderer(&renderer);
523 backendBuffer->setManager(nodeManagers->bufferManager());
524 simulateInitializationSync(frontend: dataBuffer.data(), backend: backendBuffer);
525
526 positionAttribute->setBuffer(dataBuffer.data());
527 positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
528 positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
529 positionAttribute->setVertexSize(3);
530 positionAttribute->setCount(4);
531 positionAttribute->setByteStride(3*4);
532 positionAttribute->setByteOffset(0);
533 positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
534 geometry->addAttribute(attribute: positionAttribute.data());
535
536 geometryRenderer->setGeometry(geometry);
537 geometryRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::LineLoop);
538
539 Attribute *backendAttribute = nodeManagers->attributeManager()->getOrCreateResource(id: positionAttribute->id());
540 backendAttribute->setRenderer(&renderer);
541 simulateInitializationSync(frontend: positionAttribute.data(), backend: backendAttribute);
542
543 Geometry *backendGeometry = nodeManagers->geometryManager()->getOrCreateResource(id: geometry->id());
544 backendGeometry->setRenderer(&renderer);
545 simulateInitializationSync(frontend: geometry, backend: backendGeometry);
546
547 GeometryRenderer *backendRenderer = nodeManagers->geometryRendererManager()->getOrCreateResource(id: geometryRenderer->id());
548 backendRenderer->setRenderer(&renderer);
549 backendRenderer->setManager(nodeManagers->geometryRendererManager());
550 simulateInitializationSync(frontend: geometryRenderer.data(), backend: backendRenderer);
551
552 // WHEN
553 visitor.apply(renderer: backendRenderer, id: Qt3DCore::QNodeId());
554
555 // THEN
556 QCOMPARE(visitor.segmentCount(), uint(4));
557 QVERIFY(visitor.verifySegment(0, 0,1, Vector3D(0,0,0), Vector3D(1,0,0)));
558 QVERIFY(visitor.verifySegment(1, 1,2, Vector3D(1,0,0), Vector3D(1,1,0)));
559 QVERIFY(visitor.verifySegment(2, 2,3, Vector3D(1,1,0), Vector3D(0,1,0)));
560 QVERIFY(visitor.verifySegment(3, 3,0, Vector3D(0,1,0), Vector3D(0,0,0)));
561 }
562
563 void testVisitLineLoopIndexed()
564 {
565 QScopedPointer<NodeManagers> nodeManagers(new NodeManagers());
566 Qt3DRender::QGeometry *geometry = new Qt3DRender::QGeometry();
567 QScopedPointer<Qt3DRender::QGeometryRenderer> geometryRenderer(new Qt3DRender::QGeometryRenderer());
568 QScopedPointer<Qt3DRender::QAttribute> positionAttribute(new Qt3DRender::QAttribute());
569 QScopedPointer<Qt3DRender::QAttribute> indexAttribute(new Qt3DRender::QAttribute());
570 QScopedPointer<Qt3DRender::QBuffer> dataBuffer(new Qt3DRender::QBuffer());
571 QScopedPointer<Qt3DRender::QBuffer> indexDataBuffer(new Qt3DRender::QBuffer());
572 TestVisitor visitor(nodeManagers.data());
573 TestRenderer renderer;
574
575 QByteArray data;
576 data.resize(size: sizeof(float) * 3 * 4);
577 float *dataPtr = reinterpret_cast<float *>(data.data());
578 dataPtr[0] = 0;
579 dataPtr[1] = 0;
580 dataPtr[2] = 0.0f;
581 dataPtr[3] = 1.0f;
582 dataPtr[4] = 0;
583 dataPtr[5] = 0;
584 dataPtr[6] = 1.0;
585 dataPtr[7] = 1.0f;
586 dataPtr[8] = 0;
587 dataPtr[9] = 0;
588 dataPtr[10] = 1.0f;
589 dataPtr[11] = 0;
590 dataBuffer->setData(data);
591 Buffer *backendBuffer = nodeManagers->bufferManager()->getOrCreateResource(id: dataBuffer->id());
592 backendBuffer->setRenderer(&renderer);
593 backendBuffer->setManager(nodeManagers->bufferManager());
594 simulateInitializationSync(frontend: dataBuffer.data(), backend: backendBuffer);
595
596 QByteArray indexData;
597 indexData.resize(size: sizeof(uint) * 8);
598 uint *iDataPtr = reinterpret_cast<uint *>(indexData.data());
599 iDataPtr[0] = 0;
600 iDataPtr[1] = 1;
601 iDataPtr[2] = 2;
602 iDataPtr[3] = 3;
603 iDataPtr[4] = static_cast<uint>(-1);
604 iDataPtr[5] = 0;
605 iDataPtr[6] = 1;
606 iDataPtr[7] = 2;
607 indexDataBuffer->setData(indexData);
608
609 Buffer *backendIndexBuffer = nodeManagers->bufferManager()->getOrCreateResource(id: indexDataBuffer->id());
610 backendIndexBuffer->setRenderer(&renderer);
611 backendIndexBuffer->setManager(nodeManagers->bufferManager());
612 simulateInitializationSync(frontend: indexDataBuffer.data(), backend: backendIndexBuffer);
613
614 positionAttribute->setBuffer(dataBuffer.data());
615 positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
616 positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
617 positionAttribute->setVertexSize(3);
618 positionAttribute->setCount(4);
619 positionAttribute->setByteStride(3*sizeof(float));
620 positionAttribute->setByteOffset(0);
621 positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
622
623 indexAttribute->setBuffer(indexDataBuffer.data());
624 indexAttribute->setVertexBaseType(Qt3DRender::QAttribute::UnsignedInt);
625 indexAttribute->setCount(8);
626 indexAttribute->setAttributeType(Qt3DRender::QAttribute::IndexAttribute);
627
628 geometry->addAttribute(attribute: positionAttribute.data());
629 geometry->addAttribute(attribute: indexAttribute.data());
630
631 geometryRenderer->setGeometry(geometry);
632 geometryRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::LineLoop);
633 geometryRenderer->setPrimitiveRestartEnabled(true);
634 geometryRenderer->setRestartIndexValue(-1);
635
636 Attribute *backendAttribute = nodeManagers->attributeManager()->getOrCreateResource(id: positionAttribute->id());
637 backendAttribute->setRenderer(&renderer);
638 simulateInitializationSync(frontend: positionAttribute.data(), backend: backendAttribute);
639
640 Attribute *backendIndexAttribute = nodeManagers->attributeManager()->getOrCreateResource(id: indexAttribute->id());
641 backendIndexAttribute->setRenderer(&renderer);
642 simulateInitializationSync(frontend: indexAttribute.data(), backend: backendIndexAttribute);
643
644 Geometry *backendGeometry = nodeManagers->geometryManager()->getOrCreateResource(id: geometry->id());
645 backendGeometry->setRenderer(&renderer);
646 simulateInitializationSync(frontend: geometry, backend: backendGeometry);
647
648 GeometryRenderer *backendRenderer = nodeManagers->geometryRendererManager()->getOrCreateResource(id: geometryRenderer->id());
649 backendRenderer->setRenderer(&renderer);
650 backendRenderer->setManager(nodeManagers->geometryRendererManager());
651 simulateInitializationSync(frontend: geometryRenderer.data(), backend: backendRenderer);
652
653 // WHEN
654 visitor.apply(renderer: backendRenderer, id: Qt3DCore::QNodeId());
655
656 // THEN
657 QCOMPARE(visitor.segmentCount(), uint(7));
658 QVERIFY(visitor.verifySegment(0, 0,1, Vector3D(0,0,0), Vector3D(1,0,0)));
659 QVERIFY(visitor.verifySegment(1, 1,2, Vector3D(1,0,0), Vector3D(1,1,0)));
660 QVERIFY(visitor.verifySegment(2, 2,3, Vector3D(1,1,0), Vector3D(0,1,0)));
661 QVERIFY(visitor.verifySegment(3, 3,0, Vector3D(0,1,0), Vector3D(0,0,0)));
662 QVERIFY(visitor.verifySegment(4, 0,1, Vector3D(0,0,0), Vector3D(1,0,0)));
663 QVERIFY(visitor.verifySegment(5, 1,2, Vector3D(1,0,0), Vector3D(1,1,0)));
664 QVERIFY(visitor.verifySegment(6, 2,0, Vector3D(1,1,0), Vector3D(0,0,0)));
665 }
666
667 void testVisitLineAdjacency()
668 {
669 QScopedPointer<NodeManagers> nodeManagers(new NodeManagers());
670 Qt3DRender::QGeometry *geometry = new Qt3DRender::QGeometry();
671 QScopedPointer<Qt3DRender::QGeometryRenderer> geometryRenderer(new Qt3DRender::QGeometryRenderer());
672 QScopedPointer<Qt3DRender::QAttribute> positionAttribute(new Qt3DRender::QAttribute());
673 QScopedPointer<Qt3DRender::QBuffer> dataBuffer(new Qt3DRender::QBuffer());
674 TestVisitor visitor(nodeManagers.data());
675 TestRenderer renderer;
676
677 QByteArray data;
678 data.resize(size: sizeof(float) * 3 * 2 * 2);
679 float *dataPtr = reinterpret_cast<float *>(data.data());
680 dataPtr[0] = 0;
681 dataPtr[1] = 0;
682 dataPtr[2] = 1.0f;
683 dataPtr[3] = 1.0f;
684 dataPtr[4] = 0;
685 dataPtr[5] = 0;
686
687 dataPtr[6] = 0;
688 dataPtr[7] = 1.0f;
689 dataPtr[8] = 0;
690 dataPtr[9] = 1.0f;
691 dataPtr[10] = 0;
692 dataPtr[12] = 0;
693 dataBuffer->setData(data);
694 Buffer *backendBuffer = nodeManagers->bufferManager()->getOrCreateResource(id: dataBuffer->id());
695 backendBuffer->setRenderer(&renderer);
696 backendBuffer->setManager(nodeManagers->bufferManager());
697 simulateInitializationSync(frontend: dataBuffer.data(), backend: backendBuffer);
698
699 positionAttribute->setBuffer(dataBuffer.data());
700 positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
701 positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
702 positionAttribute->setVertexSize(3);
703 positionAttribute->setCount(4);
704 positionAttribute->setByteStride(3*4);
705 positionAttribute->setByteOffset(0);
706 positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
707 geometry->addAttribute(attribute: positionAttribute.data());
708
709 geometryRenderer->setGeometry(geometry);
710 geometryRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::LinesAdjacency);
711
712 Attribute *backendAttribute = nodeManagers->attributeManager()->getOrCreateResource(id: positionAttribute->id());
713 backendAttribute->setRenderer(&renderer);
714 simulateInitializationSync(frontend: positionAttribute.data(), backend: backendAttribute);
715
716 Geometry *backendGeometry = nodeManagers->geometryManager()->getOrCreateResource(id: geometry->id());
717 backendGeometry->setRenderer(&renderer);
718 simulateInitializationSync(frontend: geometry, backend: backendGeometry);
719
720 GeometryRenderer *backendRenderer = nodeManagers->geometryRendererManager()->getOrCreateResource(id: geometryRenderer->id());
721 backendRenderer->setRenderer(&renderer);
722 backendRenderer->setManager(nodeManagers->geometryRendererManager());
723 simulateInitializationSync(frontend: geometryRenderer.data(), backend: backendRenderer);
724
725 // WHEN
726 visitor.apply(renderer: backendRenderer, id: Qt3DCore::QNodeId());
727
728 // THEN
729 QCOMPARE(visitor.segmentCount(), uint(1));
730 QVERIFY(visitor.verifySegment(0, 1, 2, Vector3D(1,0,0), Vector3D(0,1,0)));
731 }
732
733 void testVisitLinesAdjacencyIndexed()
734 {
735 QScopedPointer<NodeManagers> nodeManagers(new NodeManagers());
736 Qt3DRender::QGeometry *geometry = new Qt3DRender::QGeometry();
737 QScopedPointer<Qt3DRender::QGeometryRenderer> geometryRenderer(new Qt3DRender::QGeometryRenderer());
738 QScopedPointer<Qt3DRender::QAttribute> positionAttribute(new Qt3DRender::QAttribute());
739 QScopedPointer<Qt3DRender::QAttribute> indexAttribute(new Qt3DRender::QAttribute());
740 QScopedPointer<Qt3DRender::QBuffer> dataBuffer(new Qt3DRender::QBuffer());
741 QScopedPointer<Qt3DRender::QBuffer> indexDataBuffer(new Qt3DRender::QBuffer());
742 TestVisitor visitor(nodeManagers.data());
743 TestRenderer renderer;
744
745 QByteArray data;
746 data.resize(size: sizeof(float) * 3 * 2 * 2);
747 float *dataPtr = reinterpret_cast<float *>(data.data());
748 dataPtr[0] = 0;
749 dataPtr[1] = 0;
750 dataPtr[2] = 1.0f;
751 dataPtr[3] = 1.0f;
752 dataPtr[4] = 0;
753 dataPtr[5] = 0;
754
755 dataPtr[6] = 0;
756 dataPtr[7] = 1.0f;
757 dataPtr[8] = 0;
758 dataPtr[9] = 1.0f;
759 dataPtr[10] = 0;
760 dataPtr[12] = 0;
761 dataBuffer->setData(data);
762 Buffer *backendBuffer = nodeManagers->bufferManager()->getOrCreateResource(id: dataBuffer->id());
763 backendBuffer->setRenderer(&renderer);
764 backendBuffer->setManager(nodeManagers->bufferManager());
765 simulateInitializationSync(frontend: dataBuffer.data(), backend: backendBuffer);
766
767 QByteArray indexData;
768 indexData.resize(size: sizeof(uint) * 4);
769 uint *iDataPtr = reinterpret_cast<uint *>(indexData.data());
770 iDataPtr[0] = 0;
771 iDataPtr[1] = 1;
772 iDataPtr[2] = 2;
773 iDataPtr[3] = 3;
774 indexDataBuffer->setData(indexData);
775
776 Buffer *backendIndexBuffer = nodeManagers->bufferManager()->getOrCreateResource(id: indexDataBuffer->id());
777 backendIndexBuffer->setRenderer(&renderer);
778 backendIndexBuffer->setManager(nodeManagers->bufferManager());
779 simulateInitializationSync(frontend: indexDataBuffer.data(), backend: backendIndexBuffer);
780
781 positionAttribute->setBuffer(dataBuffer.data());
782 positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
783 positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
784 positionAttribute->setVertexSize(3);
785 positionAttribute->setCount(4);
786 positionAttribute->setByteStride(3*sizeof(float));
787 positionAttribute->setByteOffset(0);
788 positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
789
790 indexAttribute->setBuffer(indexDataBuffer.data());
791 indexAttribute->setVertexBaseType(Qt3DRender::QAttribute::UnsignedInt);
792 indexAttribute->setCount(4);
793 indexAttribute->setAttributeType(Qt3DRender::QAttribute::IndexAttribute);
794
795 geometry->addAttribute(attribute: positionAttribute.data());
796 geometry->addAttribute(attribute: indexAttribute.data());
797
798 geometryRenderer->setGeometry(geometry);
799 geometryRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::LinesAdjacency);
800
801 Attribute *backendAttribute = nodeManagers->attributeManager()->getOrCreateResource(id: positionAttribute->id());
802 backendAttribute->setRenderer(&renderer);
803 simulateInitializationSync(frontend: positionAttribute.data(), backend: backendAttribute);
804
805 Attribute *backendIndexAttribute = nodeManagers->attributeManager()->getOrCreateResource(id: indexAttribute->id());
806 backendIndexAttribute->setRenderer(&renderer);
807 simulateInitializationSync(frontend: indexAttribute.data(), backend: backendIndexAttribute);
808
809 Geometry *backendGeometry = nodeManagers->geometryManager()->getOrCreateResource(id: geometry->id());
810 backendGeometry->setRenderer(&renderer);
811 simulateInitializationSync(frontend: geometry, backend: backendGeometry);
812
813 GeometryRenderer *backendRenderer = nodeManagers->geometryRendererManager()->getOrCreateResource(id: geometryRenderer->id());
814 backendRenderer->setRenderer(&renderer);
815 backendRenderer->setManager(nodeManagers->geometryRendererManager());
816 simulateInitializationSync(frontend: geometryRenderer.data(), backend: backendRenderer);
817
818 // WHEN
819 visitor.apply(renderer: backendRenderer, id: Qt3DCore::QNodeId());
820
821 // THEN
822 QCOMPARE(visitor.segmentCount(), uint(1));
823 QVERIFY(visitor.verifySegment(0, 1, 2, Vector3D(1,0,0), Vector3D(0,1,0)));
824 }
825};
826
827QTEST_MAIN(tst_SegmentVisitor)
828
829#include "tst_segmentvisitor.moc"
830

source code of qt3d/tests/auto/render/segmentvisitor/tst_segmentvisitor.cpp