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 */
4301e04c3fSmrgstatic ALWAYS_INLINE void
4401e04c3fSmrgpoint_size(struct gl_context *ctx, GLfloat size, bool no_error)
457117f1b4Smrg{
4601e04c3fSmrg   if (ctx->Point.Size == size)
477117f1b4Smrg      return;
487117f1b4Smrg
4901e04c3fSmrg   if (!no_error && size <= 0.0F) {
5001e04c3fSmrg      _mesa_error(ctx, GL_INVALID_VALUE, "glPointSize");
517117f1b4Smrg      return;
5201e04c3fSmrg   }
537117f1b4Smrg
547ec681f3Smrg   FLUSH_VERTICES(ctx, _NEW_POINT, GL_POINT_BIT);
557117f1b4Smrg   ctx->Point.Size = size;
567117f1b4Smrg
577117f1b4Smrg   if (ctx->Driver.PointSize)
587117f1b4Smrg      ctx->Driver.PointSize(ctx, size);
597117f1b4Smrg}
607117f1b4Smrg
617117f1b4Smrg
6201e04c3fSmrgvoid GLAPIENTRY
6301e04c3fSmrg_mesa_PointSize_no_error(GLfloat size)
6401e04c3fSmrg{
6501e04c3fSmrg   GET_CURRENT_CONTEXT(ctx);
6601e04c3fSmrg   point_size(ctx, size, true);
6701e04c3fSmrg}
6801e04c3fSmrg
6901e04c3fSmrg
7001e04c3fSmrgvoid GLAPIENTRY
7101e04c3fSmrg_mesa_PointSize( GLfloat size )
7201e04c3fSmrg{
7301e04c3fSmrg   GET_CURRENT_CONTEXT(ctx);
7401e04c3fSmrg   point_size(ctx, size, false);
7501e04c3fSmrg}
7601e04c3fSmrg
7701e04c3fSmrg
787117f1b4Smrgvoid GLAPIENTRY
79c1f859d4Smrg_mesa_PointParameteri( GLenum pname, GLint param )
807117f1b4Smrg{
814a49301eSmrg   GLfloat p[3];
824a49301eSmrg   p[0] = (GLfloat) param;
834a49301eSmrg   p[1] = p[2] = 0.0F;
844a49301eSmrg   _mesa_PointParameterfv(pname, p);
857117f1b4Smrg}
867117f1b4Smrg
877117f1b4Smrg
887117f1b4Smrgvoid GLAPIENTRY
89c1f859d4Smrg_mesa_PointParameteriv( GLenum pname, const GLint *params )
907117f1b4Smrg{
917117f1b4Smrg   GLfloat p[3];
927117f1b4Smrg   p[0] = (GLfloat) params[0];
937117f1b4Smrg   if (pname == GL_DISTANCE_ATTENUATION_EXT) {
947117f1b4Smrg      p[1] = (GLfloat) params[1];
957117f1b4Smrg      p[2] = (GLfloat) params[2];
967117f1b4Smrg   }
97c1f859d4Smrg   _mesa_PointParameterfv(pname, p);
987117f1b4Smrg}
997117f1b4Smrg
1007117f1b4Smrg
1017117f1b4Smrgvoid GLAPIENTRY
102c1f859d4Smrg_mesa_PointParameterf( GLenum pname, GLfloat param)
1037117f1b4Smrg{
1044a49301eSmrg   GLfloat p[3];
1054a49301eSmrg   p[0] = param;
1064a49301eSmrg   p[1] = p[2] = 0.0F;
1074a49301eSmrg   _mesa_PointParameterfv(pname, p);
1087117f1b4Smrg}
1097117f1b4Smrg
1107117f1b4Smrg
1117117f1b4Smrgvoid GLAPIENTRY
112c1f859d4Smrg_mesa_PointParameterfv( GLenum pname, const GLfloat *params)
1137117f1b4Smrg{
1147117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
115af69d88dSmrg
116af69d88dSmrg   /* Drivers that support point sprites must also support point parameters.
117af69d88dSmrg    * If point parameters aren't supported, then this function shouldn't even
118af69d88dSmrg    * exist.
119af69d88dSmrg    */
1207ec681f3Smrg   assert(!ctx->Extensions.ARB_point_sprite ||
1217ec681f3Smrg          ctx->Extensions.EXT_point_parameters);
122af69d88dSmrg
123af69d88dSmrg   if (!ctx->Extensions.EXT_point_parameters) {
124af69d88dSmrg      _mesa_error(ctx, GL_INVALID_OPERATION,
125af69d88dSmrg                  "unsupported function called (unsupported extension)");
126af69d88dSmrg      return;
127af69d88dSmrg   }
1287117f1b4Smrg
1297117f1b4Smrg   switch (pname) {
1307117f1b4Smrg      case GL_DISTANCE_ATTENUATION_EXT:
131af69d88dSmrg         if (TEST_EQ_3V(ctx->Point.Params, params))
1327117f1b4Smrg            return;
1337ec681f3Smrg         FLUSH_VERTICES(ctx, _NEW_POINT | _NEW_FF_VERT_PROGRAM |
1347ec681f3Smrg                        _NEW_TNL_SPACES, GL_POINT_BIT);
135af69d88dSmrg         COPY_3V(ctx->Point.Params, params);
13601e04c3fSmrg         ctx->Point._Attenuated = (ctx->Point.Params[0] != 1.0F ||
13701e04c3fSmrg                                   ctx->Point.Params[1] != 0.0F ||
13801e04c3fSmrg                                   ctx->Point.Params[2] != 0.0F);
1397117f1b4Smrg         break;
1407117f1b4Smrg      case GL_POINT_SIZE_MIN_EXT:
141af69d88dSmrg         if (params[0] < 0.0F) {
142af69d88dSmrg            _mesa_error( ctx, GL_INVALID_VALUE,
143af69d88dSmrg                         "glPointParameterf[v]{EXT,ARB}(param)" );
1447117f1b4Smrg            return;
1457117f1b4Smrg         }
146af69d88dSmrg         if (ctx->Point.MinSize == params[0])
147af69d88dSmrg            return;
1487ec681f3Smrg         FLUSH_VERTICES(ctx, _NEW_POINT, GL_POINT_BIT);
149af69d88dSmrg         ctx->Point.MinSize = params[0];
1507117f1b4Smrg         break;
1517117f1b4Smrg      case GL_POINT_SIZE_MAX_EXT:
152af69d88dSmrg         if (params[0] < 0.0F) {
153af69d88dSmrg            _mesa_error( ctx, GL_INVALID_VALUE,
154af69d88dSmrg                         "glPointParameterf[v]{EXT,ARB}(param)" );
1557117f1b4Smrg            return;
1567117f1b4Smrg         }
157af69d88dSmrg         if (ctx->Point.MaxSize == params[0])
158af69d88dSmrg            return;
1597ec681f3Smrg         FLUSH_VERTICES(ctx, _NEW_POINT, GL_POINT_BIT);
160af69d88dSmrg         ctx->Point.MaxSize = params[0];
1617117f1b4Smrg         break;
1627117f1b4Smrg      case GL_POINT_FADE_THRESHOLD_SIZE_EXT:
163af69d88dSmrg         if (params[0] < 0.0F) {
164af69d88dSmrg            _mesa_error( ctx, GL_INVALID_VALUE,
165af69d88dSmrg                         "glPointParameterf[v]{EXT,ARB}(param)" );
1667117f1b4Smrg            return;
1677117f1b4Smrg         }
168af69d88dSmrg         if (ctx->Point.Threshold == params[0])
169af69d88dSmrg            return;
1707ec681f3Smrg         FLUSH_VERTICES(ctx, _NEW_POINT, GL_POINT_BIT);
171af69d88dSmrg         ctx->Point.Threshold = params[0];
1727117f1b4Smrg         break;
1737117f1b4Smrg      case GL_POINT_SPRITE_COORD_ORIGIN:
174af69d88dSmrg	 /* GL_POINT_SPRITE_COORD_ORIGIN was added to point sprites when the
175af69d88dSmrg	  * extension was merged into OpenGL 2.0.
1764a49301eSmrg	  */
177af69d88dSmrg         if ((ctx->API == API_OPENGL_COMPAT && ctx->Version >= 20)
178af69d88dSmrg             || ctx->API == API_OPENGL_CORE) {
1797117f1b4Smrg            GLenum value = (GLenum) params[0];
1807117f1b4Smrg            if (value != GL_LOWER_LEFT && value != GL_UPPER_LEFT) {
1817117f1b4Smrg               _mesa_error(ctx, GL_INVALID_VALUE,
1827117f1b4Smrg                           "glPointParameterf[v]{EXT,ARB}(param)");
1837117f1b4Smrg               return;
1847117f1b4Smrg            }
1857117f1b4Smrg            if (ctx->Point.SpriteOrigin == value)
1867117f1b4Smrg               return;
1877ec681f3Smrg            FLUSH_VERTICES(ctx, _NEW_POINT, GL_POINT_BIT);
1887117f1b4Smrg            ctx->Point.SpriteOrigin = value;
1897117f1b4Smrg         }
1907117f1b4Smrg         else {
1917117f1b4Smrg            _mesa_error(ctx, GL_INVALID_ENUM,
1927117f1b4Smrg                        "glPointParameterf[v]{EXT,ARB}(pname)");
1937117f1b4Smrg            return;
1947117f1b4Smrg         }
1957117f1b4Smrg         break;
1967117f1b4Smrg      default:
1977117f1b4Smrg         _mesa_error( ctx, GL_INVALID_ENUM,
1987117f1b4Smrg                      "glPointParameterf[v]{EXT,ARB}(pname)" );
1997117f1b4Smrg         return;
2007117f1b4Smrg   }
2017117f1b4Smrg
2027117f1b4Smrg   if (ctx->Driver.PointParameterfv)
20301e04c3fSmrg      ctx->Driver.PointParameterfv(ctx, pname, params);
2047117f1b4Smrg}
2057117f1b4Smrg
2067117f1b4Smrg
2077117f1b4Smrg
2087117f1b4Smrg/**
2097117f1b4Smrg * Initialize the context point state.
2107117f1b4Smrg *
2117117f1b4Smrg * \param ctx GL context.
2127117f1b4Smrg *
2133464ebd5Sriastradh * Initializes __struct gl_contextRec::Point and point related constants in
2143464ebd5Sriastradh * __struct gl_contextRec::Const.
2157117f1b4Smrg */
2167117f1b4Smrgvoid
2173464ebd5Sriastradh_mesa_init_point(struct gl_context *ctx)
2187117f1b4Smrg{
2197117f1b4Smrg   ctx->Point.SmoothFlag = GL_FALSE;
2207117f1b4Smrg   ctx->Point.Size = 1.0;
2217117f1b4Smrg   ctx->Point.Params[0] = 1.0;
2227117f1b4Smrg   ctx->Point.Params[1] = 0.0;
2237117f1b4Smrg   ctx->Point.Params[2] = 0.0;
2247117f1b4Smrg   ctx->Point._Attenuated = GL_FALSE;
2257117f1b4Smrg   ctx->Point.MinSize = 0.0;
2267117f1b4Smrg   ctx->Point.MaxSize
2277117f1b4Smrg      = MAX2(ctx->Const.MaxPointSize, ctx->Const.MaxPointSizeAA);
2287117f1b4Smrg   ctx->Point.Threshold = 1.0;
229af69d88dSmrg
230af69d88dSmrg   /* Page 403 (page 423 of the PDF) of the OpenGL 3.0 spec says:
231af69d88dSmrg    *
232af69d88dSmrg    *     "Non-sprite points (section 3.4) - Enable/Disable targets
233af69d88dSmrg    *     POINT_SMOOTH and POINT_SPRITE, and all associated state. Point
234af69d88dSmrg    *     rasterization is always performed as though POINT_SPRITE were
235af69d88dSmrg    *     enabled."
236af69d88dSmrg    *
237af69d88dSmrg    * In a core context, the state will default to true, and the setters and
238af69d88dSmrg    * getters are disabled.
239af69d88dSmrg    */
240af69d88dSmrg   ctx->Point.PointSprite = (ctx->API == API_OPENGL_CORE ||
241af69d88dSmrg                             ctx->API == API_OPENGLES2);
242af69d88dSmrg
2437117f1b4Smrg   ctx->Point.SpriteOrigin = GL_UPPER_LEFT; /* GL_ARB_point_sprite */
2447ec681f3Smrg   ctx->Point.CoordReplace = 0; /* GL_ARB_point_sprite */
2457117f1b4Smrg}
246