Debian Codegraph @

debian/external_deps/fetk/sg/src/gl/clip.c

raw
1 /* $Id: clip.c,v 1.2 2000/10/27 15:21:40 mholst Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 2.2
6 * Copyright (C) 1995-1997 Brian Paul
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the Free
20 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include <string.h>
24 #include "clip.h"
25 #include "context.h"
26 #include "dlist.h"
27 #include "macros.h"
28 #include "matrix.h"
29 #include "types.h"
30 #include "vb.h"
31 #include "xform.h"
32
33
34
35
36 /* Linear interpolation between A and B: */
37 #define LINTERP( T, A, B ) ( (A) + (T) * ( (B) - (A) ) )
38
39
40 #define EYE_SPACE 1
41 #define CLIP_SPACE 2
42
43 static GLuint Space;
44
45
46
47 /*
48 * This function is used to interpolate colors, indexes, and texture
49 * coordinates when clipping has to be done. In general, we compute
50 * aux[dst] = aux[in] + t * (aux[out] - aux[in])
51 * where aux is the quantity to be interpolated.
52 * Input: dst - index of array position to store interpolated value
53 * t - a value in [0,1]
54 * in - index of array position corresponding to 'inside' vertex
55 * out - index of array position corresponding to 'outside' vertex
56 */
57 static void interpolate_aux( GLcontext* ctx,
58 GLuint dst, GLfloat t, GLuint in, GLuint out )
59 {
60 struct vertex_buffer* VB = ctx->VB;
61
62 if (ctx->ClipMask & CLIP_FCOLOR_BIT) {
63 VB->Fcolor[dst][0] = LINTERP( t, VB->Fcolor[in][0], VB->Fcolor[out][0] );
64 VB->Fcolor[dst][1] = LINTERP( t, VB->Fcolor[in][1], VB->Fcolor[out][1] );
65 VB->Fcolor[dst][2] = LINTERP( t, VB->Fcolor[in][2], VB->Fcolor[out][2] );
66 VB->Fcolor[dst][3] = LINTERP( t, VB->Fcolor[in][3], VB->Fcolor[out][3] );
67 }
68 else if (ctx->ClipMask & CLIP_FINDEX_BIT) {
69 VB->Findex[dst] = (GLuint) (GLint) LINTERP( t, (GLfloat) VB->Findex[in],
70 (GLfloat) VB->Findex[out] );
71 }
72
73 if (ctx->ClipMask & CLIP_BCOLOR_BIT) {
74 VB->Bcolor[dst][0] = LINTERP( t, VB->Bcolor[in][0], VB->Bcolor[out][0] );
75 VB->Bcolor[dst][1] = LINTERP( t, VB->Bcolor[in][1], VB->Bcolor[out][1] );
76 VB->Bcolor[dst][2] = LINTERP( t, VB->Bcolor[in][2], VB->Bcolor[out][2] );
77 VB->Bcolor[dst][3] = LINTERP( t, VB->Bcolor[in][3], VB->Bcolor[out][3] );
78 }
79 else if (ctx->ClipMask & CLIP_BINDEX_BIT) {
80 VB->Bindex[dst] = (GLuint) (GLint) LINTERP( t, (GLfloat) VB->Bindex[in],
81 (GLfloat) VB->Bindex[out] );
82 }
83
84 if (ctx->ClipMask & CLIP_TEXTURE_BIT) {
85 /* TODO: is more sophisticated texture coord interpolation needed?? */
86 if (Space==CLIP_SPACE) {
87 /* also interpolate eye Z component */
88 VB->Eye[dst][2] = LINTERP( t, VB->Eye[in][2], VB->Eye[out][2] );
89 }
90 VB->TexCoord[dst][0] = LINTERP(t,VB->TexCoord[in][0],VB->TexCoord[out][0]);
91 VB->TexCoord[dst][1] = LINTERP(t,VB->TexCoord[in][1],VB->TexCoord[out][1]);
92 VB->TexCoord[dst][2] = LINTERP(t,VB->TexCoord[in][2],VB->TexCoord[out][2]);
93 VB->TexCoord[dst][3] = LINTERP(t,VB->TexCoord[in][3],VB->TexCoord[out][3]);
94 }
95
96 }
97
98
99
100
101 void gl_ClipPlane( GLcontext* ctx, GLenum plane, const GLfloat *equation )
102 {
103 GLint p;
104
105 p = (GLint) (plane - GL_CLIP_PLANE0);
106 if (p<0 || p>=MAX_CLIP_PLANES) {
107 gl_error( ctx, GL_INVALID_ENUM, "glClipPlane" );
108 return;
109 }
110
111 /*
112 * The Equation is transformed by the transpose of the inverse of the
113 * current modelview matrix and stored in the resulting eye coordinates.
114 */
115 if (!ctx->ModelViewInvValid) {
116 gl_compute_modelview_inverse(ctx);
117 }
118 gl_transform_vector( ctx->Transform.ClipEquation[p], equation,
119 ctx->ModelViewInv );
120 }
121
122
123
124 void gl_GetClipPlane( GLcontext* ctx, GLenum plane, GLdouble *equation )
125 {
126 GLint p;
127
128 if (INSIDE_BEGIN_END(ctx)) {
129 gl_error( ctx, GL_INVALID_OPERATION, "glGetClipPlane" );
130 return;
131 }
132
133 p = (GLint) (plane - GL_CLIP_PLANE0);
134 if (p<0 || p>=MAX_CLIP_PLANES) {
135 gl_error( ctx, GL_INVALID_ENUM, "glGetClipPlane" );
136 return;
137 }
138
139 equation[0] = (GLdouble) ctx->Transform.ClipEquation[p][0];
140 equation[1] = (GLdouble) ctx->Transform.ClipEquation[p][1];
141 equation[2] = (GLdouble) ctx->Transform.ClipEquation[p][2];
142 equation[3] = (GLdouble) ctx->Transform.ClipEquation[p][3];
143 }
144
145
146
147
148 /**********************************************************************/
149 /* View volume clipping. */
150 /**********************************************************************/
151
152
153 /*
154 * Clip a point against the view volume.
155 * Input: v - vertex-vector describing the point to clip
156 * Return: 0 = outside view volume
157 * 1 = inside view volume
158 */
159 GLuint gl_viewclip_point( const GLfloat v[] )
160 {
161 if ( v[0] > v[3] || v[0] < -v[3]
162 || v[1] > v[3] || v[1] < -v[3]
163 || v[2] > v[3] || v[2] < -v[3] ) {
164 return 0;
165 }
166 else {
167 return 1;
168 }
169 }
170
171
172
173
174 /*
175 * Clip a line segment against the view volume defined by -w<=x,y,z<=w.
176 * Input: i, j - indexes into VB->V* of endpoints of the line
177 * Return: 0 = line completely outside of view
178 * 1 = line is inside view.
179 */
180 GLuint gl_viewclip_line( GLcontext* ctx, GLuint *i, GLuint *j )
181 {
182 struct vertex_buffer* VB = ctx->VB;
183 GLfloat (*coord)[4] = VB->Clip;
184
185 GLfloat t, dx, dy, dz, dw;
186 register GLuint ii, jj;
187
188 Space = CLIP_SPACE;
189 ii = *i;
190 jj = *j;
191
192 /*
193 * We use 6 instances of this code to clip agains the 6 planes.
194 * For each plane, we define the OUTSIDE and COMPUTE_INTERSECTION
195 * macros apprpriately.
196 */
197 #define GENERAL_CLIP \
198 if (OUTSIDE(ii)) { \
199 if (OUTSIDE(jj)) { \
200 /* both verts are outside ==> return 0 */ \
201 return 0; \
202 } \
203 else { \
204 /* ii is outside, jj is inside ==> clip */ \
205 /* new vertex put in position VB->Free */ \
206 COMPUTE_INTERSECTION( VB->Free, jj, ii ) \
207 if (ctx->ClipMask) \
208 interpolate_aux( ctx, VB->Free, t, jj, ii ); \
209 ii = VB->Free; \
210 VB->Free++; \
211 if (VB->Free==VB_SIZE) VB->Free = 1; \
212 } \
213 } \
214 else { \
215 if (OUTSIDE(jj)) { \
216 /* ii is inside, jj is outside ==> clip */ \
217 /* new vertex put in position VB->Free */ \
218 COMPUTE_INTERSECTION( VB->Free, ii, jj ); \
219 if (ctx->ClipMask) \
220 interpolate_aux( ctx, VB->Free, t, ii, jj ); \
221 jj = VB->Free; \
222 VB->Free++; \
223 if (VB->Free==VB_SIZE) VB->Free = 1; \
224 } \
225 /* else both verts are inside ==> do nothing */ \
226 }
227
228
229 #define X(I) coord[I][0]
230 #define Y(I) coord[I][1]
231 #define Z(I) coord[I][2]
232 #define W(I) coord[I][3]
233
234 /*
235 * Begin clipping
236 */
237
238 /*** Clip against +X side ***/
239 #define OUTSIDE(K) (X(K) > W(K))
240 #define COMPUTE_INTERSECTION( new, in, out ) \
241 dx = X(out) - X(in); \
242 dw = W(out) - W(in); \
243 t = (X(in) - W(in)) / (dw-dx); \
244 X(new) = X(in) + t * dx; \
245 Y(new) = Y(in) + t * (Y(out) - Y(in)); \
246 Z(new) = Z(in) + t * (Z(out) - Z(in)); \
247 W(new) = W(in) + t * dw;
248
249 GENERAL_CLIP
250
251 #undef OUTSIDE
252 #undef COMPUTE_INTERSECTION
253
254
255 /*** Clip against -X side ***/
256 #define OUTSIDE(K) (X(K) < -W(K))
257 #define COMPUTE_INTERSECTION( new, in, out ) \
258 dx = X(out) - X(in); \
259 dw = W(out) - W(in); \
260 t = -(X(in) + W(in)) / (dw+dx); \
261 X(new) = X(in) + t * dx; \
262 Y(new) = Y(in) + t * (Y(out) - Y(in)); \
263 Z(new) = Z(in) + t * (Z(out) - Z(in)); \
264 W(new) = W(in) + t * dw;
265
266 GENERAL_CLIP
267
268 #undef OUTSIDE
269 #undef COMPUTE_INTERSECTION
270
271
272 /*** Clip against +Y side ***/
273 #define OUTSIDE(K) (Y(K) > W(K))
274 #define COMPUTE_INTERSECTION( new, in, out ) \
275 dy = Y(out) - Y(in); \
276 dw = W(out) - W(in); \
277 t = (Y(in) - W(in)) / (dw-dy); \
278 X(new) = X(in) + t * (X(out) - X(in)); \
279 Y(new) = Y(in) + t * dy; \
280 Z(new) = Z(in) + t * (Z(out) - Z(in)); \
281 W(new) = W(in) + t * dw;
282
283 GENERAL_CLIP
284
285 #undef OUTSIDE
286 #undef COMPUTE_INTERSECTION
287
288
289 /*** Clip against -Y side ***/
290 #define OUTSIDE(K) (Y(K) < -W(K))
291 #define COMPUTE_INTERSECTION( new, in, out ) \
292 dy = Y(out) - Y(in); \
293 dw = W(out) - W(in); \
294 t = -(Y(in) + W(in)) / (dw+dy); \
295 X(new) = X(in) + t * (X(out) - X(in)); \
296 Y(new) = Y(in) + t * dy; \
297 Z(new) = Z(in) + t * (Z(out) - Z(in)); \
298 W(new) = W(in) + t * dw;
299
300 GENERAL_CLIP
301
302 #undef OUTSIDE
303 #undef COMPUTE_INTERSECTION
304
305
306 /*** Clip against +Z side ***/
307 #define OUTSIDE(K) (Z(K) > W(K))
308 #define COMPUTE_INTERSECTION( new, in, out ) \
309 dz = Z(out) - Z(in); \
310 dw = W(out) - W(in); \
311 t = (Z(in) - W(in)) / (dw-dz); \
312 X(new) = X(in) + t * (X(out) - X(in)); \
313 Y(new) = Y(in) + t * (Y(out) - Y(in)); \
314 Z(new) = Z(in) + t * dz; \
315 W(new) = W(in) + t * dw;
316
317 GENERAL_CLIP
318
319 #undef OUTSIDE
320 #undef COMPUTE_INTERSECTION
321
322
323 /*** Clip against -Z side ***/
324 #define OUTSIDE(K) (Z(K) < -W(K))
325 #define COMPUTE_INTERSECTION( new, in, out ) \
326 dz = Z(out) - Z(in); \
327 dw = W(out) - W(in); \
328 t = -(Z(in) + W(in)) / (dw+dz); \
329 X(new) = X(in) + t * (X(out) - X(in)); \
330 Y(new) = Y(in) + t * (Y(out) - Y(in)); \
331 Z(new) = Z(in) + t * dz; \
332 W(new) = W(in) + t * dw;
333
334 GENERAL_CLIP
335
336 #undef OUTSIDE
337 #undef COMPUTE_INTERSECTION
338
339 #undef GENERAL_CLIP
340
341 *i = ii;
342 *j = jj;
343 return 1;
344 }
345
346
347
348
349 /*
350 * Clip a polygon against the view volume defined by -w<=x,y,z<=w.
351 * Input: n - number of vertices in input polygon.
352 * vlist - list of indexes into VB->V* of polygon to clip.
353 * Output: vlist - modified list of vertex indexes
354 * Return: number of vertices in resulting polygon
355 */
356 GLuint gl_viewclip_polygon( GLcontext* ctx, GLuint n, GLuint vlist[] )
357
358 {
359 struct vertex_buffer* VB = ctx->VB;
360 GLfloat (*coord)[4] = VB->Clip;
361
362 GLuint previ, prevj;
363 GLuint curri, currj;
364 GLuint vlist2[VB_SIZE];
365 GLuint n2;
366 GLdouble dx, dy, dz, dw, t, neww;
367
368 Space = CLIP_SPACE;
369
370 /*
371 * We use 6 instances of this code to implement clipping against the
372 * 6 sides of the view volume. Prior to each we define the macros:
373 * INLIST = array which lists input vertices
374 * OUTLIST = array which lists output vertices
375 * INCOUNT = variable which is the number of vertices in INLIST[]
376 * OUTCOUNT = variable which is the number of vertices in OUTLIST[]
377 * INSIDE(i) = test if vertex v[i] is inside the view volume
378 * COMPUTE_INTERSECTION(in,out,new) = compute intersection of line
379 * from v[in] to v[out] with the clipping plane and store
380 * the result in v[new]
381 */
382
383 #define GENERAL_CLIP \
384 if (INCOUNT<3) return 0; \
385 previ = INCOUNT-1; /* let previous = last vertex */ \
386 prevj = INLIST[previ]; \
387 OUTCOUNT = 0; \
388 for (curri=0;curri<INCOUNT;curri++) { \
389 currj = INLIST[curri]; \
390 if (INSIDE(currj)) { \
391 if (INSIDE(prevj)) { \
392 /* both verts are inside ==> copy current to outlist */ \
393 OUTLIST[OUTCOUNT] = currj; \
394 OUTCOUNT++; \
395 } \
396 else { \
397 /* current is inside and previous is outside ==> clip */ \
398 COMPUTE_INTERSECTION( currj, prevj, VB->Free ) \
399 /* if new point not coincident with previous point... */ \
400 if (t>0.0) { \
401 /* interpolate aux info using the value of t */ \
402 if (ctx->ClipMask) \
403 interpolate_aux( ctx, VB->Free, t, currj, prevj ); \
404 VB->Edgeflag[VB->Free] = VB->Edgeflag[prevj]; \
405 /* output new point */ \
406 OUTLIST[OUTCOUNT] = VB->Free; \
407 VB->Free++; \
408 if (VB->Free==VB_SIZE) VB->Free = 1; \
409 OUTCOUNT++; \
410 } \
411 /* Output current */ \
412 OUTLIST[OUTCOUNT] = currj; \
413 OUTCOUNT++; \
414 } \
415 } \
416 else { \
417 if (INSIDE(prevj)) { \
418 /* current is outside and previous is inside ==> clip */ \
419 COMPUTE_INTERSECTION( prevj, currj, VB->Free ) \
420 /* if new point not coincident with previous point... */ \
421 if (t>0.0) { \
422 /* interpolate aux info using the value of t */ \
423 if (ctx->ClipMask) \
424 interpolate_aux( ctx, VB->Free, t, prevj, currj ); \
425 VB->Edgeflag[VB->Free] = VB->Edgeflag[prevj]; \
426 /* output new point */ \
427 OUTLIST[OUTCOUNT] = VB->Free; \
428 VB->Free++; \
429 if (VB->Free==VB_SIZE) VB->Free = 1; \
430 OUTCOUNT++; \
431 } \
432 } \
433 /* else both verts are outside ==> do nothing */ \
434 } \
435 /* let previous = current */ \
436 previ = curri; \
437 prevj = currj; \
438 /* check for overflowing vertex buffer */ \
439 if (OUTCOUNT>=VB_SIZE-1) { \
440 /* Too many vertices */ \
441 if (OUTLIST==vlist2) { \
442 /* copy OUTLIST[] to vlist[] */ \
443 int i; \
444 for (i=0;i<VB_SIZE;i++) { \
445 vlist[i] = OUTLIST[i]; \
446 } \
447 } \
448 return VB_SIZE-1; \
449 } \
450 }
451
452
453 #define X(I) coord[I][0]
454 #define Y(I) coord[I][1]
455 #define Z(I) coord[I][2]
456 #define W(I) coord[I][3]
457
458 /*
459 * Clip against +X
460 */
461 #define INCOUNT n
462 #define OUTCOUNT n2
463 #define INLIST vlist
464 #define OUTLIST vlist2
465 #define INSIDE(K) (X(K) <= W(K))
466
467 #define COMPUTE_INTERSECTION( in, out, new ) \
468 dx = X(out) - X(in); \
469 dw = W(out) - W(in); \
470 t = (X(in)-W(in)) / (dw-dx); \
471 neww = W(in) + t * dw; \
472 X(new) = neww; \
473 Y(new) = Y(in) + t * (Y(out) - Y(in)); \
474 Z(new) = Z(in) + t * (Z(out) - Z(in)); \
475 W(new) = neww;
476
477 GENERAL_CLIP
478
479 #undef INCOUNT
480 #undef OUTCOUNT
481 #undef INLIST
482 #undef OUTLIST
483 #undef INSIDE
484 #undef COMPUTE_INTERSECTION
485
486
487 /*
488 * Clip against -X
489 */
490 #define INCOUNT n2
491 #define OUTCOUNT n
492 #define INLIST vlist2
493 #define OUTLIST vlist
494 #define INSIDE(K) (X(K) >= -W(K))
495 #define COMPUTE_INTERSECTION( in, out, new ) \
496 dx = X(out)-X(in); \
497 dw = W(out)-W(in); \
498 t = -(X(in)+W(in)) / (dw+dx); \
499 neww = W(in) + t * dw; \
500 X(new) = -neww; \
501 Y(new) = Y(in) + t * (Y(out) - Y(in)); \
502 Z(new) = Z(in) + t * (Z(out) - Z(in)); \
503 W(new) = neww;
504
505 GENERAL_CLIP
506
507 #undef INCOUNT
508 #undef OUTCOUNT
509 #undef INLIST
510 #undef OUTLIST
511 #undef INSIDE
512 #undef COMPUTE_INTERSECTION
513
514
515 /*
516 * Clip against +Y
517 */
518 #define INCOUNT n
519 #define OUTCOUNT n2
520 #define INLIST vlist
521 #define OUTLIST vlist2
522 #define INSIDE(K) (Y(K) <= W(K))
523 #define COMPUTE_INTERSECTION( in, out, new ) \
524 dy = Y(out)-Y(in); \
525 dw = W(out)-W(in); \
526 t = (Y(in)-W(in)) / (dw-dy); \
527 neww = W(in) + t * dw; \
528 X(new) = X(in) + t * (X(out) - X(in)); \
529 Y(new) = neww; \
530 Z(new) = Z(in) + t * (Z(out) - Z(in)); \
531 W(new) = neww;
532
533 GENERAL_CLIP
534
535 #undef INCOUNT
536 #undef OUTCOUNT
537 #undef INLIST
538 #undef OUTLIST
539 #undef INSIDE
540 #undef COMPUTE_INTERSECTION
541
542
543 /*
544 * Clip against -Y
545 */
546 #define INCOUNT n2
547 #define OUTCOUNT n
548 #define INLIST vlist2
549 #define OUTLIST vlist
550 #define INSIDE(K) (Y(K) >= -W(K))
551 #define COMPUTE_INTERSECTION( in, out, new ) \
552 dy = Y(out)-Y(in); \
553 dw = W(out)-W(in); \
554 t = -(Y(in)+W(in)) / (dw+dy); \
555 neww = W(in) + t * dw; \
556 X(new) = X(in) + t * (X(out) - X(in)); \
557 Y(new) = -neww; \
558 Z(new) = Z(in) + t * (Z(out) - Z(in)); \
559 W(new) = neww;
560
561 GENERAL_CLIP
562
563 #undef INCOUNT
564 #undef OUTCOUNT
565 #undef INLIST
566 #undef OUTLIST
567 #undef INSIDE
568 #undef COMPUTE_INTERSECTION
569
570
571
572 /*
573 * Clip against +Z
574 */
575 #define INCOUNT n
576 #define OUTCOUNT n2
577 #define INLIST vlist
578 #define OUTLIST vlist2
579 #define INSIDE(K) (Z(K) <= W(K))
580 #define COMPUTE_INTERSECTION( in, out, new ) \
581 dz = Z(out)-Z(in); \
582 dw = W(out)-W(in); \
583 t = (Z(in)-W(in)) / (dw-dz); \
584 neww = W(in) + t * dw; \
585 X(new) = X(in) + t * (X(out) - X(in)); \
586 Y(new) = Y(in) + t * (Y(out) - Y(in)); \
587 Z(new) = neww; \
588 W(new) = neww;
589
590 GENERAL_CLIP
591
592 #undef INCOUNT
593 #undef OUTCOUNT
594 #undef INLIST
595 #undef OUTLIST
596 #undef INSIDE
597 #undef COMPUTE_INTERSECTION
598
599
600 /*
601 * Clip against -Z
602 */
603 #define INCOUNT n2
604 #define OUTCOUNT n
605 #define INLIST vlist2
606 #define OUTLIST vlist
607 #define INSIDE(K) (Z(K) >= -W(K))
608 #define COMPUTE_INTERSECTION( in, out, new ) \
609 dz = Z(out)-Z(in); \
610 dw = W(out)-W(in); \
611 t = -(Z(in)+W(in)) / (dw+dz); \
612 neww = W(in) + t * dw; \
613 X(new) = X(in) + t * (X(out) - X(in)); \
614 Y(new) = Y(in) + t * (Y(out) - Y(in)); \
615 Z(new) = -neww; \
616 W(new) = neww;
617
618 GENERAL_CLIP
619
620 #undef INCOUNT
621 #undef INLIST
622 #undef OUTLIST
623 #undef INSIDE
624 #undef COMPUTE_INTERSECTION
625
626 /* 'OUTCOUNT' clipped vertices are now back in v[] */
627 return OUTCOUNT;
628
629 #undef GENERAL_CLIP
630 #undef OUTCOUNT
631 }
632
633
634
635
636 /**********************************************************************/
637 /* Clipping against user-defined clipping planes. */
638 /**********************************************************************/
639
640
641
642 /*
643 * If the dot product of the eye coordinates of a vertex with the
644 * stored plane equation components is positive or zero, the vertex
645 * is in with respect to that clipping plane, otherwise it is out.
646 */
647
648
649
650 /*
651 * Clip a point against the user clipping planes.
652 * Input: v - vertex-vector describing the point to clip.
653 * Return: 0 = point was clipped
654 * 1 = point not clipped
655 */
656 GLuint gl_userclip_point( GLcontext* ctx, const GLfloat v[] )
657 {
658 GLuint p;
659
660 for (p=0;p<MAX_CLIP_PLANES;p++) {
661 if (ctx->Transform.ClipEnabled[p]) {
662 GLfloat dot = v[0] * ctx->Transform.ClipEquation[p][0]
663 + v[1] * ctx->Transform.ClipEquation[p][1]
664 + v[2] * ctx->Transform.ClipEquation[p][2]
665 + v[3] * ctx->Transform.ClipEquation[p][3];
666 if (dot < 0.0F) {
667 return 0;
668 }
669 }
670 }
671
672 return 1;
673 }
674
675
676 #define MAGIC_NUMBER -0.8e-03F
677
678
679 /* Test if VB->Eye[J] is inside the clipping plane defined by A,B,C,D */
680 #define INSIDE( J, A, B, C, D ) \
681 ( (VB->Eye[J][0] * A + VB->Eye[J][1] * B \
682 + VB->Eye[J][2] * C + VB->Eye[J][3] * D) >= MAGIC_NUMBER )
683
684
685 /* Test if VB->Eye[J] is outside the clipping plane defined by A,B,C,D */
686 #define OUTSIDE( J, A, B, C, D ) \
687 ( (VB->Eye[J][0] * A + VB->Eye[J][1] * B \
688 + VB->Eye[J][2] * C + VB->Eye[J][3] * D) < MAGIC_NUMBER )
689
690
691 /*
692 * Clip a line against the user clipping planes.
693 * Input: i, j - indexes into VB->V*[] of endpoints
694 * Output: i, j - indexes into VB->V*[] of (possibly clipped) endpoints
695 * Return: 0 = line completely clipped
696 * 1 = line is visible
697 */
698 GLuint gl_userclip_line( GLcontext* ctx, GLuint *i, GLuint *j )
699 {
700 struct vertex_buffer* VB = ctx->VB;
701
702 GLuint p, ii, jj;
703
704 Space = EYE_SPACE;
705
706 ii = *i;
707 jj = *j;
708
709 for (p=0;p<MAX_CLIP_PLANES;p++) {
710 if (ctx->Transform.ClipEnabled[p]) {
711 register GLfloat a, b, c, d;
712 a = ctx->Transform.ClipEquation[p][0];
713 b = ctx->Transform.ClipEquation[p][1];
714 c = ctx->Transform.ClipEquation[p][2];
715 d = ctx->Transform.ClipEquation[p][3];
716
717 if (OUTSIDE( ii, a,b,c,d )) {
718 if (OUTSIDE( jj, a,b,c,d )) {
719 /* ii and jj outside ==> quit */
720 return 0;
721 }
722 else {
723 /* ii is outside, jj is inside ==> clip */
724 GLfloat dx, dy, dz, dw, t, denom;
725 dx = VB->Eye[ii][0] - VB->Eye[jj][0];
726 dy = VB->Eye[ii][1] - VB->Eye[jj][1];
727 dz = VB->Eye[ii][2] - VB->Eye[jj][2];
728 dw = VB->Eye[ii][3] - VB->Eye[jj][3];
729 denom = dx*a + dy*b + dz*c + dw*d;
730 if (denom==0.0) {
731 t = 0.0;
732 }
733 else {
734 t = -(VB->Eye[jj][0]*a+VB->Eye[jj][1]*b
735 +VB->Eye[jj][2]*c+VB->Eye[jj][3]*d) / denom;
736 if (t>1.0F) t = 1.0F;
737 }
738 VB->Eye[VB->Free][0] = VB->Eye[jj][0] + t * dx;
739 VB->Eye[VB->Free][1] = VB->Eye[jj][1] + t * dy;
740 VB->Eye[VB->Free][2] = VB->Eye[jj][2] + t * dz;
741 VB->Eye[VB->Free][3] = VB->Eye[jj][3] + t * dw;
742
743 /* Interpolate colors, indexes, and/or texture coords */
744 if (ctx->ClipMask)
745 interpolate_aux( ctx, VB->Free, t, jj, ii );
746
747 ii = VB->Free;
748 VB->Free++;
749 if (VB->Free==VB_SIZE) VB->Free = 1;
750 }
751 }
752 else {
753 if (OUTSIDE( jj, a,b,c,d )) {
754 /* ii is inside, jj is outside ==> clip */
755 GLfloat dx, dy, dz, dw, t, denom;
756 dx = VB->Eye[jj][0] - VB->Eye[ii][0];
757 dy = VB->Eye[jj][1] - VB->Eye[ii][1];
758 dz = VB->Eye[jj][2] - VB->Eye[ii][2];
759 dw = VB->Eye[jj][3] - VB->Eye[ii][3];
760 denom = dx*a + dy*b + dz*c + dw*d;
761 if (denom==0.0) {
762 t = 0.0;
763 }
764 else {
765 t = -(VB->Eye[ii][0]*a+VB->Eye[ii][1]*b
766 +VB->Eye[ii][2]*c+VB->Eye[ii][3]*d) / denom;
767 if (t>1.0F) t = 1.0F;
768 }
769 VB->Eye[VB->Free][0] = VB->Eye[ii][0] + t * dx;
770 VB->Eye[VB->Free][1] = VB->Eye[ii][1] + t * dy;
771 VB->Eye[VB->Free][2] = VB->Eye[ii][2] + t * dz;
772 VB->Eye[VB->Free][3] = VB->Eye[ii][3] + t * dw;
773
774 /* Interpolate colors, indexes, and/or texture coords */
775 if (ctx->ClipMask)
776 interpolate_aux( ctx, VB->Free, t, ii, jj );
777
778 jj = VB->Free;
779 VB->Free++;
780 if (VB->Free==VB_SIZE) VB->Free = 1;
781 }
782 else {
783 /* ii and jj inside ==> do nothing */
784 }
785 }
786 }
787 }
788
789 *i = ii;
790 *j = jj;
791 return 1;
792 }
793
794
795
796
797 /*
798 * Clip a polygon against the user clipping planes defined in eye coordinates.
799 * Input: n - number of vertices.
800 * vlist - list of vertices in input polygon.
801 * Output: vlist - list of vertices in output polygon.
802 * Return: number of vertices after clipping.
803 */
804 GLuint gl_userclip_polygon( GLcontext* ctx, GLuint n, GLuint vlist[] )
805 {
806 struct vertex_buffer* VB = ctx->VB;
807
808 GLuint vlist2[VB_SIZE];
809 GLuint *inlist, *outlist;
810 GLuint incount, outcount;
811 GLuint curri, currj;
812 GLuint previ, prevj;
813 GLuint p;
814
815 Space = EYE_SPACE;
816
817 /* initialize input vertex list */
818 incount = n;
819 inlist = vlist;
820 outlist = vlist2;
821
822 for (p=0;p<MAX_CLIP_PLANES;p++) {
823 if (ctx->Transform.ClipEnabled[p]) {
824 register float a = ctx->Transform.ClipEquation[p][0];
825 register float b = ctx->Transform.ClipEquation[p][1];
826 register float c = ctx->Transform.ClipEquation[p][2];
827 register float d = ctx->Transform.ClipEquation[p][3];
828
829 if (incount<3) return 0;
830
831 /* initialize prev to be last in the input list */
832 previ = incount - 1;
833 prevj = inlist[previ];
834
835 outcount = 0;
836
837 for (curri=0;curri<incount;curri++) {
838 currj = inlist[curri];
839
840 if (INSIDE(currj, a,b,c,d)) {
841 if (INSIDE(prevj, a,b,c,d)) {
842 /* both verts are inside ==> copy current to outlist */
843 outlist[outcount++] = currj;
844 }
845 else {
846 /* current is inside and previous is outside ==> clip */
847 GLfloat dx, dy, dz, dw, t, denom;
848 /* compute t */
849 dx = VB->Eye[prevj][0] - VB->Eye[currj][0];
850 dy = VB->Eye[prevj][1] - VB->Eye[currj][1];
851 dz = VB->Eye[prevj][2] - VB->Eye[currj][2];
852 dw = VB->Eye[prevj][3] - VB->Eye[currj][3];
853 denom = dx*a + dy*b + dz*c + dw*d;
854 if (denom==0.0) {
855 t = 0.0;
856 }
857 else {
858 t = -(VB->Eye[currj][0]*a+VB->Eye[currj][1]*b
859 +VB->Eye[currj][2]*c+VB->Eye[currj][3]*d) / denom;
860 if (t>1.0F) {
861 t = 1.0F;
862 }
863 }
864 /* interpolate new vertex position */
865 VB->Eye[VB->Free][0] = VB->Eye[currj][0] + t*dx;
866 VB->Eye[VB->Free][1] = VB->Eye[currj][1] + t*dy;
867 VB->Eye[VB->Free][2] = VB->Eye[currj][2] + t*dz;
868 VB->Eye[VB->Free][3] = VB->Eye[currj][3] + t*dw;
869
870 /* interpolate color, index, and/or texture coord */
871 if (ctx->ClipMask) {
872 interpolate_aux( ctx, VB->Free, t, currj, prevj);
873 }
874 VB->Edgeflag[VB->Free] = VB->Edgeflag[prevj];
875
876 /* output new vertex */
877 outlist[outcount++] = VB->Free;
878 VB->Free++;
879 if (VB->Free==VB_SIZE) VB->Free = 1;
880 /* output current vertex */
881 outlist[outcount++] = currj;
882 }
883 }
884 else {
885 if (INSIDE(prevj, a,b,c,d)) {
886 /* current is outside and previous is inside ==> clip */
887 GLfloat dx, dy, dz, dw, t, denom;
888 /* compute t */
889 dx = VB->Eye[currj][0]-VB->Eye[prevj][0];
890 dy = VB->Eye[currj][1]-VB->Eye[prevj][1];
891 dz = VB->Eye[currj][2]-VB->Eye[prevj][2];
892 dw = VB->Eye[currj][3]-VB->Eye[prevj][3];
893 denom = dx*a + dy*b + dz*c + dw*d;
894 if (denom==0.0) {
895 t = 0.0;
896 }
897 else {
898 t = -(VB->Eye[prevj][0]*a+VB->Eye[prevj][1]*b
899 +VB->Eye[prevj][2]*c+VB->Eye[prevj][3]*d) / denom;
900 if (t>1.0F) {
901 t = 1.0F;
902 }
903 }
904 /* interpolate new vertex position */
905 VB->Eye[VB->Free][0] = VB->Eye[prevj][0] + t*dx;
906 VB->Eye[VB->Free][1] = VB->Eye[prevj][1] + t*dy;
907 VB->Eye[VB->Free][2] = VB->Eye[prevj][2] + t*dz;
908 VB->Eye[VB->Free][3] = VB->Eye[prevj][3] + t*dw;
909
910 /* interpolate color, index, and/or texture coord */
911 if (ctx->ClipMask) {
912 interpolate_aux( ctx, VB->Free, t, prevj, currj);
913 }
914 VB->Edgeflag[VB->Free] = VB->Edgeflag[prevj];
915
916 /* output new vertex */
917 outlist[outcount++] = VB->Free;
918 VB->Free++;
919 if (VB->Free==VB_SIZE) VB->Free = 1;
920 }
921 /* else both verts are outside ==> do nothing */
922 }
923
924 previ = curri;
925 prevj = currj;
926
927 /* check for overflowing vertex buffer */
928 if (outcount>=VB_SIZE-1) {
929 /* Too many vertices */
930 if (outlist!=vlist2) {
931 MEMCPY( vlist, vlist2, outcount * sizeof(GLuint) );
932 }
933 return VB_SIZE-1;
934 }
935
936 } /* for i */
937
938 /* swap inlist and outlist pointers */
939 {
940 GLuint *tmp;
941 tmp = inlist;
942 inlist = outlist;
943 outlist = tmp;
944 incount = outcount;
945 }
946
947 } /* if */
948 } /* for p */
949
950 /* outlist points to the list of vertices resulting from the last */
951 /* clipping. If outlist == vlist2 then we have to copy the vertices */
952 /* back to vlist */
953 if (outlist!=vlist2) {
954 MEMCPY( vlist, vlist2, outcount * sizeof(GLuint) );
955 }
956
957 return outcount;
958 }
959