MakePoints: function() {
var x, y, r;
var theta=0;
var dtheta = 3.1415/this.count * 2;
r = .75;
for (var i=0; i<this.count; i++) {
tmp = vec2(r*Math.cos(theta), r*Math.sin(theta));
theta += dtheta;
this.list.push(tmp);
}
return;
}
attribute vec4 vPosition;
attribute vec4 vColor;
varying vec4 f_Color;
void main() {
gl_Position = vPosition;
f_Color = vColor;
}
// a buffer for the colors this.cBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, this.cBuffer); // "vColor" is the name of the attribute. colorAttribute = gl.getAttribLocation(program, "vColor"); gl.vertexAttribPointer(colorAttribute,3,gl.FLOAT, false,0,0); gl.enableVertexAttribArray(colorAttribute);
precision mediump float;
varying vec4 f_Color;
void main() {
gl_FragColor = f_Color;
}
<script>
.
.
.
window.addEventListener("load", function(event){
document.getElementById('DepthBox').value = canvas.GetDepth();
});
</script>
// a buffer for the vertexes
this.vBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.vBuffer);
vPosition = gl.getAttribLocation(program, "vPosition");
gl.vertexAttribPointer(vPosition,2,gl.FLOAT, false,0,0);
gl.enableVertexAttribArray(vPosition);
// a buffer for the colors
this.cBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.cBuffer);
colorAttribute = gl.getAttribLocation(program, "vColor");
gl.vertexAttribPointer(colorAttribute,3,gl.FLOAT, false,0,0);
gl.enableVertexAttribArray(colorAttribute);
var p1 = vec2(-0.8, -0.8); var p2 = vec2(0, .8); var p3 = vec2(0.8, -0.8); this.vertex= [p1, p2, p3] ;
var c1 = vec3(Math.random(), Math.random(),Math.random()) var c2 = vec3(Math.random(), Math.random(),Math.random()) var c3 = vec3(Math.random(), Math.random(),Math.random()) this.colors = [c1, c2, c3];
this.MakePoints(); this.UpdateBuffers(); this.Redisplay();
UpdateBuffers: function() {
gl = this.gl;
// change the vertex data
gl.bindBuffer(gl.ARRAY_BUFFER, this.vBuffer);
gl.bufferData(gl.ARRAY_BUFFER,flatten(this.vertex),gl.DYNAMIC_DRAW);
// change the color data
gl.bindBuffer(this.gl.ARRAY_BUFFER, this.cBuffer);
gl.bufferData(gl.ARRAY_BUFFER,flatten(this.colors),gl.DYNAMIC_DRAW);
},
MakePoints: function(){
var newVertex = [];
var newColors = [];
var a,b,c;
var p1, p2, p3;
if (this.currentDepth < this.maxDepth) {
this.currentDepth++;
for(i =0; i < this.vertex.length;i+=3) {
a = this.vertex[i];
b = this.vertex[i+1]
c = this.vertex[i+2];
p1 = this.HalfPoint(a,b,.5);
p2 = this.HalfPoint(a,c,.5);
p3 = this.HalfPoint(b,c,.5);
this.AddTri(a,p1,p2, newVertex, this.colors[i], newColors);
this.AddTri(b,p1,p3, newVertex, this.colors[i+1], newColors);
this.AddTri(c,p2,p3, newVertex, this.colors[i+2], newColors);
}
this.vertex = newVertex;
this.colors = newColors;
this.MakePoints();
}
},
AddTri: function(p1, p2, p3, vert, c1, colors) {
vert.push(p1);
colors.push(c1);
vert.push(p2);
var c2 = vec3(Math.random(), Math.random(),Math.random())
colors.push(c2);
vert.push(p3);
var c3 = vec3(Math.random(), Math.random(),Math.random())
colors.push(c3);
},