27QSSGRenderCamera::QSSGRenderCamera(QSSGRenderGraphObject::Type type)
28 : QSSGRenderNode(type)
31 , fov(qDegreesToRadians(60.0f))
32 , fovHorizontal(
false)
33 , enableFrustumClipping(
true)
35 Q_ASSERT(QSSGRenderCamera::isCamera(type));
36 markDirty(DirtyFlag::CameraDirty);
46bool QSSGRenderCamera::calculateProjection(
const QRectF &inViewport)
50 const bool argumentsChanged = inViewport != previousInViewport;
51 if (!argumentsChanged && !isDirty(DirtyFlag::CameraDirty))
53 previousInViewport = inViewport;
54 clearDirty(DirtyFlag::CameraDirty);
57 case QSSGRenderGraphObject::Type::OrthographicCamera:
58 retval = computeFrustumOrtho(inViewport);
60 case QSSGRenderGraphObject::Type::PerspectiveCamera:
61 retval = computeFrustumPerspective(inViewport);
63 case QSSGRenderGraphObject::Type::CustomCamera:
66 case QSSGRenderGraphObject::Type::CustomFrustumCamera:
67 retval = computeCustomFrustum(inViewport);
74 float *writePtr(projection.data());
75 frustumScale.setX(writePtr[0]);
76 frustumScale.setY(writePtr[5]);
86bool QSSGRenderCamera::computeFrustumPerspective(
const QRectF &inViewport)
88 projection = QMatrix4x4();
89 projection.perspective(qRadiansToDegrees(verticalFov(inViewport)), getAspectRatio(inViewport), clipNear, clipFar);
93bool QSSGRenderCamera::computeCustomFrustum(
const QRectF &inViewport)
96 projection.setToIdentity();
97 projection.frustum(left, right, bottom, top, clipNear, clipFar);
101void QSSGRenderCamera::calculateViewProjectionMatrix(
const QMatrix4x4 &globalTransform,
102 const QMatrix4x4 &projection,
103 QMatrix4x4 &outMatrix)
105 QMatrix4x4 nonScaledGlobal(Qt::Uninitialized);
106 nonScaledGlobal.setColumn(0, globalTransform.column(0).normalized());
107 nonScaledGlobal.setColumn(1, globalTransform.column(1).normalized());
108 nonScaledGlobal.setColumn(2, globalTransform.column(2).normalized());
109 nonScaledGlobal.setColumn(3, globalTransform.column(3));
110 outMatrix = projection * nonScaledGlobal.inverted();
113void QSSGRenderCamera::calculateViewProjectionMatrix(QMatrix4x4 &outMatrix,
float clipNear,
float clipFar)
const
115 if (qFuzzyIsNull(clipFar - clipNear)) {
116 qWarning() <<
"QSSGRenderCamera::calculateViewProjection: far == near";
120 QMatrix4x4 proj = projection;
121 proj(2, 2) = -(clipFar + clipNear) / (clipFar - clipNear);
122 proj(2, 3) = -2 * clipFar * clipNear / (clipFar - clipNear);
123 QMatrix4x4 nonScaledGlobal(Qt::Uninitialized);
124 nonScaledGlobal.setColumn(0, 0.5f * globalTransform.column(0).normalized());
125 nonScaledGlobal.setColumn(1, 0.5f * globalTransform.column(1).normalized());
126 nonScaledGlobal.setColumn(2, 0.5f * globalTransform.column(2).normalized());
127 nonScaledGlobal.setColumn(3, 0.5f * globalTransform.column(3));
128 outMatrix = proj * nonScaledGlobal.inverted();
136bool QSSGRenderCamera::computeFrustumOrtho(
const QRectF &inViewport)
138 projection = QMatrix4x4();
139 float halfWidth = inViewport.width() / 2.0f / horizontalMagnification / dpr;
140 float halfHeight = inViewport.height() / 2.0f / verticalMagnification / dpr;
141 projection.ortho(-halfWidth, halfWidth, -halfHeight, halfHeight, clipNear, clipFar);
152 const QVector3D &sourceDirection,
153 const QVector3D &targetPosition,
154 const QVector3D &upDirection)
156 QVector3D targetDirection = sourcePosition - targetPosition;
157 targetDirection.normalize();
159 QVector3D rotationAxis = QVector3D::crossProduct(sourceDirection, targetDirection);
161 const QVector3D normalizedAxis = rotationAxis.normalized();
162 if (qFuzzyIsNull(normalizedAxis.lengthSquared()))
163 rotationAxis = upDirection;
165 float dot = QVector3D::dotProduct(sourceDirection, targetDirection);
166 float rotationAngle =
float(qRadiansToDegrees(qAcos(qreal(dot))));
168 return QQuaternion::fromAxisAndAngle(rotationAxis, rotationAngle);
171void QSSGRenderCamera::lookAt(
const QVector3D &inCameraPos,
const QVector3D &inUpDir,
const QVector3D &inTargetPos,
const QVector3D &pivot)
173 QQuaternion rotation = rotationQuaternionForLookAt(inCameraPos, getScalingCorrectDirection(), inTargetPos, inUpDir.normalized());
174 globalTransform = localTransform = QSSGRenderNode::calculateTransformMatrix(inCameraPos, QSSGRenderNode::initScale, pivot, rotation);
175 QSSGRenderNode::markDirty(QSSGRenderNode::DirtyFlag::TransformDirty);
178void QSSGRenderCamera::calculateViewProjectionMatrix(QMatrix4x4 &outMatrix)
const
180 QMatrix4x4 nonScaledGlobal(Qt::Uninitialized);
181 nonScaledGlobal.setColumn(0, globalTransform.column(0).normalized());
182 nonScaledGlobal.setColumn(1, globalTransform.column(1).normalized());
183 nonScaledGlobal.setColumn(2, globalTransform.column(2).normalized());
184 nonScaledGlobal.setColumn(3, globalTransform.column(3));
185 outMatrix = projection * nonScaledGlobal.inverted();
188void QSSGRenderCamera::calculateViewProjectionWithoutTranslation(
float clipNear,
float clipFar, QMatrix4x4 &outMatrix)
const
190 if (qFuzzyIsNull(clipFar - clipNear)) {
191 qWarning() <<
"QSSGRenderCamera::calculateViewProjection: far == near";
195 QMatrix4x4 proj = projection;
196 proj(2, 2) = -(clipFar + clipNear) / (clipFar - clipNear);
197 proj(2, 3) = -2 * clipFar * clipNear / (clipFar - clipNear);
198 QMatrix4x4 nonScaledGlobal(Qt::Uninitialized);
199 nonScaledGlobal.setColumn(0, globalTransform.column(0).normalized());
200 nonScaledGlobal.setColumn(1, globalTransform.column(1).normalized());
201 nonScaledGlobal.setColumn(2, globalTransform.column(2).normalized());
202 nonScaledGlobal.setColumn(3, QVector4D(0, 0, 0, 1));
203 outMatrix = proj * nonScaledGlobal.inverted();
206QSSGRenderRay QSSGRenderCamera::unproject(
const QVector2D &inViewportRelativeCoords,
207 const QRectF &inViewport)
const
209 QSSGRenderRay theRay;
210 QVector2D normalizedCoords = QSSGUtils::rect::relativeToNormalizedCoordinates(inViewport, inViewportRelativeCoords);
211 QVector3D &outOrigin(theRay.origin);
212 QVector3D &outDir(theRay.direction);
213 QVector2D inverseFrustumScale(1.0f / frustumScale.x(), 1.0f / frustumScale.y());
214 QVector2D scaledCoords(inverseFrustumScale.x() * normalizedCoords.x(), inverseFrustumScale.y() * normalizedCoords.y());
216 if (type == QSSGRenderCamera::Type::OrthographicCamera) {
217 outOrigin.setX(scaledCoords.x());
218 outOrigin.setY(scaledCoords.y());
219 outOrigin.setZ(0.0f);
225 outOrigin.setX(0.0f);
226 outOrigin.setY(0.0f);
227 outOrigin.setZ(0.0f);
229 outDir.setX(scaledCoords.x());
230 outDir.setY(scaledCoords.y());
234 outOrigin = QSSGUtils::mat44::transform(globalTransform, outOrigin);
235 QMatrix3x3 theNormalMatrix = calculateNormalMatrix();
237 outDir = QSSGUtils::mat33::transform(theNormalMatrix, outDir);
243QVector3D QSSGRenderCamera::unprojectToPosition(
const QVector3D &inGlobalPos,
const QSSGRenderRay &inRay)
const
245 QVector3D theCameraDir = getDirection();
246 QVector3D theObjGlobalPos = inGlobalPos;
247 float theDistance = -1.0f * QVector3D::dotProduct(theObjGlobalPos, theCameraDir);
248 QSSGPlane theCameraPlane(theCameraDir, theDistance);
249 return QSSGRenderRay::intersect(theCameraPlane, inRay).value_or(QVector3D{});
283 const float *data = projection.constData();
285 QSSGPlane nearPlane(QVector3D(data[3] + data[2], data[7] + data[6], data[11] + data[10]), -data[15] - data[14]);
286 nearPlane.normalize();
287 QSSGPlane rightPlane(QVector3D(data[3] - data[0], data[7] - data[4], data[11] - data[8]), -data[15] + data[12]);
288 rightPlane.normalize();
289 QSSGPlane topPlane(QVector3D(data[3] - data[1], data[7] - data[5], data[11] - data[9]), -data[15] + data[13]);
290 topPlane.normalize();
293 float denom = QVector3D::dotProduct(QVector3D::crossProduct(nearPlane.n, rightPlane.n), topPlane.n);
294 if (qFuzzyIsNull(denom))
297 QVector3D intersection = (QVector3D::crossProduct(rightPlane.n, topPlane.n) * nearPlane.d +
298 (QVector3D::crossProduct(topPlane.n, nearPlane.n) * rightPlane.d) +
299 (QVector3D::crossProduct(nearPlane.n, rightPlane.n) * topPlane.d)) / denom;
301 return QVector2D(intersection.x(), intersection.y());
304float QSSGRenderCamera::getLevelOfDetailMultiplier()
const
306 if (type == QSSGRenderGraphObject::Type::OrthographicCamera)
307 return getViewportHalfExtents(projection).x();
309 float zn = getZNear(projection);
310 float width = getViewportHalfExtents(projection).x() * 2.0;
311 return 1.0 / (zn / width);