| 11 |
|
#include "rholo.h" |
| 12 |
|
#include "rhdisp.h" |
| 13 |
|
#include "rhdriver.h" |
| 14 |
+ |
#include "random.h" |
| 15 |
|
|
| 16 |
|
#ifndef MAXDIST |
| 17 |
|
#define MAXDIST 42 /* maximum distance outside section */ |
| 18 |
|
#endif |
| 19 |
+ |
#ifndef NVSAMPS |
| 20 |
+ |
#define NVSAMPS 16384 /* number of ray samples per view */ |
| 21 |
+ |
#endif |
| 22 |
+ |
#ifndef MEYERNG |
| 23 |
+ |
#define MEYERNG 0.2 /* target mean eye range (rel. to grid) */ |
| 24 |
+ |
#endif |
| 25 |
+ |
#ifndef MAXTODO |
| 26 |
+ |
#define MAXTODO 3 /* maximum sections to look at */ |
| 27 |
+ |
#endif |
| 28 |
+ |
#ifndef MAXDRAT |
| 29 |
+ |
#define MAXDRAT 3.0 /* maximum distance ratio btwn. cand. sect. */ |
| 30 |
+ |
#endif |
| 31 |
|
|
| 19 |
– |
#define MAXVOXEL 32 /* maximum number of active voxels */ |
| 20 |
– |
|
| 21 |
– |
typedef struct { |
| 22 |
– |
int hd; /* holodeck section number (-1 if inactive) */ |
| 23 |
– |
int i[3]; /* voxel index (may be outside section) */ |
| 24 |
– |
} VOXL; /* a voxel */ |
| 25 |
– |
|
| 26 |
– |
static VOXL voxel[MAXVOXEL] = {{-1}}; /* current voxel list */ |
| 27 |
– |
|
| 32 |
|
#define CBEAMBLK 1024 /* cbeam allocation block size */ |
| 33 |
|
|
| 34 |
|
static struct beamcomp { |
| 35 |
|
int hd; /* holodeck section number */ |
| 36 |
< |
int4 bi; /* beam index */ |
| 36 |
> |
int bi; /* beam index */ |
| 37 |
|
int4 nr; /* number of samples desired */ |
| 38 |
|
} *cbeam = NULL; /* current beam list */ |
| 39 |
|
|
| 40 |
+ |
VIEWPOINT cureye; /* current eye position */ |
| 41 |
+ |
|
| 42 |
|
static int ncbeams = 0; /* number of sorted beams in cbeam */ |
| 43 |
|
static int xcbeams = 0; /* extra (unregistered) beams past ncbeams */ |
| 44 |
|
static int maxcbeam = 0; /* size of cbeam array */ |
| 45 |
|
|
| 40 |
– |
struct cellact { |
| 41 |
– |
short vi; /* voxel index */ |
| 42 |
– |
short rev; /* reverse ray direction? */ |
| 43 |
– |
VIEW *vp; /* view for image */ |
| 44 |
– |
short hr, vr; /* image resolution */ |
| 45 |
– |
}; /* action for cell */ |
| 46 |
|
|
| 47 |
– |
struct beamact { |
| 48 |
– |
struct cellact ca; /* cell action */ |
| 49 |
– |
GCOORD gc; /* grid coordinate */ |
| 50 |
– |
}; /* beam origin and action */ |
| 51 |
– |
|
| 52 |
– |
|
| 47 |
|
int |
| 48 |
|
newcbeam() /* allocate new entry at end of cbeam array */ |
| 49 |
|
{ |
| 178 |
|
} |
| 179 |
|
|
| 180 |
|
|
| 181 |
< |
unsigned int4 |
| 182 |
< |
add_voxels(vp) /* add voxels corresponding to view point */ |
| 183 |
< |
FVECT vp; |
| 181 |
> |
static int |
| 182 |
> |
comptodo(tdl, vw) /* compute holodeck sections in view */ |
| 183 |
> |
int tdl[MAXTODO+1]; |
| 184 |
> |
VIEW *vw; |
| 185 |
|
{ |
| 186 |
< |
int first, n, rfl = 0; |
| 187 |
< |
FVECT gp; |
| 188 |
< |
double d; |
| 189 |
< |
int dist, bestd = 0x7fff; |
| 190 |
< |
VOXL vox; |
| 191 |
< |
register int i, j, k; |
| 192 |
< |
/* count voxels in list already */ |
| 193 |
< |
for (first = 0; first < MAXVOXEL && voxel[first].hd >= 0; first++) |
| 194 |
< |
; |
| 195 |
< |
/* find closest voxels */ |
| 196 |
< |
for (n = first, i = 0; n < MAXVOXEL && hdlist[i]; i++) { |
| 197 |
< |
hdgrid(gp, hdlist[i], vp); |
| 198 |
< |
for (j = 0; n < MAXVOXEL && j < 8; j++) { |
| 199 |
< |
dist = 0; |
| 200 |
< |
for (k = 0; k < 3; k++) { |
| 201 |
< |
d = gp[k] - .5 + (j>>k & 1); |
| 202 |
< |
if (d < 0.) |
| 203 |
< |
dist += -(vox.i[k] = (int)d - 1); |
| 209 |
< |
else if ((vox.i[k] = d) >= hdlist[i]->grid[k]) |
| 210 |
< |
dist += vox.i[k] - |
| 211 |
< |
hdlist[i]->grid[k] + 1; |
| 212 |
< |
} |
| 213 |
< |
if (dist > bestd) /* others were closer */ |
| 214 |
< |
continue; |
| 215 |
< |
if (dist < bestd) { /* start closer list */ |
| 216 |
< |
n = first; |
| 217 |
< |
bestd = dist; |
| 218 |
< |
rfl = 0; |
| 219 |
< |
} |
| 220 |
< |
/* check if already in list */ |
| 221 |
< |
for (k = first; k--; ) |
| 222 |
< |
if (voxel[k].hd == i && |
| 223 |
< |
voxel[k].i[0] == vox.i[0] && |
| 224 |
< |
voxel[k].i[1] == vox.i[1] && |
| 225 |
< |
voxel[k].i[2] == vox.i[2]) |
| 226 |
< |
break; |
| 227 |
< |
if (k >= 0) { |
| 228 |
< |
rfl |= 1<<k; |
| 229 |
< |
continue; |
| 230 |
< |
} |
| 231 |
< |
vox.hd = i; /* add this voxel */ |
| 232 |
< |
copystruct(&voxel[n], &vox); |
| 233 |
< |
rfl |= 1<<n++; |
| 186 |
> |
int n = 0; |
| 187 |
> |
FVECT gp, dv; |
| 188 |
> |
double dist2[MAXTODO+1], thisdist2; |
| 189 |
> |
register int i, j; |
| 190 |
> |
/* find closest MAXTODO sections */ |
| 191 |
> |
for (i = 0; hdlist[i]; i++) { |
| 192 |
> |
hdgrid(gp, hdlist[i], vw->vp); |
| 193 |
> |
for (j = 0; j < 3; j++) |
| 194 |
> |
if (gp[j] < 0.) |
| 195 |
> |
dv[j] = -gp[j]; |
| 196 |
> |
else if (gp[j] > hdlist[i]->grid[j]) |
| 197 |
> |
dv[j] = gp[j] - hdlist[i]->grid[j]; |
| 198 |
> |
else |
| 199 |
> |
dv[j] = 0.; |
| 200 |
> |
thisdist2 = DOT(dv,dv); |
| 201 |
> |
for (j = n; j > 0 && thisdist2 < dist2[j-1]; j--) { |
| 202 |
> |
tdl[j] = tdl[j-1]; |
| 203 |
> |
dist2[j] = dist2[j-1]; |
| 204 |
|
} |
| 205 |
+ |
tdl[j] = i; |
| 206 |
+ |
dist2[j] = thisdist2; |
| 207 |
+ |
if (n < MAXTODO) |
| 208 |
+ |
n++; |
| 209 |
|
} |
| 210 |
< |
/* check for really stupid move */ |
| 211 |
< |
if (bestd > MAXDIST) { |
| 210 |
> |
/* watch for bad move */ |
| 211 |
> |
if (n && dist2[0] > MAXDIST*MAXDIST) { |
| 212 |
|
error(COMMAND, "move past outer limits"); |
| 213 |
|
return(0); |
| 214 |
|
} |
| 215 |
< |
if (n < MAXVOXEL) |
| 216 |
< |
voxel[n].hd = -1; |
| 217 |
< |
return(rfl); |
| 215 |
> |
/* avoid big differences */ |
| 216 |
> |
for (j = 1; j < n; j++) |
| 217 |
> |
if (dist2[j] > MAXDRAT*MAXDRAT*dist2[j-1]) |
| 218 |
> |
n = j; |
| 219 |
> |
tdl[n] = -1; |
| 220 |
> |
return(n); |
| 221 |
|
} |
| 222 |
|
|
| 223 |
|
|
| 224 |
|
int |
| 225 |
< |
dobeam(gcp, bp) /* express interest in a beam */ |
| 226 |
< |
GCOORD *gcp; |
| 227 |
< |
register struct beamact *bp; |
| 225 |
> |
addview(hd, vw, rad, hres, vres) /* add view for section */ |
| 226 |
> |
int hd; |
| 227 |
> |
VIEW *vw; |
| 228 |
> |
double rad; |
| 229 |
> |
int hres, vres; |
| 230 |
|
{ |
| 231 |
+ |
int sampquant, samptot = 0; |
| 232 |
+ |
int h, v, shr, svr; |
| 233 |
|
GCOORD gc[2]; |
| 234 |
< |
int bi, i, n; |
| 235 |
< |
/* compute beam index */ |
| 236 |
< |
if (bp->ca.rev) { |
| 237 |
< |
copystruct(gc, &bp->gc); |
| 238 |
< |
copystruct(gc+1, gcp); |
| 234 |
> |
FVECT rorg, rdir; |
| 235 |
> |
/* compute sampling grid */ |
| 236 |
> |
if (hres|vres && hres*vres <= NVSAMPS) { |
| 237 |
> |
shr = hres; svr = vres; |
| 238 |
> |
sampquant = 1; |
| 239 |
|
} else { |
| 240 |
< |
copystruct(gc, gcp); |
| 241 |
< |
copystruct(gc+1, &bp->gc); |
| 240 |
> |
shr = sqrt(NVSAMPS/viewaspect(vw)) + .5; |
| 241 |
> |
svr = (NVSAMPS + shr/2)/shr; |
| 242 |
> |
sampquant = (hres*vres + shr*svr/2)/(shr*svr); |
| 243 |
|
} |
| 244 |
< |
if ((bi = hdbindex(hdlist[voxel[bp->ca.vi].hd], gc)) <= 0) |
| 245 |
< |
error(CONSISTENCY, "bad grid coordinate in dobeam"); |
| 246 |
< |
/* add it in */ |
| 247 |
< |
i = getcbeam(voxel[bp->ca.vi].hd, bi); |
| 248 |
< |
n = npixels(bp->ca.vp, bp->ca.hr, bp->ca.vr, |
| 249 |
< |
hdlist[cbeam[i].hd], cbeam[i].bi); |
| 250 |
< |
if (n > cbeam[i].nr) |
| 251 |
< |
cbeam[i].nr = n; |
| 252 |
< |
return(i == ncbeams+xcbeams-1); /* return 1 if totally new */ |
| 244 |
> |
/* intersect sample rays with section */ |
| 245 |
> |
for (v = svr; v--; ) |
| 246 |
> |
for (h = shr; h--; ) { |
| 247 |
> |
if (viewray(rorg, rdir, vw, (v+frandom())/svr, |
| 248 |
> |
(h+frandom())/shr) < -FTINY) |
| 249 |
> |
continue; |
| 250 |
> |
if (rad > FTINY) { |
| 251 |
> |
rorg[0] += (1.-2.*frandom())*rad; |
| 252 |
> |
rorg[1] += (1.-2.*frandom())*rad; |
| 253 |
> |
rorg[2] += (1.-2.*frandom())*rad; |
| 254 |
> |
} |
| 255 |
> |
if (hdinter(gc, NULL, NULL, hdlist[hd], rorg, rdir) |
| 256 |
> |
>= 0.99*FHUGE) |
| 257 |
> |
continue; |
| 258 |
> |
cbeam[getcbeam(hd,hdbindex(hdlist[hd],gc))].nr += |
| 259 |
> |
sampquant; |
| 260 |
> |
samptot += sampquant; |
| 261 |
> |
} |
| 262 |
> |
return(samptot); |
| 263 |
|
} |
| 264 |
|
|
| 265 |
|
|
| 266 |
< |
|
| 267 |
< |
int |
| 276 |
< |
docell(gcp, cap) /* find beams corresponding to cell and voxel */ |
| 277 |
< |
GCOORD *gcp; |
| 278 |
< |
register struct cellact *cap; |
| 266 |
> |
beam_init(fresh) /* clear beam list for new view(s) */ |
| 267 |
> |
int fresh; |
| 268 |
|
{ |
| 280 |
– |
register HOLO *hp = hdlist[voxel[cap->vi].hd]; |
| 281 |
– |
FVECT org, dir[4]; |
| 282 |
– |
FVECT vgp, cgp, vc; |
| 283 |
– |
FVECT v1, v2; |
| 284 |
– |
struct beamact bo; |
| 285 |
– |
int axmax, j; |
| 286 |
– |
double d, avmax; |
| 269 |
|
register int i; |
| 288 |
– |
/* compute cell center */ |
| 289 |
– |
cgp[gcp->w>>1] = gcp->w&1 ? hp->grid[gcp->w>>1] : 0 ; |
| 290 |
– |
cgp[hdwg0[gcp->w]] = gcp->i[0] + .5; |
| 291 |
– |
cgp[hdwg1[gcp->w]] = gcp->i[1] + .5; |
| 292 |
– |
hdworld(org, hp, cgp); |
| 293 |
– |
/* compute direction to voxel center */ |
| 294 |
– |
for (i = 3; i--; ) |
| 295 |
– |
vgp[i] = voxel[cap->vi].i[i] + .5; |
| 296 |
– |
hdworld(vc, hp, vgp); |
| 297 |
– |
for (i = 3; i--; ) |
| 298 |
– |
vc[i] -= org[i]; |
| 299 |
– |
/* compute maximum area axis */ |
| 300 |
– |
axmax = -1; avmax = 0; |
| 301 |
– |
for (i = 3; i--; ) { |
| 302 |
– |
d = vgp[i] - cgp[i]; |
| 303 |
– |
if (d < 0.) d = -d; |
| 304 |
– |
if (d > avmax) { |
| 305 |
– |
avmax = d; |
| 306 |
– |
axmax = i; |
| 307 |
– |
} |
| 308 |
– |
} |
| 309 |
– |
#ifdef DEBUG |
| 310 |
– |
if (axmax < 0.) |
| 311 |
– |
error(CONSISTENCY, "botched axis computation in docell"); |
| 312 |
– |
#endif |
| 313 |
– |
/* compute offset vectors */ |
| 314 |
– |
d = 0.5/hp->grid[j=(axmax+1)%3]; |
| 315 |
– |
for (i = 3; i--; ) |
| 316 |
– |
v1[i] = hp->xv[j][i] * d; |
| 317 |
– |
d = 0.5/hp->grid[j=(axmax+2)%3]; |
| 318 |
– |
if (DOT(hp->wg[axmax], vc) < 0.) |
| 319 |
– |
d = -d; /* reverse vertex order */ |
| 320 |
– |
for (i = 3; i--; ) |
| 321 |
– |
v2[i] = hp->xv[j][i] * d; |
| 322 |
– |
/* compute voxel pyramid */ |
| 323 |
– |
for (i = 3; i--; ) { |
| 324 |
– |
dir[0][i] = vc[i] - v1[i] - v2[i]; |
| 325 |
– |
dir[1][i] = vc[i] + v1[i] - v2[i]; |
| 326 |
– |
dir[2][i] = vc[i] + v1[i] + v2[i]; |
| 327 |
– |
dir[3][i] = vc[i] - v1[i] + v2[i]; |
| 328 |
– |
} |
| 329 |
– |
/* visit beams for opposite cells */ |
| 330 |
– |
copystruct(&bo.ca, cap); |
| 331 |
– |
copystruct(&bo.gc, gcp); |
| 332 |
– |
return(visit_cells(org, dir, hp, dobeam, &bo)); |
| 333 |
– |
} |
| 270 |
|
|
| 271 |
< |
|
| 272 |
< |
int |
| 273 |
< |
doview(cap) /* visit cells for a given view */ |
| 274 |
< |
struct cellact *cap; |
| 275 |
< |
{ |
| 276 |
< |
int orient; |
| 341 |
< |
FVECT org, dir[4]; |
| 342 |
< |
/* compute view pyramid */ |
| 343 |
< |
orient = viewpyramid(org, dir, hdlist[voxel[cap->vi].hd], cap->vp); |
| 344 |
< |
if (!orient) |
| 345 |
< |
error(INTERNAL, "unusable view in doview"); |
| 346 |
< |
cap->rev = orient < 0; |
| 347 |
< |
/* visit cells within pyramid */ |
| 348 |
< |
return(visit_cells(org, dir, hdlist[voxel[cap->vi].hd], docell, cap)); |
| 271 |
> |
if (fresh) /* discard old beams? */ |
| 272 |
> |
ncbeams = xcbeams = 0; |
| 273 |
> |
else /* else clear sample requests */ |
| 274 |
> |
for (i = ncbeams+xcbeams; i--; ) |
| 275 |
> |
cbeam[i].nr = 0; |
| 276 |
> |
cureye.rng = 0.; |
| 277 |
|
} |
| 278 |
|
|
| 279 |
|
|
| 280 |
< |
beam_init() /* clear beam list for new view(s) */ |
| 353 |
< |
{ |
| 354 |
< |
register int i; |
| 355 |
< |
/* clear desire flags */ |
| 356 |
< |
for (i = ncbeams+xcbeams; i--; ) |
| 357 |
< |
cbeam[i].nr = 0; |
| 358 |
< |
voxel[0].hd = -1; /* clear voxel list */ |
| 359 |
< |
} |
| 360 |
< |
|
| 361 |
< |
|
| 280 |
> |
int * |
| 281 |
|
beam_view(vn, hr, vr) /* add beam view (if advisable) */ |
| 282 |
|
VIEW *vn; |
| 283 |
|
int hr, vr; |
| 284 |
|
{ |
| 285 |
< |
struct cellact ca; |
| 286 |
< |
unsigned int4 vfl; |
| 287 |
< |
/* sort our list */ |
| 285 |
> |
static int todo[MAXTODO+1]; |
| 286 |
> |
int n; |
| 287 |
> |
double er, eravg, d; |
| 288 |
> |
register HOLO *hp; |
| 289 |
> |
register int i; |
| 290 |
> |
/* find nearby sections */ |
| 291 |
> |
if (!(n = comptodo(todo, vn))) |
| 292 |
> |
return(NULL); |
| 293 |
> |
/* sort current beam list */ |
| 294 |
|
cbeamsort(1); |
| 295 |
< |
/* add new voxels */ |
| 296 |
< |
vfl = add_voxels(vn->vp); |
| 297 |
< |
if (!vfl) |
| 298 |
< |
return(0); |
| 299 |
< |
ca.vp = vn; ca.hr = hr; ca.vr = vr; |
| 300 |
< |
/* update our beam list */ |
| 301 |
< |
for (ca.vi = 0; vfl; vfl >>= 1, ca.vi++) |
| 302 |
< |
if (vfl & 1) |
| 303 |
< |
doview(&ca); |
| 304 |
< |
return(1); |
| 295 |
> |
/* add view to nearby sections */ |
| 296 |
> |
eravg = 0.; |
| 297 |
> |
for (i = 0; i < n; i++) { |
| 298 |
> |
hp = hdlist[todo[i]]; |
| 299 |
> |
if (MEYERNG > FTINY) |
| 300 |
> |
er = MEYERNG/3. / VLEN(hp->wg[0]) + |
| 301 |
> |
MEYERNG/3. / VLEN(hp->wg[1]) + |
| 302 |
> |
MEYERNG/3. / VLEN(hp->wg[2]) ; |
| 303 |
> |
else |
| 304 |
> |
er = 0.; |
| 305 |
> |
if (!addview(todo[i], vn, 0.25*er, hr, vr)) { |
| 306 |
> |
register int j; /* whoops! */ |
| 307 |
> |
n--; /* delete from list */ |
| 308 |
> |
for (j = i--; j <= n; j++) |
| 309 |
> |
todo[j] = todo[j+1]; |
| 310 |
> |
} else |
| 311 |
> |
eravg += er; |
| 312 |
> |
} |
| 313 |
> |
if (eravg <= FTINY) |
| 314 |
> |
return(todo); |
| 315 |
> |
/* compute average eye range */ |
| 316 |
> |
eravg /= (double)n; |
| 317 |
> |
/* add to current eye position */ |
| 318 |
> |
if (cureye.rng <= FTINY) { |
| 319 |
> |
VCOPY(cureye.vpt, vn->vp); |
| 320 |
> |
cureye.rng = eravg; |
| 321 |
> |
} else if ((d = sqrt(dist2(vn->vp,cureye.vpt))) + eravg > cureye.rng) { |
| 322 |
> |
for (i = 3; i--; ) |
| 323 |
> |
cureye.vpt[i] = 0.5*(cureye.vpt[i] + vn->vp[i]); |
| 324 |
> |
cureye.rng = 0.5*(cureye.rng + eravg + d); |
| 325 |
> |
} |
| 326 |
> |
return(todo); |
| 327 |
|
} |
| 328 |
|
|
| 329 |
|
|
| 331 |
|
beam_sync(all) /* update beam list on server */ |
| 332 |
|
int all; |
| 333 |
|
{ |
| 334 |
< |
/* sort list to put orphans at end */ |
| 335 |
< |
cbeamsort(0); |
| 334 |
> |
/* set new eye position */ |
| 335 |
> |
serv_request(DR_VIEWPOINT, sizeof(VIEWPOINT), (char *)&cureye); |
| 336 |
> |
/* sort list (put orphans at end) */ |
| 337 |
> |
cbeamsort(all < 0); |
| 338 |
> |
/* send beam request */ |
| 339 |
|
if (all) |
| 340 |
|
cbeamop(DR_NEWSET, cbeam, ncbeams); |
| 341 |
|
else |