1 |
/* Copyright (c) 1997 Silicon Graphics, Inc. */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ SGI"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* Compute rays corresponding to a given picture or view. |
9 |
*/ |
10 |
|
11 |
|
12 |
#include "standard.h" |
13 |
|
14 |
#include "view.h" |
15 |
|
16 |
#include "resolu.h" |
17 |
|
18 |
extern int putf(), putd(), puta(); |
19 |
|
20 |
int (*putr)() = puta; |
21 |
|
22 |
VIEW vw = STDVIEW; |
23 |
|
24 |
RESOLU rs = {PIXSTANDARD, 512, 512}; |
25 |
|
26 |
double pa = 1.; |
27 |
|
28 |
int zfd = -1; |
29 |
|
30 |
char *progname; |
31 |
|
32 |
|
33 |
main(argc, argv) |
34 |
int argc; |
35 |
char *argv[]; |
36 |
{ |
37 |
char *err; |
38 |
int rval, getdim = 0; |
39 |
register int i; |
40 |
|
41 |
progname = argv[0]; |
42 |
if (argc < 2) |
43 |
goto userr; |
44 |
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
45 |
switch (argv[i][1]) { |
46 |
case 'f': /* output format */ |
47 |
switch (argv[i][2]) { |
48 |
case 'a': /* ASCII */ |
49 |
putr = puta; |
50 |
break; |
51 |
case 'f': /* float */ |
52 |
putr = putf; |
53 |
break; |
54 |
case 'd': /* double */ |
55 |
putr = putd; |
56 |
break; |
57 |
default: |
58 |
goto userr; |
59 |
} |
60 |
break; |
61 |
case 'v': /* view file or option */ |
62 |
if (argv[i][2] == 'f') { |
63 |
rval = viewfile(argv[++i], &vw, NULL); |
64 |
if (rval <= 0) { |
65 |
fprintf(stderr, |
66 |
"%s: no view in file\n", |
67 |
argv[i]); |
68 |
exit(1); |
69 |
} |
70 |
break; |
71 |
} |
72 |
rval = getviewopt(&vw, argc-i, argv+i); |
73 |
if (rval < 0) |
74 |
goto userr; |
75 |
i += rval; |
76 |
break; |
77 |
case 'd': /* report dimensions only */ |
78 |
getdim++; |
79 |
break; |
80 |
case 'x': /* x resolution */ |
81 |
rs.xr = atoi(argv[++i]); |
82 |
if (rs.xr <= 0) { |
83 |
fprintf(stderr, "%s: bad x resolution\n", |
84 |
progname); |
85 |
exit(1); |
86 |
} |
87 |
break; |
88 |
case 'y': /* y resolution */ |
89 |
rs.yr = atoi(argv[++i]); |
90 |
if (rs.yr <= 0) { |
91 |
fprintf(stderr, "%s: bad y resolution\n", |
92 |
progname); |
93 |
exit(1); |
94 |
} |
95 |
break; |
96 |
case 'p': /* pixel aspect ratio */ |
97 |
pa = atof(argv[++i]); |
98 |
break; |
99 |
default: |
100 |
goto userr; |
101 |
} |
102 |
if (i > argc | i+2 < argc) |
103 |
goto userr; |
104 |
if (i < argc) { |
105 |
rval = viewfile(argv[i], &vw, &rs); |
106 |
if (rval <= 0) { |
107 |
fprintf(stderr, "%s: no view in picture\n", argv[i]); |
108 |
exit(1); |
109 |
} |
110 |
if (i+1 < argc) { |
111 |
zfd = open(argv[i+1], O_RDONLY); |
112 |
if (zfd < 0) { |
113 |
fprintf(stderr, |
114 |
"%s: cannot open depth buffer\n", |
115 |
argv[i+1]); |
116 |
exit(1); |
117 |
} |
118 |
} |
119 |
} |
120 |
if ((err = setview(&vw)) != NULL) { |
121 |
fprintf(stderr, "%s: %s\n", progname, err); |
122 |
exit(1); |
123 |
} |
124 |
if (i == argc) |
125 |
normaspect(viewaspect(&vw), &pa, &rs.xr, &rs.yr); |
126 |
if (getdim) { |
127 |
printf("-x %d -y %d -ld%c\n", rs.xr, rs.yr, |
128 |
vw.vaft > FTINY ? '+' : '-'); |
129 |
exit(0); |
130 |
} |
131 |
putrays(); |
132 |
exit(0); |
133 |
userr: |
134 |
fprintf(stderr, |
135 |
"Usage: %s [ -f{a|f|d} | -d ] { view opts .. | picture [zbuf] }\n", |
136 |
progname); |
137 |
exit(1); |
138 |
} |
139 |
|
140 |
|
141 |
putrays() |
142 |
{ |
143 |
static FLOAT loc[2]; |
144 |
static FVECT rorg, rdir; |
145 |
float *zbuf; |
146 |
int sc; |
147 |
double d; |
148 |
register int si, i; |
149 |
|
150 |
if (zfd >= 0) { |
151 |
zbuf = (float *)malloc(scanlen(&rs)*sizeof(float)); |
152 |
if (zbuf == NULL) { |
153 |
fprintf(stderr, "%s: not enough memory\n", progname); |
154 |
exit(1); |
155 |
} |
156 |
} |
157 |
for (sc = 0; sc < numscans(&rs); sc++) { |
158 |
if (zfd >= 0) { |
159 |
if (read(zfd, zbuf, scanlen(&rs)*sizeof(float)) < |
160 |
scanlen(&rs)*sizeof(float)) { |
161 |
fprintf(stderr, "%s: depth buffer read error\n", |
162 |
progname); |
163 |
exit(1); |
164 |
} |
165 |
} |
166 |
for (si = 0; si < scanlen(&rs); si++) { |
167 |
pix2loc(loc, &rs, si, sc); |
168 |
d = viewray(rorg, rdir, &vw, loc[0], loc[1]); |
169 |
if (d < -FTINY) |
170 |
rorg[0] = rorg[1] = rorg[2] = |
171 |
rdir[0] = rdir[1] = rdir[2] = 0.; |
172 |
else if (zfd >= 0) |
173 |
for (i = 0; i < 3; i++) { |
174 |
rorg[i] += rdir[i]*zbuf[si]; |
175 |
rdir[i] = -rdir[i]; |
176 |
} |
177 |
else if (d > FTINY) { |
178 |
rdir[0] *= d; rdir[1] *= d; rdir[2] *= d; |
179 |
} |
180 |
(*putr)(rorg, rdir); |
181 |
} |
182 |
} |
183 |
if (zfd >= 0) |
184 |
free((char *)zbuf); |
185 |
} |
186 |
|
187 |
|
188 |
puta(ro, rd) /* put out ray in ASCII format */ |
189 |
FVECT ro, rd; |
190 |
{ |
191 |
printf("%.5e %.5e %.5e %.5e %.5e %.5e\n", |
192 |
ro[0], ro[1], ro[2], |
193 |
rd[0], rd[1], rd[2]); |
194 |
} |
195 |
|
196 |
|
197 |
putf(ro, rd) /* put out ray in float format */ |
198 |
FVECT ro, rd; |
199 |
{ |
200 |
float v[6]; |
201 |
|
202 |
v[0] = ro[0]; v[1] = ro[1]; v[2] = ro[2]; |
203 |
v[3] = rd[0]; v[4] = rd[1]; v[5] = rd[2]; |
204 |
fwrite(v, sizeof(float), 6, stdout); |
205 |
} |
206 |
|
207 |
|
208 |
putd(ro, rd) /* put out ray in double format */ |
209 |
FVECT ro, rd; |
210 |
{ |
211 |
double v[6]; |
212 |
|
213 |
v[0] = ro[0]; v[1] = ro[1]; v[2] = ro[2]; |
214 |
v[3] = rd[0]; v[4] = rd[1]; v[5] = rd[2]; |
215 |
fwrite(v, sizeof(double), 6, stdout); |
216 |
} |