-
Notifications
You must be signed in to change notification settings - Fork 0
/
cube.js
executable file
·153 lines (122 loc) · 3.66 KB
/
cube.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
///// CUBE DEFINTION
/////
///// Cube is defined to be centered at the origin of the coordinate reference system.
///// Cube size is assumed to be 2.0 x 2.0 x 2.0 .
function Cube () {
this.name = "cube";
// vertices definition
////////////////////////////////////////////////////////////
this.vertices = new Float32Array([
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
-1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
-1.0, 1.0, -1.0,
1.0, 1.0, -1.0
]);
// triangles definition
////////////////////////////////////////////////////////////
this.triangleIndices = new Uint16Array([
0, 1, 2, 2, 1, 3, // front
5, 4, 7, 7, 4, 6, // back
4, 0, 6, 6, 0, 2, // left
1, 5, 3, 3, 5, 7, // right
2, 3, 6, 6, 3, 7, // top
4, 5, 0, 0, 5, 1 // bottom
]);
this.numVertices = this.vertices.length/3;
this.numTriangles = this.triangleIndices.length/3;
}
function CheckerboardSurface (w, h) {
this.name = "surface";
// vertices definition
////////////////////////////////////////////////////////////
this.vertices = new Float32Array([
0.0, 0.0, 0.0,
0.0, 0.0, 1.0,
1.0, 0.0, 0.0,
1.0, 0.0, 1.0,
]);
this.textureCoordinates = new Float32Array([
0, 0, 0, h, w, 0, w, h
]);
// triangles definition
////////////////////////////////////////////////////////////
this.triangleIndices = new Uint16Array([
0, 1, 2, 2, 1, 3, // front
]);
this.numVertices = this.vertices.length/3;
this.numTriangles = this.triangleIndices.length/3;
}
function Cell () {
this.name = "cube";
// vertices definition
////////////////////////////////////////////////////////////
let s = 0.4;
let h = 0.3;
let hh = 0.2;
let w = 0.5;
this.vertices = new Float32Array([
-w, -0, w,//0
w, -0, w,//1
-w, hh, w,//2
w, hh, w,//3
-w, 0, -w,//4
w, 0, -w,//5
-w, hh, -w,//6
w, hh, -w,//7
-s, h, s,//8--2--0
s, h, s,//9--3--1
-s, h, -s,//10--6--4
s, h, -s//11--7--5
]);
// triangles definition
////////////////////////////////////////////////////////////
this.triangleIndices = new Uint16Array([
9, 8, 2, 9, 2, 3, // front
10, 11, 7, 6, 10, 7, // back
10, 6, 8, 8, 6, 2, // left
3, 11, 9, 7, 11, 3, // right
2, 0, 1, 2, 1, 3, // front
4, 5, 7, 4, 7, 6, // back
4, 0, 6, 6, 0, 2, // left
1, 5, 3, 3, 5, 7, // right
8, 9, 10 , 10, 9, 11,
4, 5, 0, 0, 5, 1 // bottom
]);
this.numVertices = this.vertices.length/3;
this.numTriangles = this.triangleIndices.length/3;
}
function Flag () {
this.name = "flag";
// vertices definition
////////////////////////////////////////////////////////////
var p = 0;
var vertices = [0, 0, 0, 0, 1, 0];
var faces = []
while(p<1){
let i = vertices.length/3;
var z = Math.sin(p*8)*0.1
vertices = vertices.concat(
p, p/2, z, p, 1.0-p/2, z)
faces = faces.concat(i-2, i, i-1, i, i+1, i-1)
p+= 0.07
}
this.vertices = new Float32Array(vertices);
this.updateFlag = function(t, gl){
for (var i = 0; i<this.vertices.length; i+=3){
x = this.vertices[i];
this.vertices[i+2] = Math.sin(x*8)*0.05*Math.sin(2*t)+0.2*Math.sin(t)*x
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, this.vertices, gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
}
}
// triangles definition
////////////////////////////////////////////////////////////
this.triangleIndices = new Uint16Array(faces);
this.numVertices = this.vertices.length/3;
this.numTriangles = this.triangleIndices.length/3;
}