-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcube.js
More file actions
67 lines (49 loc) · 2.26 KB
/
cube.js
File metadata and controls
67 lines (49 loc) · 2.26 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
var gl;
var points;
window.onload = function init()
{
var canvas = document.getElementById( "gl-canvas" );
gl = WebGLUtils.setupWebGL( canvas ); // this is like a one line initialization
if ( !gl ) { alert( "WebGL isn't available" ); }
var vertices = new Float32Array([-1, -1, 0, 0, 1, -1]);//typed array in javascript is a javascript object that acts like a c type aray, and probably works faster
var new_vertices = [
vec4( -0.5, -0.5, 0.5, 1.0 ),
vec4( -0.5, 0.5, 0.5, 1.0 ),
vec4( 0.5, 0.5, 0.5, 1.0 ),
vec4( 0.5, -0.5, 0.5, 1.0 ),
vec4( -0.5, -0.5, -0.5, 1.0 ),
vec4( -0.5, 0.5, -0.5, 1.0 ),
vec4( 0.5, 0.5, -0.5, 1.0 ),
vec4( 0.5, -0.5, -0.5, 1.0 )];
var vertexColors = [ // could have done the same as above, but decided to do javascript array of javascript array
[ 0.0, 0.0, 0.0, 1.0], // black
[ 1.0, 0.0, 0.0, 1.0], // this is all rgb
[ 1.0, 1.0, 0.0, 1.0],
[ 0.0, 1.0, 0.0, 1.0],
[ 0.0, 0.0, 1.0, 1.0],
[ 0.0, 0.0, 1.0, 1.0],
[ 0.0, 1.0, 1.0, 1.0],
[ 1.0, 1.0, 1.0, 1.0]
];
// Configure WebGL
gl.viewport( 0, 0, canvas.width, canvas.height );
gl.clearColor( 1.0, 1.0, 1.0, 1.0 );
// Load shaders and initialize attribute buffers
var program = initShaders( gl, "vertex-shader", "fragment-shader" ); // these are the two shaders assigned in the html file, this program is some kind of container for all the stuff and shaders we will be using, we can also set a lot of program objects and switch them back and forth
gl.useProgram( program );
// Load the data into the GPU
var bufferId = gl.createBuffer();
gl.bindBuffer( gl.ARRAY_BUFFER, bufferId );
//gl.bufferData( gl.ARRAY_BUFFER,vertices, gl.STATIC_DRAW ); // now our data is on the gpu
// Load the cube instead
gl.bufferData( gl.ARRAY_BUFFER,vertices, gl.STATIC_DRAW )
// Associate out shader variables with our data buffer
var vPosition = gl.getAttribLocation( program, "vPosition" );
gl.vertexAttribPointer( vPosition, 2, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray( vPosition );
render();
};
function render() {
gl.clear( gl.COLOR_BUFFER_BIT );
gl.drawArrays( gl.TRIANGLE_FAN, 0, 3 );
}