frustum.cpp 7.89 KB
Newer Older
schultezub's avatar
schultezub committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**********************************************************************
 *                                                                    *
 * tgt - Tiny Graphics Toolbox                                        *
 *                                                                    *
 * Copyright (C) 2006-2011 Visualization and Computer Graphics Group, *
 * Department of Computer Science, University of Muenster, Germany.   *
 * <http://viscg.uni-muenster.de>                                     *
 *                                                                    *
 * This file is part of the tgt library. This library is free         *
 * software; you can redistribute it and/or modify it under the terms *
 * of the GNU Lesser General Public License version 2.1 as published  *
 * by the Free Software Foundation.                                   *
 *                                                                    *
 * This library 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 Lesser General Public License for more details.                *
 *                                                                    *
 * You should have received a copy of the GNU Lesser General Public   *
 * License in the file "LICENSE.txt" along with this library.         *
 * If not, see <http://www.gnu.org/licenses/>.                        *
 *                                                                    *
 **********************************************************************/

25
#include "cgt/frustum.h"
schultezub's avatar
schultezub committed
26

27
28
#include "cgt/camera.h"
#include "cgt/assert.h"
schultezub's avatar
schultezub committed
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281

namespace tgt {

Frustum::Frustum(float fovy, float ratio, float nearDist, float farDist)
  : nearDist_(nearDist),
    farDist_ (farDist)
{
    float halfheight = tanf(0.5f * deg2rad(fovy)) * nearDist_;
    tnear_ =  halfheight;
    bnear_ = -halfheight;
    setRatio(ratio);
}

Frustum::Frustum(float left, float right, float bottom, float top, float nearDist, float farDist)
  : lnear_(left),
    rnear_(right),
    bnear_(bottom),
    tnear_(top),
    nearDist_(nearDist),
    farDist_(farDist)
{}

bool Frustum::operator==(const Frustum& rhs) const {
    return (rhs.lnear_ == lnear_) && (rhs.rnear_ == rnear_) && (rhs.bnear_ == bnear_) && (rhs.tnear_ == tnear_) &&
        (rhs.nearDist_ == nearDist_) && (rhs.farDist_ == farDist_);
}

void Frustum::update(Camera* c) {
    campos_ = c->getPosition();

    normals_[FARN]  = c->getLook();
    normals_[NEARN] = -1.f * normals_[FARN];

    vec3 cam2near = normals_[FARN] * nearDist_;

    nearp_ = campos_ + cam2near;
    farp_  = campos_ + normals_[FARN] * farDist_;

    vec3 strafe = c->getStrafe();
    vec3 tmp;

    tmp = cam2near + tnear_ * cross(strafe, c->getLook());
    normals_[TOPN]    = normalize(cross(strafe, tmp));

    tmp = cam2near + bnear_ * cross(strafe, c->getLook());
    normals_[BOTTOMN] = normalize(cross(tmp, strafe));

    tmp = cam2near + lnear_ * strafe;
    normals_[LEFTN]   = normalize(cross(cross(strafe, c->getLook()), tmp));

    tmp = cam2near + rnear_ * strafe;
    normals_[RIGHTN]  = normalize(cross(tmp, cross(strafe, c->getLook())));
}

// Is this bounding-box culled?
bool Frustum::isCulledXZ(const Bounds& bounds) const {
    vec3 urb = bounds.getURB();
    vec3 llf = bounds.getLLF();

    vec3 points[4];
    points[0] = vec3(llf.x, 0.f, llf.z);
    points[1] = vec3(urb.x, 0.f, llf.z);
    points[2] = vec3(urb.x, 0.f, urb.z);
    points[3] = vec3(llf.x, 0.f, urb.z);

    // All 6 sides of the frustum are tested against all four corners
    // of the bounding box. If all four points of the box are outside of
    // a frustum-plane, then the bounding box is not visible and we return
    // true.  If the box survives all tests then it is visible.
    for (size_t i = 0; i < 6; i++) {
        int outside = 0;
        for (size_t j = 0; j < 4; j++) {
            vec3 pos = (i < 4) ? campos_
                : ((i == 4) ? nearp() : farp());
            if (dot(getNormal(static_cast<int>(i)), points[j] - pos) >= 0.f)
                ++outside;
        }
        if (outside == 4)
            return true;
    }
    return false;
}

// Note: Seems to work, but not really tested.
bool Frustum::isCulled(const Bounds& bounds) const {
    vec3 urb = bounds.getURB();
    vec3 llf = bounds.getLLF();

    vec3 points[8];
    points[0] = vec3(llf.x, llf.y, llf.z);
    points[1] = vec3(urb.x, llf.y, llf.z);
    points[2] = vec3(urb.x, llf.y, urb.z);
    points[3] = vec3(llf.x, llf.y, urb.z);
    points[4] = vec3(llf.x, urb.y, llf.z);
    points[5] = vec3(urb.x, urb.y, llf.z);
    points[6] = vec3(urb.x, urb.y, urb.z);
    points[7] = vec3(llf.x, urb.y, urb.z);

    for (size_t i = 0; i < 6; i++) {
        int outside = 0;
        for (size_t j = 0; j < 8; j++) {
            vec3 pos = (i < 4) ? campos_
                : ((i == 4) ? nearp() : farp());

            if (dot(getNormal(static_cast<int>(i)), points[j] - pos) >= 0.f)
                ++outside;
        }
        if (outside == 8)
            return true;
    }

    return false;

}

// Is this point culled?
bool Frustum::isCulled(const vec3& v) const {
    // the position of the camera is lying on the top, bottom, left, and right
    // plane, so we might as well use it as the reference point for those planes
    vec3 dist = v - campos_;

    // test the point against all 6 planes of the frustum (there are more
    // efficient algorithms for this, if you deem this too slow knock yourself out and
    // code something fancy)
    if ((dot(leftn(),   dist) >= 0.f) ||
        (dot(rightn(),  dist) >= 0.f) ||
        (dot(bottomn(), dist) >= 0.f) ||
        (dot(topn(),    dist) >= 0.f) ||
        (dot(farn(),    v - farp()) >= 0.f) ||
        (dot(nearn(),   v - nearp() ) >= 0.f))
    {
        return true;
    } else {
        // all tests survived? then the point is visible
        return false;
    }
}

void Frustum::setFovy(float fovy) {
    float halfheight = tanf(0.5f * deg2rad(fovy)) * nearDist_;
    //float oldRatio = getRatio();
    tnear_ =  halfheight;
    bnear_ = -halfheight;

    // update left and right to reflect previous ratio
    //setRatio(oldRatio);
}

void Frustum::setRatio(float ratio) {
    float halfwidth = 0.5f * (tnear_-bnear_) * ratio;
    lnear_ = -halfwidth;
    rnear_ =  halfwidth;
    bnear_ = -halfwidth / ratio;
    tnear_ =  halfwidth / ratio;
}

void Frustum::setNearDist(float nearDist) {
    nearDist_ = nearDist;
}

const vec3& Frustum::campos() const {
    return campos_;
}

const vec3& Frustum::farp() const {
    return farp_;
}

const vec3& Frustum::nearp() const {
    return nearp_;
}

const vec3& Frustum::farn() const {
    return normals_[FARN];
}

const vec3& Frustum::nearn() const {
    return normals_[NEARN];
}

const vec3& Frustum::topn() const {
    return normals_[TOPN];
}

const vec3& Frustum::bottomn() const {
    return normals_[BOTTOMN];
}

const vec3& Frustum::rightn() const {
    return normals_[RIGHTN];
}

const vec3& Frustum::leftn() const {
    return normals_[LEFTN];
}

void Frustum::setBottom(float v) {
    bnear_ = v;
}

void Frustum::setTop(float v) {
    tnear_ = v;
}

void Frustum::setRight(float v) {
    rnear_ = v;
}

void Frustum::setLeft(float v) {
    lnear_ = v;
}

void Frustum::setFarDist(float farDist) {
    farDist_ = farDist;
}

float Frustum::getBottom() const {
    return bnear_;
}

float Frustum::getFovy() const {
    return rad2deg(atanf(tnear_/nearDist_) - atanf(bnear_/nearDist_) );
}

float Frustum::getRatio() const {
    return (rnear_ - lnear_)/(tnear_ - bnear_);
}

float Frustum::getNearDist() const {
    return nearDist_;
}

float Frustum::getFarDist() const {
    return farDist_;
}

float Frustum::getLeft() const {
    return lnear_;
}

float Frustum::getRight() const {
    return rnear_;
}

float Frustum::getTop() const {
    return tnear_;
}

const vec3& Frustum::getNormal(int num) const {
    return normals_[num];
}

}; // namespace tgt