Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qssgrenderclippingfrustum_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_CLIPPING_PLANE_H
6#define QSSG_RENDER_CLIPPING_PLANE_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/qssgplane_p.h>
20#include <QtQuick3DUtils/private/qssgbounds3_p.h>
21#include <QtQuick3DRuntimeRender/qtquick3druntimerenderexports.h>
22
24
26{
28 {
29 None = 0,
30 xMax = 1,
31 yMax = 1 << 1,
32 zMax = 1 << 2,
33 };
34 using BoxEdgeFlag = std::underlying_type_t<BoxEdgeID>;
35
36 // For an intesection test, we only need two points of the bounding box.
37 // There will be a point nearest to the plane, and a point furthest from the plane.
38 // We can derive these points from the plane normal equation.
44
46 float d;
48
49 // For intersection tests, we only need to know if the numerator is greater than, equal to,
50 // or less than zero.
51 [[nodiscard]] constexpr inline float distance(const QVector3D &pt) const { return QVector3D::dotProduct(normal, pt) + d; }
52
53 // Only works if p0 is above the line and p1 is below the plane.
54 inline QVector3D intersectWithLine(const QVector3D &p0, const QVector3D &p1) const
55 {
56 QVector3D dir = p1 - p0;
57 QVector3D pointOnPlane = normal * (-d);
58#ifdef _DEBUG
59 float distanceOfPoint = distance(pointOnPlane);
60 Q_ASSERT(qAbs(distanceOfPoint) < 0.0001f);
61#endif
62 float numerator = QVector3D::dotProduct(pointOnPlane - p0, normal);
63 float denominator = QVector3D::dotProduct(dir, normal);
64
65 Q_ASSERT(qAbs(denominator) > .0001f);
66 float t = (numerator / denominator);
67 QVector3D retval = p0 + dir * t;
68#ifdef _DEBUG
69 float retvalDistance = distance(retval);
70 Q_ASSERT(qAbs(retvalDistance) < .0001f);
71#endif
72 return retval;
73 }
74
75 static inline constexpr QVector3D corner(const QSSGBounds3 &bounds, BoxEdgeFlag edge)
76 {
77 return QVector3D((edge & BoxEdgeID::xMax) ? bounds.maximum[0] : bounds.minimum[0],
78 (edge & BoxEdgeID::yMax) ? bounds.maximum[1] : bounds.minimum[1],
79 (edge & BoxEdgeID::zMax) ? bounds.maximum[2] : bounds.minimum[2]);
80 }
81
82 // dividing the distance numerator
83
84 // I got this code from osg, but it is in graphics gems
85 // as well.
90 inline int intersect(const QSSGBounds3 &bounds) const
91 {
92 // if lowest point above plane than all above.
93 if (distance(corner(bounds, mEdges.lowerEdge)) > 0.0f)
94 return 1;
95
96 // if highest point is below plane then all below.
97 if (distance(corner(bounds, mEdges.upperEdge)) < 0.0f)
98 return -1;
99
100 // d_lower<=0.0f && d_upper>=0.0f
101 // therefore must be crossing plane.
102 return 0;
103 }
104
105 [[nodiscard]] constexpr bool intersectSimple(const QSSGBounds3 &bounds) const
106 {
107 return (distance(corner(bounds, mEdges.lowerEdge)) > 0.0f) || !(distance(corner(bounds, mEdges.upperEdge)) < 0.0f);
108 }
109
110 inline void calculateBBoxEdges()
111 {
113 | (normal[1] >= 0.0f ? BoxEdgeID::yMax : BoxEdgeID::None)
114 | (normal[2] >= 0.0f ? BoxEdgeID::zMax : BoxEdgeID::None);
115
117 }
118};
119
120struct Q_QUICK3DRUNTIMERENDER_EXPORT QSSGClippingFrustum
121{
122 QSSGClipPlane mPlanes[6];
123
125
126 QSSGClippingFrustum(const QMatrix4x4 &modelviewprojection, const QSSGClipPlane &nearPlane);
127
128 bool intersectsWith(const QSSGBounds3 &bounds) const
129 {
130 bool ret = true;
131
132 for (quint32 idx = 0; idx < 6 && ret; ++idx)
133 ret = mPlanes[idx].intersectSimple(bounds);
134 return ret;
135 }
136
137 bool intersectsWith(const QVector3D &point, float radius = 0.0f) const
138 {
139 bool ret = true;
140 for (quint32 idx = 0; idx < 6 && ret; ++idx)
141 ret = !(mPlanes[idx].distance(point) < radius);
142 return ret;
143 }
144};
146
147#endif
The QMatrix4x4 class represents a 4x4 transformation matrix in 3D space.
Definition qmatrix4x4.h:25
Class representing 3D range or axis aligned bounding box.
QVector3D minimum
QVector3D maximum
The QVector3D class represents a vector or vertex in 3D space.
Definition qvectornd.h:171
static constexpr float dotProduct(QVector3D v1, QVector3D v2) noexcept
Returns the dot product of v1 and v2.
Definition qvectornd.h:770
QPixmap p1
[0]
Combined button and popup list for selecting options.
return ret
constexpr T qAbs(const T &t)
Definition qnumeric.h:328
GLsizei GLsizei GLfloat distance
GLdouble GLdouble t
Definition qopenglext.h:243
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
unsigned int quint32
Definition qtypes.h:50
unsigned char quint8
Definition qtypes.h:46
QString dir
[11]
constexpr bool intersectSimple(const QSSGBounds3 &bounds) const
static constexpr QVector3D corner(const QSSGBounds3 &bounds, BoxEdgeFlag edge)
constexpr float distance(const QVector3D &pt) const
int intersect(const QSSGBounds3 &bounds) const
std::underlying_type_t< BoxEdgeID > BoxEdgeFlag
QVector3D intersectWithLine(const QVector3D &p0, const QVector3D &p1) const
bool intersectsWith(const QVector3D &point, float radius=0.0f) const
QSSGClippingFrustum()=default
bool intersectsWith(const QSSGBounds3 &bounds) const