| 1 | // Copyright (C) 2017 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QGuiApplication> |
| 5 | #include <QAnimationDriver> |
| 6 | #include <QPropertyAnimation> |
| 7 | #include <QQmlComponent> |
| 8 | #include <QQmlEngine> |
| 9 | |
| 10 | #include <Qt3DCore/QEntity> |
| 11 | #include <Qt3DCore/QAspectEngine> |
| 12 | #include <Qt3DCore/QTransform> |
| 13 | #include <Qt3DRender/QCamera> |
| 14 | |
| 15 | #include <Qt3DInput/QInputAspect> |
| 16 | |
| 17 | #include <Qt3DRender/QRenderAspect> |
| 18 | #include <Qt3DRender/QEffect> |
| 19 | #include <Qt3DRender/QMaterial> |
| 20 | #include <Qt3DExtras/QForwardRenderer> |
| 21 | #include <Qt3DQuickScene2D/QScene2D> |
| 22 | #include <Qt3DExtras/QPlaneMesh> |
| 23 | #include <Qt3DRender/QTextureWrapMode> |
| 24 | #include <Qt3DRender/QClearBuffers> |
| 25 | #include <Qt3DRender/QTexture> |
| 26 | #include <Qt3DRender/QGeometryRenderer> |
| 27 | |
| 28 | #include "qt3dwindow.h" |
| 29 | #include "qfirstpersoncameracontroller.h" |
| 30 | #include "planematerial.h" |
| 31 | |
| 32 | #include <QtGui/qquaternion.h> |
| 33 | |
| 34 | int main(int argc, char *argv[]) |
| 35 | { |
| 36 | QGuiApplication app(argc, argv); |
| 37 | |
| 38 | Qt3DExtras::Qt3DWindow view; |
| 39 | |
| 40 | // Scene Root |
| 41 | Qt3DCore::QEntity *sceneRoot = new Qt3DCore::QEntity(); |
| 42 | |
| 43 | // Scene Camera |
| 44 | Qt3DRender::QCamera *basicCamera = view.camera(); |
| 45 | basicCamera->setProjectionType(Qt3DRender::QCameraLens::PerspectiveProjection); |
| 46 | basicCamera->setAspectRatio(view.width() / view.height()); |
| 47 | basicCamera->setUpVector(QVector3D(0.0f, 1.0f, 0.0f)); |
| 48 | basicCamera->setPosition(QVector3D(0.0f, 0.0f, 6.0f)); |
| 49 | basicCamera->setViewCenter(QVector3D(0.0f, 0.0f, 0.0f)); |
| 50 | basicCamera->setNearPlane(0.1f); |
| 51 | basicCamera->setFarPlane(1000.0f); |
| 52 | basicCamera->setFieldOfView(45.0f); |
| 53 | |
| 54 | // For camera controls |
| 55 | Qt3DExtras::QFirstPersonCameraController *camController = new Qt3DExtras::QFirstPersonCameraController(sceneRoot); |
| 56 | camController->setCamera(basicCamera); |
| 57 | |
| 58 | Qt3DRender::QFrameGraphNode* frameGraphNode = view.activeFrameGraph(); |
| 59 | while (frameGraphNode->childNodes().size() > 0) |
| 60 | frameGraphNode = (Qt3DRender::QFrameGraphNode*)frameGraphNode->childNodes().at(0); |
| 61 | view.defaultFrameGraph()->setClearColor(QColor::fromRgbF(1.0f, 1.0f, 1.0f)); |
| 62 | Qt3DRender::Quick::QScene2D *qmlTextureRenderer = new Qt3DRender::Quick::QScene2D(frameGraphNode); |
| 63 | |
| 64 | Qt3DRender::QTexture2D* offscreenTexture = new Qt3DRender::QTexture2D(qmlTextureRenderer); |
| 65 | offscreenTexture->setSize(1024, 1024); |
| 66 | offscreenTexture->setFormat(Qt3DRender::QAbstractTexture::RGBA8_UNorm); |
| 67 | offscreenTexture->setGenerateMipMaps(true); |
| 68 | offscreenTexture->setMagnificationFilter(Qt3DRender::QAbstractTexture::Linear); |
| 69 | offscreenTexture->setMinificationFilter(Qt3DRender::QAbstractTexture::Linear); |
| 70 | offscreenTexture->setWrapMode(Qt3DRender::QTextureWrapMode(Qt3DRender::QTextureWrapMode::ClampToEdge, offscreenTexture)); |
| 71 | |
| 72 | Qt3DRender::QRenderTargetOutput *output = new Qt3DRender::QRenderTargetOutput(qmlTextureRenderer); |
| 73 | output->setAttachmentPoint(Qt3DRender::QRenderTargetOutput::Color0); |
| 74 | output->setTexture(offscreenTexture); |
| 75 | |
| 76 | qmlTextureRenderer->setOutput(output); |
| 77 | QQmlEngine engine; |
| 78 | QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/OffscreenGui.qml"))); |
| 79 | qmlTextureRenderer->setItem(static_cast<QQuickItem *>(component.create())); |
| 80 | |
| 81 | Qt3DCore::QEntity* planeEntity = new Qt3DCore::QEntity(sceneRoot); |
| 82 | Qt3DExtras::QPlaneMesh* planeMesh = new Qt3DExtras::QPlaneMesh(planeEntity); |
| 83 | planeMesh->setWidth(4); |
| 84 | planeMesh->setHeight(4); |
| 85 | planeEntity->addComponent(planeMesh); |
| 86 | |
| 87 | PlaneMaterial* material = new PlaneMaterial(offscreenTexture, planeEntity); |
| 88 | planeEntity->addComponent(material); |
| 89 | |
| 90 | Qt3DCore::QTransform* transform = new Qt3DCore::QTransform(planeEntity); |
| 91 | transform->setRotation(QQuaternion::fromAxisAndAngle(1,0,0,90)); |
| 92 | planeEntity->addComponent(transform); |
| 93 | |
| 94 | view.setRootEntity(sceneRoot); |
| 95 | view.show(); |
| 96 | |
| 97 | return app.exec(); |
| 98 | } |