1 |
/* Copyright (c) 1997 Silicon Graphics, Inc. */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ SGI"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* Holodeck beam support for display process |
9 |
*/ |
10 |
|
11 |
#include "rholo.h" |
12 |
#include "rhdisp.h" |
13 |
#include "view.h" |
14 |
|
15 |
|
16 |
int |
17 |
npixels(vp, hr, vr, hp, bi) /* compute appropriate number to evaluate */ |
18 |
VIEW *vp; |
19 |
int hr, vr; |
20 |
HOLO *hp; |
21 |
int bi; |
22 |
{ |
23 |
GCOORD gc[2]; |
24 |
FVECT cp[4]; |
25 |
FVECT ip[4]; |
26 |
double d; |
27 |
register int i; |
28 |
/* compute cell corners in image */ |
29 |
if (!hdbcoord(gc, hp, bi)) |
30 |
error(CONSISTENCY, "bad beam index in npixels"); |
31 |
hdcell(cp, hp, gc+1); |
32 |
for (i = 0; i < 4; i++) { |
33 |
viewloc(ip[i], vp, cp[i]); |
34 |
if (ip[i][2] < 0.) |
35 |
return(0); |
36 |
ip[i][0] *= (double)hr; /* scale by resolution */ |
37 |
ip[i][1] *= (double)vr; |
38 |
} |
39 |
/* compute quad area */ |
40 |
d = (ip[1][0]-ip[0][0])*(ip[2][1]-ip[0][1]) - |
41 |
(ip[2][0]-ip[0][0])*(ip[1][1]-ip[0][1]); |
42 |
d += (ip[2][0]-ip[3][0])*(ip[1][1]-ip[3][1]) - |
43 |
(ip[1][0]-ip[3][0])*(ip[2][1]-ip[3][1]); |
44 |
if (d < 0) |
45 |
d = -d; |
46 |
/* round off result */ |
47 |
return((int)(.5*d+.5)); |
48 |
} |
49 |
|
50 |
|
51 |
/* |
52 |
* The ray directions that define the pyramid in visit_cells() needn't |
53 |
* be normalized, but they must be given in clockwise order as seen |
54 |
* from the pyramid's apex (origin). |
55 |
*/ |
56 |
int |
57 |
visit_cells(orig, pyrd, hp, vf, dp) /* visit cells within a pyramid */ |
58 |
FVECT orig, pyrd[4]; /* pyramid ray directions in clockwise order */ |
59 |
HOLO *hp; |
60 |
int (*vf)(); |
61 |
char *dp; |
62 |
{ |
63 |
int n = 0; |
64 |
int inflags = 0; |
65 |
FVECT gp, pn[4], lo, ld; |
66 |
double po[4], lbeg, lend, d, t; |
67 |
GCOORD gc; |
68 |
register int i; |
69 |
/* figure out whose side we're on */ |
70 |
hdgrid(gp, hp, orig); |
71 |
for (i = 0; i < 3; i++) { |
72 |
inflags |= (gp[i] > FTINY) << (i<<1); |
73 |
inflags |= (gp[i] < hp->grid[i]-FTINY) << (i<<1 | 1); |
74 |
} |
75 |
/* compute pyramid planes */ |
76 |
for (i = 0; i < 4; i++) { |
77 |
fcross(pn[i], pyrd[i], pyrd[(i+1)&03]); |
78 |
po[i] = DOT(pn[i], orig); |
79 |
} |
80 |
/* traverse each wall */ |
81 |
for (gc.w = 0; gc.w < 6; gc.w++) { |
82 |
if (!(inflags & 1<<gc.w)) /* origin on wrong side */ |
83 |
continue; |
84 |
/* scanline algorithm */ |
85 |
for (gc.i[1] = hp->grid[((gc.w>>1)+2)%3]; gc.i[1]--; ) { |
86 |
/* compute scanline */ |
87 |
gp[gc.w>>1] = gc.w&1 ? hp->grid[gc.w>>1] : 0; |
88 |
gp[((gc.w>>1)+1)%3] = 0; |
89 |
gp[((gc.w>>1)+2)%3] = gc.i[1] + 0.5; |
90 |
hdworld(lo, hp, gp); |
91 |
gp[((gc.w>>1)+1)%3] = 1; |
92 |
hdworld(ld, hp, gp); |
93 |
ld[0] -= lo[0]; ld[1] -= lo[1]; ld[2] -= lo[2]; |
94 |
/* find scanline limits */ |
95 |
lbeg = 0; lend = hp->grid[((gc.w>>1)+1)%3]; |
96 |
for (i = 0; i < 4; i++) { |
97 |
t = DOT(pn[i], lo) - po[i]; |
98 |
d = -DOT(pn[i], ld); |
99 |
if (d > FTINY) { /* <- plane */ |
100 |
if ((t /= d) < lend) |
101 |
lend = t; |
102 |
} else if (d < -FTINY) { /* plane -> */ |
103 |
if ((t /= d) > lbeg) |
104 |
lbeg = t; |
105 |
} else if (t < 0) /* outside */ |
106 |
goto nextscan; |
107 |
} |
108 |
i = lend + .5; /* visit cells on this scan */ |
109 |
for (gc.i[0] = lbeg + .5; gc.i[0] < i; gc.i[0]++) |
110 |
n += (*vf)(&gc, dp); |
111 |
nextscan:; |
112 |
} |
113 |
} |
114 |
return(n); |
115 |
} |
116 |
|
117 |
|
118 |
int |
119 |
addcell(gcp, cl) /* add a cell to a list */ |
120 |
GCOORD *gcp; |
121 |
register int *cl; |
122 |
{ |
123 |
copystruct((GCOORD *)(cl+1) + *cl, gcp); |
124 |
(*cl)++; |
125 |
return(1); |
126 |
} |
127 |
|
128 |
|
129 |
int |
130 |
cellcmp(gcp1, gcp2) /* visit_cells() cell ordering */ |
131 |
register GCOORD *gcp1, *gcp2; |
132 |
{ |
133 |
register int c; |
134 |
|
135 |
if ((c = gcp1->w - gcp2->w)) |
136 |
return(c); |
137 |
if ((c = gcp2->i[1] - gcp1->i[1])) /* wg1 is reverse-ordered */ |
138 |
return(c); |
139 |
return(gcp1->i[0] - gcp2->i[0]); |
140 |
} |
141 |
|
142 |
|
143 |
int * |
144 |
getviewcells(hp, vp) /* get ordered cell list for section view */ |
145 |
register HOLO *hp; |
146 |
VIEW *vp; |
147 |
{ |
148 |
FVECT org, dir[4]; |
149 |
int n; |
150 |
register int *cl; |
151 |
/* compute view pyramid */ |
152 |
if (vp->type == VT_PAR) goto viewerr; |
153 |
if (viewray(org, dir[0], vp, 0., 0.) < -FTINY) goto viewerr; |
154 |
if (viewray(org, dir[1], vp, 0., 1.) < -FTINY) goto viewerr; |
155 |
if (viewray(org, dir[2], vp, 1., 1.) < -FTINY) goto viewerr; |
156 |
if (viewray(org, dir[3], vp, 1., 0.) < -FTINY) goto viewerr; |
157 |
/* allocate enough list space */ |
158 |
n = 2*( hp->grid[0]*hp->grid[1] + |
159 |
hp->grid[0]*hp->grid[2] + |
160 |
hp->grid[1]*hp->grid[2] ); |
161 |
cl = (int *)malloc(sizeof(int) + n*sizeof(GCOORD)); |
162 |
if (cl == NULL) |
163 |
goto memerr; |
164 |
*cl = 0; |
165 |
/* add cells within pyramid */ |
166 |
visit_cells(org, dir, hp, addcell, cl); |
167 |
if (!*cl) { |
168 |
free((char *)cl); |
169 |
return(NULL); |
170 |
} |
171 |
#if 0 |
172 |
/* We're just going to free this memory in a moment, and list is |
173 |
* sorted automatically by visit_cells(), so we don't need this. |
174 |
*/ |
175 |
if (*cl < n) { /* optimize memory use */ |
176 |
cl = (int *)realloc((char *)cl, |
177 |
sizeof(int) + *cl*sizeof(GCOORD)); |
178 |
if (cl == NULL) |
179 |
goto memerr; |
180 |
} |
181 |
/* sort the list */ |
182 |
qsort((char *)(cl+1), *cl, sizeof(GCOORD), cellcmp); |
183 |
#endif |
184 |
return(cl); |
185 |
viewerr: |
186 |
error(INTERNAL, "unusable view in getviewcells"); |
187 |
memerr: |
188 |
error(SYSTEM, "out of memory in getviewcells"); |
189 |
} |