--- ray/src/hd/rhdisp2.c 1997/11/25 10:48:38 3.7 +++ ray/src/hd/rhdisp2.c 2003/07/27 22:12:02 3.36 @@ -1,9 +1,6 @@ -/* Copyright (c) 1997 Silicon Graphics, Inc. */ - #ifndef lint -static char SCCSid[] = "$SunId$ SGI"; +static const char RCSid[] = "$Id: rhdisp2.c,v 3.36 2003/07/27 22:12:02 schorsch Exp $"; #endif - /* * Holodeck beam tracking for display process */ @@ -12,44 +9,20 @@ static char SCCSid[] = "$SunId$ SGI"; #include "rhdisp.h" #include "rhdriver.h" -extern GCOORD *getviewcells(); +#ifndef MEYERNG +#define MEYERNG 0.2 /* target mean eye range (rel. to grid) */ +#endif -static VIEW dvw; /* current view corresponding to beam list */ - -typedef struct { - int hd; /* holodeck section number (-1 if inactive) */ - int i[3]; /* voxel index (may be outside section) */ -} VOXL; /* a voxel */ - -static VOXL voxel[8] = { /* current voxel list */ - {-1}, {-1}, {-1}, {-1}, - {-1}, {-1}, {-1}, {-1} -}; - #define CBEAMBLK 1024 /* cbeam allocation block size */ -static struct beamcomp { - short wants; /* flags telling which voxels want us */ - short hd; /* holodeck section number */ - int bi; /* beam index */ -} *cbeam = NULL; /* current beam list */ - +static PACKHEAD *cbeam = NULL; /* current beam list */ static int ncbeams = 0; /* number of sorted beams in cbeam */ static int xcbeams = 0; /* extra (unregistered) beams past ncbeams */ static int maxcbeam = 0; /* size of cbeam array */ -struct cellact { - short vi; /* voxel index */ - short add; /* zero means delete */ - short rev; /* reverse ray direction? */ -}; /* action for cell */ +VIEWPOINT cureye; /* current eye position */ -struct beamact { - struct cellact ca; /* cell action */ - GCOORD gc; /* grid coordinate */ -}; /* beam origin and action */ - int newcbeam() /* allocate new entry at end of cbeam array */ { @@ -58,11 +31,11 @@ newcbeam() /* allocate new entry at end of cbeam arra if ((i = ncbeams + xcbeams++) >= maxcbeam) { /* grow array */ maxcbeam += CBEAMBLK; if (cbeam == NULL) - cbeam = (struct beamcomp *)malloc( - maxcbeam*sizeof(struct beamcomp) ); + cbeam = (PACKHEAD *)malloc( + maxcbeam*sizeof(PACKHEAD) ); else - cbeam = (struct beamcomp *)realloc( (char *)cbeam, - maxcbeam*sizeof(struct beamcomp) ); + cbeam = (PACKHEAD *)realloc((void *)cbeam, + maxcbeam*sizeof(PACKHEAD) ); if (cbeam == NULL) error(SYSTEM, "out of memory in newcbeam"); } @@ -72,7 +45,7 @@ newcbeam() /* allocate new entry at end of cbeam arra int cbeamcmp(cb1, cb2) /* compare two cbeam entries for sort: keep orphans */ -register struct beamcomp *cb1, *cb2; +register PACKHEAD *cb1, *cb2; { register int c; @@ -84,12 +57,14 @@ register struct beamcomp *cb1, *cb2; int cbeamcmp2(cb1, cb2) /* compare two cbeam entries for sort: no orphans */ -register struct beamcomp *cb1, *cb2; +register PACKHEAD *cb1, *cb2; { register int c; - if (!cb1->wants) /* put orphans at the end, unsorted */ - return(cb2->wants); + if (!cb1->nr) /* put orphans at the end, unsorted */ + return(cb2->nr); + if (!cb2->nr) + return(-1); if ((c = cb1->bi - cb2->bi)) /* sort on beam index first */ return(c); return(cb1->hd - cb2->hd); /* use hd to resolve matches */ @@ -100,14 +75,14 @@ int findcbeam(hd, bi) /* find the specified beam in our sorted list */ int hd, bi; { - struct beamcomp cb; - register struct beamcomp *p; + PACKHEAD cb; + register PACKHEAD *p; if (ncbeams <= 0) return(-1); - cb.wants = 0; cb.hd = hd; cb.bi = bi; - p = (struct beamcomp *)bsearch((char *)&cb, (char *)cbeam, ncbeams, - sizeof(struct beamcomp), cbeamcmp); + cb.hd = hd; cb.bi = bi; cb.nr = cb.nc = 0; + p = (PACKHEAD *)bsearch((char *)&cb, (char *)cbeam, ncbeams, + sizeof(PACKHEAD), cbeamcmp); if (p == NULL) return(-1); return(p - cbeam); @@ -128,12 +103,12 @@ int bi; if (cbeam[n].bi == bi && cbeam[n].hd == hd) return(n); /* check legality */ - if (hd < 0 | hd >= HDMAX || hdlist[hd] == NULL) + if ((hd < 0) | (hd >= HDMAX) || hdlist[hd] == NULL) error(INTERNAL, "illegal holodeck number in getcbeam"); - if (bi < 1 | bi > nbeams(hdlist[hd])) + if ((bi < 1) | (bi > nbeams(hdlist[hd]))) error(INTERNAL, "illegal beam index in getcbeam"); n = newcbeam(); /* allocate and assign */ - cbeam[n].wants = 0; cbeam[n].hd = hd; cbeam[n].bi = bi; + cbeam[n].hd = hd; cbeam[n].bi = bi; cbeam[n].nr = cbeam[n].nc = 0; return(n); } @@ -146,298 +121,150 @@ int adopt; if (!(ncbeams += xcbeams)) return; xcbeams = 0; - qsort((char *)cbeam, ncbeams, sizeof(struct beamcomp), + qsort((char *)cbeam, ncbeams, sizeof(PACKHEAD), adopt ? cbeamcmp : cbeamcmp2); if (adopt) return; for (i = ncbeams; i--; ) /* identify orphans */ - if (cbeam[i].wants) + if (cbeam[i].nr) break; xcbeams = ncbeams - ++i; /* put orphans after ncbeams */ ncbeams = i; } -cbeamop(op, bl, n, v, hr, vr) /* update beams on server list */ -int op; -register struct beamcomp *bl; -int n; -VIEW *v; -int hr, vr; +beam_init(fresh) /* clear beam list for new view(s) */ +int fresh; { - register PACKHEAD *pa; register int i; - if (n <= 0) - return; - pa = (PACKHEAD *)malloc(n*sizeof(PACKHEAD)); - if (pa == NULL) - error(SYSTEM, "out of memory in cbeamadd"); - for (i = 0; i < n; i++) { - pa[i].hd = bl[i].hd; - pa[i].bi = bl[i].bi; - pa[i].nr = v==NULL ? 0 : - npixels(v, hr, vr, hdlist[bl[i].hd], bl[i].bi); - } - serv_request(op, n*sizeof(PACKHEAD), (char *)pa); - free((char *)pa); + if (fresh) /* discard old beams? */ + ncbeams = xcbeams = 0; + else /* else clear sample requests */ + for (i = ncbeams+xcbeams; i--; ) + cbeam[i].nr = 0; + cureye.rng = 0.; } -int -set_voxels(vl, n) /* set new voxel array */ -VOXL vl[8]; -int n; +int16 * +beam_view(vn, hr, vr) /* add beam view (if advisable) */ +VIEW *vn; +int hr, vr; { - short wmap[256]; - int vmap[8]; - int comn = 0; - int no; - register int i, j; - /* find common voxels */ - for (j = 0; j < 8 && voxel[j].hd >= 0; j++) { - vmap[j] = -1; - for (i = n; i--; ) - if (!bcmp((char *)(vl+i), (char *)(voxel+j), - sizeof(VOXL))) { - vmap[j] = i; - comn |= 1<= 0 && i & 1< cbeam[n].nr) + cbeam[n].nr = blist.bl[i].nr; } - /* fix cbeam flags */ - for (i = ncbeams; i--; ) - cbeam[i].wants = wmap[cbeam[i].wants]; - /* update our voxel list */ - bcopy((char *)vl, (char *)voxel, n*sizeof(VOXL)); - for (j = n; j < 8; j++) - voxel[j].hd = -1; - return(comn); /* return bit array of common voxels */ -} - - -int -get_voxels(vl, vp) /* find voxels corresponding to view point */ -VOXL vl[8]; -FVECT vp; -{ - static int lastn = 0, lastd = -1; - int n = 0; - FVECT gp; - double d; - int dist, bestd = 0x7fff; - VOXL vox; - register int i, j, k; - /* find closest voxels */ - for (i = 0; n < 8 && hdlist[i]; i++) { - hdgrid(gp, hdlist[i], vp); - for (j = 0; n < 8 && j < 8; j++) { - dist = 0; - for (k = 0; k < 3; k++) { - d = gp[k] - .5 + (j>>k & 1); - if (d < 0.) - dist += -(vox.i[k] = (int)d - 1); - else if ((vox.i[k] = d) >= hdlist[i]->grid[k]) - dist += vox.i[k] - - hdlist[i]->grid[k] + 1; - } - if (dist > bestd) /* others were closer */ - continue; - if (dist < bestd) { /* start closer list */ - n = 0; - bestd = dist; - } - vox.hd = i; /* add this voxel */ - copystruct(&vl[n], &vox); - n++; - } + free((void *)blist.bl); /* free list */ + if (MEYERNG <= FTINY) + return(slist); + /* compute average eye range */ + eravg = 0.; + for (n = 0; slist[n] >= 0; n++) { + hp = hdlist[slist[n]]; + eravg += MEYERNG/3. / VLEN(hp->wg[0]) + + MEYERNG/3. / VLEN(hp->wg[1]) + + MEYERNG/3. / VLEN(hp->wg[2]) ; } - /* warn of dangerous moves */ - if (n < lastn && bestd >= lastd) - error(WARNING, "moving outside holodeck section"); - else if (n > lastn && bestd <= lastd) - error(WARNING, "moving inside holodeck section"); - lastd = bestd; - return(lastn = n); + eravg /= (double)n; + /* add to current eye position */ + if (cureye.rng <= FTINY) { + VCOPY(cureye.vpt, vn->vp); + cureye.rng = eravg; + } else if ((d = sqrt(dist2(vn->vp,cureye.vpt))) + eravg > cureye.rng) { + for (i = 3; i--; ) + cureye.vpt[i] = 0.5*(cureye.vpt[i] + vn->vp[i]); + cureye.rng = 0.5*(cureye.rng + eravg + d); + } + return(slist); } int -dobeam(gcp, bp) /* express interest or disintrest in a beam */ -GCOORD *gcp; -register struct beamact *bp; +beam_sync(all) /* update beam list on server */ +int all; { - GCOORD gc[2]; - int bi, i; - /* compute beam index */ - if (bp->ca.rev) { - copystruct(gc, &bp->gc); - copystruct(gc+1, gcp); + /* set new eye position */ + serv_request(DR_VIEWPOINT, sizeof(VIEWPOINT), (char *)&cureye); + /* sort list (put orphans at end) */ + cbeamsort(all < 0); + /* send beam request */ + if (all) { + if (ncbeams > 0) + serv_request(DR_NEWSET, + ncbeams*sizeof(PACKHEAD), cbeam); } else { - copystruct(gc, gcp); - copystruct(gc+1, &bp->gc); + if (ncbeams+xcbeams > 0) + serv_request(DR_ADJSET, + (ncbeams+xcbeams)*sizeof(PACKHEAD), cbeam); } - if ((bi = hdbindex(hdlist[voxel[bp->ca.vi].hd], gc)) <= 0) - error(CONSISTENCY, "bad grid coordinate in dobeam"); - if (bp->ca.add) { /* add it in */ - i = getcbeam(voxel[bp->ca.vi].hd, bi); - cbeam[i].wants |= 1<ca.vi; /* say we want it */ - return(i == ncbeams+xcbeams-1); /* return 1 if totally new */ - } - /* else delete it */ - i = findcbeam(voxel[bp->ca.vi].hd, bi); - if (i >= 0 && cbeam[i].wants & 1<ca.vi) { - cbeam[i].wants &= ~(1<ca.vi); /* we don't want it */ - return(1); /* indicate change */ - } - return(0); + xcbeams = 0; /* truncate our list */ + return(ncbeams); } - -int -docell(gcp, cap) /* find beams corresponding to cell and voxel */ -GCOORD *gcp; -register struct cellact *cap; +gridlines(f) /* run through holodeck section grid lines */ +int (*f)(); { - FVECT org, dir[4]; - FVECT gp, cv[4], vc; - struct beamact bo; - register int i; - /* compute cell vertices */ - hdcell(cv, hdlist[voxel[cap->vi].hd], gcp); - /* compute cell and voxel centers */ - for (i = 0; i < 3; i++) { - org[i] = 0.5*(cv[0][i] + cv[2][i]); - gp[i] = voxel[cap->vi].i[i] + 0.5; - } - hdworld(vc, hdlist[voxel[cap->vi].hd], gp); - /* compute voxel pyramid using vector trick */ - for (i = 0; i < 3; i++) { - dir[0][i] = vc[i] - cv[0][i]; /* to 3 */ - dir[2][i] = vc[i] - cv[3][i]; /* to 0 */ - if (gcp->w & 1) { /* watch vertex order! */ - dir[1][i] = vc[i] - cv[2][i]; /* to 1 */ - dir[3][i] = vc[i] - cv[1][i]; /* to 2 */ - } else { - dir[1][i] = vc[i] - cv[1][i]; /* to 2 */ - dir[3][i] = vc[i] - cv[2][i]; /* to 1 */ - } - } - /* visit beams for opposite cells */ - copystruct(&bo.ca, cap); - copystruct(&bo.gc, gcp); - return(visit_cells(org, dir, hdlist[voxel[cap->vi].hd], dobeam, &bo)); -} - - -int -doview(cap, vp) /* visit cells for a given view */ -struct cellact *cap; -VIEW *vp; -{ - int orient; - FVECT org, dir[4]; - /* compute view pyramid */ - orient = viewpyramid(org, dir, hdlist[voxel[cap->vi].hd], vp); - if (!orient) - error(INTERNAL, "unusable view in doview"); - cap->rev = orient < 0; - /* visit cells within pyramid */ - return(visit_cells(org, dir, hdlist[voxel[cap->vi].hd], docell, cap)); -} - - -mvview(voxi, vold, vnew) /* move view for a voxel */ -int voxi; -VIEW *vold, *vnew; -{ - int netchange = 0; - struct cellact oca, nca; - int ocnt, ncnt; - int c; - GCOORD *ogcl, *ngcl; - register GCOORD *ogcp, *ngcp; - /* get old and new cell lists */ - ogcp = ogcl = getviewcells(&ocnt, hdlist[voxel[voxi].hd], vold); - ngcp = ngcl = getviewcells(&ncnt, hdlist[voxel[voxi].hd], vnew); - /* set up actions */ - oca.vi = nca.vi = voxi; - oca.rev = nca.rev = 0; - oca.add = 0; nca.add = 1; - if ((oca.rev = ocnt < 0)) - ocnt = -ocnt; - if ((nca.rev = ncnt < 0)) - ncnt = -ncnt; - if (nca.rev == oca.rev) /* add and delete cells in order */ - while (ocnt > 0 & ncnt > 0) - if ((c = cellcmp(ogcp, ngcp)) > 0) { /* new cell */ - netchange += docell(ngcp++, &nca); - ncnt--; - } else if (c < 0) { /* old cell */ - netchange -= docell(ogcp++, &oca); - ocnt--; - } else { /* same cell */ - ogcp++; ocnt--; - ngcp++; ncnt--; + register int hd, w, i; + int g0, g1; + FVECT wp[2], mov; + double d; + /* do each wall on each section */ + for (hd = 0; hdlist[hd] != NULL; hd++) + for (w = 0; w < 6; w++) { + g0 = hdwg0[w]; + g1 = hdwg1[w]; + d = 1.0/hdlist[hd]->grid[g0]; + mov[0] = d * hdlist[hd]->xv[g0][0]; + mov[1] = d * hdlist[hd]->xv[g0][1]; + mov[2] = d * hdlist[hd]->xv[g0][2]; + if (w & 1) { + VSUM(wp[0], hdlist[hd]->orig, + hdlist[hd]->xv[w>>1], 1.); + VSUM(wp[0], wp[0], mov, 1.); + } else + VCOPY(wp[0], hdlist[hd]->orig); + VSUM(wp[1], wp[0], hdlist[hd]->xv[g1], 1.); + for (i = hdlist[hd]->grid[g0]; ; ) { /* g0 lines */ + (*f)(wp); + if (!--i) break; + wp[0][0] += mov[0]; wp[0][1] += mov[1]; + wp[0][2] += mov[2]; wp[1][0] += mov[0]; + wp[1][1] += mov[1]; wp[1][2] += mov[2]; } - /* take care of list tails */ - for ( ; ncnt > 0; ncnt--) - netchange += docell(ngcp++, &nca); - for ( ; ocnt > 0; ocnt--) - netchange -= docell(ogcp++, &oca); - /* clean up */ - if (ogcl != NULL) free((char *)ogcl); - if (ngcl != NULL) free((char *)ngcl); - return(netchange); -} - - -beam_sync() /* synchronize beams on server */ -{ - cbeamop(DR_NEWSET, cbeam, ncbeams, &odev.v, odev.hres, odev.vres); -} - - -beam_view(vn) /* change beam view */ -VIEW *vn; -{ - struct cellact ca; - VOXL vlnew[8]; - int n, comn; - - if (!vn->type) { /* clear our beam list */ - set_voxels(vlnew, 0); - cbeamop(DR_DELSET, cbeam, ncbeams, NULL, 0, 0); - ncbeams = 0; - copystruct(&dvw, vn); - return; - } - /* find our new voxels */ - n = get_voxels(vlnew, vn->vp); - /* set the new voxels */ - comn = set_voxels(vlnew, n); - if (!dvw.type) - comn = 0; - ca.add = 1; /* update our beam list */ - for (ca.vi = n; ca.vi--; ) - if (comn & 1<grid[g1]; + mov[0] = d * hdlist[hd]->xv[g1][0]; + mov[1] = d * hdlist[hd]->xv[g1][1]; + mov[2] = d * hdlist[hd]->xv[g1][2]; + if (w & 1) + VSUM(wp[0], hdlist[hd]->orig, + hdlist[hd]->xv[w>>1], 1.); + else + VSUM(wp[0], hdlist[hd]->orig, mov, 1.); + VSUM(wp[1], wp[0], hdlist[hd]->xv[g0], 1.); + for (i = hdlist[hd]->grid[g1]; ; ) { /* g1 lines */ + (*f)(wp); + if (!--i) break; + wp[0][0] += mov[0]; wp[0][1] += mov[1]; + wp[0][2] += mov[2]; wp[1][0] += mov[0]; + wp[1][1] += mov[1]; wp[1][2] += mov[2]; + } + } }