ed2ab0ca5e38982c338af8ef5195ef6acdef16d7
[mesa.git] / src / mesa / swrast / s_pointtemp.h
1 /* $Id: s_pointtemp.h,v 1.1 2001/01/03 22:17:16 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2000 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 = vert->win[0] - iRadius;
204 xmax = vert->win[0] + iRadius;
205 ymin = vert->win[1] - iRadius;
206 ymax = 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];
223 const GLfloat dy = y - vert->win[1];
224 const GLfloat dist2 = dx * dx + dy * dy;
225 if (dist2 < rmax2) {
226 alpha = vert->color[3];
227 if (dist2 >= rmin2) {
228 GLint coverage = (GLint) (256.0F - (dist2 - rmin2) * cscale);
229 #if FLAGS & RGBA
230 /* coverage is in [0,256] */
231 alpha = (alpha * coverage) >> 8;
232 #else
233 /* 4 fractional index bits */
234 index = (index & ~f) | (coverage >> 4); /* XXX verify */
235 #endif
236 }
237 #endif /* SMOOTH */
238
239 #if ((FLAGS & (ATTENUATE | RGBA)) == (ATTENUATE | RGBA))
240 alpha = (GLint) (alpha * alphaAtten);
241 #endif
242
243 #if FLAGS & SPECULAR
244 PB_WRITE_MULTITEX_SPEC_PIXEL(PB, x, y, z, fog,
245 red, green, blue, alpha,
246 sRed, sGreen, sBlue,
247 texcoord);
248
249 #elif FLAGS & TEXTURE
250 if (swrast->_MultiTextureEnabled) {
251 PB_WRITE_MULTITEX_PIXEL(PB, x, y, z, fog,
252 red, green, blue, alpha,
253 texcoord);
254 }
255 else {
256 PB_WRITE_TEX_PIXEL(PB, x,y,z, fog,
257 red, green, blue, alpha,
258 texcoord[0][0],
259 texcoord[0][1],
260 texcoord[0][2]);
261 }
262 #elif FLAGS & RGBA
263 PB_WRITE_RGBA_PIXEL(PB, x, y, z, fog, red, green, blue, alpha);
264 #else /* color index */
265 PB_WRITE_CI_PIXEL(PB, x, y, z, fog, index);
266 #endif
267 #if FLAGS & SMOOTH
268 }
269 #endif
270 }
271 }
272 PB_CHECK_FLUSH(ctx,PB);
273 }
274
275 #else /* LARGE || ATTENUATE || SMOOTH*/
276
277 {
278 /* size == 1 */
279 GLint x = vert->win[0];
280 GLint y = vert->win[1];
281 #if ((FLAGS & (SPECULAR | TEXTURE)) == (SPECULAR | TEXTURE))
282 PB_WRITE_MULTITEX_SPEC_PIXEL(PB, x, y, z, fog,
283 red, green, blue, alpha,
284 sRed, sGreen, sBlue,
285 texcoord);
286 #elif FLAGS & TEXTURE
287 if (swrast->_MultiTextureEnabled) {
288 PB_WRITE_MULTITEX_PIXEL(PB, x, y, z, fog,
289 red, green, blue, alpha, texcoord );
290 }
291 else {
292 PB_WRITE_TEX_PIXEL(PB, x, y, z, fog,
293 red, green, blue, alpha,
294 texcoord[0][0], texcoord[0][1], texcoord[0][2]);
295 }
296 #elif FLAGS & RGBA
297 /* rgba size 1 point */
298 alpha = vert->color[3];
299 PB_WRITE_RGBA_PIXEL(PB, x, y, z, fog, red, green, blue, alpha);
300 #else
301 /* color index size 1 point */
302 PB_WRITE_CI_PIXEL(PB, x, y, z, fog, index);
303 #endif
304 }
305 #endif /* LARGE || ATTENUATE || SMOOTH */
306
307 PB_CHECK_FLUSH(ctx, PB);
308 }
309
310
311 #undef FLAGS
312 #undef NAME