• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static ALWAYS_INLINE void
cull_face(struct gl_context * ctx,GLenum mode,bool no_error)54 cull_face(struct gl_context *ctx, GLenum mode, bool no_error)
55 {
56    if (ctx->Polygon.CullFaceMode == mode)
57       return;
58 
59    if (!no_error &&
60        mode != GL_FRONT && mode != GL_BACK && mode != GL_FRONT_AND_BACK) {
61       _mesa_error(ctx, GL_INVALID_ENUM, "glCullFace");
62       return;
63    }
64 
65    FLUSH_VERTICES(ctx, ctx->DriverFlags.NewPolygonState ? 0 : _NEW_POLYGON);
66    ctx->NewDriverState |= ctx->DriverFlags.NewPolygonState;
67    ctx->Polygon.CullFaceMode = mode;
68 
69    if (ctx->Driver.CullFace)
70       ctx->Driver.CullFace(ctx, mode);
71 }
72 
73 
74 void GLAPIENTRY
_mesa_CullFace_no_error(GLenum mode)75 _mesa_CullFace_no_error(GLenum mode)
76 {
77    GET_CURRENT_CONTEXT(ctx);
78    cull_face(ctx, mode, true);
79 }
80 
81 
82 void GLAPIENTRY
_mesa_CullFace(GLenum mode)83 _mesa_CullFace(GLenum mode)
84 {
85    GET_CURRENT_CONTEXT(ctx);
86 
87    if (MESA_VERBOSE & VERBOSE_API)
88       _mesa_debug(ctx, "glCullFace %s\n", _mesa_enum_to_string(mode));
89 
90    cull_face(ctx, mode, false);
91 }
92 
93 
94 /**
95  * Define front- and back-facing
96  *
97  * \param mode orientation of front-facing polygons.
98  *
99  * \sa glFrontFace().
100  *
101  * Verifies the parameter and updates gl_polygon_attrib::FrontFace. On change
102  * flushes the vertices and notifies the driver via
103  * the dd_function_table::FrontFace callback.
104  */
105 static ALWAYS_INLINE void
front_face(struct gl_context * ctx,GLenum mode,bool no_error)106 front_face(struct gl_context *ctx, GLenum mode, bool no_error)
107 {
108    if (ctx->Polygon.FrontFace == mode)
109       return;
110 
111    if (!no_error && mode != GL_CW && mode != GL_CCW) {
112       _mesa_error(ctx, GL_INVALID_ENUM, "glFrontFace");
113       return;
114    }
115 
116    FLUSH_VERTICES(ctx, ctx->DriverFlags.NewPolygonState ? 0 : _NEW_POLYGON);
117    ctx->NewDriverState |= ctx->DriverFlags.NewPolygonState;
118    ctx->Polygon.FrontFace = mode;
119 
120    if (ctx->Driver.FrontFace)
121       ctx->Driver.FrontFace(ctx, mode);
122 }
123 
124 
125 void GLAPIENTRY
_mesa_FrontFace_no_error(GLenum mode)126 _mesa_FrontFace_no_error(GLenum mode)
127 {
128    GET_CURRENT_CONTEXT(ctx);
129    front_face(ctx, mode, true);
130 }
131 
132 
133 void GLAPIENTRY
_mesa_FrontFace(GLenum mode)134 _mesa_FrontFace(GLenum mode)
135 {
136    GET_CURRENT_CONTEXT(ctx);
137 
138    if (MESA_VERBOSE & VERBOSE_API)
139       _mesa_debug(ctx, "glFrontFace %s\n", _mesa_enum_to_string(mode));
140 
141    front_face(ctx, mode, false);
142 }
143 
144 
145 /**
146  * Set the polygon rasterization mode.
147  *
148  * \param face the polygons which \p mode applies to.
149  * \param mode how polygons should be rasterized.
150  *
151  * \sa glPolygonMode().
152  *
153  * Verifies the parameters and updates gl_polygon_attrib::FrontMode and
154  * gl_polygon_attrib::BackMode. On change flushes the vertices and notifies the
155  * driver via the dd_function_table::PolygonMode callback.
156  */
157 static ALWAYS_INLINE void
polygon_mode(struct gl_context * ctx,GLenum face,GLenum mode,bool no_error)158 polygon_mode(struct gl_context *ctx, GLenum face, GLenum mode, bool no_error)
159 {
160    if (MESA_VERBOSE & VERBOSE_API)
161       _mesa_debug(ctx, "glPolygonMode %s %s\n",
162                   _mesa_enum_to_string(face),
163                   _mesa_enum_to_string(mode));
164 
165    if (!no_error) {
166       switch (mode) {
167       case GL_POINT:
168       case GL_LINE:
169       case GL_FILL:
170          break;
171       case GL_FILL_RECTANGLE_NV:
172          if (ctx->Extensions.NV_fill_rectangle)
173             break;
174          /* fall-through */
175       default:
176          _mesa_error(ctx, GL_INVALID_ENUM, "glPolygonMode(mode)");
177          return;
178       }
179    }
180 
181    switch (face) {
182    case GL_FRONT:
183       if (!no_error && ctx->API == API_OPENGL_CORE) {
184          _mesa_error( ctx, GL_INVALID_ENUM, "glPolygonMode(face)" );
185          return;
186       }
187       if (ctx->Polygon.FrontMode == mode)
188          return;
189       FLUSH_VERTICES(ctx, ctx->DriverFlags.NewPolygonState ? 0 : _NEW_POLYGON);
190       ctx->NewDriverState |= ctx->DriverFlags.NewPolygonState;
191       ctx->Polygon.FrontMode = mode;
192       break;
193    case GL_FRONT_AND_BACK:
194       if (ctx->Polygon.FrontMode == mode && ctx->Polygon.BackMode == mode)
195          return;
196       FLUSH_VERTICES(ctx, ctx->DriverFlags.NewPolygonState ? 0 : _NEW_POLYGON);
197       ctx->NewDriverState |= ctx->DriverFlags.NewPolygonState;
198       ctx->Polygon.FrontMode = mode;
199       ctx->Polygon.BackMode = mode;
200       break;
201    case GL_BACK:
202       if (!no_error && ctx->API == API_OPENGL_CORE) {
203          _mesa_error( ctx, GL_INVALID_ENUM, "glPolygonMode(face)" );
204          return;
205       }
206       if (ctx->Polygon.BackMode == mode)
207          return;
208       FLUSH_VERTICES(ctx, ctx->DriverFlags.NewPolygonState ? 0 : _NEW_POLYGON);
209       ctx->NewDriverState |= ctx->DriverFlags.NewPolygonState;
210       ctx->Polygon.BackMode = mode;
211       break;
212    default:
213       if (!no_error)
214          _mesa_error( ctx, GL_INVALID_ENUM, "glPolygonMode(face)" );
215       return;
216    }
217 
218    if (ctx->Driver.PolygonMode)
219       ctx->Driver.PolygonMode(ctx, face, mode);
220 }
221 
222 
223 void GLAPIENTRY
_mesa_PolygonMode_no_error(GLenum face,GLenum mode)224 _mesa_PolygonMode_no_error(GLenum face, GLenum mode)
225 {
226    GET_CURRENT_CONTEXT(ctx);
227    polygon_mode(ctx, face, mode, true);
228 }
229 
230 
231 void GLAPIENTRY
_mesa_PolygonMode(GLenum face,GLenum mode)232 _mesa_PolygonMode(GLenum face, GLenum mode)
233 {
234    GET_CURRENT_CONTEXT(ctx);
235    polygon_mode(ctx, face, mode, false);
236 }
237 
238 
239 /**
240  * Called by glPolygonStipple.
241  */
242 void GLAPIENTRY
_mesa_PolygonStipple(const GLubyte * pattern)243 _mesa_PolygonStipple(const GLubyte *pattern)
244 {
245    GET_CURRENT_CONTEXT(ctx);
246 
247    if (MESA_VERBOSE & VERBOSE_API)
248       _mesa_debug(ctx, "glPolygonStipple\n");
249 
250    FLUSH_VERTICES(ctx, ctx->DriverFlags.NewPolygonStipple ? 0 :
251                                                       _NEW_POLYGONSTIPPLE);
252    ctx->NewDriverState |= ctx->DriverFlags.NewPolygonStipple;
253 
254    pattern = _mesa_map_validate_pbo_source(ctx, 2,
255                                            &ctx->Unpack, 32, 32, 1,
256                                            GL_COLOR_INDEX, GL_BITMAP,
257                                            INT_MAX, pattern,
258                                            "glPolygonStipple");
259    if (!pattern)
260       return;
261 
262    _mesa_unpack_polygon_stipple(pattern, ctx->PolygonStipple, &ctx->Unpack);
263 
264    _mesa_unmap_pbo_source(ctx, &ctx->Unpack);
265 
266    if (ctx->Driver.PolygonStipple)
267       ctx->Driver.PolygonStipple(ctx, pattern);
268 }
269 
270 
271 /**
272  * Called by glPolygonStipple.
273  */
274 void GLAPIENTRY
_mesa_GetnPolygonStippleARB(GLsizei bufSize,GLubyte * dest)275 _mesa_GetnPolygonStippleARB( GLsizei bufSize, GLubyte *dest )
276 {
277    GET_CURRENT_CONTEXT(ctx);
278 
279    if (MESA_VERBOSE&VERBOSE_API)
280       _mesa_debug(ctx, "glGetPolygonStipple\n");
281 
282    dest = _mesa_map_validate_pbo_dest(ctx, 2,
283                                       &ctx->Pack, 32, 32, 1,
284                                       GL_COLOR_INDEX, GL_BITMAP,
285                                       bufSize, dest, "glGetPolygonStipple");
286    if (!dest)
287       return;
288 
289    _mesa_pack_polygon_stipple(ctx->PolygonStipple, dest, &ctx->Pack);
290 
291    _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
292 }
293 
294 
295 void GLAPIENTRY
_mesa_GetPolygonStipple(GLubyte * dest)296 _mesa_GetPolygonStipple( GLubyte *dest )
297 {
298    _mesa_GetnPolygonStippleARB(INT_MAX, dest);
299 }
300 
301 void
_mesa_polygon_offset_clamp(struct gl_context * ctx,GLfloat factor,GLfloat units,GLfloat clamp)302 _mesa_polygon_offset_clamp(struct gl_context *ctx,
303                            GLfloat factor, GLfloat units, GLfloat clamp)
304 {
305    if (ctx->Polygon.OffsetFactor == factor &&
306        ctx->Polygon.OffsetUnits == units &&
307        ctx->Polygon.OffsetClamp == clamp)
308       return;
309 
310    FLUSH_VERTICES(ctx, ctx->DriverFlags.NewPolygonState ? 0 : _NEW_POLYGON);
311    ctx->NewDriverState |= ctx->DriverFlags.NewPolygonState;
312    ctx->Polygon.OffsetFactor = factor;
313    ctx->Polygon.OffsetUnits = units;
314    ctx->Polygon.OffsetClamp = clamp;
315 
316    if (ctx->Driver.PolygonOffset)
317       ctx->Driver.PolygonOffset( ctx, factor, units, clamp );
318 }
319 
320 void GLAPIENTRY
_mesa_PolygonOffset(GLfloat factor,GLfloat units)321 _mesa_PolygonOffset( GLfloat factor, GLfloat units )
322 {
323    GET_CURRENT_CONTEXT(ctx);
324 
325    if (MESA_VERBOSE&VERBOSE_API)
326       _mesa_debug(ctx, "glPolygonOffset %f %f\n", factor, units);
327 
328    _mesa_polygon_offset_clamp(ctx, factor, units, 0.0);
329 }
330 
331 void GLAPIENTRY
_mesa_PolygonOffsetEXT(GLfloat factor,GLfloat bias)332 _mesa_PolygonOffsetEXT( GLfloat factor, GLfloat bias )
333 {
334    GET_CURRENT_CONTEXT(ctx);
335    /* XXX mult by DepthMaxF here??? */
336    _mesa_PolygonOffset(factor, bias * ctx->DrawBuffer->_DepthMaxF );
337 }
338 
339 void GLAPIENTRY
_mesa_PolygonOffsetClampEXT(GLfloat factor,GLfloat units,GLfloat clamp)340 _mesa_PolygonOffsetClampEXT( GLfloat factor, GLfloat units, GLfloat clamp )
341 {
342    GET_CURRENT_CONTEXT(ctx);
343 
344    if (!ctx->Extensions.ARB_polygon_offset_clamp) {
345       _mesa_error(ctx, GL_INVALID_OPERATION,
346                   "unsupported function (%s) called", "glPolygonOffsetClamp");
347       return;
348    }
349 
350    if (MESA_VERBOSE&VERBOSE_API)
351       _mesa_debug(ctx, "glPolygonOffsetClamp %f %f %f\n", factor, units, clamp);
352 
353    _mesa_polygon_offset_clamp(ctx, factor, units, clamp);
354 }
355 
356 /**********************************************************************/
357 /** \name Initialization */
358 /*@{*/
359 
360 /**
361  * Initialize the context polygon state.
362  *
363  * \param ctx GL context.
364  *
365  * Initializes __struct gl_contextRec::Polygon and __struct gl_contextRec::PolygonStipple
366  * attribute groups.
367  */
_mesa_init_polygon(struct gl_context * ctx)368 void _mesa_init_polygon( struct gl_context * ctx )
369 {
370    /* Polygon group */
371    ctx->Polygon.CullFlag = GL_FALSE;
372    ctx->Polygon.CullFaceMode = GL_BACK;
373    ctx->Polygon.FrontFace = GL_CCW;
374    ctx->Polygon.FrontMode = GL_FILL;
375    ctx->Polygon.BackMode = GL_FILL;
376    ctx->Polygon.SmoothFlag = GL_FALSE;
377    ctx->Polygon.StippleFlag = GL_FALSE;
378    ctx->Polygon.OffsetFactor = 0.0F;
379    ctx->Polygon.OffsetUnits = 0.0F;
380    ctx->Polygon.OffsetClamp = 0.0F;
381    ctx->Polygon.OffsetPoint = GL_FALSE;
382    ctx->Polygon.OffsetLine = GL_FALSE;
383    ctx->Polygon.OffsetFill = GL_FALSE;
384 
385 
386    /* Polygon Stipple group */
387    memset( ctx->PolygonStipple, 0xff, 32*sizeof(GLuint) );
388 }
389 
390 /*@}*/
391