-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtetrix.js
163 lines (123 loc) · 3.71 KB
/
tetrix.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
154
155
156
157
158
159
160
161
162
163
var points = [];
var colors = [];
var gl;
var canvas;
var xAxis = 2;
var yAxis = 1;
var zAxis = 0;
var axis = 0;
var theta = [0, 0, 0];
var thetaLoc;
var NumTimesToSubdivide = 0;
function init()
{
canvas = document.getElementById( "gl-canvas" );
gl = WebGLUtils.setupWebGL( canvas );
if ( !gl ) { alert( "WebGL isn't available" ); }
//
// Initialize our data for the Sierpinski Gasket
//
// First, initialize the corners of our tetrahedron
var vertices = [
vec3( 0.0000, 0.0000, -1.0000 ),
vec3( 0.0000, 0.9428, 0.3333 ),
vec3( -0.8165, -0.4714, 0.3333 ),
vec3( 0.8165, -0.4714, 0.3333 )
];
document.getElementById("slider").onchange = function() {
NumTimesToSubdivide = event.srcElement.value;
};
divideTetra( vertices[0], vertices[1], vertices[2], vertices[3],
NumTimesToSubdivide);
//
// Configure WebGL
//
gl.viewport( 0, 0, canvas.width, canvas.height );
gl.clearColor( 1.0, 1.0, 1.0, 1.0 );
//enable depth
gl.enable(gl.DEPTH_TEST);
// Load shaders and initialize attribute buffers
var program = initShaders( gl, "vertex-shader", "fragment-shader" );
gl.useProgram( program );
// create color buffer
var cBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(colors), gl.STATIC_DRAW);
// associate shader variables
var vColor = gl.getAttribLocation(program, "vColor");
gl.vertexAttribPointer(vColor, 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(vColor);
// Load the data into the GPU
var vBuffer = gl.createBuffer();
gl.bindBuffer( gl.ARRAY_BUFFER, vBuffer );
gl.bufferData( gl.ARRAY_BUFFER, flatten(points), gl.STATIC_DRAW );
// Associate shader variables with our data buffer
var vPosition = gl.getAttribLocation( program, "vPosition" );
gl.vertexAttribPointer( vPosition, 3, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray( vPosition);
thetaLoc = gl.getUniformLocation(program, "theta");
document.getElementById( "xButton").onclick = function (){
axis = xAxis;
};
document.getElementById( "yButton").onclick = function(){
axis = yAxis;
};
document.getElementById( "zButton").onclick = function(){
axis = zAxis;
};
render();
};
function triangle( a, b, c, color)
{
var tetraColors = [
vec3(1.0, 0.0, 0.0),
vec3(0.0, 0.0, 1.0),
vec3(1.0, 0.0, 0.0),
vec3(0.0, 0.0, 1.0)
];
colors.push(tetraColors[color]);
points.push(a);
colors.push(tetraColors[color]);
points.push(b);
colors.push(tetraColors[color]);
points.push(c);
}
function tetra (a, b, c, d)
{
triangle( a, c, b, 0);
triangle( a, c, d, 1);
triangle( a, b, d, 2);
triangle( b, c, d, 3);
}
function divideTetra( a, b, c, d, count )
{
// check for end of recursion
if ( count === 0 ) {
tetra( a, b, c, d);
}
else {
//bisect the sides
var ab = mix( a, b, 0.5 );
var ac = mix( a, c, 0.5);
var ad = mix( a, d, 0.5 );
var bc = mix( b, c, 0.5 );
var bd = mix( b, d, 0.5 );
var cd = mix( c, d, 0.5 );
--count;
// three new triangles
divideTetra( a, ab, ac, ad, count );
divideTetra( ab, b, bc, bd, count );
divideTetra( ac, bc, c, cd, count );
divideTetra( ad, bd, cd, d, count );
}
}
window.onload = init;
function render()
{
gl.clear( gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
theta[axis] += 2.0;
gl.uniform3fv(thetaLoc, theta);
gl.drawArrays( gl.TRIANGLES, 0, points.length );
points = [];
requestAnimFrame(init);
}