1 /* $Id: t_vb_normals.c,v 1.6 2001/03/07 05:06:13 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2000 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
[0])(&ctx
->ModelView
,
66 ctx
->_ModelViewInvScale
,
72 VB
->NormalPtr
= &store
->normal
;
77 static GLboolean
run_validate_normal_stage( GLcontext
*ctx
,
78 struct gl_pipeline_stage
*stage
)
80 struct normal_stage_data
*store
= NORMAL_STAGE_DATA(stage
);
82 ASSERT(ctx
->_NeedNormals
);
84 if (ctx
->_NeedEyeCoords
) {
85 GLuint transform
= NORM_TRANSFORM_NO_ROT
;
87 if (ctx
->ModelView
.flags
& (MAT_FLAG_GENERAL
|
90 MAT_FLAG_PERSPECTIVE
))
91 transform
= NORM_TRANSFORM
;
94 if (ctx
->Transform
.Normalize
) {
95 store
->NormalTransform
= _mesa_normal_tab
[transform
| NORM_NORMALIZE
];
97 else if (ctx
->Transform
.RescaleNormals
&&
98 ctx
->_ModelViewInvScale
!= 1.0) {
99 store
->NormalTransform
= _mesa_normal_tab
[transform
| NORM_RESCALE
];
102 store
->NormalTransform
= _mesa_normal_tab
[transform
];
106 if (ctx
->Transform
.Normalize
) {
107 store
->NormalTransform
= _mesa_normal_tab
[NORM_NORMALIZE
];
109 else if (!ctx
->Transform
.RescaleNormals
&&
110 ctx
->_ModelViewInvScale
!= 1.0) {
111 store
->NormalTransform
= _mesa_normal_tab
[NORM_RESCALE
];
114 store
->NormalTransform
= 0;
118 if (store
->NormalTransform
) {
119 stage
->run
= run_normal_stage
;
120 return stage
->run( ctx
, stage
);
122 stage
->active
= GL_FALSE
; /* !!! */
128 static void check_normal_transform( GLcontext
*ctx
,
129 struct gl_pipeline_stage
*stage
)
131 stage
->active
= ctx
->_NeedNormals
;
132 /* Don't clobber the initialize function:
134 if (stage
->privatePtr
)
135 stage
->run
= run_validate_normal_stage
;
139 static GLboolean
alloc_normal_data( GLcontext
*ctx
,
140 struct gl_pipeline_stage
*stage
)
142 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
143 struct normal_stage_data
*store
;
144 stage
->privatePtr
= MALLOC(sizeof(*store
));
145 store
= NORMAL_STAGE_DATA(stage
);
149 _mesa_vector3f_alloc( &store
->normal
, 0, tnl
->vb
.Size
, 32 );
151 /* Now run the stage.
153 stage
->run
= run_validate_normal_stage
;
154 return stage
->run( ctx
, stage
);
159 static void free_normal_data( struct gl_pipeline_stage
*stage
)
161 struct normal_stage_data
*store
= NORMAL_STAGE_DATA(stage
);
163 _mesa_vector3f_free( &store
->normal
);
165 stage
->privatePtr
= NULL
;
169 #define _TNL_NEW_NORMAL_TRANSFORM (_NEW_MODELVIEW| \
171 _MESA_NEW_NEED_NORMALS| \
172 _MESA_NEW_NEED_EYE_COORDS)
176 const struct gl_pipeline_stage _tnl_normal_transform_stage
=
179 _TNL_NEW_NORMAL_TRANSFORM
, /* re-check */
180 _TNL_NEW_NORMAL_TRANSFORM
, /* re-run */
181 0,VERT_NORM
,VERT_NORM
, /* active, inputs, outputs */
182 0, 0, /* changed_inputs, private */
183 free_normal_data
, /* destructor */
184 check_normal_transform
, /* check */
185 alloc_normal_data
/* run -- initially set to alloc */