-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3dClip.c
303 lines (275 loc) · 8.84 KB
/
3dClip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*****************************************************************************
* file: "3dClipping.c"
*
* purpose: Implements point and line canonical clipping to a perspective
* view volume bounded by a near (hither), far (yon) and as an
* option top, bottom,left and right (at 90¡ angles) planes.
*
* ©1990 Mark M. Owen. All rights reserved.
*****************************************************************************
*/
#include "xvt.h"
#include "3dClip.h"
#include "3dFMath.h"
BOOLEAN canonicalClip = TRUE; /* clip to 90¡ pyramid? */
#if XVT_CC_PROTO
static void PushDown (Point3d *,Point3d *);
static void PushUp (Point3d *,Point3d *);
static void PushLeft (Point3d *,Point3d *);
static void PushRight (Point3d *,Point3d *);
static void PushBeyond (Point3d *,Point3d *);
static void PushBehind (Point3d *,Point3d *);
static void Push (ClipBits *,Point3d *,Point3d *);
static void ClipEncode (Point3d *,ClipBits *);
#else
static void PushDown ();
static void PushUp ();
static void PushLeft ();
static void PushRight ();
static void PushBeyond ();
static void PushBehind ();
static void Push ();
static void ClipEncode ();
#endif
void
#if XVT_CC_PROTO
SetCanonicalClipping(BOOLEAN state)
#else
SetCanonicalClipping(state)
BOOLEAN state;
#endif
/*****************************************************************************
* purpose: Enables or disables clipping to the top, bottom, left and
* right planes of the 90¡ view pyramid.
*****************************************************************************/
{
canonicalClip = state;
}
static void PushDown(S,E) /* inverted y axis by viewer transform */
register Point3d *S,*E;
/*****************************************************************************
* purpose: moves the start point of a line segment to its intercept
* with the plane y=z (the top clipping plane).
*****************************************************************************/
{
register Fixed k;
Point3d s,e,r;
s = *S;
e = *E;
k = fixdiv(s.z + s.y,(s.y + s.z) - (e.y + e.z));
r.x = fixmul((e.x - s.x),k) + s.x;
r.y = fixmul((e.y - s.y),k) + s.y;
r.z = -r.y;
*S = r;
}
static void PushUp(S,E) /* inverted y axis by viewer transform */
register Point3d *S,*E;
/*****************************************************************************
* purpose: moves the start point of a line segment to its intercept
* with the plane y=-z (the bottom clipping plane).
*****************************************************************************/
{
register Fixed k;
Point3d s,e,r;
s = *S;
e = *E;
k = fixdiv(s.z - s.y,(e.y + s.z) - (s.y + e.z));
r.x = fixmul((e.x - s.x),k) + s.x;
r.y = fixmul((e.y - s.y),k) + s.y;
r.z = r.y;
*S = r;
}
static void PushLeft(S,E)
register Point3d *S,*E;
/*****************************************************************************
* purpose: moves the start point of a line segment to its intercept
* with the plane x=z (the right clipping plane).
*****************************************************************************/
{
register Fixed k;
Point3d s,e,r;
s = *S;
e = *E;
k = fixdiv(s.z - s.x,(e.x + s.z) - (s.x + e.z));
r.x = fixmul((e.x - s.x),k) + s.x;
r.y = fixmul((e.y - s.y),k) + s.y;
r.z = r.x;
*S = r;
}
static void PushRight(S,E)
register Point3d *S,*E;
/*****************************************************************************
* purpose: moves the start point of a line segment to its intercept
* with the plane x=-z (the left clipping plane).
*****************************************************************************/
{
register Fixed k;
Point3d s,e,r;
s = *S;
e = *E;
k = fixdiv(s.z + s.x,(s.x + s.z) - (e.x + e.z));
r.x = fixmul((e.x - s.x),k) + s.x;
r.y = fixmul((e.y - s.y),k) + s.y;
r.z = -r.x;
*S = r;
}
static void PushBeyond(S,E)
register Point3d *S,*E;
/*****************************************************************************
* purpose: moves the start point of a line segment to its intercept
* with the plane hither=z (the near clipping plane).
*****************************************************************************/
{
register Fixed m1,m2;
Point3d d,s,e;
s = *S;
e = *E;
d.x = e.x - s.x;
d.y = e.y - s.y;
d.z = e.z - s.z;
m1 = fixdiv(d.y,d.z); /* y/z slope */
m2 = fixdiv(d.x,d.z); /* x/z slope */
S->x= s.x+fixmul(m2,hither-s.z);
S->y= s.y+fixmul(m1,hither-s.z);
S->z= hither;
}
static void PushBehind(S,E)
register Point3d *S,*E;
/*****************************************************************************
* purpose: moves the start point of a line segment to its intercept
* with the plane yon=z (the far clipping plane).
*****************************************************************************/
{
register Fixed m1,m2;
Point3d d,s,e;
s = *S;
e = *E;
d.x = e.x - s.x;
d.y = e.y - s.y;
d.z = e.z - s.z;
m1 = fixdiv(d.y,d.z); /* y/z slope */
m2 = fixdiv(d.x,d.z); /* x/z slope */
S->x= s.x+fixmul(m2,yon-s.z);
S->y= s.y+fixmul(m1,yon-s.z);
S->z= yon;
}
static void Push(cb, p1, p2)
register ClipBits *cb;
register Point3d *p1,*p2;
/*****************************************************************************
* purpose: based on the start point's location pushes the point to
* the appropriate clipping plane.
*****************************************************************************/
{
if (cb->behind)
PushBeyond(p1,p2);
else if (cb->beyond)
PushBehind(p1,p2);
else if (cb->above)
PushDown(p1,p2);
else if (cb->below)
PushUp(p1,p2);
else if (cb->right)
PushLeft(p1,p2);
else if (cb->left)
PushRight(p1,p2);
}
static void ClipEncode(p,cb)
register Point3d *p;
register ClipBits *cb;
/*****************************************************************************
* purpose: classifies a point's location relative to the six clipping
* planes (y=z,y=-z,x=z,x=-z,z=hither,z=yon) when canonical
* clipping is enabled; or to the far and near planes (z=yon
* and z=hither) if canonical clipping is disabled.
*
* method: based on Cohen-Sutherland algorithm.
*
*****************************************************************************/
{
register Fixed z;
z = FxAbs(p->z);
if ( canonicalClip )
{
cb->left = (p->x < -z);
cb->right = (p->x > z);
cb->above = (p->y < -z);
cb->below = (p->y > z);
}
else
{
cb->left = FALSE;
cb->right = FALSE;
cb->above = FALSE;
cb->below = FALSE;
}
cb->behind = (p->z < hither);
cb->beyond = (p->z > yon);
cb->b7 =
cb->b8 = 0;
}
enum visibility ClipLine(startPt, endPt)
register Point3d *startPt, *endPt;
/*****************************************************************************
* purpose: determines if a line segment intersects the view volume,
* and if so clips it to the view volume.
*
* warning: it is expected that the start and end points have been
* transformed prior to calling "ClipLine".
*****************************************************************************/
{
register int i;
union startClipBits
{
ClipBits as_bits;
char as_char;
} startClipBits;
union endClipBits
{
ClipBits as_bits;
char as_char;
} endClipBits;
ClipEncode(startPt, &startClipBits.as_bits);
ClipEncode(endPt, &endClipBits.as_bits);
if(!(startClipBits.as_char | endClipBits.as_char))
return(is_visible);
if( (startClipBits.as_char & endClipBits.as_char))
return(isnt_visible);
i = 5;
while (i--)
{
if (startClipBits.as_char)
{
Push(&startClipBits.as_bits, startPt, endPt);
ClipEncode(startPt, &startClipBits.as_bits);
}
else if (endClipBits.as_char)
{
Push(&endClipBits.as_bits, endPt, startPt);
ClipEncode(endPt, &endClipBits.as_bits);
}
if (!(startClipBits.as_char | endClipBits.as_char))
return(is_visible);
if ( (startClipBits.as_char & endClipBits.as_char))
return(isnt_visible);
}
return (isnt_visible);
}
enum visibility ClipPt(Pt)
register Point3d *Pt;
/*****************************************************************************
* purpose: determines if a points lies within the view volume.
*
* warning: it is expected that the point has been transformed prior to
* calling "ClipPt".
*****************************************************************************/
{
union ptClipBits
{ ClipBits as_bits;
char as_char;
} ptClipBits;
ClipEncode(Pt,&ptClipBits.as_bits);
if(! ptClipBits.as_char )
return(is_visible);
return(isnt_visible);
}