Update Visual Studio Project file for src tree updates.
[mesa.git] / src / mesa / tnl / t_vtx_eval.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.1
4 *
5 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Keith Whitwell <keith@tungstengraphics.com>
26 */
27
28 #include "glheader.h"
29 #include "api_eval.h"
30 #include "context.h"
31 #include "macros.h"
32 #include "math/m_eval.h"
33 #include "t_vtx_api.h"
34
35
36 static void clear_active_eval1( TNLcontext *tnl, GLuint attr )
37 {
38 tnl->vtx.eval.map1[attr].map = 0;
39 }
40
41 static void clear_active_eval2( TNLcontext *tnl, GLuint attr )
42 {
43 tnl->vtx.eval.map2[attr].map = 0;
44 }
45
46 static void set_active_eval1( TNLcontext *tnl, GLuint attr, GLuint dim,
47 struct gl_1d_map *map )
48 {
49 if (!tnl->vtx.eval.map1[attr].map) {
50 tnl->vtx.eval.map1[attr].map = map;
51 tnl->vtx.eval.map1[attr].sz = dim;
52 }
53 }
54
55 static void set_active_eval2( TNLcontext *tnl, GLuint attr, GLuint dim,
56 struct gl_2d_map *map )
57 {
58 if (!tnl->vtx.eval.map2[attr].map) {
59 tnl->vtx.eval.map2[attr].map = map;
60 tnl->vtx.eval.map2[attr].sz = dim;
61 }
62 }
63
64 void _tnl_update_eval( GLcontext *ctx )
65 {
66 TNLcontext *tnl = TNL_CONTEXT(ctx);
67 GLuint attr;
68
69 /* Vertex program maps have priority over conventional attribs */
70
71 for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
72 clear_active_eval1( tnl, attr );
73 clear_active_eval2( tnl, attr );
74 }
75
76 if (ctx->VertexProgram._Enabled) {
77 for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
78 if (ctx->Eval.Map1Attrib[attr])
79 set_active_eval1( tnl, attr, 4, &ctx->EvalMap.Map1Attrib[attr] );
80
81 if (ctx->Eval.Map2Attrib[attr])
82 set_active_eval2( tnl, attr, 4, &ctx->EvalMap.Map2Attrib[attr] );
83 }
84 }
85
86 if (ctx->Eval.Map1Color4)
87 set_active_eval1( tnl, VERT_ATTRIB_COLOR0, 4, &ctx->EvalMap.Map1Color4 );
88
89 if (ctx->Eval.Map2Color4)
90 set_active_eval2( tnl, VERT_ATTRIB_COLOR0, 4, &ctx->EvalMap.Map2Color4 );
91
92 if (ctx->Eval.Map1TextureCoord4)
93 set_active_eval1( tnl, VERT_ATTRIB_TEX0, 4, &ctx->EvalMap.Map1Texture4 );
94 else if (ctx->Eval.Map1TextureCoord3)
95 set_active_eval1( tnl, VERT_ATTRIB_TEX0, 3, &ctx->EvalMap.Map1Texture3 );
96 else if (ctx->Eval.Map1TextureCoord2)
97 set_active_eval1( tnl, VERT_ATTRIB_TEX0, 2, &ctx->EvalMap.Map1Texture2 );
98 else if (ctx->Eval.Map1TextureCoord1)
99 set_active_eval1( tnl, VERT_ATTRIB_TEX0, 1, &ctx->EvalMap.Map1Texture1 );
100
101 if (ctx->Eval.Map2TextureCoord4)
102 set_active_eval2( tnl, VERT_ATTRIB_TEX0, 4, &ctx->EvalMap.Map2Texture4 );
103 else if (ctx->Eval.Map2TextureCoord3)
104 set_active_eval2( tnl, VERT_ATTRIB_TEX0, 3, &ctx->EvalMap.Map2Texture3 );
105 else if (ctx->Eval.Map2TextureCoord2)
106 set_active_eval2( tnl, VERT_ATTRIB_TEX0, 2, &ctx->EvalMap.Map2Texture2 );
107 else if (ctx->Eval.Map2TextureCoord1)
108 set_active_eval2( tnl, VERT_ATTRIB_TEX0, 1, &ctx->EvalMap.Map2Texture1 );
109
110 if (ctx->Eval.Map1Normal)
111 set_active_eval1( tnl, VERT_ATTRIB_NORMAL, 3, &ctx->EvalMap.Map1Normal );
112
113 if (ctx->Eval.Map2Normal)
114 set_active_eval2( tnl, VERT_ATTRIB_NORMAL, 3, &ctx->EvalMap.Map2Normal );
115
116 if (ctx->Eval.Map1Vertex4)
117 set_active_eval1( tnl, VERT_ATTRIB_POS, 4, &ctx->EvalMap.Map1Vertex4 );
118 else if (ctx->Eval.Map1Vertex3)
119 set_active_eval1( tnl, VERT_ATTRIB_POS, 3, &ctx->EvalMap.Map1Vertex3 );
120
121 if (ctx->Eval.Map2Vertex4)
122 set_active_eval2( tnl, VERT_ATTRIB_POS, 4, &ctx->EvalMap.Map2Vertex4 );
123 else if (ctx->Eval.Map2Vertex3)
124 set_active_eval2( tnl, VERT_ATTRIB_POS, 3, &ctx->EvalMap.Map2Vertex3 );
125
126 tnl->vtx.eval.new_state = 0;
127 }
128
129
130
131 void _tnl_do_EvalCoord1f(GLcontext* ctx, GLfloat u)
132 {
133 TNLcontext *tnl = TNL_CONTEXT(ctx);
134 GLuint attr;
135
136 for (attr = 1; attr <= _TNL_ATTRIB_INDEX; attr++) {
137 struct gl_1d_map *map = tnl->vtx.eval.map1[attr].map;
138 if (map) {
139 GLfloat uu = (u - map->u1) * map->du;
140 GLfloat data[4];
141
142 ASSIGN_4V(data, 0, 0, 0, 1);
143
144 _math_horner_bezier_curve(map->Points, data, uu,
145 tnl->vtx.eval.map1[attr].sz,
146 map->Order);
147
148 COPY_SZ_4V( tnl->vtx.attrptr[attr],
149 tnl->vtx.attrsz[attr],
150 data );
151 }
152 }
153
154 /** Vertex -- EvalCoord1f is a noop if this map not enabled:
155 **/
156 if (tnl->vtx.eval.map1[0].map) {
157 struct gl_1d_map *map = tnl->vtx.eval.map1[0].map;
158 GLfloat uu = (u - map->u1) * map->du;
159 GLfloat vertex[4];
160
161 ASSIGN_4V(vertex, 0, 0, 0, 1);
162
163 _math_horner_bezier_curve(map->Points, vertex, uu,
164 tnl->vtx.eval.map1[0].sz,
165 map->Order);
166
167 if (tnl->vtx.eval.map1[0].sz == 4)
168 glVertex4fv( vertex );
169 else
170 glVertex3fv( vertex );
171 }
172 }
173
174
175
176 void _tnl_do_EvalCoord2f( GLcontext* ctx, GLfloat u, GLfloat v )
177 {
178 TNLcontext *tnl = TNL_CONTEXT(ctx);
179 GLuint attr;
180
181 for (attr = 1; attr <= _TNL_ATTRIB_INDEX; attr++) {
182 struct gl_2d_map *map = tnl->vtx.eval.map2[attr].map;
183 if (map) {
184 GLfloat uu = (u - map->u1) * map->du;
185 GLfloat vv = (v - map->v1) * map->dv;
186 GLfloat data[4];
187
188 ASSIGN_4V(data, 0, 0, 0, 1);
189
190 _math_horner_bezier_surf(map->Points,
191 data,
192 uu, vv,
193 tnl->vtx.eval.map2[attr].sz,
194 map->Uorder, map->Vorder);
195
196 COPY_SZ_4V( tnl->vtx.attrptr[attr],
197 tnl->vtx.attrsz[attr],
198 data );
199 }
200 }
201
202 /** Vertex -- EvalCoord2f is a noop if this map not enabled:
203 **/
204 if (tnl->vtx.eval.map2[0].map) {
205 struct gl_2d_map *map = tnl->vtx.eval.map2[0].map;
206 GLfloat uu = (u - map->u1) * map->du;
207 GLfloat vv = (v - map->v1) * map->dv;
208 GLfloat vertex[4];
209
210 ASSIGN_4V(vertex, 0, 0, 0, 1);
211
212 if (ctx->Eval.AutoNormal) {
213 GLfloat normal[4];
214 GLfloat du[4], dv[4];
215
216 _math_de_casteljau_surf(map->Points, vertex, du, dv, uu, vv,
217 tnl->vtx.eval.map2[0].sz,
218 map->Uorder, map->Vorder);
219
220 if (tnl->vtx.eval.map2[0].sz == 4) {
221 du[0] = du[0]*vertex[3] - du[3]*vertex[0];
222 du[1] = du[1]*vertex[3] - du[3]*vertex[1];
223 du[2] = du[2]*vertex[3] - du[3]*vertex[2];
224
225 dv[0] = dv[0]*vertex[3] - dv[3]*vertex[0];
226 dv[1] = dv[1]*vertex[3] - dv[3]*vertex[1];
227 dv[2] = dv[2]*vertex[3] - dv[3]*vertex[2];
228 }
229
230
231 CROSS3(normal, du, dv);
232 NORMALIZE_3FV(normal);
233 normal[3] = 1.0;
234
235 COPY_SZ_4V( tnl->vtx.attrptr[_TNL_ATTRIB_NORMAL],
236 tnl->vtx.attrsz[_TNL_ATTRIB_NORMAL],
237 normal );
238
239 }
240 else {
241 _math_horner_bezier_surf(map->Points, vertex, uu, vv,
242 tnl->vtx.eval.map2[0].sz,
243 map->Uorder, map->Vorder);
244 }
245
246 if (tnl->vtx.attrsz[0] == 4)
247 glVertex4fv( vertex );
248 else
249 glVertex3fv( vertex );
250 }
251 }
252
253