mesa: Fix array out-of-bounds access by _mesa_PointParameterf.
[mesa.git] / src / mesa / main / points.c
1 /**
2 * \file points.c
3 * Point operations.
4 */
5
6 /*
7 * Mesa 3-D graphics library
8 * Version: 7.1
9 *
10 * Copyright (C) 1999-2007 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 "context.h"
33 #include "macros.h"
34 #include "points.h"
35 #include "texstate.h"
36 #include "mtypes.h"
37
38
39 /**
40 * Set current point size.
41 * \param size point diameter in pixels
42 * \sa glPointSize().
43 */
44 void GLAPIENTRY
45 _mesa_PointSize( GLfloat size )
46 {
47 GET_CURRENT_CONTEXT(ctx);
48 ASSERT_OUTSIDE_BEGIN_END(ctx);
49
50 if (size <= 0.0) {
51 _mesa_error( ctx, GL_INVALID_VALUE, "glPointSize" );
52 return;
53 }
54
55 if (ctx->Point.Size == size)
56 return;
57
58 FLUSH_VERTICES(ctx, _NEW_POINT);
59 ctx->Point.Size = size;
60
61 if (ctx->Driver.PointSize)
62 ctx->Driver.PointSize(ctx, size);
63 }
64
65
66 #if _HAVE_FULL_GL
67
68
69 void GLAPIENTRY
70 _mesa_PointParameteri( GLenum pname, GLint param )
71 {
72 const GLfloat value = (GLfloat) param;
73 _mesa_PointParameterfv(pname, &value);
74 }
75
76
77 void GLAPIENTRY
78 _mesa_PointParameteriv( GLenum pname, const GLint *params )
79 {
80 GLfloat p[3];
81 p[0] = (GLfloat) params[0];
82 if (pname == GL_DISTANCE_ATTENUATION_EXT) {
83 p[1] = (GLfloat) params[1];
84 p[2] = (GLfloat) params[2];
85 }
86 _mesa_PointParameterfv(pname, p);
87 }
88
89
90 void GLAPIENTRY
91 _mesa_PointParameterf( GLenum pname, GLfloat param)
92 {
93 GLfloat p[3];
94 p[0] = param;
95 p[1] = p[2] = 0.0F;
96 _mesa_PointParameterfv(pname, p);
97 }
98
99
100 void GLAPIENTRY
101 _mesa_PointParameterfv( GLenum pname, const GLfloat *params)
102 {
103 GET_CURRENT_CONTEXT(ctx);
104 ASSERT_OUTSIDE_BEGIN_END(ctx);
105
106 switch (pname) {
107 case GL_DISTANCE_ATTENUATION_EXT:
108 if (ctx->Extensions.EXT_point_parameters) {
109 if (TEST_EQ_3V(ctx->Point.Params, params))
110 return;
111 FLUSH_VERTICES(ctx, _NEW_POINT);
112 COPY_3V(ctx->Point.Params, params);
113 ctx->Point._Attenuated = (ctx->Point.Params[0] != 1.0 ||
114 ctx->Point.Params[1] != 0.0 ||
115 ctx->Point.Params[2] != 0.0);
116
117 if (ctx->Point._Attenuated)
118 ctx->_TriangleCaps |= DD_POINT_ATTEN;
119 else
120 ctx->_TriangleCaps &= ~DD_POINT_ATTEN;
121 }
122 else {
123 _mesa_error(ctx, GL_INVALID_ENUM,
124 "glPointParameterf[v]{EXT,ARB}(pname)");
125 return;
126 }
127 break;
128 case GL_POINT_SIZE_MIN_EXT:
129 if (ctx->Extensions.EXT_point_parameters) {
130 if (params[0] < 0.0F) {
131 _mesa_error( ctx, GL_INVALID_VALUE,
132 "glPointParameterf[v]{EXT,ARB}(param)" );
133 return;
134 }
135 if (ctx->Point.MinSize == params[0])
136 return;
137 FLUSH_VERTICES(ctx, _NEW_POINT);
138 ctx->Point.MinSize = params[0];
139 }
140 else {
141 _mesa_error(ctx, GL_INVALID_ENUM,
142 "glPointParameterf[v]{EXT,ARB}(pname)");
143 return;
144 }
145 break;
146 case GL_POINT_SIZE_MAX_EXT:
147 if (ctx->Extensions.EXT_point_parameters) {
148 if (params[0] < 0.0F) {
149 _mesa_error( ctx, GL_INVALID_VALUE,
150 "glPointParameterf[v]{EXT,ARB}(param)" );
151 return;
152 }
153 if (ctx->Point.MaxSize == params[0])
154 return;
155 FLUSH_VERTICES(ctx, _NEW_POINT);
156 ctx->Point.MaxSize = params[0];
157 }
158 else {
159 _mesa_error(ctx, GL_INVALID_ENUM,
160 "glPointParameterf[v]{EXT,ARB}(pname)");
161 return;
162 }
163 break;
164 case GL_POINT_FADE_THRESHOLD_SIZE_EXT:
165 if (ctx->Extensions.EXT_point_parameters) {
166 if (params[0] < 0.0F) {
167 _mesa_error( ctx, GL_INVALID_VALUE,
168 "glPointParameterf[v]{EXT,ARB}(param)" );
169 return;
170 }
171 if (ctx->Point.Threshold == params[0])
172 return;
173 FLUSH_VERTICES(ctx, _NEW_POINT);
174 ctx->Point.Threshold = params[0];
175 }
176 else {
177 _mesa_error(ctx, GL_INVALID_ENUM,
178 "glPointParameterf[v]{EXT,ARB}(pname)");
179 return;
180 }
181 break;
182 case GL_POINT_SPRITE_R_MODE_NV:
183 /* This is one area where ARB_point_sprite and NV_point_sprite
184 * differ. In ARB_point_sprite the POINT_SPRITE_R_MODE is
185 * always ZERO. NV_point_sprite adds the S and R modes.
186 */
187 if (ctx->Extensions.NV_point_sprite) {
188 GLenum value = (GLenum) params[0];
189 if (value != GL_ZERO && value != GL_S && value != GL_R) {
190 _mesa_error(ctx, GL_INVALID_VALUE,
191 "glPointParameterf[v]{EXT,ARB}(param)");
192 return;
193 }
194 if (ctx->Point.SpriteRMode == value)
195 return;
196 FLUSH_VERTICES(ctx, _NEW_POINT);
197 ctx->Point.SpriteRMode = value;
198 }
199 else {
200 _mesa_error(ctx, GL_INVALID_ENUM,
201 "glPointParameterf[v]{EXT,ARB}(pname)");
202 return;
203 }
204 break;
205 case GL_POINT_SPRITE_COORD_ORIGIN:
206 /* This is not completely correct. GL_POINT_SPRITE_COORD_ORIGIN was
207 * added to point sprites when the extension was merged into OpenGL
208 * 2.0. It is expected that an implementation supporting OpenGL 1.4
209 * and GL_ARB_point_sprite will generate an error here.
210 */
211 if (ctx->Extensions.ARB_point_sprite) {
212 GLenum value = (GLenum) params[0];
213 if (value != GL_LOWER_LEFT && value != GL_UPPER_LEFT) {
214 _mesa_error(ctx, GL_INVALID_VALUE,
215 "glPointParameterf[v]{EXT,ARB}(param)");
216 return;
217 }
218 if (ctx->Point.SpriteOrigin == value)
219 return;
220 FLUSH_VERTICES(ctx, _NEW_POINT);
221 ctx->Point.SpriteOrigin = value;
222 }
223 else {
224 _mesa_error(ctx, GL_INVALID_ENUM,
225 "glPointParameterf[v]{EXT,ARB}(pname)");
226 return;
227 }
228 break;
229 default:
230 _mesa_error( ctx, GL_INVALID_ENUM,
231 "glPointParameterf[v]{EXT,ARB}(pname)" );
232 return;
233 }
234
235 if (ctx->Driver.PointParameterfv)
236 (*ctx->Driver.PointParameterfv)(ctx, pname, params);
237 }
238 #endif
239
240
241
242 /**
243 * Initialize the context point state.
244 *
245 * \param ctx GL context.
246 *
247 * Initializes __GLcontextRec::Point and point related constants in
248 * __GLcontextRec::Const.
249 */
250 void
251 _mesa_init_point(GLcontext *ctx)
252 {
253 GLuint i;
254
255 ctx->Point.SmoothFlag = GL_FALSE;
256 ctx->Point.Size = 1.0;
257 ctx->Point.Params[0] = 1.0;
258 ctx->Point.Params[1] = 0.0;
259 ctx->Point.Params[2] = 0.0;
260 ctx->Point._Attenuated = GL_FALSE;
261 ctx->Point.MinSize = 0.0;
262 ctx->Point.MaxSize
263 = MAX2(ctx->Const.MaxPointSize, ctx->Const.MaxPointSizeAA);
264 ctx->Point.Threshold = 1.0;
265 ctx->Point.PointSprite = GL_FALSE; /* GL_ARB/NV_point_sprite */
266 ctx->Point.SpriteRMode = GL_ZERO; /* GL_NV_point_sprite (only!) */
267 ctx->Point.SpriteOrigin = GL_UPPER_LEFT; /* GL_ARB_point_sprite */
268 for (i = 0; i < MAX_TEXTURE_COORD_UNITS; i++) {
269 ctx->Point.CoordReplace[i] = GL_FALSE; /* GL_ARB/NV_point_sprite */
270 }
271 }