1 /* $Id: t_vb_normals.c,v 1.8 2001/03/30 14:44:44 gareth Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 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 <keithw@valinux.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 store
->NormalTransform( &ctx
->ModelView
,
66 ctx
->_ModelViewInvScale
,
71 VB
->NormalPtr
= &store
->normal
;
76 static GLboolean
run_validate_normal_stage( GLcontext
*ctx
,
77 struct gl_pipeline_stage
*stage
)
79 struct normal_stage_data
*store
= NORMAL_STAGE_DATA(stage
);
81 ASSERT(ctx
->_NeedNormals
);
83 if (ctx
->_NeedEyeCoords
) {
84 GLuint transform
= NORM_TRANSFORM_NO_ROT
;
86 if (ctx
->ModelView
.flags
& (MAT_FLAG_GENERAL
|
89 MAT_FLAG_PERSPECTIVE
))
90 transform
= NORM_TRANSFORM
;
93 if (ctx
->Transform
.Normalize
) {
94 store
->NormalTransform
= _mesa_normal_tab
[transform
| NORM_NORMALIZE
];
96 else if (ctx
->Transform
.RescaleNormals
&&
97 ctx
->_ModelViewInvScale
!= 1.0) {
98 store
->NormalTransform
= _mesa_normal_tab
[transform
| NORM_RESCALE
];
101 store
->NormalTransform
= _mesa_normal_tab
[transform
];
105 if (ctx
->Transform
.Normalize
) {
106 store
->NormalTransform
= _mesa_normal_tab
[NORM_NORMALIZE
];
108 else if (!ctx
->Transform
.RescaleNormals
&&
109 ctx
->_ModelViewInvScale
!= 1.0) {
110 store
->NormalTransform
= _mesa_normal_tab
[NORM_RESCALE
];
113 store
->NormalTransform
= 0;
117 if (store
->NormalTransform
) {
118 stage
->run
= run_normal_stage
;
119 return stage
->run( ctx
, stage
);
121 stage
->active
= GL_FALSE
; /* !!! */
127 static void check_normal_transform( GLcontext
*ctx
,
128 struct gl_pipeline_stage
*stage
)
130 stage
->active
= ctx
->_NeedNormals
;
131 /* Don't clobber the initialize function:
133 if (stage
->privatePtr
)
134 stage
->run
= run_validate_normal_stage
;
138 static GLboolean
alloc_normal_data( GLcontext
*ctx
,
139 struct gl_pipeline_stage
*stage
)
141 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
142 struct normal_stage_data
*store
;
143 stage
->privatePtr
= MALLOC(sizeof(*store
));
144 store
= NORMAL_STAGE_DATA(stage
);
148 _mesa_vector3f_alloc( &store
->normal
, 0, tnl
->vb
.Size
, 32 );
150 /* Now run the stage.
152 stage
->run
= run_validate_normal_stage
;
153 return stage
->run( ctx
, stage
);
158 static void free_normal_data( struct gl_pipeline_stage
*stage
)
160 struct normal_stage_data
*store
= NORMAL_STAGE_DATA(stage
);
162 _mesa_vector3f_free( &store
->normal
);
164 stage
->privatePtr
= NULL
;
168 #define _TNL_NEW_NORMAL_TRANSFORM (_NEW_MODELVIEW| \
170 _MESA_NEW_NEED_NORMALS| \
171 _MESA_NEW_NEED_EYE_COORDS)
175 const struct gl_pipeline_stage _tnl_normal_transform_stage
=
178 _TNL_NEW_NORMAL_TRANSFORM
, /* re-check */
179 _TNL_NEW_NORMAL_TRANSFORM
, /* re-run */
180 0,VERT_NORM
,VERT_NORM
, /* active, inputs, outputs */
181 0, 0, /* changed_inputs, private */
182 free_normal_data
, /* destructor */
183 check_normal_transform
, /* check */
184 alloc_normal_data
/* run -- initially set to alloc */