2017-05-05 14:00:25 +03:00
|
|
|
WebGLUtil = (function() {
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// WebGL helpers
|
|
|
|
|
|
|
|
// Returns a valid shader, or null on errors.
|
|
|
|
function createShaderById(gl, id) {
|
|
|
|
var elem = document.getElementById(id);
|
|
|
|
if (!elem) {
|
2021-09-23 03:53:32 +03:00
|
|
|
throw new Error(
|
|
|
|
"Failed to create shader from non-existent id '" + id + "'."
|
|
|
|
);
|
2017-05-05 14:00:25 +03:00
|
|
|
}
|
|
|
|
|
2021-09-23 03:53:32 +03:00
|
|
|
var src = elem.innerHTML.trim();
|
2017-05-05 14:00:25 +03:00
|
|
|
|
|
|
|
var shader;
|
|
|
|
if (elem.type == "x-shader/x-fragment") {
|
|
|
|
shader = gl.createShader(gl.FRAGMENT_SHADER);
|
|
|
|
} else if (elem.type == "x-shader/x-vertex") {
|
|
|
|
shader = gl.createShader(gl.VERTEX_SHADER);
|
|
|
|
} else {
|
2021-09-23 03:53:32 +03:00
|
|
|
throw new Error(
|
|
|
|
"Bad MIME type for shader '" + id + "': " + elem.type + "."
|
|
|
|
);
|
2017-05-05 14:00:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
gl.shaderSource(shader, src);
|
|
|
|
gl.compileShader(shader);
|
|
|
|
|
|
|
|
return shader;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createProgramByIds(gl, vsId, fsId) {
|
|
|
|
var vs = createShaderById(gl, vsId);
|
|
|
|
var fs = createShaderById(gl, fsId);
|
2021-09-23 03:53:32 +03:00
|
|
|
if (!vs || !fs) {
|
2017-05-05 14:00:25 +03:00
|
|
|
return null;
|
2021-09-23 03:53:32 +03:00
|
|
|
}
|
2017-05-05 14:00:25 +03:00
|
|
|
|
|
|
|
var prog = gl.createProgram();
|
|
|
|
gl.attachShader(prog, vs);
|
|
|
|
gl.attachShader(prog, fs);
|
|
|
|
gl.linkProgram(prog);
|
|
|
|
|
|
|
|
if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) {
|
|
|
|
var str = "Shader program linking failed:";
|
|
|
|
str += "\nShader program info log:\n" + gl.getProgramInfoLog(prog);
|
|
|
|
str += "\n\nVert shader log:\n" + gl.getShaderInfoLog(vs);
|
|
|
|
str += "\n\nFrag shader log:\n" + gl.getShaderInfoLog(fs);
|
2021-09-23 03:53:32 +03:00
|
|
|
console.error(str);
|
2017-05-05 14:00:25 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return prog;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2021-09-23 03:53:32 +03:00
|
|
|
createShaderById,
|
|
|
|
createProgramByIds,
|
2017-05-05 14:00:25 +03:00
|
|
|
};
|
|
|
|
})();
|