Advertisement
Delta

WebGLX.js

Mar 23rd, 2011
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function VertexPositionColorBuffer(gl)
  2. {
  3.     this.PositionBuffer = gl.createBuffer();
  4.     this.ColorBuffer = gl.createBuffer();
  5.     this.NumVertices = 0;
  6.     this.OnLoad = null;
  7.     this.File = null;
  8.     this.gl = gl;
  9. }
  10.        
  11. VertexPositionColorBuffer.prototype.Load = function()
  12. {
  13.     var x = new XMLHttpRequest();
  14.     x.open("GET", this.File);
  15.     x.parent = this;
  16.     x.onload = function()
  17.     {
  18.         if(this.readyState != 4)
  19.             return;
  20.        
  21.         var Obj = eval("(" + this.responseText + ")");
  22.            
  23.         this.parent.gl.bindBuffer(gl.ARRAY_BUFFER, this.parent.PositionBuffer);
  24.         this.parent.gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(Obj.Vertices), gl.STATIC_DRAW);
  25.    
  26.         this.parent.gl.bindBuffer(gl.ARRAY_BUFFER, this.parent.ColorBuffer);
  27.         this.parent.gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(Obj.Colors), gl.STATIC_DRAW);
  28.        
  29.         this.parent.NumVertices = Obj.Vertices.length / 3;
  30.        
  31.         typeof this.parent.OnLoad == "function" && this.parent.OnLoad();
  32.     };
  33.     x.send(null);
  34. };
  35.  
  36. VertexPositionColorBuffer.prototype.Draw = function(PointerPosition, PointerColor)
  37. {
  38.     this.gl.bindBuffer(gl.ARRAY_BUFFER, this.PositionBuffer);
  39.     this.gl.vertexAttribPointer(PointerPosition, 3, gl.FLOAT, false, 0, 0);
  40.    
  41.     this.gl.bindBuffer(gl.ARRAY_BUFFER, this.ColorBuffer);
  42.     this.gl.vertexAttribPointer(PointerColor, 4, gl.FLOAT, false, 0, 0);
  43.    
  44.     this.gl.drawArrays(gl.TRIANGLES, 0, this.NumVertices);
  45. };
  46.  
  47. function VertexPositionColorNormalBuffer(gl)
  48. {
  49.     this.PositionBuffer = gl.createBuffer();
  50.     this.ColorBuffer = gl.createBuffer();
  51.     this.NormalBuffer = gl.createBuffer();
  52.     this.NumVertices = 0;
  53.     this.OnLoad = null;
  54.     this.File = null;
  55.     this.gl = gl;
  56. }
  57.        
  58. VertexPositionColorNormalBuffer.prototype.Load = function()
  59. {
  60.     var x = new XMLHttpRequest();
  61.     x.open("GET", this.File);
  62.     x.parent = this;
  63.     x.onload = function()
  64.     {
  65.         if(this.readyState != 4)
  66.             return;
  67.        
  68.         var Obj = eval("(" + this.responseText + ")");
  69.            
  70.         this.parent.gl.bindBuffer(gl.ARRAY_BUFFER, this.parent.PositionBuffer);
  71.         this.parent.gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(Obj.Vertices), gl.STATIC_DRAW);
  72.    
  73.         this.parent.gl.bindBuffer(gl.ARRAY_BUFFER, this.parent.ColorBuffer);
  74.         this.parent.gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(Obj.Colors), gl.STATIC_DRAW);
  75.        
  76.         this.parent.gl.bindBuffer(gl.ARRAY_BUFFER, this.parent.NormalBuffer);
  77.         this.parent.gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(Obj.Normals), gl.STATIC_DRAW);
  78.        
  79.         this.parent.NumVertices = Obj.Vertices.length / 3;
  80.        
  81.         typeof this.parent.OnLoad == "function" && this.parent.OnLoad();
  82.     };
  83.     x.send(null);
  84. };
  85.  
  86. VertexPositionColorNormalBuffer.prototype.Draw = function(PointerPosition, PointerColor, PointerNormal)
  87. {
  88.     this.gl.bindBuffer(gl.ARRAY_BUFFER, this.PositionBuffer);
  89.     this.gl.vertexAttribPointer(PointerPosition, 3, gl.FLOAT, false, 0, 0);
  90.    
  91.     this.gl.bindBuffer(gl.ARRAY_BUFFER, this.ColorBuffer);
  92.     this.gl.vertexAttribPointer(PointerColor, 4, gl.FLOAT, false, 0, 0);
  93.    
  94.     this.gl.bindBuffer(gl.ARRAY_BUFFER, this.NormalBuffer);
  95.     this.gl.vertexAttribPointer(PointerNormal, 3, gl.FLOAT, false, 0, 0);
  96.    
  97.     this.gl.drawArrays(gl.TRIANGLES, 0, this.NumVertices);
  98. };
  99.  
  100. function VertexPositionNormalTextureIndexBuffer(gl)
  101. {
  102.     this.PositionBuffer = gl.createBuffer();
  103.     this.NormalBuffer = gl.createBuffer();
  104.     this.TextureCoordinateBuffer = gl.createBuffer();
  105.     this.IndexBuffer = gl.createBuffer();
  106.     this._Texture = gl.createTexture();
  107.     this.NumVertices = 0;
  108.     this.OnLoad = null;
  109.     this.File = null;
  110.     this.gl = gl;
  111.    
  112.     this.Sampler = {}
  113.     this.Sampler.MinFilter = this.gl.LINEAR;
  114.     this.Sampler.MagFilter = this.gl.LINEAR;
  115.     this.Sampler.AddressU = this.gl.REPEAT;
  116.     this.Sampler.AddressV = this.gl.REPEAT;
  117. }
  118.  
  119. VertexPositionNormalTextureIndexBuffer.prototype.__defineSetter__("Texture", function(Image)
  120. {
  121.     this._Texture.image = Image;
  122.     this.gl.bindTexture(this.gl.TEXTURE_2D, this._Texture);
  123.     this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MIN_FILTER, this.Sampler.MinFilter)
  124.     this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MAG_FILTER, this.Sampler.MagFilter);
  125.     this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_S, this.Sampler.AddressU);
  126.     this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_T, this.Sampler.AddressV);
  127.     this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGB, this.gl.RGB, this.gl.UNSIGNED_BYTE, this._Texture.image);
  128.    
  129.     //this.gl.enable(gl.TEXTURE_2D); //ATI Mipmap bug =/
  130.     this.gl.generateMipmap(this.gl.TEXTURE_2D);
  131. });
  132.  
  133. VertexPositionNormalTextureIndexBuffer.prototype.Load = function()
  134. {
  135.     var x = new XMLHttpRequest();
  136.     x.open("GET", this.File);
  137.     x.parent = this;
  138.     x.onload = function()
  139.     {
  140.         if(this.readyState != 4)
  141.             return;
  142.        
  143.         var Obj = eval("(" + this.responseText + ")");
  144.            
  145.         this.parent.gl.bindBuffer(gl.ARRAY_BUFFER, this.parent.PositionBuffer);
  146.         this.parent.gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(Obj.Vertices), gl.STATIC_DRAW);
  147.        
  148.         this.parent.gl.bindBuffer(gl.ARRAY_BUFFER, this.parent.NormalBuffer);
  149.         this.parent.gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(Obj.Normals), gl.STATIC_DRAW);
  150.        
  151.         this.parent.gl.bindBuffer(gl.ARRAY_BUFFER, this.parent.TextureCoordinateBuffer);
  152.         this.parent.gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(Obj.TextureCoordinates), gl.STATIC_DRAW);
  153.        
  154.         this.parent.gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.parent.IndexBuffer);
  155.         this.parent.gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(Obj.Indices), gl.STATIC_DRAW);
  156.        
  157.         this.parent.NumVertices = Obj.Indices.length;
  158.        
  159.         typeof this.parent.OnLoad == "function" && this.parent.OnLoad();
  160.     };
  161.     x.send(null);
  162. };
  163.  
  164. VertexPositionNormalTextureIndexBuffer.prototype.Draw = function(PointerPosition, PointerNormal, PointerTexCoord)
  165. {
  166.     this.gl.bindTexture(gl.TEXTURE_2D, this._Texture);
  167.     this.gl.activeTexture(gl.TEXTURE0);
  168.    
  169.     this.gl.bindBuffer(gl.ARRAY_BUFFER, this.PositionBuffer);
  170.     this.gl.vertexAttribPointer(PointerPosition, 3, gl.FLOAT, false, 0, 0);
  171.    
  172.     this.gl.bindBuffer(gl.ARRAY_BUFFER, this.NormalBuffer);
  173.     this.gl.vertexAttribPointer(PointerNormal, 3, gl.FLOAT, false, 0, 0);
  174.    
  175.     this.gl.bindBuffer(gl.ARRAY_BUFFER, this.TextureCoordinateBuffer);
  176.     this.gl.vertexAttribPointer(PointerTexCoord, 2, gl.FLOAT, false, 0, 0);
  177.    
  178.     this.gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.IndexBuffer);
  179.  
  180.     this.gl.drawElements(gl.TRIANGLES, this.NumVertices, gl.UNSIGNED_SHORT, 0);
  181. };
  182.  
  183. function Effect(gl)
  184. {
  185.     this.gl = gl;
  186.     this._Technique = this.gl.createProgram();
  187.     this.Linked = false;
  188.     this.Parameters = {};
  189.     this.Attributes = {};
  190. }
  191.    
  192. Effect.prototype.getUniforms = function(ShaderSource)
  193. {
  194.     ShaderSource = ShaderSource.replace(/\{[^\}]+\}/g, "");
  195.     var Uniforms = ShaderSource.match(/uniform\s+[^\s]+\s+[^\r^\n^;]+/gi);
  196.    
  197.     if(!Uniforms)
  198.         return;
  199.    
  200.     for(var I = 0; I < Uniforms.length; I++)
  201.     {
  202.         var Uniform = Uniforms[I].match(/\s[^\s]+(:?\s{0,}=|;|$)/)[0].replace(/\s+|=/g, "");
  203.         this.Parameters[Uniform] = this.gl.getUniformLocation(this._Technique, Uniform);
  204.     }
  205. };
  206.        
  207. Effect.prototype.getAttributes = function()
  208. {
  209.     var ShaderSource = this.VertexShaderSource;
  210.     ShaderSource = ShaderSource.replace(/\{[^\}]+\}/g, "");
  211.     var Attributes = ShaderSource.match(/attribute\s+[^\s]+\s+[^\r^\n^;]+/gi);
  212.            
  213.     for(var I = 0; I < Attributes.length; I++)
  214.     {
  215.         var Attribute = Attributes[I].match(/\s[^\s]+$/)[0].replace(/\s+/g, "");
  216.         this.Attributes[Attribute] = this.gl.getAttribLocation(this._Technique, Attribute);
  217.     }
  218. }
  219.        
  220. Effect.prototype.__defineSetter__("VertexShader", function(VS)
  221. {
  222.     this.VertexShaderSource = document.getElementById(VS).text;
  223.     var Shader = this.gl.createShader(gl.VERTEX_SHADER);
  224.     this.gl.shaderSource(Shader, this.VertexShaderSource);
  225.     this.gl.compileShader(Shader);
  226.     this.gl.attachShader(this._Technique, Shader);
  227. });
  228.        
  229. Effect.prototype.__defineSetter__("PixelShader", function(PS)
  230. {
  231.     this.PixelShaderSource = document.getElementById(PS).text;
  232.     var Shader = this.gl.createShader(gl.FRAGMENT_SHADER);
  233.     this.gl.shaderSource(Shader, this.PixelShaderSource);
  234.     this.gl.compileShader(Shader);
  235.     this.gl.attachShader(this._Technique, Shader);
  236. });
  237.        
  238. Effect.prototype.__defineGetter__("Technique", function()
  239. {
  240.     if(!this.Linked)
  241.     {
  242.         this.gl.linkProgram(this._Technique);
  243.         this.Linked = true;
  244.                
  245.         this.getUniforms(this.VertexShaderSource);
  246.         this.getUniforms(this.PixelShaderSource);
  247.         this.getAttributes();
  248.                
  249.         for(var I in this.Attributes)
  250.             this.gl.enableVertexAttribArray(this.Attributes[I]);
  251.     }
  252.            
  253.     return this._Technique;
  254. });
  255.        
  256. Effect.prototype.Use = function()
  257. {
  258.     this.gl.useProgram(this.Technique);
  259. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement