1 |
gregl |
3.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\n", rs.xr, rs.yr); |
128 |
|
|
exit(0); |
129 |
|
|
} |
130 |
|
|
putrays(); |
131 |
|
|
exit(0); |
132 |
|
|
userr: |
133 |
|
|
fprintf(stderr, |
134 |
|
|
"Usage: %s [ -f{a|f|d} | -d ] { view opts .. | picture [zbuf] }\n", |
135 |
|
|
progname); |
136 |
|
|
exit(1); |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
|
140 |
|
|
putrays() |
141 |
|
|
{ |
142 |
|
|
float *zbuf; |
143 |
|
|
int sc; |
144 |
|
|
FLOAT loc[2]; |
145 |
|
|
FVECT rorg, rdir; |
146 |
|
|
register int si, i; |
147 |
|
|
|
148 |
|
|
if (zfd >= 0) { |
149 |
|
|
zbuf = (float *)malloc(scanlen(&rs)*sizeof(float)); |
150 |
|
|
if (zbuf == NULL) { |
151 |
|
|
fprintf(stderr, "%s: not enough memory\n", progname); |
152 |
|
|
exit(1); |
153 |
|
|
} |
154 |
|
|
} |
155 |
|
|
for (sc = 0; sc < numscans(&rs); sc++) { |
156 |
|
|
if (zfd >= 0) { |
157 |
|
|
if (read(zfd, zbuf, scanlen(&rs)*sizeof(float)) < |
158 |
|
|
scanlen(&rs)*sizeof(float)) { |
159 |
|
|
fprintf(stderr, "%s: depth buffer read error\n", |
160 |
|
|
progname); |
161 |
|
|
exit(1); |
162 |
|
|
} |
163 |
|
|
} |
164 |
|
|
for (si = 0; si < scanlen(&rs); si++) { |
165 |
|
|
pix2loc(loc, &rs, si, sc); |
166 |
|
|
if (viewray(rorg, rdir, &vw, loc[0], loc[1]) < -FTINY) |
167 |
|
|
rorg[0] = rorg[1] = rorg[2] = |
168 |
|
|
rdir[0] = rdir[1] = rdir[2] = 0.; |
169 |
|
|
else if (zfd >= 0) |
170 |
|
|
for (i = 0; i < 3; i++) { |
171 |
|
|
rorg[i] += rdir[i]*zbuf[si]; |
172 |
|
|
rdir[i] = -rdir[i]; |
173 |
|
|
} |
174 |
|
|
(*putr)(rorg, rdir); |
175 |
|
|
} |
176 |
|
|
} |
177 |
|
|
if (zfd >= 0) |
178 |
|
|
free((char *)zbuf); |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
|
182 |
|
|
puta(ro, rd) /* put out ray in ASCII format */ |
183 |
|
|
FVECT ro, rd; |
184 |
|
|
{ |
185 |
|
|
printf("%.5e %.5e %.5e %.5e %.5e %.5e\n", |
186 |
|
|
ro[0], ro[1], ro[2], |
187 |
|
|
rd[0], rd[1], rd[2]); |
188 |
|
|
} |
189 |
|
|
|
190 |
|
|
|
191 |
|
|
putf(ro, rd) /* put out ray in float format */ |
192 |
|
|
FVECT ro, rd; |
193 |
|
|
{ |
194 |
|
|
float v[6]; |
195 |
|
|
|
196 |
|
|
v[0] = ro[0]; v[1] = ro[1]; v[2] = ro[2]; |
197 |
|
|
v[3] = rd[0]; v[4] = rd[1]; v[5] = rd[2]; |
198 |
|
|
fwrite(v, sizeof(float), 6, stdout); |
199 |
|
|
} |
200 |
|
|
|
201 |
|
|
|
202 |
|
|
putd(ro, rd) /* put out ray in double format */ |
203 |
|
|
FVECT ro, rd; |
204 |
|
|
{ |
205 |
|
|
double v[6]; |
206 |
|
|
|
207 |
|
|
v[0] = ro[0]; v[1] = ro[1]; v[2] = ro[2]; |
208 |
|
|
v[3] = rd[0]; v[4] = rd[1]; v[5] = rd[2]; |
209 |
|
|
fwrite(v, sizeof(double), 6, stdout); |
210 |
|
|
} |