Remove all traces of CULL_MASK_ACTIVE.
[mesa.git] / src / mesa / tnl / t_vb_normals.c
1 /* $Id: t_vb_normals.c,v 1.8 2001/03/30 14:44:44 gareth Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
8 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
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.
25 *
26 * Authors:
27 * Keith Whitwell <keithw@valinux.com>
28 */
29
30
31 #include "glheader.h"
32 #include "colormac.h"
33 #include "context.h"
34 #include "macros.h"
35 #include "mem.h"
36 #include "mmath.h"
37 #include "mtypes.h"
38
39 #include "math/m_xform.h"
40
41 #include "t_context.h"
42 #include "t_pipeline.h"
43
44
45
46 struct normal_stage_data {
47 normal_func NormalTransform;
48 GLvector3f normal;
49 };
50
51 #define NORMAL_STAGE_DATA(stage) ((struct normal_stage_data *)stage->privatePtr)
52
53
54
55
56 static GLboolean run_normal_stage( GLcontext *ctx,
57 struct gl_pipeline_stage *stage )
58 {
59 struct normal_stage_data *store = NORMAL_STAGE_DATA(stage);
60 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
61
62 ASSERT(store->NormalTransform);
63
64 if (stage->changed_inputs)
65 store->NormalTransform( &ctx->ModelView,
66 ctx->_ModelViewInvScale,
67 VB->NormalPtr,
68 0,
69 &store->normal );
70
71 VB->NormalPtr = &store->normal;
72 return GL_TRUE;
73 }
74
75
76 static GLboolean run_validate_normal_stage( GLcontext *ctx,
77 struct gl_pipeline_stage *stage )
78 {
79 struct normal_stage_data *store = NORMAL_STAGE_DATA(stage);
80
81 ASSERT(ctx->_NeedNormals);
82
83 if (ctx->_NeedEyeCoords) {
84 GLuint transform = NORM_TRANSFORM_NO_ROT;
85
86 if (ctx->ModelView.flags & (MAT_FLAG_GENERAL |
87 MAT_FLAG_ROTATION |
88 MAT_FLAG_GENERAL_3D |
89 MAT_FLAG_PERSPECTIVE))
90 transform = NORM_TRANSFORM;
91
92
93 if (ctx->Transform.Normalize) {
94 store->NormalTransform = _mesa_normal_tab[transform | NORM_NORMALIZE];
95 }
96 else if (ctx->Transform.RescaleNormals &&
97 ctx->_ModelViewInvScale != 1.0) {
98 store->NormalTransform = _mesa_normal_tab[transform | NORM_RESCALE];
99 }
100 else {
101 store->NormalTransform = _mesa_normal_tab[transform];
102 }
103 }
104 else {
105 if (ctx->Transform.Normalize) {
106 store->NormalTransform = _mesa_normal_tab[NORM_NORMALIZE];
107 }
108 else if (!ctx->Transform.RescaleNormals &&
109 ctx->_ModelViewInvScale != 1.0) {
110 store->NormalTransform = _mesa_normal_tab[NORM_RESCALE];
111 }
112 else {
113 store->NormalTransform = 0;
114 }
115 }
116
117 if (store->NormalTransform) {
118 stage->run = run_normal_stage;
119 return stage->run( ctx, stage );
120 } else {
121 stage->active = GL_FALSE; /* !!! */
122 return GL_TRUE;
123 }
124 }
125
126
127 static void check_normal_transform( GLcontext *ctx,
128 struct gl_pipeline_stage *stage )
129 {
130 stage->active = ctx->_NeedNormals;
131 /* Don't clobber the initialize function:
132 */
133 if (stage->privatePtr)
134 stage->run = run_validate_normal_stage;
135 }
136
137
138 static GLboolean alloc_normal_data( GLcontext *ctx,
139 struct gl_pipeline_stage *stage )
140 {
141 TNLcontext *tnl = TNL_CONTEXT(ctx);
142 struct normal_stage_data *store;
143 stage->privatePtr = MALLOC(sizeof(*store));
144 store = NORMAL_STAGE_DATA(stage);
145 if (!store)
146 return GL_FALSE;
147
148 _mesa_vector3f_alloc( &store->normal, 0, tnl->vb.Size, 32 );
149
150 /* Now run the stage.
151 */
152 stage->run = run_validate_normal_stage;
153 return stage->run( ctx, stage );
154 }
155
156
157
158 static void free_normal_data( struct gl_pipeline_stage *stage )
159 {
160 struct normal_stage_data *store = NORMAL_STAGE_DATA(stage);
161 if (store) {
162 _mesa_vector3f_free( &store->normal );
163 FREE( store );
164 stage->privatePtr = NULL;
165 }
166 }
167
168 #define _TNL_NEW_NORMAL_TRANSFORM (_NEW_MODELVIEW| \
169 _NEW_TRANSFORM| \
170 _MESA_NEW_NEED_NORMALS| \
171 _MESA_NEW_NEED_EYE_COORDS)
172
173
174
175 const struct gl_pipeline_stage _tnl_normal_transform_stage =
176 {
177 "normal transform",
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 */
185 };