1 /* $Id: clip.c,v 1.6 2000/02/25 03:55:39 keithw Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45 #define CLIP_RGBA0 0x1
46 #define CLIP_RGBA1 0x2
49 #define CLIP_INDEX0 0x10
50 #define CLIP_INDEX1 0x20
51 #define CLIP_FOG_COORD 0x40
54 /* Linear interpolation between A and B: */
55 #define LINTERP( T, A, B ) ( (A) + (T) * ( (B) - (A) ) )
59 #define INTERP_SZ( t, vec, to, a, b, sz ) \
62 case 4: vec[to][3] = LINTERP( t, vec[a][3], vec[b][3] ); \
63 case 3: vec[to][2] = LINTERP( t, vec[a][2], vec[b][2] ); \
64 case 2: vec[to][1] = LINTERP( t, vec[a][1], vec[b][1] ); \
65 case 1: vec[to][0] = LINTERP( t, vec[a][0], vec[b][0] ); \
70 static clip_interp_func clip_interp_tab
[0x80];
74 #include "interp_tmp.h"
76 #define IND (CLIP_RGBA0)
77 #define NAME clipRGBA0
78 #include "interp_tmp.h"
80 #define IND (CLIP_RGBA0|CLIP_RGBA1)
81 #define NAME clipRGBA0_RGBA1
82 #include "interp_tmp.h"
84 #define IND (CLIP_TEX0|CLIP_RGBA0)
85 #define NAME clipTEX0_RGBA0
86 #include "interp_tmp.h"
88 #define IND (CLIP_TEX0|CLIP_RGBA0|CLIP_RGBA1)
89 #define NAME clipTEX0_RGBA0_RGBA1
90 #include "interp_tmp.h"
92 #define IND (CLIP_TEX1|CLIP_TEX0|CLIP_RGBA0)
93 #define NAME clipTEX1_TEX0_RGBA0
94 #include "interp_tmp.h"
96 #define IND (CLIP_TEX0)
98 #include "interp_tmp.h"
100 #define IND (CLIP_TEX1|CLIP_TEX0)
101 #define NAME clipTEX1_TEX0
102 #include "interp_tmp.h"
104 #define IND (CLIP_TEX1|CLIP_TEX0|CLIP_RGBA0|CLIP_RGBA1)
105 #define NAME clipTEX1_TEX0_RGBA0_RGBA1
106 #include "interp_tmp.h"
108 #define IND (CLIP_INDEX0)
109 #define NAME clipINDEX0
110 #include "interp_tmp.h"
112 #define IND (CLIP_INDEX0|CLIP_INDEX1)
113 #define NAME clipINDEX0_INDEX1
114 #include "interp_tmp.h"
116 #define IND (CLIP_FOG_COORD)
117 #define NAME clip_FOG
118 #include "interp_tmp.h"
120 #define IND (CLIP_RGBA0|CLIP_FOG_COORD)
121 #define NAME clipRGBA0_FOG
122 #include "interp_tmp.h"
124 #define IND (CLIP_RGBA0|CLIP_RGBA1|CLIP_FOG_COORD)
125 #define NAME clipRGBA0_RGBA1_FOG
126 #include "interp_tmp.h"
128 #define IND (CLIP_TEX0|CLIP_RGBA0|CLIP_FOG_COORD)
129 #define NAME clipTEX0_RGBA0_FOG
130 #include "interp_tmp.h"
132 #define IND (CLIP_TEX0|CLIP_RGBA0|CLIP_RGBA1|CLIP_FOG_COORD)
133 #define NAME clipTEX0_RGBA0_RGBA1_FOG
134 #include "interp_tmp.h"
136 #define IND (CLIP_TEX1|CLIP_TEX0|CLIP_RGBA0|CLIP_FOG_COORD)
137 #define NAME clipTEX1_TEX0_RGBA0_FOG
138 #include "interp_tmp.h"
140 #define IND (CLIP_TEX0|CLIP_FOG_COORD)
141 #define NAME clipTEX0_FOG
142 #include "interp_tmp.h"
144 #define IND (CLIP_TEX1|CLIP_TEX0|CLIP_FOG_COORD)
145 #define NAME clipTEX1_TEX0_FOG
146 #include "interp_tmp.h"
148 #define IND (CLIP_TEX1|CLIP_TEX0|CLIP_RGBA0|CLIP_RGBA1|CLIP_FOG_COORD)
149 #define NAME clipTEX1_TEX0_RGBA0_RGBA1_FOG
150 #include "interp_tmp.h"
152 #define IND (CLIP_INDEX0|CLIP_FOG_COORD)
153 #define NAME clipINDEX0_FOG
154 #include "interp_tmp.h"
156 #define IND (CLIP_INDEX0|CLIP_INDEX1|CLIP_FOG_COORD)
157 #define NAME clipINDEX0_INDEX1_FOG
158 #include "interp_tmp.h"
163 /**********************************************************************/
164 /* Get/Set User clip-planes. */
165 /**********************************************************************/
170 _mesa_ClipPlane( GLenum plane
, const GLdouble
*eq
)
172 GET_CURRENT_CONTEXT(ctx
);
181 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glClipPlane");
183 p
= (GLint
) plane
- (GLint
) GL_CLIP_PLANE0
;
184 if (p
<0 || p
>=MAX_CLIP_PLANES
) {
185 gl_error( ctx
, GL_INVALID_ENUM
, "glClipPlane" );
190 * The equation is transformed by the transpose of the inverse of the
191 * current modelview matrix and stored in the resulting eye coordinates.
193 * KW: Eqn is then transformed to the current clip space, where user
194 * clipping now takes place. The clip-space equations are recalculated
195 * whenever the projection matrix changes.
197 if (ctx
->ModelView
.flags
& MAT_DIRTY_ALL_OVER
) {
198 gl_matrix_analyze( &ctx
->ModelView
);
200 gl_transform_vector( ctx
->Transform
.EyeUserPlane
[p
], equation
,
201 ctx
->ModelView
.inv
);
204 if (ctx
->Transform
.ClipEnabled
[p
]) {
205 ctx
->NewState
|= NEW_USER_CLIP
;
207 if (ctx
->ProjectionMatrix
.flags
& MAT_DIRTY_ALL_OVER
) {
208 gl_matrix_analyze( &ctx
->ProjectionMatrix
);
210 gl_transform_vector( ctx
->Transform
.ClipUserPlane
[p
],
211 ctx
->Transform
.EyeUserPlane
[p
],
212 ctx
->ProjectionMatrix
.inv
);
217 void gl_update_userclip( GLcontext
*ctx
)
221 for (p
= 0 ; p
< MAX_CLIP_PLANES
; p
++) {
222 if (ctx
->Transform
.ClipEnabled
[p
]) {
223 gl_transform_vector( ctx
->Transform
.ClipUserPlane
[p
],
224 ctx
->Transform
.EyeUserPlane
[p
],
225 ctx
->ProjectionMatrix
.inv
);
231 _mesa_GetClipPlane( GLenum plane
, GLdouble
*equation
)
233 GET_CURRENT_CONTEXT(ctx
);
236 ASSERT_OUTSIDE_BEGIN_END(ctx
, "glGetClipPlane");
239 p
= (GLint
) (plane
- GL_CLIP_PLANE0
);
240 if (p
<0 || p
>=MAX_CLIP_PLANES
) {
241 gl_error( ctx
, GL_INVALID_ENUM
, "glGetClipPlane" );
245 equation
[0] = (GLdouble
) ctx
->Transform
.EyeUserPlane
[p
][0];
246 equation
[1] = (GLdouble
) ctx
->Transform
.EyeUserPlane
[p
][1];
247 equation
[2] = (GLdouble
) ctx
->Transform
.EyeUserPlane
[p
][2];
248 equation
[3] = (GLdouble
) ctx
->Transform
.EyeUserPlane
[p
][3];
254 /**********************************************************************/
255 /* View volume clipping. */
256 /**********************************************************************/
260 * Clip a point against the view volume.
261 * Input: v - vertex-vector describing the point to clip
262 * Return: 0 = outside view volume
263 * 1 = inside view volume
265 GLuint
gl_viewclip_point( const GLfloat v
[] )
267 if ( v
[0] > v
[3] || v
[0] < -v
[3]
268 || v
[1] > v
[3] || v
[1] < -v
[3]
269 || v
[2] > v
[3] || v
[2] < -v
[3] ) {
278 * Clip a point against the user clipping planes.
279 * Input: v - vertex-vector describing the point to clip.
280 * Return: 0 = point was clipped
281 * 1 = point not clipped
283 GLuint
gl_userclip_point( GLcontext
* ctx
, const GLfloat v
[] )
287 for (p
=0;p
<MAX_CLIP_PLANES
;p
++) {
288 if (ctx
->Transform
.ClipEnabled
[p
]) {
289 GLfloat dot
= v
[0] * ctx
->Transform
.ClipUserPlane
[p
][0]
290 + v
[1] * ctx
->Transform
.ClipUserPlane
[p
][1]
291 + v
[2] * ctx
->Transform
.ClipUserPlane
[p
][2]
292 + v
[3] * ctx
->Transform
.ClipUserPlane
[p
][3];
305 #if defined(__i386__)
306 #define NEGATIVE(x) ((*(int *)&x)<0)
308 #define NEGATIVE(x) (x < 0)
312 static clip_poly_func gl_poly_clip_tab
[2][5];
313 static clip_line_func gl_line_clip_tab
[2][5];
315 #define W(i) coord[i][3]
316 #define Z(i) coord[i][2]
317 #define Y(i) coord[i][1]
318 #define X(i) coord[i][0]
322 #include "clip_funcs.h"
325 #define Z(i) coord[i][2]
326 #define Y(i) coord[i][1]
327 #define X(i) coord[i][0]
331 #include "clip_funcs.h"
335 #define Y(i) coord[i][1]
336 #define X(i) coord[i][0]
340 #include "clip_funcs.h"
342 #define W(i) coord[i][3]
343 #define Z(i) coord[i][2]
344 #define Y(i) coord[i][1]
345 #define X(i) coord[i][0]
347 #define IND CLIP_TAB_EDGEFLAG
348 #define TAG(x) x##_4_edgeflag
349 #include "clip_funcs.h"
352 #define Z(i) coord[i][2]
353 #define Y(i) coord[i][1]
354 #define X(i) coord[i][0]
356 #define IND CLIP_TAB_EDGEFLAG
357 #define TAG(x) x##_3_edgeflag
358 #include "clip_funcs.h"
362 #define Y(i) coord[i][1]
363 #define X(i) coord[i][0]
365 #define IND CLIP_TAB_EDGEFLAG
366 #define TAG(x) x##_2_edgeflag
367 #include "clip_funcs.h"
372 void gl_update_clipmask( GLcontext
*ctx
)
376 if (ctx
->Visual
->RGBAflag
)
378 if (ctx
->Light
.ShadeModel
==GL_SMOOTH
)
382 if (ctx
->TriangleCaps
& (DD_TRI_LIGHT_TWOSIDE
|DD_SEPERATE_SPECULAR
))
386 if (ctx
->Texture
.ReallyEnabled
& 0xf0)
387 mask
|= CLIP_TEX1
|CLIP_TEX0
;
389 if (ctx
->Texture
.ReallyEnabled
& 0xf)
392 else if (ctx
->Light
.ShadeModel
==GL_SMOOTH
)
396 if (ctx
->TriangleCaps
& DD_TRI_LIGHT_TWOSIDE
)
400 if (ctx
->FogMode
== FOG_FRAGMENT
&& (ctx
->TriangleCaps
& DD_CLIP_FOG_COORD
))
401 mask
|= CLIP_FOG_COORD
;
403 ctx
->ClipInterpFunc
= clip_interp_tab
[mask
];
404 ctx
->poly_clip_tab
= gl_poly_clip_tab
[0];
405 ctx
->line_clip_tab
= gl_line_clip_tab
[0];
407 if (ctx
->TriangleCaps
& DD_TRI_UNFILLED
) {
408 ctx
->poly_clip_tab
= gl_poly_clip_tab
[1];
409 ctx
->line_clip_tab
= gl_line_clip_tab
[0];
414 #define USER_CLIPTEST(NAME, SZ) \
415 static void NAME( struct vertex_buffer *VB ) \
417 GLcontext *ctx = VB->ctx; \
418 GLubyte *clipMask = VB->ClipMask; \
419 GLubyte *userClipMask = VB->UserClipMask; \
420 GLuint start = VB->Start; \
421 GLuint count = VB->Count; \
426 for (bit = 1, p = 0; p < MAX_CLIP_PLANES ; p++, bit *=2) \
427 if (ctx->Transform.ClipEnabled[p]) { \
429 const GLfloat a = ctx->Transform.ClipUserPlane[p][0]; \
430 const GLfloat b = ctx->Transform.ClipUserPlane[p][1]; \
431 const GLfloat c = ctx->Transform.ClipUserPlane[p][2]; \
432 const GLfloat d = ctx->Transform.ClipUserPlane[p][3]; \
433 GLfloat *coord = VB->ClipPtr->start; \
434 GLuint stride = VB->ClipPtr->stride; \
436 for (i = start ; i < count ; i++, STRIDE_F(coord, stride)) { \
437 GLfloat dp = coord[0] * a + coord[1] * b; \
438 if (SZ > 2) dp += coord[2] * c; \
439 if (SZ > 3) dp += coord[3] * d; else dp += d; \
442 clipMask[i] |= CLIP_USER_BIT; \
443 userClipMask[i] |= bit; \
449 VB->ClipOrMask |= CLIP_USER_BIT; \
450 VB->CullMode |= CLIP_MASK_ACTIVE; \
451 if (nr == count - start) { \
452 VB->ClipAndMask |= CLIP_USER_BIT; \
461 USER_CLIPTEST(userclip2
, 2)
462 USER_CLIPTEST(userclip3
, 3)
463 USER_CLIPTEST(userclip4
, 4)
465 static void (*(usercliptab
[5]))( struct vertex_buffer
* ) = {
473 void gl_user_cliptest( struct vertex_buffer
*VB
)
475 usercliptab
[VB
->ClipPtr
->size
]( VB
);
479 void gl_init_clip(void)
485 init_clip_funcs_4_edgeflag();
486 init_clip_funcs_3_edgeflag();
487 init_clip_funcs_2_edgeflag();
489 clip_interp_tab
[0] = clip_nil
;
490 clip_interp_tab
[CLIP_RGBA0
] = clipRGBA0
;
491 clip_interp_tab
[CLIP_RGBA0
|CLIP_RGBA1
] = clipRGBA0_RGBA1
;
492 clip_interp_tab
[CLIP_TEX0
|CLIP_RGBA0
] = clipTEX0_RGBA0
;
493 clip_interp_tab
[CLIP_TEX0
|CLIP_RGBA0
|CLIP_RGBA1
] = clipTEX0_RGBA0_RGBA1
;
494 clip_interp_tab
[CLIP_TEX1
|CLIP_TEX0
|CLIP_RGBA0
] = clipTEX1_TEX0_RGBA0
;
495 clip_interp_tab
[CLIP_TEX1
|CLIP_TEX0
|CLIP_RGBA0
|CLIP_RGBA1
] =
496 clipTEX1_TEX0_RGBA0_RGBA1
;
497 clip_interp_tab
[CLIP_TEX0
] = clipTEX0
;
498 clip_interp_tab
[CLIP_TEX1
|CLIP_TEX0
] = clipTEX1_TEX0
;
499 clip_interp_tab
[CLIP_INDEX0
] = clipINDEX0
;
500 clip_interp_tab
[CLIP_INDEX0
|CLIP_INDEX1
] = clipINDEX0_INDEX1
;
502 clip_interp_tab
[CLIP_FOG_COORD
] = clip_FOG
;
503 clip_interp_tab
[CLIP_RGBA0
|CLIP_FOG_COORD
] = clipRGBA0_FOG
;
504 clip_interp_tab
[CLIP_RGBA0
|CLIP_RGBA1
|CLIP_FOG_COORD
] = clipRGBA0_RGBA1_FOG
;
505 clip_interp_tab
[CLIP_TEX0
|CLIP_RGBA0
|CLIP_FOG_COORD
] = clipTEX0_RGBA0_FOG
;
506 clip_interp_tab
[CLIP_TEX0
|CLIP_RGBA0
|CLIP_RGBA1
|CLIP_FOG_COORD
] = clipTEX0_RGBA0_RGBA1_FOG
;
507 clip_interp_tab
[CLIP_TEX1
|CLIP_TEX0
|CLIP_RGBA0
|CLIP_FOG_COORD
] = clipTEX1_TEX0_RGBA0_FOG
;
508 clip_interp_tab
[CLIP_TEX1
|CLIP_TEX0
|CLIP_RGBA0
|CLIP_RGBA1
|CLIP_FOG_COORD
] =
509 clipTEX1_TEX0_RGBA0_RGBA1_FOG
;
510 clip_interp_tab
[CLIP_TEX0
|CLIP_FOG_COORD
] = clipTEX0_FOG
;
511 clip_interp_tab
[CLIP_TEX1
|CLIP_TEX0
|CLIP_FOG_COORD
] = clipTEX1_TEX0_FOG
;
512 clip_interp_tab
[CLIP_INDEX0
|CLIP_FOG_COORD
] = clipINDEX0_FOG
;
513 clip_interp_tab
[CLIP_INDEX0
|CLIP_INDEX1
|CLIP_FOG_COORD
] = clipINDEX0_INDEX1_FOG
;