Home | History | Annotate | Download | only in tnl
      1 /*
      2  * Mesa 3-D graphics library
      3  *
      4  * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the "Software"),
      8  * to deal in the Software without restriction, including without limitation
      9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  * and/or sell copies of the Software, and to permit persons to whom the
     11  * Software is furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be included
     14  * in all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
     20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     22  * OTHER DEALINGS IN THE SOFTWARE.
     23  *
     24  * Authors:
     25  *    Keith Whitwell <keithw (at) vmware.com>
     26  */
     27 
     28 
     29 #define CLIP_DOTPROD(K, A, B, C, D) X(K)*A + Y(K)*B + Z(K)*C + W(K)*D
     30 
     31 #define POLY_CLIP( PLANE_BIT, A, B, C, D )				\
     32 do {									\
     33    if (mask & PLANE_BIT) {						\
     34       GLuint idxPrev = inlist[0];					\
     35       GLfloat dpPrev = CLIP_DOTPROD(idxPrev, A, B, C, D );		\
     36       GLuint outcount = 0;						\
     37       GLuint i;								\
     38 									\
     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 );			\
     43 									\
     44 	 if (dpPrev >= 0.0f) {						\
     45 	    outlist[outcount++] = idxPrev;				\
     46 	 }								\
     47 									\
     48 	 if (DIFFERENT_SIGNS(dp, dpPrev)) {				\
     49 	    if (dp < 0.0f) {						\
     50 	       /* Going out of bounds.  Avoid division by zero as we	\
     51 		* know dp != dpPrev from DIFFERENT_SIGNS, above.	\
     52 		*/							\
     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 );	\
     56 	    } else {							\
     57 	       /* Coming back in.					\
     58 		*/							\
     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 );	\
     62 	    }								\
     63             outlist[outcount++] = newvert++;				\
     64 	 }								\
     65 									\
     66 	 idxPrev = idx;							\
     67 	 dpPrev = dp;							\
     68       }									\
     69 									\
     70       if (outcount < 3)							\
     71 	 return;							\
     72 									\
     73       {									\
     74 	 GLuint *tmp = inlist;						\
     75 	 inlist = outlist;						\
     76 	 outlist = tmp;							\
     77 	 n = outcount;							\
     78       }									\
     79    }									\
     80 } while (0)
     81 
     82 
     83 #define LINE_CLIP(PLANE_BIT, A, B, C, D )				\
     84 do {									\
     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 = dp0 < 0.0f;				\
     89       const GLboolean neg_dp1 = dp1 < 0.0f;				\
     90       									\
     91       /* For regular clipping, we know from the clipmask that one	\
     92        * (or both) of these must be negative (otherwise we wouldn't	\
     93        * be here).							\
     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 < 0.0f test:					\
     97        */								\
     98       if (neg_dp0 && neg_dp1)						\
     99          return; /* both vertices outside clip plane: discard */	\
    100 									\
    101       if (neg_dp1) {							\
    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;						\
    107       }									\
    108       if (t0 + t1 >= 1.0)						\
    109 	 return; /* discard */						\
    110    }									\
    111 } while (0)
    112 
    113 
    114 
    115 /* Clip a line against the viewport and user clip planes.
    116  */
    117 static inline void
    118 TAG(clip_line)( struct gl_context *ctx, GLuint v0, GLuint v1, GLubyte mask )
    119 {
    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;
    125    GLfloat t0 = 0;
    126    GLfloat t1 = 0;
    127    const GLuint v0_orig = v0;
    128 
    129    if (mask & CLIP_FRUSTUM_BITS) {
    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 );
    136    }
    137 
    138    if (mask & CLIP_USER_BIT) {
    139       GLbitfield enabled = ctx->Transform.ClipPlanesEnabled;
    140       while (enabled) {
    141          const int p = u_bit_scan(&enabled);
    142          const GLfloat a = ctx->Transform._ClipUserPlane[p][0];
    143          const GLfloat b = ctx->Transform._ClipUserPlane[p][1];
    144          const GLfloat c = ctx->Transform._ClipUserPlane[p][2];
    145          const GLfloat d = ctx->Transform._ClipUserPlane[p][3];
    146          LINE_CLIP( CLIP_USER_BIT, a, b, c, d );
    147       }
    148    }
    149 
    150    if (VB->ClipMask[v0]) {
    151       INTERP_4F( t0, coord[newvert], coord[v0], coord[v1] );
    152       interp( ctx, t0, newvert, v0, v1, GL_FALSE );
    153       v0 = newvert;
    154       newvert++;
    155    }
    156    else {
    157       assert(t0 == 0.0);
    158    }
    159 
    160    /* Note: we need to use vertex v0_orig when computing the new
    161     * interpolated/clipped vertex position, not the current v0 which
    162     * may have got set when we clipped the other end of the line!
    163     */
    164    if (VB->ClipMask[v1]) {
    165       INTERP_4F( t1, coord[newvert], coord[v1], coord[v0_orig] );
    166       interp( ctx, t1, newvert, v1, v0_orig, GL_FALSE );
    167 
    168       if (ctx->Light.ShadeModel == GL_FLAT)
    169 	 tnl->Driver.Render.CopyPV( ctx, newvert, v1 );
    170 
    171       v1 = newvert;
    172 
    173       newvert++;
    174    }
    175    else {
    176       assert(t1 == 0.0);
    177    }
    178 
    179    tnl->Driver.Render.ClippedLine( ctx, v0, v1 );
    180 }
    181 
    182 
    183 /* Clip a triangle against the viewport and user clip planes.
    184  */
    185 static inline void
    186 TAG(clip_tri)( struct gl_context *ctx, GLuint v0, GLuint v1, GLuint v2, GLubyte mask )
    187 {
    188    TNLcontext *tnl = TNL_CONTEXT(ctx);
    189    struct vertex_buffer *VB = &tnl->vb;
    190    tnl_interp_func interp = tnl->Driver.Render.Interp;
    191    GLuint newvert = VB->Count;
    192    GLfloat (*coord)[4] = VB->ClipPtr->data;
    193    GLuint pv = v2;
    194    GLuint vlist[2][MAX_CLIPPED_VERTICES];
    195    GLuint *inlist = vlist[0], *outlist = vlist[1];
    196    GLuint n = 3;
    197 
    198    ASSIGN_3V(inlist, v2, v0, v1 ); /* pv rotated to slot zero */
    199 
    200    if (0) {
    201       /* print pre-clip vertex coords */
    202       GLuint i, j;
    203       printf("pre clip:\n");
    204       for (i = 0; i < n; i++) {
    205          j = inlist[i];
    206          printf("  %u: %u: %f, %f, %f, %f\n",
    207 		i, j,
    208 		coord[j][0], coord[j][1], coord[j][2], coord[j][3]);
    209          assert(!IS_INF_OR_NAN(coord[j][0]));
    210          assert(!IS_INF_OR_NAN(coord[j][1]));
    211          assert(!IS_INF_OR_NAN(coord[j][2]));
    212          assert(!IS_INF_OR_NAN(coord[j][3]));
    213       }
    214    }
    215 
    216 
    217    if (mask & CLIP_FRUSTUM_BITS) {
    218       POLY_CLIP( CLIP_RIGHT_BIT,  -1,  0,  0, 1 );
    219       POLY_CLIP( CLIP_LEFT_BIT,    1,  0,  0, 1 );
    220       POLY_CLIP( CLIP_TOP_BIT,     0, -1,  0, 1 );
    221       POLY_CLIP( CLIP_BOTTOM_BIT,  0,  1,  0, 1 );
    222       POLY_CLIP( CLIP_FAR_BIT,     0,  0, -1, 1 );
    223       POLY_CLIP( CLIP_NEAR_BIT,    0,  0,  1, 1 );
    224    }
    225 
    226    if (mask & CLIP_USER_BIT) {
    227       GLbitfield enabled = ctx->Transform.ClipPlanesEnabled;
    228       while (enabled) {
    229          const int p = u_bit_scan(&enabled);
    230          const GLfloat a = ctx->Transform._ClipUserPlane[p][0];
    231          const GLfloat b = ctx->Transform._ClipUserPlane[p][1];
    232          const GLfloat c = ctx->Transform._ClipUserPlane[p][2];
    233          const GLfloat d = ctx->Transform._ClipUserPlane[p][3];
    234          POLY_CLIP( CLIP_USER_BIT, a, b, c, d );
    235       }
    236    }
    237 
    238    if (ctx->Light.ShadeModel == GL_FLAT) {
    239       if (pv != inlist[0]) {
    240 	 assert( inlist[0] >= VB->Count );
    241 	 tnl->Driver.Render.CopyPV( ctx, inlist[0], pv );
    242       }
    243    }
    244 
    245    if (0) {
    246       /* print post-clip vertex coords */
    247       GLuint i, j;
    248       printf("post clip:\n");
    249       for (i = 0; i < n; i++) {
    250          j = inlist[i];
    251          printf("  %u: %u: %f, %f, %f, %f\n",
    252 		i, j,
    253 		coord[j][0], coord[j][1], coord[j][2], coord[j][3]);
    254       }
    255    }
    256 
    257    tnl->Driver.Render.ClippedPolygon( ctx, inlist, n );
    258 }
    259 
    260 
    261 /* Clip a quad against the viewport and user clip planes.
    262  */
    263 static inline void
    264 TAG(clip_quad)( struct gl_context *ctx, GLuint v0, GLuint v1, GLuint v2, GLuint v3,
    265                 GLubyte mask )
    266 {
    267    TNLcontext *tnl = TNL_CONTEXT(ctx);
    268    struct vertex_buffer *VB = &tnl->vb;
    269    tnl_interp_func interp = tnl->Driver.Render.Interp;
    270    GLuint newvert = VB->Count;
    271    GLfloat (*coord)[4] = VB->ClipPtr->data;
    272    GLuint pv = v3;
    273    GLuint vlist[2][MAX_CLIPPED_VERTICES];
    274    GLuint *inlist = vlist[0], *outlist = vlist[1];
    275    GLuint n = 4;
    276 
    277    ASSIGN_4V(inlist, v3, v0, v1, v2 ); /* pv rotated to slot zero */
    278 
    279    if (mask & CLIP_FRUSTUM_BITS) {
    280       POLY_CLIP( CLIP_RIGHT_BIT,  -1,  0,  0, 1 );
    281       POLY_CLIP( CLIP_LEFT_BIT,    1,  0,  0, 1 );
    282       POLY_CLIP( CLIP_TOP_BIT,     0, -1,  0, 1 );
    283       POLY_CLIP( CLIP_BOTTOM_BIT,  0,  1,  0, 1 );
    284       POLY_CLIP( CLIP_FAR_BIT,     0,  0, -1, 1 );
    285       POLY_CLIP( CLIP_NEAR_BIT,    0,  0,  1, 1 );
    286    }
    287 
    288    if (mask & CLIP_USER_BIT) {
    289       GLbitfield enabled = ctx->Transform.ClipPlanesEnabled;
    290       while (enabled) {
    291          const int p = u_bit_scan(&enabled);
    292          const GLfloat a = ctx->Transform._ClipUserPlane[p][0];
    293          const GLfloat b = ctx->Transform._ClipUserPlane[p][1];
    294          const GLfloat c = ctx->Transform._ClipUserPlane[p][2];
    295          const GLfloat d = ctx->Transform._ClipUserPlane[p][3];
    296          POLY_CLIP( CLIP_USER_BIT, a, b, c, d );
    297       }
    298    }
    299 
    300    if (ctx->Light.ShadeModel == GL_FLAT) {
    301       if (pv != inlist[0]) {
    302 	 assert( inlist[0] >= VB->Count );
    303 	 tnl->Driver.Render.CopyPV( ctx, inlist[0], pv );
    304       }
    305    }
    306 
    307    tnl->Driver.Render.ClippedPolygon( ctx, inlist, n );
    308 }
    309 
    310 #undef W
    311 #undef Z
    312 #undef Y
    313 #undef X
    314 #undef SIZE
    315 #undef TAG
    316 #undef POLY_CLIP
    317 #undef LINE_CLIP
    318