ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rv3.c
Revision: 1.1
Committed: Thu Feb 2 10:41:39 1989 UTC (35 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Log Message:
Initial revision

File Contents

# Content
1 /* Copyright (c) 1987 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * rv3.c - miscellaneous routines for rview.
9 *
10 * 5/11/87
11 */
12
13 #include "ray.h"
14
15 #include "rpaint.h"
16
17 #include "random.h"
18
19
20 float * /* keep consistent with COLOR typedef */
21 greyof(col) /* convert color to greyscale */
22 register COLOR col;
23 {
24 static COLOR gcol;
25 double b;
26
27 b = bright(col);
28 setcolor(gcol, b, b, b);
29 return(gcol);
30 }
31
32
33 paint(p, xmin, ymin, xmax, ymax) /* compute and paint a rectangle */
34 register PNODE *p;
35 int xmin, ymin, xmax, ymax;
36 {
37 static RAY thisray;
38 double h, v;
39 register int i;
40
41 if (xmax - xmin <= 0 || ymax - ymin <= 0) { /* empty */
42 p->x = xmin;
43 p->y = ymin;
44 setcolor(p->v, 0.0, 0.0, 0.0);
45 return;
46 }
47 /* jitter ray direction */
48 p->x = h = xmin + (xmax-xmin)*frandom();
49 p->y = v = ymin + (ymax-ymin)*frandom();
50
51 rayview(thisray.rorg, thisray.rdir, &ourview, h, v);
52
53 rayorigin(&thisray, NULL, PRIMARY, 1.0);
54
55 rayvalue(&thisray);
56
57 copycolor(p->v, thisray.rcol);
58
59 scalecolor(p->v, exposure);
60
61 (*dev->paintr)(greyscale?greyof(p->v):p->v, xmin, ymin, xmax, ymax);
62 }
63
64
65 newimage() /* start a new image */
66 {
67 /* free old image */
68 freepkids(&ptrunk);
69 /* set up frame */
70 if (ourview.hresolu > dev->xsiz || ourview.vresolu > dev->ysiz)
71 error(USER, "resolution mismatch");
72 pframe.l = pframe.d = 0;
73 pframe.r = ourview.hresolu; pframe.u = ourview.vresolu;
74 pdepth = 0;
75 /* clear device */
76 (*dev->clear)(ourview.hresolu, ourview.vresolu);
77 /* get first value */
78 paint(&ptrunk, 0, 0, ourview.hresolu, ourview.vresolu);
79 }
80
81
82 redraw() /* redraw the image */
83 {
84 (*dev->clear)(ourview.hresolu, ourview.vresolu);
85 (*dev->comout)("redrawing...\n");
86 repaint(0, 0, ourview.hresolu, ourview.vresolu);
87 (*dev->comout)("\n");
88 }
89
90
91 repaint(xmin, ymin, xmax, ymax) /* repaint a region */
92 int xmin, ymin, xmax, ymax;
93 {
94 RECT reg;
95
96 reg.l = xmin; reg.r = xmax;
97 reg.d = ymin; reg.u = ymax;
98
99 paintrect(&ptrunk, 0, 0, ourview.hresolu, ourview.vresolu, &reg);
100 }
101
102
103 paintrect(p, xmin, ymin, xmax, ymax, r) /* paint picture rectangle */
104 register PNODE *p;
105 int xmin, ymin, xmax, ymax;
106 register RECT *r;
107 {
108 int mx, my;
109
110 if (dev->inpready)
111 return; /* break for input */
112
113 if (xmax - xmin <= 0 || ymax - ymin <= 0)
114 return;
115
116 if (p->kid == NULL) {
117 (*dev->paintr)(greyscale?greyof(p->v):p->v,
118 xmin, ymin, xmax, ymax); /* do this */
119 return;
120 }
121 mx = (xmin + xmax) >> 1; /* do kids */
122 my = (ymin + ymax) >> 1;
123 if (mx > r->l) {
124 if (my > r->d)
125 paintrect(p->kid+DL, xmin, ymin, mx, my, r);
126 if (my < r->u)
127 paintrect(p->kid+UL, xmin, my, mx, ymax, r);
128 }
129 if (mx < r->r) {
130 if (my > r->d)
131 paintrect(p->kid+DR, mx, ymin, xmax, my, r);
132 if (my < r->u)
133 paintrect(p->kid+UR, mx, my, xmax, ymax, r);
134 }
135 }
136
137
138 PNODE *
139 findrect(x, y, p, r, pd) /* find a rectangle */
140 int x, y;
141 register PNODE *p;
142 register RECT *r;
143 int pd;
144 {
145 int mx, my;
146
147 while (p->kid != NULL && pd--) {
148
149 mx = (r->l + r->r) >> 1;
150 my = (r->d + r->u) >> 1;
151
152 if (x < mx) {
153 r->r = mx;
154 if (y < my) {
155 r->u = my;
156 p = p->kid+DL;
157 } else {
158 r->d = my;
159 p = p->kid+UL;
160 }
161 } else {
162 r->l = mx;
163 if (y < my) {
164 r->u = my;
165 p = p->kid+DR;
166 } else {
167 r->d = my;
168 p = p->kid+UR;
169 }
170 }
171 }
172 return(p);
173 }
174
175
176 scalepict(p, sf) /* scale picture values */
177 register PNODE *p;
178 double sf;
179 {
180 scalecolor(p->v, sf); /* do this node */
181
182 if (p->kid == NULL)
183 return;
184 /* do children */
185 scalepict(p->kid+DL, sf);
186 scalepict(p->kid+DR, sf);
187 scalepict(p->kid+UL, sf);
188 scalepict(p->kid+UR, sf);
189 }
190
191
192 getpictcolrs(yoff, scan, p, xsiz, ysiz) /* get scanline from picture */
193 int yoff;
194 register COLR *scan;
195 register PNODE *p;
196 int xsiz, ysiz;
197 {
198 register int mx;
199 int my;
200
201 if (p->kid == NULL) { /* do this node */
202 setcolr(scan[0], colval(p->v,RED),
203 colval(p->v,GRN),
204 colval(p->v,BLU));
205 for (mx = 1; mx < xsiz; mx++)
206 copycolr(scan[mx], scan[0]);
207 return;
208 }
209 /* do kids */
210 mx = xsiz >> 1;
211 my = ysiz >> 1;
212 if (yoff < my) {
213 getpictcolrs(yoff, scan, p->kid+DL, mx, my);
214 getpictcolrs(yoff, scan+mx, p->kid+DR, xsiz-mx, my);
215 } else {
216 getpictcolrs(yoff-my, scan, p->kid+UL, mx, ysiz-my);
217 getpictcolrs(yoff-my, scan+mx, p->kid+UR, xsiz-mx, ysiz-my);
218 }
219 }
220
221
222 pcopy(p1, p2) /* copy paint node p1 into p2 */
223 register PNODE *p1, *p2;
224 {
225 copycolor(p2->v, p1->v);
226 p2->x = p1->x;
227 p2->y = p1->y;
228 }
229
230
231 freepkids(p) /* free pnode's children */
232 register PNODE *p;
233 {
234 if (p->kid == NULL)
235 return;
236 freepkids(p->kid+DL);
237 freepkids(p->kid+DR);
238 freepkids(p->kid+UL);
239 freepkids(p->kid+UR);
240 free((char *)p->kid);
241 p->kid = NULL;
242 }
243
244
245 newview(vp) /* change viewing parameters */
246 register VIEW *vp;
247 {
248 char *err;
249
250 if (vp->hresolu > dev->xsiz || vp->vresolu > dev->ysiz) {
251 error(COMMAND, "view not set - resolution mismatch");
252 } else if ((err = setview(vp)) != NULL) {
253 sprintf(errmsg, "view not set - %s", err);
254 error(COMMAND, errmsg);
255 } else if (bcmp(vp, &ourview, sizeof(VIEW))) {
256 bcopy(&ourview, &oldview, sizeof(VIEW));
257 bcopy(vp, &ourview, sizeof(VIEW));
258 newimage();
259 }
260 }
261
262
263 moveview(angle, mag, vc) /* move viewpoint */
264 double angle, mag;
265 FVECT vc;
266 {
267 extern double sqrt(), dist2();
268 double d;
269 VIEW nv;
270 register int i;
271
272 VCOPY(nv.vup, ourview.vup);
273 nv.hresolu = ourview.hresolu; nv.vresolu = ourview.vresolu;
274 spinvector(nv.vdir, ourview.vdir, ourview.vup, angle*(PI/180.));
275 d = sqrt(dist2(ourview.vp, vc)) / mag;
276 for (i = 0; i < 3; i++)
277 nv.vp[i] = vc[i] - d*nv.vdir[i];
278 if ((nv.type = ourview.type) == VT_PAR) {
279 nv.horiz = ourview.horiz / mag;
280 nv.vert = ourview.vert / mag;
281 } else {
282 nv.horiz = ourview.horiz;
283 nv.vert = ourview.vert;
284 }
285 newview(&nv);
286 }
287
288
289 spinvector(vres, vorig, vnorm, theta) /* rotate vector around normal */
290 FVECT vres, vorig, vnorm;
291 double theta;
292 {
293 extern double sin(), cos();
294 double sint, cost, dotp;
295 FVECT vperp;
296 register int i;
297
298 if (theta == 0.0) {
299 VCOPY(vres, vorig);
300 return;
301 }
302 sint = sin(theta);
303 cost = cos(theta);
304 dotp = DOT(vorig, vnorm);
305 fcross(vperp, vnorm, vorig);
306 for (i = 0; i < 3; i++)
307 vres[i] = vnorm[i]*dotp*(1.-cost) +
308 vorig[i]*cost + vperp[i]*sint;
309 }