-
Notifications
You must be signed in to change notification settings - Fork 0
/
18beziersurface.html
133 lines (122 loc) · 3.6 KB
/
18beziersurface.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Bezier Surface</title>
<script type="text/javascript" src="utils.js"></script>
<script type="text/javascript" src="15perspectiveprojection.js"></script>
</head>
<body>
<canvas id="container" width="800" height="800"></canvas>
<script>
var NI = 5, NJ = 4, RESOLUTIONI = 10 * NI, RESOLUTIONJ = 10 * NJ;
var inp = [], outp = [];
var context;
draw();
// http://paulbourke.net/geometry/bezier/
function draw() {
var canvas = document.getElementById("container");
context = canvas.getContext("2d");
context.translate(400, 400);
var i, j, ki, kj, mui, muj, bi, bj;
// Create a random surface
for (i = 0; i <= NI; i++) {
for (j = 0; j <= NJ; j++) {
if (inp[i] == undefined)
inp[i] = [];
if (inp[i][j] == undefined)
inp[i][j] = {};
inp[i][j].x = i * 100;
inp[i][j].y = j * 100;
inp[i][j].z = (Math.random() % 10000) / 5000.0 - 1;
}
}
for (i = 0; i < RESOLUTIONI; i++) {
mui = i / (RESOLUTIONI - 1);
for (j = 0; j < RESOLUTIONJ; j++) {
muj = j / (RESOLUTIONJ - 1);
if (outp[i] == undefined)
outp[i] = [];
if (outp[i][j] == undefined)
outp[i][j] = {};
outp[i][j].x = 0;
outp[i][j].y = 0;
outp[i][j].z = 0;
for (ki = 0; ki <= NI; ki++) {
bi = bezierBlend(ki, mui, NI);
for (kj = 0; kj <= NJ; kj++) {
bj = bezierBlend(kj, muj, NJ);
outp[i][j].x += (inp[ki][kj].x * bi * bj);
outp[i][j].y += (inp[ki][kj].y * bi * bj);
outp[i][j].z += (inp[ki][kj].z * bi * bj);
}
}
}
}
// Display the surface
var displaySurface = [];
var indexData = [];
for (i = 0; i < RESOLUTIONI - 1; i++) {
for (j = 0; j < RESOLUTIONJ - 1; j++) {
displaySurface.push([outp[i][j].x, outp[i][j].y, outp[i][j].z, 1],
[outp[i][j+1].x, outp[i][j+1].y, outp[i][j+1].z, 1],
[outp[i+1][j+1].x, outp[i+1][j+1].y, outp[i+1][j+1].z, 1],
[outp[i+1][j].x, outp[i+1][j].y, outp[i+1][j].z, 1]
);
indexData.push(indexData.length);
}
}
var projectionMatrix = projectionStrategy["isometric"]["get"]();
var result = [];
for (i = 0; i < displaySurface.length; i++) {
var p = vector3DDotProductMatrix3D(displaySurface[i], projectionMatrix);
result.push(p);
}
drawVertex(indexData, result, "red");
// Control point polygon
var controlPolygon = [];
indexData = [];
for (i = 0; i < NI; i++) {
for (j = 0; j < NJ; j++) {
controlPolygon.push([inp[i][j].x, inp[i][j].y, inp[i][j].z, 1],
[inp[i][j+1].x, inp[i][j+1].y, inp[i][j+1].z, 1],
[inp[i+1][j+1].x, inp[i+1][j+1].y, inp[i+1][j+1].z, 1],
[inp[i+1][j].x, inp[i+1][j].y, inp[i+1][j].z, 1]
);
indexData.push(indexData.length);
}
}
result = [];
for (i = 0; i < controlPolygon.length; i++) {
p = vector3DDotProductMatrix3D(controlPolygon[i], projectionMatrix);
result.push(p);
}
drawVertex(indexData, result, "green");
}
function bezierBlend(k, mu, n) {
var nn, kn, nkn;
var blend = 1;
nn = n;
kn = k;
nkn = n - k;
while (nn >= 1) {
blend *= nn;
nn--;
if (kn > 1) {
blend /= kn;
kn--;
}
if (nkn > 1) {
blend /= nkn;
nkn--;
}
}
if (k > 0)
blend *= Math.pow(mu, k);
if (n - k > 0)
blend *= Math.pow(1 - mu, n - k);
return blend;
}
</script>
</body>
</html>