public void compile(WebGLRenderingContext webgl) { this.vs = webgl.CreateShader(webgl.VERTEX_SHADER); this.fs = webgl.CreateShader(webgl.FRAGMENT_SHADER); //分别编译shader webgl.ShaderSource(this.vs, this.vscode); webgl.CompileShader(this.vs); var r1 = webgl.GetShaderParameter(this.vs, webgl.COMPILE_STATUS); if (r1.As <bool>() == false) { alert(webgl.GetShaderInfoLog(this.vs)); } // webgl.ShaderSource(this.fs, this.fscode); webgl.CompileShader(this.fs); var r2 = webgl.GetShaderParameter(this.fs, webgl.COMPILE_STATUS); if (r2.As <bool>() == false) { alert(webgl.GetShaderInfoLog(this.fs)); } //program link this.program = webgl.CreateProgram().As <WebGLProgram>(); webgl.AttachShader(this.program, this.vs); webgl.AttachShader(this.program, this.fs); webgl.LinkProgram(this.program); var r3 = webgl.GetProgramParameter(this.program, webgl.LINK_STATUS); if (r3.As <bool>() == false) { alert(webgl.GetProgramInfoLog(this.program)); } //绑定vbo和shader顶点格式,这部分应该要区分材质改变与参数改变,可以少切换一些状态 this.posPos = webgl.GetAttribLocation(this.program, "position"); this.posColor = webgl.GetAttribLocation(this.program, "color"); this.posColor2 = webgl.GetAttribLocation(this.program, "color2"); this.posUV = webgl.GetAttribLocation(this.program, "uv"); this.uniMatrix = webgl.GetUniformLocation(this.program, "matrix"); this.uniTex0 = webgl.GetUniformLocation(this.program, "tex0"); this.uniTex1 = webgl.GetUniformLocation(this.program, "tex1"); this.uniCol0 = webgl.GetUniformLocation(this.program, "col0"); this.uniCol1 = webgl.GetUniformLocation(this.program, "col1"); }
public static void DrawLine(WebGLRenderingContext gl, WebGLProgram program, RGBAColour colour, int x0, int y0, int x1, int y1) { gl.UseProgram(program); var positionCoords = new float[] { x0, y0, x1, y1 }; /* Add the components of the colour twice (one for each vertex). */ var colours = new float[] { colour.R, colour.G, colour.B, colour.A, colour.R, colour.G, colour.B, colour.A }; var matrixLocation = gl.GetUniformLocation(program, "u_matrix"); /* Load the colour into a buffer and bind to shader attribute. */ var colourBuffer = gl.CreateBuffer(); var colourLocation = gl.GetAttribLocation(program, "a_color"); gl.BindBuffer(gl.ARRAY_BUFFER, colourBuffer); gl.BufferData(gl.ARRAY_BUFFER, new Float32Array(colours), gl.STATIC_DRAW); gl.VertexAttribPointer(colourLocation, 4, gl.FLOAT, false, 0, 0); gl.EnableVertexAttribArray(colourLocation); /* Load the line endpoints into a buffer and bind to shader attribute. */ var vertexBuffer = gl.CreateBuffer(); var positionLocation = gl.GetAttribLocation(program, "a_position"); gl.BindBuffer(gl.ARRAY_BUFFER, vertexBuffer); gl.BufferData(gl.ARRAY_BUFFER, new Float32Array(positionCoords), gl.STATIC_DRAW); gl.VertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0); gl.EnableVertexAttribArray(positionLocation); /* Set up projection matrix and translate to screen origin. */ var matrix = CameraHelpers.Orthographic(0, gl.Canvas.Width, gl.Canvas.Height, 0, -1, 1); gl.UniformMatrix4fv(matrixLocation, false, matrix.ToArray()); matrix = matrix.Translate(x0 / (float)gl.Canvas.Width * 2, -y0 / (float)gl.Canvas.Height * 2, 0); gl.DrawArrays(gl.LINES, 0, 2); gl.BindBuffer(gl.ARRAY_BUFFER, null); }
public void InitShaders() { var fragmentShader = this.GetShader(gl, "shader-fs"); var vertexShader = this.GetShader(gl, "shader-vs"); var shaderProgram = gl.CreateProgram().As <WebGLProgram>(); if (shaderProgram.Is <int>()) { Global.Alert("Could not initialise program"); } gl.AttachShader(shaderProgram, vertexShader); gl.AttachShader(shaderProgram, fragmentShader); gl.LinkProgram(shaderProgram); if (!gl.GetProgramParameter(shaderProgram, gl.LINK_STATUS).As <bool>()) { Global.Alert("Could not initialise shaders"); } gl.UseProgram(shaderProgram); this.vertexPositionAttribute = gl.GetAttribLocation(shaderProgram, "aVertexPosition"); this.vertexNormalAttribute = gl.GetAttribLocation(shaderProgram, "aVertexNormal"); this.textureCoordAttribute = gl.GetAttribLocation(shaderProgram, "aTextureCoord"); gl.EnableVertexAttribArray(this.vertexPositionAttribute); gl.EnableVertexAttribArray(this.vertexNormalAttribute); gl.EnableVertexAttribArray(this.textureCoordAttribute); this.pMatrixUniform = gl.GetUniformLocation(shaderProgram, "uPMatrix"); this.mvMatrixUniform = gl.GetUniformLocation(shaderProgram, "uMVMatrix"); this.nMatrixUniform = gl.GetUniformLocation(shaderProgram, "uNMatrix"); this.samplerUniform = gl.GetUniformLocation(shaderProgram, "uSampler"); this.useLightingUniform = gl.GetUniformLocation(shaderProgram, "uUseLighting"); this.ambientColorUniform = gl.GetUniformLocation(shaderProgram, "uAmbientColor"); this.lightingDirectionUniform = gl.GetUniformLocation(shaderProgram, "uLightingDirection"); this.directionalColorUniform = gl.GetUniformLocation(shaderProgram, "uDirectionalColor"); this.alphaUniform = gl.GetUniformLocation(shaderProgram, "uAlpha"); this.program = shaderProgram; }
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); }