2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 * Keith Whitwell <keith@tungstengraphics.com>
29 #define CLIP_DOTPROD(K, A, B, C, D) X(K)*A + Y(K)*B + Z(K)*C + W(K)*D
31 #define POLY_CLIP( PLANE_BIT, A, B, C, D ) \
33 if (mask & PLANE_BIT) { \
34 GLuint idxPrev = inlist[0]; \
35 GLfloat dpPrev = CLIP_DOTPROD(idxPrev, A, B, C, D ); \
36 GLuint outcount = 0; \
39 inlist[n] = inlist[0]; /* prevent rotation of vertices */ \
40 for (i = 1; i <= n; i++) { \
41 GLuint idx = inlist[i]; \
42 GLfloat dp = CLIP_DOTPROD(idx, A, B, C, D ); \
44 if (!IS_NEGATIVE(dpPrev)) { \
45 outlist[outcount++] = idxPrev; \
48 if (DIFFERENT_SIGNS(dp, dpPrev)) { \
49 if (IS_NEGATIVE(dp)) { \
50 /* Going out of bounds. Avoid division by zero as we \
51 * know dp != dpPrev from DIFFERENT_SIGNS, above. \
53 GLfloat t = dp / (dp - dpPrev); \
54 INTERP_4F( t, coord[newvert], coord[idx], coord[idxPrev]); \
55 interp( ctx, t, newvert, idx, idxPrev, GL_TRUE ); \
59 GLfloat t = dpPrev / (dpPrev - dp); \
60 INTERP_4F( t, coord[newvert], coord[idxPrev], coord[idx]); \
61 interp( ctx, t, newvert, idxPrev, idx, GL_FALSE ); \
63 outlist[outcount++] = newvert++; \
74 GLuint *tmp = inlist; \
83 #define LINE_CLIP(PLANE_BIT, A, B, C, D ) \
85 if (mask & PLANE_BIT) { \
86 const GLfloat dp0 = CLIP_DOTPROD( v0, A, B, C, D ); \
87 const GLfloat dp1 = CLIP_DOTPROD( v1, A, B, C, D ); \
88 const GLboolean neg_dp0 = IS_NEGATIVE(dp0); \
89 const GLboolean neg_dp1 = IS_NEGATIVE(dp1); \
91 /* For regular clipping, we know from the clipmask that one \
92 * (or both) of these must be negative (otherwise we wouldn't \
94 * For userclip, there is only a single bit for all active \
95 * planes, so we can end up here when there is nothing to do, \
96 * hence the second IS_NEGATIVE() test: \
98 if (neg_dp0 && neg_dp1) \
99 return; /* both vertices outside clip plane: discard */ \
102 GLfloat t = dp1 / (dp1 - dp0); \
103 if (t > t1) t1 = t; \
104 } else if (neg_dp0) { \
105 GLfloat t = dp0 / (dp0 - dp1); \
106 if (t > t0) t0 = t; \
108 if (t0 + t1 >= 1.0) \
109 return; /* discard */ \
115 /* Clip a line against the viewport and user clip planes.
118 TAG(clip_line
)( GLcontext
*ctx
, GLuint v0
, GLuint v1
, GLubyte mask
)
120 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
121 struct vertex_buffer
*VB
= &tnl
->vb
;
122 tnl_interp_func interp
= tnl
->Driver
.Render
.Interp
;
123 GLfloat (*coord
)[4] = VB
->ClipPtr
->data
;
124 GLuint newvert
= VB
->Count
;
130 LINE_CLIP( CLIP_RIGHT_BIT
, -1, 0, 0, 1 );
131 LINE_CLIP( CLIP_LEFT_BIT
, 1, 0, 0, 1 );
132 LINE_CLIP( CLIP_TOP_BIT
, 0, -1, 0, 1 );
133 LINE_CLIP( CLIP_BOTTOM_BIT
, 0, 1, 0, 1 );
134 LINE_CLIP( CLIP_FAR_BIT
, 0, 0, -1, 1 );
135 LINE_CLIP( CLIP_NEAR_BIT
, 0, 0, 1, 1 );
138 if (mask
& CLIP_USER_BIT
) {
139 for (p
= 0; p
< ctx
->Const
.MaxClipPlanes
; p
++) {
140 if (ctx
->Transform
.ClipPlanesEnabled
& (1 << p
)) {
141 const GLfloat a
= ctx
->Transform
._ClipUserPlane
[p
][0];
142 const GLfloat b
= ctx
->Transform
._ClipUserPlane
[p
][1];
143 const GLfloat c
= ctx
->Transform
._ClipUserPlane
[p
][2];
144 const GLfloat d
= ctx
->Transform
._ClipUserPlane
[p
][3];
145 LINE_CLIP( CLIP_USER_BIT
, a
, b
, c
, d
);
150 if (VB
->ClipMask
[v0
]) {
151 INTERP_4F( t0
, coord
[newvert
], coord
[v0
], coord
[v1
] );
152 interp( ctx
, t0
, newvert
, v0
, v1
, GL_FALSE
);
159 if (VB
->ClipMask
[v1
]) {
160 INTERP_4F( t1
, coord
[newvert
], coord
[v1
], coord
[v0
] );
161 interp( ctx
, t1
, newvert
, v1
, v0
, GL_FALSE
);
163 if (ctx
->_TriangleCaps
& DD_FLATSHADE
)
164 tnl
->Driver
.Render
.CopyPV( ctx
, newvert
, v1
);
173 tnl
->Driver
.Render
.ClippedLine( ctx
, v0
, v1
);
177 /* Clip a triangle against the viewport and user clip planes.
180 TAG(clip_tri
)( GLcontext
*ctx
, GLuint v0
, GLuint v1
, GLuint v2
, GLubyte mask
)
182 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
183 struct vertex_buffer
*VB
= &tnl
->vb
;
184 tnl_interp_func interp
= tnl
->Driver
.Render
.Interp
;
185 GLuint newvert
= VB
->Count
;
186 GLfloat (*coord
)[4] = VB
->ClipPtr
->data
;
188 GLuint vlist
[2][MAX_CLIPPED_VERTICES
];
189 GLuint
*inlist
= vlist
[0], *outlist
= vlist
[1];
193 ASSIGN_3V(inlist
, v2
, v0
, v1
); /* pv rotated to slot zero */
196 POLY_CLIP( CLIP_RIGHT_BIT
, -1, 0, 0, 1 );
197 POLY_CLIP( CLIP_LEFT_BIT
, 1, 0, 0, 1 );
198 POLY_CLIP( CLIP_TOP_BIT
, 0, -1, 0, 1 );
199 POLY_CLIP( CLIP_BOTTOM_BIT
, 0, 1, 0, 1 );
200 POLY_CLIP( CLIP_FAR_BIT
, 0, 0, -1, 1 );
201 POLY_CLIP( CLIP_NEAR_BIT
, 0, 0, 1, 1 );
204 if (mask
& CLIP_USER_BIT
) {
205 for (p
= 0; p
< ctx
->Const
.MaxClipPlanes
; p
++) {
206 if (ctx
->Transform
.ClipPlanesEnabled
& (1 << p
)) {
207 const GLfloat a
= ctx
->Transform
._ClipUserPlane
[p
][0];
208 const GLfloat b
= ctx
->Transform
._ClipUserPlane
[p
][1];
209 const GLfloat c
= ctx
->Transform
._ClipUserPlane
[p
][2];
210 const GLfloat d
= ctx
->Transform
._ClipUserPlane
[p
][3];
211 POLY_CLIP( CLIP_USER_BIT
, a
, b
, c
, d
);
216 if (ctx
->_TriangleCaps
& DD_FLATSHADE
) {
217 if (pv
!= inlist
[0]) {
218 ASSERT( inlist
[0] >= VB
->Count
);
219 tnl
->Driver
.Render
.CopyPV( ctx
, inlist
[0], pv
);
223 tnl
->Driver
.Render
.ClippedPolygon( ctx
, inlist
, n
);
227 /* Clip a quad against the viewport and user clip planes.
230 TAG(clip_quad
)( GLcontext
*ctx
, GLuint v0
, GLuint v1
, GLuint v2
, GLuint v3
,
233 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
234 struct vertex_buffer
*VB
= &tnl
->vb
;
235 tnl_interp_func interp
= tnl
->Driver
.Render
.Interp
;
236 GLuint newvert
= VB
->Count
;
237 GLfloat (*coord
)[4] = VB
->ClipPtr
->data
;
239 GLuint vlist
[2][MAX_CLIPPED_VERTICES
];
240 GLuint
*inlist
= vlist
[0], *outlist
= vlist
[1];
244 ASSIGN_4V(inlist
, v3
, v0
, v1
, v2
); /* pv rotated to slot zero */
247 POLY_CLIP( CLIP_RIGHT_BIT
, -1, 0, 0, 1 );
248 POLY_CLIP( CLIP_LEFT_BIT
, 1, 0, 0, 1 );
249 POLY_CLIP( CLIP_TOP_BIT
, 0, -1, 0, 1 );
250 POLY_CLIP( CLIP_BOTTOM_BIT
, 0, 1, 0, 1 );
251 POLY_CLIP( CLIP_FAR_BIT
, 0, 0, -1, 1 );
252 POLY_CLIP( CLIP_NEAR_BIT
, 0, 0, 1, 1 );
255 if (mask
& CLIP_USER_BIT
) {
256 for (p
= 0; p
< ctx
->Const
.MaxClipPlanes
; p
++) {
257 if (ctx
->Transform
.ClipPlanesEnabled
& (1 << p
)) {
258 const GLfloat a
= ctx
->Transform
._ClipUserPlane
[p
][0];
259 const GLfloat b
= ctx
->Transform
._ClipUserPlane
[p
][1];
260 const GLfloat c
= ctx
->Transform
._ClipUserPlane
[p
][2];
261 const GLfloat d
= ctx
->Transform
._ClipUserPlane
[p
][3];
262 POLY_CLIP( CLIP_USER_BIT
, a
, b
, c
, d
);
267 if (ctx
->_TriangleCaps
& DD_FLATSHADE
) {
268 if (pv
!= inlist
[0]) {
269 ASSERT( inlist
[0] >= VB
->Count
);
270 tnl
->Driver
.Render
.CopyPV( ctx
, inlist
[0], pv
);
274 tnl
->Driver
.Render
.ClippedPolygon( ctx
, inlist
, n
);