Merge vtx-0-2-branch
[mesa.git] / src / mesa / tnl / t_vb_points.c
1
2 /*
3 * Mesa 3-D graphics library
4 * Version: 5.1
5 *
6 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 * Brian Paul
27 */
28
29 #include "mtypes.h"
30 #include "imports.h"
31 #include "t_context.h"
32 #include "t_pipeline.h"
33
34
35 struct point_stage_data {
36 GLvector4f PointSize;
37 };
38
39 #define POINT_STAGE_DATA(stage) ((struct point_stage_data *)stage->privatePtr)
40
41
42 /*
43 * Compute attenuated point sizes
44 */
45 static GLboolean run_point_stage( GLcontext *ctx,
46 struct tnl_pipeline_stage *stage )
47 {
48 struct point_stage_data *store = POINT_STAGE_DATA(stage);
49 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
50 const GLfloat (*eye)[4] = (const GLfloat (*)[4]) VB->EyePtr->data;
51 const GLfloat p0 = ctx->Point.Params[0];
52 const GLfloat p1 = ctx->Point.Params[1];
53 const GLfloat p2 = ctx->Point.Params[2];
54 const GLfloat pointSize = ctx->Point._Size;
55 GLfloat (*size)[4] = store->PointSize.data;
56 GLuint i;
57
58 if (stage->changed_inputs) {
59 /* XXX do threshold and min/max clamping here? */
60 for (i = 0; i < VB->Count; i++) {
61 const GLfloat dist = -eye[i][2];
62 /* GLfloat dist = SQRTF(pos[0]*pos[0]+pos[1]*pos[1]+pos[2]*pos[2]);*/
63 size[i][0] = pointSize / (p0 + dist * (p1 + dist * p2));
64 }
65 }
66
67 VB->PointSizePtr = &store->PointSize;
68
69 return GL_TRUE;
70 }
71
72
73 /* If point size attenuation is on we'll compute the point size for
74 * each vertex in a special pipeline stage.
75 */
76 static void check_point_size( GLcontext *ctx, struct tnl_pipeline_stage *d )
77 {
78 d->active = ctx->Point._Attenuated && !ctx->VertexProgram.Enabled;
79 }
80
81 static GLboolean alloc_point_data( GLcontext *ctx,
82 struct tnl_pipeline_stage *stage )
83 {
84 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
85 struct point_stage_data *store;
86 stage->privatePtr = MALLOC(sizeof(*store));
87 store = POINT_STAGE_DATA(stage);
88 if (!store)
89 return GL_FALSE;
90
91 _mesa_vector4f_alloc( &store->PointSize, 0, VB->Size, 32 );
92
93 /* Now run the stage.
94 */
95 stage->run = run_point_stage;
96 return stage->run( ctx, stage );
97 }
98
99
100 static void free_point_data( struct tnl_pipeline_stage *stage )
101 {
102 struct point_stage_data *store = POINT_STAGE_DATA(stage);
103 if (store) {
104 _mesa_vector4f_free( &store->PointSize );
105 FREE( store );
106 stage->privatePtr = 0;
107 }
108 }
109
110 const struct tnl_pipeline_stage _tnl_point_attenuation_stage =
111 {
112 "point size attenuation", /* name */
113 _NEW_POINT, /* build_state_change */
114 _NEW_POINT, /* run_state_change */
115 GL_FALSE, /* active */
116 _TNL_BIT_POS, /* inputs */
117 _TNL_BIT_POS, /* outputs */
118 0, /* changed_inputs (temporary value) */
119 NULL, /* stage private data */
120 free_point_data, /* destructor */
121 check_point_size, /* check */
122 alloc_point_data /* run -- initially set to alloc data */
123 };