Created
May 15, 2016 22:58
-
-
Save raphamorim/812e845f9a2602acdab18011a5587b29 to your computer and use it in GitHub Desktop.
WebGL Boilerplate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createShaders(gl) { | |
var vertexShaderSrc = String(); // fill it with shader string or Script Text | |
var vertexShader = gl.createShader(gl.VERTEX_SHADER); | |
gl.shaderSource(vertexShader, vertexShaderSrc); | |
gl.compileShader(vertexShader); | |
var success = gl.getShaderParameter(vertexShader, gl.COMPILE_STATUS); | |
if (!success) { | |
throw "could not compile shader:" + gl.getShaderInfoLog(vertexShader); | |
} | |
var fragmentShaderSrc = String(); // fill it with shader string or Script Text | |
var fragmentShader = gl.createShader(gl.FRAGMENT_SHADER); | |
gl.shaderSource(fragmentShader, fragmentShaderSrc); | |
gl.compileShader(fragmentShader); | |
var success = gl.getShaderParameter(fragmentShader, gl.COMPILE_STATUS); | |
if (!success) { | |
throw "could not compile shader:" + gl.getShaderInfoLog(fragmentShader); | |
} | |
return { | |
vertexShader: vertexShader, | |
fragmentShader: fragmentShader | |
}; | |
} | |
function initWebGL() { | |
var gl = null, | |
canvas = document.querySelector('canvas'); | |
try { | |
gl = ( | |
canvas.getContext("webgl") || | |
canvas.getContext("experimental-webgl") | |
) | |
} catch (e) {} | |
if (!gl) { | |
console.warn("Unable to initialize WebGL. Your browser may not support it."); | |
gl = null; | |
} | |
return gl; | |
} | |
function start() { | |
var gl = initWebGL(), | |
shaders = createShaders(gl); | |
var program = gl.createProgram(); | |
gl.attachShader(program, shaders.vertexShader); | |
gl.attachShader(program, shaders.fragmentShader); | |
gl.linkProgram(program); | |
gl.useProgram(program); | |
} | |
start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
:)