Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function VertexPositionColorBuffer(gl)
- {
- this.PositionBuffer = gl.createBuffer();
- this.ColorBuffer = gl.createBuffer();
- this.NumVertices = 0;
- this.OnLoad = null;
- this.File = null;
- this.gl = gl;
- }
- VertexPositionColorBuffer.prototype.Load = function()
- {
- var x = new XMLHttpRequest();
- x.open("GET", this.File);
- x.parent = this;
- x.onload = function()
- {
- if(this.readyState != 4)
- return;
- var Obj = eval("(" + this.responseText + ")");
- this.parent.gl.bindBuffer(gl.ARRAY_BUFFER, this.parent.PositionBuffer);
- this.parent.gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(Obj.Vertices), gl.STATIC_DRAW);
- this.parent.gl.bindBuffer(gl.ARRAY_BUFFER, this.parent.ColorBuffer);
- this.parent.gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(Obj.Colors), gl.STATIC_DRAW);
- this.parent.NumVertices = Obj.Vertices.length / 3;
- typeof this.parent.OnLoad == "function" && this.parent.OnLoad();
- };
- x.send(null);
- };
- VertexPositionColorBuffer.prototype.Draw = function(PointerPosition, PointerColor)
- {
- this.gl.bindBuffer(gl.ARRAY_BUFFER, this.PositionBuffer);
- this.gl.vertexAttribPointer(PointerPosition, 3, gl.FLOAT, false, 0, 0);
- this.gl.bindBuffer(gl.ARRAY_BUFFER, this.ColorBuffer);
- this.gl.vertexAttribPointer(PointerColor, 4, gl.FLOAT, false, 0, 0);
- this.gl.drawArrays(gl.TRIANGLES, 0, this.NumVertices);
- };
- function VertexPositionColorNormalBuffer(gl)
- {
- this.PositionBuffer = gl.createBuffer();
- this.ColorBuffer = gl.createBuffer();
- this.NormalBuffer = gl.createBuffer();
- this.NumVertices = 0;
- this.OnLoad = null;
- this.File = null;
- this.gl = gl;
- }
- VertexPositionColorNormalBuffer.prototype.Load = function()
- {
- var x = new XMLHttpRequest();
- x.open("GET", this.File);
- x.parent = this;
- x.onload = function()
- {
- if(this.readyState != 4)
- return;
- var Obj = eval("(" + this.responseText + ")");
- this.parent.gl.bindBuffer(gl.ARRAY_BUFFER, this.parent.PositionBuffer);
- this.parent.gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(Obj.Vertices), gl.STATIC_DRAW);
- this.parent.gl.bindBuffer(gl.ARRAY_BUFFER, this.parent.ColorBuffer);
- this.parent.gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(Obj.Colors), gl.STATIC_DRAW);
- this.parent.gl.bindBuffer(gl.ARRAY_BUFFER, this.parent.NormalBuffer);
- this.parent.gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(Obj.Normals), gl.STATIC_DRAW);
- this.parent.NumVertices = Obj.Vertices.length / 3;
- typeof this.parent.OnLoad == "function" && this.parent.OnLoad();
- };
- x.send(null);
- };
- VertexPositionColorNormalBuffer.prototype.Draw = function(PointerPosition, PointerColor, PointerNormal)
- {
- this.gl.bindBuffer(gl.ARRAY_BUFFER, this.PositionBuffer);
- this.gl.vertexAttribPointer(PointerPosition, 3, gl.FLOAT, false, 0, 0);
- this.gl.bindBuffer(gl.ARRAY_BUFFER, this.ColorBuffer);
- this.gl.vertexAttribPointer(PointerColor, 4, gl.FLOAT, false, 0, 0);
- this.gl.bindBuffer(gl.ARRAY_BUFFER, this.NormalBuffer);
- this.gl.vertexAttribPointer(PointerNormal, 3, gl.FLOAT, false, 0, 0);
- this.gl.drawArrays(gl.TRIANGLES, 0, this.NumVertices);
- };
- function VertexPositionNormalTextureIndexBuffer(gl)
- {
- this.PositionBuffer = gl.createBuffer();
- this.NormalBuffer = gl.createBuffer();
- this.TextureCoordinateBuffer = gl.createBuffer();
- this.IndexBuffer = gl.createBuffer();
- this._Texture = gl.createTexture();
- this.NumVertices = 0;
- this.OnLoad = null;
- this.File = null;
- this.gl = gl;
- this.Sampler = {}
- this.Sampler.MinFilter = this.gl.LINEAR;
- this.Sampler.MagFilter = this.gl.LINEAR;
- this.Sampler.AddressU = this.gl.REPEAT;
- this.Sampler.AddressV = this.gl.REPEAT;
- }
- VertexPositionNormalTextureIndexBuffer.prototype.__defineSetter__("Texture", function(Image)
- {
- this._Texture.image = Image;
- this.gl.bindTexture(this.gl.TEXTURE_2D, this._Texture);
- this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MIN_FILTER, this.Sampler.MinFilter)
- this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MAG_FILTER, this.Sampler.MagFilter);
- this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_S, this.Sampler.AddressU);
- this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_T, this.Sampler.AddressV);
- this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGB, this.gl.RGB, this.gl.UNSIGNED_BYTE, this._Texture.image);
- //this.gl.enable(gl.TEXTURE_2D); //ATI Mipmap bug =/
- this.gl.generateMipmap(this.gl.TEXTURE_2D);
- });
- VertexPositionNormalTextureIndexBuffer.prototype.Load = function()
- {
- var x = new XMLHttpRequest();
- x.open("GET", this.File);
- x.parent = this;
- x.onload = function()
- {
- if(this.readyState != 4)
- return;
- var Obj = eval("(" + this.responseText + ")");
- this.parent.gl.bindBuffer(gl.ARRAY_BUFFER, this.parent.PositionBuffer);
- this.parent.gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(Obj.Vertices), gl.STATIC_DRAW);
- this.parent.gl.bindBuffer(gl.ARRAY_BUFFER, this.parent.NormalBuffer);
- this.parent.gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(Obj.Normals), gl.STATIC_DRAW);
- this.parent.gl.bindBuffer(gl.ARRAY_BUFFER, this.parent.TextureCoordinateBuffer);
- this.parent.gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(Obj.TextureCoordinates), gl.STATIC_DRAW);
- this.parent.gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.parent.IndexBuffer);
- this.parent.gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(Obj.Indices), gl.STATIC_DRAW);
- this.parent.NumVertices = Obj.Indices.length;
- typeof this.parent.OnLoad == "function" && this.parent.OnLoad();
- };
- x.send(null);
- };
- VertexPositionNormalTextureIndexBuffer.prototype.Draw = function(PointerPosition, PointerNormal, PointerTexCoord)
- {
- this.gl.bindTexture(gl.TEXTURE_2D, this._Texture);
- this.gl.activeTexture(gl.TEXTURE0);
- this.gl.bindBuffer(gl.ARRAY_BUFFER, this.PositionBuffer);
- this.gl.vertexAttribPointer(PointerPosition, 3, gl.FLOAT, false, 0, 0);
- this.gl.bindBuffer(gl.ARRAY_BUFFER, this.NormalBuffer);
- this.gl.vertexAttribPointer(PointerNormal, 3, gl.FLOAT, false, 0, 0);
- this.gl.bindBuffer(gl.ARRAY_BUFFER, this.TextureCoordinateBuffer);
- this.gl.vertexAttribPointer(PointerTexCoord, 2, gl.FLOAT, false, 0, 0);
- this.gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.IndexBuffer);
- this.gl.drawElements(gl.TRIANGLES, this.NumVertices, gl.UNSIGNED_SHORT, 0);
- };
- function Effect(gl)
- {
- this.gl = gl;
- this._Technique = this.gl.createProgram();
- this.Linked = false;
- this.Parameters = {};
- this.Attributes = {};
- }
- Effect.prototype.getUniforms = function(ShaderSource)
- {
- ShaderSource = ShaderSource.replace(/\{[^\}]+\}/g, "");
- var Uniforms = ShaderSource.match(/uniform\s+[^\s]+\s+[^\r^\n^;]+/gi);
- if(!Uniforms)
- return;
- for(var I = 0; I < Uniforms.length; I++)
- {
- var Uniform = Uniforms[I].match(/\s[^\s]+(:?\s{0,}=|;|$)/)[0].replace(/\s+|=/g, "");
- this.Parameters[Uniform] = this.gl.getUniformLocation(this._Technique, Uniform);
- }
- };
- Effect.prototype.getAttributes = function()
- {
- var ShaderSource = this.VertexShaderSource;
- ShaderSource = ShaderSource.replace(/\{[^\}]+\}/g, "");
- var Attributes = ShaderSource.match(/attribute\s+[^\s]+\s+[^\r^\n^;]+/gi);
- for(var I = 0; I < Attributes.length; I++)
- {
- var Attribute = Attributes[I].match(/\s[^\s]+$/)[0].replace(/\s+/g, "");
- this.Attributes[Attribute] = this.gl.getAttribLocation(this._Technique, Attribute);
- }
- }
- Effect.prototype.__defineSetter__("VertexShader", function(VS)
- {
- this.VertexShaderSource = document.getElementById(VS).text;
- var Shader = this.gl.createShader(gl.VERTEX_SHADER);
- this.gl.shaderSource(Shader, this.VertexShaderSource);
- this.gl.compileShader(Shader);
- this.gl.attachShader(this._Technique, Shader);
- });
- Effect.prototype.__defineSetter__("PixelShader", function(PS)
- {
- this.PixelShaderSource = document.getElementById(PS).text;
- var Shader = this.gl.createShader(gl.FRAGMENT_SHADER);
- this.gl.shaderSource(Shader, this.PixelShaderSource);
- this.gl.compileShader(Shader);
- this.gl.attachShader(this._Technique, Shader);
- });
- Effect.prototype.__defineGetter__("Technique", function()
- {
- if(!this.Linked)
- {
- this.gl.linkProgram(this._Technique);
- this.Linked = true;
- this.getUniforms(this.VertexShaderSource);
- this.getUniforms(this.PixelShaderSource);
- this.getAttributes();
- for(var I in this.Attributes)
- this.gl.enableVertexAttribArray(this.Attributes[I]);
- }
- return this._Technique;
- });
- Effect.prototype.Use = function()
- {
- this.gl.useProgram(this.Technique);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement