1/*
2** License Applicability. Except to the extent portions of this file are
3** made subject to an alternative license as permitted in the SGI Free
4** Software License B, Version 1.1 (the "License"), the contents of this
5** file are subject only to the provisions of the License. You may not use
6** this file except in compliance with the License. You may obtain a copy
7** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
8** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
9**
10** http://oss.sgi.com/projects/FreeB
11**
12** Note that, as provided in the License, the Software is distributed on an
13** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
14** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
15** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
16** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
17**
18** Original Code. The Original Code is: OpenGL Sample Implementation,
19** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
20** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
21** Copyright in any portions created by third parties is as indicated
22** elsewhere herein. All Rights Reserved.
23**
24** Additional Notice Provisions: The application programming interfaces
25** established by SGI in conjunction with the Original Code are The
26** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
27** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
28** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
29** Window System(R) (Version 1.3), released October 19, 1998. This software
30** was created using the OpenGL(R) version 1.2.1 Sample Implementation
31** published by SGI, but has not been independently verified as being
32** compliant with the OpenGL(R) version 1.2.1 Specification.
33**
34*/
35/*
36*/
37
38#include <stdlib.h>
39#include <stdio.h>
40
41#include "glcurveval.h"
42
43
44/*
45 *compute the Bezier polynomials C[n,j](v) for all j at v with
46 *return values stored in coeff[], where
47 *  C[n,j](v) = (n,j) * v^j * (1-v)^(n-j),
48 *  j=0,1,2,...,n.
49 *order : n+1
50 *vprime: v
51 *coeff : coeff[j]=C[n,j](v), this array store the returned values.
52 *The algorithm is a recursive scheme:
53 *   C[0,0]=1;
54 *   C[n,j](v) = (1-v)*C[n-1,j](v) + v*C[n-1,j-1](v), n>=1
55 *This code is copied from opengl/soft/so_eval.c:PreEvaluate
56 */
57void OpenGLCurveEvaluator::inPreEvaluate(int order, REAL vprime, REAL *coeff)
58{
59  int i, j;
60  REAL oldval, temp;
61  REAL oneMinusvprime;
62
63  /*
64   * Minor optimization
65   * Compute orders 1 and 2 outright, and set coeff[0], coeff[1] to
66     * their i==1 loop values to avoid the initialization and the i==1 loop.
67     */
68  if (order == 1) {
69    coeff[0] = 1.0;
70    return;
71  }
72
73  oneMinusvprime = 1-vprime;
74  coeff[0] = oneMinusvprime;
75  coeff[1] = vprime;
76  if (order == 2) return;
77
78  for (i = 2; i < order; i++) {
79    oldval = coeff[0] * vprime;
80    coeff[0] = oneMinusvprime * coeff[0];
81    for (j = 1; j < i; j++) {
82      temp = oldval;
83      oldval = coeff[j] * vprime;
84	    coeff[j] = temp + oneMinusvprime * coeff[j];
85    }
86    coeff[j] = oldval;
87  }
88}
89
90void OpenGLCurveEvaluator::inMap1f(int which, //0: vert, 1: norm, 2: color, 3: tex
91				   int k, //dimension
92				   REAL ulower,
93				   REAL uupper,
94				   int ustride,
95				   int uorder,
96				   REAL *ctlpoints)
97{
98  int i,x;
99  curveEvalMachine *temp_em;
100  switch(which){
101  case 0: //vertex
102    vertex_flag = 1;
103    temp_em = &em_vertex;
104    break;
105  case 1: //normal
106    normal_flag = 1;
107    temp_em = &em_normal;
108    break;
109  case 2: //color
110    color_flag = 1;
111    temp_em = &em_color;
112    break;
113  default:
114    texcoord_flag = 1;
115    temp_em = &em_texcoord;
116    break;
117  }
118
119  REAL *data = temp_em->ctlpoints;
120  temp_em->uprime = -1; //initialized
121  temp_em->k = k;
122  temp_em->u1 = ulower;
123  temp_em->u2 = uupper;
124  temp_em->ustride = ustride;
125  temp_em->uorder = uorder;
126  /*copy the control points*/
127  for(i=0; i<uorder; i++){
128    for(x=0; x<k; x++){
129      data[x] = ctlpoints[x];
130    }
131    ctlpoints += ustride;
132    data += k;
133  }
134}
135
136void OpenGLCurveEvaluator::inDoDomain1(curveEvalMachine *em, REAL u, REAL *retPoint)
137{
138  int j, row;
139  REAL the_uprime;
140  REAL *data;
141
142  if(em->u2 == em->u1)
143    return;
144  the_uprime = (u-em->u1) / (em->u2-em->u1);
145  /*use already cached values if possible*/
146  if(em->uprime != the_uprime){
147    inPreEvaluate(em->uorder, the_uprime, em->ucoeff);
148    em->uprime = the_uprime;
149  }
150
151  for(j=0; j<em->k; j++){
152    data = em->ctlpoints+j;
153    retPoint[j] = 0.0;
154    for(row=0; row<em->uorder; row++)
155      {
156	retPoint[j] += em->ucoeff[row] * (*data);
157	data += em->k;
158      }
159  }
160}
161
162void  OpenGLCurveEvaluator::inDoEvalCoord1(REAL u)
163{
164  REAL temp_vertex[4];
165  REAL temp_normal[3];
166  REAL temp_color[4];
167  REAL temp_texcoord[4];
168  if(texcoord_flag) //there is a texture map
169    {
170      inDoDomain1(&em_texcoord, u, temp_texcoord);
171      texcoordCallBack(temp_texcoord, userData);
172    }
173#ifdef DEBUG
174printf("color_flag = %i\n", color_flag);
175#endif
176  if(color_flag) //there is a color map
177    {
178      inDoDomain1(&em_color, u, temp_color);
179      colorCallBack(temp_color, userData);
180    }
181  if(normal_flag) //there is a normal map
182    {
183      inDoDomain1(&em_normal, u, temp_normal);
184      normalCallBack(temp_normal, userData);
185    }
186  if(vertex_flag)
187    {
188      inDoDomain1(&em_vertex, u, temp_vertex);
189      vertexCallBack(temp_vertex, userData);
190    }
191}
192
193void OpenGLCurveEvaluator::inMapMesh1f(int umin, int umax)
194{
195  REAL du, u;
196  int i;
197  if(global_grid_nu == 0)
198    return; //no points to output
199  du = (global_grid_u1 - global_grid_u0) / (REAL) global_grid_nu;
200  bgnline();
201  for(i=umin; i<= umax; i++){
202    u = (i==global_grid_nu)? global_grid_u1: global_grid_u0 + i*du;
203    inDoEvalCoord1(u);
204  }
205  endline();
206}
207