1 |
/* Copyright (c) 1986 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* image.c - routines for image generation. |
9 |
* |
10 |
* 10/17/85 |
11 |
*/ |
12 |
|
13 |
#include "standard.h" |
14 |
|
15 |
#include "view.h" |
16 |
|
17 |
VIEW stdview = STDVIEW(512); /* default view parameters */ |
18 |
|
19 |
|
20 |
char * |
21 |
setview(v) /* set vhinc and vvinc, return message on error */ |
22 |
register VIEW *v; |
23 |
{ |
24 |
double tan(), dt; |
25 |
|
26 |
if (normalize(v->vdir) == 0.0) /* normalize direction */ |
27 |
return("zero view direction"); |
28 |
|
29 |
fcross(v->vhinc, v->vdir, v->vup); /* compute horiz dir */ |
30 |
|
31 |
if (normalize(v->vhinc) == 0.0) |
32 |
return("illegal view up vector"); |
33 |
|
34 |
fcross(v->vvinc, v->vhinc, v->vdir); /* compute vert dir */ |
35 |
|
36 |
if (v->type == VT_PAR) |
37 |
dt = v->horiz; |
38 |
else if (v->type == VT_PER) |
39 |
dt = 2.0 * tan(v->horiz*(PI/180.0/2.0)); |
40 |
else |
41 |
return("unknown view type"); |
42 |
|
43 |
if (dt <= FTINY || dt >= FHUGE) |
44 |
return("illegal horizontal view size"); |
45 |
|
46 |
if (v->hresolu <= 0) |
47 |
return("illegal horizontal resolution"); |
48 |
|
49 |
dt /= (double)v->hresolu; |
50 |
v->vhinc[0] *= dt; |
51 |
v->vhinc[1] *= dt; |
52 |
v->vhinc[2] *= dt; |
53 |
|
54 |
if (v->type == VT_PAR) |
55 |
dt = v->vert; |
56 |
else |
57 |
dt = 2.0 * tan(v->vert*(PI/180.0/2.0)); |
58 |
|
59 |
if (dt <= FTINY || dt >= FHUGE) |
60 |
return("illegal vertical view size"); |
61 |
|
62 |
if (v->vresolu <= 0) |
63 |
return("illegal vertical resolution"); |
64 |
|
65 |
dt /= (double)v->vresolu; |
66 |
v->vvinc[0] *= dt; |
67 |
v->vvinc[1] *= dt; |
68 |
v->vvinc[2] *= dt; |
69 |
|
70 |
return(NULL); |
71 |
} |
72 |
|
73 |
|
74 |
rayview(orig, direc, v, x, y) /* compute ray origin and direction */ |
75 |
FVECT orig, direc; |
76 |
register VIEW *v; |
77 |
double x, y; |
78 |
{ |
79 |
register int i; |
80 |
|
81 |
x -= 0.5 * v->hresolu; |
82 |
y -= 0.5 * v->vresolu; |
83 |
|
84 |
if (v->type == VT_PAR) { /* parallel view */ |
85 |
for (i = 0; i < 3; i++) |
86 |
orig[i] = v->vp[i] + x*v->vhinc[i] + y*v->vvinc[i]; |
87 |
VCOPY(direc, v->vdir); |
88 |
} else { /* perspective view */ |
89 |
VCOPY(orig, v->vp); |
90 |
for (i = 0; i < 3; i++) |
91 |
direc[i] = v->vdir[i] + x*v->vhinc[i] + y*v->vvinc[i]; |
92 |
normalize(direc); |
93 |
} |
94 |
} |
95 |
|
96 |
|
97 |
sscanview(vp, s) /* get view parameters */ |
98 |
register VIEW *vp; |
99 |
register char *s; |
100 |
{ |
101 |
for ( ; ; ) |
102 |
switch (*s++) { |
103 |
case '\0': |
104 |
case '\n': |
105 |
return(0); |
106 |
case '-': |
107 |
switch (*s++) { |
108 |
case '\0': |
109 |
return(-1); |
110 |
case 'x': |
111 |
if (sscanf(s, "%d", &vp->hresolu) != 1) |
112 |
return(-1); |
113 |
s = sskip(s); |
114 |
continue; |
115 |
case 'y': |
116 |
if (sscanf(s, "%d", &vp->vresolu) != 1) |
117 |
return(-1); |
118 |
s = sskip(s); |
119 |
continue; |
120 |
case 'v': |
121 |
switch (*s++) { |
122 |
case '\0': |
123 |
return(-1); |
124 |
case 't': |
125 |
vp->type = *s++; |
126 |
continue; |
127 |
case 'p': |
128 |
if (sscanf(s, "%lf %lf %lf", |
129 |
&vp->vp[0], |
130 |
&vp->vp[1], |
131 |
&vp->vp[2]) != 3) |
132 |
return(-1); |
133 |
s = sskip(sskip(sskip(s))); |
134 |
continue; |
135 |
case 'd': |
136 |
if (sscanf(s, "%lf %lf %lf", |
137 |
&vp->vdir[0], |
138 |
&vp->vdir[1], |
139 |
&vp->vdir[2]) != 3) |
140 |
return(-1); |
141 |
s = sskip(sskip(sskip(s))); |
142 |
continue; |
143 |
case 'u': |
144 |
if (sscanf(s, "%lf %lf %lf", |
145 |
&vp->vup[0], |
146 |
&vp->vup[1], |
147 |
&vp->vup[2]) != 3) |
148 |
return(-1); |
149 |
s = sskip(sskip(sskip(s))); |
150 |
continue; |
151 |
case 'h': |
152 |
if (sscanf(s, "%lf", |
153 |
&vp->horiz) != 1) |
154 |
return(-1); |
155 |
s = sskip(s); |
156 |
continue; |
157 |
case 'v': |
158 |
if (sscanf(s, "%lf", |
159 |
&vp->vert) != 1) |
160 |
return(-1); |
161 |
s = sskip(s); |
162 |
continue; |
163 |
} |
164 |
continue; |
165 |
} |
166 |
continue; |
167 |
} |
168 |
} |
169 |
|
170 |
|
171 |
fprintview(vp, fp) /* write out view parameters */ |
172 |
register VIEW *vp; |
173 |
FILE *fp; |
174 |
{ |
175 |
fprintf(fp, " -vt%c", vp->type); |
176 |
fprintf(fp, " -vp %.6g %.6g %.6g", vp->vp[0], vp->vp[1], vp->vp[2]); |
177 |
fprintf(fp, " -vd %.6g %.6g %.6g", vp->vdir[0], vp->vdir[1], vp->vdir[2]); |
178 |
fprintf(fp, " -vu %.6g %.6g %.6g", vp->vup[0], vp->vup[1], vp->vup[2]); |
179 |
fprintf(fp, " -vh %.6g -vv %.6g", vp->horiz, vp->vert); |
180 |
fprintf(fp, " -x %d -y %d", vp->hresolu, vp->vresolu); |
181 |
} |
182 |
|
183 |
|
184 |
static VIEW *hview; /* view from header */ |
185 |
static int gothview; /* success indicator */ |
186 |
static char *altname[] = {NULL,"rpict","rview",VIEWSTR,NULL}; |
187 |
|
188 |
|
189 |
static |
190 |
gethview(s) /* get view from header */ |
191 |
char *s; |
192 |
{ |
193 |
register char **an; |
194 |
|
195 |
for (an = altname; *an != NULL; an++) |
196 |
if (!strncmp(*an, s, strlen(*an))) { |
197 |
if (sscanview(hview, s+strlen(*an)) == 0) |
198 |
gothview++; |
199 |
return; |
200 |
} |
201 |
} |
202 |
|
203 |
|
204 |
int |
205 |
viewfile(fname, vp) /* get view from file */ |
206 |
char *fname; |
207 |
VIEW *vp; |
208 |
{ |
209 |
extern char *progname; |
210 |
FILE *fp; |
211 |
|
212 |
if ((fp = fopen(fname, "r")) == NULL) |
213 |
return(-1); |
214 |
|
215 |
altname[0] = progname; |
216 |
hview = vp; |
217 |
gothview = 0; |
218 |
|
219 |
getheader(fp, gethview); |
220 |
|
221 |
fclose(fp); |
222 |
|
223 |
return(gothview); |
224 |
} |