17117f1b4Smrg/*
27117f1b4Smrg * Mesa 3-D graphics library
37117f1b4Smrg *
4c1f859d4Smrg * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
57117f1b4Smrg *
67117f1b4Smrg * Permission is hereby granted, free of charge, to any person obtaining a
77117f1b4Smrg * copy of this software and associated documentation files (the "Software"),
87117f1b4Smrg * to deal in the Software without restriction, including without limitation
97117f1b4Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
107117f1b4Smrg * and/or sell copies of the Software, and to permit persons to whom the
117117f1b4Smrg * Software is furnished to do so, subject to the following conditions:
127117f1b4Smrg *
137117f1b4Smrg * The above copyright notice and this permission notice shall be included
147117f1b4Smrg * in all copies or substantial portions of the Software.
157117f1b4Smrg *
167117f1b4Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
177117f1b4Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
187117f1b4Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19af69d88dSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20af69d88dSmrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21af69d88dSmrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22af69d88dSmrg * OTHER DEALINGS IN THE SOFTWARE.
237117f1b4Smrg *
247117f1b4Smrg * Authors:
25af69d88dSmrg *    Keith Whitwell <keithw@vmware.com>
267117f1b4Smrg */
277117f1b4Smrg
287117f1b4Smrg
29c1f859d4Smrg/**
30c1f859d4Smrg * This is where we handle assigning vertex colors based on front/back
31c1f859d4Smrg * facing, compute polygon offset and handle glPolygonMode().
32c1f859d4Smrg */
333464ebd5Sriastradhstatic void TAG(triangle)(struct gl_context *ctx, GLuint e0, GLuint e1, GLuint e2 )
347117f1b4Smrg{
357117f1b4Smrg   struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
36c1f859d4Smrg   SScontext *swsetup = SWSETUP_CONTEXT(ctx);
377117f1b4Smrg   SWvertex *verts = SWSETUP_CONTEXT(ctx)->verts;
387117f1b4Smrg   SWvertex *v[3];
397117f1b4Smrg   GLfloat z[3];
40c1f859d4Smrg   GLfloat offset, oz0, oz1, oz2;
417117f1b4Smrg   GLenum mode = GL_FILL;
427117f1b4Smrg   GLuint facing = 0;
43cdc920a0Smrg   GLchan saved_color[3][4] = { { 0 } };
444a49301eSmrg   GLfloat saved_col0[3][4] = { { 0 } };
454a49301eSmrg   GLfloat saved_spec[3][4] = { { 0 } };
467117f1b4Smrg
477117f1b4Smrg   v[0] = &verts[e0];
487117f1b4Smrg   v[1] = &verts[e1];
497117f1b4Smrg   v[2] = &verts[e2];
507117f1b4Smrg
517117f1b4Smrg   if (IND & (SS_TWOSIDE_BIT | SS_OFFSET_BIT | SS_UNFILLED_BIT))
527117f1b4Smrg   {
53af69d88dSmrg      GLfloat ex = v[0]->attrib[VARYING_SLOT_POS][0] - v[2]->attrib[VARYING_SLOT_POS][0];
54af69d88dSmrg      GLfloat ey = v[0]->attrib[VARYING_SLOT_POS][1] - v[2]->attrib[VARYING_SLOT_POS][1];
55af69d88dSmrg      GLfloat fx = v[1]->attrib[VARYING_SLOT_POS][0] - v[2]->attrib[VARYING_SLOT_POS][0];
56af69d88dSmrg      GLfloat fy = v[1]->attrib[VARYING_SLOT_POS][1] - v[2]->attrib[VARYING_SLOT_POS][1];
577117f1b4Smrg      GLfloat cc  = ex*fy - ey*fx;
587117f1b4Smrg
597117f1b4Smrg      if (IND & (SS_TWOSIDE_BIT | SS_UNFILLED_BIT))
607117f1b4Smrg      {
6101e04c3fSmrg	 facing = (cc < 0.0F) ^ _mesa_polygon_get_front_bit(ctx);
627117f1b4Smrg
637117f1b4Smrg	 if (IND & SS_UNFILLED_BIT)
647117f1b4Smrg	    mode = facing ? ctx->Polygon.BackMode : ctx->Polygon.FrontMode;
657117f1b4Smrg
667117f1b4Smrg	 if (facing == 1) {
677117f1b4Smrg	    if (IND & SS_TWOSIDE_BIT) {
68cdc920a0Smrg               if (VB->BackfaceColorPtr) {
69cdc920a0Smrg                  GLfloat (*vbcolor)[4] = VB->BackfaceColorPtr->data;
707117f1b4Smrg
71cdc920a0Smrg                  if (swsetup->intColors) {
72cdc920a0Smrg                     COPY_CHAN4(saved_color[0], v[0]->color);
73cdc920a0Smrg                     COPY_CHAN4(saved_color[1], v[1]->color);
74cdc920a0Smrg                     COPY_CHAN4(saved_color[2], v[2]->color);
75cdc920a0Smrg                  }
76cdc920a0Smrg                  else {
77af69d88dSmrg                     COPY_4V(saved_col0[0], v[0]->attrib[VARYING_SLOT_COL0]);
78af69d88dSmrg                     COPY_4V(saved_col0[1], v[1]->attrib[VARYING_SLOT_COL0]);
79af69d88dSmrg                     COPY_4V(saved_col0[2], v[2]->attrib[VARYING_SLOT_COL0]);
80cdc920a0Smrg                  }
81cdc920a0Smrg
82cdc920a0Smrg                  if (VB->BackfaceColorPtr->stride) {
83c1f859d4Smrg                     if (swsetup->intColors) {
84cdc920a0Smrg                        SS_COLOR(v[0]->color, vbcolor[e0]);
85cdc920a0Smrg                        SS_COLOR(v[1]->color, vbcolor[e1]);
86cdc920a0Smrg                        SS_COLOR(v[2]->color, vbcolor[e2]);
87c1f859d4Smrg                     }
88c1f859d4Smrg                     else {
89af69d88dSmrg                        COPY_4V(v[0]->attrib[VARYING_SLOT_COL0], vbcolor[e0]);
90af69d88dSmrg                        COPY_4V(v[1]->attrib[VARYING_SLOT_COL0], vbcolor[e1]);
91af69d88dSmrg                        COPY_4V(v[2]->attrib[VARYING_SLOT_COL0], vbcolor[e2]);
92c1f859d4Smrg                     }
93cdc920a0Smrg                  }
94cdc920a0Smrg                  else {
95cdc920a0Smrg                     /* flat shade */
96cdc920a0Smrg                     if (swsetup->intColors) {
97cdc920a0Smrg                        SS_COLOR(v[0]->color, vbcolor[0]);
98cdc920a0Smrg                        SS_COLOR(v[1]->color, vbcolor[0]);
99cdc920a0Smrg                        SS_COLOR(v[2]->color, vbcolor[0]);
1007117f1b4Smrg                     }
1017117f1b4Smrg                     else {
102af69d88dSmrg                        COPY_4V(v[0]->attrib[VARYING_SLOT_COL0], vbcolor[0]);
103af69d88dSmrg                        COPY_4V(v[1]->attrib[VARYING_SLOT_COL0], vbcolor[0]);
104af69d88dSmrg                        COPY_4V(v[2]->attrib[VARYING_SLOT_COL0], vbcolor[0]);
1057117f1b4Smrg                     }
1067117f1b4Smrg                  }
107cdc920a0Smrg               }
1087117f1b4Smrg
109cdc920a0Smrg               if (VB->BackfaceSecondaryColorPtr) {
110cdc920a0Smrg		  GLfloat (*vbspec)[4] = VB->BackfaceSecondaryColorPtr->data;
1117117f1b4Smrg
112af69d88dSmrg		  COPY_4V(saved_spec[0], v[0]->attrib[VARYING_SLOT_COL1]);
113af69d88dSmrg		  COPY_4V(saved_spec[1], v[1]->attrib[VARYING_SLOT_COL1]);
114af69d88dSmrg		  COPY_4V(saved_spec[2], v[2]->attrib[VARYING_SLOT_COL1]);
1157117f1b4Smrg
116cdc920a0Smrg		  if (VB->BackfaceSecondaryColorPtr->stride) {
117af69d88dSmrg		    SS_SPEC(v[0]->attrib[VARYING_SLOT_COL1], vbspec[e0]);
118af69d88dSmrg		    SS_SPEC(v[1]->attrib[VARYING_SLOT_COL1], vbspec[e1]);
119af69d88dSmrg		    SS_SPEC(v[2]->attrib[VARYING_SLOT_COL1], vbspec[e2]);
120cdc920a0Smrg		  }
121cdc920a0Smrg		  else {
122af69d88dSmrg		    SS_SPEC(v[0]->attrib[VARYING_SLOT_COL1], vbspec[0]);
123af69d88dSmrg		    SS_SPEC(v[1]->attrib[VARYING_SLOT_COL1], vbspec[0]);
124af69d88dSmrg		    SS_SPEC(v[2]->attrib[VARYING_SLOT_COL1], vbspec[0]);
1257117f1b4Smrg		  }
1267117f1b4Smrg	       }
1277117f1b4Smrg	    }
1287117f1b4Smrg	 }
1297117f1b4Smrg      }
1307117f1b4Smrg
131c1f859d4Smrg      if (IND & SS_OFFSET_BIT) {
132c1f859d4Smrg         const GLfloat max = ctx->DrawBuffer->_DepthMaxF;
133c1f859d4Smrg         /* save original Z values (restored later) */
134af69d88dSmrg	 z[0] = v[0]->attrib[VARYING_SLOT_POS][2];
135af69d88dSmrg	 z[1] = v[1]->attrib[VARYING_SLOT_POS][2];
136af69d88dSmrg	 z[2] = v[2]->attrib[VARYING_SLOT_POS][2];
137c1f859d4Smrg         /* Note that Z values are already scaled to [0,65535] (for example)
138c1f859d4Smrg          * so no MRD value is used here.
139c1f859d4Smrg          */
140c1f859d4Smrg	 offset = ctx->Polygon.OffsetUnits;
14101e04c3fSmrg	 if (cc * cc > 1e-16F) {
1427117f1b4Smrg	    const GLfloat ez = z[0] - z[2];
1437117f1b4Smrg	    const GLfloat fz = z[1] - z[2];
1447117f1b4Smrg	    const GLfloat oneOverArea = 1.0F / cc;
14501e04c3fSmrg	    const GLfloat dzdx = fabsf((ey * fz - ez * fy) * oneOverArea);
14601e04c3fSmrg	    const GLfloat dzdy = fabsf((ez * fx - ex * fz) * oneOverArea);
1477117f1b4Smrg	    offset += MAX2(dzdx, dzdy) * ctx->Polygon.OffsetFactor;
1487117f1b4Smrg	 }
149c1f859d4Smrg         /* new Z values */
150af69d88dSmrg         oz0 = CLAMP(v[0]->attrib[VARYING_SLOT_POS][2] + offset, 0.0F, max);
151af69d88dSmrg         oz1 = CLAMP(v[1]->attrib[VARYING_SLOT_POS][2] + offset, 0.0F, max);
152af69d88dSmrg         oz2 = CLAMP(v[2]->attrib[VARYING_SLOT_POS][2] + offset, 0.0F, max);
1537117f1b4Smrg      }
1547117f1b4Smrg   }
1557117f1b4Smrg
1567117f1b4Smrg   if (mode == GL_POINT) {
1577117f1b4Smrg      if ((IND & SS_OFFSET_BIT) && ctx->Polygon.OffsetPoint) {
158af69d88dSmrg	 v[0]->attrib[VARYING_SLOT_POS][2] = oz0;
159af69d88dSmrg	 v[1]->attrib[VARYING_SLOT_POS][2] = oz1;
160af69d88dSmrg	 v[2]->attrib[VARYING_SLOT_POS][2] = oz2;
1617117f1b4Smrg      }
1624a49301eSmrg      _swsetup_render_tri(ctx, e0, e1, e2, facing, _swsetup_edge_render_point_tri);
1637117f1b4Smrg   } else if (mode == GL_LINE) {
1647117f1b4Smrg      if ((IND & SS_OFFSET_BIT) && ctx->Polygon.OffsetLine) {
165af69d88dSmrg	 v[0]->attrib[VARYING_SLOT_POS][2] = oz0;
166af69d88dSmrg	 v[1]->attrib[VARYING_SLOT_POS][2] = oz1;
167af69d88dSmrg	 v[2]->attrib[VARYING_SLOT_POS][2] = oz2;
1687117f1b4Smrg      }
1694a49301eSmrg      _swsetup_render_tri(ctx, e0, e1, e2, facing, _swsetup_edge_render_line_tri);
1707117f1b4Smrg   } else {
1717117f1b4Smrg      if ((IND & SS_OFFSET_BIT) && ctx->Polygon.OffsetFill) {
172af69d88dSmrg	 v[0]->attrib[VARYING_SLOT_POS][2] = oz0;
173af69d88dSmrg	 v[1]->attrib[VARYING_SLOT_POS][2] = oz1;
174af69d88dSmrg	 v[2]->attrib[VARYING_SLOT_POS][2] = oz2;
1757117f1b4Smrg      }
1767117f1b4Smrg      _swrast_Triangle( ctx, v[0], v[1], v[2] );
1777117f1b4Smrg   }
1787117f1b4Smrg
179c1f859d4Smrg   /*
180c1f859d4Smrg    * Restore original vertex colors, etc.
181c1f859d4Smrg    */
1827117f1b4Smrg   if (IND & SS_OFFSET_BIT) {
183af69d88dSmrg      v[0]->attrib[VARYING_SLOT_POS][2] = z[0];
184af69d88dSmrg      v[1]->attrib[VARYING_SLOT_POS][2] = z[1];
185af69d88dSmrg      v[2]->attrib[VARYING_SLOT_POS][2] = z[2];
1867117f1b4Smrg   }
1877117f1b4Smrg
1887117f1b4Smrg   if (IND & SS_TWOSIDE_BIT) {
1897117f1b4Smrg      if (facing == 1) {
190cdc920a0Smrg	if (VB->BackfaceColorPtr) {
191cdc920a0Smrg	  if (swsetup->intColors) {
192cdc920a0Smrg	    COPY_CHAN4(v[0]->color, saved_color[0]);
193cdc920a0Smrg	    COPY_CHAN4(v[1]->color, saved_color[1]);
194cdc920a0Smrg	    COPY_CHAN4(v[2]->color, saved_color[2]);
195cdc920a0Smrg	  }
196cdc920a0Smrg	  else {
197af69d88dSmrg	    COPY_4V(v[0]->attrib[VARYING_SLOT_COL0], saved_col0[0]);
198af69d88dSmrg	    COPY_4V(v[1]->attrib[VARYING_SLOT_COL0], saved_col0[1]);
199af69d88dSmrg	    COPY_4V(v[2]->attrib[VARYING_SLOT_COL0], saved_col0[2]);
200cdc920a0Smrg	  }
201cdc920a0Smrg	}
202cdc920a0Smrg
203cdc920a0Smrg	if (VB->BackfaceSecondaryColorPtr) {
204af69d88dSmrg	  COPY_4V(v[0]->attrib[VARYING_SLOT_COL1], saved_spec[0]);
205af69d88dSmrg	  COPY_4V(v[1]->attrib[VARYING_SLOT_COL1], saved_spec[1]);
206af69d88dSmrg	  COPY_4V(v[2]->attrib[VARYING_SLOT_COL1], saved_spec[2]);
207cdc920a0Smrg	}
2087117f1b4Smrg      }
2097117f1b4Smrg   }
2107117f1b4Smrg}
2117117f1b4Smrg
2127117f1b4Smrg
2137117f1b4Smrg
2147117f1b4Smrg/* Need to fixup edgeflags when decomposing to triangles:
2157117f1b4Smrg */
2163464ebd5Sriastradhstatic void TAG(quadfunc)( struct gl_context *ctx, GLuint v0,
2177117f1b4Smrg		       GLuint v1, GLuint v2, GLuint v3 )
2187117f1b4Smrg{
2197117f1b4Smrg   if (IND & SS_UNFILLED_BIT) {
2207117f1b4Smrg      struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
2217117f1b4Smrg      if (VB->EdgeFlag) { /* XXX this test shouldn't be needed (bug 12614) */
2227117f1b4Smrg         GLubyte ef1 = VB->EdgeFlag[v1];
2237117f1b4Smrg         GLubyte ef3 = VB->EdgeFlag[v3];
2247117f1b4Smrg         VB->EdgeFlag[v1] = 0;
2257117f1b4Smrg         TAG(triangle)( ctx, v0, v1, v3 );
2267117f1b4Smrg         VB->EdgeFlag[v1] = ef1;
2277117f1b4Smrg         VB->EdgeFlag[v3] = 0;
2287117f1b4Smrg         TAG(triangle)( ctx, v1, v2, v3 );
2297117f1b4Smrg         VB->EdgeFlag[v3] = ef3;
2307117f1b4Smrg      }
2317117f1b4Smrg   } else {
2327117f1b4Smrg      TAG(triangle)( ctx, v0, v1, v3 );
2337117f1b4Smrg      TAG(triangle)( ctx, v1, v2, v3 );
2347117f1b4Smrg   }
2357117f1b4Smrg}
2367117f1b4Smrg
2377117f1b4Smrg
2387117f1b4Smrg
2397117f1b4Smrg
2407117f1b4Smrgstatic void TAG(init)( void )
2417117f1b4Smrg{
2427117f1b4Smrg   tri_tab[IND] = TAG(triangle);
2437117f1b4Smrg   quad_tab[IND] = TAG(quadfunc);
2447117f1b4Smrg}
2457117f1b4Smrg
2467117f1b4Smrg
2477117f1b4Smrg#undef IND
2487117f1b4Smrg#undef TAG
249