| 1 | // Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include "examplescene.h" |
| 5 | #include "boxentity.h" |
| 6 | |
| 7 | #include <QTimer> |
| 8 | #include <qmath.h> |
| 9 | |
| 10 | ExampleScene::ExampleScene(Qt3DCore::QNode *parent) |
| 11 | : Qt3DCore::QEntity(parent) |
| 12 | , m_timer(new QTimer(this)) |
| 13 | , m_even(true) |
| 14 | { |
| 15 | buildScene(); |
| 16 | QObject::connect(m_timer, SIGNAL(timeout()), SLOT(updateScene())); |
| 17 | m_timer->setInterval(1200); |
| 18 | m_timer->start(); |
| 19 | } |
| 20 | |
| 21 | ExampleScene::~ExampleScene() |
| 22 | { |
| 23 | qDeleteAll(m_entities); |
| 24 | } |
| 25 | |
| 26 | void ExampleScene::updateScene() |
| 27 | { |
| 28 | for (int i = 0; i < m_entities.size(); ++i) { |
| 29 | const bool visible = (i % 2) ^ static_cast<int>(m_even); |
| 30 | m_entities[i]->setParent(visible ? this : nullptr); |
| 31 | } |
| 32 | m_even = !m_even; |
| 33 | } |
| 34 | |
| 35 | void ExampleScene::buildScene() |
| 36 | { |
| 37 | int count = 20; |
| 38 | const float radius = 5.0f; |
| 39 | |
| 40 | for (int i = 0; i < count; ++i) { |
| 41 | BoxEntity *entity = new BoxEntity; |
| 42 | const float angle = M_PI * 2.0f * float(i) / count; |
| 43 | entity->setAngle(angle); |
| 44 | entity->setRadius(radius); |
| 45 | entity->setDiffuseColor(QColor(qFabs(qCos(angle)) * 255, 204, 75)); |
| 46 | m_entities.append(entity); |
| 47 | } |
| 48 | } |
| 49 |