sawine@2: // sawine@2: // initWebGL sawine@2: // sawine@2: // Initialize the Canvas element with the passed name as a WebGL object and return the sawine@2: // WebGLRenderingContext. sawine@2: // sawine@2: // Load shaders with the passed names and create a program with them. Return this program sawine@2: // in the 'program' property of the returned context. sawine@2: // sawine@2: // For each string in the passed attribs array, bind an attrib with that name at that index. sawine@2: // Once the attribs are bound, link the program and then use it. sawine@2: // sawine@2: // Set the clear color to the passed array (4 values) and set the clear depth to the passed value. sawine@2: // Enable depth testing and blending with a blend func of (SRC_ALPHA, ONE_MINUS_SRC_ALPHA) sawine@2: // sawine@2: function initWebGL(canvasName, vshader, fshader, attribs, clearColor, clearDepth) sawine@2: { sawine@2: var canvas = document.getElementById(canvasName); sawine@2: var gl = canvas.getContext("webkit-3d"); sawine@2: sawine@2: // create our shaders sawine@2: var vertexShader = loadShader(gl, vshader); sawine@2: var fragmentShader = loadShader(gl, fshader); sawine@2: sawine@2: if (!vertexShader || !fragmentShader) sawine@2: return null; sawine@2: sawine@2: // Create the program object sawine@2: gl.program = gl.createProgram(); sawine@2: sawine@2: if (!gl.program) sawine@2: return null; sawine@2: sawine@2: // Attach our two shaders to the program sawine@2: gl.attachShader (gl.program, vertexShader); sawine@2: gl.attachShader (gl.program, fragmentShader); sawine@2: sawine@2: // Bind attributes sawine@2: for (var i in attribs) sawine@2: gl.bindAttribLocation (gl.program, i, attribs[i]); sawine@2: sawine@2: // Link the program sawine@2: gl.linkProgram(gl.program); sawine@2: sawine@2: // Check the link status sawine@2: var linked = gl.getProgrami(gl.program, gl.LINK_STATUS); sawine@2: if (!linked) { sawine@2: // something went wrong with the link sawine@2: var error = gl.getProgramInfoLog (gl.program); sawine@2: console.log("Error in program linking:"+error); sawine@2: sawine@2: gl.deleteProgram(gl.program); sawine@2: gl.deleteProgram(fragmentShader); sawine@2: gl.deleteProgram(vertexShader); sawine@2: sawine@2: return null; sawine@2: } sawine@2: sawine@2: gl.useProgram(gl.program); sawine@2: sawine@2: gl.clearColor (clearColor[0], clearColor[1], clearColor[2], clearColor[3]); sawine@2: gl.clearDepth (clearDepth); sawine@2: sawine@2: gl.enable(gl.DEPTH_TEST); sawine@2: gl.enable(gl.BLEND); sawine@2: gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA); sawine@2: sawine@2: return gl; sawine@2: } sawine@2: sawine@2: // sawine@2: // loadShader sawine@2: // sawine@2: // 'shaderId' is the id of a