Debian Codegraph @

debian/external_deps/fetk/sg/src/gl/points.c

raw
1 /* $Id: points.c,v 1.2 2000/10/27 15:21:40 mholst Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 2.2
6 * Copyright (C) 1995-1997 Brian Paul
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the Free
20 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "context.h"
24 #include "feedback.h"
25 #include "dlist.h"
26 #include "macros.h"
27 #include "pb.h"
28 #include "span.h"
29 #include "texture.h"
30 #include "types.h"
31 #include "vb.h"
32
33
34
35
36 void gl_PointSize( GLcontext *ctx, GLfloat size )
37 {
38 if (size<=0.0) {
39 gl_error( ctx, GL_INVALID_VALUE, "glPointSize" );
40 return;
41 }
42 if (INSIDE_BEGIN_END(ctx)) {
43 gl_error( ctx, GL_INVALID_OPERATION, "glPointSize" );
44 return;
45 }
46 ctx->Point.Size = size;
47 ctx->NewState |= NEW_RASTER_OPS;
48 }
49
50
51
52 /**********************************************************************/
53 /***** Rasterization *****/
54 /**********************************************************************/
55
56
57 /*
58 * There are 3 pairs (RGBA, CI) of point rendering functions:
59 * 1. simple: size=1 and no special rasterization functions (fastest)
60 * 2. size1: size=1 and any rasterization functions
61 * 3. general: any size and rasterization functions (slowest)
62 *
63 * All point rendering functions take the same two arguments: first and
64 * last which specify that the points specified by VB[first] through
65 * VB[last] are to be rendered.
66 */
67
68
69
70 /*
71 * Put points in feedback buffer.
72 */
73 static void feedback_points( GLcontext *ctx, GLuint first, GLuint last )
74 {
75 struct vertex_buffer *VB = ctx->VB;
76 GLuint i;
77 GLfloat invRedScale = ctx->Visual->InvRedScale;
78 GLfloat invGreenScale = ctx->Visual->InvGreenScale;
79 GLfloat invBlueScale = ctx->Visual->InvBlueScale;
80 GLfloat invAlphaScale = ctx->Visual->InvAlphaScale;
81
82 for (i=first;i<=last;i++) {
83 if (VB->Unclipped[i]) {
84 GLfloat x, y, z, w, invq;
85 GLfloat color[4], texcoord[4];
86
87 x = VB->Win[i][0];
88 y = VB->Win[i][1];
89 z = VB->Win[i][2] / DEPTH_SCALE;
90 w = VB->Clip[i][3];
91
92 /* convert color from integer back to a float in [0,1] */
93 if (ctx->Light.ShadeModel==GL_SMOOTH) {
94 /* smooth shading - colors are in fixed point */
95 color[0] = FixedToFloat(VB->Color[i][0]) * invRedScale;
96 color[1] = FixedToFloat(VB->Color[i][1]) * invGreenScale;
97 color[2] = FixedToFloat(VB->Color[i][2]) * invBlueScale;
98 color[3] = FixedToFloat(VB->Color[i][3]) * invAlphaScale;
99 }
100 else {
101 /* flat shading - colors are integers */
102 color[0] = VB->Color[i][0] * invRedScale;
103 color[1] = VB->Color[i][1] * invGreenScale;
104 color[2] = VB->Color[i][2] * invBlueScale;
105 color[3] = VB->Color[i][3] * invAlphaScale;
106 }
107 invq = 1.0F / VB->TexCoord[i][3];
108 texcoord[0] = VB->TexCoord[i][0] * invq;
109 texcoord[1] = VB->TexCoord[i][1] * invq;
110 texcoord[2] = VB->TexCoord[i][2] * invq;
111 texcoord[3] = VB->TexCoord[i][3];
112
113 FEEDBACK_TOKEN( ctx, (GLfloat) GL_POINT_TOKEN );
114 gl_feedback_vertex( ctx, x, y, z, w, color,
115 (GLfloat) VB->Index[i], texcoord );
116 }
117 }
118 }
119
120
121
122 /*
123 * Put points in selection buffer.
124 */
125 static void select_points( GLcontext *ctx, GLuint first, GLuint last )
126 {
127 struct vertex_buffer *VB = ctx->VB;
128 GLuint i;
129
130 for (i=first;i<=last;i++) {
131 if (VB->Unclipped[i]) {
132 gl_update_hitflag( ctx, VB->Win[i][2] / DEPTH_SCALE );
133 }
134 }
135 }
136
137
138 /*
139 * CI points with size == 1.0
140 */
141 void size1_ci_points( GLcontext *ctx, GLuint first, GLuint last )
142 {
143 struct vertex_buffer *VB = ctx->VB;
144 struct pixel_buffer *PB = ctx->PB;
145 GLfloat *win;
146 GLint *pbx = PB->x, *pby = PB->y;
147 GLdepth *pbz = PB->z;
148 GLuint *pbi = PB->i;
149 GLuint pbcount = PB->count;
150 GLuint i;
151
152 win = &VB->Win[first][0];
153 for (i=first;i<=last;i++) {
154 if (VB->Unclipped[i]) {
155 pbx[pbcount] = (GLint) win[0];
156 pby[pbcount] = (GLint) win[1];
157 pbz[pbcount] = (GLint) (win[2] + ctx->PointZoffset);
158 pbi[pbcount] = VB->Index[i];
159 pbcount++;
160 }
161 win += 3;
162 }
163 PB->count = pbcount;
164 PB_CHECK_FLUSH(ctx, PB)
165 }
166
167
168
169 /*
170 * RGBA points with size == 1.0
171 */
172 static void size1_rgba_points( GLcontext *ctx, GLuint first, GLuint last )
173 {
174 struct vertex_buffer *VB = ctx->VB;
175 struct pixel_buffer *PB = ctx->PB;
176 GLuint i;
177 GLint shift = ctx->ColorShift;
178
179 for (i=first;i<=last;i++) {
180 if (VB->Unclipped[i]) {
181 GLint x, y, z;
182 GLint red, green, blue, alpha;
183
184 x = (GLint) VB->Win[i][0];
185 y = (GLint) VB->Win[i][1];
186 z = (GLint) (VB->Win[i][2] + ctx->PointZoffset);
187
188 red = VB->Color[i][0] >> shift;
189 green = VB->Color[i][1] >> shift;
190 blue = VB->Color[i][2] >> shift;
191 alpha = VB->Color[i][3] >> shift;
192
193 PB_WRITE_RGBA_PIXEL( PB, x, y, z, red, green, blue, alpha );
194 }
195 }
196 PB_CHECK_FLUSH(ctx,PB)
197 }
198
199
200
201 /*
202 * General CI points.
203 */
204 static void general_ci_points( GLcontext *ctx, GLuint first, GLuint last )
205 {
206 struct vertex_buffer *VB = ctx->VB;
207 struct pixel_buffer *PB = ctx->PB;
208 GLuint i;
209 GLint isize;
210
211 isize = (GLint) (CLAMP(ctx->Point.Size,MIN_POINT_SIZE,MAX_POINT_SIZE) + 0.5F);
212
213 for (i=first;i<=last;i++) {
214 if (VB->Unclipped[i]) {
215 GLint x, y, z;
216 GLint x0, x1, y0, y1;
217 GLint ix, iy;
218
219 x = (GLint) VB->Win[i][0];
220 y = (GLint) VB->Win[i][1];
221 z = (GLint) (VB->Win[i][2] + ctx->PointZoffset);
222
223 if (isize&1) {
224 /* odd size */
225 x0 = x - isize/2;
226 x1 = x + isize/2;
227 y0 = y - isize/2;
228 y1 = y + isize/2;
229 }
230 else {
231 /* even size */
232 x0 = (GLint) (x + 0.5F) - isize/2;
233 x1 = x0 + isize-1;
234 y0 = (GLint) (y + 0.5F) - isize/2;
235 y1 = y0 + isize-1;
236 }
237
238 PB_SET_INDEX( ctx, PB, VB->Index[i] );
239
240 for (iy=y0;iy<=y1;iy++) {
241 for (ix=x0;ix<=x1;ix++) {
242 PB_WRITE_PIXEL( PB, ix, iy, z );
243 }
244 }
245 PB_CHECK_FLUSH(ctx,PB)
246 }
247 }
248 }
249
250
251 /*
252 * General RGBA points.
253 */
254 static void general_rgba_points( GLcontext *ctx, GLuint first, GLuint last )
255 {
256 struct vertex_buffer *VB = ctx->VB;
257 struct pixel_buffer *PB = ctx->PB;
258 GLuint i;
259 GLint isize;
260 GLint shift = ctx->ColorShift;
261
262 isize = (GLint) (CLAMP(ctx->Point.Size,MIN_POINT_SIZE,MAX_POINT_SIZE) + 0.5F);
263
264 for (i=first;i<=last;i++) {
265 if (VB->Unclipped[i]) {
266 GLint x, y, z;
267 GLint x0, x1, y0, y1;
268 GLint ix, iy;
269
270 x = (GLint) VB->Win[i][0];
271 y = (GLint) VB->Win[i][1];
272 z = (GLint) (VB->Win[i][2] + ctx->PointZoffset);
273
274 if (isize&1) {
275 /* odd size */
276 x0 = x - isize/2;
277 x1 = x + isize/2;
278 y0 = y - isize/2;
279 y1 = y + isize/2;
280 }
281 else {
282 /* even size */
283 x0 = (GLint) (x + 0.5F) - isize/2;
284 x1 = x0 + isize-1;
285 y0 = (GLint) (y + 0.5F) - isize/2;
286 y1 = y0 + isize-1;
287 }
288
289 PB_SET_COLOR( ctx, PB,
290 VB->Color[i][0] >> shift,
291 VB->Color[i][1] >> shift,
292 VB->Color[i][2] >> shift,
293 VB->Color[i][3] >> shift );
294
295 for (iy=y0;iy<=y1;iy++) {
296 for (ix=x0;ix<=x1;ix++) {
297 PB_WRITE_PIXEL( PB, ix, iy, z );
298 }
299 }
300 PB_CHECK_FLUSH(ctx,PB)
301 }
302 }
303 }
304
305
306
307
308 /*
309 * Textured RGBA points.
310 */
311 static void textured_rgba_points( GLcontext *ctx, GLuint first, GLuint last )
312 {
313 struct vertex_buffer *VB = ctx->VB;
314 struct pixel_buffer *PB = ctx->PB;
315 GLuint i;
316 GLint shift = ctx->ColorShift;
317
318 for (i=first;i<=last;i++) {
319 if (VB->Unclipped[i]) {
320 GLint x, y, z;
321 GLint x0, x1, y0, y1;
322 GLint ix, iy;
323 GLint isize;
324 GLint red, green, blue, alpha;
325 GLfloat s, t, u;
326
327 x = (GLint) VB->Win[i][0];
328 y = (GLint) VB->Win[i][1];
329 z = (GLint) (VB->Win[i][2] + ctx->PointZoffset);
330
331 isize = (GLint)
332 (CLAMP(ctx->Point.Size,MIN_POINT_SIZE,MAX_POINT_SIZE) + 0.5F);
333 if (isize<1) {
334 isize = 1;
335 }
336
337 if (isize&1) {
338 /* odd size */
339 x0 = x - isize/2;
340 x1 = x + isize/2;
341 y0 = y - isize/2;
342 y1 = y + isize/2;
343 }
344 else {
345 /* even size */
346 x0 = (GLint) (x + 0.5F) - isize/2;
347 x1 = x0 + isize-1;
348 y0 = (GLint) (y + 0.5F) - isize/2;
349 y1 = y0 + isize-1;
350 }
351
352 red = VB->Color[i][0] >> shift;
353 green = VB->Color[i][1] >> shift;
354 blue = VB->Color[i][2] >> shift;
355 alpha = VB->Color[i][3] >> shift;
356 s = VB->TexCoord[i][0] / VB->TexCoord[i][3];
357 t = VB->TexCoord[i][1] / VB->TexCoord[i][3];
358 u = VB->TexCoord[i][2] / VB->TexCoord[i][3];
359
360 /* don't think this is needed
361 PB_SET_COLOR( red, green, blue, alpha );
362 */
363
364 for (iy=y0;iy<=y1;iy++) {
365 for (ix=x0;ix<=x1;ix++) {
366 PB_WRITE_TEX_PIXEL( PB, ix, iy, z, red, green, blue, alpha, s, t, u );
367 }
368 }
369 PB_CHECK_FLUSH(ctx,PB)
370 }
371 }
372 }
373
374
375
376 /*
377 * Antialiased points with or without texture mapping.
378 */
379 static void antialiased_rgba_points( GLcontext *ctx,
380 GLuint first, GLuint last )
381 {
382 struct vertex_buffer *VB = ctx->VB;
383 struct pixel_buffer *PB = ctx->PB;
384 GLuint i;
385 GLfloat radius, rmin, rmax, rmin2, rmax2, cscale;
386 GLint shift = ctx->ColorShift;
387
388 radius = CLAMP( ctx->Point.Size, MIN_POINT_SIZE, MAX_POINT_SIZE ) * 0.5F;
389 rmin = radius - 0.7071F; /* 0.7071 = sqrt(2)/2 */
390 rmax = radius + 0.7071F;
391 rmin2 = rmin*rmin;
392 rmax2 = rmax*rmax;
393 cscale = 256.0F / (rmax2-rmin2);
394
395 if (gl_texturing_enabled(ctx)) {
396 for (i=first;i<=last;i++) {
397 if (VB->Unclipped[i]) {
398 GLint xmin, ymin, xmax, ymax;
399 GLint x, y, z;
400 GLint red, green, blue, alpha;
401 GLfloat s, t, u;
402
403 xmin = (GLint) (VB->Win[i][0] - radius);
404 xmax = (GLint) (VB->Win[i][0] + radius);
405 ymin = (GLint) (VB->Win[i][1] - radius);
406 ymax = (GLint) (VB->Win[i][1] + radius);
407 z = (GLint) (VB->Win[i][2] + ctx->PointZoffset);
408
409 red = VB->Color[i][0] >> shift;
410 green = VB->Color[i][1] >> shift;
411 blue = VB->Color[i][2] >> shift;
412 s = VB->TexCoord[i][0] / VB->TexCoord[i][3];
413 t = VB->TexCoord[i][1] / VB->TexCoord[i][3];
414 u = VB->TexCoord[i][2] / VB->TexCoord[i][3];
415
416 for (y=ymin;y<=ymax;y++) {
417 for (x=xmin;x<=xmax;x++) {
418 GLfloat dx = x/*+0.5F*/ - VB->Win[i][0];
419 GLfloat dy = y/*+0.5F*/ - VB->Win[i][1];
420 GLfloat dist2 = dx*dx + dy*dy;
421 if (dist2<rmax2) {
422 alpha = VB->Color[i][3] >> shift;
423 if (dist2>=rmin2) {
424 GLint coverage = (GLint) (256.0F-(dist2-rmin2)*cscale);
425 /* coverage is in [0,256] */
426 alpha = (alpha * coverage) >> 8;
427 }
428 PB_WRITE_TEX_PIXEL( PB, x,y,z, red, green, blue, alpha, s, t, u );
429 }
430 }
431 }
432 PB_CHECK_FLUSH(ctx,PB)
433 }
434 }
435 }
436 else {
437 /* Not texture mapped */
438 for (i=first;i<=last;i++) {
439 if (VB->Unclipped[i]) {
440 GLint xmin, ymin, xmax, ymax;
441 GLint x, y, z;
442 GLint red, green, blue, alpha;
443
444 xmin = (GLint) (VB->Win[i][0] - radius);
445 xmax = (GLint) (VB->Win[i][0] + radius);
446 ymin = (GLint) (VB->Win[i][1] - radius);
447 ymax = (GLint) (VB->Win[i][1] + radius);
448 z = (GLint) (VB->Win[i][2] + ctx->PointZoffset);
449
450 red = VB->Color[i][0] >> shift;
451 green = VB->Color[i][1] >> shift;
452 blue = VB->Color[i][2] >> shift;
453
454 for (y=ymin;y<=ymax;y++) {
455 for (x=xmin;x<=xmax;x++) {
456 GLfloat dx = x/*+0.5F*/ - VB->Win[i][0];
457 GLfloat dy = y/*+0.5F*/ - VB->Win[i][1];
458 GLfloat dist2 = dx*dx + dy*dy;
459 if (dist2<rmax2) {
460 alpha = VB->Color[i][3] >> shift;
461 if (dist2>=rmin2) {
462 GLint coverage = (GLint) (256.0F-(dist2-rmin2)*cscale);
463 /* coverage is in [0,256] */
464 alpha = (alpha * coverage) >> 8;
465 }
466 PB_WRITE_RGBA_PIXEL( PB, x, y, z, red, green, blue, alpha );
467 }
468 }
469 }
470 PB_CHECK_FLUSH(ctx,PB)
471 }
472 }
473 }
474 }
475
476
477
478 /*
479 * Null rasterizer for measuring transformation speed.
480 */
481 static void null_points( GLcontext *ctx, GLuint first, GLuint last )
482 {
483 }
484
485
486
487 /*
488 * Examine the current context to determine which point drawing function
489 * should be used.
490 */
491 void gl_set_point_function( GLcontext *ctx )
492 {
493 GLboolean rgbmode = ctx->Visual->RGBAflag;
494
495 if (ctx->RenderMode==GL_RENDER) {
496 if (ctx->NoRaster) {
497 ctx->PointsFunc = null_points;
498 return;
499 }
500 if (ctx->Driver.PointsFunc) {
501 /* Device driver will draw points. */
502 ctx->PointsFunc = ctx->Driver.PointsFunc;
503 }
504 else if (ctx->Point.SmoothFlag && rgbmode) {
505 ctx->PointsFunc = antialiased_rgba_points;
506 }
507 else if (gl_texturing_enabled(ctx)) {
508 ctx->PointsFunc = textured_rgba_points;
509 }
510 else if (ctx->Point.Size==1.0) {
511 /* size=1, any raster ops */
512 ctx->PointsFunc = rgbmode ? size1_rgba_points : size1_ci_points;
513 }
514 else {
515 /* every other kind of point rendering */
516 ctx->PointsFunc = rgbmode ? general_rgba_points
517 : general_ci_points;
518 }
519 }
520 else if (ctx->RenderMode==GL_FEEDBACK) {
521 ctx->PointsFunc = feedback_points;
522 }
523 else {
524 /* GL_SELECT mode */
525 ctx->PointsFunc = select_points;
526 }
527
528 }
529