| 1 |
/* |
| 2 |
* Mesa 3-D graphics library |
| 3 |
* Version: 6.5 |
| 4 |
* |
| 5 |
* Copyright (C) 1999-2005 Brian Paul All Rights Reserved. |
| 6 |
* |
| 7 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 8 |
* copy of this software and associated documentation files (the "Software"), |
| 9 |
* to deal in the Software without restriction, including without limitation |
| 10 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 12 |
* Software is furnished to do so, subject to the following conditions: |
| 13 |
* |
| 14 |
* The above copyright notice and this permission notice shall be included |
| 15 |
* in all copies or substantial portions of the Software. |
| 16 |
* |
| 17 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 18 |
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 |
* BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 21 |
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 |
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 |
* |
| 24 |
* Authors: |
| 25 |
* Brian Paul |
| 26 |
*/ |
| 27 |
|
| 28 |
#include "mtypes.h" |
| 29 |
#include "imports.h" |
| 30 |
#include "t_context.h" |
| 31 |
#include "t_pipeline.h" |
| 32 |
|
| 33 |
|
| 34 |
struct point_stage_data { |
| 35 |
GLvector4f PointSize; |
| 36 |
}; |
| 37 |
|
| 38 |
#define POINT_STAGE_DATA(stage) ((struct point_stage_data *)stage->privatePtr) |
| 39 |
|
| 40 |
|
| 41 |
/** |
| 42 |
* Compute point size for each vertex from the vertex eye-space Z |
| 43 |
* coordinate and the point size attenuation factors. |
| 44 |
* Only done when point size attenuation is enabled and vertex program is |
| 45 |
* disabled. |
| 46 |
*/ |
| 47 |
static GLboolean |
| 48 |
run_point_stage(GLcontext *ctx, struct tnl_pipeline_stage *stage) |
| 49 |
{ |
| 50 |
if (ctx->Point._Attenuated && !ctx->VertexProgram._Enabled) { |
| 51 |
struct point_stage_data *store = POINT_STAGE_DATA(stage); |
| 52 |
struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb; |
| 53 |
const GLfloat (*eye)[4] = (const GLfloat (*)[4]) VB->EyePtr->data; |
| 54 |
const GLfloat p0 = ctx->Point.Params[0]; |
| 55 |
const GLfloat p1 = ctx->Point.Params[1]; |
| 56 |
const GLfloat p2 = ctx->Point.Params[2]; |
| 57 |
const GLfloat pointSize = ctx->Point.Size; |
| 58 |
GLfloat (*size)[4] = store->PointSize.data; |
| 59 |
GLuint i; |
| 60 |
|
| 61 |
for (i = 0; i < VB->Count; i++) { |
| 62 |
const GLfloat dist = FABSF(eye[i][2]); |
| 63 |
const GLfloat q = p0 + dist * (p1 + dist * p2); |
| 64 |
const GLfloat atten = (q != 0.0) ? SQRTF(1.0 / q) : 1.0; |
| 65 |
size[i][0] = pointSize * atten; /* clamping done in rasterization */ |
| 66 |
} |
| 67 |
|
| 68 |
VB->PointSizePtr = &store->PointSize; |
| 69 |
VB->AttribPtr[_TNL_ATTRIB_POINTSIZE] = &store->PointSize; |
| 70 |
} |
| 71 |
|
| 72 |
return GL_TRUE; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
static GLboolean |
| 77 |
alloc_point_data(GLcontext *ctx, struct tnl_pipeline_stage *stage) |
| 78 |
{ |
| 79 |
struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb; |
| 80 |
struct point_stage_data *store; |
| 81 |
stage->privatePtr = _mesa_malloc(sizeof(*store)); |
| 82 |
store = POINT_STAGE_DATA(stage); |
| 83 |
if (!store) |
| 84 |
return GL_FALSE; |
| 85 |
|
| 86 |
_mesa_vector4f_alloc( &store->PointSize, 0, VB->Size, 32 ); |
| 87 |
return GL_TRUE; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
static void |
| 92 |
free_point_data(struct tnl_pipeline_stage *stage) |
| 93 |
{ |
| 94 |
struct point_stage_data *store = POINT_STAGE_DATA(stage); |
| 95 |
if (store) { |
| 96 |
_mesa_vector4f_free( &store->PointSize ); |
| 97 |
_mesa_free( store ); |
| 98 |
stage->privatePtr = NULL; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
const struct tnl_pipeline_stage _tnl_point_attenuation_stage = |
| 104 |
{ |
| 105 |
"point size attenuation", /* name */ |
| 106 |
NULL, /* stage private data */ |
| 107 |
alloc_point_data, /* alloc data */ |
| 108 |
free_point_data, /* destructor */ |
| 109 |
NULL, |
| 110 |
run_point_stage /* run */ |
| 111 |
}; |