1/****************************************************************************
2**
3** Copyright (C) 2017 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 TestReader : public CoordinateReader
44{
45public:
46 TestReader(NodeManagers *manager)
47 : CoordinateReader(manager)
48 {
49
50 }
51 NodeManagers *manager() const
52 {
53 return m_manager;
54 }
55
56 Attribute *attribute() const
57 {
58 return m_attribute;
59 }
60
61 Buffer *buffer() const
62 {
63 return m_buffer;
64 }
65
66 BufferInfo bufferInfo() const
67 {
68 return m_bufferInfo;
69 }
70 bool verifyCoordinate(uint index, Vector4D value)
71 {
72 return qFuzzyCompare(v1: getCoordinate(vertexIndex: index), v2: value);
73 }
74};
75
76class tst_CoordinateReader : public Qt3DCore::QBackendNodeTester
77{
78 Q_OBJECT
79
80private Q_SLOTS:
81
82 void checkInitialize()
83 {
84 // WHEN
85 QScopedPointer<NodeManagers> nodeManagers(new NodeManagers());
86 TestReader reader(nodeManagers.data());
87
88 // THEN
89 QCOMPARE(reader.manager(), nodeManagers.data());
90 }
91
92 void checkSetEmptyGeometry()
93 {
94 QScopedPointer<NodeManagers> nodeManagers(new NodeManagers());
95 QScopedPointer<Qt3DRender::QGeometryRenderer> geometryRenderer(
96 new Qt3DRender::QGeometryRenderer());
97 TestReader reader(nodeManagers.data());
98 TestRenderer renderer;
99
100 GeometryRenderer *backendRenderer = nodeManagers->geometryRendererManager()
101 ->getOrCreateResource(id: geometryRenderer->id());
102 backendRenderer->setRenderer(&renderer);
103 backendRenderer->setManager(nodeManagers->geometryRendererManager());
104 simulateInitializationSync(frontend: geometryRenderer.data(), backend: backendRenderer);
105
106 // WHEN
107 bool ret = reader.setGeometry(renderer: backendRenderer, attributeName: QString(""));
108
109 // THEN
110 QCOMPARE(ret, false);
111 QCOMPARE(reader.attribute(), nullptr);
112 QCOMPARE(reader.buffer(), nullptr);
113 }
114
115 void checkSetGeometry()
116 {
117 QScopedPointer<NodeManagers> nodeManagers(new NodeManagers());
118 Qt3DRender::QGeometry *geometry = new Qt3DRender::QGeometry();
119 QScopedPointer<Qt3DRender::QGeometryRenderer> geometryRenderer(
120 new Qt3DRender::QGeometryRenderer());
121 QScopedPointer<Qt3DRender::QAttribute> positionAttribute(new Qt3DRender::QAttribute());
122 QScopedPointer<Qt3DRender::QBuffer> dataBuffer(new Qt3DRender::QBuffer());
123 TestReader reader(nodeManagers.data());
124 TestRenderer renderer;
125
126 QByteArray data;
127 data.resize(size: sizeof(float) * 3 * 3 * 2);
128 float *dataPtr = reinterpret_cast<float *>(data.data());
129 dataPtr[0] = 0;
130 dataPtr[1] = 0;
131 dataPtr[2] = 1.0f;
132 dataPtr[3] = 1.0f;
133 dataPtr[4] = 0;
134 dataPtr[5] = 0;
135 dataPtr[6] = 0;
136 dataPtr[7] = 1.0f;
137 dataPtr[8] = 0;
138
139 dataPtr[9] = 0;
140 dataPtr[10] = 0;
141 dataPtr[11] = 1.0f;
142 dataPtr[12] = 1.0f;
143 dataPtr[13] = 0;
144 dataPtr[14] = 0;
145 dataPtr[15] = 0;
146 dataPtr[16] = 1.0f;
147 dataPtr[17] = 0;
148 dataBuffer->setData(data);
149 Buffer *backendBuffer = nodeManagers->bufferManager()->getOrCreateResource(id: dataBuffer->id());
150 backendBuffer->setRenderer(&renderer);
151 backendBuffer->setManager(nodeManagers->bufferManager());
152 simulateInitializationSync(frontend: dataBuffer.data(), backend: backendBuffer);
153
154 positionAttribute->setBuffer(dataBuffer.data());
155 positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
156 positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
157 positionAttribute->setVertexSize(3);
158 positionAttribute->setCount(6);
159 positionAttribute->setByteStride(3 * 4);
160 positionAttribute->setByteOffset(0);
161 positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
162 geometry->addAttribute(attribute: positionAttribute.data());
163
164 geometryRenderer->setGeometry(geometry);
165 geometryRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles);
166
167 Attribute *backendAttribute = nodeManagers->attributeManager()
168 ->getOrCreateResource(id: positionAttribute->id());
169 backendAttribute->setRenderer(&renderer);
170 simulateInitializationSync(frontend: positionAttribute.data(), backend: backendAttribute);
171
172 Geometry *backendGeometry = nodeManagers->geometryManager()
173 ->getOrCreateResource(id: geometry->id());
174 backendGeometry->setRenderer(&renderer);
175 simulateInitializationSync(frontend: geometry, backend: backendGeometry);
176
177 GeometryRenderer *backendRenderer = nodeManagers->geometryRendererManager()
178 ->getOrCreateResource(id: geometryRenderer->id());
179 backendRenderer->setRenderer(&renderer);
180 backendRenderer->setManager(nodeManagers->geometryRendererManager());
181 simulateInitializationSync(frontend: geometryRenderer.data(), backend: backendRenderer);
182
183 // WHEN
184 bool ret = reader.setGeometry(renderer: backendRenderer,
185 attributeName: Qt3DRender::QAttribute::defaultPositionAttributeName());
186
187 // THEN
188 QCOMPARE(ret, true);
189 QCOMPARE(reader.attribute(), backendAttribute);
190 QCOMPARE(reader.buffer(), backendBuffer);
191 QCOMPARE(reader.bufferInfo().type, Qt3DRender::QAttribute::Float);
192 QCOMPARE(reader.bufferInfo().dataSize, 3u);
193 QCOMPARE(reader.bufferInfo().count, 6u);
194 QCOMPARE(reader.bufferInfo().byteStride, 12u);
195 QCOMPARE(reader.bufferInfo().byteOffset, 0u);
196 }
197
198 void testReadCoordinate()
199 {
200 QScopedPointer<NodeManagers> nodeManagers(new NodeManagers());
201 Qt3DRender::QGeometry *geometry = new Qt3DRender::QGeometry();
202 QScopedPointer<Qt3DRender::QGeometryRenderer> geometryRenderer(
203 new Qt3DRender::QGeometryRenderer());
204 QScopedPointer<Qt3DRender::QAttribute> positionAttribute(new Qt3DRender::QAttribute());
205 QScopedPointer<Qt3DRender::QBuffer> dataBuffer(new Qt3DRender::QBuffer());
206 TestReader reader(nodeManagers.data());
207 TestRenderer renderer;
208
209 QByteArray data;
210 data.resize(size: sizeof(float) * 3 * 3 * 2);
211 float *dataPtr = reinterpret_cast<float *>(data.data());
212 dataPtr[0] = 0;
213 dataPtr[1] = 0;
214 dataPtr[2] = 1.0f;
215 dataPtr[3] = 1.0f;
216 dataPtr[4] = 0;
217 dataPtr[5] = 0;
218 dataPtr[6] = 0;
219 dataPtr[7] = 1.0f;
220 dataPtr[8] = 0;
221
222 dataPtr[9] = 0;
223 dataPtr[10] = 0;
224 dataPtr[11] = 1.0f;
225 dataPtr[12] = 1.0f;
226 dataPtr[13] = 0;
227 dataPtr[14] = 0;
228 dataPtr[15] = 0;
229 dataPtr[16] = 1.0f;
230 dataPtr[17] = 0;
231 dataBuffer->setData(data);
232 Buffer *backendBuffer = nodeManagers->bufferManager()->getOrCreateResource(id: dataBuffer->id());
233 backendBuffer->setRenderer(&renderer);
234 backendBuffer->setManager(nodeManagers->bufferManager());
235 simulateInitializationSync(frontend: dataBuffer.data(), backend: backendBuffer);
236
237 positionAttribute->setBuffer(dataBuffer.data());
238 positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
239 positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
240 positionAttribute->setVertexSize(3);
241 positionAttribute->setCount(6);
242 positionAttribute->setByteStride(3 * 4);
243 positionAttribute->setByteOffset(0);
244 positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
245 geometry->addAttribute(attribute: positionAttribute.data());
246
247 geometryRenderer->setGeometry(geometry);
248 geometryRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles);
249
250 Attribute *backendAttribute = nodeManagers->attributeManager()
251 ->getOrCreateResource(id: positionAttribute->id());
252 backendAttribute->setRenderer(&renderer);
253 simulateInitializationSync(frontend: positionAttribute.data(), backend: backendAttribute);
254
255 Geometry *backendGeometry = nodeManagers->geometryManager()
256 ->getOrCreateResource(id: geometry->id());
257 backendGeometry->setRenderer(&renderer);
258 simulateInitializationSync(frontend: geometry, backend: backendGeometry);
259
260 GeometryRenderer *backendRenderer = nodeManagers->geometryRendererManager()
261 ->getOrCreateResource(id: geometryRenderer->id());
262 backendRenderer->setRenderer(&renderer);
263 backendRenderer->setManager(nodeManagers->geometryRendererManager());
264 simulateInitializationSync(frontend: geometryRenderer.data(), backend: backendRenderer);
265
266 // WHEN
267 bool ret = reader.setGeometry(renderer: backendRenderer,
268 attributeName: Qt3DRender::QAttribute::defaultPositionAttributeName());
269
270 // THEN
271 QCOMPARE(ret, true);
272
273 QVERIFY(reader.verifyCoordinate(0, Vector4D(0, 0, 1, 1)));
274 QVERIFY(reader.verifyCoordinate(1, Vector4D(1, 0, 0, 1)));
275 QVERIFY(reader.verifyCoordinate(2, Vector4D(0, 1, 0, 1)));
276 QVERIFY(reader.verifyCoordinate(3, Vector4D(0, 0, 1, 1)));
277 QVERIFY(reader.verifyCoordinate(4, Vector4D(1, 0, 0, 1)));
278 QVERIFY(reader.verifyCoordinate(5, Vector4D(0, 1, 0, 1)));
279 }
280
281 void testReadCoordinateVec4()
282 {
283 QScopedPointer<NodeManagers> nodeManagers(new NodeManagers());
284 Qt3DRender::QGeometry *geometry = new Qt3DRender::QGeometry();
285 QScopedPointer<Qt3DRender::QGeometryRenderer> geometryRenderer(
286 new Qt3DRender::QGeometryRenderer());
287 QScopedPointer<Qt3DRender::QAttribute> positionAttribute(new Qt3DRender::QAttribute());
288 QScopedPointer<Qt3DRender::QBuffer> dataBuffer(new Qt3DRender::QBuffer());
289 TestReader reader(nodeManagers.data());
290 TestRenderer renderer;
291
292 QByteArray data;
293 data.resize(size: sizeof(float) * 4 * 3 * 2);
294 float *dataPtr = reinterpret_cast<float *>(data.data());
295 dataPtr[0] = 0;
296 dataPtr[1] = 0;
297 dataPtr[2] = 1.0f;
298 dataPtr[3] = 1.0f;
299
300 dataPtr[4] = 1.0f;
301 dataPtr[5] = 0;
302 dataPtr[6] = 0;
303 dataPtr[7] = 1.0f;
304
305 dataPtr[8] = 0;
306 dataPtr[9] = 1.0f;
307 dataPtr[10] = 0;
308 dataPtr[11] = 0;
309
310 dataPtr[12] = 0;
311 dataPtr[13] = 0;
312 dataPtr[14] = 1.0f;
313 dataPtr[15] = 0;
314
315 dataPtr[16] = 1.0f;
316 dataPtr[17] = 0;
317 dataPtr[18] = 0;
318 dataPtr[19] = 0;
319
320 dataPtr[20] = 0;
321 dataPtr[21] = 1.0f;
322 dataPtr[22] = 0;
323 dataPtr[23] = 1.0f;
324
325 dataBuffer->setData(data);
326 Buffer *backendBuffer = nodeManagers->bufferManager()->getOrCreateResource(id: dataBuffer->id());
327 backendBuffer->setRenderer(&renderer);
328 backendBuffer->setManager(nodeManagers->bufferManager());
329 simulateInitializationSync(frontend: dataBuffer.data(), backend: backendBuffer);
330
331 positionAttribute->setBuffer(dataBuffer.data());
332 positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
333 positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
334 positionAttribute->setVertexSize(4);
335 positionAttribute->setCount(6);
336 positionAttribute->setByteStride(4 * 4);
337 positionAttribute->setByteOffset(0);
338 positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
339 geometry->addAttribute(attribute: positionAttribute.data());
340
341 geometryRenderer->setGeometry(geometry);
342 geometryRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles);
343
344 Attribute *backendAttribute = nodeManagers->attributeManager()
345 ->getOrCreateResource(id: positionAttribute->id());
346 backendAttribute->setRenderer(&renderer);
347 simulateInitializationSync(frontend: positionAttribute.data(), backend: backendAttribute);
348
349 Geometry *backendGeometry = nodeManagers->geometryManager()
350 ->getOrCreateResource(id: geometry->id());
351 backendGeometry->setRenderer(&renderer);
352 simulateInitializationSync(frontend: geometry, backend: backendGeometry);
353
354 GeometryRenderer *backendRenderer = nodeManagers->geometryRendererManager()
355 ->getOrCreateResource(id: geometryRenderer->id());
356 backendRenderer->setRenderer(&renderer);
357 backendRenderer->setManager(nodeManagers->geometryRendererManager());
358 simulateInitializationSync(frontend: geometryRenderer.data(), backend: backendRenderer);
359
360 // WHEN
361 bool ret = reader.setGeometry(renderer: backendRenderer,
362 attributeName: Qt3DRender::QAttribute::defaultPositionAttributeName());
363
364 // THEN
365 QCOMPARE(ret, true);
366
367 QVERIFY(reader.verifyCoordinate(0, Vector4D(0, 0, 1, 1)));
368 QVERIFY(reader.verifyCoordinate(1, Vector4D(1, 0, 0, 1)));
369 QVERIFY(reader.verifyCoordinate(2, Vector4D(0, 1, 0, 0)));
370 QVERIFY(reader.verifyCoordinate(3, Vector4D(0, 0, 1, 0)));
371 QVERIFY(reader.verifyCoordinate(4, Vector4D(1, 0, 0, 0)));
372 QVERIFY(reader.verifyCoordinate(5, Vector4D(0, 1, 0, 1)));
373 }
374
375 void testReadCoordinateFromAttribute()
376 {
377 QScopedPointer<NodeManagers> nodeManagers(new NodeManagers());
378 Qt3DRender::QGeometry *geometry = new Qt3DRender::QGeometry();
379 QScopedPointer<Qt3DRender::QGeometryRenderer> geometryRenderer(
380 new Qt3DRender::QGeometryRenderer());
381 QScopedPointer<Qt3DRender::QAttribute> positionAttribute(new Qt3DRender::QAttribute());
382 QScopedPointer<Qt3DRender::QAttribute> texcoordAttribute(new Qt3DRender::QAttribute());
383 QScopedPointer<Qt3DRender::QBuffer> dataBuffer(new Qt3DRender::QBuffer());
384 TestReader reader(nodeManagers.data());
385 TestRenderer renderer;
386
387 QByteArray data;
388 data.resize(size: sizeof(float) * 3 * 3 * 2);
389 float *dataPtr = reinterpret_cast<float *>(data.data());
390 dataPtr[0] = 0;
391 dataPtr[1] = 0;
392 dataPtr[2] = 1.0f;
393
394 dataPtr[3] = 1.0f;
395 dataPtr[4] = 0;
396 dataPtr[5] = 0;
397
398 dataPtr[6] = 0;
399 dataPtr[7] = 1.0f;
400 dataPtr[8] = 0;
401
402 dataPtr[9] = 0;
403 dataPtr[10] = 0;
404 dataPtr[11] = 1.0f;
405
406 dataPtr[12] = 1.0f;
407 dataPtr[13] = 0;
408 dataPtr[14] = 0;
409
410 dataPtr[15] = 0;
411 dataPtr[16] = 1.0f;
412 dataPtr[17] = 0;
413
414 dataBuffer->setData(data);
415 Buffer *backendBuffer = nodeManagers->bufferManager()->getOrCreateResource(id: dataBuffer->id());
416 backendBuffer->setRenderer(&renderer);
417 backendBuffer->setManager(nodeManagers->bufferManager());
418 simulateInitializationSync(frontend: dataBuffer.data(), backend: backendBuffer);
419
420 positionAttribute->setBuffer(dataBuffer.data());
421 positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
422 positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
423 positionAttribute->setVertexSize(3);
424 positionAttribute->setCount(3);
425 positionAttribute->setByteStride(3 * 4 * 2);
426 positionAttribute->setByteOffset(0);
427 positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
428 geometry->addAttribute(attribute: positionAttribute.data());
429
430 texcoordAttribute->setBuffer(dataBuffer.data());
431 texcoordAttribute->setName(Qt3DRender::QAttribute::defaultTextureCoordinateAttributeName());
432 texcoordAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
433 texcoordAttribute->setVertexSize(3);
434 texcoordAttribute->setCount(6);
435 texcoordAttribute->setByteStride(3 * 4 * 2);
436 texcoordAttribute->setByteOffset(3 * 4);
437 texcoordAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
438 geometry->addAttribute(attribute: texcoordAttribute.data());
439
440 geometryRenderer->setGeometry(geometry);
441 geometryRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles);
442
443 Attribute *backendAttribute = nodeManagers->attributeManager()->getOrCreateResource(
444 id: positionAttribute->id());
445 backendAttribute->setRenderer(&renderer);
446 simulateInitializationSync(frontend: positionAttribute.data(), backend: backendAttribute);
447
448 Attribute *backendTexcoordAttribute = nodeManagers->attributeManager()
449 ->getOrCreateResource(id: texcoordAttribute->id());
450 backendTexcoordAttribute->setRenderer(&renderer);
451 simulateInitializationSync(frontend: texcoordAttribute.data(), backend: backendTexcoordAttribute);
452
453 Geometry *backendGeometry = nodeManagers->geometryManager()
454 ->getOrCreateResource(id: geometry->id());
455 backendGeometry->setRenderer(&renderer);
456 simulateInitializationSync(frontend: geometry, backend: backendGeometry);
457
458 GeometryRenderer *backendRenderer = nodeManagers->geometryRendererManager()
459 ->getOrCreateResource(id: geometryRenderer->id());
460 backendRenderer->setRenderer(&renderer);
461 backendRenderer->setManager(nodeManagers->geometryRendererManager());
462 simulateInitializationSync(frontend: geometryRenderer.data(), backend: backendRenderer);
463
464 // WHEN
465 bool ret = reader.setGeometry(renderer: backendRenderer,
466 attributeName: Qt3DRender::QAttribute::defaultPositionAttributeName());
467
468 // THEN
469 QCOMPARE(ret, true);
470
471 QVERIFY(reader.verifyCoordinate(0, Vector4D(0, 0, 1, 1)));
472 QVERIFY(reader.verifyCoordinate(1, Vector4D(0, 1, 0, 1)));
473 QVERIFY(reader.verifyCoordinate(2, Vector4D(1, 0, 0, 1)));
474
475 // WHEN
476 ret = reader.setGeometry(renderer: backendRenderer,
477 attributeName: Qt3DRender::QAttribute::defaultTextureCoordinateAttributeName());
478
479 // THEN
480 QCOMPARE(ret, true);
481
482 QVERIFY(reader.verifyCoordinate(0, Vector4D(1, 0, 0, 1)));
483 QVERIFY(reader.verifyCoordinate(1, Vector4D(0, 0, 1, 1)));
484 QVERIFY(reader.verifyCoordinate(2, Vector4D(0, 1, 0, 1)));
485 }
486
487};
488
489QTEST_MAIN(tst_CoordinateReader)
490
491#include "tst_coordinatereader.moc"
492

source code of qt3d/tests/auto/render/coordinatereader/tst_coordinatereader.cpp