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