1
// worldocull.cpp: occlusion map and occlusion test
12
void toggleocull() { ocull = !ocull; }
14
COMMAND(toggleocull, ARG_NONE);
16
// constructs occlusion map: cast rays in all directions on the 2d plane and record distance.
17
// done exactly once per frame.
19
void disableraytable()
22
loopi(NUMRAYS) rdist[i] = 1e16f;
25
void computeraytable(float vx, float vy, float fov)
27
if(!ocull) { disableraytable(); return; }
29
odist = getvar("fog")*1.5f;
31
float apitch = (float)fabs(camera1->pitch);
32
float af = fov/2+apitch/1.5f+3;
33
float byaw = (camera1->yaw-90+af)/360*PI2;
34
float syaw = (camera1->yaw-90-af)/360*PI2;
38
float angle = i*PI2/NUMRAYS;
39
if((apitch>45 // must be bigger if fov>120
40
|| (angle<byaw && angle>syaw)
41
|| (angle<byaw-PI2 && angle>syaw-PI2)
42
|| (angle<byaw+PI2 && angle>syaw+PI2))
44
&& !SOLID(S(int(vx), int(vy)))) // try to avoid tracing ray if outside of frustrum
46
float ray = i*8/(float)NUMRAYS;
48
if(ray>1 && ray<3) { dx = -(ray-2); dy = 1; }
49
else if(ray>=3 && ray<5) { dx = -1; dy = -(ray-4); }
50
else if(ray>=5 && ray<7) { dx = ray-6; dy = -1; }
51
else { dx = 1; dy = ray>4 ? ray-8 : ray; }
58
if(SOLID(S(int(sx), int(sy)))) // 90% of time spend in this function is on this line
60
rdist[i] = (float)(fabs(sx-vx)+fabs(sy-vy));
72
// test occlusion for a cube... one of the most computationally expensive functions in the engine
73
// as its done for every cube and entity, but its effect is more than worth it!
76
// GCC seems to have trouble inlining these
77
#define ca(xv, yv) ({ float x = (xv), y = (yv); x>y ? y/x : 2-x/y; })
78
#define ma(xv, yv) ({ float x = (xv), y = (yv); x==0 ? (y>0 ? 2 : -2) : y/x; })
80
static inline float ca(float x, float y) { return x>y ? y/x : 2-x/y; }
81
static inline float ma(float x, float y) { return x==0 ? (y>0 ? 2 : -2) : y/x; }
84
int isoccluded(float vx, float vy, float cx, float cy, float csize) // v = viewer, c = cube to test
90
// - check middle cube? BG
92
// find highest and lowest angle in the occlusion map that this cube spans, based on its most left and right
93
// points on the border from the viewer pov... I see no easier way to do this than this silly code below
95
float xdist = 0, ydist = 0, h, l;
98
if(cx+csize<vx) // ADF
100
if((xdist = vx-(cx+csize)) > odist) return 2;
103
if(cy+csize<vy) { if((ydist = vy-(cy+csize)) > odist) return 2; h = ca(-(cx-vx), ydist)+4; l = ca(xdist, -(cy-vy))+4; } // A
104
else { h = ma(xdist, -(cy+csize-vy))+4; l = ma(xdist, -(cy-vy))+4; } // D
106
else { if((ydist = cy-vy) > odist) return 2; h = ca(cy+csize-vy, xdist)+2; l = ca(ydist, -(cx-vx))+2; } // F
112
if(cy+csize<vy) { if((ydist = vy-(cy+csize)) > odist) return 2; h = ma(ydist, cx-vx)+6; l = ma(ydist, cx+csize-vx)+6; } // B
115
else { if((ydist = cy-vy) > odist) return 2; h = ma(ydist, -(cx+csize-vx))+2; l = ma(ydist, -(cx-vx))+2; } // G
120
if((xdist = cx-vx) > odist) return 2;
123
if(cy+csize<vy) { if((ydist = vy-(cy+csize)) > odist) return 2; h = ca(-(cy-vy), xdist)+6; l = ca(ydist, cx+csize-vx)+6; } // C
124
else { h = ma(xdist, cy-vy); l = ma(xdist, cy+csize-vy); } // E
126
else { if((ydist = cy-vy) > odist) return 2; h = ca(cx+csize-vx, ydist); l = ca(xdist, cy+csize-vy); } // H
129
float dist = xdist+ydist-1; // 1 needed?
130
int si = int(h*(NUMRAYS/8))+NUMRAYS; // get indexes into occlusion map from angles
131
int ei = int(l*(NUMRAYS/8))+NUMRAYS+1;
132
if(ei<=si) ei += NUMRAYS;
134
for(int i = si; i<=ei; i++)
136
if(dist<rdist[i&(NUMRAYS-1)]) return 0; // if any value in this segment of the occlusion map is further away then cube is not occluded
139
return 1; // cube is entirely occluded