Advertisement
Guest User

Untitled

a guest
Nov 13th, 2012
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. GL.CreateProgram = function(identifier, uniforms, attribs, textures)
  2. {
  3.     var p = gl.createProgram();
  4.     var program =
  5.     {
  6.         identifier: identifier,
  7.         program: p,
  8.         attribs: []
  9.     };
  10.  
  11.     var vsh = gl.createShader(gl.VERTEX_SHADER);
  12.     gl.shaderSource(vsh, document.getElementById('vsh' + identifier).text);
  13.     gl.compileShader(vsh);
  14.     if (gl.getShaderParameter(vsh, gl.COMPILE_STATUS) !== true)
  15.         Sys.Error('Error compiling shader: ' + gl.getShaderInfoLog(vsh));
  16.  
  17.     var fsh = gl.createShader(gl.FRAGMENT_SHADER);
  18.     gl.shaderSource(fsh, document.getElementById('fsh' + identifier).text);
  19.     gl.compileShader(fsh);
  20.     if (gl.getShaderParameter(fsh, gl.COMPILE_STATUS) !== true)
  21.         Sys.Error('Error compiling shader: ' + gl.getShaderInfoLog(fsh));
  22.  
  23.     gl.attachShader(p, vsh);
  24.     gl.attachShader(p, fsh);
  25.  
  26.     gl.linkProgram(p);
  27.     if (gl.getProgramParameter(p, gl.LINK_STATUS) !== true)
  28.         Sys.Error('Error linking program: ' + gl.getProgramInfoLog(p));
  29.  
  30.     gl.useProgram(p);
  31.     var i;
  32.     for (i = 0; i < uniforms.length; ++i)
  33.         program[uniforms[i]] = gl.getUniformLocation(p, uniforms[i]);
  34.     for (i = 0; i < attribs.length; ++i)
  35.     {
  36.         program.attribs[program.attribs.length] = attribs[i];
  37.         program[attribs[i]] = gl.getAttribLocation(p, attribs[i]);
  38.     }
  39.     for (i = 0; i < textures.length; ++i)
  40.     {
  41.         program[textures[i]] = gl.TEXTURE0 + i;
  42.         gl.uniform1i(gl.getUniformLocation(p, textures[i]), i);
  43.     }
  44.  
  45.     GL.programs[GL.programs.length] = program;
  46. };
  47.  
  48. GL.UseProgram = function(identifier)
  49. {
  50.     var i, j;
  51.     var program = GL.currentprogram;
  52.     if (program != null)
  53.     {
  54.         if (program.identifier === identifier)
  55.             return program;
  56.         for (i = 0; i < program.attribs.length; ++i)
  57.             gl.disableVertexAttribArray(program[program.attribs[i]]);
  58.     }
  59.     for (i = 0; i < GL.programs.length; ++i)
  60.     {
  61.         program = GL.programs[i];
  62.         if (program.identifier === identifier)
  63.         {
  64.             GL.currentprogram = program;
  65.             gl.useProgram(program.program);
  66.             for (j = 0; j < program.attribs.length; ++j)
  67.                 gl.enableVertexAttribArray(program[program.attribs[j]]);
  68.             return program;
  69.         }
  70.     }
  71. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement