| 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 4096 /* number of ray samples per view */ |
| 21 |
+ |
#endif |
| 22 |
|
|
| 23 |
< |
#define MAXVOXEL 32 /* maximum number of active voxels */ |
| 23 |
> |
#define MAXTODO 3 /* maximum sections to look at */ |
| 24 |
> |
#define MAXDRAT 3.0 /* maximum distance ratio */ |
| 25 |
|
|
| 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 |
– |
|
| 26 |
|
#define CBEAMBLK 1024 /* cbeam allocation block size */ |
| 27 |
|
|
| 28 |
|
static struct beamcomp { |
| 35 |
|
static int xcbeams = 0; /* extra (unregistered) beams past ncbeams */ |
| 36 |
|
static int maxcbeam = 0; /* size of cbeam array */ |
| 37 |
|
|
| 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 */ |
| 38 |
|
|
| 47 |
– |
struct beamact { |
| 48 |
– |
struct cellact ca; /* cell action */ |
| 49 |
– |
GCOORD gc; /* grid coordinate */ |
| 50 |
– |
}; /* beam origin and action */ |
| 51 |
– |
|
| 52 |
– |
|
| 39 |
|
int |
| 40 |
|
newcbeam() /* allocate new entry at end of cbeam array */ |
| 41 |
|
{ |
| 170 |
|
} |
| 171 |
|
|
| 172 |
|
|
| 173 |
< |
unsigned int4 |
| 174 |
< |
add_voxels(vp) /* add voxels corresponding to view point */ |
| 175 |
< |
FVECT vp; |
| 173 |
> |
int |
| 174 |
> |
comptodo(tdl, vw) /* compute holodeck sections in view */ |
| 175 |
> |
int tdl[MAXTODO+1]; |
| 176 |
> |
VIEW *vw; |
| 177 |
|
{ |
| 178 |
< |
int first, n, rfl = 0; |
| 179 |
< |
FVECT gp; |
| 180 |
< |
double d; |
| 181 |
< |
int dist, bestd = 0x7fff; |
| 182 |
< |
VOXL vox; |
| 183 |
< |
register int i, j, k; |
| 184 |
< |
/* count voxels in list already */ |
| 185 |
< |
for (first = 0; first < MAXVOXEL && voxel[first].hd >= 0; first++) |
| 186 |
< |
; |
| 187 |
< |
/* find closest voxels */ |
| 188 |
< |
for (n = first, i = 0; n < MAXVOXEL && hdlist[i]; i++) { |
| 189 |
< |
hdgrid(gp, hdlist[i], vp); |
| 190 |
< |
for (j = 0; n < MAXVOXEL && j < 8; j++) { |
| 191 |
< |
dist = 0; |
| 192 |
< |
for (k = 0; k < 3; k++) { |
| 193 |
< |
d = gp[k] - .5 + (j>>k & 1); |
| 194 |
< |
if (d < 0.) |
| 195 |
< |
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++; |
| 178 |
> |
int n = 0; |
| 179 |
> |
FVECT gp, dv; |
| 180 |
> |
double dist2[MAXTODO+1], thisdist2; |
| 181 |
> |
register int i, j; |
| 182 |
> |
/* find closest MAXTODO sections */ |
| 183 |
> |
for (i = 0; hdlist[i]; i++) { |
| 184 |
> |
hdgrid(gp, hdlist[i], vw->vp); |
| 185 |
> |
for (j = 0; j < 3; j++) |
| 186 |
> |
if (gp[j] < 0.) |
| 187 |
> |
dv[j] = -gp[j]; |
| 188 |
> |
else if (gp[j] > hdlist[i]->grid[j]) |
| 189 |
> |
dv[j] = gp[j] - hdlist[i]->grid[j]; |
| 190 |
> |
else |
| 191 |
> |
dv[j] = 0.; |
| 192 |
> |
thisdist2 = DOT(dv,dv); |
| 193 |
> |
for (j = n; j > 0 && thisdist2 < dist2[j-1]; j--) { |
| 194 |
> |
tdl[j] = tdl[j-1]; |
| 195 |
> |
dist2[j] = dist2[j-1]; |
| 196 |
|
} |
| 197 |
+ |
tdl[j] = i; |
| 198 |
+ |
dist2[j] = thisdist2; |
| 199 |
+ |
if (n < MAXTODO) |
| 200 |
+ |
n++; |
| 201 |
|
} |
| 202 |
< |
/* check for really stupid move */ |
| 203 |
< |
if (bestd > MAXDIST) { |
| 202 |
> |
/* watch for bad move */ |
| 203 |
> |
if (n && dist2[0] > MAXDIST*MAXDIST) { |
| 204 |
|
error(COMMAND, "move past outer limits"); |
| 205 |
|
return(0); |
| 206 |
|
} |
| 207 |
< |
if (n < MAXVOXEL) |
| 208 |
< |
voxel[n].hd = -1; |
| 209 |
< |
return(rfl); |
| 207 |
> |
/* avoid big differences */ |
| 208 |
> |
for (j = 1; j < n; j++) |
| 209 |
> |
if (dist2[j] > MAXDRAT*MAXDRAT*dist2[j-1]) |
| 210 |
> |
n = j; |
| 211 |
> |
tdl[n] = -1; |
| 212 |
> |
return(n); |
| 213 |
|
} |
| 214 |
|
|
| 215 |
|
|
| 216 |
< |
int |
| 217 |
< |
dobeam(gcp, bp) /* express interest in a beam */ |
| 218 |
< |
GCOORD *gcp; |
| 219 |
< |
register struct beamact *bp; |
| 216 |
> |
addview(hd, vw, hres, vres) /* add view for section */ |
| 217 |
> |
int hd; |
| 218 |
> |
VIEW *vw; |
| 219 |
> |
int hres, vres; |
| 220 |
|
{ |
| 221 |
+ |
int sampquant; |
| 222 |
+ |
int h, v, shr, svr; |
| 223 |
|
GCOORD gc[2]; |
| 224 |
< |
int bi, i, n; |
| 225 |
< |
/* compute beam index */ |
| 226 |
< |
if (bp->ca.rev) { |
| 227 |
< |
copystruct(gc, &bp->gc); |
| 228 |
< |
copystruct(gc+1, gcp); |
| 224 |
> |
FVECT rorg, rdir; |
| 225 |
> |
/* compute sampling grid */ |
| 226 |
> |
if (hres|vres && hres*vres <= NVSAMPS) { |
| 227 |
> |
shr = hres; svr = vres; |
| 228 |
> |
sampquant = 1; |
| 229 |
|
} else { |
| 230 |
< |
copystruct(gc, gcp); |
| 231 |
< |
copystruct(gc+1, &bp->gc); |
| 230 |
> |
shr = sqrt(NVSAMPS/viewaspect(vw)) + .5; |
| 231 |
> |
svr = (NVSAMPS + shr/2)/shr; |
| 232 |
> |
sampquant = (hres*vres + shr*svr/2)/(shr*svr); |
| 233 |
|
} |
| 234 |
< |
if ((bi = hdbindex(hdlist[voxel[bp->ca.vi].hd], gc)) <= 0) |
| 235 |
< |
error(CONSISTENCY, "bad grid coordinate in dobeam"); |
| 236 |
< |
/* add it in */ |
| 237 |
< |
i = getcbeam(voxel[bp->ca.vi].hd, bi); |
| 238 |
< |
n = npixels(bp->ca.vp, bp->ca.hr, bp->ca.vr, |
| 239 |
< |
hdlist[cbeam[i].hd], cbeam[i].bi); |
| 240 |
< |
if (n > cbeam[i].nr) |
| 241 |
< |
cbeam[i].nr = n; |
| 242 |
< |
return(i == ncbeams+xcbeams-1); /* return 1 if totally new */ |
| 243 |
< |
} |
| 244 |
< |
|
| 273 |
< |
|
| 274 |
< |
|
| 275 |
< |
int |
| 276 |
< |
docell(gcp, cap) /* find beams corresponding to cell and voxel */ |
| 277 |
< |
GCOORD *gcp; |
| 278 |
< |
register struct cellact *cap; |
| 279 |
< |
{ |
| 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; |
| 287 |
< |
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; |
| 234 |
> |
/* intersect sample rays with section */ |
| 235 |
> |
for (v = svr; v--; ) |
| 236 |
> |
for (h = shr; h--; ) { |
| 237 |
> |
if (viewray(rorg, rdir, vw, (v+frandom())/svr, |
| 238 |
> |
(h+frandom())/shr) < -FTINY) |
| 239 |
> |
continue; |
| 240 |
> |
if (hdinter(gc, NULL, NULL, hdlist[hd], rorg, rdir) |
| 241 |
> |
>= FHUGE) |
| 242 |
> |
continue; |
| 243 |
> |
cbeam[getcbeam(hd,hdbindex(hdlist[hd],gc))].nr += |
| 244 |
> |
sampquant; |
| 245 |
|
} |
| 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)); |
| 246 |
|
} |
| 247 |
|
|
| 248 |
|
|
| 336 |
– |
int |
| 337 |
– |
doview(cap) /* visit cells for a given view */ |
| 338 |
– |
struct cellact *cap; |
| 339 |
– |
{ |
| 340 |
– |
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)); |
| 349 |
– |
} |
| 350 |
– |
|
| 351 |
– |
|
| 249 |
|
beam_init(fresh) /* clear beam list for new view(s) */ |
| 250 |
|
int fresh; |
| 251 |
|
{ |
| 256 |
|
else /* else clear sample requests */ |
| 257 |
|
for (i = ncbeams+xcbeams; i--; ) |
| 258 |
|
cbeam[i].nr = 0; |
| 362 |
– |
voxel[0].hd = -1; /* clear voxel list */ |
| 259 |
|
} |
| 260 |
|
|
| 261 |
|
|
| 263 |
|
VIEW *vn; |
| 264 |
|
int hr, vr; |
| 265 |
|
{ |
| 266 |
< |
struct cellact ca; |
| 267 |
< |
unsigned int4 vfl; |
| 266 |
> |
int todo[MAXTODO+1]; |
| 267 |
> |
int n; |
| 268 |
|
/* sort our list */ |
| 269 |
|
cbeamsort(1); |
| 270 |
< |
/* add new voxels */ |
| 271 |
< |
vfl = add_voxels(vn->vp); |
| 376 |
< |
if (!vfl) |
| 270 |
> |
/* add view to nearby sections */ |
| 271 |
> |
if (!(n = comptodo(todo, vn))) |
| 272 |
|
return(0); |
| 273 |
< |
ca.vp = vn; ca.hr = hr; ca.vr = vr; |
| 274 |
< |
/* update our beam list */ |
| 380 |
< |
for (ca.vi = 0; vfl; vfl >>= 1, ca.vi++) |
| 381 |
< |
if (vfl & 1) |
| 382 |
< |
doview(&ca); |
| 273 |
> |
while (n--) |
| 274 |
> |
addview(todo[n], vn, hr, vr); |
| 275 |
|
return(1); |
| 276 |
|
} |
| 277 |
|
|