1/****************************************************************************
2**
3** Copyright (C) 2015 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:LGPL$
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** 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.LGPL3 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-3.0.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 (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qforwardrenderer.h"
41#include "qforwardrenderer_p.h"
42
43#include <Qt3DCore/qentity.h>
44#include <Qt3DRender/qviewport.h>
45#include <Qt3DRender/qcameraselector.h>
46#include <Qt3DRender/qclearbuffers.h>
47#include <Qt3DRender/qfilterkey.h>
48#include <Qt3DRender/qfrustumculling.h>
49#include <Qt3DRender/qrendersurfaceselector.h>
50#include <Qt3DRender/qdebugoverlay.h>
51
52static void initResources()
53{
54#ifdef QT_STATIC
55 Q_INIT_RESOURCE(extras);
56#endif
57}
58
59QT_BEGIN_NAMESPACE
60
61using namespace Qt3DRender;
62
63namespace Qt3DExtras {
64
65QForwardRendererPrivate::QForwardRendererPrivate()
66 : QTechniqueFilterPrivate()
67 , m_surfaceSelector(new QRenderSurfaceSelector)
68 , m_viewport(new QViewport())
69 , m_cameraSelector(new QCameraSelector())
70 , m_clearBuffer(new QClearBuffers())
71 , m_frustumCulling(new QFrustumCulling())
72 , m_debugOverlay(new QDebugOverlay())
73{
74}
75
76void QForwardRendererPrivate::init()
77{
78 Q_Q(QForwardRenderer);
79
80 initResources();
81
82 m_debugOverlay->setParent(m_frustumCulling);
83 m_debugOverlay->setEnabled(false);
84 m_frustumCulling->setParent(m_clearBuffer);
85 m_clearBuffer->setParent(m_cameraSelector);
86 m_cameraSelector->setParent(m_viewport);
87 m_viewport->setParent(m_surfaceSelector);
88 m_surfaceSelector->setParent(q);
89
90 m_viewport->setNormalizedRect(QRectF(0.0, 0.0, 1.0, 1.0));
91 m_clearBuffer->setClearColor(Qt::white);
92 m_clearBuffer->setBuffers(QClearBuffers::ColorDepthBuffer);
93
94 QFilterKey *forwardRenderingStyle = new QFilterKey(q);
95 forwardRenderingStyle->setName(QStringLiteral("renderingStyle"));
96 forwardRenderingStyle->setValue(QStringLiteral("forward"));
97 q->addMatch(filterKey: forwardRenderingStyle);
98}
99
100/*!
101 \class Qt3DExtras::QForwardRenderer
102 \brief The QForwardRenderer provides a default \l{Qt 3D Render Framegraph}{FrameGraph}
103 implementation of a forward renderer.
104 \inmodule Qt3DExtras
105 \since 5.7
106 \inherits Qt3DRender::QTechniqueFilter
107
108 Forward rendering is what OpenGL traditionally uses. It renders directly to the backbuffer
109 one object at a time shading each one as it goes.
110
111 QForwardRenderer is a single leaf \l{Qt 3D Render Framegraph}{FrameGraph} tree which contains
112 a Qt3DRender::QViewport, a Qt3DRender::QCameraSelector, and a Qt3DRender::QClearBuffers.
113 The QForwardRenderer has a default requirement filter key whose name is "renderingStyle" and
114 value "forward".
115 If you need to filter out your techniques, you should do so based on that filter key.
116
117 By default the viewport occupies the whole screen and the clear color is white.
118 Frustum culling is also enabled.
119*/
120/*!
121 \qmltype ForwardRenderer
122 \brief The ForwardRenderer provides a default \l{Qt 3D Render Framegraph}{FrameGraph}
123 implementation of a forward renderer.
124 \since 5.7
125 \inqmlmodule Qt3D.Extras
126 \instantiates Qt3DExtras::QForwardRenderer
127
128 Forward rendering is what OpenGL traditionally uses. It renders directly to the backbuffer
129 one object at a time shading each one as it goes.
130
131 ForwardRenderer is a single leaf \l{Qt 3D Render Framegraph}{FrameGraph} tree which contains
132 a Viewport, a CameraSelector, and a ClearBuffers.
133 The ForwardRenderer has a default requirement filter key whose name is "renderingStyle" and
134 value "forward".
135 If you need to filter out your techniques, you should do so based on that filter key.
136
137 By default the viewport occupies the whole screen and the clear color is white.
138 Frustum culling is also enabled.
139 */
140
141QForwardRenderer::QForwardRenderer(QNode *parent)
142 : QTechniqueFilter(*new QForwardRendererPrivate, parent)
143{
144 Q_D(QForwardRenderer);
145 QObject::connect(sender: d->m_clearBuffer, signal: &QClearBuffers::clearColorChanged, receiver: this, slot: &QForwardRenderer::clearColorChanged);
146 QObject::connect(sender: d->m_clearBuffer, signal: &QClearBuffers::buffersChanged, receiver: this, slot: &QForwardRenderer::buffersToClearChanged);
147 QObject::connect(sender: d->m_viewport, signal: &QViewport::normalizedRectChanged, receiver: this, slot: &QForwardRenderer::viewportRectChanged);
148 QObject::connect(sender: d->m_cameraSelector, signal: &QCameraSelector::cameraChanged, receiver: this, slot: &QForwardRenderer::cameraChanged);
149 QObject::connect(sender: d->m_surfaceSelector, signal: &QRenderSurfaceSelector::surfaceChanged, receiver: this, slot: &QForwardRenderer::surfaceChanged);
150 QObject::connect(sender: d->m_surfaceSelector, signal: &QRenderSurfaceSelector::externalRenderTargetSizeChanged, receiver: this, slot: &QForwardRenderer::externalRenderTargetSizeChanged);
151 QObject::connect(sender: d->m_frustumCulling, signal: &QFrustumCulling::enabledChanged, receiver: this, slot: &QForwardRenderer::frustumCullingEnabledChanged);
152 QObject::connect(sender: d->m_viewport, signal: &QViewport::gammaChanged, receiver: this, slot: &QForwardRenderer::gammaChanged);
153 QObject::connect(sender: d->m_debugOverlay, signal: &QDebugOverlay::enabledChanged, receiver: this, slot: &QForwardRenderer::showDebugOverlayChanged);
154 d->init();
155}
156
157QForwardRenderer::~QForwardRenderer()
158{
159}
160
161void QForwardRenderer::setViewportRect(const QRectF &viewportRect)
162{
163 Q_D(QForwardRenderer);
164 d->m_viewport->setNormalizedRect(viewportRect);
165}
166
167void QForwardRenderer::setClearColor(const QColor &clearColor)
168{
169 Q_D(QForwardRenderer);
170 d->m_clearBuffer->setClearColor(clearColor);
171}
172
173void QForwardRenderer::setBuffersToClear(QClearBuffers::BufferType buffers)
174{
175 Q_D(QForwardRenderer);
176 d->m_clearBuffer->setBuffers(buffers);
177}
178
179void QForwardRenderer::setCamera(Qt3DCore::QEntity *camera)
180{
181 Q_D(QForwardRenderer);
182 d->m_cameraSelector->setCamera(camera);
183}
184
185void QForwardRenderer::setSurface(QObject *surface)
186{
187 Q_D(QForwardRenderer);
188 d->m_surfaceSelector->setSurface(surface);
189}
190
191void QForwardRenderer::setExternalRenderTargetSize(const QSize &size)
192{
193 Q_D(QForwardRenderer);
194 d->m_surfaceSelector->setExternalRenderTargetSize(size);
195}
196
197void QForwardRenderer::setFrustumCullingEnabled(bool enabled)
198{
199 Q_D(QForwardRenderer);
200 d->m_frustumCulling->setEnabled(enabled);
201}
202
203void QForwardRenderer::setGamma(float gamma)
204{
205 Q_D(QForwardRenderer);
206 d->m_viewport->setGamma(gamma);
207}
208
209void QForwardRenderer::setShowDebugOverlay(bool showDebugOverlay)
210{
211 Q_D(QForwardRenderer);
212 d->m_debugOverlay->setEnabled(showDebugOverlay);
213}
214
215/*!
216 \qmlproperty rect ForwardRenderer::viewportRect
217
218 Holds the current normalized viewport rectangle.
219*/
220/*!
221 \property QForwardRenderer::viewportRect
222
223 Holds the current normalized viewport rectangle.
224*/
225QRectF QForwardRenderer::viewportRect() const
226{
227 Q_D(const QForwardRenderer);
228 return d->m_viewport->normalizedRect();
229}
230
231/*!
232 \qmlproperty color ForwardRenderer::clearColor
233
234 Holds the current clear color of the scene. The frame buffer is initialized to the clear color
235 before rendering.
236*/
237/*!
238 \property QForwardRenderer::clearColor
239
240 Holds the current clear color of the scene. The frame buffer is initialized to the clear color
241 before rendering.
242*/
243QColor QForwardRenderer::clearColor() const
244{
245 Q_D(const QForwardRenderer);
246 return d->m_clearBuffer->clearColor();
247}
248
249/*!
250 \qmlproperty color ForwardRenderer::buffersToClear
251
252 Holds the current buffers to be cleared. Default value is ColorDepthBuffer
253 \since 5.14
254*/
255/*!
256 \property QForwardRenderer::buffersToClear
257
258 Holds the current buffers to be cleared. Default value is ColorDepthBuffer
259 \since 5.14
260*/
261QClearBuffers::BufferType QForwardRenderer::buffersToClear() const
262{
263 Q_D(const QForwardRenderer);
264 return d->m_clearBuffer->buffers();
265}
266
267/*!
268 \qmlproperty Entity ForwardRenderer::camera
269
270 Holds the current camera entity used to render the scene.
271
272 \note A camera is an Entity that has a CameraLens as one of its components.
273*/
274/*!
275 \property QForwardRenderer::camera
276
277 Holds the current camera entity used to render the scene.
278
279 \note A camera is a QEntity that has a QCameraLens as one of its components.
280*/
281Qt3DCore::QEntity *QForwardRenderer::camera() const
282{
283 Q_D(const QForwardRenderer);
284 return d->m_cameraSelector->camera();
285}
286
287/*!
288 \qmlproperty Object ForwardRenderer::window
289
290 Holds the current render surface.
291
292 \deprecated
293*/
294/*!
295 \property QForwardRenderer::window
296
297 Holds the current render surface.
298
299 \deprecated
300*/
301
302/*!
303 \qmlproperty Object ForwardRenderer::surface
304
305 Holds the current render surface.
306*/
307/*!
308 \property QForwardRenderer::surface
309
310 Holds the current render surface.
311*/
312QObject *QForwardRenderer::surface() const
313{
314 Q_D(const QForwardRenderer);
315 return d->m_surfaceSelector->surface();
316}
317
318/*!
319 \qmlproperty QSize ForwardRenderer::externalRenderTargetSize
320
321 Contains the size of the external render target. External render
322 targets are relevant when rendering does not target a window
323 surface (as set in \l {surface}).
324*/
325/*!
326 \property QForwardRenderer::externalRenderTargetSize
327
328 Contains the size of the external render target. External render
329 targets are relevant when rendering does not target a window
330 surface (as set in \l {surface}).
331*/
332QSize QForwardRenderer::externalRenderTargetSize() const
333{
334 Q_D(const QForwardRenderer);
335 return d->m_surfaceSelector->externalRenderTargetSize();
336}
337
338/*!
339 \qmlproperty bool ForwardRenderer::frustumCulling
340
341 Indicates if the renderer applies frustum culling to the scene.
342*/
343/*!
344 \property QForwardRenderer::frustumCulling
345
346 Indicates if the renderer applies frustum culling to the scene.
347*/
348bool QForwardRenderer::isFrustumCullingEnabled() const
349{
350 Q_D(const QForwardRenderer);
351 return d->m_frustumCulling->isEnabled();
352}
353
354/*!
355 \qmlproperty real ForwardRenderer::gamma
356
357 Holds the gamma value the renderer applies to the scene.
358*/
359/*!
360 \property QForwardRenderer::gamma
361
362 Holds the gamma value the renderer applies to the scene.
363*/
364float QForwardRenderer::gamma() const
365{
366 Q_D(const QForwardRenderer);
367 return d->m_viewport->gamma();
368}
369
370/*!
371 \qmlproperty bool ForwardRenderer::showDebugOverlay
372
373 If true, a debug overlay will be rendered over the scene. It will show
374 detailed information about the runtime rendering state, let the user
375 turn logging on and off, etc.
376
377 \since 5.15
378*/
379/*!
380 \property QForwardRenderer::showDebugOverlay
381
382 If true, a debug overlay will be rendered over the scene. It will show
383 detailed information about the runtime rendering state, let the user
384 turn logging on and off, etc.
385
386 \since 5.15
387*/
388bool QForwardRenderer::showDebugOverlay() const
389{
390 Q_D(const QForwardRenderer);
391 return d->m_debugOverlay->isEnabled();
392}
393
394} // namespace Qt3DExtras
395
396QT_END_NAMESPACE
397

source code of qt3d/src/extras/defaults/qforwardrenderer.cpp