Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qssgrenderray_p.h
Go to the documentation of this file.
1// Copyright (C) 2008-2012 NVIDIA Corporation.
2// Copyright (C) 2019 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
4
5#ifndef QSSG_RENDER_RAY_H
6#define QSSG_RENDER_RAY_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtQuick3DUtils/private/qssgbounds3_p.h>
20#include <QtQuick3DUtils/private/qssgplane_p.h>
21
22#include <QtGui/QVector2D>
23#include <QtGui/QVector3D>
24#include <QtGui/QMatrix4x4>
25
26#include <optional>
27
28QT_BEGIN_NAMESPACE
29class QSSGMeshBVHNode;
30struct QSSGRenderMesh;
31struct QSSGMeshBVHTriangle;
33{
37};
38
39struct Q_AUTOTEST_EXPORT QSSGRenderRay
40{
41 QVector3D origin;
42 QVector3D direction;
43 QSSGRenderRay() = default;
44 QSSGRenderRay(const QVector3D &inOrigin, const QVector3D &inDirection)
45 : origin(inOrigin), direction(inDirection)
46 {
47 }
48 // If we are parallel, then no intersection of course.
49 static std::optional<QVector3D> intersect(const QSSGPlane &inPlane, const QSSGRenderRay &ray);
50
51 // Perform an intersection aslo returning Barycentric Coordinates
52 static bool triangleIntersect(const QSSGRenderRay &ray,
53 const QVector3D &v0,
54 const QVector3D &v1,
55 const QVector3D &v2,
56 float &u,
57 float &v,
58 QVector3D &normal);
59
60 struct IntersectionResult
61 {
62 bool intersects = false;
63 float rayLengthSquared = 0.; // Length of the ray in world coordinates for the hit.
64 QVector2D relXY; // UV coords for further mouse picking against a offscreen-rendered object.
65 QVector3D scenePosition;
66 QVector3D localPosition;
67 QVector3D faceNormal;
68 QVector3D sceneFaceNormal;
69 IntersectionResult() = default;
70 inline constexpr IntersectionResult(float rl,
71 const QVector2D &relxy,
72 const QVector3D &scenePosition,
73 const QVector3D &localPosition,
74 const QVector3D &normal,
75 const QVector3D &sceneNormal)
76 : intersects(true)
77 , rayLengthSquared(rl)
78 , relXY(relxy)
79 , scenePosition(scenePosition)
80 , localPosition(localPosition)
81 , faceNormal(normal)
82 , sceneFaceNormal(sceneNormal)
83 {}
84 };
85
86 struct HitResult
87 {
88 float min;
89 float max;
90 const QSSGBounds3 *bounds;
91 inline bool intersects() const { return bounds && max >= std::max(min, 0.0f); }
92 };
93
94 struct RayData
95 {
96 enum class DirectionOp : quint8
97 {
98 Normal,
99 Swap,
100 Zero = 0x10
101 };
102
103 const QMatrix4x4 &globalTransform;
104 const QSSGRenderRay &ray;
105 // Cached data calculated from the global transform and the ray
106 const QVector3D origin;
107 const QVector3D directionInvers;
108 const QVector3D direction;
109 const DirectionOp dirOp[3];
110 };
111
112 static RayData createRayData(const QMatrix4x4 &globalTransform,
113 const QSSGRenderRay &ray);
114 static IntersectionResult createIntersectionResult(const RayData &data,
115 const HitResult &hit);
116
117 static HitResult intersectWithAABBv2(const RayData &data,
118 const QSSGBounds3 &bounds);
119
120 static void intersectWithBVH(const RayData &data,
121 const QSSGMeshBVHNode *bvh,
122 const QSSGRenderMesh *mesh,
123 QVector<IntersectionResult> &intersections,
124 int depth = 0);
125
126 static QVector<IntersectionResult> intersectWithBVHTriangles(const RayData &data,
127 const std::vector<QSSGMeshBVHTriangle> &bvhTriangles,
128 int triangleOffset,
129 int triangleCount);
130
131 std::optional<QVector2D> relative(const QMatrix4x4 &inGlobalTransform,
132 const QSSGBounds3 &inBounds,
133 QSSGRenderBasisPlanes inPlane) const;
134
135 std::optional<QVector2D> relativeXY(const QMatrix4x4 &inGlobalTransform, const QSSGBounds3 &inBounds) const
136 {
137 return relative(inGlobalTransform, inBounds, QSSGRenderBasisPlanes::XY);
138 }
139};
140QT_END_NAMESPACE
141#endif
QSSGRenderBasisPlanes