1 /* $Id: t_vb_normals.c,v 1.17 2002/10/29 20:29:04 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
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:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
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.
27 * Keith Whitwell <keith@tungstengraphics.com>
39 #include "math/m_xform.h"
41 #include "t_context.h"
42 #include "t_pipeline.h"
46 struct normal_stage_data
{
47 normal_func NormalTransform
;
51 #define NORMAL_STAGE_DATA(stage) ((struct normal_stage_data *)stage->privatePtr)
56 static GLboolean
run_normal_stage( GLcontext
*ctx
,
57 struct gl_pipeline_stage
*stage
)
59 struct normal_stage_data
*store
= NORMAL_STAGE_DATA(stage
);
60 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
62 ASSERT(store
->NormalTransform
);
64 if (stage
->changed_inputs
) {
65 /* We can only use the display list's saved normal lengths if we've
66 * got a transformation matrix with uniform scaling.
68 const GLfloat
*lengths
;
69 if (ctx
->ModelviewMatrixStack
.Top
->flags
& MAT_FLAG_GENERAL_SCALE
)
72 lengths
= VB
->NormalLengthPtr
;
74 store
->NormalTransform( ctx
->ModelviewMatrixStack
.Top
,
75 ctx
->_ModelViewInvScale
,
76 VB
->NormalPtr
, /* input normals */
78 &store
->normal
); /* resulting normals */
81 VB
->NormalPtr
= &store
->normal
;
82 VB
->NormalLengthPtr
= 0; /* no longer valid */
87 static GLboolean
run_validate_normal_stage( GLcontext
*ctx
,
88 struct gl_pipeline_stage
*stage
)
90 struct normal_stage_data
*store
= NORMAL_STAGE_DATA(stage
);
92 ASSERT(ctx
->_NeedNormals
);
94 if (ctx
->_NeedEyeCoords
) {
95 GLuint transform
= NORM_TRANSFORM_NO_ROT
;
97 if (ctx
->ModelviewMatrixStack
.Top
->flags
& (MAT_FLAG_GENERAL
|
100 MAT_FLAG_PERSPECTIVE
))
101 transform
= NORM_TRANSFORM
;
104 if (ctx
->Transform
.Normalize
) {
105 store
->NormalTransform
= _mesa_normal_tab
[transform
| NORM_NORMALIZE
];
107 else if (ctx
->Transform
.RescaleNormals
&&
108 ctx
->_ModelViewInvScale
!= 1.0) {
109 store
->NormalTransform
= _mesa_normal_tab
[transform
| NORM_RESCALE
];
112 store
->NormalTransform
= _mesa_normal_tab
[transform
];
116 if (ctx
->Transform
.Normalize
) {
117 store
->NormalTransform
= _mesa_normal_tab
[NORM_NORMALIZE
];
119 else if (!ctx
->Transform
.RescaleNormals
&&
120 ctx
->_ModelViewInvScale
!= 1.0) {
121 store
->NormalTransform
= _mesa_normal_tab
[NORM_RESCALE
];
124 store
->NormalTransform
= 0;
128 if (store
->NormalTransform
) {
129 stage
->run
= run_normal_stage
;
130 return stage
->run( ctx
, stage
);
132 stage
->active
= GL_FALSE
; /* !!! */
138 static void check_normal_transform( GLcontext
*ctx
,
139 struct gl_pipeline_stage
*stage
)
141 stage
->active
= ctx
->_NeedNormals
&& !ctx
->VertexProgram
.Enabled
;
142 /* Don't clobber the initialize function:
144 if (stage
->privatePtr
)
145 stage
->run
= run_validate_normal_stage
;
149 static GLboolean
alloc_normal_data( GLcontext
*ctx
,
150 struct gl_pipeline_stage
*stage
)
152 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
153 struct normal_stage_data
*store
;
154 stage
->privatePtr
= MALLOC(sizeof(*store
));
155 store
= NORMAL_STAGE_DATA(stage
);
159 _mesa_vector4f_alloc( &store
->normal
, 0, tnl
->vb
.Size
, 32 );
161 /* Now run the stage.
163 stage
->run
= run_validate_normal_stage
;
164 return stage
->run( ctx
, stage
);
169 static void free_normal_data( struct gl_pipeline_stage
*stage
)
171 struct normal_stage_data
*store
= NORMAL_STAGE_DATA(stage
);
173 _mesa_vector4f_free( &store
->normal
);
175 stage
->privatePtr
= NULL
;
179 #define _TNL_NEW_NORMAL_TRANSFORM (_NEW_MODELVIEW| \
181 _MESA_NEW_NEED_NORMALS| \
182 _MESA_NEW_NEED_EYE_COORDS)
186 const struct gl_pipeline_stage _tnl_normal_transform_stage
=
188 "normal transform", /* name */
189 _TNL_NEW_NORMAL_TRANSFORM
, /* re-check */
190 _TNL_NEW_NORMAL_TRANSFORM
, /* re-run */
191 GL_FALSE
, /* active? */
192 VERT_BIT_NORMAL
, /* inputs */
193 VERT_BIT_NORMAL
, /* outputs */
194 0, /* changed_inputs */
195 NULL
, /* private data */
196 free_normal_data
, /* destructor */
197 check_normal_transform
, /* check */
198 alloc_normal_data
/* run -- initially set to alloc */