-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcylinder.js
executable file
·92 lines (72 loc) · 2.75 KB
/
cylinder.js
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
///// CYLINDER DEFINITION
/////
///// Resolution is the number of faces used to tesselate the cylinder.
///// Cylinder is defined to be centered at the origin of the coordinate axis, and lying on the XZ plane.
///// Cylinder height is assumed to be 2.0. Cylinder radius is assumed to be 1.0 .
function Cylinder (resolution) {
this.name = "cylinder";
// vertices definition
////////////////////////////////////////////////////////////
this.vertices = new Float32Array(3*(2*resolution+2));
var radius = 1.0;
var angle;
var step = 6.283185307179586476925286766559 / resolution;
// lower circle
var vertexoffset = 0;
for (var i = 0; i < resolution; i++) {
angle = step * i;
this.vertices[vertexoffset] = radius * Math.cos(angle);
this.vertices[vertexoffset+1] = 0.0;
this.vertices[vertexoffset+2] = radius * Math.sin(angle);
vertexoffset += 3;
}
// upper circle
for (var i = 0; i < resolution; i++) {
angle = step * i;
this.vertices[vertexoffset] = radius * Math.cos(angle);
this.vertices[vertexoffset+1] = 2.0;
this.vertices[vertexoffset+2] = radius * Math.sin(angle);
vertexoffset += 3;
}
this.vertices[vertexoffset] = 0.0;
this.vertices[vertexoffset+1] = 0.0;
this.vertices[vertexoffset+2] = 0.0;
vertexoffset += 3;
this.vertices[vertexoffset] = 0.0;
this.vertices[vertexoffset+1] = 2.0;
this.vertices[vertexoffset+2] = 0.0;
// triangles definition
////////////////////////////////////////////////////////////
this.triangleIndices = new Uint16Array(3*4*resolution);
// lateral surface
var triangleoffset = 0;
for (var i = 0; i < resolution; i++)
{
this.triangleIndices[triangleoffset] = i;
this.triangleIndices[triangleoffset+1] = (i+1) % resolution;
this.triangleIndices[triangleoffset+2] = (i % resolution) + resolution;
triangleoffset += 3;
this.triangleIndices[triangleoffset] = (i % resolution) + resolution;
this.triangleIndices[triangleoffset+1] = (i+1) % resolution;
this.triangleIndices[triangleoffset+2] = ((i+1) % resolution) + resolution;
triangleoffset += 3;
}
// bottom of the cylinder
for (var i = 0; i < resolution; i++)
{
this.triangleIndices[triangleoffset] = i;
this.triangleIndices[triangleoffset+1] = (i+1) % resolution;
this.triangleIndices[triangleoffset+2] = 2*resolution;
triangleoffset += 3;
}
// top of the cylinder
for (var i = 0; i < resolution; i++)
{
this.triangleIndices[triangleoffset] = resolution + i;
this.triangleIndices[triangleoffset+1] = ((i+1) % resolution) + resolution;
this.triangleIndices[triangleoffset+2] = 2*resolution+1;
triangleoffset += 3;
}
this.numVertices = this.vertices.length/3;
this.numTriangles = this.triangleIndices.length/3;
}