1
// worldrender.cpp: goes through all cubes in top down quad tree fashion, determines what has to
2
// be rendered and how (depending on neighbouring cubes), then calls functions in rendercubes.cpp
7
void render_wall(sqr *o, sqr *s, int x1, int y1, int x2, int y2, int mip, sqr *d1, sqr *d2, bool topleft, int dir)
10
if(SOLID(o) || o->type==SEMISOLID)
14
if(s->type==FHF) { c1 -= d1->vdelta/4.0f; c2 -= d2->vdelta/4.0f; }
17
if(s->type==CHF) { f1 += d1->vdelta/4.0f; f2 += d2->vdelta/4.0f; }
18
//if(f1-c1<=0 && f2-c2<=0) return;
19
render_square(o->wtex, c1, c2, f1, f2, x1<<mip, y1<<mip, x2<<mip, y2<<mip, 1<<mip, d1, d2, topleft, dir);
27
if(o->type==FHF && s->type!=FHF)
29
c1 -= d1->vdelta/4.0f;
30
c2 -= d2->vdelta/4.0f;
32
if(s->type==FHF && o->type!=FHF)
34
f1 -= d1->vdelta/4.0f;
35
f2 -= d2->vdelta/4.0f;
37
if(f1>=c1 && f2>=c2) goto skip;
38
render_square(o->wtex, f1, f2, c1, c2, x1<<mip, y1<<mip, x2<<mip, y2<<mip, 1<<mip, d1, d2, topleft, dir);
46
if(o->type==CHF && s->type!=CHF)
48
f1 += d1->vdelta/4.0f;
49
f2 += d2->vdelta/4.0f;
51
else if(s->type==CHF && o->type!=CHF)
53
c1 += d1->vdelta/4.0f;
54
c2 += d2->vdelta/4.0f;
56
if(c1<=f1 && c2<=f2) return;
57
render_square(o->utex, f1, f2, c1, c2, x1<<mip, y1<<mip, x2<<mip, y2<<mip, 1<<mip, d1, d2, topleft, dir);
61
const int MAX_MIP = 5; // 32x32 unit blocks
62
const int MIN_LOD = 2;
63
const int LOW_LOD = 25;
64
const int MAX_LOD = 250;
66
int lod = 40, lodtop, lodbot, lodleft, lodright;
69
int lod_factor() { return lod; }
71
VARP(minlod, LOW_LOD, 60, MAX_LOD);
73
int stats[LARGEST_FACTOR];
75
// detect those cases where a higher mip solid has a visible wall next to lower mip cubes
76
// (used for wall rendering below)
78
bool issemi(int mip, int x, int y, int x1, int y1, int x2, int y2)
80
if(!(mip--)) return true;
82
int mfactor = sfactor - mip;
85
switch(SWS(w, x+x1, y+y1, mfactor)->type)
87
case SEMISOLID: if(issemi(mip, x+x1, y+y1, x1, y1, x2, y2)) return true;
92
switch(SWS(w, x+x2, y+y2, mfactor)->type)
94
case SEMISOLID: if(issemi(mip, x+x2, y+y2, x1, y1, x2, y2)) return true;
102
bool render_floor, render_ceil;
104
// the core recursive function, renders a rect of cubes at a certain mip level from a viewer perspective
105
// call itself for lower mip levels, on most modern machines however this function will use the higher
106
// mip levels only for perfect mips.
108
void render_seg_new(float vx, float vy, float vh, int mip, int x, int y, int xs, int ys)
111
int mfactor = sfactor - mip;
113
int vxx = ((int)vx+(1<<mip)/2)>>mip;
114
int vyy = ((int)vy+(1<<mip)/2)>>mip;
115
int lx = vxx-lodleft; // these mark the rect inside the current rest that we want to render using a lower mip level
117
int rx = vxx+lodright;
120
float fsize = (float)(1<<mip);
121
for(int oy = y; oy<ys; oy++) for(int ox = x; ox<xs; ox++) // first collect occlusion information for this block
123
SWS(w,ox,oy,mfactor)->occluded = isoccluded(camera1->o.x, camera1->o.y, (float)(ox<<mip), (float)(oy<<mip), fsize);
126
int pvx = (int)vx>>mip;
127
int pvy = (int)vy>>mip;
128
if(pvx>=0 && pvy>=0 && pvx<sz && pvy<sz)
130
//SWS(w,vxx,vyy,mfactor)->occluded = 0;
131
SWS(w, pvx, pvy, mfactor)->occluded = 0; // player cell never occluded
134
#define df(x) s->floor-(x->vdelta/4.0f)
135
#define dc(x) s->ceil+(x->vdelta/4.0f)
137
// loop through the rect 3 times (for floor/ceil/walls seperately, to facilitate dynamic stripify)
138
// for each we skip occluded cubes (occlusion at higher mip levels is a big time saver!).
139
// during the first loop (ceil) we collect cubes that lie within the lower mip rect and are
140
// also deferred, and render them recursively. Anything left (perfect mips and higher lods) we
143
#define LOOPH {for(int yy = y; yy<ys; yy++) for(int xx = x; xx<xs; xx++) { \
144
sqr *s = SWS(w,xx,yy,mfactor); if(s->occluded) continue; \
145
if(s->defer && mip && xx>=lx && xx<rx && yy>=ly && yy<ry)
146
#define LOOPD sqr *t = SWS(s,1,0,mfactor); \
147
sqr *u = SWS(s,1,1,mfactor); \
148
sqr *v = SWS(s,0,1,mfactor);
155
while(xx<xs-1 && (next = SWS(w,xx+1,yy,mfactor))->defer && !next->occluded) xx++; // collect 2xN rect of lower mip
156
render_seg_new(vx, vy, vh, mip-1, start*2, yy*2, xx*2+2, yy*2+2);
165
if(s->floor<=vh && render_floor)
167
render_flat(s->ftex, xx<<mip, yy<<mip, 1<<mip, s->floor, s, t, u, v, false);
168
if(s->floor<hdr.waterlevel && !reflecting) addwaterquad(xx<<mip, yy<<mip, 1<<mip);
172
render_flatdelta(s->ftex, xx<<mip, yy<<mip, 1<<mip, df(s), df(t), df(u), df(v), s, t, u, v, false);
173
if(s->floor-s->vdelta/4.0f<hdr.waterlevel && !reflecting) addwaterquad(xx<<mip, yy<<mip, 1<<mip);
178
if(!rendered) return;
179
stats[mip] += rendered;
181
if(!minimap) LOOPH continue; // ceils
187
if(s->ceil>=vh && render_ceil)
188
render_flat(s->ctex, xx<<mip, yy<<mip, 1<<mip, s->ceil, s, t, u, v, true);
191
render_flatdelta(s->ctex, xx<<mip, yy<<mip, 1<<mip, dc(s), dc(t), dc(u), dc(v), s, t, u, v, true);
196
LOOPH continue; // walls
202
sqr *w = SWS(s,0,-1,mfactor);
203
sqr *z = SWS(s,-1,0,mfactor);
204
bool normalwall = true;
214
if(SOLID(w)) { render_wall(w, h2 = s, xx+1, yy, xx, yy+1, mip, t, v, false, 4); topleft = false; }
215
else if(SOLID(v)) { render_wall(v, h2 = s, xx, yy, xx+1, yy+1, mip, s, u, false, 5); }
219
if(SOLID(w)) { render_wall(w, h1 = s, xx+1, yy+1, xx, yy, mip, u, s, false, 6); }
220
else if(SOLID(v)) { render_wall(v, h1 = s, xx, yy+1, xx+1, yy, mip, v, t, false, 7); topleft = false; }
225
bool wv = w->ceil-w->floor < v->ceil-v->floor;
226
if(z->ceil-z->floor < t->ceil-t->floor)
228
if(wv) { render_wall(h1 = s, h2 = v, xx+1, yy, xx, yy+1, mip, t, v, false, 4); topleft = false; }
229
else { render_wall(h1 = s, h2 = w, xx, yy, xx+1, yy+1, mip, s, u, false, 5); }
233
if(wv) { render_wall(h2 = s, h1 = v, xx+1, yy+1, xx, yy, mip, u, s, false, 6); }
234
else { render_wall(h2 = s, h1 = w, xx, yy+1, xx+1, yy, mip, v, t, false, 7); topleft = false; }
237
render_tris(xx<<mip, yy<<mip, 1<<mip, topleft, h1, h2, s, t, u, v);
242
bool inner = xx!=sz-1 && yy!=sz-1;
244
if(xx>=vxx && xx!=0 && yy!=sz-1 && !SOLID(z) && (!SOLID(s) || z->type!=CORNER)
245
&& (z->type!=SEMISOLID || issemi(mip, xx-1, yy, 1, 0, 1, 1)))
246
render_wall(s, z, xx, yy, xx, yy+1, mip, s, v, true, 0);
247
if(xx<=vxx && inner && !SOLID(t) && (!SOLID(s) || t->type!=CORNER)
248
&& (t->type!=SEMISOLID || issemi(mip, xx+1, yy, 0, 0, 0, 1)))
249
render_wall(s, t, xx+1, yy, xx+1, yy+1, mip, t, u, false, 1);
250
if(yy>=vyy && yy!=0 && xx!=sz-1 && !SOLID(w) && (!SOLID(s) || w->type!=CORNER)
251
&& (w->type!=SEMISOLID || issemi(mip, xx, yy-1, 0, 1, 1, 1)))
252
render_wall(s, w, xx, yy, xx+1, yy, mip, s, t, false, 2);
253
if(yy<=vyy && inner && !SOLID(v) && (!SOLID(s) || v->type!=CORNER)
254
&& (v->type!=SEMISOLID || issemi(mip, xx, yy+1, 0, 0, 1, 0)))
255
render_wall(s, v, xx, yy+1, xx+1, yy+1, mip, v, u, true, 3);
260
void distlod(int &low, int &high, int angle, float widef)
262
float f = 90.0f/lod/widef;
263
low = (int)((90-angle)/f);
264
high = (int)(angle/f);
265
if(low<min_lod) low = min_lod;
266
if(high<min_lod) high = min_lod;
269
// does some out of date view frustrum optimisation that doesn't contribute much anymore
271
void render_world(float vx, float vy, float vh, float changelod, int yaw, int pitch, float fov, float fovy, int w, int h)
273
loopi(LARGEST_FACTOR) stats[i] = 0;
274
min_lod = minimap ? MAX_LOD : MIN_LOD+abs(pitch)/12;
276
float widef = fov/75.0f;
277
int cdist = abs(yaw%90-45);
278
if(cdist<7) // hack to avoid popup at high fovs at 45 yaw
280
min_lod = max(min_lod, (int)(MIN_LOD+(10-cdist)/1.0f*widef)); // less if lod worked better
283
lod = (int)(lod*changelod);
284
if(lod<minlod) lod = minlod;
285
if(lod>MAX_LOD) lod = MAX_LOD;
286
lodtop = lodbot = lodleft = lodright = min_lod;
287
if(yaw>45 && yaw<=135)
290
distlod(lodtop, lodbot, yaw-45, widef);
292
else if(yaw>135 && yaw<=225)
295
distlod(lodleft, lodright, yaw-135, widef);
297
else if(yaw>225 && yaw<=315)
300
distlod(lodbot, lodtop, yaw-225, widef);
305
distlod(lodright, lodleft, yaw<=45 ? yaw+45 : yaw-315, widef);
307
render_floor = pitch<0.5f*fovy;
308
render_ceil = -pitch<0.5f*fovy;
310
render_seg_new(vx, vy, vh, MAX_MIP, 0, 0, ssize>>MAX_MIP, ssize>>MAX_MIP);
311
mipstats(stats[0], stats[1], stats[2]);