| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2014 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 "deferredrenderer.h" | 
| 52 | #include "gbuffer.h" | 
| 53 | #include <Qt3DRender/QParameter> | 
| 54 | #include <QWindow> | 
| 55 |  | 
| 56 | DeferredRenderer::DeferredRenderer(Qt3DCore::QNode *parent) | 
| 57 |     : Qt3DRender::QViewport(parent) | 
| 58 |     , m_surfaceSelector(new Qt3DRender::QRenderSurfaceSelector(this)) | 
| 59 |     , m_sceneFilter(new Qt3DRender::QLayerFilter(m_surfaceSelector)) | 
| 60 |     , m_screenQuadFilter(new Qt3DRender::QLayerFilter(m_surfaceSelector)) | 
| 61 |     , m_clearScreenQuad(new Qt3DRender::QClearBuffers(m_screenQuadFilter)) | 
| 62 |     , m_gBufferTargetSelector(new Qt3DRender::QRenderTargetSelector(m_sceneFilter)) | 
| 63 |     , m_clearGBuffer(new Qt3DRender::QClearBuffers(m_gBufferTargetSelector)) | 
| 64 |     , m_geometryPassFilter(new Qt3DRender::QRenderPassFilter(m_clearGBuffer)) | 
| 65 |     , m_finalPassFilter(new Qt3DRender::QRenderPassFilter(m_clearScreenQuad)) | 
| 66 |     , m_sceneCameraSelector(new Qt3DRender::QCameraSelector(m_geometryPassFilter)) | 
| 67 |     , m_winSize(new Qt3DRender::QParameter(QStringLiteral("winSize" ), QSizeF(1024.0f, 768.0f))) | 
| 68 |     , m_gBuffer(new GBuffer(this)) | 
| 69 |     , m_window(nullptr) | 
| 70 | { | 
| 71 |     m_clearGBuffer->setBuffers(Qt3DRender::QClearBuffers::ColorDepthBuffer); | 
| 72 |     m_clearScreenQuad->setBuffers(Qt3DRender::QClearBuffers::ColorDepthBuffer); | 
| 73 |     m_gBufferTargetSelector->setTarget(m_gBuffer); | 
| 74 |  | 
| 75 |     m_finalPassFilter->addParameter(parameter: new Qt3DRender::QParameter(QStringLiteral("position" ), m_gBuffer->positionTexture())); | 
| 76 |     m_finalPassFilter->addParameter(parameter: new Qt3DRender::QParameter(QStringLiteral("normal" ), m_gBuffer->normalTexture())); | 
| 77 |     m_finalPassFilter->addParameter(parameter: new Qt3DRender::QParameter(QStringLiteral("color" ), m_gBuffer->colorTexture())); | 
| 78 |     m_finalPassFilter->addParameter(parameter: m_winSize); | 
| 79 | } | 
| 80 |  | 
| 81 | void DeferredRenderer::setSceneCamera(Qt3DCore::QEntity *camera) | 
| 82 | { | 
| 83 |     m_sceneCameraSelector->setCamera(camera); | 
| 84 | } | 
| 85 |  | 
| 86 | void DeferredRenderer::setGeometryPassCriteria(QList<Qt3DRender::QFilterKey *> criteria) | 
| 87 | { | 
| 88 |     for (Qt3DRender::QFilterKey *c : qAsConst(t&: criteria)) | 
| 89 |         m_geometryPassFilter->addMatch(filterKey: c); | 
| 90 | } | 
| 91 |  | 
| 92 | void DeferredRenderer::setFinalPassCriteria(QList<Qt3DRender::QFilterKey *> criteria) | 
| 93 | { | 
| 94 |     for (Qt3DRender::QFilterKey *c : qAsConst(t&: criteria)) | 
| 95 |         c->setParent(m_finalPassFilter); | 
| 96 | } | 
| 97 |  | 
| 98 | void DeferredRenderer::setGBufferLayer(Qt3DRender::QLayer *layer) | 
| 99 | { | 
| 100 |     m_sceneFilter->addLayer(layer); | 
| 101 | } | 
| 102 |  | 
| 103 | void DeferredRenderer::setScreenQuadLayer(Qt3DRender::QLayer *layer) | 
| 104 | { | 
| 105 |     m_screenQuadFilter->addLayer(layer); | 
| 106 | } | 
| 107 |  | 
| 108 | void DeferredRenderer::setSurface(QWindow *surface) | 
| 109 | { | 
| 110 |     if (surface != m_window) { | 
| 111 |  | 
| 112 |         // Disconnect old window's signals | 
| 113 |         if (m_window != nullptr) { | 
| 114 |             QObject::disconnect(m_widthChangedConnection); | 
| 115 |             QObject::disconnect(m_heightChangedConnection); | 
| 116 |         } | 
| 117 |  | 
| 118 |         m_window = surface; | 
| 119 |         m_surfaceSelector->setSurface(surface); | 
| 120 |  | 
| 121 |         if (m_window != nullptr) { | 
| 122 |             // Store connections | 
| 123 |             m_widthChangedConnection = QObject::connect(sender: surface, signal: &QWindow::widthChanged, | 
| 124 |                                                         slot: [this] (int width) { | 
| 125 |                 m_winSize->setValue(QSizeF(float(width), m_winSize->value().toSizeF().height())); | 
| 126 |             }); | 
| 127 |             m_heightChangedConnection = QObject::connect(sender: surface, signal: &QWindow::heightChanged, | 
| 128 |                                                          slot: [this] (int height) { | 
| 129 |                 m_winSize->setValue(QSizeF(m_winSize->value().toSizeF().width(), float(height))); | 
| 130 |             }); | 
| 131 |         } | 
| 132 |     } | 
| 133 | } | 
| 134 |  |