| 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 <QGuiApplication> |
| 7 | |
| 8 | #include <Qt3DCore/qentity.h> |
| 9 | #include <QtGui/QScreen> |
| 10 | |
| 11 | #include <QtWidgets/QApplication> |
| 12 | #include <QtWidgets/QWidget> |
| 13 | #include <QtWidgets/QHBoxLayout> |
| 14 | #include <QtWidgets/QSlider> |
| 15 | #include <QtWidgets/QLabel> |
| 16 | #include <QtWidgets/QPushButton> |
| 17 | |
| 18 | #include <Qt3DRender/qcamera.h> |
| 19 | #include <Qt3DInput/QInputAspect> |
| 20 | #include <Qt3DExtras/qforwardrenderer.h> |
| 21 | #include <Qt3DExtras/qt3dwindow.h> |
| 22 | #include <Qt3DExtras/qfirstpersoncameracontroller.h> |
| 23 | |
| 24 | int main(int argc, char **argv) |
| 25 | { |
| 26 | QApplication app(argc, argv); |
| 27 | |
| 28 | Qt3DExtras::Qt3DWindow *view = new Qt3DExtras::Qt3DWindow(); |
| 29 | view->defaultFrameGraph()->setClearColor(QColor(QRgb(0x4d4d4f))); |
| 30 | QWidget *container = QWidget::createWindowContainer(view); |
| 31 | QSize screenSize = view->screen()->size(); |
| 32 | container->setMinimumSize(QSize(200, 100)); |
| 33 | container->setMaximumSize(screenSize); |
| 34 | |
| 35 | Qt3DInput::QInputAspect *input = new Qt3DInput::QInputAspect; |
| 36 | view->registerAspect(input); |
| 37 | |
| 38 | // Root entity |
| 39 | Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity(); |
| 40 | |
| 41 | // Camera |
| 42 | Qt3DRender::QCamera *cameraEntity = view->camera(); |
| 43 | |
| 44 | cameraEntity->setPosition(QVector3D(0, 0, 20.0f)); |
| 45 | cameraEntity->setUpVector(QVector3D(0, 1, 0)); |
| 46 | cameraEntity->setViewCenter(QVector3D(0, 0, 0)); |
| 47 | |
| 48 | // For camera controls |
| 49 | Qt3DExtras::QFirstPersonCameraController *camController = new Qt3DExtras::QFirstPersonCameraController(rootEntity); |
| 50 | camController->setCamera(cameraEntity); |
| 51 | |
| 52 | // scene |
| 53 | Scene *scene = new Scene(rootEntity); |
| 54 | |
| 55 | // Set root object of the scene |
| 56 | view->setRootEntity(rootEntity); |
| 57 | |
| 58 | // 'Generate New' Button |
| 59 | QPushButton *genNewButton = new QPushButton(); |
| 60 | genNewButton->setText("Generate New"); |
| 61 | QObject::connect(genNewButton, &QPushButton::pressed, scene, &Scene::redrawTexture); |
| 62 | |
| 63 | // current size label |
| 64 | QLabel *sizeLabel = new QLabel(); |
| 65 | sizeLabel->setText(QString("Current Size: 128x128")); |
| 66 | |
| 67 | // 'Change Size' Slider |
| 68 | QSlider *changeSizeSlider = new QSlider(); |
| 69 | changeSizeSlider->setOrientation(Qt::Horizontal); |
| 70 | changeSizeSlider->setMinimum(0); |
| 71 | changeSizeSlider->setMaximum(4); |
| 72 | QObject::connect(changeSizeSlider, &QSlider::valueChanged, scene, [scene, sizeLabel](int value) { |
| 73 | int sz = 128 << value; |
| 74 | scene->setSize(QSize(sz, sz)); |
| 75 | sizeLabel->setText(QString("Current Size: %1x%1").arg(sz)); |
| 76 | }); |
| 77 | |
| 78 | // create widget |
| 79 | QWidget *widget = new QWidget; |
| 80 | widget->setWindowTitle(QStringLiteral("QPaintedTexture Example")); |
| 81 | |
| 82 | QVBoxLayout *hLayout = new QVBoxLayout(widget); |
| 83 | hLayout->addWidget(container, 1); |
| 84 | hLayout->addWidget(genNewButton, 1); |
| 85 | hLayout->addWidget(changeSizeSlider, 1); |
| 86 | hLayout->addWidget(sizeLabel); |
| 87 | |
| 88 | // Show window |
| 89 | widget->show(); |
| 90 | widget->resize(1200, 800); |
| 91 | |
| 92 | return app.exec(); |
| 93 | } |