fixed a bunch of g++ warnings/errors. Compiling with g++ can help find lots of poten...
[mesa.git] / src / mesa / swrast / s_pointtemp.h
1 /* $Id: s_pointtemp.h,v 1.4 2001/03/07 05:06:12 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28 /*
29 * Point rendering template code.
30 *
31 * Set FLAGS = bitwise-OR of the following tokens:
32 *
33 * RGBA = do rgba instead of color index
34 * SMOOTH = do antialiasing
35 * TEXTURE = do texture coords
36 * SPECULAR = do separate specular color
37 * LARGE = do points with diameter > 1 pixel
38 * ATTENUATE = compute point size attenuation
39 * SPRITE = GL_MESA_sprite_point
40 *
41 * Notes: LARGE and ATTENUATE are exclusive of each other.
42 * TEXTURE requires RGBA
43 * SPECULAR requires TEXTURE
44 */
45
46
47 /*
48 * NOTES on antialiased point rasterization:
49 *
50 * Let d = distance of fragment center from vertex.
51 * if d < rmin2 then
52 * fragment has 100% coverage
53 * else if d > rmax2 then
54 * fragment has 0% coverage
55 * else
56 * fragement has % coverage = (d - rmin2) / (rmax2 - rmin2)
57 */
58
59
60
61 static void
62 NAME ( GLcontext *ctx, const SWvertex *vert )
63 {
64 SWcontext *swrast = SWRAST_CONTEXT(ctx);
65 struct pixel_buffer *PB = swrast->PB;
66
67 const GLint z = (GLint) (vert->win[2]);
68 const GLfixed fog = FloatToFixed( vert->fog );
69
70 #if FLAGS & RGBA
71 const GLint red = vert->color[0];
72 const GLint green = vert->color[1];
73 const GLint blue = vert->color[2];
74 GLint alpha = vert->color[3];
75 #if FLAGS & SPECULAR
76 const GLint sRed = vert->specular[0];
77 const GLint sGreen = vert->specular[1];
78 const GLint sBlue = vert->specular[2];
79 #endif
80 #else
81 GLint index = vert->index;
82 #endif
83 #if FLAGS & (ATTENUATE | LARGE | SMOOTH)
84 GLfloat size;
85 #endif
86 #if FLAGS & ATTENUATE
87 GLfloat alphaAtten;
88 #endif
89
90 #if FLAGS & TEXTURE
91 GLfloat texcoord[MAX_TEXTURE_UNITS][4];
92 GLuint u;
93 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
94 if (ctx->Texture.Unit[u]._ReallyEnabled) {
95 if (vert->texcoord[u][3] != 1.0 && vert->texcoord[u][3] != 0.0) {
96 texcoord[u][0] = vert->texcoord[u][0] / vert->texcoord[u][3];
97 texcoord[u][1] = vert->texcoord[u][1] / vert->texcoord[u][3];
98 texcoord[u][2] = vert->texcoord[u][2] / vert->texcoord[u][3];
99 }
100 else {
101 texcoord[u][0] = vert->texcoord[u][0];
102 texcoord[u][1] = vert->texcoord[u][1];
103 texcoord[u][2] = vert->texcoord[u][2];
104 }
105 }
106 }
107 #endif
108
109 #if FLAGS & ATTENUATE
110 if (vert->pointSize >= ctx->Point.Threshold) {
111 size = MIN2(vert->pointSize, ctx->Point.MaxSize);
112 alphaAtten = 1.0F;
113 }
114 else {
115 GLfloat dsize = vert->pointSize / ctx->Point.Threshold;
116 size = MAX2(ctx->Point.Threshold, ctx->Point.MinSize);
117 alphaAtten = dsize * dsize;
118 }
119 #elif FLAGS & (LARGE | SMOOTH)
120 size = ctx->Point._Size;
121 #endif
122
123 #if FLAGS & SPRITE
124 {
125 SWcontext *swctx = SWRAST_CONTEXT(ctx);
126 const GLfloat radius = 0.5 * vert->pointSize; /* XXX threshold, alpha */
127 SWvertex v0, v1, v2, v3;
128 GLuint unit;
129
130 (void) red;
131 (void) green;
132 (void) blue;
133 (void) alpha;
134 (void) fog;
135 (void) z;
136
137 /* lower left corner */
138 v0 = *vert;
139 v0.win[0] -= radius;
140 v0.win[1] -= radius;
141
142 /* lower right corner */
143 v1 = *vert;
144 v1.win[0] += radius;
145 v1.win[1] -= radius;
146
147 /* upper right corner */
148 v2 = *vert;
149 v2.win[0] += radius;
150 v2.win[1] += radius;
151
152 /* upper left corner */
153 v3 = *vert;
154 v3.win[0] -= radius;
155 v3.win[1] += radius;
156
157 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
158 if (ctx->Texture.Unit[unit]._ReallyEnabled) {
159 v0.texcoord[unit][0] = 0.0;
160 v0.texcoord[unit][1] = 0.0;
161 v1.texcoord[unit][0] = 1.0;
162 v1.texcoord[unit][1] = 0.0;
163 v2.texcoord[unit][0] = 1.0;
164 v2.texcoord[unit][1] = 1.0;
165 v3.texcoord[unit][0] = 0.0;
166 v3.texcoord[unit][1] = 1.0;
167 }
168 }
169
170 /* XXX if radius < threshold, attenuate alpha? */
171
172 /* XXX need to implement clipping!!! */
173
174 /* render */
175 swctx->Triangle(ctx, &v0, &v1, &v2);
176 swctx->Triangle(ctx, &v0, &v2, &v3);
177 }
178
179 #elif FLAGS & (LARGE | ATTENUATE | SMOOTH)
180
181 {
182 GLint x, y;
183 const GLfloat radius = 0.5F * size;
184 #if FLAGS & SMOOTH
185 const GLfloat rmin = radius - 0.7071F; /* 0.7071 = sqrt(2)/2 */
186 const GLfloat rmax = radius + 0.7071F;
187 const GLfloat rmin2 = MAX2(0.0, rmin * rmin);
188 const GLfloat rmax2 = rmax * rmax;
189 const GLfloat cscale = 256.0F / (rmax2 - rmin2);
190 const GLint xmin = (GLint) (vert->win[0] - radius);
191 const GLint xmax = (GLint) (vert->win[0] + radius);
192 const GLint ymin = (GLint) (vert->win[1] - radius);
193 const GLint ymax = (GLint) (vert->win[1] + radius);
194 #else
195 /* non-smooth */
196 GLint xmin, xmax, ymin, ymax;
197 GLint iSize = (GLint) (size + 0.5F);
198 GLint iRadius;
199 iSize = MAX2(1, iSize);
200 iRadius = iSize / 2;
201 if (iSize & 1) {
202 /* odd size */
203 xmin = (GLint) (vert->win[0] - iRadius);
204 xmax = (GLint) (vert->win[0] + iRadius);
205 ymin = (GLint) (vert->win[1] - iRadius);
206 ymax = (GLint) (vert->win[1] + iRadius);
207 }
208 else {
209 /* even size */
210 xmin = (GLint) vert->win[0] - iRadius + 1;
211 xmax = xmin + iSize - 1;
212 ymin = (GLint) vert->win[1] - iRadius + 1;
213 ymax = ymin + iSize - 1;
214 }
215 #endif
216 (void) radius;
217
218 for (y = ymin; y <= ymax; y++) {
219 for (x = xmin; x <= xmax; x++) {
220 #if FLAGS & SMOOTH
221 /* compute coverage */
222 const GLfloat dx = x - vert->win[0] + 0.5F;
223 const GLfloat dy = y - vert->win[1] + 0.5F;
224 const GLfloat dist2 = dx * dx + dy * dy;
225 if (dist2 < rmax2) {
226 #if FLAGS & RGBA
227 alpha = vert->color[3];
228 #endif
229 if (dist2 >= rmin2) {
230 GLint coverage = (GLint) (256.0F - (dist2 - rmin2) * cscale);
231 #if FLAGS & RGBA
232 /* coverage is in [0,256] */
233 alpha = (alpha * coverage) >> 8;
234 #else
235 /* 4 fractional index bits */
236 index = (index & ~0xf) | (coverage >> 4); /* XXX verify */
237 #endif
238 }
239 #endif /* SMOOTH */
240
241 #if ((FLAGS & (ATTENUATE | RGBA)) == (ATTENUATE | RGBA))
242 alpha = (GLint) (alpha * alphaAtten);
243 #endif
244
245 #if FLAGS & SPECULAR
246 PB_WRITE_MULTITEX_SPEC_PIXEL(PB, x, y, z, fog,
247 red, green, blue, alpha,
248 sRed, sGreen, sBlue,
249 texcoord);
250 #elif FLAGS & TEXTURE
251 if (swrast->_MultiTextureEnabled) {
252 PB_WRITE_MULTITEX_PIXEL(PB, x, y, z, fog,
253 red, green, blue, alpha,
254 texcoord);
255 }
256 else if (ctx->Texture._ReallyEnabled) {
257 PB_WRITE_TEX_PIXEL(PB, x,y,z, fog,
258 red, green, blue, alpha,
259 texcoord[0][0],
260 texcoord[0][1],
261 texcoord[0][2]);
262 }
263 else {
264 PB_WRITE_RGBA_PIXEL(PB, x, y, z, fog,
265 red, green, blue, alpha);
266 }
267 #elif FLAGS & RGBA
268 PB_WRITE_RGBA_PIXEL(PB, x, y, z, fog, red, green, blue, alpha);
269 #else /* color index */
270 PB_WRITE_CI_PIXEL(PB, x, y, z, fog, index);
271 #endif
272 #if FLAGS & SMOOTH
273 }
274 #endif
275 }
276 }
277 PB_CHECK_FLUSH(ctx,PB);
278 }
279
280 #else /* LARGE || ATTENUATE || SMOOTH*/
281
282 {
283 /* size == 1 */
284 GLint x = (GLint) vert->win[0];
285 GLint y = (GLint) vert->win[1];
286 #if ((FLAGS & (SPECULAR | TEXTURE)) == (SPECULAR | TEXTURE))
287 PB_WRITE_MULTITEX_SPEC_PIXEL(PB, x, y, z, fog,
288 red, green, blue, alpha,
289 sRed, sGreen, sBlue,
290 texcoord);
291 #elif FLAGS & TEXTURE
292 if (swrast->_MultiTextureEnabled) {
293 PB_WRITE_MULTITEX_PIXEL(PB, x, y, z, fog,
294 red, green, blue, alpha, texcoord );
295 }
296 else {
297 PB_WRITE_TEX_PIXEL(PB, x, y, z, fog,
298 red, green, blue, alpha,
299 texcoord[0][0], texcoord[0][1], texcoord[0][2]);
300 }
301 #elif FLAGS & RGBA
302 /* rgba size 1 point */
303 alpha = vert->color[3];
304 PB_WRITE_RGBA_PIXEL(PB, x, y, z, fog, red, green, blue, alpha);
305 #else
306 /* color index size 1 point */
307 PB_WRITE_CI_PIXEL(PB, x, y, z, fog, index);
308 #endif
309 }
310 #endif /* LARGE || ATTENUATE || SMOOTH */
311
312 PB_CHECK_FLUSH(ctx, PB);
313 }
314
315
316 #undef FLAGS
317 #undef NAME