0f562420b085fa689dfd36ddb68d3da417fa4bf7
[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.0
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 /* XXX correct clamp limits? */
61 ctx->Point._Size = CLAMP(ctx->Point.Size,
62 ctx->Point.MinSize,
63 ctx->Point.MaxSize);
64
65 if (ctx->Driver.PointSize)
66 ctx->Driver.PointSize(ctx, size);
67 }
68
69
70 #if _HAVE_FULL_GL
71
72 /*
73 * Added by GL_NV_point_sprite
74 */
75 void GLAPIENTRY
76 _mesa_PointParameteriNV( GLenum pname, GLint param )
77 {
78 const GLfloat value = (GLfloat) param;
79 _mesa_PointParameterfvEXT(pname, &value);
80 }
81
82
83 /*
84 * Added by GL_NV_point_sprite
85 */
86 void GLAPIENTRY
87 _mesa_PointParameterivNV( GLenum pname, const GLint *params )
88 {
89 const GLfloat value = (GLfloat) params[0];
90 _mesa_PointParameterfvEXT(pname, &value);
91 }
92
93
94
95 /*
96 * Same for both GL_EXT_point_parameters and GL_ARB_point_parameters.
97 */
98 void GLAPIENTRY
99 _mesa_PointParameterfEXT( GLenum pname, GLfloat param)
100 {
101 _mesa_PointParameterfvEXT(pname, &param);
102 }
103
104
105
106 /*
107 * Same for both GL_EXT_point_parameters and GL_ARB_point_parameters.
108 */
109 void GLAPIENTRY
110 _mesa_PointParameterfvEXT( GLenum pname, const GLfloat *params)
111 {
112 GET_CURRENT_CONTEXT(ctx);
113 ASSERT_OUTSIDE_BEGIN_END(ctx);
114
115 switch (pname) {
116 case GL_DISTANCE_ATTENUATION_EXT:
117 if (ctx->Extensions.EXT_point_parameters) {
118 if (TEST_EQ_3V(ctx->Point.Params, params))
119 return;
120 FLUSH_VERTICES(ctx, _NEW_POINT);
121 COPY_3V(ctx->Point.Params, params);
122 ctx->Point._Attenuated = (ctx->Point.Params[0] != 1.0 ||
123 ctx->Point.Params[1] != 0.0 ||
124 ctx->Point.Params[2] != 0.0);
125 }
126 else {
127 _mesa_error(ctx, GL_INVALID_ENUM,
128 "glPointParameterf[v]{EXT,ARB}(pname)");
129 return;
130 }
131 break;
132 case GL_POINT_SIZE_MIN_EXT:
133 if (ctx->Extensions.EXT_point_parameters) {
134 if (params[0] < 0.0F) {
135 _mesa_error( ctx, GL_INVALID_VALUE,
136 "glPointParameterf[v]{EXT,ARB}(param)" );
137 return;
138 }
139 if (ctx->Point.MinSize == params[0])
140 return;
141 FLUSH_VERTICES(ctx, _NEW_POINT);
142 ctx->Point.MinSize = params[0];
143 }
144 else {
145 _mesa_error(ctx, GL_INVALID_ENUM,
146 "glPointParameterf[v]{EXT,ARB}(pname)");
147 return;
148 }
149 break;
150 case GL_POINT_SIZE_MAX_EXT:
151 if (ctx->Extensions.EXT_point_parameters) {
152 if (params[0] < 0.0F) {
153 _mesa_error( ctx, GL_INVALID_VALUE,
154 "glPointParameterf[v]{EXT,ARB}(param)" );
155 return;
156 }
157 if (ctx->Point.MaxSize == params[0])
158 return;
159 FLUSH_VERTICES(ctx, _NEW_POINT);
160 ctx->Point.MaxSize = params[0];
161 }
162 else {
163 _mesa_error(ctx, GL_INVALID_ENUM,
164 "glPointParameterf[v]{EXT,ARB}(pname)");
165 return;
166 }
167 break;
168 case GL_POINT_FADE_THRESHOLD_SIZE_EXT:
169 if (ctx->Extensions.EXT_point_parameters) {
170 if (params[0] < 0.0F) {
171 _mesa_error( ctx, GL_INVALID_VALUE,
172 "glPointParameterf[v]{EXT,ARB}(param)" );
173 return;
174 }
175 if (ctx->Point.Threshold == params[0])
176 return;
177 FLUSH_VERTICES(ctx, _NEW_POINT);
178 ctx->Point.Threshold = params[0];
179 }
180 else {
181 _mesa_error(ctx, GL_INVALID_ENUM,
182 "glPointParameterf[v]{EXT,ARB}(pname)");
183 return;
184 }
185 break;
186 case GL_POINT_SPRITE_R_MODE_NV:
187 /* This is one area where ARB_point_sprite and NV_point_sprite
188 * differ. In ARB_point_sprite the POINT_SPRITE_R_MODE is
189 * always ZERO. NV_point_sprite adds the S and R modes.
190 */
191 if (ctx->Extensions.NV_point_sprite) {
192 GLenum value = (GLenum) params[0];
193 if (value != GL_ZERO && value != GL_S && value != GL_R) {
194 _mesa_error(ctx, GL_INVALID_VALUE,
195 "glPointParameterf[v]{EXT,ARB}(param)");
196 return;
197 }
198 if (ctx->Point.SpriteRMode == value)
199 return;
200 FLUSH_VERTICES(ctx, _NEW_POINT);
201 ctx->Point.SpriteRMode = value;
202 }
203 else {
204 _mesa_error(ctx, GL_INVALID_ENUM,
205 "glPointParameterf[v]{EXT,ARB}(pname)");
206 return;
207 }
208 break;
209 case GL_POINT_SPRITE_COORD_ORIGIN:
210 if (ctx->Extensions.ARB_point_sprite) {
211 GLenum value = (GLenum) params[0];
212 if (value != GL_LOWER_LEFT && value != GL_UPPER_LEFT) {
213 _mesa_error(ctx, GL_INVALID_VALUE,
214 "glPointParameterf[v]{EXT,ARB}(param)");
215 return;
216 }
217 if (ctx->Point.SpriteOrigin == value)
218 return;
219 FLUSH_VERTICES(ctx, _NEW_POINT);
220 ctx->Point.SpriteOrigin = value;
221 }
222 else {
223 _mesa_error(ctx, GL_INVALID_ENUM,
224 "glPointParameterf[v]{EXT,ARB}(pname)");
225 return;
226 }
227 break;
228 default:
229 _mesa_error( ctx, GL_INVALID_ENUM,
230 "glPointParameterf[v]{EXT,ARB}(pname)" );
231 return;
232 }
233
234 if (ctx->Driver.PointParameterfv)
235 (*ctx->Driver.PointParameterfv)(ctx, pname, params);
236 }
237 #endif
238
239
240
241 /**
242 * Initialize the context point state.
243 *
244 * \param ctx GL context.
245 *
246 * Initializes __GLcontextRec::Point and point related constants in
247 * __GLcontextRec::Const.
248 */
249 void
250 _mesa_init_point(GLcontext *ctx)
251 {
252 GLuint i;
253
254 ctx->Point.SmoothFlag = GL_FALSE;
255 ctx->Point.Size = 1.0;
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_UNITS; i++) {
269 ctx->Point.CoordReplace[i] = GL_FALSE; /* GL_ARB/NV_point_sprite */
270 }
271 }