| 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 |  |  | packrays(rod, p)                /* pack ray origins and directions */ | 
| 17 | gwlarson | 3.14 | float   *rod; | 
| 18 | gregl | 3.1 | register PACKET *p; | 
| 19 |  |  | { | 
| 20 | gwlarson | 3.14 | static int      nmh = 0; | 
| 21 |  |  | static int      *mhtab; | 
| 22 |  |  | FVECT   ro, rd; | 
| 23 |  |  | register BEAM   *bp; | 
| 24 | gregl | 3.3 | GCOORD  gc[2]; | 
| 25 | gwlarson | 3.14 | int     ila[4], offset; | 
| 26 | gregl | 3.4 | double  d, sl[4]; | 
| 27 | gwlarson | 3.14 | register int    i, j, k; | 
| 28 | gregl | 3.1 |  | 
| 29 |  |  | if (!hdbcoord(gc, hdlist[p->hd], p->bi)) | 
| 30 |  |  | error(CONSISTENCY, "bad beam index in packrays"); | 
| 31 | gwlarson | 3.14 | /* uniqueness hash */ | 
| 32 |  |  | if ((bp = hdgetbeam(hdlist[p->hd], p->bi)) != NULL) { | 
| 33 |  |  | if (2*bp->nrm > nmh) { | 
| 34 |  |  | if (nmh) free((char *)mhtab); | 
| 35 |  |  | nmh = 2*bp->nrm + 1; | 
| 36 |  |  | mhtab = (int *)malloc(nmh*sizeof(int)); | 
| 37 |  |  | if (mhtab == NULL) | 
| 38 |  |  | error(SYSTEM, "out of memory in packrays"); | 
| 39 |  |  | } | 
| 40 |  |  | for (k = nmh; k--; ) | 
| 41 |  |  | mhtab[k] = -1; | 
| 42 |  |  | for (i = bp->nrm; i--; ) { | 
| 43 |  |  | ila[0] = hdbray(bp)[i].r[0][0]; | 
| 44 |  |  | ila[1] = hdbray(bp)[i].r[0][1]; | 
| 45 |  |  | ila[2] = hdbray(bp)[i].r[1][0]; | 
| 46 |  |  | ila[3] = hdbray(bp)[i].r[1][1]; | 
| 47 |  |  | for (k = ilhash(ila,4); mhtab[k%nmh] >= 0; k++) | 
| 48 |  |  | ; | 
| 49 |  |  | mhtab[k%nmh] = i; | 
| 50 |  |  | } | 
| 51 |  |  | } | 
| 52 |  |  | /* init each ray */ | 
| 53 | gregl | 3.4 | ila[0] = p->hd; ila[1] = p->bi; | 
| 54 | gwlarson | 3.14 | offset = ilhash(ila,2) + p->nc; | 
| 55 | gregl | 3.1 | for (i = 0; i < p->nr; i++) { | 
| 56 | gwlarson | 3.14 | do {                                    /* next unique ray */ | 
| 57 |  |  | multisamp(sl, 4, urand(offset+i)); | 
| 58 |  |  | p->ra[i].r[0][0] = ila[0] = sl[0] * 256.; | 
| 59 |  |  | p->ra[i].r[0][1] = ila[1] = sl[1] * 256.; | 
| 60 |  |  | p->ra[i].r[1][0] = ila[2] = sl[2] * 256.; | 
| 61 |  |  | p->ra[i].r[1][1] = ila[3] = sl[3] * 256.; | 
| 62 |  |  | if (bp == NULL) | 
| 63 |  |  | break; | 
| 64 |  |  | for (k = ilhash(ila,4); (j = mhtab[k%nmh]) >= 0; k++) | 
| 65 |  |  | if (hdbray(bp)[j].r[0][0] == | 
| 66 |  |  | p->ra[i].r[0][0] && | 
| 67 |  |  | hdbray(bp)[j].r[0][1] == | 
| 68 |  |  | p->ra[i].r[0][1] && | 
| 69 |  |  | hdbray(bp)[j].r[1][0] == | 
| 70 |  |  | p->ra[i].r[1][0] && | 
| 71 |  |  | hdbray(bp)[j].r[1][1] == | 
| 72 |  |  | p->ra[i].r[1][1]) { | 
| 73 |  |  | offset += bp->nrm - j; | 
| 74 |  |  | break; | 
| 75 |  |  | } | 
| 76 |  |  | } while (j >= 0); | 
| 77 | gregl | 3.1 | d = hdray(ro, rd, hdlist[p->hd], gc, p->ra[i].r); | 
| 78 | gregl | 3.8 | if (!vdef(OBSTRUCTIONS)) | 
| 79 |  |  | d *= frandom();                 /* random offset */ | 
| 80 | gregl | 3.1 | if (p->offset != NULL) { | 
| 81 | gregl | 3.8 | VSUM(ro, ro, rd, d);            /* advance ray */ | 
| 82 | gregl | 3.1 | p->offset[i] = d; | 
| 83 |  |  | } | 
| 84 |  |  | VCOPY(rod, ro); | 
| 85 |  |  | rod += 3; | 
| 86 |  |  | VCOPY(rod, rd); | 
| 87 |  |  | rod += 3; | 
| 88 |  |  | } | 
| 89 |  |  | } | 
| 90 |  |  |  | 
| 91 |  |  |  | 
| 92 |  |  | donerays(p, rvl)                /* encode finished ray computations */ | 
| 93 |  |  | register PACKET *p; | 
| 94 |  |  | register float  *rvl; | 
| 95 |  |  | { | 
| 96 |  |  | double  d; | 
| 97 |  |  | register int    i; | 
| 98 |  |  |  | 
| 99 |  |  | for (i = 0; i < p->nr; i++) { | 
| 100 |  |  | setcolr(p->ra[i].v, rvl[0], rvl[1], rvl[2]); | 
| 101 |  |  | d = rvl[3]; | 
| 102 |  |  | if (p->offset != NULL) | 
| 103 |  |  | d += p->offset[i]; | 
| 104 |  |  | p->ra[i].d = hdcode(hdlist[p->hd], d); | 
| 105 |  |  | rvl += 4; | 
| 106 |  |  | } | 
| 107 | gregl | 3.4 | p->nc += p->nr; | 
| 108 | gregl | 3.5 | } | 
| 109 |  |  |  | 
| 110 |  |  |  | 
| 111 |  |  | int | 
| 112 |  |  | done_rtrace()                   /* clean up and close rtrace calculation */ | 
| 113 |  |  | { | 
| 114 |  |  | int     status; | 
| 115 |  |  | /* already closed? */ | 
| 116 |  |  | if (!nprocs) | 
| 117 |  |  | return; | 
| 118 |  |  | /* flush beam queue */ | 
| 119 |  |  | done_packets(flush_queue()); | 
| 120 | gregl | 3.7 | /* sync holodeck */ | 
| 121 |  |  | hdsync(NULL, 1); | 
| 122 | gregl | 3.5 | /* close rtrace */ | 
| 123 |  |  | if ((status = end_rtrace())) | 
| 124 |  |  | error(WARNING, "bad exit status from rtrace"); | 
| 125 | gregl | 3.9 | if (vdef(REPORT)) {             /* report time */ | 
| 126 | gregl | 3.10 | eputs("rtrace process closed\n"); | 
| 127 | gregl | 3.5 | report(0); | 
| 128 | gregl | 3.9 | } | 
| 129 | gregl | 3.5 | return(status);                 /* return status */ | 
| 130 |  |  | } | 
| 131 |  |  |  | 
| 132 |  |  |  | 
| 133 |  |  | new_rtrace()                    /* restart rtrace calculation */ | 
| 134 |  |  | { | 
| 135 |  |  | char    combuf[128]; | 
| 136 |  |  |  | 
| 137 |  |  | if (nprocs > 0)                 /* already running? */ | 
| 138 |  |  | return; | 
| 139 | gregl | 3.6 | starttime = time(NULL);         /* reset start time and counts */ | 
| 140 |  |  | npacksdone = nraysdone = 0L; | 
| 141 |  |  | if (vdef(TIME))                 /* reset end time */ | 
| 142 |  |  | endtime = starttime + vflt(TIME)*3600. + .5; | 
| 143 | gregl | 3.5 | if (vdef(RIF)) {                /* rerun rad to update octree */ | 
| 144 |  |  | sprintf(combuf, "rad -v 0 -s -w %s", vval(RIF)); | 
| 145 |  |  | if (system(combuf)) | 
| 146 |  |  | error(WARNING, "error running rad"); | 
| 147 |  |  | } | 
| 148 |  |  | if (start_rtrace() < 1)         /* start rtrace */ | 
| 149 |  |  | error(WARNING, "cannot restart rtrace"); | 
| 150 | gregl | 3.9 | else if (vdef(REPORT)) { | 
| 151 |  |  | eputs("rtrace process restarted\n"); | 
| 152 | gregl | 3.5 | report(0); | 
| 153 | gregl | 3.9 | } | 
| 154 | gregl | 3.5 | } | 
| 155 |  |  |  | 
| 156 |  |  |  | 
| 157 |  |  | getradfile()                    /* run rad and get needed variables */ | 
| 158 |  |  | { | 
| 159 | gwlarson | 3.12 | static short    mvar[] = {OCTREE,EYESEP,-1}; | 
| 160 | gregl | 3.5 | static char     tf1[] = TEMPLATE; | 
| 161 |  |  | char    tf2[64]; | 
| 162 |  |  | char    combuf[256]; | 
| 163 |  |  | char    *pippt; | 
| 164 |  |  | register int    i; | 
| 165 |  |  | register char   *cp; | 
| 166 |  |  | /* check if rad file specified */ | 
| 167 |  |  | if (!vdef(RIF)) | 
| 168 | gregl | 3.11 | return(0); | 
| 169 | gregl | 3.5 | /* create rad command */ | 
| 170 |  |  | mktemp(tf1); | 
| 171 |  |  | sprintf(tf2, "%s.rif", tf1); | 
| 172 |  |  | sprintf(combuf, | 
| 173 |  |  | "rad -v 0 -s -e -w %s OPTFILE=%s | egrep '^[ \t]*(NOMATCH", | 
| 174 |  |  | vval(RIF), tf1); | 
| 175 |  |  | cp = combuf; | 
| 176 |  |  | while (*cp){ | 
| 177 |  |  | if (*cp == '|') pippt = cp; | 
| 178 |  |  | cp++; | 
| 179 |  |  | }                               /* match unset variables */ | 
| 180 |  |  | for (i = 0; mvar[i] >= 0; i++) | 
| 181 |  |  | if (!vdef(mvar[i])) { | 
| 182 |  |  | *cp++ = '|'; | 
| 183 |  |  | strcpy(cp, vnam(mvar[i])); | 
| 184 |  |  | while (*cp) cp++; | 
| 185 |  |  | pippt = NULL; | 
| 186 |  |  | } | 
| 187 |  |  | if (pippt != NULL) | 
| 188 |  |  | strcpy(pippt, "> /dev/null");   /* nothing to match */ | 
| 189 |  |  | else | 
| 190 |  |  | sprintf(cp, ")[ \t]*=' > %s", tf2); | 
| 191 | gwlarson | 3.13 | #ifdef DEBUG | 
| 192 |  |  | wputs(combuf); wputs("\n"); | 
| 193 |  |  | #endif | 
| 194 |  |  | system(combuf);                         /* ignore exit code */ | 
| 195 | gregl | 3.5 | if (pippt == NULL) { | 
| 196 |  |  | loadvars(tf2);                  /* load variables */ | 
| 197 |  |  | unlink(tf2); | 
| 198 |  |  | } | 
| 199 |  |  | rtargc += wordfile(rtargv+rtargc, tf1); /* get rtrace options */ | 
| 200 |  |  | unlink(tf1);                    /* clean up */ | 
| 201 | gregl | 3.11 | return(1); | 
| 202 | gregl | 3.6 | } | 
| 203 |  |  |  | 
| 204 |  |  |  | 
| 205 |  |  | report(t)                       /* report progress so far */ | 
| 206 |  |  | time_t  t; | 
| 207 |  |  | { | 
| 208 |  |  | static time_t   seconds2go = 1000000; | 
| 209 |  |  |  | 
| 210 |  |  | if (t == 0L) | 
| 211 |  |  | t = time(NULL); | 
| 212 |  |  | sprintf(errmsg, "%ld packets (%ld rays) done after %.2f hours\n", | 
| 213 |  |  | npacksdone, nraysdone, (t-starttime)/3600.); | 
| 214 |  |  | eputs(errmsg); | 
| 215 |  |  | if (seconds2go == 1000000) | 
| 216 |  |  | seconds2go = vdef(REPORT) ? (long)(vflt(REPORT)*60. + .5) : 0L; | 
| 217 |  |  | if (seconds2go) | 
| 218 |  |  | reporttime = t + seconds2go; | 
| 219 | gregl | 3.1 | } |