Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. var cubeShader = new shaderProgram();
  2. cubeShader.addShader(shaders.colorVertexShader);
  3. cubeShader.addShader(shaders.colorFragmentShader);
  4. cubeShader.init();
  5. cubeShader.enableAttrib("position", 3, 4 * (3 + 3), 0);
  6. cubeShader.enableAttrib("color", 3, 4 * (3 + 3), 3 * 4);
  7.  
  8. class shaderProgram {
  9. constructor() {
  10. this.Id = gl.createProgram();
  11. }
  12.  
  13. public Id;
  14.  
  15. public addShader(shader:WebGLShader[]):void {
  16. if(shader instanceof Array) {
  17. shader.forEach(s => {
  18. gl.attachShader(this.Id, s);
  19. });
  20. } else {
  21. gl.attachShader(this.Id, shader);
  22. }
  23. }
  24.  
  25. public init():void {
  26. gl.linkProgram(this.Id);
  27. }
  28.  
  29. public setActive() {
  30. gl.useProgram(this.Id);
  31. }
  32.  
  33. public enableAttrib(attribName: string, index: number, stride:number, offset: number) {
  34. var attrib = gl.getAttribLocation(this.Id, attribName);
  35. gl.enableVertexAttribArray(attrib);
  36. gl.vertexAttribPointer(attrib, index, gl.FLOAT, false, stride, offset);
  37. }
  38.  
  39. public setFloatAttrib(attribName:string, value:number) {
  40. var attrib = gl.getAttribLocation(this.Id, attribName);
  41. gl.vertexAttrib1f(attrib, value);
  42. }
  43.  
  44. public setMatrix(uniName: string, value: number[]) {
  45. var uniform = gl.getUniformLocation(this.Id, uniName);
  46. gl.uniformMatrix4fv(uniform, false, value);
  47. }
  48. }
  49.  
  50. public render():void {
  51. gl.drawElements(gl.TRIANGLES, this._triangles, gl.UNSIGNED_SHORT, 0);
  52. }
  53.  
  54. attribute vec3 position; //the position of the point
  55. uniform mat4 Pmatrix;
  56. uniform mat4 Vmatrix;
  57. uniform mat4 Mmatrix;
  58. attribute vec3 color; //the color of the point
  59. varying vec3 vColor;
  60. void main(void) { //pre-built function
  61. gl_Position = Pmatrix*Vmatrix*Mmatrix*vec4(position, 1.); //0. is the z, and 1 is w
  62. vColor=color;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement