File: Frustum.cpp

package info (click to toggle)
0ad 0.0.26-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 130,460 kB
  • sloc: cpp: 261,824; ansic: 198,392; javascript: 19,067; python: 14,557; sh: 7,629; perl: 4,072; xml: 849; makefile: 741; java: 533; ruby: 229; php: 190; pascal: 30; sql: 21; tcl: 4
file content (156 lines) | stat: -rw-r--r-- 3,950 bytes parent folder | download | duplicates (4)
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
/* Copyright (C) 2021 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
Usually associated with the camera, there are 6 planes which define the
view pyramid. But we allow more planes per frustum which may be used for
portal rendering, where a portal may have 3 or more edges.
*/

#include "precompiled.h"

#include "Frustum.h"

#include "maths/BoundingBoxAligned.h"
#include "maths/MathUtil.h"
#include "maths/Matrix3D.h"

CFrustum::CFrustum()
{
	m_NumPlanes = 0;
}

CFrustum::~CFrustum()
{
}

void CFrustum::SetNumPlanes(size_t num)
{
	m_NumPlanes = num;

	if (m_NumPlanes >= MAX_NUM_FRUSTUM_PLANES)
	{
		debug_warn(L"CFrustum::SetNumPlanes: Too many planes");
		m_NumPlanes = MAX_NUM_FRUSTUM_PLANES - 1;
	}
}

void CFrustum::AddPlane(const CPlane& plane)
{
	if (m_NumPlanes >= MAX_NUM_FRUSTUM_PLANES)
	{
		debug_warn(L"CFrustum::AddPlane: Too many planes");
		return;
	}

	m_Planes[m_NumPlanes++] = plane;
}

void CFrustum::Transform(const CMatrix3D& m)
{
	for (size_t i = 0; i < m_NumPlanes; ++i)
	{
		CVector3D n = m.Rotate(m_Planes[i].m_Norm);
		CVector3D p = m.Transform(m_Planes[i].m_Norm * -m_Planes[i].m_Dist);
		m_Planes[i].Set(n, p);
		m_Planes[i].Normalize();
	}
}

bool CFrustum::IsPointVisible(const CVector3D& point) const
{
	for (size_t i = 0; i < m_NumPlanes; ++i)
	{
		if (m_Planes[i].IsPointOnBackSide(point))
			return false;
	}

	return true;
}

bool CFrustum::DoesSegmentIntersect(const CVector3D& startRef, const CVector3D& endRef) const
{
	CVector3D start = startRef;
	CVector3D end = endRef;

	if (IsPointVisible(start) || IsPointVisible(end))
		return true;

	CVector3D intersect;
	for (size_t i = 0; i < m_NumPlanes; ++i)
	{
		if (m_Planes[i].FindLineSegIntersection(start, end, &intersect))
		{
			if (IsPointVisible(intersect))
				return true;
		}
	}
	return false;
}

bool CFrustum::IsSphereVisible(const CVector3D& center, float radius) const
{
	for (size_t i = 0; i < m_NumPlanes; ++i)
	{
		// If none of the sphere is in front of the plane, then
		// it is outside the frustum.
		if (-m_Planes[i].DistanceToPlane(center) > radius)
			return false;
	}

	return true;
}

bool CFrustum::IsBoxVisible(const CVector3D& position, const CBoundingBoxAligned& bounds) const
{
	// Basically for every plane we calculate the furthest point
	// in the box to that plane. If that point is beyond the plane
	// then the box is not visible.
	CVector3D farPoint;
	CVector3D minPoint = position + bounds[0];
	CVector3D maxPoint = position + bounds[1];

	for (size_t i = 0; i < m_NumPlanes; ++i)
	{
		farPoint.X = m_Planes[i].m_Norm.X > 0.0f ? maxPoint.X : minPoint.X;
		farPoint.Y = m_Planes[i].m_Norm.Y > 0.0f ? maxPoint.Y : minPoint.Y;
		farPoint.Z = m_Planes[i].m_Norm.Z > 0.0f ? maxPoint.Z : minPoint.Z;

		if (m_Planes[i].IsPointOnBackSide(farPoint))
			return false;
	}

	return true;
}

bool CFrustum::IsBoxVisible(const CBoundingBoxAligned& bounds) const
{
	// Same as the previous one, but with the position = (0, 0, 0).
	CVector3D farPoint;

	for (size_t i = 0; i < m_NumPlanes; ++i)
	{
		farPoint.X = m_Planes[i].m_Norm.X > 0.0f ? bounds[1].X : bounds[0].X;
		farPoint.Y = m_Planes[i].m_Norm.Y > 0.0f ? bounds[1].Y : bounds[0].Y;
		farPoint.Z = m_Planes[i].m_Norm.Z > 0.0f ? bounds[1].Z : bounds[0].Z;

		if (m_Planes[i].IsPointOnBackSide(farPoint))
			return false;
	}

	return true;
}