ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhpict2.c
Revision: 3.2
Committed: Fri Mar 5 17:03:38 1999 UTC (25 years, 1 month ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.1: +239 -7 lines
Log Message:
added kernel and occlusion culling

File Contents

# User Rev Content
1 gwlarson 3.1 /* Copyright (c) 1999 Silicon Graphics, Inc. */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ SGI";
5     #endif
6    
7     /*
8     * Rendering routines for rhpict.
9     */
10    
11     #include "holo.h"
12     #include "view.h"
13    
14 gwlarson 3.2 #ifndef DEPS
15     #define DEPS 0.01 /* depth epsilon */
16     #endif
17     #ifndef MAXRAD
18     #define MAXRAD 64 /* maximum kernel radius */
19     #endif
20     #ifndef NNEIGH
21     #define NNEIGH 7 /* find this many neighbors */
22     #endif
23    
24     #define NINF 16382
25    
26     #define MAXRAD2 (MAXRAD*MAXRAD+1)
27    
28     #define G0NORM 0.286 /* ground zero normalization (1/x integral) */
29    
30     #ifndef FL4OP
31     #define FL4OP(f,i,op) ((f)[(i)>>5] op (1L<<((i)&0x1f)))
32     #define CHK4(f,i) FL4OP(f,i,&)
33     #define SET4(f,i) FL4OP(f,i,|=)
34     #define CLR4(f,i) FL4OP(f,i,&=~)
35     #define TGL4(f,i) FL4OP(f,i,^=)
36     #define FL4NELS(n) (((n)+0x1f)>>5)
37     #define CLR4ALL(f,n) bzero((char *)(f),FL4NELS(n)*sizeof(int4))
38     #endif
39    
40     static int4 *pixFlags; /* pixel occupancy flags */
41     static float pixWeight[MAXRAD2]; /* pixel weighting function */
42    
43 gwlarson 3.1 extern VIEW myview; /* current output view */
44     extern COLOR *mypixel; /* pixels being rendered */
45     extern float *myweight; /* weights (used to compute final pixels) */
46 gwlarson 3.2 extern float *mydepth; /* depth values (visibility culling) */
47 gwlarson 3.1 extern int hres, vres; /* current horizontal and vertical res. */
48    
49    
50 gwlarson 3.2 pixBeam(bp, hb) /* render a particular beam */
51 gwlarson 3.1 BEAM *bp;
52     register HDBEAMI *hb;
53     {
54     GCOORD gc[2];
55     register RAYVAL *rv;
56     FVECT rorg, rdir, wp, ip;
57     double d, prox;
58     COLOR col;
59 gwlarson 3.2 int n;
60     register int4 p;
61 gwlarson 3.1
62     if (!hdbcoord(gc, hb->h, hb->b))
63     error(CONSISTENCY, "bad beam in render_beam");
64     for (n = bp->nrm, rv = hdbray(bp); n--; rv++) {
65     /* reproject each sample */
66     hdray(rorg, rdir, hb->h, gc, rv->r);
67     if (rv->d < DCINF) {
68     d = hddepth(hb->h, rv->d);
69     VSUM(wp, rorg, rdir, d);
70     VSUB(ip, wp, myview.vp);
71     d = DOT(ip,rdir);
72 gwlarson 3.2 prox = d*d/DOT(ip,ip); /* cos(diff_angle)^32 */
73     prox *= prox; prox *= prox; prox *= prox; prox *= prox;
74 gwlarson 3.1 } else {
75     if (myview.type == VT_PAR || myview.vaft > FTINY)
76     continue; /* inf. off view */
77     VSUM(wp, myview.vp, rdir, FHUGE);
78 gwlarson 3.2 prox = 1.;
79 gwlarson 3.1 }
80     viewloc(ip, &myview, wp); /* frustum clipping */
81     if (ip[2] < 0.)
82     continue;
83     if (ip[0] < 0. || ip[0] >= 1.)
84     continue;
85     if (ip[1] < 0. || ip[1] >= 1.)
86     continue;
87     if (myview.vaft > FTINY && ip[2] > myview.vaft - myview.vfore)
88 gwlarson 3.2 continue; /* not exact for VT_PER */
89 gwlarson 3.1 p = (int)(ip[1]*vres)*hres + (int)(ip[0]*hres);
90 gwlarson 3.2 if (mydepth[p] > FTINY) { /* check depth */
91     if (ip[2] > mydepth[p]*(1.+DEPS))
92     continue;
93     if (ip[2] < mydepth[p]*(1.-DEPS)) {
94     setcolor(mypixel[p], 0., 0., 0.);
95     myweight[p] = 0.;
96     }
97     }
98 gwlarson 3.1 colr_color(col, rv->v);
99 gwlarson 3.2 scalecolor(col, prox);
100 gwlarson 3.1 addcolor(mypixel[p], col);
101 gwlarson 3.2 myweight[p] += prox;
102     mydepth[p] = ip[2];
103 gwlarson 3.1 }
104 gwlarson 3.2 }
105    
106    
107     int
108     kill_occl(h, v, nl, n) /* check for occlusion errors */
109     int h, v;
110     short nl[NNEIGH][2];
111     int n;
112     {
113     short forequad[2][2];
114     int d;
115     register int4 i;
116    
117     if (n <= 0)
118     return(1);
119     forequad[0][0] = forequad[0][1] = forequad[1][0] = forequad[1][1] = 0;
120     for (i = n; i--; ) {
121     d = (h-nl[i][0])*(h-nl[i][0]) + (v-nl[i][1])*(v-nl[i][1]);
122     if (mydepth[nl[i][1]*hres+nl[i][0]] <
123     mydepth[v*hres+h]*(1.-DEPS*d))
124     forequad[nl[i][0]<h][nl[i][1]<v] = 1;
125     }
126     if (forequad[0][0]+forequad[0][1]+forequad[1][0]+forequad[1][1] > 1) {
127     i = v*hres + h;
128     setcolor(mypixel[i], 0., 0., 0.);
129     myweight[i] = 0.; /* occupancy reset afterwards */
130     }
131     return(1);
132     }
133    
134    
135     int
136     grow_samp(h, v, nl, n) /* grow sample point appropriately */
137     int h, v;
138     register short nl[NNEIGH][2];
139     int n;
140     {
141     COLOR mykern[MAXRAD2];
142     float mykw[MAXRAD2];
143     int4 maxr2;
144     double w;
145     register int4 p, r2;
146     int maxr, h2, v2;
147    
148     if (n <= 0)
149     return(1);
150     p = v*hres + h; /* build kernel values */
151     maxr2 = (h-nl[n-1][0])*(h-nl[n-1][0]) + (v-nl[n-1][1])*(v-nl[n-1][1]);
152     DCHECK(maxr2>=MAXRAD2, CONSISTENCY, "out of range neighbor");
153     for (r2 = maxr2+1; --r2; ) {
154     copycolor(mykern[r2], mypixel[p]);
155     mykw[r2] = pixWeight[r2];
156     if (2*r2 >= maxr2) /* soften skirt */
157     mykw[r2] *= (2*(maxr2-r2)+1.0)/maxr2;
158     scalecolor(mykern[r2], mykw[r2]);
159     }
160     maxr = sqrt((double)maxr2) + .99; /* stamp out that kernel */
161     for (v2 = v-maxr; v2 <= v+maxr; v2++) {
162     if (v2 < 0) continue;
163     if (v2 >= vres) break;
164     for (h2 = h-maxr; h2 <= h+maxr; h2++) {
165     if (h2 < 0) continue;
166     if (h2 >= hres) break;
167     r2 = (v2-v)*(v2-v) + (h2-h)*(h2-h);
168     if (r2 > maxr2) continue;
169     if (CHK4(pixFlags, v2*hres+h2))
170     continue; /* occupied */
171     addcolor(mypixel[v2*hres+h2], mykern[r2]);
172     myweight[v2*hres+h2] += mykw[r2]*myweight[v*hres+h];
173     }
174     }
175     return(1);
176     }
177    
178    
179     pixFlush() /* done with beams -- flush pixel values */
180     {
181     reset_flags(); /* set occupancy flags */
182     meet_neighbors(kill_occl); /* eliminate occlusion errors */
183     reset_flags(); /* reset occupancy flags */
184     if (pixWeight[0] <= FTINY) { /* initialize weighting function */
185     register int r;
186     for (r = MAXRAD2; --r; )
187     pixWeight[r] = G0NORM/sqrt((double)r);
188     pixWeight[0] = 1.;
189     }
190     meet_neighbors(grow_samp); /* grow valid samples over image */
191     free((char *)pixFlags); /* free pixel flags */
192     pixFlags = NULL;
193     }
194    
195    
196     reset_flags() /* allocate/set/reset occupancy flags */
197     {
198     register int p;
199    
200     if (pixFlags == NULL) {
201     pixFlags = (int4 *)calloc(FL4NELS(hres*vres), sizeof(int4));
202     CHECK(pixFlags==NULL, SYSTEM, "out of memory in reset_flags");
203     } else
204     CLR4ALL(pixFlags, hres*vres);
205     for (p = hres*vres; p--; )
206     if (myweight[p] > FTINY)
207     SET4(pixFlags, p);
208     }
209    
210    
211     int
212     findneigh(nl, h, v, rnl) /* find NNEIGH neighbors for pixel */
213     short nl[NNEIGH][2];
214     int h, v;
215     register short (*rnl)[NNEIGH];
216     {
217     int nn = 0;
218     int4 d, ld, nd[NNEIGH+1];
219     int n, hoff;
220     register int h2, n2;
221    
222     ld = MAXRAD2;
223     for (hoff = 1; hoff < hres; hoff = (hoff<0) - hoff) {
224     h2 = h + hoff;
225     if (h2 < 0 | h2 >= hres)
226     continue;
227     if ((h2-h)*(h2-h) >= ld)
228     break;
229     for (n = 0; n < NNEIGH && rnl[h2][n] < NINF; n++) {
230     d = (h2-h)*(h2-h) + (v-rnl[h2][n])*(v-rnl[h2][n]);
231     if (d >= ld)
232     continue;
233     for (n2 = nn; ; n2--) { /* insert neighbor */
234     if (!n2 || d >= nd[n2-1]) {
235     nd[n2] = d;
236     nl[n2][0] = h2;
237     nl[n2][1] = rnl[h2][n];
238     break;
239     }
240     nd[n2] = nd[n2-1];
241     nl[n2][0] = nl[n2-1][0];
242     nl[n2][1] = nl[n2-1][1];
243     }
244     if (nn < NNEIGH)
245     nn++;
246     else
247     ld = nd[NNEIGH-1];
248     }
249     }
250     return(nn);
251     }
252    
253    
254     meet_neighbors(nf) /* run through samples and their neighbors */
255     int (*nf)();
256     {
257     short ln[NNEIGH][2];
258     int h, v, n, v2;
259     register short (*rnl)[NNEIGH];
260     /* initialize bottom row list */
261     rnl = (short (*)[NNEIGH])malloc(NNEIGH*sizeof(short)*hres);
262     CHECK(rnl==NULL, SYSTEM, "out of memory in meet_neighbors");
263     for (h = 0; h < hres; h++) {
264     for (n = v = 0; v < vres; v++)
265     if (CHK4(pixFlags, v*hres+h)) {
266     rnl[h][n++] = v;
267     if (n >= NNEIGH)
268     break;
269     }
270     while (n < NNEIGH)
271     rnl[h][n++] = NINF;
272     }
273     v = 0; /* do each row */
274     for ( ; ; ) {
275     for (h = 0; h < hres; h++) {
276     if (!CHK4(pixFlags, v*hres+h))
277     continue; /* no one home */
278     n = findneigh(ln, h, v, rnl);
279     (*nf)(h, v, ln, n); /* call on neighbors */
280     }
281     if (++v >= vres) /* reinitialize row list */
282     break;
283     for (h = 0; h < hres; h++)
284     for (v2 = rnl[h][NNEIGH-1]+1; v2 < vres; v2++) {
285     if (v2 - v > v - rnl[h][0])
286     break; /* not close enough */
287     if (CHK4(pixFlags, v2*hres+h)) {
288     for (n = 0; n < NNEIGH-1; n++)
289     rnl[h][n] = rnl[h][n+1];
290     rnl[h][NNEIGH-1] = v2;
291     }
292     }
293     }
294     free((char *)rnl); /* free row list */
295 gwlarson 3.1 }