points.c revision af69d88d
17117f1b4Smrg/**
27117f1b4Smrg * \file points.c
37117f1b4Smrg * Point operations.
47117f1b4Smrg */
57117f1b4Smrg
67117f1b4Smrg/*
77117f1b4Smrg * Mesa 3-D graphics library
87117f1b4Smrg *
97117f1b4Smrg * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
107117f1b4Smrg *
117117f1b4Smrg * Permission is hereby granted, free of charge, to any person obtaining a
127117f1b4Smrg * copy of this software and associated documentation files (the "Software"),
137117f1b4Smrg * to deal in the Software without restriction, including without limitation
147117f1b4Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
157117f1b4Smrg * and/or sell copies of the Software, and to permit persons to whom the
167117f1b4Smrg * Software is furnished to do so, subject to the following conditions:
177117f1b4Smrg *
187117f1b4Smrg * The above copyright notice and this permission notice shall be included
197117f1b4Smrg * in all copies or substantial portions of the Software.
207117f1b4Smrg *
217117f1b4Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
227117f1b4Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
237117f1b4Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
24af69d88dSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25af69d88dSmrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26af69d88dSmrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27af69d88dSmrg * OTHER DEALINGS IN THE SOFTWARE.
287117f1b4Smrg */
297117f1b4Smrg
307117f1b4Smrg
317117f1b4Smrg#include "glheader.h"
327117f1b4Smrg#include "context.h"
337117f1b4Smrg#include "macros.h"
347117f1b4Smrg#include "points.h"
357117f1b4Smrg#include "mtypes.h"
367117f1b4Smrg
377117f1b4Smrg
387117f1b4Smrg/**
397117f1b4Smrg * Set current point size.
407117f1b4Smrg * \param size  point diameter in pixels
417117f1b4Smrg * \sa glPointSize().
427117f1b4Smrg */
437117f1b4Smrgvoid GLAPIENTRY
447117f1b4Smrg_mesa_PointSize( GLfloat size )
457117f1b4Smrg{
467117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
477117f1b4Smrg
487117f1b4Smrg   if (size <= 0.0) {
497117f1b4Smrg      _mesa_error( ctx, GL_INVALID_VALUE, "glPointSize" );
507117f1b4Smrg      return;
517117f1b4Smrg   }
527117f1b4Smrg
537117f1b4Smrg   if (ctx->Point.Size == size)
547117f1b4Smrg      return;
557117f1b4Smrg
567117f1b4Smrg   FLUSH_VERTICES(ctx, _NEW_POINT);
577117f1b4Smrg   ctx->Point.Size = size;
587117f1b4Smrg
597117f1b4Smrg   if (ctx->Driver.PointSize)
607117f1b4Smrg      ctx->Driver.PointSize(ctx, size);
617117f1b4Smrg}
627117f1b4Smrg
637117f1b4Smrg
647117f1b4Smrgvoid GLAPIENTRY
65c1f859d4Smrg_mesa_PointParameteri( GLenum pname, GLint param )
667117f1b4Smrg{
674a49301eSmrg   GLfloat p[3];
684a49301eSmrg   p[0] = (GLfloat) param;
694a49301eSmrg   p[1] = p[2] = 0.0F;
704a49301eSmrg   _mesa_PointParameterfv(pname, p);
717117f1b4Smrg}
727117f1b4Smrg
737117f1b4Smrg
747117f1b4Smrgvoid GLAPIENTRY
75c1f859d4Smrg_mesa_PointParameteriv( GLenum pname, const GLint *params )
767117f1b4Smrg{
777117f1b4Smrg   GLfloat p[3];
787117f1b4Smrg   p[0] = (GLfloat) params[0];
797117f1b4Smrg   if (pname == GL_DISTANCE_ATTENUATION_EXT) {
807117f1b4Smrg      p[1] = (GLfloat) params[1];
817117f1b4Smrg      p[2] = (GLfloat) params[2];
827117f1b4Smrg   }
83c1f859d4Smrg   _mesa_PointParameterfv(pname, p);
847117f1b4Smrg}
857117f1b4Smrg
867117f1b4Smrg
877117f1b4Smrgvoid GLAPIENTRY
88c1f859d4Smrg_mesa_PointParameterf( GLenum pname, GLfloat param)
897117f1b4Smrg{
904a49301eSmrg   GLfloat p[3];
914a49301eSmrg   p[0] = param;
924a49301eSmrg   p[1] = p[2] = 0.0F;
934a49301eSmrg   _mesa_PointParameterfv(pname, p);
947117f1b4Smrg}
957117f1b4Smrg
967117f1b4Smrg
977117f1b4Smrgvoid GLAPIENTRY
98c1f859d4Smrg_mesa_PointParameterfv( GLenum pname, const GLfloat *params)
997117f1b4Smrg{
1007117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
101af69d88dSmrg
102af69d88dSmrg   /* Drivers that support point sprites must also support point parameters.
103af69d88dSmrg    * If point parameters aren't supported, then this function shouldn't even
104af69d88dSmrg    * exist.
105af69d88dSmrg    */
106af69d88dSmrg   ASSERT(!(ctx->Extensions.ARB_point_sprite
107af69d88dSmrg            || ctx->Extensions.NV_point_sprite)
108af69d88dSmrg          || ctx->Extensions.EXT_point_parameters);
109af69d88dSmrg
110af69d88dSmrg   if (!ctx->Extensions.EXT_point_parameters) {
111af69d88dSmrg      _mesa_error(ctx, GL_INVALID_OPERATION,
112af69d88dSmrg                  "unsupported function called (unsupported extension)");
113af69d88dSmrg      return;
114af69d88dSmrg   }
1157117f1b4Smrg
1167117f1b4Smrg   switch (pname) {
1177117f1b4Smrg      case GL_DISTANCE_ATTENUATION_EXT:
118af69d88dSmrg         if (TEST_EQ_3V(ctx->Point.Params, params))
1197117f1b4Smrg            return;
120af69d88dSmrg         FLUSH_VERTICES(ctx, _NEW_POINT);
121af69d88dSmrg         COPY_3V(ctx->Point.Params, params);
122af69d88dSmrg         ctx->Point._Attenuated = (ctx->Point.Params[0] != 1.0 ||
123af69d88dSmrg                                   ctx->Point.Params[1] != 0.0 ||
124af69d88dSmrg                                   ctx->Point.Params[2] != 0.0);
1257117f1b4Smrg         break;
1267117f1b4Smrg      case GL_POINT_SIZE_MIN_EXT:
127af69d88dSmrg         if (params[0] < 0.0F) {
128af69d88dSmrg            _mesa_error( ctx, GL_INVALID_VALUE,
129af69d88dSmrg                         "glPointParameterf[v]{EXT,ARB}(param)" );
1307117f1b4Smrg            return;
1317117f1b4Smrg         }
132af69d88dSmrg         if (ctx->Point.MinSize == params[0])
133af69d88dSmrg            return;
134af69d88dSmrg         FLUSH_VERTICES(ctx, _NEW_POINT);
135af69d88dSmrg         ctx->Point.MinSize = params[0];
1367117f1b4Smrg         break;
1377117f1b4Smrg      case GL_POINT_SIZE_MAX_EXT:
138af69d88dSmrg         if (params[0] < 0.0F) {
139af69d88dSmrg            _mesa_error( ctx, GL_INVALID_VALUE,
140af69d88dSmrg                         "glPointParameterf[v]{EXT,ARB}(param)" );
1417117f1b4Smrg            return;
1427117f1b4Smrg         }
143af69d88dSmrg         if (ctx->Point.MaxSize == params[0])
144af69d88dSmrg            return;
145af69d88dSmrg         FLUSH_VERTICES(ctx, _NEW_POINT);
146af69d88dSmrg         ctx->Point.MaxSize = params[0];
1477117f1b4Smrg         break;
1487117f1b4Smrg      case GL_POINT_FADE_THRESHOLD_SIZE_EXT:
149af69d88dSmrg         if (params[0] < 0.0F) {
150af69d88dSmrg            _mesa_error( ctx, GL_INVALID_VALUE,
151af69d88dSmrg                         "glPointParameterf[v]{EXT,ARB}(param)" );
1527117f1b4Smrg            return;
1537117f1b4Smrg         }
154af69d88dSmrg         if (ctx->Point.Threshold == params[0])
155af69d88dSmrg            return;
156af69d88dSmrg         FLUSH_VERTICES(ctx, _NEW_POINT);
157af69d88dSmrg         ctx->Point.Threshold = params[0];
1587117f1b4Smrg         break;
1597117f1b4Smrg      case GL_POINT_SPRITE_R_MODE_NV:
1607117f1b4Smrg         /* This is one area where ARB_point_sprite and NV_point_sprite
1617117f1b4Smrg	  * differ.  In ARB_point_sprite the POINT_SPRITE_R_MODE is
1627117f1b4Smrg	  * always ZERO.  NV_point_sprite adds the S and R modes.
1637117f1b4Smrg	  */
164af69d88dSmrg         if (_mesa_is_desktop_gl(ctx) && ctx->Extensions.NV_point_sprite) {
1657117f1b4Smrg            GLenum value = (GLenum) params[0];
1667117f1b4Smrg            if (value != GL_ZERO && value != GL_S && value != GL_R) {
1677117f1b4Smrg               _mesa_error(ctx, GL_INVALID_VALUE,
1687117f1b4Smrg                           "glPointParameterf[v]{EXT,ARB}(param)");
1697117f1b4Smrg               return;
1707117f1b4Smrg            }
1717117f1b4Smrg            if (ctx->Point.SpriteRMode == value)
1727117f1b4Smrg               return;
1737117f1b4Smrg            FLUSH_VERTICES(ctx, _NEW_POINT);
1747117f1b4Smrg            ctx->Point.SpriteRMode = value;
1757117f1b4Smrg         }
1767117f1b4Smrg         else {
1777117f1b4Smrg            _mesa_error(ctx, GL_INVALID_ENUM,
1787117f1b4Smrg                        "glPointParameterf[v]{EXT,ARB}(pname)");
1797117f1b4Smrg            return;
1807117f1b4Smrg         }
1817117f1b4Smrg         break;
1827117f1b4Smrg      case GL_POINT_SPRITE_COORD_ORIGIN:
183af69d88dSmrg	 /* GL_POINT_SPRITE_COORD_ORIGIN was added to point sprites when the
184af69d88dSmrg	  * extension was merged into OpenGL 2.0.
1854a49301eSmrg	  */
186af69d88dSmrg         if ((ctx->API == API_OPENGL_COMPAT && ctx->Version >= 20)
187af69d88dSmrg             || ctx->API == API_OPENGL_CORE) {
1887117f1b4Smrg            GLenum value = (GLenum) params[0];
1897117f1b4Smrg            if (value != GL_LOWER_LEFT && value != GL_UPPER_LEFT) {
1907117f1b4Smrg               _mesa_error(ctx, GL_INVALID_VALUE,
1917117f1b4Smrg                           "glPointParameterf[v]{EXT,ARB}(param)");
1927117f1b4Smrg               return;
1937117f1b4Smrg            }
1947117f1b4Smrg            if (ctx->Point.SpriteOrigin == value)
1957117f1b4Smrg               return;
1967117f1b4Smrg            FLUSH_VERTICES(ctx, _NEW_POINT);
1977117f1b4Smrg            ctx->Point.SpriteOrigin = value;
1987117f1b4Smrg         }
1997117f1b4Smrg         else {
2007117f1b4Smrg            _mesa_error(ctx, GL_INVALID_ENUM,
2017117f1b4Smrg                        "glPointParameterf[v]{EXT,ARB}(pname)");
2027117f1b4Smrg            return;
2037117f1b4Smrg         }
2047117f1b4Smrg         break;
2057117f1b4Smrg      default:
2067117f1b4Smrg         _mesa_error( ctx, GL_INVALID_ENUM,
2077117f1b4Smrg                      "glPointParameterf[v]{EXT,ARB}(pname)" );
2087117f1b4Smrg         return;
2097117f1b4Smrg   }
2107117f1b4Smrg
2117117f1b4Smrg   if (ctx->Driver.PointParameterfv)
2127117f1b4Smrg      (*ctx->Driver.PointParameterfv)(ctx, pname, params);
2137117f1b4Smrg}
2147117f1b4Smrg
2157117f1b4Smrg
2167117f1b4Smrg
2177117f1b4Smrg/**
2187117f1b4Smrg * Initialize the context point state.
2197117f1b4Smrg *
2207117f1b4Smrg * \param ctx GL context.
2217117f1b4Smrg *
2223464ebd5Sriastradh * Initializes __struct gl_contextRec::Point and point related constants in
2233464ebd5Sriastradh * __struct gl_contextRec::Const.
2247117f1b4Smrg */
2257117f1b4Smrgvoid
2263464ebd5Sriastradh_mesa_init_point(struct gl_context *ctx)
2277117f1b4Smrg{
2287117f1b4Smrg   GLuint i;
2297117f1b4Smrg
2307117f1b4Smrg   ctx->Point.SmoothFlag = GL_FALSE;
2317117f1b4Smrg   ctx->Point.Size = 1.0;
2327117f1b4Smrg   ctx->Point.Params[0] = 1.0;
2337117f1b4Smrg   ctx->Point.Params[1] = 0.0;
2347117f1b4Smrg   ctx->Point.Params[2] = 0.0;
2357117f1b4Smrg   ctx->Point._Attenuated = GL_FALSE;
2367117f1b4Smrg   ctx->Point.MinSize = 0.0;
2377117f1b4Smrg   ctx->Point.MaxSize
2387117f1b4Smrg      = MAX2(ctx->Const.MaxPointSize, ctx->Const.MaxPointSizeAA);
2397117f1b4Smrg   ctx->Point.Threshold = 1.0;
240af69d88dSmrg
241af69d88dSmrg   /* Page 403 (page 423 of the PDF) of the OpenGL 3.0 spec says:
242af69d88dSmrg    *
243af69d88dSmrg    *     "Non-sprite points (section 3.4) - Enable/Disable targets
244af69d88dSmrg    *     POINT_SMOOTH and POINT_SPRITE, and all associated state. Point
245af69d88dSmrg    *     rasterization is always performed as though POINT_SPRITE were
246af69d88dSmrg    *     enabled."
247af69d88dSmrg    *
248af69d88dSmrg    * In a core context, the state will default to true, and the setters and
249af69d88dSmrg    * getters are disabled.
250af69d88dSmrg    */
251af69d88dSmrg   ctx->Point.PointSprite = (ctx->API == API_OPENGL_CORE ||
252af69d88dSmrg                             ctx->API == API_OPENGLES2);
253af69d88dSmrg
2547117f1b4Smrg   ctx->Point.SpriteRMode = GL_ZERO; /* GL_NV_point_sprite (only!) */
2557117f1b4Smrg   ctx->Point.SpriteOrigin = GL_UPPER_LEFT; /* GL_ARB_point_sprite */
256cdc920a0Smrg   for (i = 0; i < Elements(ctx->Point.CoordReplace); i++) {
2577117f1b4Smrg      ctx->Point.CoordReplace[i] = GL_FALSE; /* GL_ARB/NV_point_sprite */
2587117f1b4Smrg   }
2597117f1b4Smrg}
260