fix culling problem (bug 630649)
[mesa.git] / src / mesa / swrast_setup / ss_triangle.c
1 /* $Id: ss_triangle.c,v 1.20 2002/10/29 22:25:57 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
8 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
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.
25 *
26 * Authors:
27 * Keith Whitwell <keith@tungstengraphics.com>
28 */
29
30 #include "glheader.h"
31 #include "colormac.h"
32 #include "macros.h"
33 #include "mtypes.h"
34
35 #include "tnl/t_context.h"
36
37 #include "ss_triangle.h"
38 #include "ss_context.h"
39
40 #define SS_RGBA_BIT 0x1
41 #define SS_OFFSET_BIT 0x2
42 #define SS_TWOSIDE_BIT 0x4
43 #define SS_UNFILLED_BIT 0x8
44 #define SS_MAX_TRIFUNC 0x10
45
46 static triangle_func tri_tab[SS_MAX_TRIFUNC];
47 static quad_func quad_tab[SS_MAX_TRIFUNC];
48
49
50 static void _swsetup_render_line_tri( GLcontext *ctx,
51 GLuint e0, GLuint e1, GLuint e2,
52 GLuint facing )
53 {
54 SScontext *swsetup = SWSETUP_CONTEXT(ctx);
55 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
56 GLubyte *ef = VB->EdgeFlag;
57 SWvertex *verts = swsetup->verts;
58 SWvertex *v0 = &verts[e0];
59 SWvertex *v1 = &verts[e1];
60 SWvertex *v2 = &verts[e2];
61 GLchan c[2][4];
62 GLchan s[2][4];
63 GLuint i[2];
64
65 /* cull testing */
66 if (ctx->Polygon.CullFlag) {
67 if (facing == 1 && ctx->Polygon.CullFaceMode != GL_FRONT)
68 return;
69 if (facing == 0 && ctx->Polygon.CullFaceMode != GL_BACK)
70 return;
71 }
72
73 if (ctx->_TriangleCaps & DD_FLATSHADE) {
74 COPY_CHAN4(c[0], v0->color);
75 COPY_CHAN4(c[1], v1->color);
76 COPY_CHAN4(s[0], v0->specular);
77 COPY_CHAN4(s[1], v1->specular);
78 i[0] = v0->index;
79 i[1] = v1->index;
80
81 COPY_CHAN4(v0->color, v2->color);
82 COPY_CHAN4(v1->color, v2->color);
83 COPY_CHAN4(v0->specular, v2->specular);
84 COPY_CHAN4(v1->specular, v2->specular);
85 v0->index = v2->index;
86 v1->index = v2->index;
87 }
88
89 if (swsetup->render_prim == GL_POLYGON) {
90 if (ef[e2]) _swrast_Line( ctx, v2, v0 );
91 if (ef[e0]) _swrast_Line( ctx, v0, v1 );
92 if (ef[e1]) _swrast_Line( ctx, v1, v2 );
93 } else {
94 if (ef[e0]) _swrast_Line( ctx, v0, v1 );
95 if (ef[e1]) _swrast_Line( ctx, v1, v2 );
96 if (ef[e2]) _swrast_Line( ctx, v2, v0 );
97 }
98
99 if (ctx->_TriangleCaps & DD_FLATSHADE) {
100 COPY_CHAN4(v0->color, c[0]);
101 COPY_CHAN4(v1->color, c[1]);
102 COPY_CHAN4(v0->specular, s[0]);
103 COPY_CHAN4(v1->specular, s[1]);
104 v0->index = i[0];
105 v1->index = i[1];
106 }
107 }
108
109 static void _swsetup_render_point_tri( GLcontext *ctx,
110 GLuint e0, GLuint e1, GLuint e2,
111 GLuint facing )
112 {
113 SScontext *swsetup = SWSETUP_CONTEXT(ctx);
114 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
115 GLubyte *ef = VB->EdgeFlag;
116 SWvertex *verts = swsetup->verts;
117 SWvertex *v0 = &verts[e0];
118 SWvertex *v1 = &verts[e1];
119 SWvertex *v2 = &verts[e2];
120 GLchan c[2][4];
121 GLchan s[2][4];
122 GLuint i[2];
123
124 /* cull testing */
125 if (ctx->Polygon.CullFlag) {
126 if (facing == 1 && ctx->Polygon.CullFaceMode != GL_FRONT)
127 return;
128 if (facing == 0 && ctx->Polygon.CullFaceMode != GL_BACK)
129 return;
130 }
131
132 if (ctx->_TriangleCaps & DD_FLATSHADE) {
133 COPY_CHAN4(c[0], v0->color);
134 COPY_CHAN4(c[1], v1->color);
135 COPY_CHAN4(s[0], v0->specular);
136 COPY_CHAN4(s[1], v1->specular);
137 i[0] = v0->index;
138 i[1] = v1->index;
139
140 COPY_CHAN4(v0->color, v2->color);
141 COPY_CHAN4(v1->color, v2->color);
142 COPY_CHAN4(v0->specular, v2->specular);
143 COPY_CHAN4(v1->specular, v2->specular);
144 v0->index = v2->index;
145 v1->index = v2->index;
146 }
147
148 if (ef[e0]) _swrast_Point( ctx, v0 );
149 if (ef[e1]) _swrast_Point( ctx, v1 );
150 if (ef[e2]) _swrast_Point( ctx, v2 );
151
152 if (ctx->_TriangleCaps & DD_FLATSHADE) {
153 COPY_CHAN4(v0->color, c[0]);
154 COPY_CHAN4(v1->color, c[1]);
155 COPY_CHAN4(v0->specular, s[0]);
156 COPY_CHAN4(v1->specular, s[1]);
157 v0->index = i[0];
158 v1->index = i[1];
159 }
160 _swrast_flush(ctx);
161 }
162
163 #define SS_COLOR(a,b) COPY_CHAN4(a,b)
164 #define SS_SPEC(a,b) COPY_3V(a,b)
165 #define SS_IND(a,b) (a = b)
166
167 #define IND (0)
168 #define TAG(x) x
169 #include "ss_tritmp.h"
170
171 #define IND (SS_OFFSET_BIT)
172 #define TAG(x) x##_offset
173 #include "ss_tritmp.h"
174
175 #define IND (SS_TWOSIDE_BIT)
176 #define TAG(x) x##_twoside
177 #include "ss_tritmp.h"
178
179 #define IND (SS_OFFSET_BIT|SS_TWOSIDE_BIT)
180 #define TAG(x) x##_offset_twoside
181 #include "ss_tritmp.h"
182
183 #define IND (SS_UNFILLED_BIT)
184 #define TAG(x) x##_unfilled
185 #include "ss_tritmp.h"
186
187 #define IND (SS_OFFSET_BIT|SS_UNFILLED_BIT)
188 #define TAG(x) x##_offset_unfilled
189 #include "ss_tritmp.h"
190
191 #define IND (SS_TWOSIDE_BIT|SS_UNFILLED_BIT)
192 #define TAG(x) x##_twoside_unfilled
193 #include "ss_tritmp.h"
194
195 #define IND (SS_OFFSET_BIT|SS_TWOSIDE_BIT|SS_UNFILLED_BIT)
196 #define TAG(x) x##_offset_twoside_unfilled
197 #include "ss_tritmp.h"
198
199 #define IND (0|SS_RGBA_BIT)
200 #define TAG(x) x##_rgba
201 #include "ss_tritmp.h"
202
203 #define IND (SS_OFFSET_BIT|SS_RGBA_BIT)
204 #define TAG(x) x##_offset_rgba
205 #include "ss_tritmp.h"
206
207 #define IND (SS_TWOSIDE_BIT|SS_RGBA_BIT)
208 #define TAG(x) x##_twoside_rgba
209 #include "ss_tritmp.h"
210
211 #define IND (SS_OFFSET_BIT|SS_TWOSIDE_BIT|SS_RGBA_BIT)
212 #define TAG(x) x##_offset_twoside_rgba
213 #include "ss_tritmp.h"
214
215 #define IND (SS_UNFILLED_BIT|SS_RGBA_BIT)
216 #define TAG(x) x##_unfilled_rgba
217 #include "ss_tritmp.h"
218
219 #define IND (SS_OFFSET_BIT|SS_UNFILLED_BIT|SS_RGBA_BIT)
220 #define TAG(x) x##_offset_unfilled_rgba
221 #include "ss_tritmp.h"
222
223 #define IND (SS_TWOSIDE_BIT|SS_UNFILLED_BIT|SS_RGBA_BIT)
224 #define TAG(x) x##_twoside_unfilled_rgba
225 #include "ss_tritmp.h"
226
227 #define IND (SS_OFFSET_BIT|SS_TWOSIDE_BIT|SS_UNFILLED_BIT|SS_RGBA_BIT)
228 #define TAG(x) x##_offset_twoside_unfilled_rgba
229 #include "ss_tritmp.h"
230
231
232 void _swsetup_trifuncs_init( GLcontext *ctx )
233 {
234 (void) ctx;
235
236 init();
237 init_offset();
238 init_twoside();
239 init_offset_twoside();
240 init_unfilled();
241 init_offset_unfilled();
242 init_twoside_unfilled();
243 init_offset_twoside_unfilled();
244
245 init_rgba();
246 init_offset_rgba();
247 init_twoside_rgba();
248 init_offset_twoside_rgba();
249 init_unfilled_rgba();
250 init_offset_unfilled_rgba();
251 init_twoside_unfilled_rgba();
252 init_offset_twoside_unfilled_rgba();
253 }
254
255
256 static void swsetup_points( GLcontext *ctx, GLuint first, GLuint last )
257 {
258 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
259 SWvertex *verts = SWSETUP_CONTEXT(ctx)->verts;
260 GLuint i;
261
262 if (VB->Elts) {
263 for (i = first; i < last; i++)
264 if (VB->ClipMask[VB->Elts[i]] == 0)
265 _swrast_Point( ctx, &verts[VB->Elts[i]] );
266 }
267 else {
268 for (i = first; i < last; i++)
269 if (VB->ClipMask[i] == 0)
270 _swrast_Point( ctx, &verts[i] );
271 }
272 }
273
274 static void swsetup_line( GLcontext *ctx, GLuint v0, GLuint v1 )
275 {
276 SWvertex *verts = SWSETUP_CONTEXT(ctx)->verts;
277 _swrast_Line( ctx, &verts[v0], &verts[v1] );
278 }
279
280
281
282 void _swsetup_choose_trifuncs( GLcontext *ctx )
283 {
284 TNLcontext *tnl = TNL_CONTEXT(ctx);
285 GLuint ind = 0;
286
287 if (ctx->Polygon.OffsetPoint ||
288 ctx->Polygon.OffsetLine ||
289 ctx->Polygon.OffsetFill)
290 ind |= SS_OFFSET_BIT;
291
292 if (ctx->Light.Enabled && ctx->Light.Model.TwoSide)
293 ind |= SS_TWOSIDE_BIT;
294
295 /* We piggyback the two-sided stencil front/back determination on the
296 * unfilled triangle path.
297 */
298 if ((ctx->_TriangleCaps & DD_TRI_UNFILLED) ||
299 (ctx->Stencil.Enabled && ctx->Stencil.TestTwoSide))
300 ind |= SS_UNFILLED_BIT;
301
302 if (ctx->Visual.rgbMode)
303 ind |= SS_RGBA_BIT;
304
305 tnl->Driver.Render.Triangle = tri_tab[ind];
306 tnl->Driver.Render.Quad = quad_tab[ind];
307 tnl->Driver.Render.Line = swsetup_line;
308 tnl->Driver.Render.Points = swsetup_points;
309
310 ctx->_Facing = 0;
311 }