summaryrefslogtreecommitdiffstats
path: root/src/Runtime/Source/runtimerender/Qt3DSRenderClippingFrustum.h
blob: b788ad242ff642132f97003e1488aae03fc3f16c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/****************************************************************************
**
** Copyright (C) 2008-2012 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#pragma once
#ifndef QT3DS_RENDER_CLIPPING_PLANE_H
#define QT3DS_RENDER_CLIPPING_PLANE_H
#include "Qt3DSRender.h"
#include "foundation/Qt3DSPlane.h"
#include "foundation/Qt3DSFlags.h"
#include "foundation/Qt3DSBounds3.h"

namespace qt3ds {
namespace render {

    struct BoxEdgeFlagValues
    {
        enum Enum {
            xMax = 1,
            yMax = 1 << 1,
            zMax = 1 << 2,
        };
    };

    typedef NVFlags<BoxEdgeFlagValues::Enum, QT3DSU8> TRenderBoxEdge;

    // For an intesection test, we only need two points of the bounding box.
    // There will be a point nearest to the plane, and a point furthest from the plane.
    // We can derive these points from the plane normal equation.
    struct SPlaneBoxEdge
    {
        TRenderBoxEdge lowerEdge;
        TRenderBoxEdge upperEdge;
    };

    struct SClipPlane
    {
        QT3DSVec3 normal;
        QT3DSF32 d;
        SPlaneBoxEdge mEdges;

        // For intersection tests, we only need to know if the numerator is greater than, equal to,
        // or less than zero.
        inline QT3DSF32 distance(const QT3DSVec3 &pt) const { return normal.dot(pt) + d; }

        // Only works if p0 is above the line and p1 is below the plane.
        inline QT3DSVec3 intersectWithLine(const QT3DSVec3 &p0, const QT3DSVec3 &p1) const
        {
            QT3DSVec3 dir = p1 - p0;
            QT3DSVec3 pointOnPlane = normal * (-d);
#ifdef _DEBUG
            QT3DSF32 distanceOfPoint = distance(pointOnPlane);
            QT3DS_ASSERT(NVAbs(distanceOfPoint) < 0.0001f);
#endif
            QT3DSF32 numerator = (pointOnPlane - p0).dot(normal);
            QT3DSF32 denominator = dir.dot(normal);

            QT3DS_ASSERT(NVAbs(denominator) > .0001f);
            QT3DSF32 t = (numerator / denominator);
            QT3DSVec3 retval = p0 + dir * t;
#ifdef _DEBUG
            QT3DSF32 retvalDistance = distance(retval);
            QT3DS_ASSERT(NVAbs(retvalDistance) < .0001f);
#endif
            return retval;
        }

        static inline QT3DSVec3 corner(const NVBounds3 &bounds, TRenderBoxEdge edge)
        {
            return QT3DSVec3((edge & BoxEdgeFlagValues::xMax) ? bounds.maximum[0] : bounds.minimum[0],
                          (edge & BoxEdgeFlagValues::yMax) ? bounds.maximum[1] : bounds.minimum[1],
                          (edge & BoxEdgeFlagValues::zMax) ? bounds.maximum[2] : bounds.minimum[2]);
        }

        // dividing the distance numerator

        // I got this code from osg, but it is in graphics gems
        // as well.
        /** intersection test between plane and bounding sphere.
        return 1 if the bs is completely above plane,
        return 0 if the bs intersects the plane,
        return -1 if the bs is completely below the plane.*/
        inline int intersect(const NVBounds3 &bounds) const
        {
            // if lowest point above plane than all above.
            if (distance(corner(bounds, mEdges.lowerEdge)) > 0.0f)
                return 1;

            // if highest point is below plane then all below.
            if (distance(corner(bounds, mEdges.upperEdge)) < 0.0f)
                return -1;

            // d_lower<=0.0f && d_upper>=0.0f
            // therefore must be crossing plane.
            return 0;
        }

        inline void calculateBBoxEdges()
        {
            mEdges.upperEdge = TRenderBoxEdge(
                static_cast<QT3DSU8>((normal[0] >= 0.0f ? BoxEdgeFlagValues::xMax : 0)
                                  | (normal[1] >= 0.0f ? BoxEdgeFlagValues::yMax : 0)
                                  | (normal[2] >= 0.0f ? BoxEdgeFlagValues::zMax : 0)));

            mEdges.lowerEdge = TRenderBoxEdge((~((QT3DSU8)mEdges.upperEdge)) & 7);
        }
    };

    struct SClippingFrustum
    {
        SClipPlane mPlanes[6];

        SClippingFrustum() {}

        SClippingFrustum(const QT3DSMat44 &modelviewprojection, SClipPlane nearPlane);

        bool intersectsWith(const NVBounds3 &bounds) const
        {
            for (QT3DSU32 idx = 0; idx < 6; ++idx)
                if (mPlanes[idx].intersect(bounds) < 0)
                    return false;
            return true;
        }

        bool intersectsWith(const QT3DSVec3 &point, QT3DSF32 radius = 0.0f) const
        {
            for (QT3DSU32 idx = 0; idx < 6; ++idx)
                if (mPlanes[idx].distance(point) < radius)
                    return false;
            return true;
        }
    };
}
}

#endif