2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
26 * Keith Whitwell <keith@tungstengraphics.com>
31 * This is where we handle assigning vertex colors based on front/back
32 * facing, compute polygon offset and handle glPolygonMode().
34 static void TAG(triangle
)(struct gl_context
*ctx
, GLuint e0
, GLuint e1
, GLuint e2
)
36 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
37 SScontext
*swsetup
= SWSETUP_CONTEXT(ctx
);
38 SWvertex
*verts
= SWSETUP_CONTEXT(ctx
)->verts
;
41 GLfloat offset
, oz0
, oz1
, oz2
;
42 GLenum mode
= GL_FILL
;
44 GLchan saved_color
[3][4] = { { 0 } };
45 GLfloat saved_col0
[3][4] = { { 0 } };
46 GLfloat saved_spec
[3][4] = { { 0 } };
52 if (IND
& (SS_TWOSIDE_BIT
| SS_OFFSET_BIT
| SS_UNFILLED_BIT
))
54 GLfloat ex
= v
[0]->attrib
[VARYING_SLOT_POS
][0] - v
[2]->attrib
[VARYING_SLOT_POS
][0];
55 GLfloat ey
= v
[0]->attrib
[VARYING_SLOT_POS
][1] - v
[2]->attrib
[VARYING_SLOT_POS
][1];
56 GLfloat fx
= v
[1]->attrib
[VARYING_SLOT_POS
][0] - v
[2]->attrib
[VARYING_SLOT_POS
][0];
57 GLfloat fy
= v
[1]->attrib
[VARYING_SLOT_POS
][1] - v
[2]->attrib
[VARYING_SLOT_POS
][1];
58 GLfloat cc
= ex
*fy
- ey
*fx
;
60 if (IND
& (SS_TWOSIDE_BIT
| SS_UNFILLED_BIT
))
62 facing
= (cc
< 0.0) ^ ctx
->Polygon
._FrontBit
;
64 if (IND
& SS_UNFILLED_BIT
)
65 mode
= facing
? ctx
->Polygon
.BackMode
: ctx
->Polygon
.FrontMode
;
68 if (IND
& SS_TWOSIDE_BIT
) {
69 if (VB
->BackfaceColorPtr
) {
70 GLfloat (*vbcolor
)[4] = VB
->BackfaceColorPtr
->data
;
72 if (swsetup
->intColors
) {
73 COPY_CHAN4(saved_color
[0], v
[0]->color
);
74 COPY_CHAN4(saved_color
[1], v
[1]->color
);
75 COPY_CHAN4(saved_color
[2], v
[2]->color
);
78 COPY_4V(saved_col0
[0], v
[0]->attrib
[VARYING_SLOT_COL0
]);
79 COPY_4V(saved_col0
[1], v
[1]->attrib
[VARYING_SLOT_COL0
]);
80 COPY_4V(saved_col0
[2], v
[2]->attrib
[VARYING_SLOT_COL0
]);
83 if (VB
->BackfaceColorPtr
->stride
) {
84 if (swsetup
->intColors
) {
85 SS_COLOR(v
[0]->color
, vbcolor
[e0
]);
86 SS_COLOR(v
[1]->color
, vbcolor
[e1
]);
87 SS_COLOR(v
[2]->color
, vbcolor
[e2
]);
90 COPY_4V(v
[0]->attrib
[VARYING_SLOT_COL0
], vbcolor
[e0
]);
91 COPY_4V(v
[1]->attrib
[VARYING_SLOT_COL0
], vbcolor
[e1
]);
92 COPY_4V(v
[2]->attrib
[VARYING_SLOT_COL0
], vbcolor
[e2
]);
97 if (swsetup
->intColors
) {
98 SS_COLOR(v
[0]->color
, vbcolor
[0]);
99 SS_COLOR(v
[1]->color
, vbcolor
[0]);
100 SS_COLOR(v
[2]->color
, vbcolor
[0]);
103 COPY_4V(v
[0]->attrib
[VARYING_SLOT_COL0
], vbcolor
[0]);
104 COPY_4V(v
[1]->attrib
[VARYING_SLOT_COL0
], vbcolor
[0]);
105 COPY_4V(v
[2]->attrib
[VARYING_SLOT_COL0
], vbcolor
[0]);
110 if (VB
->BackfaceSecondaryColorPtr
) {
111 GLfloat (*vbspec
)[4] = VB
->BackfaceSecondaryColorPtr
->data
;
113 COPY_4V(saved_spec
[0], v
[0]->attrib
[VARYING_SLOT_COL1
]);
114 COPY_4V(saved_spec
[1], v
[1]->attrib
[VARYING_SLOT_COL1
]);
115 COPY_4V(saved_spec
[2], v
[2]->attrib
[VARYING_SLOT_COL1
]);
117 if (VB
->BackfaceSecondaryColorPtr
->stride
) {
118 SS_SPEC(v
[0]->attrib
[VARYING_SLOT_COL1
], vbspec
[e0
]);
119 SS_SPEC(v
[1]->attrib
[VARYING_SLOT_COL1
], vbspec
[e1
]);
120 SS_SPEC(v
[2]->attrib
[VARYING_SLOT_COL1
], vbspec
[e2
]);
123 SS_SPEC(v
[0]->attrib
[VARYING_SLOT_COL1
], vbspec
[0]);
124 SS_SPEC(v
[1]->attrib
[VARYING_SLOT_COL1
], vbspec
[0]);
125 SS_SPEC(v
[2]->attrib
[VARYING_SLOT_COL1
], vbspec
[0]);
132 if (IND
& SS_OFFSET_BIT
) {
133 const GLfloat max
= ctx
->DrawBuffer
->_DepthMaxF
;
134 /* save original Z values (restored later) */
135 z
[0] = v
[0]->attrib
[VARYING_SLOT_POS
][2];
136 z
[1] = v
[1]->attrib
[VARYING_SLOT_POS
][2];
137 z
[2] = v
[2]->attrib
[VARYING_SLOT_POS
][2];
138 /* Note that Z values are already scaled to [0,65535] (for example)
139 * so no MRD value is used here.
141 offset
= ctx
->Polygon
.OffsetUnits
;
142 if (cc
* cc
> 1e-16) {
143 const GLfloat ez
= z
[0] - z
[2];
144 const GLfloat fz
= z
[1] - z
[2];
145 const GLfloat oneOverArea
= 1.0F
/ cc
;
146 const GLfloat dzdx
= FABSF((ey
* fz
- ez
* fy
) * oneOverArea
);
147 const GLfloat dzdy
= FABSF((ez
* fx
- ex
* fz
) * oneOverArea
);
148 offset
+= MAX2(dzdx
, dzdy
) * ctx
->Polygon
.OffsetFactor
;
151 oz0
= CLAMP(v
[0]->attrib
[VARYING_SLOT_POS
][2] + offset
, 0.0F
, max
);
152 oz1
= CLAMP(v
[1]->attrib
[VARYING_SLOT_POS
][2] + offset
, 0.0F
, max
);
153 oz2
= CLAMP(v
[2]->attrib
[VARYING_SLOT_POS
][2] + offset
, 0.0F
, max
);
157 if (mode
== GL_POINT
) {
158 if ((IND
& SS_OFFSET_BIT
) && ctx
->Polygon
.OffsetPoint
) {
159 v
[0]->attrib
[VARYING_SLOT_POS
][2] = oz0
;
160 v
[1]->attrib
[VARYING_SLOT_POS
][2] = oz1
;
161 v
[2]->attrib
[VARYING_SLOT_POS
][2] = oz2
;
163 _swsetup_render_tri(ctx
, e0
, e1
, e2
, facing
, _swsetup_edge_render_point_tri
);
164 } else if (mode
== GL_LINE
) {
165 if ((IND
& SS_OFFSET_BIT
) && ctx
->Polygon
.OffsetLine
) {
166 v
[0]->attrib
[VARYING_SLOT_POS
][2] = oz0
;
167 v
[1]->attrib
[VARYING_SLOT_POS
][2] = oz1
;
168 v
[2]->attrib
[VARYING_SLOT_POS
][2] = oz2
;
170 _swsetup_render_tri(ctx
, e0
, e1
, e2
, facing
, _swsetup_edge_render_line_tri
);
172 if ((IND
& SS_OFFSET_BIT
) && ctx
->Polygon
.OffsetFill
) {
173 v
[0]->attrib
[VARYING_SLOT_POS
][2] = oz0
;
174 v
[1]->attrib
[VARYING_SLOT_POS
][2] = oz1
;
175 v
[2]->attrib
[VARYING_SLOT_POS
][2] = oz2
;
177 _swrast_Triangle( ctx
, v
[0], v
[1], v
[2] );
181 * Restore original vertex colors, etc.
183 if (IND
& SS_OFFSET_BIT
) {
184 v
[0]->attrib
[VARYING_SLOT_POS
][2] = z
[0];
185 v
[1]->attrib
[VARYING_SLOT_POS
][2] = z
[1];
186 v
[2]->attrib
[VARYING_SLOT_POS
][2] = z
[2];
189 if (IND
& SS_TWOSIDE_BIT
) {
191 if (VB
->BackfaceColorPtr
) {
192 if (swsetup
->intColors
) {
193 COPY_CHAN4(v
[0]->color
, saved_color
[0]);
194 COPY_CHAN4(v
[1]->color
, saved_color
[1]);
195 COPY_CHAN4(v
[2]->color
, saved_color
[2]);
198 COPY_4V(v
[0]->attrib
[VARYING_SLOT_COL0
], saved_col0
[0]);
199 COPY_4V(v
[1]->attrib
[VARYING_SLOT_COL0
], saved_col0
[1]);
200 COPY_4V(v
[2]->attrib
[VARYING_SLOT_COL0
], saved_col0
[2]);
204 if (VB
->BackfaceSecondaryColorPtr
) {
205 COPY_4V(v
[0]->attrib
[VARYING_SLOT_COL1
], saved_spec
[0]);
206 COPY_4V(v
[1]->attrib
[VARYING_SLOT_COL1
], saved_spec
[1]);
207 COPY_4V(v
[2]->attrib
[VARYING_SLOT_COL1
], saved_spec
[2]);
215 /* Need to fixup edgeflags when decomposing to triangles:
217 static void TAG(quadfunc
)( struct gl_context
*ctx
, GLuint v0
,
218 GLuint v1
, GLuint v2
, GLuint v3
)
220 if (IND
& SS_UNFILLED_BIT
) {
221 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
222 if (VB
->EdgeFlag
) { /* XXX this test shouldn't be needed (bug 12614) */
223 GLubyte ef1
= VB
->EdgeFlag
[v1
];
224 GLubyte ef3
= VB
->EdgeFlag
[v3
];
225 VB
->EdgeFlag
[v1
] = 0;
226 TAG(triangle
)( ctx
, v0
, v1
, v3
);
227 VB
->EdgeFlag
[v1
] = ef1
;
228 VB
->EdgeFlag
[v3
] = 0;
229 TAG(triangle
)( ctx
, v1
, v2
, v3
);
230 VB
->EdgeFlag
[v3
] = ef3
;
233 TAG(triangle
)( ctx
, v0
, v1
, v3
);
234 TAG(triangle
)( ctx
, v1
, v2
, v3
);
241 static void TAG(init
)( void )
243 tri_tab
[IND
] = TAG(triangle
);
244 quad_tab
[IND
] = TAG(quadfunc
);