43 // Create and compile a vertex or fragment shader as given by the shader type. 44 function compileShader(gl, shaderSource, shaderType) 45 { 46 var shader = gl.createShader(shaderType); 47 gl.shaderSource(shader, shaderSource); 48 gl.compileShader(shader); 49 50 if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) 51 { 52 alert(gl.getShaderInfoLog(shader)); 53 shader = null; 54 } 55 56 return shader; 57 }
The process for compiling a shader is the same for vertex or fragment shaders.
gl.createShader creates an empty vertex shader program and returns a reference to it.
gl.shaderSource takes a reference to a shader as returned from createShader and a string. The string is expected to be the source for a shader of the type given in createShader. The contents of the string are loaded as the source for the shader.
Now that we have created the shader and loaded a program into it, the next step is to compile it. gl.compileShader, as you would expect, compiles the given shader.
The low level nature of the WebGL api is evident in the need to explicitly query for the compilation status and then for the log.