]> icculus.org git repositories - divverent/darkplaces.git/blob - curves.c
made an optimized SV_ClipMoveToWorld function to save time on large numbers of world...
[divverent/darkplaces.git] / curves.c
1
2 /*
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
5
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.
10
11 example equations for 1-5 control point bsplines being sampled as t=0...1
12 1: flat (0th dimension)
13 o = a
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
22
23 arbitrary dimension bspline
24 double factorial(int n)
25 {
26         int i;
27         double f;
28         f = 1;
29         for (i = 1;i < n;i++)
30                 f = f * i;
31         return f;
32 }
33 double bsplinesample(int dimensions, double t, double *param)
34 {
35         double o = 0;
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);
38 }
39 */
40
41 #include <math.h>
42 #include "curves.h"
43
44 // usage:
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)
48 {
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)
55         {
56                 for (l = 0;l < patchwidth-1;l += 2)
57                 {
58                         // set up control point pointers for quicker lookup later
59                         for (y = 0;y < 3;y++)
60                                 for (x = 0;x < 3;x++)
61                                         cp[y][x] = (float *)((unsigned char *)patchvertices + ((k+y)*patchwidth+(l+x)) * inputstride);
62                         // for each row...
63                         for (y = 0;y <= tesselationheight*2;y++)
64                         {
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));
71                                 c = ((       py) * (       py));
72                                 for (component = 0;component < numcomponents;component++)
73                                 {
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;
77                                 }
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++)
82                                 {
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));
89                                         c = ((       px) * (       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);
96                                 }
97                         }
98                 }
99         }
100 #if 0
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++)
104         {
105                 for (x = 0;x < (patchwidth-1)*tesselationwidth+1;x++)
106                 {
107                         printf("(");
108                         for (component = 0;component < numcomponents;component++)
109                                 printf("%f ", outputvertices[(y*((patchwidth-1)*tesselationwidth+1)+x)*numcomponents+component]);
110                         printf(") ");
111                 }
112                 printf("\n");
113         }
114         printf("}\n");
115 #endif
116 }
117
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)
120 {
121         int c, x, y;
122         const float *patch;
123         float deviation, squareddeviation, bestsquareddeviation;
124         bestsquareddeviation = 0;
125         for (y = 0;y < patchheight;y++)
126         {
127                 for (x = 0;x < patchwidth-1;x += 2)
128                 {
129                         squareddeviation = 0;
130                         for (c = 0, patch = in + ((y * patchwidth) + x) * components;c < components;c++, patch++)
131                         {
132                                 deviation = patch[components] * 0.5f - patch[0] * 0.25f - patch[2*components] * 0.25f;
133                                 squareddeviation += deviation*deviation;
134                         }
135                         if (bestsquareddeviation < squareddeviation)
136                                 bestsquareddeviation = squareddeviation;
137                 }
138         }
139         return (int)floor(log(sqrt(bestsquareddeviation) / tolerance) / log(2)) + 1;
140 }
141
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)
144 {
145         int c, x, y;
146         const float *patch;
147         float deviation, squareddeviation, bestsquareddeviation;
148         bestsquareddeviation = 0;
149         for (y = 0;y < patchheight-1;y += 2)
150         {
151                 for (x = 0;x < patchwidth;x++)
152                 {
153                         squareddeviation = 0;
154                         for (c = 0, patch = in + ((y * patchwidth) + x) * components;c < components;c++, patch++)
155                         {
156                                 deviation = patch[patchwidth*components] * 0.5f - patch[0] * 0.25f - patch[2*patchwidth*components] * 0.25f;
157                                 squareddeviation += deviation*deviation;
158                         }
159                         if (bestsquareddeviation < squareddeviation)
160                                 bestsquareddeviation = squareddeviation;
161                 }
162         }
163         return (int)floor(log(sqrt(bestsquareddeviation) / tolerance) / log(2)) + 1;
164 }
165
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)
171 {
172         int x, y, row0, row1;
173         for (y = 0;y < height - 1;y++)
174         {
175                 row0 = firstvertex + (y + 0) * width;
176                 row1 = firstvertex + (y + 1) * width;
177                 for (x = 0;x < width - 1;x++)
178                 {
179                         *elements++ = row0;
180                         *elements++ = row1;
181                         *elements++ = row0 + 1;
182                         *elements++ = row1;
183                         *elements++ = row1 + 1;
184                         *elements++ = row0 + 1;
185                         row0++;
186                         row1++;
187                 }
188         }
189 }
190