76d601977b1805dd28d5fff49deddc21a642eed3
[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 *
9 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27 * 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 "pack.h"
37 #include "pbo.h"
38 #include "polygon.h"
39 #include "mtypes.h"
40
41
42 /**
43 * Specify whether to cull front- or back-facing facets.
44 *
45 * \param mode culling mode.
46 *
47 * \sa glCullFace().
48 *
49 * Verifies the parameter and updates gl_polygon_attrib::CullFaceMode. On
50 * change, flushes the vertices and notifies the driver via
51 * the dd_function_table::CullFace callback.
52 */
53 void GLAPIENTRY
54 _mesa_CullFace( GLenum mode )
55 {
56 GET_CURRENT_CONTEXT(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
93 if (MESA_VERBOSE&VERBOSE_API)
94 _mesa_debug(ctx, "glFrontFace %s\n", _mesa_lookup_enum_by_nr(mode));
95
96 if (mode!=GL_CW && mode!=GL_CCW) {
97 _mesa_error( ctx, GL_INVALID_ENUM, "glFrontFace" );
98 return;
99 }
100
101 if (ctx->Polygon.FrontFace == mode)
102 return;
103
104 FLUSH_VERTICES(ctx, _NEW_POLYGON);
105 ctx->Polygon.FrontFace = mode;
106
107 if (ctx->Driver.FrontFace)
108 ctx->Driver.FrontFace( ctx, mode );
109 }
110
111
112 /**
113 * Set the polygon rasterization mode.
114 *
115 * \param face the polygons which \p mode applies to.
116 * \param mode how polygons should be rasterized.
117 *
118 * \sa glPolygonMode().
119 *
120 * Verifies the parameters and updates gl_polygon_attrib::FrontMode and
121 * gl_polygon_attrib::BackMode. On change flushes the vertices and notifies the
122 * driver via the dd_function_table::PolygonMode callback.
123 */
124 void GLAPIENTRY
125 _mesa_PolygonMode( GLenum face, GLenum mode )
126 {
127 GET_CURRENT_CONTEXT(ctx);
128
129 if (MESA_VERBOSE&VERBOSE_API)
130 _mesa_debug(ctx, "glPolygonMode %s %s\n",
131 _mesa_lookup_enum_by_nr(face),
132 _mesa_lookup_enum_by_nr(mode));
133
134 if (mode!=GL_POINT && mode!=GL_LINE && mode!=GL_FILL) {
135 _mesa_error( ctx, GL_INVALID_ENUM, "glPolygonMode(mode)" );
136 return;
137 }
138
139 switch (face) {
140 case GL_FRONT:
141 if (ctx->API == API_OPENGL_CORE) {
142 _mesa_error( ctx, GL_INVALID_ENUM, "glPolygonMode(face)" );
143 return;
144 }
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->API == API_OPENGL_CORE) {
160 _mesa_error( ctx, GL_INVALID_ENUM, "glPolygonMode(face)" );
161 return;
162 }
163 if (ctx->Polygon.BackMode == mode)
164 return;
165 FLUSH_VERTICES(ctx, _NEW_POLYGON);
166 ctx->Polygon.BackMode = mode;
167 break;
168 default:
169 _mesa_error( ctx, GL_INVALID_ENUM, "glPolygonMode(face)" );
170 return;
171 }
172
173 if (ctx->Driver.PolygonMode)
174 ctx->Driver.PolygonMode(ctx, face, mode);
175 }
176
177
178 /**
179 * This routine updates the ctx->Polygon.Stipple state.
180 * If we're getting the stipple data from a PBO, we map the buffer
181 * in order to access the data.
182 * In any case, we obey the current pixel unpacking parameters when fetching
183 * the stipple data.
184 *
185 * In the future, this routine should be used as a fallback, called via
186 * ctx->Driver.PolygonStipple(). We'll have to update all the DRI drivers
187 * too.
188 */
189 void
190 _mesa_polygon_stipple(struct gl_context *ctx, const GLubyte *pattern)
191 {
192 pattern = _mesa_map_validate_pbo_source(ctx, 2,
193 &ctx->Unpack, 32, 32, 1,
194 GL_COLOR_INDEX, GL_BITMAP,
195 INT_MAX, pattern,
196 "glPolygonStipple");
197 if (!pattern)
198 return;
199
200 _mesa_unpack_polygon_stipple(pattern, ctx->PolygonStipple, &ctx->Unpack);
201
202 _mesa_unmap_pbo_source(ctx, &ctx->Unpack);
203 }
204
205
206 /**
207 * Called by glPolygonStipple.
208 */
209 void GLAPIENTRY
210 _mesa_PolygonStipple( const GLubyte *pattern )
211 {
212 GET_CURRENT_CONTEXT(ctx);
213
214 if (MESA_VERBOSE&VERBOSE_API)
215 _mesa_debug(ctx, "glPolygonStipple\n");
216
217 FLUSH_VERTICES(ctx, _NEW_POLYGONSTIPPLE);
218
219 _mesa_polygon_stipple(ctx, pattern);
220
221 if (ctx->Driver.PolygonStipple)
222 ctx->Driver.PolygonStipple(ctx, pattern);
223 }
224
225
226 /**
227 * Called by glPolygonStipple.
228 */
229 void GLAPIENTRY
230 _mesa_GetnPolygonStippleARB( GLsizei bufSize, GLubyte *dest )
231 {
232 GET_CURRENT_CONTEXT(ctx);
233
234 if (MESA_VERBOSE&VERBOSE_API)
235 _mesa_debug(ctx, "glGetPolygonStipple\n");
236
237 dest = _mesa_map_validate_pbo_dest(ctx, 2,
238 &ctx->Pack, 32, 32, 1,
239 GL_COLOR_INDEX, GL_BITMAP,
240 bufSize, dest, "glGetPolygonStipple");
241 if (!dest)
242 return;
243
244 _mesa_pack_polygon_stipple(ctx->PolygonStipple, dest, &ctx->Pack);
245
246 _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
247 }
248
249
250 void GLAPIENTRY
251 _mesa_GetPolygonStipple( GLubyte *dest )
252 {
253 _mesa_GetnPolygonStippleARB(INT_MAX, dest);
254 }
255
256
257 void GLAPIENTRY
258 _mesa_PolygonOffset( GLfloat factor, GLfloat units )
259 {
260 GET_CURRENT_CONTEXT(ctx);
261
262 if (MESA_VERBOSE&VERBOSE_API)
263 _mesa_debug(ctx, "glPolygonOffset %f %f\n", factor, units);
264
265 if (ctx->Polygon.OffsetFactor == factor &&
266 ctx->Polygon.OffsetUnits == units)
267 return;
268
269 FLUSH_VERTICES(ctx, _NEW_POLYGON);
270 ctx->Polygon.OffsetFactor = factor;
271 ctx->Polygon.OffsetUnits = units;
272
273 if (ctx->Driver.PolygonOffset)
274 ctx->Driver.PolygonOffset( ctx, factor, units );
275 }
276
277
278 void GLAPIENTRY
279 _mesa_PolygonOffsetEXT( GLfloat factor, GLfloat bias )
280 {
281 GET_CURRENT_CONTEXT(ctx);
282 /* XXX mult by DepthMaxF here??? */
283 _mesa_PolygonOffset(factor, bias * ctx->DrawBuffer->_DepthMaxF );
284 }
285
286
287
288 /**********************************************************************/
289 /** \name Initialization */
290 /*@{*/
291
292 /**
293 * Initialize the context polygon state.
294 *
295 * \param ctx GL context.
296 *
297 * Initializes __struct gl_contextRec::Polygon and __struct gl_contextRec::PolygonStipple
298 * attribute groups.
299 */
300 void _mesa_init_polygon( struct gl_context * ctx )
301 {
302 /* Polygon group */
303 ctx->Polygon.CullFlag = GL_FALSE;
304 ctx->Polygon.CullFaceMode = GL_BACK;
305 ctx->Polygon.FrontFace = GL_CCW;
306 ctx->Polygon._FrontBit = 0;
307 ctx->Polygon.FrontMode = GL_FILL;
308 ctx->Polygon.BackMode = GL_FILL;
309 ctx->Polygon.SmoothFlag = GL_FALSE;
310 ctx->Polygon.StippleFlag = GL_FALSE;
311 ctx->Polygon.OffsetFactor = 0.0F;
312 ctx->Polygon.OffsetUnits = 0.0F;
313 ctx->Polygon.OffsetPoint = GL_FALSE;
314 ctx->Polygon.OffsetLine = GL_FALSE;
315 ctx->Polygon.OffsetFill = GL_FALSE;
316
317
318 /* Polygon Stipple group */
319 memset( ctx->PolygonStipple, 0xff, 32*sizeof(GLuint) );
320 }
321
322 /*@}*/