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 |
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[1]; |
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 && d >= -FTINY) { |
100 |
if (t < 0) |
101 |
goto nextscan; |
102 |
continue; |
103 |
} |
104 |
if (t > 0) { |
105 |
if ((t /= d) < lend) |
106 |
lend = t; |
107 |
} else { |
108 |
if ((t /= d) > lbeg) |
109 |
lbeg = t; |
110 |
} |
111 |
} |
112 |
i = lend + .5; /* visit cells on this scan */ |
113 |
for (gc.i[0] = lbeg + .5; gc.i[0] < i; gc.i[0]++) |
114 |
n += (*vf)(&gc, dp); |
115 |
nextscan:; |
116 |
} |
117 |
} |
118 |
return(n); |
119 |
} |
120 |
|
121 |
|
122 |
int |
123 |
addcell(gcp, cl) /* add a cell to a list */ |
124 |
GCOORD *gcp; |
125 |
register int *cl; |
126 |
{ |
127 |
copystruct((GCOORD *)(cl+1) + *cl, gcp); |
128 |
(*cl)++; |
129 |
return(1); |
130 |
} |
131 |
|
132 |
|
133 |
int |
134 |
cellcmp(gcp1, gcp2) /* visit_cells() cell ordering */ |
135 |
register GCOORD *gcp1, *gcp2; |
136 |
{ |
137 |
register int c; |
138 |
|
139 |
if ((c = gcp1->w - gcp2->w)) |
140 |
return(c); |
141 |
if ((c = gcp2->i[1] - gcp1->i[1])) /* wg1 is reverse-ordered */ |
142 |
return(c); |
143 |
return(gcp1->i[0] - gcp2->i[0]); |
144 |
} |
145 |
|
146 |
|
147 |
int * |
148 |
getviewcells(hp, vp) /* get ordered cell list for section view */ |
149 |
register HOLO *hp; |
150 |
VIEW *vp; |
151 |
{ |
152 |
FVECT org, dir[4]; |
153 |
int n; |
154 |
register int *cl; |
155 |
/* compute view pyramid */ |
156 |
if (vp->type == VT_PAR) goto viewerr; |
157 |
if (viewray(org, dir[0], vp, 0., 0.) < -FTINY) goto viewerr; |
158 |
if (viewray(org, dir[1], vp, 0., 1.) < -FTINY) goto viewerr; |
159 |
if (viewray(org, dir[2], vp, 1., 1.) < -FTINY) goto viewerr; |
160 |
if (viewray(org, dir[3], vp, 1., 0.) < -FTINY) goto viewerr; |
161 |
/* allocate enough list space */ |
162 |
n = 2*( hp->grid[0]*hp->grid[1] + |
163 |
hp->grid[0]*hp->grid[2] + |
164 |
hp->grid[1]*hp->grid[2] ); |
165 |
cl = (int *)malloc(sizeof(int) + n*sizeof(GCOORD)); |
166 |
if (cl == NULL) |
167 |
goto memerr; |
168 |
*cl = 0; |
169 |
/* add cells within pyramid */ |
170 |
visit_cells(org, dir, hp, addcell, cl); |
171 |
if (!*cl) { |
172 |
free((char *)cl); |
173 |
return(NULL); |
174 |
} |
175 |
#if 0 |
176 |
/* We're just going to free this memory in a moment, and list is */ |
177 |
/* sorted automatically by visit_cells(), so we don't need this. */ |
178 |
if (*cl < n) { /* optimize memory use */ |
179 |
cl = (int *)realloc((char *)cl, |
180 |
sizeof(int) + *cl*sizeof(GCOORD)); |
181 |
if (cl == NULL) |
182 |
goto memerr; |
183 |
} |
184 |
/* sort the list */ |
185 |
qsort((char *)(cl+1), *cl, sizeof(GCOORD), cellcmp); |
186 |
#endif |
187 |
return(cl); |
188 |
viewerr: |
189 |
error(INTERNAL, "unusable view in getviewcells"); |
190 |
memerr: |
191 |
error(SYSTEM, "out of memory in getviewcells"); |
192 |
} |