1// Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "cameralens_p.h"
5#include <Qt3DRender/qcameralens.h>
6#include <Qt3DRender/qrenderaspect.h>
7#include <Qt3DRender/private/nodemanagers_p.h>
8#include <Qt3DRender/private/managers_p.h>
9#include <Qt3DRender/private/renderlogging_p.h>
10#include <Qt3DRender/private/abstractrenderer_p.h>
11#include <Qt3DRender/private/entity_p.h>
12#include <Qt3DRender/private/sphere_p.h>
13#include <Qt3DRender/private/computefilteredboundingvolumejob_p.h>
14#include <Qt3DRender/private/renderlogging_p.h>
15#include <Qt3DRender/private/qrenderaspect_p.h>
16#include <Qt3DCore/qentity.h>
17#include <Qt3DCore/qtransform.h>
18#include <Qt3DCore/private/qaspectmanager_p.h>
19
20QT_BEGIN_NAMESPACE
21
22using namespace Qt3DCore;
23
24namespace Qt3DRender {
25namespace Render {
26
27
28namespace {
29
30class GetBoundingVolumeWithoutCameraJob : public ComputeFilteredBoundingVolumeJob
31{
32public:
33 GetBoundingVolumeWithoutCameraJob(CameraLens *lens, QNodeId commandId)
34 : m_lens(lens), m_requestId(commandId)
35 {
36 }
37
38protected:
39 // called in main thread
40 void finished(Qt3DCore::QAspectManager *aspectManager, const Sphere &sphere) override
41 {
42 m_lens->processViewAllResult(aspectManager, sphere, commandId: m_requestId);
43 }
44
45private:
46 CameraLens *m_lens;
47 QNodeId m_requestId;
48};
49
50} // namespace
51
52CameraLens::CameraLens()
53 : BackendNode(QBackendNode::ReadWrite)
54 , m_renderAspect(nullptr)
55 , m_exposure(0.0f)
56{
57}
58
59CameraLens::~CameraLens()
60{
61 cleanup();
62}
63
64void CameraLens::cleanup()
65{
66 QBackendNode::setEnabled(false);
67}
68
69void CameraLens::setRenderAspect(QRenderAspect *renderAspect)
70{
71 m_renderAspect = renderAspect;
72}
73
74Matrix4x4 CameraLens::viewMatrix(const Matrix4x4 &worldTransform)
75{
76 const Vector4D position = worldTransform * Vector4D(0.0f, 0.0f, 0.0f, 1.0f);
77 // OpenGL convention is looking down -Z
78 const Vector4D viewDirection = worldTransform * Vector4D(0.0f, 0.0f, -1.0f, 0.0f);
79 const Vector4D upVector = worldTransform * Vector4D(0.0f, 1.0f, 0.0f, 0.0f);
80
81 QMatrix4x4 m;
82 m.lookAt(eye: convertToQVector3D(v: Vector3D(position)),
83 center: convertToQVector3D(v: Vector3D(position + viewDirection)),
84 up: convertToQVector3D(v: Vector3D(upVector)));
85
86 return Matrix4x4(m);
87}
88
89void CameraLens::syncFromFrontEnd(const Qt3DCore::QNode *frontEnd, bool firstTime)
90{
91 const QCameraLens *node = qobject_cast<const QCameraLens *>(object: frontEnd);
92 if (!node)
93 return;
94
95 BackendNode::syncFromFrontEnd(frontEnd, firstTime);
96
97 const Matrix4x4 projectionMatrix(node->projectionMatrix());
98 if (projectionMatrix != m_projection) {
99 m_projection = projectionMatrix;
100 markDirty(changes: AbstractRenderer::ParameterDirty);
101 }
102
103 if (!qFuzzyCompare(p1: node->exposure(), p2: m_exposure)) {
104 m_exposure = node->exposure();
105 markDirty(changes: AbstractRenderer::ParameterDirty);
106 }
107
108 const QCameraLensPrivate *d = static_cast<const QCameraLensPrivate *>(QNodePrivate::get(q: node));
109 if (d->m_pendingViewAllRequest != m_pendingViewAllRequest) {
110 m_pendingViewAllRequest = d->m_pendingViewAllRequest;
111
112 if (m_pendingViewAllRequest)
113 computeSceneBoundingVolume(entityId: m_pendingViewAllRequest.entityId, cameraId: m_pendingViewAllRequest.cameraId, requestId: m_pendingViewAllRequest.requestId);
114 }
115}
116
117void CameraLens::computeSceneBoundingVolume(QNodeId entityId,
118 QNodeId cameraId,
119 QNodeId requestId)
120{
121 if (!m_renderer || !m_renderAspect)
122 return;
123 NodeManagers *nodeManagers = m_renderer->nodeManagers();
124
125 Entity *root = m_renderer->sceneRoot();
126 if (!entityId.isNull())
127 root = nodeManagers->renderNodesManager()->lookupResource(id: entityId);
128 if (!root)
129 return;
130
131 Entity *camNode = nodeManagers->renderNodesManager()->lookupResource(id: cameraId);
132 ComputeFilteredBoundingVolumeJobPtr job(new GetBoundingVolumeWithoutCameraJob(this, requestId));
133 job->addDependency(dependency: QRenderAspectPrivate::get(q: m_renderer->aspect())->m_expandBoundingVolumeJob);
134 job->setRoot(root);
135 job->setManagers(nodeManagers);
136 job->ignoreSubTree(node: camNode);
137 m_renderAspect->scheduleSingleShotJob(job);
138}
139
140void CameraLens::processViewAllResult(QAspectManager *aspectManager, const Sphere &sphere, QNodeId commandId)
141{
142 if (!m_pendingViewAllRequest || m_pendingViewAllRequest.requestId != commandId)
143 return;
144 if (sphere.radius() > 0.f) {
145 QCameraLens *lens = qobject_cast<QCameraLens *>(object: aspectManager->lookupNode(id: peerId()));
146 if (lens) {
147 QCameraLensPrivate *dlens = static_cast<QCameraLensPrivate *>(QCameraLensPrivate::get(q: lens));
148 dlens->processViewAllResult(requestId: m_pendingViewAllRequest.requestId, center: { sphere.center().x(), sphere.center().y(), sphere.center().z() }, radius: sphere.radius());
149 }
150 }
151 m_pendingViewAllRequest = {};
152}
153
154void CameraLens::setProjection(const Matrix4x4 &projection)
155{
156 m_projection = projection;
157}
158
159void CameraLens::setExposure(float exposure)
160{
161 m_exposure = exposure;
162}
163
164bool CameraLens::viewMatrixForCamera(EntityManager* manager, Qt3DCore::QNodeId cameraId,
165 Matrix4x4 &viewMatrix, Matrix4x4 &projectionMatrix)
166{
167 Entity *camNode = manager->lookupResource(id: cameraId);
168 if (!camNode)
169 return false;
170 Render::CameraLens *lens = camNode->renderComponent<CameraLens>();
171 if (!lens || !lens->isEnabled())
172 return false;
173
174 viewMatrix = lens->viewMatrix(worldTransform: *camNode->worldTransform());
175 projectionMatrix = lens->projection();
176 return true;
177}
178
179CameraLensFunctor::CameraLensFunctor(AbstractRenderer *renderer, QRenderAspect *renderAspect)
180 : m_manager(renderer->nodeManagers()->manager<CameraLens, CameraManager>())
181 , m_renderer(renderer)
182 , m_renderAspect(renderAspect)
183{
184}
185
186QBackendNode *CameraLensFunctor::create(const QNodeId id) const
187{
188 CameraLens *backend = m_manager->getOrCreateResource(id);
189 backend->setRenderer(m_renderer);
190 backend->setRenderAspect(m_renderAspect);
191 return backend;
192}
193
194QBackendNode *CameraLensFunctor::get(QNodeId id) const
195{
196 return m_manager->lookupResource(id);
197}
198
199void CameraLensFunctor::destroy(QNodeId id) const
200{
201 m_manager->releaseResource(id);
202}
203
204} // namespace Render
205} // namespace Qt3DRender
206
207QT_END_NAMESPACE
208

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