📄 raybox.c
字号:
/* Fast Ray-Box Intersectionby Andrew Woofrom "Graphics Gems", Academic Press, 1990*/#include "GGems.h"#define NUMDIM 3#define RIGHT 0#define LEFT 1#define MIDDLE 2char 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<NUMDIM; i++) if(origin[i] < minB[i]) { quadrant[i] = LEFT; candidatePlane[i] = minB[i]; inside = FALSE; }else if (origin[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 ((quadrant[i] == RIGHT && coord[i] < minB[i]) || (quadrant[i] == LEFT && coord[i] > maxB[i])) return (FALSE); /* outside box */ }else { coord[i] = candidatePlane[i]; } return (TRUE); /* ray hits box */}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -