ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rview.c
(Generate patch)

Comparing ray/src/rt/rview.c (file contents):
Revision 2.17 by greg, Wed Apr 17 14:02:16 1996 UTC vs.
Revision 2.31 by greg, Fri Sep 5 19:45:42 2008 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1995 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  rview.c - routines and variables for interactive view generation.
6   *
7 < *     3/24/87
7 > *  External symbols declared in rpaint.h
8   */
9  
10 < #include  "ray.h"
10 > #include "copyright.h"
11  
15 #include  "rpaint.h"
16
12   #include  <signal.h>
18
13   #include  <ctype.h>
14  
15 < VIEW  ourview = STDVIEW;                /* viewing parameters */
16 < int  hresolu, vresolu;                  /* image resolution */
15 > #include  "ray.h"
16 > #include  "rpaint.h"
17  
24 int  dimlist[MAXDIM];                   /* sampling dimensions */
25 int  ndims = 0;                         /* number of sampling dimensions */
26 int  samplendx = 0;                     /* index for this sample */
27
28 int  psample = 8;                       /* pixel sample size */
29 double  maxdiff = .15;                  /* max. sample difference */
30
31 double  exposure = 1.0;                 /* exposure for scene */
32
33 double  dstrsrc = 0.0;                  /* square source distribution */
34 double  shadthresh = .1;                /* shadow threshold */
35 double  shadcert = .25;                 /* shadow certainty */
36 int  directrelay = 0;                   /* number of source relays */
37 int  vspretest = 128;                   /* virtual source pretest density */
38 int  directvis = 1;                     /* sources visible? */
39 double  srcsizerat = 0.;                /* maximum ratio source size/dist. */
40
41 COLOR  cextinction = BLKCOLOR;          /* global extinction coefficient */
42 COLOR  salbedo = BLKCOLOR;              /* global scattering albedo */
43 double  seccg = 0.;                     /* global scattering eccentricity */
44 double  ssampdist = 0.;                 /* scatter sampling distance */
45
46 double  specthresh = .3;                /* specular sampling threshold */
47 double  specjitter = 1.;                /* specular sampling jitter */
48
49 int  backvis = 1;                       /* back face visibility */
50
51 int  maxdepth = 4;                      /* maximum recursion depth */
52 double  minweight = 1e-2;               /* minimum ray weight */
53
54 COLOR  ambval = BLKCOLOR;               /* ambient value */
55 int  ambvwt = 0;                        /* initial weight for ambient value */
56 double  ambacc = 0.2;                   /* ambient accuracy */
57 int  ambres = 8;                        /* ambient resolution */
58 int  ambdiv = 32;                       /* ambient divisions */
59 int  ambssamp = 0;                      /* ambient super-samples */
60 int  ambounce = 0;                      /* ambient bounces */
61 char  *amblist[128];                    /* ambient include/exclude list */
62 int  ambincl = -1;                      /* include == 1, exclude == 0 */
63
64 int  greyscale = 0;                     /* map colors to brightness? */
65 char  *devname = dev_default;           /* output device name */
66
67 struct driver  *dev = NULL;             /* driver functions */
68
69 char  rifname[128];                     /* rad input file name */
70
71 VIEW  oldview;                          /* previous view parameters */
72
73 PNODE  ptrunk;                          /* the base of our image */
74 RECT  pframe;                           /* current frame boundaries */
75 int  pdepth;                            /* image depth in current frame */
76
77 static char  *reserve_mem = NULL;       /* pre-allocated reserve memory */
78
79 #define RESERVE_AMT     32768           /* amount of memory to reserve */
80
18   #define  CTRL(c)        ((c)-'@')
19  
20  
21 < quit(code)                      /* quit program */
22 < int  code;
21 > void
22 > devopen(                                /* open device driver */
23 >        char  *dname
24 > )
25   {
87 #ifdef MSTATS
88        if (code == 2 && errno == ENOMEM)
89                printmemstats(stderr);
90 #endif
91        devclose();
92        exit(code);
93 }
94
95
96 devopen(dname)                          /* open device driver */
97 char  *dname;
98 {
26          extern char  *progname, *octname;
27          char  *id;
28 <        register int  i;
28 >        int  i;
29  
30          id = octname!=NULL ? octname : progname;
31                                                  /* check device table */
32          for (i = 0; devtable[i].name; i++)
33 <                if (!strcmp(dname, devtable[i].name))
33 >                if (!strcmp(dname, devtable[i].name)) {
34                          if ((dev = (*devtable[i].init)(dname, id)) == NULL) {
35                                  sprintf(errmsg, "cannot initialize %s", dname);
36                                  error(USER, errmsg);
37                          } else
38                                  return;
39 < #ifndef NIX                                                
39 >                }
40                                                  /* not there, try exec */
41          if ((dev = comm_init(dname, id)) == NULL) {
42                  sprintf(errmsg, "cannot start device \"%s\"", dname);
43                  error(USER, errmsg);
44          }
118 #endif
45   }
46  
47  
48 < devclose()                              /* close our device */
48 > void
49 > devclose(void)                          /* close our device */
50   {
51          if (dev != NULL)
52                  (*dev->close)();
# Line 127 | Line 54 | devclose()                             /* close our device */
54   }
55  
56  
57 < printdevices()                          /* print list of output devices */
57 > void
58 > printdevices(void)                      /* print list of output devices */
59   {
60 <        register int  i;
60 >        int  i;
61  
62          for (i = 0; devtable[i].name; i++)
63                  printf("%-16s # %s\n", devtable[i].name, devtable[i].descrip);
64   }
65  
66  
67 < rview()                         /* do a view */
67 > void
68 > rview(void)                             /* do a view */
69   {
70          char  buf[32];
71  
72 <        devopen(devname);               /* open device */
73 <        newimage();                     /* start image (calls fillreserves) */
72 >        devopen(dvcname);               /* open device */
73 >        newimage(NULL);                 /* start image */
74  
75          for ( ; ; ) {                   /* quit in command() */
76                  while (hresolu <= 1<<pdepth && vresolu <= 1<<pdepth)
77                          command("done: ");
149                while (reserve_mem == NULL)
150                        command("out of memory: ");
78                  errno = 0;
79                  if (hresolu <= psample<<pdepth && vresolu <= psample<<pdepth) {
80                          sprintf(buf, "%d sampling...\n", 1<<pdepth);
# Line 156 | Line 83 | rview()                                /* do a view */
83                  } else {
84                          sprintf(buf, "%d refining...\n", 1<<pdepth);
85                          (*dev->comout)(buf);
86 <                        refine(&ptrunk, 0, 0, hresolu, vresolu, pdepth+1);
86 >                        refine(&ptrunk, pdepth+1);
87                  }
88 <                if (errno == ENOMEM)            /* ran out of memory */
162 <                        freereserves();
163 <                else if (dev->inpready)         /* noticed some input */
88 >                if (dev->inpready)              /* noticed some input */
89                          command(": ");
90                  else                            /* finished this depth */
91                          pdepth++;
# Line 168 | Line 93 | rview()                                /* do a view */
93   }
94  
95  
96 < fillreserves()                  /* fill memory reserves */
96 > void
97 > command(                        /* get/execute command */
98 >        char  *prompt
99 > )
100   {
173        if (reserve_mem != NULL)
174                return;
175        reserve_mem = malloc(RESERVE_AMT);
176 }
177
178
179 freereserves()                  /* free memory reserves */
180 {
181        if (reserve_mem == NULL)
182                return;
183        free(reserve_mem);
184        reserve_mem = NULL;
185 }
186
187
188 command(prompt)                 /* get/execute command */
189 char  *prompt;
190 {
101   #define  badcom(s)      strncmp(s, inpbuf, args-inpbuf-1)
102          char  inpbuf[256];
103          char  *args;
# Line 197 | Line 107 | again:
107                  ;
108          if (*args) *args++ = '\0';
109          else *++args = '\0';
110 +
111 +        if (waitrays() < 0)                     /* clear ray queue */
112 +                quit(1);
113          
114          switch (inpbuf[0]) {
115 <        case 'f':                               /* new frame (or free mem.) */
115 >        case 'f':                               /* new frame (|focus|free) */
116                  if (badcom("frame")) {
117 <                        if (badcom("free"))
118 <                                goto commerr;
119 <                        free_objmem();
117 >                        if (badcom("focus")) {
118 >                                if (badcom("free"))
119 >                                        goto commerr;
120 >                                free_objmem();
121 >                                break;
122 >                        }
123 >                        getfocus(args);
124                          break;
125                  }
126                  getframe(args);
# Line 246 | Line 163 | again:
163          case 'n':                               /* new picture */
164                  if (badcom("new"))
165                          goto commerr;
166 <                newimage();
166 >                newimage(args);
167                  break;
168          case 't':                               /* trace a ray */
169                  if (badcom("trace"))
# Line 313 | Line 230 | dostop:
230                  devclose();
231                  kill(0, SIGTSTP);
232                  /* pc stops here */
233 <                devopen(devname);
233 >                devopen(dvcname);
234                  redraw();
235                  break;
236   #endif
# Line 333 | Line 250 | commerr:
250   }
251  
252  
253 < rsample()                       /* sample the image */
253 > void
254 > rsample(void)                   /* sample the image */
255   {
256          int  xsiz, ysiz, y;
339        RECT  r;
257          PNODE  *p;
258 <        register RECT  *rl;
259 <        register PNODE  **pl;
343 <        register int  x;
258 >        PNODE   **pl;
259 >        int  x;
260          /*
261           *     We initialize the bottom row in the image at our current
262           * resolution.  During sampling, we check super-pixels to the
# Line 350 | Line 266 | rsample()                      /* sample the image */
266           */
267          xsiz = (((long)(pframe.r-pframe.l)<<pdepth)+hresolu-1) / hresolu;
268          ysiz = (((long)(pframe.u-pframe.d)<<pdepth)+vresolu-1) / vresolu;
353        rl = (RECT *)malloc(xsiz*sizeof(RECT));
354        if (rl == NULL)
355                return;
269          pl = (PNODE **)malloc(xsiz*sizeof(PNODE *));
270 <        if (pl == NULL) {
358 <                free((char *)rl);
270 >        if (pl == NULL)
271                  return;
360        }
272          /*
273           * Initialize the bottom row.
274           */
275 <        rl[0].l = rl[0].d = 0;
365 <        rl[0].r = hresolu; rl[0].u = vresolu;
366 <        pl[0] = findrect(pframe.l, pframe.d, &ptrunk, rl, pdepth);
275 >        pl[0] = findrect(pframe.l, pframe.d, &ptrunk, pdepth);
276          for (x = 1; x < xsiz; x++) {
368                rl[x].l = rl[x].d = 0;
369                rl[x].r = hresolu; rl[x].u = vresolu;
277                  pl[x] = findrect(pframe.l+((x*hresolu)>>pdepth),
278 <                                pframe.d, &ptrunk, rl+x, pdepth);
278 >                                pframe.d, &ptrunk, pdepth);
279          }
280                                                  /* sample the image */
281          for (y = 0; /* y < ysiz */ ; y++) {
# Line 380 | Line 287 | rsample()                      /* sample the image */
287                           */
288                          if (pl[x] != pl[x+1] && bigdiff(pl[x]->v,
289                                          pl[x+1]->v, maxdiff)) {
290 <                                refine(pl[x], rl[x].l, rl[x].d,
291 <                                                rl[x].r, rl[x].u, 1);
385 <                                refine(pl[x+1], rl[x+1].l, rl[x+1].d,
386 <                                                rl[x+1].r, rl[x+1].u, 1);
290 >                                refine(pl[x], 1);
291 >                                refine(pl[x+1], 1);
292                          }
293                  }
294                  if (y >= ysiz-1)
# Line 394 | Line 299 | rsample()                      /* sample the image */
299                          /*
300                           * Find super-pixel at this position in next row.
301                           */
397                        r.l = r.d = 0;
398                        r.r = hresolu; r.u = vresolu;
302                          p = findrect(pframe.l+((x*hresolu)>>pdepth),
303                                  pframe.d+(((y+1)*vresolu)>>pdepth),
304 <                                        &ptrunk, &r, pdepth);
304 >                                        &ptrunk, pdepth);
305                          /*
306                           * Test super-pixel in next row.
307                           */
308                          if (pl[x] != p && bigdiff(pl[x]->v, p->v, maxdiff)) {
309 <                                refine(pl[x], rl[x].l, rl[x].d,
310 <                                                rl[x].r, rl[x].u, 1);
408 <                                refine(p, r.l, r.d, r.r, r.u, 1);
309 >                                refine(pl[x], 1);
310 >                                refine(p, 1);
311                          }
312                          /*
313                           * Copy into super-pixel array.
314                           */
413                        rl[x].l = r.l; rl[x].d = r.d;
414                        rl[x].r = r.r; rl[x].u = r.u;
315                          pl[x] = p;
316                  }
317          }
318   escape:
319 <        free((char *)rl);
420 <        free((char *)pl);
319 >        free((void *)pl);
320   }
321  
322  
323   int
324 < refine(p, xmin, ymin, xmax, ymax, pd)           /* refine a node */
325 < register PNODE  *p;
326 < int  xmin, ymin, xmax, ymax;
327 < int  pd;
324 > refine(                         /* refine a node */
325 >        PNODE   *p,
326 >        int  pd
327 > )
328   {
329          int  growth;
330          int  mx, my;
# Line 437 | Line 336 | int  pd;
336          if (pd <= 0)                            /* depth limit */
337                  return(0);
338  
339 <        mx = (xmin + xmax) >> 1;
340 <        my = (ymin + ymax) >> 1;
339 >        mx = (p->xmin + p->xmax) >> 1;
340 >        my = (p->ymin + p->ymax) >> 1;
341          growth = 0;
342  
343          if (p->kid == NULL) {                   /* subdivide */
344  
345                  if ((p->kid = newptree()) == NULL)
346                          return(0);
347 +
348 +                p->kid[UR].xmin = mx;
349 +                p->kid[UR].ymin = my;
350 +                p->kid[UR].xmax = p->xmax;
351 +                p->kid[UR].ymax = p->ymax;
352 +                p->kid[UL].xmin = p->xmin;
353 +                p->kid[UL].ymin = my;
354 +                p->kid[UL].xmax = mx;
355 +                p->kid[UL].ymax = p->ymax;
356 +                p->kid[DR].xmin = mx;
357 +                p->kid[DR].ymin = p->ymin;
358 +                p->kid[DR].xmax = p->xmax;
359 +                p->kid[DR].ymax = my;
360 +                p->kid[DL].xmin = p->xmin;
361 +                p->kid[DL].ymin = p->ymin;
362 +                p->kid[DL].xmax = mx;
363 +                p->kid[DL].ymax = my;
364                  /*
365                   *  The following paint order can leave a black pixel
366                   *  if redraw() is called in (*dev->paintr)().
367                   */
368                  if (p->x >= mx && p->y >= my)
369                          pcopy(p, p->kid+UR);
370 <                else
371 <                        paint(p->kid+UR, mx, my, xmax, ymax);
370 >                else if (paint(p->kid+UR) < 0)
371 >                        quit(1);
372                  if (p->x < mx && p->y >= my)
373                          pcopy(p, p->kid+UL);
374 <                else
375 <                        paint(p->kid+UL, xmin, my, mx, ymax);
374 >                else if (paint(p->kid+UL) < 0)
375 >                        quit(1);
376                  if (p->x >= mx && p->y < my)
377                          pcopy(p, p->kid+DR);
378 <                else
379 <                        paint(p->kid+DR, mx, ymin, xmax, my);
378 >                else if (paint(p->kid+DR) < 0)
379 >                        quit(1);
380                  if (p->x < mx && p->y < my)
381                          pcopy(p, p->kid+DL);
382 <                else
383 <                        paint(p->kid+DL, xmin, ymin, mx, my);
382 >                else if (paint(p->kid+DL) < 0)
383 >                        quit(1);
384  
385                  growth++;
386          }
387                                                  /* do children */
388          if (mx > pframe.l) {
389                  if (my > pframe.d)
390 <                        growth += refine(p->kid+DL, xmin, ymin, mx, my, pd-1);
390 >                        growth += refine(p->kid+DL, pd-1);
391                  if (my < pframe.u)
392 <                        growth += refine(p->kid+UL, xmin, my, mx, ymax, pd-1);
392 >                        growth += refine(p->kid+UL, pd-1);
393          }
394          if (mx < pframe.r) {
395                  if (my > pframe.d)
396 <                        growth += refine(p->kid+DR, mx, ymin, xmax, my, pd-1);
396 >                        growth += refine(p->kid+DR, pd-1);
397                  if (my < pframe.u)
398 <                        growth += refine(p->kid+UR, mx, my, xmax, ymax, pd-1);
483 <        }
484 <                                                /* recompute sum */
485 <        if (growth) {
486 <                setcolor(p->v, 0.0, 0.0, 0.0);
487 <                for (i = 0; i < 4; i++)
488 <                        addcolor(p->v, p->kid[i].v);
489 <                scalecolor(p->v, 0.25);
398 >                        growth += refine(p->kid+UR, pd-1);
399          }
400          return(growth);
401   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines