Merge vtx-0-2-branch
[mesa.git] / src / mesa / main / polygon.c
1 /**
2 * \file polygon.c
3 * Polygon operations.
4 */
5
6 /*
7 * Mesa 3-D graphics library
8 * Version: 4.1
9 *
10 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 */
29
30
31 #include "glheader.h"
32 #include "imports.h"
33 #include "context.h"
34 #include "image.h"
35 #include "enums.h"
36 #include "macros.h"
37 #include "polygon.h"
38 #include "mtypes.h"
39
40
41 /**
42 * Specify whether to cull front- or back-facing facets.
43 *
44 * \param mode culling mode.
45 *
46 * \sa glCullFace().
47 *
48 * Verifies the parameter and updates gl_polygon_attrib::CullFaceMode. On
49 * change, flushes the vertices and notifies the driver via
50 * the dd_function_table::CullFace callback.
51 */
52 void GLAPIENTRY
53 _mesa_CullFace( GLenum mode )
54 {
55 GET_CURRENT_CONTEXT(ctx);
56 ASSERT_OUTSIDE_BEGIN_END(ctx);
57
58 if (MESA_VERBOSE&VERBOSE_API)
59 _mesa_debug(ctx, "glCullFace %s\n", _mesa_lookup_enum_by_nr(mode));
60
61 if (mode!=GL_FRONT && mode!=GL_BACK && mode!=GL_FRONT_AND_BACK) {
62 _mesa_error( ctx, GL_INVALID_ENUM, "glCullFace" );
63 return;
64 }
65
66 if (ctx->Polygon.CullFaceMode == mode)
67 return;
68
69 FLUSH_VERTICES(ctx, _NEW_POLYGON);
70 ctx->Polygon.CullFaceMode = mode;
71
72 if (ctx->Driver.CullFace)
73 ctx->Driver.CullFace( ctx, mode );
74 }
75
76
77 /**
78 * Define front- and back-facing
79 *
80 * \param mode orientation of front-facing polygons.
81 *
82 * \sa glFrontFace().
83 *
84 * Verifies the parameter and updates gl_polygon_attrib::FrontFace. On change
85 * flushes the vertices and notifies the driver via
86 * the dd_function_table::FrontFace callback.
87 */
88 void GLAPIENTRY
89 _mesa_FrontFace( GLenum mode )
90 {
91 GET_CURRENT_CONTEXT(ctx);
92 ASSERT_OUTSIDE_BEGIN_END(ctx);
93
94 if (MESA_VERBOSE&VERBOSE_API)
95 _mesa_debug(ctx, "glFrontFace %s\n", _mesa_lookup_enum_by_nr(mode));
96
97 if (mode!=GL_CW && mode!=GL_CCW) {
98 _mesa_error( ctx, GL_INVALID_ENUM, "glFrontFace" );
99 return;
100 }
101
102 if (ctx->Polygon.FrontFace == mode)
103 return;
104
105 FLUSH_VERTICES(ctx, _NEW_POLYGON);
106 ctx->Polygon.FrontFace = mode;
107
108 ctx->Polygon._FrontBit = (GLboolean) (mode == GL_CW);
109
110 if (ctx->Driver.FrontFace)
111 ctx->Driver.FrontFace( ctx, mode );
112 }
113
114
115 /**
116 * Set the polygon rasterization mode.
117 *
118 * \param face the polygons which \p mode applies to.
119 * \param mode how polygons should be rasterized.
120 *
121 * \sa glPolygonMode().
122 *
123 * Verifies the parameters and updates gl_polygon_attrib::FrontMode and
124 * gl_polygon_attrib::BackMode. On change flushes the vertices and notifies the
125 * driver via the dd_function_table::PolygonMode callback.
126 */
127 void GLAPIENTRY
128 _mesa_PolygonMode( GLenum face, GLenum mode )
129 {
130 GET_CURRENT_CONTEXT(ctx);
131 ASSERT_OUTSIDE_BEGIN_END(ctx);
132
133 if (MESA_VERBOSE&VERBOSE_API)
134 _mesa_debug(ctx, "glPolygonMode %s %s\n",
135 _mesa_lookup_enum_by_nr(face),
136 _mesa_lookup_enum_by_nr(mode));
137
138 if (mode!=GL_POINT && mode!=GL_LINE && mode!=GL_FILL) {
139 _mesa_error( ctx, GL_INVALID_ENUM, "glPolygonMode(mode)" );
140 return;
141 }
142
143 switch (face) {
144 case GL_FRONT:
145 if (ctx->Polygon.FrontMode == mode)
146 return;
147 FLUSH_VERTICES(ctx, _NEW_POLYGON);
148 ctx->Polygon.FrontMode = mode;
149 break;
150 case GL_FRONT_AND_BACK:
151 if (ctx->Polygon.FrontMode == mode &&
152 ctx->Polygon.BackMode == mode)
153 return;
154 FLUSH_VERTICES(ctx, _NEW_POLYGON);
155 ctx->Polygon.FrontMode = mode;
156 ctx->Polygon.BackMode = mode;
157 break;
158 case GL_BACK:
159 if (ctx->Polygon.BackMode == mode)
160 return;
161 FLUSH_VERTICES(ctx, _NEW_POLYGON);
162 ctx->Polygon.BackMode = mode;
163 break;
164 default:
165 _mesa_error( ctx, GL_INVALID_ENUM, "glPolygonMode(face)" );
166 return;
167 }
168
169 ctx->_TriangleCaps &= ~DD_TRI_UNFILLED;
170 if (ctx->Polygon.FrontMode!=GL_FILL || ctx->Polygon.BackMode!=GL_FILL)
171 ctx->_TriangleCaps |= DD_TRI_UNFILLED;
172
173 if (ctx->Driver.PolygonMode) {
174 (*ctx->Driver.PolygonMode)( ctx, face, mode );
175 }
176 }
177
178 #if _HAVE_FULL_GL
179
180 void GLAPIENTRY
181 _mesa_PolygonStipple( const GLubyte *pattern )
182 {
183 GET_CURRENT_CONTEXT(ctx);
184 ASSERT_OUTSIDE_BEGIN_END(ctx);
185
186 if (MESA_VERBOSE&VERBOSE_API)
187 _mesa_debug(ctx, "glPolygonStipple\n");
188
189 FLUSH_VERTICES(ctx, _NEW_POLYGONSTIPPLE);
190 _mesa_unpack_polygon_stipple(pattern, ctx->PolygonStipple, &ctx->Unpack);
191
192 if (ctx->Driver.PolygonStipple)
193 ctx->Driver.PolygonStipple( ctx, (const GLubyte *) ctx->PolygonStipple );
194 }
195
196
197
198 void GLAPIENTRY
199 _mesa_GetPolygonStipple( GLubyte *dest )
200 {
201 GET_CURRENT_CONTEXT(ctx);
202 ASSERT_OUTSIDE_BEGIN_END(ctx);
203
204 if (MESA_VERBOSE&VERBOSE_API)
205 _mesa_debug(ctx, "glGetPolygonStipple\n");
206
207 _mesa_pack_polygon_stipple(ctx->PolygonStipple, dest, &ctx->Pack);
208 }
209
210
211 void GLAPIENTRY
212 _mesa_PolygonOffset( GLfloat factor, GLfloat units )
213 {
214 GET_CURRENT_CONTEXT(ctx);
215 ASSERT_OUTSIDE_BEGIN_END(ctx);
216
217 if (MESA_VERBOSE&VERBOSE_API)
218 _mesa_debug(ctx, "glPolygonOffset %f %f\n", factor, units);
219
220 if (ctx->Polygon.OffsetFactor == factor &&
221 ctx->Polygon.OffsetUnits == units)
222 return;
223
224 FLUSH_VERTICES(ctx, _NEW_POLYGON);
225 ctx->Polygon.OffsetFactor = factor;
226 ctx->Polygon.OffsetUnits = units;
227
228 if (ctx->Driver.PolygonOffset)
229 ctx->Driver.PolygonOffset( ctx, factor, units );
230 }
231
232
233
234 void GLAPIENTRY
235 _mesa_PolygonOffsetEXT( GLfloat factor, GLfloat bias )
236 {
237 GET_CURRENT_CONTEXT(ctx);
238 _mesa_PolygonOffset(factor, bias * ctx->DepthMaxF );
239 }
240
241 #endif
242
243
244 /**********************************************************************/
245 /** \name State Management */
246 /*@{*/
247
248 /*
249 * Check polygon state and set DD_TRI_CULL_FRONT_BACK and/or DD_TRI_OFFSET
250 * in ctx->_TriangleCaps if needed.
251 */
252 void _mesa_update_polygon( GLcontext *ctx )
253 {
254 ctx->_TriangleCaps &= ~(DD_TRI_CULL_FRONT_BACK | DD_TRI_OFFSET);
255
256 if (ctx->Polygon.CullFlag && ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK)
257 ctx->_TriangleCaps |= DD_TRI_CULL_FRONT_BACK;
258
259 /* Any Polygon offsets enabled? */
260 if (ctx->Polygon.OffsetPoint ||
261 ctx->Polygon.OffsetLine ||
262 ctx->Polygon.OffsetFill) {
263 ctx->_TriangleCaps |= DD_TRI_OFFSET;
264 }
265 }
266
267 /*@}*/
268
269
270 /**********************************************************************/
271 /** \name Initialization */
272 /*@{*/
273
274 /**
275 * Initialize the context polygon state.
276 *
277 * \param ctx GL context.
278 *
279 * Initializes __GLcontextRec::Polygon and __GLcontextRec::PolygonStipple
280 * attribute groups.
281 */
282 void _mesa_init_polygon( GLcontext * ctx )
283 {
284 /* Polygon group */
285 ctx->Polygon.CullFlag = GL_FALSE;
286 ctx->Polygon.CullFaceMode = GL_BACK;
287 ctx->Polygon.FrontFace = GL_CCW;
288 ctx->Polygon._FrontBit = 0;
289 ctx->Polygon.FrontMode = GL_FILL;
290 ctx->Polygon.BackMode = GL_FILL;
291 ctx->Polygon.SmoothFlag = GL_FALSE;
292 ctx->Polygon.StippleFlag = GL_FALSE;
293 ctx->Polygon.OffsetFactor = 0.0F;
294 ctx->Polygon.OffsetUnits = 0.0F;
295 ctx->Polygon.OffsetPoint = GL_FALSE;
296 ctx->Polygon.OffsetLine = GL_FALSE;
297 ctx->Polygon.OffsetFill = GL_FALSE;
298
299
300 /* Polygon Stipple group */
301 MEMSET( ctx->PolygonStipple, 0xff, 32*sizeof(GLuint) );
302 }
303
304 /*@}*/