summaryrefslogtreecommitdiff
path: root/src/mesa/tnl/t_vtx_eval.c
blob: d948e700b0d3a1e4f4ef178bebe9ab7ad0ff9232 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
 * Mesa 3-D graphics library
 * Version:  6.1
 *
 * Copyright (C) 1999-2004  Brian Paul   All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 *    Keith Whitwell <keith@tungstengraphics.com>
 */

#include "glheader.h"
#include "api_eval.h"
#include "context.h"
#include "macros.h"
#include "math/m_eval.h"
#include "t_vtx_api.h"
#include "dispatch.h"


static void clear_active_eval1( TNLcontext *tnl, GLuint attr ) 
{
   tnl->vtx.eval.map1[attr].map = NULL;
}

static void clear_active_eval2( TNLcontext *tnl, GLuint attr ) 
{
   tnl->vtx.eval.map2[attr].map = NULL;
}

static void set_active_eval1( TNLcontext *tnl, GLuint attr, GLuint dim, 
			      struct gl_1d_map *map )
{
   if (!tnl->vtx.eval.map1[attr].map) {
      tnl->vtx.eval.map1[attr].map = map;
      tnl->vtx.eval.map1[attr].sz = dim;
   }
} 

static void set_active_eval2( TNLcontext *tnl, GLuint attr, GLuint dim, 
			      struct gl_2d_map *map )
{
   if (!tnl->vtx.eval.map2[attr].map) {
      tnl->vtx.eval.map2[attr].map = map;
      tnl->vtx.eval.map2[attr].sz = dim;
   }
} 

void _tnl_update_eval( GLcontext *ctx )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   GLuint attr;

   /* Vertex program maps have priority over conventional attribs */

   for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
      clear_active_eval1( tnl, attr );
      clear_active_eval2( tnl, attr );
   }

   if (ctx->VertexProgram._Enabled) {
      for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
	 if (ctx->Eval.Map1Attrib[attr]) 
	    set_active_eval1( tnl, attr, 4, &ctx->EvalMap.Map1Attrib[attr] );

	 if (ctx->Eval.Map2Attrib[attr]) 
	    set_active_eval2( tnl, attr, 4, &ctx->EvalMap.Map2Attrib[attr] );
      }
   }

   if (ctx->Eval.Map1Color4) 
      set_active_eval1( tnl, VERT_ATTRIB_COLOR0, 4, &ctx->EvalMap.Map1Color4 );
      
   if (ctx->Eval.Map2Color4) 
      set_active_eval2( tnl, VERT_ATTRIB_COLOR0, 4, &ctx->EvalMap.Map2Color4 );

   if (ctx->Eval.Map1TextureCoord4) 
      set_active_eval1( tnl, VERT_ATTRIB_TEX0, 4, &ctx->EvalMap.Map1Texture4 );
   else if (ctx->Eval.Map1TextureCoord3) 
      set_active_eval1( tnl, VERT_ATTRIB_TEX0, 3, &ctx->EvalMap.Map1Texture3 );
   else if (ctx->Eval.Map1TextureCoord2) 
      set_active_eval1( tnl, VERT_ATTRIB_TEX0, 2, &ctx->EvalMap.Map1Texture2 );
   else if (ctx->Eval.Map1TextureCoord1) 
      set_active_eval1( tnl, VERT_ATTRIB_TEX0, 1, &ctx->EvalMap.Map1Texture1 );

   if (ctx->Eval.Map2TextureCoord4) 
      set_active_eval2( tnl, VERT_ATTRIB_TEX0, 4, &ctx->EvalMap.Map2Texture4 );
   else if (ctx->Eval.Map2TextureCoord3) 
      set_active_eval2( tnl, VERT_ATTRIB_TEX0, 3, &ctx->EvalMap.Map2Texture3 );
   else if (ctx->Eval.Map2TextureCoord2) 
      set_active_eval2( tnl, VERT_ATTRIB_TEX0, 2, &ctx->EvalMap.Map2Texture2 );
   else if (ctx->Eval.Map2TextureCoord1) 
      set_active_eval2( tnl, VERT_ATTRIB_TEX0, 1, &ctx->EvalMap.Map2Texture1 );

   if (ctx->Eval.Map1Normal) 
      set_active_eval1( tnl, VERT_ATTRIB_NORMAL, 3, &ctx->EvalMap.Map1Normal );

   if (ctx->Eval.Map2Normal) 
      set_active_eval2( tnl, VERT_ATTRIB_NORMAL, 3, &ctx->EvalMap.Map2Normal );

   if (ctx->Eval.Map1Vertex4) 
      set_active_eval1( tnl, VERT_ATTRIB_POS, 4, &ctx->EvalMap.Map1Vertex4 );
   else if (ctx->Eval.Map1Vertex3) 
      set_active_eval1( tnl, VERT_ATTRIB_POS, 3, &ctx->EvalMap.Map1Vertex3 );

   if (ctx->Eval.Map2Vertex4) 
      set_active_eval2( tnl, VERT_ATTRIB_POS, 4, &ctx->EvalMap.Map2Vertex4 );
   else if (ctx->Eval.Map2Vertex3) 
      set_active_eval2( tnl, VERT_ATTRIB_POS, 3, &ctx->EvalMap.Map2Vertex3 );

   tnl->vtx.eval.new_state = 0;
}



void _tnl_do_EvalCoord1f(GLcontext* ctx, GLfloat u)
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   GLuint attr;

   for (attr = 1; attr <= _TNL_ATTRIB_INDEX; attr++) {
      struct gl_1d_map *map = tnl->vtx.eval.map1[attr].map;
      if (map) {
	 GLfloat uu = (u - map->u1) * map->du;
	 GLfloat data[4];

	 ASSIGN_4V(data, 0, 0, 0, 1);

	 _math_horner_bezier_curve(map->Points, data, uu, 
				   tnl->vtx.eval.map1[attr].sz, 
				   map->Order);

	 COPY_SZ_4V( tnl->vtx.attrptr[attr],
		     tnl->vtx.attrsz[attr],
		     data );
      }
   }

   /** Vertex -- EvalCoord1f is a noop if this map not enabled:
    **/
   if (tnl->vtx.eval.map1[0].map) {
      struct gl_1d_map *map = tnl->vtx.eval.map1[0].map;
      GLfloat uu = (u - map->u1) * map->du;
      GLfloat vertex[4];

      ASSIGN_4V(vertex, 0, 0, 0, 1);

      _math_horner_bezier_curve(map->Points, vertex, uu, 
				tnl->vtx.eval.map1[0].sz, 
				map->Order);

      if (tnl->vtx.eval.map1[0].sz == 4) 
	 CALL_Vertex4fv(GET_DISPATCH(), ( vertex ));
      else
	 CALL_Vertex3fv(GET_DISPATCH(), ( vertex )); 
   }
}



void _tnl_do_EvalCoord2f( GLcontext* ctx, GLfloat u, GLfloat v )
{   
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   GLuint attr;

   for (attr = 1; attr <= _TNL_ATTRIB_INDEX; attr++) {
      struct gl_2d_map *map = tnl->vtx.eval.map2[attr].map;
      if (map) {
	 GLfloat uu = (u - map->u1) * map->du;
	 GLfloat vv = (v - map->v1) * map->dv;
	 GLfloat data[4];

	 ASSIGN_4V(data, 0, 0, 0, 1);

	 _math_horner_bezier_surf(map->Points, 
				  data, 
				  uu, vv, 
				  tnl->vtx.eval.map2[attr].sz, 
				  map->Uorder, map->Vorder);

	 COPY_SZ_4V( tnl->vtx.attrptr[attr],
		     tnl->vtx.attrsz[attr],
		     data );
      }
   }

   /** Vertex -- EvalCoord2f is a noop if this map not enabled:
    **/
   if (tnl->vtx.eval.map2[0].map) {
      struct gl_2d_map *map = tnl->vtx.eval.map2[0].map;
      GLfloat uu = (u - map->u1) * map->du;
      GLfloat vv = (v - map->v1) * map->dv;
      GLfloat vertex[4];

      ASSIGN_4V(vertex, 0, 0, 0, 1);

      if (ctx->Eval.AutoNormal) {
	 GLfloat normal[4];
         GLfloat du[4], dv[4];

         _math_de_casteljau_surf(map->Points, vertex, du, dv, uu, vv, 
				 tnl->vtx.eval.map2[0].sz,
				 map->Uorder, map->Vorder);

	 if (tnl->vtx.eval.map2[0].sz == 4) {
	    du[0] = du[0]*vertex[3] - du[3]*vertex[0];
	    du[1] = du[1]*vertex[3] - du[3]*vertex[1];
	    du[2] = du[2]*vertex[3] - du[3]*vertex[2];
	 
	    dv[0] = dv[0]*vertex[3] - dv[3]*vertex[0];
	    dv[1] = dv[1]*vertex[3] - dv[3]*vertex[1];
	    dv[2] = dv[2]*vertex[3] - dv[3]*vertex[2];
	 }


         CROSS3(normal, du, dv);
         NORMALIZE_3FV(normal);
	 normal[3] = 1.0;

	 COPY_SZ_4V( tnl->vtx.attrptr[_TNL_ATTRIB_NORMAL],
		     tnl->vtx.attrsz[_TNL_ATTRIB_NORMAL],
		     normal );

      }
      else {
         _math_horner_bezier_surf(map->Points, vertex, uu, vv, 
				  tnl->vtx.eval.map2[0].sz,
				  map->Uorder, map->Vorder);
      }

      if (tnl->vtx.attrsz[0] == 4) 
	 CALL_Vertex4fv(GET_DISPATCH(), ( vertex ));
      else
	 CALL_Vertex3fv(GET_DISPATCH(), ( vertex )); 
   }
}