-
Notifications
You must be signed in to change notification settings - Fork 6
/
Frustum.cpp
163 lines (141 loc) · 5.46 KB
/
Frustum.cpp
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
162
163
/*
This file is part of the Geometry library.
Copyright (C) 2007-2012 Benjamin Eikel <[email protected]>
Copyright (C) 2007-2012 Claudius Jähn <[email protected]>
Copyright (C) 2007-2012 Ralf Petring <[email protected]>
This library is subject to the terms of the Mozilla Public License, v. 2.0.
You should have received a copy of the MPL along with this library; see the
file LICENSE. If not, you can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "Frustum.h"
#include "Box.h"
#include "BoxHelper.h"
#include <cmath>
#include <cstdint>
//
///**
// * sphereInFrustum
// * // www.lighthouse3d.com/opengl/viewfrustum/index.php?rimp
// */
//// 1. Sphere-Test
//
// if (angle != 0) // 0 --> Orthogonal Kamera, Spheretest funktioniert noch nicht ;-)
// {
// int c=sphereInFrustum(b.getCenter(),b.getBoundingSphereRadius());
// if (c==Frustum::OUTSIDE || c==Frustum::INSIDE)
// return c;
// }
namespace Geometry {
//! [dtor]
Frustum::Frustum() {
setPosition(Vec3(0, 0, 0), Vec3(0, 0, 1), Vec3(0, 1, 0));
setPerspective(Angle::deg(60.0f), 1.0f, 0.1f, 1000.0f);
}
//! [dtor]
Frustum::Frustum(const Angle & angle, float ratio, float _near, float _far) {
setPerspective(angle, ratio, _near, _far);
// ctor
}
void Frustum::setPerspective(const Angle & angle, float ratio, float _near, float _far) {
const float tang = std::tan(angle.rad() * 0.5f);
setFrustum(-_near * tang * ratio, _near * tang * ratio, -_near * tang, _near * tang, _near, _far, false);
}
void Frustum::setFrustum(float _left, float _right, float _bottom, float _top, float _near, float _far,
bool _orthogonal /*= false*/) {
orthogonal = _orthogonal;
nearValue = _near;
farValue = _far;
leftValue = _left;
rightValue = _right;
topValue = _top;
bottomValue = _bottom;
// update matrix
if (orthogonal) {
projectionMatrix =
Matrix4x4::orthographicProjection(leftValue, rightValue, bottomValue, topValue, nearValue, farValue);
} else {
projectionMatrix =
Matrix4x4::perspectiveProjection(leftValue, rightValue, bottomValue, topValue, nearValue, farValue);
}
// update corners
recalculateCornersAndPlanes();
}
void Frustum::setFrustumFromAngles(const Angle & fovLeft, const Angle & fovRight, const Angle & fovBottom,
const Angle & fovTop, float _near, float _far) {
setFrustum(_near * std::tan(fovLeft.rad()), _near * std::tan(fovRight.rad()), _near * std::tan(fovBottom.rad()),
_near * std::tan(fovTop.rad()), _near, _far, false);
}
/**
* @param _pos vector to pyramid-root
* _dir direction-vector relative to (0,0,0)
* _up up-vector relative to (0,0,0)
*/
void Frustum::setPosition(const Vec3 & pos, const Vec3 & _dir, const Vec3 & _up) {
orientation = SRT(pos, Vec3(_dir).normalize(), Vec3(_up).normalize());
recalculateCornersAndPlanes();
}
void Frustum::recalculateCornersAndPlanes() {
SRT s2 = SRT(getPos(), -getDir(), getUp());
Matrix4x4 m = Matrix4x4(s2) * projectionMatrix.inverse();
// Careful: Z values are inverted by matrix. Therefore the front and back side are swapped (see sign of Z values
// below).
corners[static_cast<std::size_t>(corner_t::xyz)] = m.transformPosition(-1, -1, 1);
corners[static_cast<std::size_t>(corner_t::Xyz)] = m.transformPosition(1, -1, 1);
corners[static_cast<std::size_t>(corner_t::xYz)] = m.transformPosition(-1, 1, 1);
corners[static_cast<std::size_t>(corner_t::XYz)] = m.transformPosition(1, 1, 1);
corners[static_cast<std::size_t>(corner_t::xyZ)] = m.transformPosition(-1, -1, -1);
corners[static_cast<std::size_t>(corner_t::XyZ)] = m.transformPosition(1, -1, -1);
corners[static_cast<std::size_t>(corner_t::xYZ)] = m.transformPosition(-1, 1, -1);
corners[static_cast<std::size_t>(corner_t::XYZ)] = m.transformPosition(1, 1, -1);
for (uint_fast8_t side = 0; side < 6; ++side) {
// Side corners
const corner_t * sC = Helper::getCornerIndices(static_cast<side_t>(side));
// Calculate plane
const Vec3 normal(((corners[static_cast<std::size_t>(sC[2])] - corners[static_cast<std::size_t>(sC[1])]).cross(corners[static_cast<std::size_t>(sC[0])] - corners[static_cast<std::size_t>(sC[1])])).normalize());
planes[side] = Plane(normal, normal.dot(corners[static_cast<std::size_t>(sC[1])]));
}
// Calculate bit-fields for bounding box corners.
for (uint_fast8_t plane = 0; plane < 6; ++plane) {
const Vec3 & currentNormal = planes[plane].getNormal();
uint8_t neg = 0;
if (currentNormal.x() < 0.0f) {
neg |= 1;
}
if (currentNormal.y() < 0.0f) {
neg |= 2;
}
if (currentNormal.z() < 0.0f) {
neg |= 4;
}
uint8_t pos = ~neg & 7;
negCorner[plane] = static_cast<corner_t>(neg);
posCorner[plane] = static_cast<corner_t>(pos);
}
}
Frustum::intersection_t Frustum::isBoxInFrustum(const Box & b) const {
/*
* Implementation corresponding to the pseudo-code in
* Ulf Assarsson, Tomas Möller: Optimized view frustum culling algorithms for bounding boxes.
* Journal of Graphics Tools, Volume 5, Issue 1, September 2000. Pages 9-22.
*/
bool intersect = false;
for (uint_fast8_t plane = 0; plane < 6; ++plane) {
const Vec3 nVec = b.getCorner(negCorner[plane]);
if (planes[plane].planeTest(nVec) > 0) {
return intersection_t::OUTSIDE;
}
const Vec3 pVec = b.getCorner(posCorner[plane]);
if (planes[plane].planeTest(pVec) > 0) {
intersect = true;
}
}
if (intersect) {
return intersection_t::INTERSECT;
} else {
return intersection_t::INSIDE;
}
}
bool Frustum::operator==(const Frustum & other) const {
return projectionMatrix == other.projectionMatrix;
}
}