| 1 | gwlarson | 3.12 | /* Copyright (c) 1998 Silicon Graphics, Inc. */ | 
| 2 | gregl | 3.1 |  | 
| 3 |  |  | #ifndef lint | 
| 4 |  |  | static char SCCSid[] = "$SunId$ SGI"; | 
| 5 |  |  | #endif | 
| 6 |  |  |  | 
| 7 |  |  | /* | 
| 8 |  |  | * Rtrace support routines for holodeck rendering | 
| 9 |  |  | */ | 
| 10 |  |  |  | 
| 11 |  |  | #include "rholo.h" | 
| 12 | gregl | 3.5 | #include "paths.h" | 
| 13 | gregl | 3.1 | #include "random.h" | 
| 14 |  |  |  | 
| 15 |  |  |  | 
| 16 | gwlarson | 3.16 | VIEWPOINT       myeye;          /* target view position */ | 
| 17 |  |  |  | 
| 18 |  |  |  | 
| 19 | gregl | 3.1 | packrays(rod, p)                /* pack ray origins and directions */ | 
| 20 | gwlarson | 3.15 | register float  *rod; | 
| 21 | gregl | 3.1 | register PACKET *p; | 
| 22 |  |  | { | 
| 23 | gwlarson | 3.17 | float   packdc2[RPACKSIZ]; | 
| 24 | gwlarson | 3.18 | int     iterleft = 3*p->nr + 9; | 
| 25 | gwlarson | 3.16 | BYTE    rpos[2][2]; | 
| 26 |  |  | FVECT   ro, rd, rp1; | 
| 27 | gregl | 3.3 | GCOORD  gc[2]; | 
| 28 | gwlarson | 3.18 | double  d, dc2, md2, td2, dc2worst = FHUGE; | 
| 29 | gwlarson | 3.16 | int     i; | 
| 30 |  |  | register int    ii; | 
| 31 | gregl | 3.1 |  | 
| 32 |  |  | if (!hdbcoord(gc, hdlist[p->hd], p->bi)) | 
| 33 |  |  | error(CONSISTENCY, "bad beam index in packrays"); | 
| 34 | gwlarson | 3.18 | td2 = myeye.rng + FTINY; td2 *= td2; | 
| 35 |  |  | for (i = 0, md2 = 0.; i < p->nr || (md2 > td2 && iterleft--); ) { | 
| 36 | gwlarson | 3.16 | rpos[0][0] = frandom() * 256.; | 
| 37 |  |  | rpos[0][1] = frandom() * 256.; | 
| 38 |  |  | rpos[1][0] = frandom() * 256.; | 
| 39 |  |  | rpos[1][1] = frandom() * 256.; | 
| 40 |  |  | d = hdray(ro, rd, hdlist[p->hd], gc, rpos); | 
| 41 |  |  | if (myeye.rng > FTINY) {                /* check eyepoint */ | 
| 42 |  |  | register int    nexti; | 
| 43 |  |  |  | 
| 44 |  |  | VSUM(rp1, ro, rd, d); | 
| 45 | gwlarson | 3.18 | dc2 = dist2line(myeye.vpt, ro, rp1) / p->nr; | 
| 46 | gwlarson | 3.16 | if (i == p->nr) {               /* packet full */ | 
| 47 | gwlarson | 3.18 | if (dc2 >= dc2worst)    /* quick check */ | 
| 48 |  |  | continue; | 
| 49 |  |  | nexti = 0;              /* find worst */ | 
| 50 |  |  | for (ii = i; --ii; ) | 
| 51 |  |  | if (packdc2[ii] > packdc2[nexti]) | 
| 52 |  |  | nexti = ii; | 
| 53 |  |  | if (dc2 >= (dc2worst = packdc2[nexti])) | 
| 54 | gwlarson | 3.16 | continue;       /* worse than worst */ | 
| 55 | gwlarson | 3.18 | md2 -= dc2worst; | 
| 56 | gwlarson | 3.16 | } else | 
| 57 |  |  | nexti = i++; | 
| 58 | gwlarson | 3.17 | md2 += packdc2[nexti] = dc2;    /* new distance */ | 
| 59 | gwlarson | 3.16 | ii = nexti;                     /* put it here */ | 
| 60 |  |  | } else | 
| 61 |  |  | ii = i++; | 
| 62 | gregl | 3.1 | if (p->offset != NULL) { | 
| 63 | gwlarson | 3.16 | if (!vdef(OBSTRUCTIONS)) | 
| 64 |  |  | d *= frandom();         /* random offset */ | 
| 65 | gregl | 3.8 | VSUM(ro, ro, rd, d);            /* advance ray */ | 
| 66 | gwlarson | 3.16 | p->offset[ii] = d; | 
| 67 | gregl | 3.1 | } | 
| 68 | gwlarson | 3.16 | p->ra[ii].r[0][0] = rpos[0][0]; | 
| 69 |  |  | p->ra[ii].r[0][1] = rpos[0][1]; | 
| 70 |  |  | p->ra[ii].r[1][0] = rpos[1][0]; | 
| 71 |  |  | p->ra[ii].r[1][1] = rpos[1][1]; | 
| 72 |  |  | VCOPY(rod+6*ii, ro); | 
| 73 |  |  | VCOPY(rod+6*ii+3, rd); | 
| 74 | gregl | 3.1 | } | 
| 75 | gwlarson | 3.16 | #ifdef DEBUG | 
| 76 | gwlarson | 3.17 | fprintf(stderr, "%f mean distance for target %f (%d iterations left)\n", | 
| 77 |  |  | sqrt(md2), myeye.rng, iterleft); | 
| 78 | gwlarson | 3.16 | #endif | 
| 79 | gregl | 3.1 | } | 
| 80 |  |  |  | 
| 81 |  |  |  | 
| 82 |  |  | donerays(p, rvl)                /* encode finished ray computations */ | 
| 83 |  |  | register PACKET *p; | 
| 84 |  |  | register float  *rvl; | 
| 85 |  |  | { | 
| 86 |  |  | double  d; | 
| 87 |  |  | register int    i; | 
| 88 |  |  |  | 
| 89 |  |  | for (i = 0; i < p->nr; i++) { | 
| 90 |  |  | setcolr(p->ra[i].v, rvl[0], rvl[1], rvl[2]); | 
| 91 |  |  | d = rvl[3]; | 
| 92 |  |  | if (p->offset != NULL) | 
| 93 |  |  | d += p->offset[i]; | 
| 94 |  |  | p->ra[i].d = hdcode(hdlist[p->hd], d); | 
| 95 |  |  | rvl += 4; | 
| 96 |  |  | } | 
| 97 | gregl | 3.4 | p->nc += p->nr; | 
| 98 | gregl | 3.5 | } | 
| 99 |  |  |  | 
| 100 |  |  |  | 
| 101 |  |  | int | 
| 102 |  |  | done_rtrace()                   /* clean up and close rtrace calculation */ | 
| 103 |  |  | { | 
| 104 |  |  | int     status; | 
| 105 |  |  | /* already closed? */ | 
| 106 |  |  | if (!nprocs) | 
| 107 |  |  | return; | 
| 108 |  |  | /* flush beam queue */ | 
| 109 |  |  | done_packets(flush_queue()); | 
| 110 | gregl | 3.7 | /* sync holodeck */ | 
| 111 |  |  | hdsync(NULL, 1); | 
| 112 | gregl | 3.5 | /* close rtrace */ | 
| 113 |  |  | if ((status = end_rtrace())) | 
| 114 |  |  | error(WARNING, "bad exit status from rtrace"); | 
| 115 | gregl | 3.9 | if (vdef(REPORT)) {             /* report time */ | 
| 116 | gregl | 3.10 | eputs("rtrace process closed\n"); | 
| 117 | gregl | 3.5 | report(0); | 
| 118 | gregl | 3.9 | } | 
| 119 | gregl | 3.5 | return(status);                 /* return status */ | 
| 120 |  |  | } | 
| 121 |  |  |  | 
| 122 |  |  |  | 
| 123 |  |  | new_rtrace()                    /* restart rtrace calculation */ | 
| 124 |  |  | { | 
| 125 |  |  | char    combuf[128]; | 
| 126 |  |  |  | 
| 127 |  |  | if (nprocs > 0)                 /* already running? */ | 
| 128 |  |  | return; | 
| 129 | gregl | 3.6 | starttime = time(NULL);         /* reset start time and counts */ | 
| 130 |  |  | npacksdone = nraysdone = 0L; | 
| 131 |  |  | if (vdef(TIME))                 /* reset end time */ | 
| 132 |  |  | endtime = starttime + vflt(TIME)*3600. + .5; | 
| 133 | gregl | 3.5 | if (vdef(RIF)) {                /* rerun rad to update octree */ | 
| 134 |  |  | sprintf(combuf, "rad -v 0 -s -w %s", vval(RIF)); | 
| 135 |  |  | if (system(combuf)) | 
| 136 |  |  | error(WARNING, "error running rad"); | 
| 137 |  |  | } | 
| 138 |  |  | if (start_rtrace() < 1)         /* start rtrace */ | 
| 139 |  |  | error(WARNING, "cannot restart rtrace"); | 
| 140 | gregl | 3.9 | else if (vdef(REPORT)) { | 
| 141 |  |  | eputs("rtrace process restarted\n"); | 
| 142 | gregl | 3.5 | report(0); | 
| 143 | gregl | 3.9 | } | 
| 144 | gregl | 3.5 | } | 
| 145 |  |  |  | 
| 146 |  |  |  | 
| 147 |  |  | getradfile()                    /* run rad and get needed variables */ | 
| 148 |  |  | { | 
| 149 | gwlarson | 3.12 | static short    mvar[] = {OCTREE,EYESEP,-1}; | 
| 150 | gregl | 3.5 | static char     tf1[] = TEMPLATE; | 
| 151 |  |  | char    tf2[64]; | 
| 152 |  |  | char    combuf[256]; | 
| 153 |  |  | char    *pippt; | 
| 154 |  |  | register int    i; | 
| 155 |  |  | register char   *cp; | 
| 156 |  |  | /* check if rad file specified */ | 
| 157 |  |  | if (!vdef(RIF)) | 
| 158 | gregl | 3.11 | return(0); | 
| 159 | gregl | 3.5 | /* create rad command */ | 
| 160 |  |  | mktemp(tf1); | 
| 161 |  |  | sprintf(tf2, "%s.rif", tf1); | 
| 162 |  |  | sprintf(combuf, | 
| 163 |  |  | "rad -v 0 -s -e -w %s OPTFILE=%s | egrep '^[ \t]*(NOMATCH", | 
| 164 |  |  | vval(RIF), tf1); | 
| 165 |  |  | cp = combuf; | 
| 166 |  |  | while (*cp){ | 
| 167 |  |  | if (*cp == '|') pippt = cp; | 
| 168 |  |  | cp++; | 
| 169 |  |  | }                               /* match unset variables */ | 
| 170 |  |  | for (i = 0; mvar[i] >= 0; i++) | 
| 171 |  |  | if (!vdef(mvar[i])) { | 
| 172 |  |  | *cp++ = '|'; | 
| 173 |  |  | strcpy(cp, vnam(mvar[i])); | 
| 174 |  |  | while (*cp) cp++; | 
| 175 |  |  | pippt = NULL; | 
| 176 |  |  | } | 
| 177 |  |  | if (pippt != NULL) | 
| 178 |  |  | strcpy(pippt, "> /dev/null");   /* nothing to match */ | 
| 179 |  |  | else | 
| 180 |  |  | sprintf(cp, ")[ \t]*=' > %s", tf2); | 
| 181 | gwlarson | 3.13 | #ifdef DEBUG | 
| 182 |  |  | wputs(combuf); wputs("\n"); | 
| 183 |  |  | #endif | 
| 184 |  |  | system(combuf);                         /* ignore exit code */ | 
| 185 | gregl | 3.5 | if (pippt == NULL) { | 
| 186 |  |  | loadvars(tf2);                  /* load variables */ | 
| 187 |  |  | unlink(tf2); | 
| 188 |  |  | } | 
| 189 |  |  | rtargc += wordfile(rtargv+rtargc, tf1); /* get rtrace options */ | 
| 190 |  |  | unlink(tf1);                    /* clean up */ | 
| 191 | gregl | 3.11 | return(1); | 
| 192 | gregl | 3.6 | } | 
| 193 |  |  |  | 
| 194 |  |  |  | 
| 195 |  |  | report(t)                       /* report progress so far */ | 
| 196 |  |  | time_t  t; | 
| 197 |  |  | { | 
| 198 |  |  | static time_t   seconds2go = 1000000; | 
| 199 |  |  |  | 
| 200 |  |  | if (t == 0L) | 
| 201 |  |  | t = time(NULL); | 
| 202 |  |  | sprintf(errmsg, "%ld packets (%ld rays) done after %.2f hours\n", | 
| 203 |  |  | npacksdone, nraysdone, (t-starttime)/3600.); | 
| 204 |  |  | eputs(errmsg); | 
| 205 |  |  | if (seconds2go == 1000000) | 
| 206 |  |  | seconds2go = vdef(REPORT) ? (long)(vflt(REPORT)*60. + .5) : 0L; | 
| 207 |  |  | if (seconds2go) | 
| 208 |  |  | reporttime = t + seconds2go; | 
| 209 | gregl | 3.1 | } |