| 1 | // Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // Copyright (C) 2016 The Qt Company Ltd and/or its subsidiary(-ies). |
| 3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 4 | |
| 5 | #include <QApplication> |
| 6 | |
| 7 | #include <QWidget> |
| 8 | #include <QHBoxLayout> |
| 9 | #include <QLabel> |
| 10 | #include <QPushButton> |
| 11 | #include <QCheckBox> |
| 12 | |
| 13 | #include <Qt3DCore/QEntity> |
| 14 | #include <Qt3DCore/QTransform> |
| 15 | |
| 16 | #include <Qt3DRender/QCamera> |
| 17 | #include <Qt3DRender/QRenderCapture> |
| 18 | #include <Qt3DRender/QGeometryRenderer> |
| 19 | |
| 20 | #include <Qt3DExtras/QPhongMaterial> |
| 21 | #include <Qt3DExtras/QSphereMesh> |
| 22 | #include <Qt3DExtras/QTorusMesh> |
| 23 | |
| 24 | #include <QPropertyAnimation> |
| 25 | |
| 26 | #include "qt3dwindow.h" |
| 27 | #include "orbittransformcontroller.h" |
| 28 | #include "qorbitcameracontroller.h" |
| 29 | #include "mycapture.h" |
| 30 | |
| 31 | #include <QtGui/qquaternion.h> |
| 32 | |
| 33 | Qt3DCore::QEntity *createScene() |
| 34 | { |
| 35 | // Root entity |
| 36 | Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity; |
| 37 | |
| 38 | // Material |
| 39 | Qt3DRender::QMaterial *material = new Qt3DExtras::QPhongMaterial(rootEntity); |
| 40 | |
| 41 | // Torus |
| 42 | Qt3DCore::QEntity *torusEntity = new Qt3DCore::QEntity(rootEntity); |
| 43 | Qt3DExtras::QTorusMesh *torusMesh = new Qt3DExtras::QTorusMesh; |
| 44 | torusMesh->setRadius(5); |
| 45 | torusMesh->setMinorRadius(1); |
| 46 | torusMesh->setRings(100); |
| 47 | torusMesh->setSlices(20); |
| 48 | |
| 49 | Qt3DCore::QTransform *torusTransform = new Qt3DCore::QTransform; |
| 50 | torusTransform->setScale3D(QVector3D(1.5, 1, 0.5)); |
| 51 | torusTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1, 0, 0), 45.0f)); |
| 52 | |
| 53 | torusEntity->addComponent(torusMesh); |
| 54 | torusEntity->addComponent(torusTransform); |
| 55 | torusEntity->addComponent(material); |
| 56 | |
| 57 | // Sphere |
| 58 | Qt3DCore::QEntity *sphereEntity = new Qt3DCore::QEntity(rootEntity); |
| 59 | Qt3DExtras::QSphereMesh *sphereMesh = new Qt3DExtras::QSphereMesh; |
| 60 | sphereMesh->setRadius(3); |
| 61 | |
| 62 | Qt3DCore::QTransform *sphereTransform = new Qt3DCore::QTransform; |
| 63 | OrbitTransformController *controller = new OrbitTransformController(sphereTransform); |
| 64 | controller->setTarget(sphereTransform); |
| 65 | controller->setRadius(20.0f); |
| 66 | |
| 67 | QPropertyAnimation *sphereRotateTransformAnimation = new QPropertyAnimation(sphereTransform); |
| 68 | sphereRotateTransformAnimation->setTargetObject(controller); |
| 69 | sphereRotateTransformAnimation->setPropertyName("angle"); |
| 70 | sphereRotateTransformAnimation->setStartValue(QVariant::fromValue(0)); |
| 71 | sphereRotateTransformAnimation->setEndValue(QVariant::fromValue(360)); |
| 72 | sphereRotateTransformAnimation->setDuration(10000); |
| 73 | sphereRotateTransformAnimation->setLoopCount(-1); |
| 74 | sphereRotateTransformAnimation->start(); |
| 75 | |
| 76 | sphereEntity->addComponent(sphereMesh); |
| 77 | sphereEntity->addComponent(sphereTransform); |
| 78 | sphereEntity->addComponent(material); |
| 79 | |
| 80 | return rootEntity; |
| 81 | } |
| 82 | |
| 83 | int main(int argc, char* argv[]) |
| 84 | { |
| 85 | QApplication app(argc, argv); |
| 86 | Qt3DExtras::Qt3DWindow view; |
| 87 | |
| 88 | Qt3DCore::QEntity *scene = createScene(); |
| 89 | view.setRootEntity(scene); |
| 90 | view.resize(600, 600); |
| 91 | |
| 92 | Qt3DRender::QRenderCapture* capture = new Qt3DRender::QRenderCapture; |
| 93 | view.activeFrameGraph()->setParent(capture); |
| 94 | view.setActiveFrameGraph(capture); |
| 95 | |
| 96 | // Camera |
| 97 | Qt3DRender::QCamera *camera = view.camera(); |
| 98 | camera->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f); |
| 99 | camera->setPosition(QVector3D(0, 0, 40.0f)); |
| 100 | camera->setViewCenter(QVector3D(0, 0, 0)); |
| 101 | |
| 102 | // For camera controls |
| 103 | Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(scene); |
| 104 | camController->setLinearSpeed( 50.0f ); |
| 105 | camController->setLookSpeed( 180.0f ); |
| 106 | camController->setCamera(camera); |
| 107 | |
| 108 | QWidget *container = QWidget::createWindowContainer(&view); |
| 109 | container->setMinimumSize(QSize(600, 600)); |
| 110 | container->setMaximumSize(QSize(600, 600)); |
| 111 | |
| 112 | QPushButton *captureButton = new QPushButton(); |
| 113 | captureButton->setText("Capture"); |
| 114 | QCheckBox *checkBox = new QCheckBox(); |
| 115 | checkBox->setText("continuous"); |
| 116 | |
| 117 | QLabel *imageLabel = new QLabel(); |
| 118 | imageLabel->setBackgroundRole(QPalette::Base); |
| 119 | imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); |
| 120 | imageLabel->setScaledContents(true); |
| 121 | imageLabel->resize(600, 600); |
| 122 | imageLabel->setMinimumSize(QSize(600, 600)); |
| 123 | imageLabel->setMaximumSize(QSize(600, 600)); |
| 124 | |
| 125 | MyCapture myc(capture, imageLabel); |
| 126 | QObject::connect(captureButton, &QPushButton::pressed, &myc, &MyCapture::capture); |
| 127 | QObject::connect(checkBox, &QCheckBox::clicked, &myc, &MyCapture::setContinuous); |
| 128 | |
| 129 | // create widget |
| 130 | QWidget *widget = new QWidget; |
| 131 | widget->setWindowTitle(QStringLiteral("RenderCapture example")); |
| 132 | |
| 133 | QWidget* subWidget = new QWidget; |
| 134 | QVBoxLayout *vLayout = new QVBoxLayout(subWidget); |
| 135 | vLayout->addWidget(captureButton); |
| 136 | vLayout->addWidget(checkBox); |
| 137 | vLayout->addWidget(imageLabel); |
| 138 | |
| 139 | QHBoxLayout *hLayout = new QHBoxLayout(widget); |
| 140 | hLayout->addWidget(container); |
| 141 | hLayout->addWidget(subWidget); |
| 142 | |
| 143 | // Show window |
| 144 | widget->show(); |
| 145 | widget->resize(1250, 700); |
| 146 | |
| 147 | return app.exec(); |
| 148 | } |