1/****************************************************************************
2**
3** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB).
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt3D module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
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 https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "qt3dwindow.h"
52#include "qt3dwindow_p.h"
53
54#include <Qt3DCore/qaspectengine.h>
55#include <Qt3DCore/qentity.h>
56#include <Qt3DExtras/qforwardrenderer.h>
57#include <Qt3DRender/qrendersettings.h>
58#include <Qt3DRender/qrenderaspect.h>
59#include <Qt3DInput/qinputaspect.h>
60#include <Qt3DInput/qinputsettings.h>
61#include <Qt3DLogic/qlogicaspect.h>
62#include <Qt3DRender/qcamera.h>
63#include <QtGui/qopenglcontext.h>
64
65#include <QEvent>
66
67static void initResources()
68{
69#ifdef QT_STATIC
70 Q_INIT_RESOURCE(extras);
71#endif
72}
73
74QT_BEGIN_NAMESPACE
75
76namespace Qt3DExtras {
77
78Qt3DWindowPrivate::Qt3DWindowPrivate()
79 : m_aspectEngine(new Qt3DCore::QAspectEngine)
80 , m_renderAspect(new Qt3DRender::QRenderAspect)
81 , m_inputAspect(new Qt3DInput::QInputAspect)
82 , m_logicAspect(new Qt3DLogic::QLogicAspect)
83 , m_renderSettings(new Qt3DRender::QRenderSettings)
84 , m_forwardRenderer(new Qt3DExtras::QForwardRenderer)
85 , m_defaultCamera(new Qt3DRender::QCamera)
86 , m_inputSettings(new Qt3DInput::QInputSettings)
87 , m_root(new Qt3DCore::QEntity)
88 , m_userRoot(nullptr)
89 , m_initialized(false)
90{
91}
92
93Qt3DWindow::Qt3DWindow(QScreen *screen)
94 : QWindow(*new Qt3DWindowPrivate(), nullptr)
95{
96 Q_D(Qt3DWindow);
97
98 initResources();
99
100 if (!d->parentWindow)
101 d->connectToScreen(screen ? screen : d->topLevelScreen.data());
102
103 setSurfaceType(QSurface::OpenGLSurface);
104
105 resize(1024, 768);
106
107 QSurfaceFormat format = QSurfaceFormat::defaultFormat();
108#ifdef QT_OPENGL_ES_2
109 format.setRenderableType(QSurfaceFormat::OpenGLES);
110#else
111 if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL) {
112 format.setVersion(4, 3);
113 format.setProfile(QSurfaceFormat::CoreProfile);
114 }
115#endif
116 format.setDepthBufferSize(24);
117 format.setSamples(4);
118 format.setStencilBufferSize(8);
119 setFormat(format);
120 QSurfaceFormat::setDefaultFormat(format);
121
122 d->m_aspectEngine->registerAspect(d->m_renderAspect);
123 d->m_aspectEngine->registerAspect(d->m_inputAspect);
124 d->m_aspectEngine->registerAspect(d->m_logicAspect);
125
126 d->m_defaultCamera->setParent(d->m_root);
127 d->m_forwardRenderer->setCamera(d->m_defaultCamera);
128 d->m_forwardRenderer->setSurface(this);
129 d->m_renderSettings->setActiveFrameGraph(d->m_forwardRenderer);
130 d->m_inputSettings->setEventSource(this);
131}
132
133Qt3DWindow::~Qt3DWindow()
134{
135 Q_D(Qt3DWindow);
136 delete d->m_aspectEngine;
137}
138
139/*!
140 Registers the specified \a aspect.
141*/
142void Qt3DWindow::registerAspect(Qt3DCore::QAbstractAspect *aspect)
143{
144 Q_ASSERT(!isVisible());
145 Q_D(Qt3DWindow);
146 d->m_aspectEngine->registerAspect(aspect);
147}
148
149/*!
150 Registers the specified aspect \a name.
151*/
152void Qt3DWindow::registerAspect(const QString &name)
153{
154 Q_ASSERT(!isVisible());
155 Q_D(Qt3DWindow);
156 d->m_aspectEngine->registerAspect(name);
157}
158
159/*!
160 Sets the specified \a root entity of the scene.
161*/
162void Qt3DWindow::setRootEntity(Qt3DCore::QEntity *root)
163{
164 Q_D(Qt3DWindow);
165 if (d->m_userRoot != root) {
166 if (d->m_userRoot != nullptr)
167 d->m_userRoot->setParent(static_cast<Qt3DCore::QNode*>(nullptr));
168 if (root != nullptr)
169 root->setParent(d->m_root);
170 d->m_userRoot = root;
171 }
172}
173
174/*!
175 Activates the specified \a activeFrameGraph.
176*/
177void Qt3DWindow::setActiveFrameGraph(Qt3DRender::QFrameGraphNode *activeFrameGraph)
178{
179 Q_D(Qt3DWindow);
180 d->m_renderSettings->setActiveFrameGraph(activeFrameGraph);
181}
182
183/*!
184 Returns the node of the active frame graph.
185*/
186Qt3DRender::QFrameGraphNode *Qt3DWindow::activeFrameGraph() const
187{
188 Q_D(const Qt3DWindow);
189 return d->m_renderSettings->activeFrameGraph();
190}
191
192/*!
193 Returns the node of the default framegraph
194*/
195Qt3DExtras::QForwardRenderer *Qt3DWindow::defaultFrameGraph() const
196{
197 Q_D(const Qt3DWindow);
198 return d->m_forwardRenderer;
199}
200
201Qt3DRender::QCamera *Qt3DWindow::camera() const
202{
203 Q_D(const Qt3DWindow);
204 return d->m_defaultCamera;
205}
206
207/*!
208 Returns the render settings of the 3D Window.
209*/
210Qt3DRender::QRenderSettings *Qt3DWindow::renderSettings() const
211{
212 Q_D(const Qt3DWindow);
213 return d->m_renderSettings;
214}
215
216/*!
217 Manages the display events specified in \a e.
218*/
219void Qt3DWindow::showEvent(QShowEvent *e)
220{
221 Q_D(Qt3DWindow);
222 if (!d->m_initialized) {
223 d->m_root->addComponent(d->m_renderSettings);
224 d->m_root->addComponent(d->m_inputSettings);
225 d->m_aspectEngine->setRootEntity(Qt3DCore::QEntityPtr(d->m_root));
226
227 d->m_initialized = true;
228 }
229
230 QWindow::showEvent(e);
231}
232
233/*!
234 Resets the aspect ratio of the 3D window.
235*/
236void Qt3DWindow::resizeEvent(QResizeEvent *)
237{
238 Q_D(Qt3DWindow);
239 d->m_defaultCamera->setAspectRatio(float(width()) / float(height()));
240}
241
242/*!
243 \reimp
244
245 Requests renderer to redraw if we are using OnDemand render policy.
246*/
247bool Qt3DWindow::event(QEvent *e)
248{
249 Q_D(Qt3DWindow);
250 const bool needsRedraw = (e->type() == QEvent::Expose || e->type() == QEvent::UpdateRequest);
251 if (needsRedraw && d->m_renderSettings->renderPolicy() == Qt3DRender::QRenderSettings::OnDemand)
252 d->m_renderSettings->sendCommand(QLatin1String("InvalidateFrame"));
253 return QWindow::event(e);
254}
255
256} // Qt3DExtras
257
258QT_END_NAMESPACE
259