1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the Qt3D module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL3$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37#include <QGuiApplication>
38#include <QAnimationDriver>
39#include <QPropertyAnimation>
40#include <QQmlComponent>
41#include <QQmlEngine>
42
43#include <Qt3DCore/QEntity>
44#include <Qt3DCore/QAspectEngine>
45#include <Qt3DCore/QTransform>
46#include <Qt3DRender/QCamera>
47
48#include <Qt3DInput/QInputAspect>
49
50#include <Qt3DRender/QRenderAspect>
51#include <Qt3DRender/QEffect>
52#include <Qt3DRender/QMaterial>
53#include <Qt3DExtras/QForwardRenderer>
54#include <Qt3DQuickScene2D/QScene2D>
55#include <Qt3DExtras/QPlaneMesh>
56#include <Qt3DRender/QTextureWrapMode>
57#include <Qt3DRender/QClearBuffers>
58#include <Qt3DRender/QTexture>
59
60#include "qt3dwindow.h"
61#include "qfirstpersoncameracontroller.h"
62#include "planematerial.h"
63
64int main(int argc, char *argv[])
65{
66 QGuiApplication app(argc, argv);
67
68 Qt3DExtras::Qt3DWindow view;
69
70 // Scene Root
71 Qt3DCore::QEntity *sceneRoot = new Qt3DCore::QEntity();
72
73 // Scene Camera
74 Qt3DRender::QCamera *basicCamera = view.camera();
75 basicCamera->setProjectionType(Qt3DRender::QCameraLens::PerspectiveProjection);
76 basicCamera->setAspectRatio(view.width() / view.height());
77 basicCamera->setUpVector(QVector3D(0.0f, 1.0f, 0.0f));
78 basicCamera->setPosition(QVector3D(0.0f, 0.0f, 6.0f));
79 basicCamera->setViewCenter(QVector3D(0.0f, 0.0f, 0.0f));
80 basicCamera->setNearPlane(0.1f);
81 basicCamera->setFarPlane(1000.0f);
82 basicCamera->setFieldOfView(45.0f);
83
84 // For camera controls
85 Qt3DExtras::QFirstPersonCameraController *camController = new Qt3DExtras::QFirstPersonCameraController(sceneRoot);
86 camController->setCamera(basicCamera);
87
88 Qt3DRender::QFrameGraphNode* frameGraphNode = view.activeFrameGraph();
89 while (frameGraphNode->childNodes().size() > 0)
90 frameGraphNode = (Qt3DRender::QFrameGraphNode*)frameGraphNode->childNodes().at(i: 0);
91 view.defaultFrameGraph()->setClearColor(QColor::fromRgbF(r: 1.0f, g: 1.0f, b: 1.0f));
92 Qt3DRender::Quick::QScene2D *qmlTextureRenderer = new Qt3DRender::Quick::QScene2D(frameGraphNode);
93
94 Qt3DRender::QTexture2D* offscreenTexture = new Qt3DRender::QTexture2D(qmlTextureRenderer);
95 offscreenTexture->setSize(width: 1024, height: 1024);
96 offscreenTexture->setFormat(Qt3DRender::QAbstractTexture::RGBA8_UNorm);
97 offscreenTexture->setGenerateMipMaps(true);
98 offscreenTexture->setMagnificationFilter(Qt3DRender::QAbstractTexture::Linear);
99 offscreenTexture->setMinificationFilter(Qt3DRender::QAbstractTexture::Linear);
100 offscreenTexture->setWrapMode(Qt3DRender::QTextureWrapMode(Qt3DRender::QTextureWrapMode::ClampToEdge, offscreenTexture));
101
102 Qt3DRender::QRenderTargetOutput *output = new Qt3DRender::QRenderTargetOutput(qmlTextureRenderer);
103 output->setAttachmentPoint(Qt3DRender::QRenderTargetOutput::Color0);
104 output->setTexture(offscreenTexture);
105
106 qmlTextureRenderer->setOutput(output);
107 QQmlEngine engine;
108 QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/OffscreenGui.qml")));
109 qmlTextureRenderer->setItem(static_cast<QQuickItem *>(component.create()));
110
111 Qt3DCore::QEntity* planeEntity = new Qt3DCore::QEntity(sceneRoot);
112 Qt3DExtras::QPlaneMesh* planeMesh = new Qt3DExtras::QPlaneMesh(planeEntity);
113 planeMesh->setWidth(4);
114 planeMesh->setHeight(4);
115 planeEntity->addComponent(comp: planeMesh);
116
117 PlaneMaterial* material = new PlaneMaterial(offscreenTexture, planeEntity);
118 planeEntity->addComponent(comp: material);
119
120 Qt3DCore::QTransform* transform = new Qt3DCore::QTransform(planeEntity);
121 transform->setRotation(QQuaternion::fromAxisAndAngle(x: 1,y: 0,z: 0,angle: 90));
122 planeEntity->addComponent(comp: transform);
123
124 view.setRootEntity(sceneRoot);
125 view.show();
126
127 return app.exec();
128}
129

source code of qt3d/tests/manual/render-qml-to-texture/main.cpp