fixed a bunch of g++ warnings/errors. Compiling with g++ can help find lots of poten...
[mesa.git] / src / mesa / tnl / t_vb_normals.c
1 /* $Id: t_vb_normals.c,v 1.6 2001/03/07 05:06:13 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2000 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 * Author:
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[0])(&ctx->ModelView,
66 ctx->_ModelViewInvScale,
67 VB->NormalPtr,
68 0,
69 0,
70 &store->normal);
71
72 VB->NormalPtr = &store->normal;
73 return GL_TRUE;
74 }
75
76
77 static GLboolean run_validate_normal_stage( GLcontext *ctx,
78 struct gl_pipeline_stage *stage)
79 {
80 struct normal_stage_data *store = NORMAL_STAGE_DATA(stage);
81
82 ASSERT(ctx->_NeedNormals);
83
84 if (ctx->_NeedEyeCoords) {
85 GLuint transform = NORM_TRANSFORM_NO_ROT;
86
87 if (ctx->ModelView.flags & (MAT_FLAG_GENERAL |
88 MAT_FLAG_ROTATION |
89 MAT_FLAG_GENERAL_3D |
90 MAT_FLAG_PERSPECTIVE))
91 transform = NORM_TRANSFORM;
92
93
94 if (ctx->Transform.Normalize) {
95 store->NormalTransform = _mesa_normal_tab[transform | NORM_NORMALIZE];
96 }
97 else if (ctx->Transform.RescaleNormals &&
98 ctx->_ModelViewInvScale != 1.0) {
99 store->NormalTransform = _mesa_normal_tab[transform | NORM_RESCALE];
100 }
101 else {
102 store->NormalTransform = _mesa_normal_tab[transform];
103 }
104 }
105 else {
106 if (ctx->Transform.Normalize) {
107 store->NormalTransform = _mesa_normal_tab[NORM_NORMALIZE];
108 }
109 else if (!ctx->Transform.RescaleNormals &&
110 ctx->_ModelViewInvScale != 1.0) {
111 store->NormalTransform = _mesa_normal_tab[NORM_RESCALE];
112 }
113 else {
114 store->NormalTransform = 0;
115 }
116 }
117
118 if (store->NormalTransform) {
119 stage->run = run_normal_stage;
120 return stage->run( ctx, stage );
121 } else {
122 stage->active = GL_FALSE; /* !!! */
123 return GL_TRUE;
124 }
125 }
126
127
128 static void check_normal_transform( GLcontext *ctx,
129 struct gl_pipeline_stage *stage )
130 {
131 stage->active = ctx->_NeedNormals;
132 /* Don't clobber the initialize function:
133 */
134 if (stage->privatePtr)
135 stage->run = run_validate_normal_stage;
136 }
137
138
139 static GLboolean alloc_normal_data( GLcontext *ctx,
140 struct gl_pipeline_stage *stage )
141 {
142 TNLcontext *tnl = TNL_CONTEXT(ctx);
143 struct normal_stage_data *store;
144 stage->privatePtr = MALLOC(sizeof(*store));
145 store = NORMAL_STAGE_DATA(stage);
146 if (!store)
147 return GL_FALSE;
148
149 _mesa_vector3f_alloc( &store->normal, 0, tnl->vb.Size, 32 );
150
151 /* Now run the stage.
152 */
153 stage->run = run_validate_normal_stage;
154 return stage->run( ctx, stage );
155 }
156
157
158
159 static void free_normal_data( struct gl_pipeline_stage *stage )
160 {
161 struct normal_stage_data *store = NORMAL_STAGE_DATA(stage);
162 if (store) {
163 _mesa_vector3f_free( &store->normal );
164 FREE( store );
165 stage->privatePtr = NULL;
166 }
167 }
168
169 #define _TNL_NEW_NORMAL_TRANSFORM (_NEW_MODELVIEW| \
170 _NEW_TRANSFORM| \
171 _MESA_NEW_NEED_NORMALS| \
172 _MESA_NEW_NEED_EYE_COORDS)
173
174
175
176 const struct gl_pipeline_stage _tnl_normal_transform_stage =
177 {
178 "normal transform",
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 */
186 };
187