| 1 | < | /* Copyright (c) 1997 Silicon Graphics, Inc. */ | 
| 1 | > | /* Copyright (c) 1998 Silicon Graphics, Inc. */ | 
| 2 |  |  | 
| 3 |  | #ifndef lint | 
| 4 |  | static char SCCSid[] = "$SunId$ SGI"; | 
| 5 |  | #endif | 
| 6 |  |  | 
| 7 |  | /* | 
| 8 | < | * Holodeck beam tracking | 
| 8 | > | * Holodeck beam tracking for display process | 
| 9 |  | */ | 
| 10 |  |  | 
| 11 |  | #include "rholo.h" | 
| 12 |  | #include "rhdisp.h" | 
| 13 |  | #include "rhdriver.h" | 
| 14 | + | #include "random.h" | 
| 15 |  |  | 
| 16 | < | extern int      *getviewcells(); | 
| 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 |  |  | 
| 17 | – | typedef struct { | 
| 18 | – | int     hd;             /* holodeck section number */ | 
| 19 | – | int     i[3];           /* voxel index (may be outside section) */ | 
| 20 | – | } VOXL;                 /* a voxel */ | 
| 21 | – |  | 
| 22 | – | static VOXL voxel[8] = {        /* current voxel list */ | 
| 23 | – | {-1}, {-1}, {-1}, {-1}, | 
| 24 | – | {-1}, {-1}, {-1}, {-1} | 
| 25 | – | }; | 
| 26 | – |  | 
| 32 |  | #define CBEAMBLK        1024    /* cbeam allocation block size */ | 
| 33 |  |  | 
| 34 |  | static struct beamcomp { | 
| 35 | < | short   wants;          /* flags telling which voxels want us */ | 
| 31 | < | short   hd;             /* holodeck section number */ | 
| 35 | > | int     hd;             /* holodeck section number */ | 
| 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 |  |  | 
| 46 |  |  | 
| 40 | – | struct cellact { | 
| 41 | – | short   vi;             /* voxel index */ | 
| 42 | – | short   add;            /* zero means delete */ | 
| 43 | – | };              /* action for cell */ | 
| 44 | – |  | 
| 45 | – |  | 
| 46 | – | struct beamact { | 
| 47 | – | struct cellact  ca;     /* cell action */ | 
| 48 | – | GCOORD  gc;             /* grid coordinate */ | 
| 49 | – | };              /* beam origin and action */ | 
| 50 | – |  | 
| 51 | – |  | 
| 47 |  | int | 
| 48 |  | newcbeam()              /* allocate new entry at end of cbeam array */ | 
| 49 |  | { | 
| 82 |  | { | 
| 83 |  | register int    c; | 
| 84 |  |  | 
| 85 | < | if (!cb1->wants)                /* put orphans at the end, unsorted */ | 
| 86 | < | return(cb2->wants); | 
| 85 | > | if (!cb1->nr)                   /* put orphans at the end, unsorted */ | 
| 86 | > | return(cb2->nr); | 
| 87 | > | if (!cb2->nr) | 
| 88 | > | return(-1); | 
| 89 |  | if ((c = cb1->bi - cb2->bi))    /* sort on beam index first */ | 
| 90 |  | return(c); | 
| 91 |  | return(cb1->hd - cb2->hd);      /* use hd to resolve matches */ | 
| 101 |  |  | 
| 102 |  | if (ncbeams <= 0) | 
| 103 |  | return(-1); | 
| 104 | < | cb.wants = 0; cb.hd = hd; cb.bi = bi; | 
| 104 | > | cb.hd = hd; cb.bi = bi; cb.nr = 0; | 
| 105 |  | p = (struct beamcomp *)bsearch((char *)&cb, (char *)cbeam, ncbeams, | 
| 106 |  | sizeof(struct beamcomp), cbeamcmp); | 
| 107 |  | if (p == NULL) | 
| 129 |  | if (bi < 1 | bi > nbeams(hdlist[hd])) | 
| 130 |  | error(INTERNAL, "illegal beam index in getcbeam"); | 
| 131 |  | n = newcbeam();         /* allocate and assign */ | 
| 132 | < | cbeam[n].wants = 0; cbeam[n].hd = hd; cbeam[n].bi = bi; | 
| 132 | > | cbeam[n].nr = 0; cbeam[n].hd = hd; cbeam[n].bi = bi; | 
| 133 |  | return(n); | 
| 134 |  | } | 
| 135 |  |  | 
| 147 |  | if (adopt) | 
| 148 |  | return; | 
| 149 |  | for (i = ncbeams; i--; )        /* identify orphans */ | 
| 150 | < | if (cbeam[i].wants) | 
| 150 | > | if (cbeam[i].nr) | 
| 151 |  | break; | 
| 152 |  | xcbeams = ncbeams - ++i;        /* put orphans after ncbeams */ | 
| 153 |  | ncbeams = i; | 
| 154 |  | } | 
| 155 |  |  | 
| 156 |  |  | 
| 157 | < | cbeamadd(bl, n, v, hr, vr)      /* add beams to server list */ | 
| 157 | > | cbeamop(op, bl, n)              /* update beams on server list */ | 
| 158 | > | int     op; | 
| 159 |  | register struct beamcomp        *bl; | 
| 160 |  | int     n; | 
| 163 | – | VIEW    *v; | 
| 164 | – | int     hr, vr; | 
| 161 |  | { | 
| 162 |  | register PACKHEAD       *pa; | 
| 163 |  | register int    i; | 
| 166 |  | return; | 
| 167 |  | pa = (PACKHEAD *)malloc(n*sizeof(PACKHEAD)); | 
| 168 |  | if (pa == NULL) | 
| 169 | < | error(SYSTEM, "out of memory in cbeamadd"); | 
| 174 | < | for (i = 0; i < n; i++) | 
| 175 | < | pa[i].nr = npixels(v, hr, vr, | 
| 176 | < | hdlist[pa[i].hd=bl[i].hd], | 
| 177 | < | pa[i].bi=bl[i].bi) / 8; | 
| 178 | < |  | 
| 179 | < | serv_request(DR_ADDSET, n*sizeof(PACKHEAD), (char *)pa); | 
| 180 | < | free((char *)pa); | 
| 181 | < | } | 
| 182 | < |  | 
| 183 | < |  | 
| 184 | < | cbeamdel(bl, n)         /* delete unwanted beam requests */ | 
| 185 | < | register struct beamcomp        *bl; | 
| 186 | < | int     n; | 
| 187 | < | { | 
| 188 | < | register PACKHEAD       *pa; | 
| 189 | < | register int    i; | 
| 190 | < |  | 
| 191 | < | if (n <= 0) | 
| 192 | < | return; | 
| 193 | < | pa = (PACKHEAD *)malloc(n*sizeof(PACKHEAD)); | 
| 194 | < | if (pa == NULL) | 
| 195 | < | error(SYSTEM, "out of memory in cbeamdel"); | 
| 169 | > | error(SYSTEM, "out of memory in cbeamop"); | 
| 170 |  | for (i = 0; i < n; i++) { | 
| 171 |  | pa[i].hd = bl[i].hd; | 
| 172 |  | pa[i].bi = bl[i].bi; | 
| 173 | < | pa[i].nr = 0;           /* removes any request */ | 
| 173 | > | pa[i].nr = bl[i].nr; | 
| 174 | > | pa[i].nc = 0; | 
| 175 |  | } | 
| 176 | < | serv_request(DR_DELSET, n*sizeof(PACKHEAD), (char *)pa); | 
| 176 | > | serv_request(op, n*sizeof(PACKHEAD), (char *)pa); | 
| 177 |  | free((char *)pa); | 
| 178 |  | } | 
| 179 |  |  | 
| 180 |  |  | 
| 181 | < | int | 
| 182 | < | set_voxels(vl, n)       /* set new voxel array */ | 
| 183 | < | VOXL    vl[8]; | 
| 184 | < | int     n; | 
| 181 | > | static int | 
| 182 | > | comptodo(tdl, vw)               /* compute holodeck sections in view */ | 
| 183 | > | int     tdl[MAXTODO+1]; | 
| 184 | > | VIEW    *vw; | 
| 185 |  | { | 
| 186 | < | short   wmap[256]; | 
| 187 | < | int     vmap[8]; | 
| 188 | < | int     comn = 0; | 
| 214 | < | int     no; | 
| 186 | > | int     n = 0; | 
| 187 | > | FVECT   gp, dv; | 
| 188 | > | double  dist2[MAXTODO+1], thisdist2; | 
| 189 |  | register int    i, j; | 
| 190 | < | /* find common voxels */ | 
| 191 | < | for (j = 0; j < 8 && voxel[j].hd >= 0; j++) { | 
| 192 | < | vmap[j] = -1; | 
| 193 | < | for (i = n; i--; ) | 
| 194 | < | if (!bcmp((char *)(vl+i), (char *)(voxel+j), | 
| 195 | < | sizeof(VOXL))) { | 
| 196 | < | vmap[j] = i; | 
| 197 | < | comn |= 1<<i; | 
| 198 | < | break; | 
| 199 | < | } | 
| 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 | < | no = comn ? j : 0;              /* compute flag mapping */ | 
| 211 | < | for (i = 256; i--; ) { | 
| 212 | < | wmap[i] = 0; | 
| 213 | < | for (j = no; j--; ) | 
| 231 | < | if (vmap[j] >= 0 && i & 1<<j) | 
| 232 | < | wmap[i] |= 1<<vmap[j]; | 
| 210 | > | /* watch for bad move */ | 
| 211 | > | if (n && dist2[0] > MAXDIST*MAXDIST) { | 
| 212 | > | error(COMMAND, "move past outer limits"); | 
| 213 | > | return(0); | 
| 214 |  | } | 
| 215 | < | /* fix cbeam flags */ | 
| 216 | < | for (i = ncbeams; i--; ) | 
| 217 | < | cbeam[i].wants = wmap[cbeam[i].wants]; | 
| 218 | < | /* update our voxel list */ | 
| 219 | < | bcopy((char *)vl, (char *)voxel, n*sizeof(VOXL)); | 
| 220 | < | for (j = n; j < 8; j++) | 
| 240 | < | voxel[j].hd = -1; | 
| 241 | < | return(comn);                   /* return bit array of common voxels */ | 
| 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 | < | get_voxels(vl, vp)      /* find voxels corresponding to view point */ | 
| 226 | < | VOXL    vl[8]; | 
| 227 | < | FVECT   vp; | 
| 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     n = 0; | 
| 232 | < | FVECT   gp; | 
| 233 | < | double  d; | 
| 234 | < | int     dist, bestd = 0x7fff; | 
| 235 | < | VOXL    vox; | 
| 236 | < | register int    i, j, k; | 
| 237 | < | /* find closest voxels */ | 
| 238 | < | for (i = 0; n < 8 && hdlist[i]; i++) { | 
| 239 | < | hdgrid(gp, hdlist[i], vp); | 
| 240 | < | for (j = 0; n < 8 && j < 8; j++) { | 
| 241 | < | dist = 0; | 
| 242 | < | for (k = 0; k < 3; k++) { | 
| 243 | < | d = gp[k] - .5 + (j>>k & 1); | 
| 244 | < | if (d < 0.) | 
| 245 | < | dist += -(vox.i[k] = (int)d - 1); | 
| 246 | < | else if ((vox.i[k] = d) >= hdlist[i]->grid[k]) | 
| 247 | < | dist += vox.i[k] - | 
| 248 | < | hdlist[i]->grid[k] + 1; | 
| 268 | < | } | 
| 269 | < | if (dist > bestd)       /* others were closer */ | 
| 231 | > | int     sampquant, samptot = 0; | 
| 232 | > | int     h, v, shr, svr; | 
| 233 | > | GCOORD  gc[2]; | 
| 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 | > | shr = sqrt(NVSAMPS/viewaspect(vw)) + .5; | 
| 241 | > | svr = (NVSAMPS + shr/2)/shr; | 
| 242 | > | sampquant = (hres*vres + shr*svr/2)/(shr*svr); | 
| 243 | > | } | 
| 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 (dist < bestd) {     /* start closer list */ | 
| 251 | < | n = 0; | 
| 252 | < | bestd = dist; | 
| 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 | < | vox.hd = i;             /* add this voxel */ | 
| 256 | < | copystruct(&vl[n], &vox); | 
| 257 | < | n++; | 
| 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 | < | } | 
| 280 | < | return(n); | 
| 262 | > | return(samptot); | 
| 263 |  | } | 
| 264 |  |  | 
| 265 |  |  | 
| 266 | < | int | 
| 267 | < | dobeam(gcp, bp)         /* express interest or disintrest in a beam */ | 
| 286 | < | GCOORD  *gcp; | 
| 287 | < | register struct beamact *bp; | 
| 266 | > | beam_init(fresh)                /* clear beam list for new view(s) */ | 
| 267 | > | int     fresh; | 
| 268 |  | { | 
| 269 | < | GCOORD  gc[2]; | 
| 270 | < | int     bi, i; | 
| 271 | < | /* compute beam index */ | 
| 272 | < | copystruct(gc, gcp); | 
| 273 | < | copystruct(gc+1, &bp->gc); | 
| 274 | < | if ((bi = hdbindex(hdlist[voxel[bp->ca.vi].hd], gc)) <= 0) | 
| 275 | < | return(0);              /* should report an error? */ | 
| 276 | < | if (bp->ca.add) {               /* add it in */ | 
| 297 | < | i = getcbeam(voxel[bp->ca.vi].hd, bi); | 
| 298 | < | cbeam[i].wants |= 1<<bp->ca.vi; /* say we want it */ | 
| 299 | < | return(i == ncbeams+xcbeams-1); /* return 1 if totally new */ | 
| 300 | < | } | 
| 301 | < | /* else delete it */ | 
| 302 | < | i = findcbeam(voxel[bp->ca.vi].hd, bi); | 
| 303 | < | if (i >= 0 && cbeam[i].wants & 1<<bp->ca.vi) { | 
| 304 | < | cbeam[i].wants &= ~(1<<bp->ca.vi);      /* we don't want it */ | 
| 305 | < | return(1);                      /* indicate change */ | 
| 306 | < | } | 
| 307 | < | return(0); | 
| 269 | > | register int    i; | 
| 270 | > |  | 
| 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 | < |  | 
| 281 | < | int | 
| 282 | < | docell(gcp, cap)        /* find beams corresponding to cell and voxel */ | 
| 283 | < | GCOORD  *gcp; | 
| 315 | < | register struct cellact *cap; | 
| 280 | > | int * | 
| 281 | > | beam_view(vn, hr, vr)           /* add beam view (if advisable) */ | 
| 282 | > | VIEW    *vn; | 
| 283 | > | int     hr, vr; | 
| 284 |  | { | 
| 285 | < | FVECT   org, dir[4]; | 
| 286 | < | FVECT   gp, cv[4], vc; | 
| 287 | < | struct beamact  bo; | 
| 285 | > | static int      todo[MAXTODO+1]; | 
| 286 | > | int     n; | 
| 287 | > | double  er, eravg, d; | 
| 288 | > | register HOLO   *hp; | 
| 289 |  | register int    i; | 
| 290 | < | /* compute cell vertices */ | 
| 291 | < | hdcell(cv, hdlist[voxel[cap->vi].hd], gcp); | 
| 292 | < | /* compute cell and voxel centers */ | 
| 293 | < | for (i = 0; i < 3; i++) { | 
| 294 | < | org[i] = 0.5*(cv[0][i] + cv[2][i]); | 
| 295 | < | gp[i] = voxel[cap->vi].i[i] + 0.5; | 
| 290 | > | /* find nearby sections */ | 
| 291 | > | if (!(n = comptodo(todo, vn))) | 
| 292 | > | return(NULL); | 
| 293 | > | /* sort current beam list */ | 
| 294 | > | cbeamsort(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 | < | hdworld(vc, hdlist[voxel[cap->vi].hd], gp); | 
| 314 | < | /* compute voxel pyramid using vector trick */ | 
| 315 | < | for (i = 0; i < 3; i++) { | 
| 316 | < | dir[0][i] = vc[i] - cv[0][i];           /* to 3 */ | 
| 317 | < | dir[2][i] = vc[i] - cv[3][i];           /* to 0 */ | 
| 318 | < | if (gcp->w & 1) {       /* watch vertex order! */ | 
| 319 | < | dir[1][i] = vc[i] - cv[2][i];   /* to 1 */ | 
| 320 | < | dir[3][i] = vc[i] - cv[1][i];   /* to 2 */ | 
| 321 | < | } else { | 
| 322 | < | dir[1][i] = vc[i] - cv[1][i];   /* to 2 */ | 
| 323 | < | dir[3][i] = vc[i] - cv[2][i];   /* to 1 */ | 
| 324 | < | } | 
| 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 | < | /* visit beams for opposite cells */ | 
| 342 | < | copystruct(&bo.ca, cap); | 
| 343 | < | copystruct(&bo.gc, gcp); | 
| 344 | < | return(visit_cells(org, dir, hdlist[voxel[cap->vi].hd], dobeam, &bo)); | 
| 326 | > | return(todo); | 
| 327 |  | } | 
| 328 |  |  | 
| 329 |  |  | 
| 330 |  | int | 
| 331 | < | doview(cap, vp)                 /* visit cells for a given view */ | 
| 332 | < | struct cellact  *cap; | 
| 351 | < | VIEW    *vp; | 
| 331 | > | beam_sync(all)                  /* update beam list on server */ | 
| 332 | > | int     all; | 
| 333 |  | { | 
| 334 | < | FVECT   org, dir[4]; | 
| 335 | < | /* compute view pyramid */ | 
| 336 | < | if (vp->type == VT_PAR) goto viewerr; | 
| 337 | < | if (viewray(org, dir[0], vp, 0., 0.) < -FTINY) goto viewerr; | 
| 338 | < | if (viewray(org, dir[1], vp, 0., 1.) < -FTINY) goto viewerr; | 
| 339 | < | if (viewray(org, dir[2], vp, 1., 1.) < -FTINY) goto viewerr; | 
| 340 | < | if (viewray(org, dir[3], vp, 1., 0.) < -FTINY) goto viewerr; | 
| 341 | < | /* visit cells within pyramid */ | 
| 342 | < | return(visit_cells(org, dir, hdlist[voxel[cap->vi].hd], docell, cap)); | 
| 362 | < | viewerr: | 
| 363 | < | error(INTERNAL, "unusable view in doview"); | 
| 364 | < | } | 
| 365 | < |  | 
| 366 | < |  | 
| 367 | < | mvview(voxi, vold, vnew)        /* move view for a voxel */ | 
| 368 | < | int     voxi; | 
| 369 | < | VIEW    *vold, *vnew; | 
| 370 | < | { | 
| 371 | < | int     netchange = 0; | 
| 372 | < | int     *ocl, *ncl; | 
| 373 | < | struct cellact  ca; | 
| 374 | < | int     ocnt, ncnt; | 
| 375 | < | int     c; | 
| 376 | < | register GCOORD *ogcp, *ngcp; | 
| 377 | < | /* get old and new cell lists */ | 
| 378 | < | ocl = getviewcells(hdlist[voxel[voxi].hd], vold); | 
| 379 | < | ncl = getviewcells(hdlist[voxel[voxi].hd], vnew); | 
| 380 | < | if (ocl != NULL) { | 
| 381 | < | ocnt = *ocl; ogcp = (GCOORD *)(ocl+1); | 
| 382 | < | } else { | 
| 383 | < | ocnt = 0; ogcp = NULL; | 
| 384 | < | } | 
| 385 | < | if (ncl != NULL) { | 
| 386 | < | ncnt = *ncl; ngcp = (GCOORD *)(ncl+1); | 
| 387 | < | } else { | 
| 388 | < | ncnt = 0; ngcp = NULL; | 
| 389 | < | } | 
| 390 | < | ca.vi = voxi;           /* add and delete cells */ | 
| 391 | < | while (ocnt > 0 & ncnt > 0) | 
| 392 | < | if ((c = cellcmp(ogcp, ngcp)) > 0) { | 
| 393 | < | ca.add = 1;             /* new cell */ | 
| 394 | < | netchange += docell(ngcp++, &ca); | 
| 395 | < | ncnt--; | 
| 396 | < | } else if (c < 0) { | 
| 397 | < | ca.add = 0;             /* obsolete cell */ | 
| 398 | < | netchange -= docell(ogcp++, &ca); | 
| 399 | < | ocnt--; | 
| 400 | < | } else { | 
| 401 | < | ogcp++; ocnt--;         /* unchanged cell */ | 
| 402 | < | ngcp++; ncnt--; | 
| 403 | < | } | 
| 404 | < | /* take care of list tails */ | 
| 405 | < | for (ca.add = 1; ncnt > 0; ncnt--) | 
| 406 | < | docell(ngcp++, &ca); | 
| 407 | < | for (ca.add = 0; ocnt > 0; ocnt--) | 
| 408 | < | docell(ogcp++, &ca); | 
| 409 | < | /* clean up */ | 
| 410 | < | if (ocl != NULL) free((char *)ocl); | 
| 411 | < | if (ncl != NULL) free((char *)ncl); | 
| 412 | < | return(netchange); | 
| 413 | < | } | 
| 414 | < |  | 
| 415 | < |  | 
| 416 | < | beam_view(vo, vn)       /* change beam view */ | 
| 417 | < | VIEW    *vo, *vn; | 
| 418 | < | { | 
| 419 | < | struct cellact  ca; | 
| 420 | < | VOXL    vlnew[8]; | 
| 421 | < | int     n, comn; | 
| 422 | < |  | 
| 423 | < | if (!vn->type) {                /* clear our beam list */ | 
| 424 | < | set_voxels(vlnew, 0); | 
| 425 | < | cbeamdel(cbeam, ncbeams); | 
| 426 | < | ncbeams = 0; | 
| 427 | < | return; | 
| 428 | < | } | 
| 429 | < | /* find our new voxels */ | 
| 430 | < | n = get_voxels(vlnew, vn->vp); | 
| 431 | < | /* set the new voxels */ | 
| 432 | < | comn = set_voxels(vlnew, n); | 
| 433 | < | if (!vo->type) | 
| 434 | < | comn = 0; | 
| 435 | < | ca.add = 1;                     /* update our beam list */ | 
| 436 | < | for (ca.vi = n; ca.vi--; ) | 
| 437 | < | if (comn & 1<<ca.vi)    /* change which cells we see */ | 
| 438 | < | mvview(ca.vi, vo, vn); | 
| 439 | < | else                    /* else add all new cells */ | 
| 440 | < | doview(&ca, vn); | 
| 441 | < | /* inform server of new beams */ | 
| 442 | < | cbeamadd(cbeam+ncbeams, xcbeams, vn, odev.hres, odev.vres); | 
| 443 | < | /* sort list to put orphans at end */ | 
| 444 | < | cbeamsort(0); | 
| 445 | < | /* tell server to delete orphans */ | 
| 446 | < | cbeamdel(cbeam+ncbeams, xcbeams); | 
| 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 | 
| 342 | > | cbeamop(DR_ADJSET, cbeam, ncbeams+xcbeams); | 
| 343 |  | xcbeams = 0;                    /* truncate our list */ | 
| 344 | + | return(ncbeams); | 
| 345 |  | } |