| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). | 
| 4 | ** Copyright (C) 2016 The Qt Company Ltd and/or its subsidiary(-ies). | 
| 5 | ** Contact: https://www.qt.io/licensing/ | 
| 6 | ** | 
| 7 | ** This file is part of the Qt3D module of the Qt Toolkit. | 
| 8 | ** | 
| 9 | ** $QT_BEGIN_LICENSE:BSD$ | 
| 10 | ** Commercial License Usage | 
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 12 | ** accordance with the commercial license agreement provided with the | 
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
| 14 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 16 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 17 | ** | 
| 18 | ** BSD License Usage | 
| 19 | ** Alternatively, you may use this file under the terms of the BSD license | 
| 20 | ** as follows: | 
| 21 | ** | 
| 22 | ** "Redistribution and use in source and binary forms, with or without | 
| 23 | ** modification, are permitted provided that the following conditions are | 
| 24 | ** met: | 
| 25 | **   * Redistributions of source code must retain the above copyright | 
| 26 | **     notice, this list of conditions and the following disclaimer. | 
| 27 | **   * Redistributions in binary form must reproduce the above copyright | 
| 28 | **     notice, this list of conditions and the following disclaimer in | 
| 29 | **     the documentation and/or other materials provided with the | 
| 30 | **     distribution. | 
| 31 | **   * Neither the name of The Qt Company Ltd nor the names of its | 
| 32 | **     contributors may be used to endorse or promote products derived | 
| 33 | **     from this software without specific prior written permission. | 
| 34 | ** | 
| 35 | ** | 
| 36 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
| 37 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
| 38 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 
| 39 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 
| 40 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 
| 41 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 
| 42 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
| 43 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
| 44 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
| 45 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
| 46 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." | 
| 47 | ** | 
| 48 | ** $QT_END_LICENSE$ | 
| 49 | ** | 
| 50 | ****************************************************************************/ | 
| 51 |  | 
| 52 | #include <QApplication> | 
| 53 |  | 
| 54 | #include <QWidget> | 
| 55 | #include <QHBoxLayout> | 
| 56 | #include <QLabel> | 
| 57 | #include <QPushButton> | 
| 58 | #include <QCheckBox> | 
| 59 |  | 
| 60 | #include <Qt3DCore/QEntity> | 
| 61 | #include <Qt3DCore/QTransform> | 
| 62 |  | 
| 63 | #include <Qt3DRender/QCamera> | 
| 64 | #include <Qt3DRender/QRenderCapture> | 
| 65 |  | 
| 66 | #include <Qt3DExtras/QPhongMaterial> | 
| 67 | #include <Qt3DExtras/QSphereMesh> | 
| 68 | #include <Qt3DExtras/QTorusMesh> | 
| 69 |  | 
| 70 | #include <QPropertyAnimation> | 
| 71 |  | 
| 72 | #include "qt3dwindow.h" | 
| 73 | #include "orbittransformcontroller.h" | 
| 74 | #include "qorbitcameracontroller.h" | 
| 75 | #include "mycapture.h" | 
| 76 |  | 
| 77 | Qt3DCore::QEntity *createScene() | 
| 78 | { | 
| 79 |     // Root entity | 
| 80 |     Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity; | 
| 81 |  | 
| 82 |     // Material | 
| 83 |     Qt3DRender::QMaterial *material = new Qt3DExtras::QPhongMaterial(rootEntity); | 
| 84 |  | 
| 85 |     // Torus | 
| 86 |     Qt3DCore::QEntity *torusEntity = new Qt3DCore::QEntity(rootEntity); | 
| 87 |     Qt3DExtras::QTorusMesh *torusMesh = new Qt3DExtras::QTorusMesh; | 
| 88 |     torusMesh->setRadius(5); | 
| 89 |     torusMesh->setMinorRadius(1); | 
| 90 |     torusMesh->setRings(100); | 
| 91 |     torusMesh->setSlices(20); | 
| 92 |  | 
| 93 |     Qt3DCore::QTransform *torusTransform = new Qt3DCore::QTransform; | 
| 94 |     torusTransform->setScale3D(QVector3D(1.5, 1, 0.5)); | 
| 95 |     torusTransform->setRotation(QQuaternion::fromAxisAndAngle(axis: QVector3D(1, 0, 0), angle: 45.0f)); | 
| 96 |  | 
| 97 |     torusEntity->addComponent(comp: torusMesh); | 
| 98 |     torusEntity->addComponent(comp: torusTransform); | 
| 99 |     torusEntity->addComponent(comp: material); | 
| 100 |  | 
| 101 |     // Sphere | 
| 102 |     Qt3DCore::QEntity *sphereEntity = new Qt3DCore::QEntity(rootEntity); | 
| 103 |     Qt3DExtras::QSphereMesh *sphereMesh = new Qt3DExtras::QSphereMesh; | 
| 104 |     sphereMesh->setRadius(3); | 
| 105 |  | 
| 106 |     Qt3DCore::QTransform *sphereTransform = new Qt3DCore::QTransform; | 
| 107 |     OrbitTransformController *controller = new OrbitTransformController(sphereTransform); | 
| 108 |     controller->setTarget(sphereTransform); | 
| 109 |     controller->setRadius(20.0f); | 
| 110 |  | 
| 111 |     QPropertyAnimation *sphereRotateTransformAnimation = new QPropertyAnimation(sphereTransform); | 
| 112 |     sphereRotateTransformAnimation->setTargetObject(controller); | 
| 113 |     sphereRotateTransformAnimation->setPropertyName("angle" ); | 
| 114 |     sphereRotateTransformAnimation->setStartValue(QVariant::fromValue(value: 0)); | 
| 115 |     sphereRotateTransformAnimation->setEndValue(QVariant::fromValue(value: 360)); | 
| 116 |     sphereRotateTransformAnimation->setDuration(10000); | 
| 117 |     sphereRotateTransformAnimation->setLoopCount(-1); | 
| 118 |     sphereRotateTransformAnimation->start(); | 
| 119 |  | 
| 120 |     sphereEntity->addComponent(comp: sphereMesh); | 
| 121 |     sphereEntity->addComponent(comp: sphereTransform); | 
| 122 |     sphereEntity->addComponent(comp: material); | 
| 123 |  | 
| 124 |     return rootEntity; | 
| 125 | } | 
| 126 |  | 
| 127 | int main(int argc, char* argv[]) | 
| 128 | { | 
| 129 |     QApplication app(argc, argv); | 
| 130 |     Qt3DExtras::Qt3DWindow view; | 
| 131 |  | 
| 132 |     Qt3DCore::QEntity *scene = createScene(); | 
| 133 |     view.setRootEntity(scene); | 
| 134 |     view.resize(w: 600, h: 600); | 
| 135 |  | 
| 136 |     Qt3DRender::QRenderCapture* capture = new Qt3DRender::QRenderCapture; | 
| 137 |     view.activeFrameGraph()->setParent(capture); | 
| 138 |     view.setActiveFrameGraph(capture); | 
| 139 |  | 
| 140 |     // Camera | 
| 141 |     Qt3DRender::QCamera *camera = view.camera(); | 
| 142 |     camera->lens()->setPerspectiveProjection(fieldOfView: 45.0f, aspect: 16.0f/9.0f, nearPlane: 0.1f, farPlane: 1000.0f); | 
| 143 |     camera->setPosition(QVector3D(0, 0, 40.0f)); | 
| 144 |     camera->setViewCenter(QVector3D(0, 0, 0)); | 
| 145 |  | 
| 146 |     // For camera controls | 
| 147 |     Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(scene); | 
| 148 |     camController->setLinearSpeed( 50.0f ); | 
| 149 |     camController->setLookSpeed( 180.0f ); | 
| 150 |     camController->setCamera(camera); | 
| 151 |  | 
| 152 |     QWidget *container = QWidget::createWindowContainer(window: &view); | 
| 153 |     container->setMinimumSize(QSize(600, 600)); | 
| 154 |     container->setMaximumSize(QSize(600, 600)); | 
| 155 |  | 
| 156 |     QPushButton *captureButton = new QPushButton(); | 
| 157 |     captureButton->setText("Capture" ); | 
| 158 |     QCheckBox *checkBox = new QCheckBox(); | 
| 159 |     checkBox->setText("continuous" ); | 
| 160 |  | 
| 161 |     QLabel *imageLabel = new QLabel(); | 
| 162 |     imageLabel->setBackgroundRole(QPalette::Base); | 
| 163 |     imageLabel->setSizePolicy(hor: QSizePolicy::Ignored, ver: QSizePolicy::Ignored); | 
| 164 |     imageLabel->setScaledContents(true); | 
| 165 |     imageLabel->resize(w: 600, h: 600); | 
| 166 |     imageLabel->setMinimumSize(QSize(600, 600)); | 
| 167 |     imageLabel->setMaximumSize(QSize(600, 600)); | 
| 168 |  | 
| 169 |     MyCapture myc(capture, imageLabel); | 
| 170 |     QObject::connect(sender: captureButton, signal: &QPushButton::pressed, receiver: &myc, slot: &MyCapture::capture); | 
| 171 |     QObject::connect(sender: checkBox, signal: &QCheckBox::clicked, receiver: &myc, slot: &MyCapture::setContinuous); | 
| 172 |  | 
| 173 |     // create widget | 
| 174 |     QWidget *widget = new QWidget; | 
| 175 |     widget->setWindowTitle(QStringLiteral("RenderCapture example" )); | 
| 176 |  | 
| 177 |     QWidget* subWidget = new QWidget; | 
| 178 |     QVBoxLayout *vLayout = new QVBoxLayout(subWidget); | 
| 179 |     vLayout->addWidget(captureButton); | 
| 180 |     vLayout->addWidget(checkBox); | 
| 181 |     vLayout->addWidget(imageLabel); | 
| 182 |  | 
| 183 |     QHBoxLayout *hLayout = new QHBoxLayout(widget); | 
| 184 |     hLayout->addWidget(container); | 
| 185 |     hLayout->addWidget(subWidget); | 
| 186 |  | 
| 187 |     // Show window | 
| 188 |     widget->show(); | 
| 189 |     widget->resize(w: 1250, h: 700); | 
| 190 |  | 
| 191 |     return app.exec(); | 
| 192 | } | 
| 193 |  |