00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "frustum.h"
00036
00037 #include <math.h>
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 static void
00048 setFrustumPlane
00049 (
00050 IO frustum_t& f,
00051 IN frustum_t::ePlane index,
00052 IN const point3d_t& a,
00053 IN const point3d_t& b,
00054 IN const point3d_t& p
00055 )
00056 throw()
00057 {
00058 plane_t& plane = f.plane[index];
00059
00060
00061 plane.n = crossProduct(a, b);
00062 normalize(plane.n);
00063
00064
00065 plane.d = dotProduct(plane.n, p);
00066 }
00067
00068
00069
00070 static float
00071 getTimeWhenLineIntersectsPlane
00072 (
00073 IN const plane_t& P,
00074 IN const point3d_t& p0,
00075 IN const point3d_t& pt
00076 )
00077 throw()
00078 {
00079 float denom = dotProduct(P.n, pt);
00080 float np = dotProduct(P.n, p0);
00081
00082 return (P.d - np) / denom;
00083 }
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093 eContains
00094 frustum_t::containsRect
00095 (
00096 IN const rect3d_t& r
00097 )
00098 const
00099 throw()
00100 {
00101
00102
00103
00104
00105
00106 eContains retval = eContains_Fully;
00107
00108
00109 for (int i = 0; i < ePlaneCount; ++i) {
00110 bool someInside = false;
00111 bool someOutside = false;
00112
00113
00114 for (int j = 0; j < 8 && (!someInside || !someOutside); ++j) {
00115 if (plane[i].distance(r.getCorner(j)) < 0) {
00116 someOutside = true;
00117 } else {
00118 someInside = true;
00119 }
00120 }
00121
00122 if (!someInside) {
00123
00124 return eContains_None;
00125 }
00126
00127 if (someOutside) {
00128
00129 retval = eContains_Partial;
00130 }
00131 }
00132
00133
00134 return retval;
00135 }
00136
00137
00138
00139 void
00140 frustum_t::getEdge
00141 (
00142 IN int index,
00143 OUT point3d_t& p0,
00144 OUT point3d_t& p1
00145 )
00146 const
00147 throw()
00148 {
00149 ASSERT(index >= 0 && index < eEdgeCount,
00150 "Bad frustum edge index: %d", index);
00151
00152
00153
00154
00155
00156
00157
00158
00159 struct edge_info_t {
00160 int planeA;
00161 int planeB;
00162 int start;
00163 int end;
00164 };
00165
00166 static edge_info_t s_edgeInfo[eEdgeCount] = {
00167
00168 { eTop, eRight, eNear, eFar },
00169 { eRight, eBottom, eNear, eFar },
00170 { eBottom, eLeft, eNear, eFar },
00171 { eLeft, eTop, eNear, eFar },
00172
00173 { eNear, eTop, eLeft, eRight },
00174 { eNear, eRight, eTop, eBottom },
00175 { eNear, eBottom, eRight, eLeft },
00176 { eNear, eLeft, eBottom, eTop },
00177
00178 { eFar, eTop, eLeft, eRight },
00179 { eFar, eRight, eTop, eBottom },
00180 { eFar, eBottom, eRight, eLeft },
00181 { eFar, eLeft, eBottom, eTop }
00182 };
00183
00184 const edge_info_t& ei = s_edgeInfo[index];
00185
00186
00187
00188 const plane_t& A = plane[ei.planeA];
00189 const plane_t& B = plane[ei.planeB];
00190
00191 point3d_t p, pt;
00192 ASSERT(getLineOfIntersection(A, B, p, pt),
00193 "Badly constructed frustum? Two adjacent planes are parallel?");
00194
00195
00196
00197
00198
00199 float t0 = getTimeWhenLineIntersectsPlane(plane[ei.start], p, pt);
00200 float t1 = getTimeWhenLineIntersectsPlane(plane[ei.end], p, pt);
00201
00202
00203
00204
00205 p0 = p + t0 * pt;
00206 p1 = p + t1 * pt;
00207 }
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217 bool
00218 createViewFrustum
00219 (
00220 IN const point3d_t& position,
00221 IN const point3d_t& facing,
00222 IN const point3d_t& up,
00223 IN float fovyRadians,
00224 IN float zNear,
00225 IN float zFar,
00226 IN float aspect,
00227 OUT frustum_t& f
00228 )
00229 throw()
00230 {
00231 ASSERT(fovyRadians > 0, "bad field of view: %f", fovyRadians);
00232 ASSERT(zNear > 0, "Bad z near: %f", zNear);
00233 ASSERT(zFar > zNear, "Bad z far: %f", zFar);
00234 ASSERT(aspect > 0, "Bad aspect ratio: %f", aspect);
00235 f.clear();
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 point3d_t right = crossProduct(facing, up);
00248
00249 normalize(right);
00250
00251
00252
00253 point3d_t newUp = crossProduct(right, facing);
00254 normalize(newUp);
00255
00256
00257 point3d_t nc;
00258 point3d_t fc;
00259
00260 nc = position + zNear * facing;
00261 fc = position + zFar * facing;
00262
00263
00264
00265
00266
00267 float yRadians = 0.5 * fovyRadians;
00268
00269 float hScale = tan(yRadians);
00270 float wScale = aspect * hScale;
00271
00272
00273 float farHeight = hScale * zFar;
00274 float farWidth = wScale * zFar;
00275
00276 float nearHeight = hScale * zNear;
00277 float nearWidth = wScale * zNear;
00278
00279
00280
00281
00282
00283
00284
00285 point3d_t ftl;
00286 point3d_t ftr;
00287 point3d_t fbl;
00288 point3d_t fbr;
00289
00290 point3d_t ntl;
00291 point3d_t ntr;
00292 point3d_t nbl;
00293 point3d_t nbr;
00294
00295 ftl = fc + farHeight * newUp - farWidth * right;
00296 ftr = fc + farHeight * newUp + farWidth * right;
00297 fbl = fc - farHeight * newUp - farWidth * right;
00298 fbr = fc - farHeight * newUp + farWidth * right;
00299
00300 ntl = nc + nearHeight * newUp - nearWidth * right;
00301 ntr = nc + nearHeight * newUp + nearWidth * right;
00302 nbl = nc - nearHeight * newUp - nearWidth * right;
00303 nbr = nc - nearHeight * newUp + nearWidth * right;
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321 setFrustumPlane(f, frustum_t::eFar, right, newUp, fc);
00322 setFrustumPlane(f, frustum_t::eNear, newUp, right, nc);
00323 setFrustumPlane(f, frustum_t::eLeft, newUp, ntl - ftl, position);
00324 setFrustumPlane(f, frustum_t::eRight, newUp, ftr - ntr, position);
00325 setFrustumPlane(f, frustum_t::eTop, right, ntr - ftr, position);
00326 setFrustumPlane(f, frustum_t::eBottom, right, fbr - nbr, position);
00327
00328
00329 return true;
00330 }
00331