2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
25 * Keith Whitwell <keith@tungstengraphics.com>
36 #include "math/m_xform.h"
38 #include "t_context.h"
39 #include "t_pipeline.h"
43 struct normal_stage_data
{
44 normal_func NormalTransform
;
48 #define NORMAL_STAGE_DATA(stage) ((struct normal_stage_data *)stage->privatePtr)
53 static GLboolean
run_normal_stage( GLcontext
*ctx
,
54 struct tnl_pipeline_stage
*stage
)
56 struct normal_stage_data
*store
= NORMAL_STAGE_DATA(stage
);
57 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
59 ASSERT(store
->NormalTransform
);
61 if (stage
->changed_inputs
) {
62 /* We can only use the display list's saved normal lengths if we've
63 * got a transformation matrix with uniform scaling.
65 const GLfloat
*lengths
;
66 if (ctx
->ModelviewMatrixStack
.Top
->flags
& MAT_FLAG_GENERAL_SCALE
)
69 lengths
= VB
->NormalLengthPtr
;
71 store
->NormalTransform( ctx
->ModelviewMatrixStack
.Top
,
72 ctx
->_ModelViewInvScale
,
73 VB
->NormalPtr
, /* input normals */
75 &store
->normal
); /* resulting normals */
77 if (VB
->NormalPtr
->count
> 1) {
78 store
->normal
.stride
= 16;
81 store
->normal
.stride
= 0;
85 VB
->NormalPtr
= &store
->normal
;
86 VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
] = VB
->NormalPtr
;
88 VB
->NormalLengthPtr
= 0; /* no longer valid */
93 static GLboolean
run_validate_normal_stage( GLcontext
*ctx
,
94 struct tnl_pipeline_stage
*stage
)
96 struct normal_stage_data
*store
= NORMAL_STAGE_DATA(stage
);
99 if (ctx
->_NeedEyeCoords
) {
100 GLuint transform
= NORM_TRANSFORM_NO_ROT
;
102 if (ctx
->ModelviewMatrixStack
.Top
->flags
& (MAT_FLAG_GENERAL
|
104 MAT_FLAG_GENERAL_3D
|
105 MAT_FLAG_PERSPECTIVE
))
106 transform
= NORM_TRANSFORM
;
109 if (ctx
->Transform
.Normalize
) {
110 store
->NormalTransform
= _mesa_normal_tab
[transform
| NORM_NORMALIZE
];
112 else if (ctx
->Transform
.RescaleNormals
&&
113 ctx
->_ModelViewInvScale
!= 1.0) {
114 store
->NormalTransform
= _mesa_normal_tab
[transform
| NORM_RESCALE
];
117 store
->NormalTransform
= _mesa_normal_tab
[transform
];
121 if (ctx
->Transform
.Normalize
) {
122 store
->NormalTransform
= _mesa_normal_tab
[NORM_NORMALIZE
];
124 else if (!ctx
->Transform
.RescaleNormals
&&
125 ctx
->_ModelViewInvScale
!= 1.0) {
126 store
->NormalTransform
= _mesa_normal_tab
[NORM_RESCALE
];
129 store
->NormalTransform
= 0;
133 if (store
->NormalTransform
) {
134 stage
->run
= run_normal_stage
;
135 return stage
->run( ctx
, stage
);
137 stage
->active
= GL_FALSE
; /* !!! */
143 static void check_normal_transform( GLcontext
*ctx
,
144 struct tnl_pipeline_stage
*stage
)
146 stage
->active
= !ctx
->VertexProgram
._Enabled
&&
147 (ctx
->Light
.Enabled
|| (ctx
->Texture
._GenFlags
& TEXGEN_NEED_NORMALS
));
149 /* Don't clobber the initialize function:
151 if (stage
->privatePtr
)
152 stage
->run
= run_validate_normal_stage
;
156 static GLboolean
alloc_normal_data( GLcontext
*ctx
,
157 struct tnl_pipeline_stage
*stage
)
159 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
160 struct normal_stage_data
*store
;
161 stage
->privatePtr
= MALLOC(sizeof(*store
));
162 store
= NORMAL_STAGE_DATA(stage
);
166 _mesa_vector4f_alloc( &store
->normal
, 0, tnl
->vb
.Size
, 32 );
168 /* Now run the stage.
170 stage
->run
= run_validate_normal_stage
;
171 return stage
->run( ctx
, stage
);
176 static void free_normal_data( struct tnl_pipeline_stage
*stage
)
178 struct normal_stage_data
*store
= NORMAL_STAGE_DATA(stage
);
180 _mesa_vector4f_free( &store
->normal
);
182 stage
->privatePtr
= NULL
;
186 #define _TNL_NEW_NORMAL_TRANSFORM (_NEW_MODELVIEW| \
189 _MESA_NEW_NEED_NORMALS| \
190 _MESA_NEW_NEED_EYE_COORDS)
194 const struct tnl_pipeline_stage _tnl_normal_transform_stage
=
196 "normal transform", /* name */
197 _TNL_NEW_NORMAL_TRANSFORM
, /* re-check */
198 _TNL_NEW_NORMAL_TRANSFORM
, /* re-run */
199 GL_FALSE
, /* active? */
200 _TNL_BIT_NORMAL
, /* inputs */
201 _TNL_BIT_NORMAL
, /* outputs */
202 0, /* changed_inputs */
203 NULL
, /* private data */
204 free_normal_data
, /* destructor */
205 check_normal_transform
, /* check */
206 alloc_normal_data
/* run -- initially set to alloc */