Skip to content

Instantly share code, notes, and snippets.

@idettman
Forked from philogb/simple.js
Created February 3, 2017 16:18
Show Gist options
  • Save idettman/32aa4e75b24d2015cba5c39d8e8d106b to your computer and use it in GitHub Desktop.
Save idettman/32aa4e75b24d2015cba5c39d8e8d106b to your computer and use it in GitHub Desktop.
window.addEventListener('DOMContentLoaded', function() {
//check support
if (!supportsWebGL()) {
$('webgl-canvas').innerHTML = 'Your browser doesn\'t seem to support WebGL. More info <a href=\'http://get.webgl.org/\'>here</a>.';
return;
}
//get context
var canvas = $('webgl-canvas'),
gl = getWebGLContext(canvas);
//create a program
createProgramFromURIs(gl, {
vsURI: 'shaders/simple.vs',
fsURI: 'shaders/simple.fs',
onComplete: function(program) {
render(program);
}
});
function render(program) {
gl.useProgram(program);
var sizeLocation = gl.getUniformLocation(program, 'size'),
positionLocation = gl.getAttribLocation(program, 'position'),
buffer = gl.createBuffer(),
vertices = [-1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1];
//set uniform size data
gl.uniform1f(sizeLocation, canvas.width);
//set position attribute data
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
gl.enableVertexAttribArray(positionLocation);
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
//draw rectangle
gl.drawArrays(gl.TRIANGLES, 0, 6);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment