1 |
– |
/* Copyright (c) 1997 Silicon Graphics, Inc. */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ SGI"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Compute rays corresponding to a given picture or view. |
6 |
|
*/ |
7 |
|
|
8 |
< |
|
8 |
> |
#include "platform.h" |
9 |
|
#include "standard.h" |
10 |
< |
|
10 |
> |
#include "random.h" |
11 |
|
#include "view.h" |
12 |
|
|
13 |
< |
#include "resolu.h" |
13 |
> |
typedef void putfunc(FVECT ro, FVECT rd); |
14 |
> |
static putfunc puta; |
15 |
> |
static putfunc putf; |
16 |
> |
static putfunc putd; |
17 |
> |
static void pix2rays(FILE *fp); |
18 |
> |
static void putrays(void); |
19 |
|
|
20 |
< |
extern int putf(), putd(), puta(); |
20 |
> |
static putfunc *putr = puta; |
21 |
|
|
20 |
– |
int (*putr)() = puta; |
21 |
– |
|
22 |
|
VIEW vw = STDVIEW; |
23 |
|
|
24 |
|
RESOLU rs = {PIXSTANDARD, 512, 512}; |
25 |
|
|
26 |
|
double pa = 1.; |
27 |
|
|
28 |
+ |
double pj = 0.; |
29 |
+ |
|
30 |
|
int zfd = -1; |
31 |
|
|
32 |
+ |
int fromstdin = 0; |
33 |
+ |
|
34 |
+ |
int unbuffered = 0; |
35 |
+ |
|
36 |
+ |
int repeatcnt = 1; |
37 |
+ |
|
38 |
|
char *progname; |
39 |
|
|
40 |
|
|
41 |
< |
main(argc, argv) |
42 |
< |
int argc; |
43 |
< |
char *argv[]; |
41 |
> |
int |
42 |
> |
main( |
43 |
> |
int argc, |
44 |
> |
char *argv[] |
45 |
> |
) |
46 |
|
{ |
47 |
|
char *err; |
48 |
|
int rval, getdim = 0; |
49 |
< |
register int i; |
49 |
> |
int i; |
50 |
|
|
51 |
|
progname = argv[0]; |
52 |
|
if (argc < 2) |
60 |
|
break; |
61 |
|
case 'f': /* float */ |
62 |
|
putr = putf; |
63 |
+ |
SET_FILE_BINARY(stdout); |
64 |
|
break; |
65 |
|
case 'd': /* double */ |
66 |
|
putr = putd; |
67 |
+ |
SET_FILE_BINARY(stdout); |
68 |
|
break; |
69 |
|
default: |
70 |
|
goto userr; |
105 |
|
exit(1); |
106 |
|
} |
107 |
|
break; |
108 |
< |
case 'p': /* pixel aspect ratio */ |
109 |
< |
pa = atof(argv[++i]); |
108 |
> |
case 'c': /* repeat count */ |
109 |
> |
repeatcnt = atoi(argv[++i]); |
110 |
|
break; |
111 |
+ |
case 'p': /* pixel aspect or jitter */ |
112 |
+ |
if (argv[i][2] == 'a') |
113 |
+ |
pa = atof(argv[++i]); |
114 |
+ |
else if (argv[i][2] == 'j') |
115 |
+ |
pj = atof(argv[++i]); |
116 |
+ |
else |
117 |
+ |
goto userr; |
118 |
+ |
break; |
119 |
+ |
case 'i': /* get pixels from stdin */ |
120 |
+ |
fromstdin = 1; |
121 |
+ |
break; |
122 |
+ |
case 'u': /* unbuffered output */ |
123 |
+ |
unbuffered = 1; |
124 |
+ |
break; |
125 |
|
default: |
126 |
|
goto userr; |
127 |
|
} |
128 |
< |
if (i > argc | i+2 < argc) |
128 |
> |
if ((i > argc) | (i+2 < argc)) |
129 |
|
goto userr; |
130 |
|
if (i < argc) { |
131 |
|
rval = viewfile(argv[i], &vw, &rs); |
154 |
|
vw.vaft > FTINY ? '+' : '-'); |
155 |
|
exit(0); |
156 |
|
} |
157 |
< |
putrays(); |
157 |
> |
if (fromstdin) |
158 |
> |
pix2rays(stdin); |
159 |
> |
else |
160 |
> |
putrays(); |
161 |
|
exit(0); |
162 |
|
userr: |
163 |
|
fprintf(stderr, |
164 |
< |
"Usage: %s [ -f{a|f|d} | -d ] { view opts .. | picture [zbuf] }\n", |
164 |
> |
"Usage: %s [ -i -u -f{a|f|d} -c rept | -d ] { view opts .. | picture [zbuf] }\n", |
165 |
|
progname); |
166 |
|
exit(1); |
167 |
|
} |
168 |
|
|
169 |
|
|
170 |
< |
putrays() |
170 |
> |
static void |
171 |
> |
jitterloc( |
172 |
> |
RREAL loc[2] |
173 |
> |
) |
174 |
|
{ |
175 |
< |
static FLOAT loc[2]; |
175 |
> |
if (pj > FTINY) { |
176 |
> |
loc[0] += pj*(.5 - frandom())/rs.xr; |
177 |
> |
loc[1] += pj*(.5 - frandom())/rs.yr; |
178 |
> |
} |
179 |
> |
} |
180 |
> |
|
181 |
> |
|
182 |
> |
static void |
183 |
> |
pix2rays( |
184 |
> |
FILE *fp |
185 |
> |
) |
186 |
> |
{ |
187 |
|
static FVECT rorg, rdir; |
188 |
< |
float *zbuf; |
188 |
> |
float zval; |
189 |
> |
double px, py; |
190 |
> |
RREAL loc[2]; |
191 |
> |
int pp[2]; |
192 |
> |
double d; |
193 |
> |
int i; |
194 |
> |
|
195 |
> |
while (fscanf(fp, "%lf %lf", &px, &py) == 2) { |
196 |
> |
px += .5; py += .5; |
197 |
> |
loc[0] = px/rs.xr; loc[1] = py/rs.yr; |
198 |
> |
if (zfd >= 0) { |
199 |
> |
if ((loc[0] < 0) | (loc[0] >= 1) | |
200 |
> |
(loc[1] < 0) | (loc[1] >= 1)) { |
201 |
> |
fprintf(stderr, "%s: input pixel outside image\n", |
202 |
> |
progname); |
203 |
> |
exit(1); |
204 |
> |
} |
205 |
> |
loc2pix(pp, &rs, loc[0], loc[1]); |
206 |
> |
if (lseek(zfd, |
207 |
> |
(pp[1]*scanlen(&rs)+pp[0])*sizeof(float), |
208 |
> |
SEEK_SET) < 0 || |
209 |
> |
read(zfd, &zval, sizeof(float)) |
210 |
> |
< sizeof(float)) { |
211 |
> |
fprintf(stderr, "%s: depth buffer read error\n", |
212 |
> |
progname); |
213 |
> |
exit(1); |
214 |
> |
} |
215 |
> |
} |
216 |
> |
jitterloc(loc); |
217 |
> |
d = viewray(rorg, rdir, &vw, loc[0], loc[1]); |
218 |
> |
if (d < -FTINY) |
219 |
> |
rorg[0] = rorg[1] = rorg[2] = |
220 |
> |
rdir[0] = rdir[1] = rdir[2] = 0.; |
221 |
> |
else if (zfd >= 0) |
222 |
> |
for (i = 0; i < 3; i++) { |
223 |
> |
rorg[i] += rdir[i]*zval; |
224 |
> |
rdir[i] = -rdir[i]; |
225 |
> |
} |
226 |
> |
else if (d > FTINY) { |
227 |
> |
rdir[0] *= d; rdir[1] *= d; rdir[2] *= d; |
228 |
> |
} |
229 |
> |
(*putr)(rorg, rdir); |
230 |
> |
if (unbuffered) |
231 |
> |
fflush(stdout); |
232 |
> |
} |
233 |
> |
if (!feof(fp)) { |
234 |
> |
fprintf(stderr, "%s: expected px py on input\n", progname); |
235 |
> |
exit(1); |
236 |
> |
} |
237 |
> |
} |
238 |
> |
|
239 |
> |
|
240 |
> |
static void |
241 |
> |
putrays(void) |
242 |
> |
{ |
243 |
> |
RREAL loc[2]; |
244 |
> |
FVECT rorg, rdir; |
245 |
> |
float *zbuf = NULL; |
246 |
|
int sc; |
247 |
|
double d; |
248 |
< |
register int si, i; |
248 |
> |
int si, i, c; |
249 |
|
|
250 |
|
if (zfd >= 0) { |
251 |
|
zbuf = (float *)malloc(scanlen(&rs)*sizeof(float)); |
264 |
|
} |
265 |
|
} |
266 |
|
for (si = 0; si < scanlen(&rs); si++) { |
267 |
+ |
for (c = repeatcnt; c-- > 0; ) { |
268 |
|
pix2loc(loc, &rs, si, sc); |
269 |
+ |
jitterloc(loc); |
270 |
|
d = viewray(rorg, rdir, &vw, loc[0], loc[1]); |
271 |
|
if (d < -FTINY) |
272 |
|
rorg[0] = rorg[1] = rorg[2] = |
280 |
|
rdir[0] *= d; rdir[1] *= d; rdir[2] *= d; |
281 |
|
} |
282 |
|
(*putr)(rorg, rdir); |
283 |
+ |
} |
284 |
|
} |
285 |
|
} |
286 |
|
if (zfd >= 0) |
287 |
< |
free((char *)zbuf); |
287 |
> |
free((void *)zbuf); |
288 |
|
} |
289 |
|
|
290 |
|
|
291 |
< |
puta(ro, rd) /* put out ray in ASCII format */ |
292 |
< |
FVECT ro, rd; |
291 |
> |
static void |
292 |
> |
puta( /* put out ray in ASCII format */ |
293 |
> |
FVECT ro, |
294 |
> |
FVECT rd |
295 |
> |
) |
296 |
|
{ |
297 |
|
printf("%.5e %.5e %.5e %.5e %.5e %.5e\n", |
298 |
|
ro[0], ro[1], ro[2], |
300 |
|
} |
301 |
|
|
302 |
|
|
303 |
< |
putf(ro, rd) /* put out ray in float format */ |
304 |
< |
FVECT ro, rd; |
303 |
> |
static void |
304 |
> |
putf( /* put out ray in float format */ |
305 |
> |
FVECT ro, |
306 |
> |
FVECT rd |
307 |
> |
) |
308 |
|
{ |
309 |
|
float v[6]; |
310 |
|
|
311 |
|
v[0] = ro[0]; v[1] = ro[1]; v[2] = ro[2]; |
312 |
|
v[3] = rd[0]; v[4] = rd[1]; v[5] = rd[2]; |
313 |
< |
fwrite(v, sizeof(float), 6, stdout); |
313 |
> |
putbinary(v, sizeof(float), 6, stdout); |
314 |
|
} |
315 |
|
|
316 |
|
|
317 |
< |
putd(ro, rd) /* put out ray in double format */ |
318 |
< |
FVECT ro, rd; |
317 |
> |
static void |
318 |
> |
putd( /* put out ray in double format */ |
319 |
> |
FVECT ro, |
320 |
> |
FVECT rd |
321 |
> |
) |
322 |
|
{ |
323 |
|
double v[6]; |
324 |
|
|
325 |
|
v[0] = ro[0]; v[1] = ro[1]; v[2] = ro[2]; |
326 |
|
v[3] = rd[0]; v[4] = rd[1]; v[5] = rd[2]; |
327 |
< |
fwrite(v, sizeof(double), 6, stdout); |
327 |
> |
putbinary(v, sizeof(double), 6, stdout); |
328 |
|
} |