public void HandleLoadedTexture(HTMLImageElement image) { gl.PixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true); gl.BindTexture(gl.TEXTURE_2D, this.texture); gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image); gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST); gl.GenerateMipmap(gl.TEXTURE_2D); gl.BindTexture(gl.TEXTURE_2D, null); }
public static TextureInfo Create(WebGLRenderingContext gl, string fileName) { var texture = gl.CreateTexture(); var textureInfo = new TextureInfo( texture: texture, width: Optional <int> .Missing, height: Optional <int> .Missing ); var image = new HTMLImageElement(); image.Src = fileName; image.OnLoad = (ev) => { textureInfo.Width = image.Width; textureInfo.Height = image.Height; gl.BindTexture(gl.TEXTURE_2D, textureInfo.Texture); gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image); gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); textureInfo.OnLoad?.Invoke(); }; return(textureInfo); }
public override void BindTexture(uint target, uint texture) { WebGLTexture t = _textures[texture]; if (t == null) { t = gl.CreateTexture(); _textures[texture] = t; } //Log("binding texture " + t + " id " + texture + " for activeTexture: " + (_activeTexture - TEXTURE0)); gl.BindTexture(target, t); CheckError("glBindTexture"); _boundTextureId[_activeTexture] = texture; //glColor3f((float)Math.Random(), (float)Math.Random(), (float)Math.Random()); }
private void DrawSprite(Sprite sprite) { Mat4.Perspective(45, (double)canvas.Width / canvas.Height, 0.1, 1000, pMatrix); Mat4.Identity(mvMatrix); Scale(mvMatrix, sprite.Width, sprite.Height); Mat4.Translate(mvMatrix, new double[] { sprite.X / sprite.Width, sprite.Y / sprite.Height, -12 }); //Mat4.Rotate(mvMatrix, this.DegToRad(xRotation), new double[] { 1, 0, 0 }); //Mat4.Rotate(mvMatrix, this.DegToRad(yRotation), new double[] { 0, 1, 0 }); WebGlRenderingContext.BindBuffer(WebGlRenderingContext.ARRAY_BUFFER, this.cubeVertexPositionBuffer); WebGlRenderingContext.VertexAttribPointer(this.vertexPositionAttribute, 3, WebGlRenderingContext.FLOAT, false, 0, 0); WebGlRenderingContext.BindBuffer(WebGlRenderingContext.ARRAY_BUFFER, this.cubeVertexNormalBuffer); WebGlRenderingContext.VertexAttribPointer(this.vertexNormalAttribute, 3, WebGlRenderingContext.FLOAT, false, 0, 0); WebGlRenderingContext.BindBuffer(WebGlRenderingContext.ARRAY_BUFFER, this.cubeVertexTextureCoordBuffer); WebGlRenderingContext.VertexAttribPointer(this.textureCoordAttribute, 2, WebGlRenderingContext.FLOAT, false, 0, 0); WebGlRenderingContext.ActiveTexture(WebGlRenderingContext.TEXTURE0); WebGlRenderingContext.BindTexture(WebGlRenderingContext.TEXTURE_2D, sprite.Texture.WebGLTexture); WebGlRenderingContext.Uniform1i(this.samplerUniform, 0); // Add Blending if (this.useBlending) { WebGlRenderingContext.BlendFunc(WebGlRenderingContext.SRC_ALPHA, WebGlRenderingContext.ONE); WebGlRenderingContext.Enable(WebGlRenderingContext.BLEND); WebGlRenderingContext.Disable(WebGlRenderingContext.DEPTH_TEST); WebGlRenderingContext.Uniform1f(this.alphaUniform, this.alpha); } else { WebGlRenderingContext.Disable(WebGlRenderingContext.BLEND); WebGlRenderingContext.Enable(WebGlRenderingContext.DEPTH_TEST); WebGlRenderingContext.Uniform1f(this.alphaUniform, 1); } // Add Lighting WebGlRenderingContext.Uniform1i(this.useLightingUniform, this.useLighting); if (this.useLighting) { WebGlRenderingContext.Uniform3f(this.ambientColorUniform, this.ambientR, this.ambientG, this.ambientB); var lightingDirection = new double[] { this.lightDirectionX, this.lightDirectionY, this.lightDirectionZ }; var adjustedLD = Vec3.Create(); Vec3.Normalize(lightingDirection, adjustedLD); Vec3.Scale(adjustedLD, -1); WebGlRenderingContext.Uniform3fv(this.lightingDirectionUniform, adjustedLD); WebGlRenderingContext.Uniform3f(this.directionalColorUniform, this.directionalR, this.directionalG, this.directionalB); } WebGlRenderingContext.BindBuffer(WebGlRenderingContext.ELEMENT_ARRAY_BUFFER, this.cubeVertexIndexBuffer); this.SetMatrixUniforms(); WebGlRenderingContext.DrawElements(WebGlRenderingContext.TRIANGLES, 6, WebGlRenderingContext.UNSIGNED_SHORT, 0); }
public static void DrawImage(WebGLRenderingContext gl, TextureInfo textureInfo, WebGLProgram program, int xSrc, int ySrc, int srcWidth, int srcHeight, int xDest, int yDest, int destWidth, int destHeight) { gl.UseProgram(program); var positionLocation = gl.GetAttribLocation(program, "a_position"); var texCoordLocation = gl.GetAttribLocation(program, "a_texcoord"); var matrixLocation = gl.GetUniformLocation(program, "u_matrix"); var textureLocation = gl.GetUniformLocation(program, "u_texture"); /* Create a buffer to hold the position coordinates. */ var positionBuffer = gl.CreateBuffer(); var positionCoords = new float[] { 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1 }; gl.BindBuffer(gl.ARRAY_BUFFER, positionBuffer); gl.BufferData(gl.ARRAY_BUFFER, new Float32Array(positionCoords), gl.STATIC_DRAW); /* Create a buffer to hold the texture coordinates. */ var sx = xSrc / (float)textureInfo.Width.Value; var sy = ySrc / (float)textureInfo.Height.Value; var sw = srcWidth / (float)textureInfo.Width.Value; var sh = srcHeight / (float)textureInfo.Height.Value; var texBuffer = gl.CreateBuffer(); var texCoords = new float[] { 0 + sx, 0 + sy, 0 + sx, sh + sy, sw + sx, 0 + sy, sw + sx, 0 + sy, 0 + sx, sh + sy, sw + sx, sh + sy }; gl.BindBuffer(gl.ARRAY_BUFFER, texBuffer); gl.BufferData(gl.ARRAY_BUFFER, new Float32Array(texCoords), gl.STATIC_DRAW); gl.BindTexture(gl.TEXTURE_2D, textureInfo.Texture); gl.BindBuffer(gl.ARRAY_BUFFER, positionBuffer); gl.EnableVertexAttribArray(positionLocation); gl.VertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0); gl.BindBuffer(gl.ARRAY_BUFFER, texBuffer); gl.EnableVertexAttribArray(texCoordLocation); gl.VertexAttribPointer(texCoordLocation, 2, gl.FLOAT, false, 0, 0); /* Create an standard orthographic projection to transform our coordinates by and bind to our shader uniform. */ var matrix = CameraHelpers.Orthographic(0, gl.Canvas.Width, gl.Canvas.Height, 0, -1, 1); matrix = matrix.Translate(xDest / (float)gl.Canvas.Width * 2, -yDest / (float)gl.Canvas.Height * 2, 0); matrix = matrix.Scale(destWidth, destHeight, 1); gl.UniformMatrix4fv(matrixLocation, false, matrix.ToArray()); gl.Uniform1i(textureLocation, 0); /* 6 points per image rendered as two triangles. */ gl.DrawArrays(gl.TRIANGLES, 0, 6); gl.BindBuffer(gl.ARRAY_BUFFER, null); }