3 this code written by Forest Hale, on 2004-10-17, and placed into public domain
4 this implements Quadratic BSpline surfaces as seen in Quake3 by id Software
6 a small rant on misuse of the name 'bezier': many people seem to think that
7 bezier is a generic term for splines, but it is not, it is a term for a
8 specific type of bspline (4 control points, cubic bspline), bsplines are the
9 generalization of the bezier spline to support dimensions other than cubic.
11 example equations for 1-5 control point bsplines being sampled as t=0...1
12 1: flat (0th dimension)
14 2: linear (1st dimension)
15 o = a * (1 - t) + b * t
16 3: quadratic bspline (2nd dimension)
17 o = a * (1 - t) * (1 - t) + 2 * b * (1 - t) * t + c * t * t
18 4: cubic (bezier) bspline (3rd dimension)
19 o = a * (1 - t) * (1 - t) * (1 - t) + 3 * b * (1 - t) * (1 - t) * t + 3 * c * (1 - t) * t * t + d * t * t * t
20 5: quartic bspline (4th dimension)
21 o = a * (1 - t) * (1 - t) * (1 - t) * (1 - t) + 4 * b * (1 - t) * (1 - t) * (1 - t) * t + 6 * c * (1 - t) * (1 - t) * t * t + 4 * d * (1 - t) * t * t * t + e * t * t * t * t
23 arbitrary dimension bspline
24 double factorial(int n)
33 double bsplinesample(int dimensions, double t, double *param)
36 for (i = 0;i < dimensions + 1;i++)
37 o += param[i] * factorial(dimensions)/(factorial(i)*factorial(dimensions-i)) * pow(t, i) * pow(1 - t, dimensions - i);
45 // to expand a 5x5 patch to 21x21 vertices (4x4 tesselation), one might use this call:
46 // Q3PatchSubdivideFloat(3, sizeof(float[3]), outvertices, 5, 5, sizeof(float[3]), patchvertices, 4, 4);
47 void Q3PatchTesselateFloat(int numcomponents, int outputstride, float *outputvertices, int patchwidth, int patchheight, int inputstride, float *patchvertices, int tesselationwidth, int tesselationheight)
49 int k, l, x, y, component, outputwidth = (patchwidth-1)*tesselationwidth+1;
50 float px, py, *v, a, b, c, *cp[3][3], temp[3][64];
51 // iterate over the individual 3x3 quadratic spline surfaces one at a time
52 // expanding them to fill the output array (with some overlap to ensure
53 // the edges are filled)
54 for (k = 0;k < patchheight-1;k += 2)
56 for (l = 0;l < patchwidth-1;l += 2)
58 // set up control point pointers for quicker lookup later
61 cp[y][x] = (float *)((unsigned char *)patchvertices + ((k+y)*patchwidth+(l+x)) * inputstride);
63 for (y = 0;y <= tesselationheight*2;y++)
65 // calculate control points for this row by collapsing the 3
66 // rows of control points to one row using py
67 py = (float)y / (float)(tesselationheight*2);
68 // calculate quadratic spline weights for py
69 a = ((1.0f - py) * (1.0f - py));
70 b = ((1.0f - py) * (2.0f * py));
72 for (component = 0;component < numcomponents;component++)
74 temp[0][component] = cp[0][0][component] * a + cp[1][0][component] * b + cp[2][0][component] * c;
75 temp[1][component] = cp[0][1][component] * a + cp[1][1][component] * b + cp[2][1][component] * c;
76 temp[2][component] = cp[0][2][component] * a + cp[1][2][component] * b + cp[2][2][component] * c;
78 // fetch a pointer to the beginning of the output vertex row
79 v = (float *)((unsigned char *)outputvertices + ((k * tesselationheight + y) * outputwidth + l * tesselationwidth) * outputstride);
80 // for each column of the row...
81 for (x = 0;x <= (tesselationwidth*2);x++)
83 // calculate point based on the row control points
84 px = (float)x / (float)(tesselationwidth*2);
85 // calculate quadratic spline weights for px
86 // (could be precalculated)
87 a = ((1.0f - px) * (1.0f - px));
88 b = ((1.0f - px) * (2.0f * px));
90 for (component = 0;component < numcomponents;component++)
91 v[component] = temp[0][component] * a + temp[1][component] * b + temp[2][component] * c;
92 // advance to next output vertex using outputstride
93 // (the next vertex may not be directly following this
94 // one, as this may be part of a larger structure)
95 v = (float *)((unsigned char *)v + outputstride);
101 // enable this if you want results printed out
102 printf("vertices[%i][%i] =\n{\n", (patchheight-1)*tesselationheight+1, (patchwidth-1)*tesselationwidth+1);
103 for (y = 0;y < (patchheight-1)*tesselationheight+1;y++)
105 for (x = 0;x < (patchwidth-1)*tesselationwidth+1;x++)
108 for (component = 0;component < numcomponents;component++)
109 printf("%f ", outputvertices[(y*((patchwidth-1)*tesselationwidth+1)+x)*numcomponents+component]);
118 // returns how much tesselation of each segment is needed to remain under tolerance
119 int Q3PatchTesselationOnX(int patchwidth, int patchheight, int components, const float *in, float tolerance)
123 float deviation, squareddeviation, bestsquareddeviation;
124 bestsquareddeviation = 0;
125 for (y = 0;y < patchheight;y++)
127 for (x = 0;x < patchwidth-1;x += 2)
129 squareddeviation = 0;
130 for (c = 0, patch = in + ((y * patchwidth) + x) * components;c < components;c++, patch++)
132 deviation = patch[components] * 0.5f - patch[0] * 0.25f - patch[2*components] * 0.25f;
133 squareddeviation += deviation*deviation;
135 if (bestsquareddeviation < squareddeviation)
136 bestsquareddeviation = squareddeviation;
139 return (int)floor(log(sqrt(bestsquareddeviation) / tolerance) / log(2)) + 1;
142 // returns how much tesselation of each segment is needed to remain under tolerance
143 int Q3PatchTesselationOnY(int patchwidth, int patchheight, int components, const float *in, float tolerance)
147 float deviation, squareddeviation, bestsquareddeviation;
148 bestsquareddeviation = 0;
149 for (y = 0;y < patchheight-1;y += 2)
151 for (x = 0;x < patchwidth;x++)
153 squareddeviation = 0;
154 for (c = 0, patch = in + ((y * patchwidth) + x) * components;c < components;c++, patch++)
156 deviation = patch[patchwidth*components] * 0.5f - patch[0] * 0.25f - patch[2*patchwidth*components] * 0.25f;
157 squareddeviation += deviation*deviation;
159 if (bestsquareddeviation < squareddeviation)
160 bestsquareddeviation = squareddeviation;
163 return (int)floor(log(sqrt(bestsquareddeviation) / tolerance) / log(2)) + 1;
166 // calculates elements for a grid of vertices
167 // (such as those produced by Q3PatchTesselate)
168 // (note: width and height are the actual vertex size, this produces
169 // (width-1)*(height-1)*2 triangles, 3 elements each)
170 void Q3PatchTriangleElements(int *elements, int width, int height, int firstvertex)
172 int x, y, row0, row1;
173 for (y = 0;y < height - 1;y++)
175 row0 = firstvertex + (y + 0) * width;
176 row1 = firstvertex + (y + 1) * width;
177 for (x = 0;x < width - 1;x++)
181 *elements++ = row0 + 1;
183 *elements++ = row1 + 1;
184 *elements++ = row0 + 1;