# erich666/GraphicsGems

Switch branches/tags
Nothing to show
3f85c80 May 25, 2013
1 contributor

### Users who have contributed to this file

74 lines (63 sloc) 1.67 KB
 /* Fast Ray-Box Intersection by Andrew Woo from "Graphics Gems", Academic Press, 1990 */ #include "GraphicsGems.h" #define NUMDIM 3 #define RIGHT 0 #define LEFT 1 #define MIDDLE 2 char HitBoundingBox(minB,maxB, origin, dir,coord) double minB[NUMDIM], maxB[NUMDIM]; /*box */ double origin[NUMDIM], dir[NUMDIM]; /*ray */ double coord[NUMDIM]; /* hit point */ { char inside = TRUE; char quadrant[NUMDIM]; register int i; int whichPlane; double maxT[NUMDIM]; double candidatePlane[NUMDIM]; /* Find candidate planes; this loop can be avoided if rays cast all from the eye(assume perpsective view) */ for (i=0; i maxB[i]) { quadrant[i] = RIGHT; candidatePlane[i] = maxB[i]; inside = FALSE; }else { quadrant[i] = MIDDLE; } /* Ray origin inside bounding box */ if(inside) { coord = origin; return (TRUE); } /* Calculate T distances to candidate planes */ for (i = 0; i < NUMDIM; i++) if (quadrant[i] != MIDDLE && dir[i] !=0.) maxT[i] = (candidatePlane[i]-origin[i]) / dir[i]; else maxT[i] = -1.; /* Get largest of the maxT's for final choice of intersection */ whichPlane = 0; for (i = 1; i < NUMDIM; i++) if (maxT[whichPlane] < maxT[i]) whichPlane = i; /* Check final candidate actually inside box */ if (maxT[whichPlane] < 0.) return (FALSE); for (i = 0; i < NUMDIM; i++) if (whichPlane != i) { coord[i] = origin[i] + maxT[whichPlane] *dir[i]; if (coord[i] < minB[i] || coord[i] > maxB[i]) return (FALSE); } else { coord[i] = candidatePlane[i]; } return (TRUE); /* ray hits box */ }