Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- GL.CreateProgram = function(identifier, uniforms, attribs, textures)
- {
- var p = gl.createProgram();
- var program =
- {
- identifier: identifier,
- program: p,
- attribs: []
- };
- var vsh = gl.createShader(gl.VERTEX_SHADER);
- gl.shaderSource(vsh, document.getElementById('vsh' + identifier).text);
- gl.compileShader(vsh);
- if (gl.getShaderParameter(vsh, gl.COMPILE_STATUS) !== true)
- Sys.Error('Error compiling shader: ' + gl.getShaderInfoLog(vsh));
- var fsh = gl.createShader(gl.FRAGMENT_SHADER);
- gl.shaderSource(fsh, document.getElementById('fsh' + identifier).text);
- gl.compileShader(fsh);
- if (gl.getShaderParameter(fsh, gl.COMPILE_STATUS) !== true)
- Sys.Error('Error compiling shader: ' + gl.getShaderInfoLog(fsh));
- gl.attachShader(p, vsh);
- gl.attachShader(p, fsh);
- gl.linkProgram(p);
- if (gl.getProgramParameter(p, gl.LINK_STATUS) !== true)
- Sys.Error('Error linking program: ' + gl.getProgramInfoLog(p));
- gl.useProgram(p);
- var i;
- for (i = 0; i < uniforms.length; ++i)
- program[uniforms[i]] = gl.getUniformLocation(p, uniforms[i]);
- for (i = 0; i < attribs.length; ++i)
- {
- program.attribs[program.attribs.length] = attribs[i];
- program[attribs[i]] = gl.getAttribLocation(p, attribs[i]);
- }
- for (i = 0; i < textures.length; ++i)
- {
- program[textures[i]] = gl.TEXTURE0 + i;
- gl.uniform1i(gl.getUniformLocation(p, textures[i]), i);
- }
- GL.programs[GL.programs.length] = program;
- };
- GL.UseProgram = function(identifier)
- {
- var i, j;
- var program = GL.currentprogram;
- if (program != null)
- {
- if (program.identifier === identifier)
- return program;
- for (i = 0; i < program.attribs.length; ++i)
- gl.disableVertexAttribArray(program[program.attribs[i]]);
- }
- for (i = 0; i < GL.programs.length; ++i)
- {
- program = GL.programs[i];
- if (program.identifier === identifier)
- {
- GL.currentprogram = program;
- gl.useProgram(program.program);
- for (j = 0; j < program.attribs.length; ++j)
- gl.enableVertexAttribArray(program[program.attribs[j]]);
- return program;
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement