Created
August 29, 2014 17:51
-
-
Save sgrif/fc9e4a7cb765b49b6890 to your computer and use it in GitHub Desktop.
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
@main = -> | |
canvas = document.getElementsByTagName("canvas")[0] | |
gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl") | |
compiler = new WebGLCompiler(gl, window.shaders) | |
program = compiler.createProgramWithShaders("main_vertex", "main_fragment") | |
gl.clearColor(1.0, 1.0, 1.0, 1.0) | |
gl.clear(gl.COLOR_BUFFER_BIT) | |
gl.useProgram(program) | |
coordinates = new Float32Array([ | |
0.0, 0.8 | |
-0.8, -0.8 | |
0.8, -0.8 | |
]) | |
vertexCoord = gl.getAttribLocation(program, "vertexCoord") | |
buffer = gl.createBuffer() | |
gl.bindBuffer(gl.ARRAY_BUFFER, buffer) | |
gl.bufferData(gl.ARRAY_BUFFER, coordinates, gl.STATIC_DRAW) | |
gl.enableVertexAttribArray(vertexCoord) | |
gl.vertexAttribPointer(vertexCoord, 2, gl.FLOAT, false, 0, 0) | |
gl.drawArrays(gl.TRIANGLES, 0, 3) |
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
void main() { | |
gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0); | |
} |
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
attribute vec2 vertexCoord; | |
void main() { | |
gl_Position = vec4(vertexCoord, 0.0, 1.0); | |
} |
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
class @WebGLCompiler | |
constructor: (@gl, @shaders) -> | |
createProgramWithShaders: (vertexShaderName, fragmentShaderName) -> | |
vertexShader = @_createShader(vertexShaderName) | |
fragmentShader = @_createShader(fragmentShaderName) | |
@_createProgram(vertexShader, fragmentShader) | |
_createShader: (shaderName) -> | |
shaderSource = @shaders["#{shaderName}.glsl"] | |
unless shaderSource | |
throw "Unknown shader: #{shaderName}" | |
@_compileShader(shaderSource, @_typeForShader(shaderName)) | |
_typeForShader: (name) -> | |
if name.indexOf("vertex") != -1 | |
@gl.VERTEX_SHADER | |
else if name.indexOf("fragment") != -1 | |
@gl.FRAGMENT_SHADER | |
else | |
throw "Unknown shader type for #{name}" | |
_compileShader: (shaderSource, shaderType) -> | |
shader = @gl.createShader(shaderType) | |
@gl.shaderSource(shader, shaderSource) | |
@gl.compileShader(shader) | |
unless @gl.getShaderParameter(shader, @gl.COMPILE_STATUS) | |
error = @gl.getShaderInfoLog(shader) | |
console.error(error) | |
throw "Could not compile shader. Error: #{error}" | |
shader | |
_createProgram: (vertexShader, fragmentShader) -> | |
program = @gl.createProgram() | |
@gl.attachShader(program, vertexShader) | |
@gl.attachShader(program, fragmentShader) | |
@gl.linkProgram(program) | |
unless @gl.getProgramParameter(program, @gl.LINK_STATUS) | |
error = @gl.getProgramInfoLog(program) | |
console.error(error) | |
throw "Program failed to link. Error: #{error}" | |
program |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment