| 1 | // Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include "scene.h" |
| 5 | |
| 6 | #include <QtCore/QDebug> |
| 7 | #include <QtCore/QTimer> |
| 8 | #include <QtCore/QDateTime> |
| 9 | |
| 10 | #include <QtGui/QPainter> |
| 11 | #include <QtGui/qquaternion.h> |
| 12 | |
| 13 | #include <Qt3DRender/QPaintedTextureImage> |
| 14 | #include <Qt3DRender/QTexture> |
| 15 | #include <Qt3DRender/QGeometryRenderer> |
| 16 | |
| 17 | #include <Qt3DExtras/QCuboidMesh> |
| 18 | #include <Qt3DExtras/QDiffuseMapMaterial> |
| 19 | |
| 20 | class MyTextureImage : public Qt3DRender::QPaintedTextureImage |
| 21 | { |
| 22 | public: |
| 23 | MyTextureImage(int w, int h) |
| 24 | { |
| 25 | setSize(QSize(w, h)); |
| 26 | } |
| 27 | |
| 28 | protected: |
| 29 | void paint(QPainter *painter) override |
| 30 | { |
| 31 | int w = painter->device()->width(); |
| 32 | int h = painter->device()->height(); |
| 33 | |
| 34 | painter->setViewport(0, h, w, -h); |
| 35 | |
| 36 | // clear to white |
| 37 | painter->fillRect(0, 0, w, h, QColor(255, 255, 255)); |
| 38 | |
| 39 | // draw some text |
| 40 | painter->setPen(QColor(255, 0, 0)); |
| 41 | painter->setFont(QFont("Times", 30, QFont::Normal, true)); |
| 42 | painter->drawText(w / 8, h / 6, QDateTime::currentDateTime().toString()); |
| 43 | |
| 44 | painter->setPen(QPen(QBrush(QColor(0, 0, 0)) ,10)); |
| 45 | painter->setBrush(QColor(0, 0, 255)); |
| 46 | |
| 47 | // draw some circles |
| 48 | for (int i = 0; i < m_circleCount; i++) { |
| 49 | float r = w / 8.f; |
| 50 | float cw = 0.25 * w + 0.5 * h * (i % 2); |
| 51 | float ch = 0.375 * w + 0.375 * h * (i / 2); |
| 52 | painter->drawEllipse(QPointF(cw, ch), r, r); |
| 53 | } |
| 54 | |
| 55 | m_circleCount = (m_circleCount % 4) + 1; |
| 56 | } |
| 57 | |
| 58 | int m_circleCount = 1; |
| 59 | }; |
| 60 | |
| 61 | Scene::Scene(Qt3DCore::QEntity *rootEntity) |
| 62 | : m_rootEntity(rootEntity) |
| 63 | { |
| 64 | // Cuboid shape data |
| 65 | Qt3DExtras::QCuboidMesh *cuboid = new Qt3DExtras::QCuboidMesh(); |
| 66 | |
| 67 | // CuboidMesh Transform |
| 68 | m_transform = new Qt3DCore::QTransform(); |
| 69 | m_transform->setScale(4.0f);; |
| 70 | |
| 71 | // create texture |
| 72 | m_paintedTextureImage = new MyTextureImage(128, 128); |
| 73 | m_paintedTextureImage->update(); |
| 74 | |
| 75 | Qt3DRender::QTexture2D *tex = new Qt3DRender::QTexture2D(); |
| 76 | tex->addTextureImage(m_paintedTextureImage); |
| 77 | |
| 78 | Qt3DExtras::QDiffuseMapMaterial *mat = new Qt3DExtras::QDiffuseMapMaterial(); |
| 79 | mat->setAmbient(QColor(64, 64, 64)); |
| 80 | mat->setSpecular(QColor(255, 255, 255)); |
| 81 | mat->setDiffuse(tex); |
| 82 | |
| 83 | //Cuboid |
| 84 | m_cuboidEntity = new Qt3DCore::QEntity(m_rootEntity); |
| 85 | m_cuboidEntity->addComponent(cuboid); |
| 86 | m_cuboidEntity->addComponent(mat); |
| 87 | m_cuboidEntity->addComponent(m_transform); |
| 88 | |
| 89 | QTimer *timer = new QTimer(this); |
| 90 | connect(timer, &QTimer::timeout, this, &Scene::updateTimer); |
| 91 | timer->start(50); |
| 92 | } |
| 93 | |
| 94 | Scene::~Scene() |
| 95 | { |
| 96 | } |
| 97 | |
| 98 | void Scene::redrawTexture() |
| 99 | { |
| 100 | m_paintedTextureImage->update(); |
| 101 | } |
| 102 | |
| 103 | void Scene::setSize(QSize size) |
| 104 | { |
| 105 | m_paintedTextureImage->setSize(size); |
| 106 | } |
| 107 | |
| 108 | void Scene::updateTimer() |
| 109 | { |
| 110 | m_transform->setRotation(QQuaternion::fromAxisAndAngle(0, 1, 0, m_angle)); |
| 111 | m_angle += 0.4f; |
| 112 | } |