-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathgasket3.js
More file actions
70 lines (49 loc) · 1.62 KB
/
Copy pathgasket3.js
File metadata and controls
70 lines (49 loc) · 1.62 KB
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
"use strict";
var canvas;
var gl;
var points;
var NumPoints = 5000;
window.onload = 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 vertices of our 3D gasket
var vertices = [
vec3( -0.5, -0.5, -0.5 ),
vec3( 0.5, -0.5, -0.5 ),
vec3( 0.0, 0.5, 0.0 ),
vec3( 0.0, -0.5, 0.5 ),
];
points = [ vec3( 0.0, 0.0, 0.0 ) ];
for ( var i = 0; points.length < NumPoints; ++i ) {
var j = Math.floor(Math.random() * 4);
points.push(mix(points[i], vertices[j], 0.5) );
}
//
// Configure WebGL
//
gl.viewport( 0, 0, canvas.width, canvas.height );
gl.clearColor( 1.0, 1.0, 1.0, 1.0 );
gl.enable( gl.DEPTH_TEST );
// Load shaders and initialize attribute buffers
var program = initShaders( gl, "vertex-shader", "fragment-shader" );
gl.useProgram( program );
// Load the data into the GPU
var bufferId = gl.createBuffer();
gl.bindBuffer( gl.ARRAY_BUFFER, bufferId );
gl.bufferData( gl.ARRAY_BUFFER, flatten(points), gl.STATIC_DRAW );
// Associate out shader variables with our data buffer
var vPosition = gl.getAttribLocation( program, "vPosition" );
gl.vertexAttribPointer( vPosition, 3, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray( vPosition );
render();
};
function render()
{
gl.clear( gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT );
gl.drawArrays( gl.POINTS, 0, points.length );
}