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