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: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 "cameralens_p.h"
41#include <Qt3DRender/qcameralens.h>
42#include <Qt3DRender/qrenderaspect.h>
43#include <Qt3DRender/private/nodemanagers_p.h>
44#include <Qt3DRender/private/managers_p.h>
45#include <Qt3DRender/private/renderlogging_p.h>
46#include <Qt3DRender/private/abstractrenderer_p.h>
47#include <Qt3DRender/private/entity_p.h>
48#include <Qt3DRender/private/sphere_p.h>
49#include <Qt3DRender/private/computefilteredboundingvolumejob_p.h>
50#include <Qt3DRender/private/renderlogging_p.h>
51#include <Qt3DRender/private/qrenderaspect_p.h>
52#include <Qt3DCore/qentity.h>
53#include <Qt3DCore/qtransform.h>
54#include <Qt3DCore/private/qaspectmanager_p.h>
55
56QT_BEGIN_NAMESPACE
57
58using namespace Qt3DCore;
59
60namespace Qt3DRender {
61namespace Render {
62
63
64namespace {
65
66class GetBoundingVolumeWithoutCameraJob : public ComputeFilteredBoundingVolumeJob
67{
68public:
69 GetBoundingVolumeWithoutCameraJob(CameraLens *lens, QNodeId commandId)
70 : m_lens(lens), m_requestId(commandId)
71 {
72 }
73
74protected:
75 // called in main thread
76 void finished(Qt3DCore::QAspectManager *aspectManager, const Sphere &sphere) override
77 {
78 m_lens->processViewAllResult(aspectManager, sphere, commandId: m_requestId);
79 }
80
81private:
82 CameraLens *m_lens;
83 QNodeId m_requestId;
84};
85
86} // namespace
87
88CameraLens::CameraLens()
89 : BackendNode(QBackendNode::ReadWrite)
90 , m_renderAspect(nullptr)
91 , m_exposure(0.0f)
92{
93}
94
95CameraLens::~CameraLens()
96{
97 cleanup();
98}
99
100void CameraLens::cleanup()
101{
102 QBackendNode::setEnabled(false);
103}
104
105void CameraLens::setRenderAspect(QRenderAspect *renderAspect)
106{
107 m_renderAspect = renderAspect;
108}
109
110Matrix4x4 CameraLens::viewMatrix(const Matrix4x4 &worldTransform)
111{
112 const Vector4D position = worldTransform * Vector4D(0.0f, 0.0f, 0.0f, 1.0f);
113 // OpenGL convention is looking down -Z
114 const Vector4D viewDirection = worldTransform * Vector4D(0.0f, 0.0f, -1.0f, 0.0f);
115 const Vector4D upVector = worldTransform * Vector4D(0.0f, 1.0f, 0.0f, 0.0f);
116
117 QMatrix4x4 m;
118 m.lookAt(eye: convertToQVector3D(v: Vector3D(position)),
119 center: convertToQVector3D(v: Vector3D(position + viewDirection)),
120 up: convertToQVector3D(v: Vector3D(upVector)));
121
122 return Matrix4x4(m);
123}
124
125void CameraLens::syncFromFrontEnd(const Qt3DCore::QNode *frontEnd, bool firstTime)
126{
127 const QCameraLens *node = qobject_cast<const QCameraLens *>(object: frontEnd);
128 if (!node)
129 return;
130
131 BackendNode::syncFromFrontEnd(frontEnd, firstTime);
132
133 const Matrix4x4 projectionMatrix(node->projectionMatrix());
134 if (projectionMatrix != m_projection) {
135 m_projection = projectionMatrix;
136 markDirty(changes: AbstractRenderer::ParameterDirty);
137 }
138
139 if (!qFuzzyCompare(p1: node->exposure(), p2: m_exposure)) {
140 m_exposure = node->exposure();
141 markDirty(changes: AbstractRenderer::ParameterDirty);
142 }
143
144 const QCameraLensPrivate *d = static_cast<const QCameraLensPrivate *>(QNodePrivate::get(q: node));
145 if (d->m_pendingViewAllRequest != m_pendingViewAllRequest) {
146 m_pendingViewAllRequest = d->m_pendingViewAllRequest;
147
148 if (m_pendingViewAllRequest)
149 computeSceneBoundingVolume(entityId: m_pendingViewAllRequest.entityId, cameraId: m_pendingViewAllRequest.cameraId, requestId: m_pendingViewAllRequest.requestId);
150 }
151}
152
153void CameraLens::computeSceneBoundingVolume(QNodeId entityId,
154 QNodeId cameraId,
155 QNodeId requestId)
156{
157 if (!m_renderer || !m_renderAspect)
158 return;
159 NodeManagers *nodeManagers = m_renderer->nodeManagers();
160
161 Entity *root = m_renderer->sceneRoot();
162 if (!entityId.isNull())
163 root = nodeManagers->renderNodesManager()->lookupResource(id: entityId);
164 if (!root)
165 return;
166
167 Entity *camNode = nodeManagers->renderNodesManager()->lookupResource(id: cameraId);
168 ComputeFilteredBoundingVolumeJobPtr job(new GetBoundingVolumeWithoutCameraJob(this, requestId));
169 job->addDependency(dependency: QRenderAspectPrivate::get(q: m_renderer->aspect())->m_expandBoundingVolumeJob);
170 job->setRoot(root);
171 job->setManagers(nodeManagers);
172 job->ignoreSubTree(node: camNode);
173 m_renderAspect->scheduleSingleShotJob(job);
174}
175
176void CameraLens::processViewAllResult(QAspectManager *aspectManager, const Sphere &sphere, QNodeId commandId)
177{
178 if (!m_pendingViewAllRequest || m_pendingViewAllRequest.requestId != commandId)
179 return;
180 if (sphere.radius() > 0.f) {
181 QCameraLens *lens = qobject_cast<QCameraLens *>(object: aspectManager->lookupNode(id: peerId()));
182 if (lens) {
183 QCameraLensPrivate *dlens = static_cast<QCameraLensPrivate *>(QCameraLensPrivate::get(q: lens));
184 dlens->processViewAllResult(requestId: m_pendingViewAllRequest.requestId, center: { sphere.center().x(), sphere.center().y(), sphere.center().z() }, radius: sphere.radius());
185 }
186 }
187 m_pendingViewAllRequest = {};
188}
189
190void CameraLens::setProjection(const Matrix4x4 &projection)
191{
192 m_projection = projection;
193}
194
195void CameraLens::setExposure(float exposure)
196{
197 m_exposure = exposure;
198}
199
200bool CameraLens::viewMatrixForCamera(EntityManager* manager, Qt3DCore::QNodeId cameraId,
201 Matrix4x4 &viewMatrix, Matrix4x4 &projectionMatrix)
202{
203 Entity *camNode = manager->lookupResource(id: cameraId);
204 if (!camNode)
205 return false;
206 Render::CameraLens *lens = camNode->renderComponent<CameraLens>();
207 if (!lens || !lens->isEnabled())
208 return false;
209
210 viewMatrix = lens->viewMatrix(worldTransform: *camNode->worldTransform());
211 projectionMatrix = lens->projection();
212 return true;
213}
214
215CameraLensFunctor::CameraLensFunctor(AbstractRenderer *renderer, QRenderAspect *renderAspect)
216 : m_manager(renderer->nodeManagers()->manager<CameraLens, CameraManager>())
217 , m_renderer(renderer)
218 , m_renderAspect(renderAspect)
219{
220}
221
222QBackendNode *CameraLensFunctor::create(const QNodeCreatedChangeBasePtr &change) const
223{
224 CameraLens *backend = m_manager->getOrCreateResource(id: change->subjectId());
225 backend->setRenderer(m_renderer);
226 backend->setRenderAspect(m_renderAspect);
227 return backend;
228}
229
230QBackendNode *CameraLensFunctor::get(QNodeId id) const
231{
232 return m_manager->lookupResource(id);
233}
234
235void CameraLensFunctor::destroy(QNodeId id) const
236{
237 m_manager->releaseResource(id);
238}
239
240} // namespace Render
241} // namespace Qt3DRender
242
243QT_END_NAMESPACE
244

source code of qt3d/src/render/backend/cameralens.cpp