public void DrawScene() { gl.Viewport(0, 0, canvas.Width, canvas.Height); gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); Mat4.Perspective(45, (double)canvas.Width / canvas.Height, 0.1, 100, pMatrix); Mat4.Identity(mvMatrix); Mat4.Translate(mvMatrix, new double[] { 0.0, 0.0, z }); Mat4.Rotate(mvMatrix, this.DegToRad(xRotation), new double[] { 1, 0, 0 }); Mat4.Rotate(mvMatrix, this.DegToRad(yRotation), new double[] { 0, 1, 0 }); gl.BindBuffer(gl.ARRAY_BUFFER, this.cubeVertexPositionBuffer); gl.VertexAttribPointer(this.vertexPositionAttribute, 3, gl.FLOAT, false, 0, 0); gl.BindBuffer(gl.ARRAY_BUFFER, this.cubeVertexNormalBuffer); gl.VertexAttribPointer(this.vertexNormalAttribute, 3, gl.FLOAT, false, 0, 0); gl.BindBuffer(gl.ARRAY_BUFFER, this.cubeVertexTextureCoordBuffer); gl.VertexAttribPointer(this.textureCoordAttribute, 2, gl.FLOAT, false, 0, 0); gl.ActiveTexture(gl.TEXTURE0); gl.BindTexture(gl.TEXTURE_2D, this.texture); gl.Uniform1i(this.samplerUniform, 0); // Add Blending if (this.useBlending) { gl.BlendFunc(gl.SRC_ALPHA, gl.ONE); gl.Enable(gl.BLEND); gl.Disable(gl.DEPTH_TEST); gl.Uniform1f(this.alphaUniform, this.alpha); } else { gl.Disable(gl.BLEND); gl.Enable(gl.DEPTH_TEST); gl.Uniform1f(this.alphaUniform, 1); } // Add Lighting gl.Uniform1i(this.useLightingUniform, this.useLighting); if (this.useLighting) { gl.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); gl.Uniform3fv(this.lightingDirectionUniform, adjustedLD); gl.Uniform3f(this.directionalColorUniform, this.directionalR, this.directionalG, this.directionalB); } gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.cubeVertexIndexBuffer); this.SetMatrixUniforms(); gl.DrawElements(gl.TRIANGLES, 36, gl.UNSIGNED_SHORT, 0); }
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); }