Removed all RCS / CVS tags (Id, Header, Date, etc.) from everything.
[mesa.git] / src / mesa / tnl / t_vb_normals.c
1
2 /*
3 * Mesa 3-D graphics library
4 * Version: 5.1
5 *
6 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 * Keith Whitwell <keith@tungstengraphics.com>
27 */
28
29
30 #include "glheader.h"
31 #include "colormac.h"
32 #include "context.h"
33 #include "macros.h"
34 #include "imports.h"
35 #include "mtypes.h"
36
37 #include "math/m_xform.h"
38
39 #include "t_context.h"
40 #include "t_pipeline.h"
41
42
43
44 struct normal_stage_data {
45 normal_func NormalTransform;
46 GLvector4f normal;
47 };
48
49 #define NORMAL_STAGE_DATA(stage) ((struct normal_stage_data *)stage->privatePtr)
50
51
52
53
54 static GLboolean run_normal_stage( GLcontext *ctx,
55 struct gl_pipeline_stage *stage )
56 {
57 struct normal_stage_data *store = NORMAL_STAGE_DATA(stage);
58 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
59
60 ASSERT(store->NormalTransform);
61
62 if (stage->changed_inputs) {
63 /* We can only use the display list's saved normal lengths if we've
64 * got a transformation matrix with uniform scaling.
65 */
66 const GLfloat *lengths;
67 if (ctx->ModelviewMatrixStack.Top->flags & MAT_FLAG_GENERAL_SCALE)
68 lengths = NULL;
69 else
70 lengths = VB->NormalLengthPtr;
71
72 store->NormalTransform( ctx->ModelviewMatrixStack.Top,
73 ctx->_ModelViewInvScale,
74 VB->NormalPtr, /* input normals */
75 lengths,
76 &store->normal ); /* resulting normals */
77 }
78
79 VB->NormalPtr = &store->normal;
80 VB->NormalLengthPtr = 0; /* no longer valid */
81 return GL_TRUE;
82 }
83
84
85 static GLboolean run_validate_normal_stage( GLcontext *ctx,
86 struct gl_pipeline_stage *stage )
87 {
88 struct normal_stage_data *store = NORMAL_STAGE_DATA(stage);
89
90 ASSERT(ctx->_NeedNormals);
91
92 if (ctx->_NeedEyeCoords) {
93 GLuint transform = NORM_TRANSFORM_NO_ROT;
94
95 if (ctx->ModelviewMatrixStack.Top->flags & (MAT_FLAG_GENERAL |
96 MAT_FLAG_ROTATION |
97 MAT_FLAG_GENERAL_3D |
98 MAT_FLAG_PERSPECTIVE))
99 transform = NORM_TRANSFORM;
100
101
102 if (ctx->Transform.Normalize) {
103 store->NormalTransform = _mesa_normal_tab[transform | NORM_NORMALIZE];
104 }
105 else if (ctx->Transform.RescaleNormals &&
106 ctx->_ModelViewInvScale != 1.0) {
107 store->NormalTransform = _mesa_normal_tab[transform | NORM_RESCALE];
108 }
109 else {
110 store->NormalTransform = _mesa_normal_tab[transform];
111 }
112 }
113 else {
114 if (ctx->Transform.Normalize) {
115 store->NormalTransform = _mesa_normal_tab[NORM_NORMALIZE];
116 }
117 else if (!ctx->Transform.RescaleNormals &&
118 ctx->_ModelViewInvScale != 1.0) {
119 store->NormalTransform = _mesa_normal_tab[NORM_RESCALE];
120 }
121 else {
122 store->NormalTransform = 0;
123 }
124 }
125
126 if (store->NormalTransform) {
127 stage->run = run_normal_stage;
128 return stage->run( ctx, stage );
129 } else {
130 stage->active = GL_FALSE; /* !!! */
131 return GL_TRUE;
132 }
133 }
134
135
136 static void check_normal_transform( GLcontext *ctx,
137 struct gl_pipeline_stage *stage )
138 {
139 stage->active = ctx->_NeedNormals && !ctx->VertexProgram.Enabled;
140 /* Don't clobber the initialize function:
141 */
142 if (stage->privatePtr)
143 stage->run = run_validate_normal_stage;
144 }
145
146
147 static GLboolean alloc_normal_data( GLcontext *ctx,
148 struct gl_pipeline_stage *stage )
149 {
150 TNLcontext *tnl = TNL_CONTEXT(ctx);
151 struct normal_stage_data *store;
152 stage->privatePtr = MALLOC(sizeof(*store));
153 store = NORMAL_STAGE_DATA(stage);
154 if (!store)
155 return GL_FALSE;
156
157 _mesa_vector4f_alloc( &store->normal, 0, tnl->vb.Size, 32 );
158
159 /* Now run the stage.
160 */
161 stage->run = run_validate_normal_stage;
162 return stage->run( ctx, stage );
163 }
164
165
166
167 static void free_normal_data( struct gl_pipeline_stage *stage )
168 {
169 struct normal_stage_data *store = NORMAL_STAGE_DATA(stage);
170 if (store) {
171 _mesa_vector4f_free( &store->normal );
172 FREE( store );
173 stage->privatePtr = NULL;
174 }
175 }
176
177 #define _TNL_NEW_NORMAL_TRANSFORM (_NEW_MODELVIEW| \
178 _NEW_TRANSFORM| \
179 _MESA_NEW_NEED_NORMALS| \
180 _MESA_NEW_NEED_EYE_COORDS)
181
182
183
184 const struct gl_pipeline_stage _tnl_normal_transform_stage =
185 {
186 "normal transform", /* name */
187 _TNL_NEW_NORMAL_TRANSFORM, /* re-check */
188 _TNL_NEW_NORMAL_TRANSFORM, /* re-run */
189 GL_FALSE, /* active? */
190 VERT_BIT_NORMAL, /* inputs */
191 VERT_BIT_NORMAL, /* outputs */
192 0, /* changed_inputs */
193 NULL, /* private data */
194 free_normal_data, /* destructor */
195 check_normal_transform, /* check */
196 alloc_normal_data /* run -- initially set to alloc */
197 };