console
main();
function main() {
const canvas = document.querySelector("#glcanvas");
const gl = canvas.getContext("webgl");
if (!gl) {
alert("Unable to initialize WebGL. Your browser or machine may not support it.");
return;
}
let vertexSource = `
attribute vec4 a_Position;
attribute vec2 a_TexCoord;
varying lowp vec2 v_TexCoord;
void main(void){
gl_Position = a_Position;
v_TexCoord = a_TexCoord;
}`;
let fragmentSource = `
precision mediump float;
uniform sampler2D u_Sampler;
varying lowp vec2 v_TexCoord;
void main(void){
gl_FragColor = texture2D(u_Sampler, v_TexCoord);
}`;
let shaderProgram = initShaderProgram(gl, vertexSource, fragmentSource);
gl.useProgram(shaderProgram);
initBuffers(gl,shaderProgram);
initTextures(gl,shaderProgram,'http://jsrun.net/res/css/img/wxpay.png')
}
function initBuffers(gl, program){
var verticesTexCoords = new Float32Array([
-0.5, 0.5, 0.0, 1.0,
-0.5, -0.5, 0.0, 0.0,
0.5, 0.5, 1.0, 1.0,
0.5, -0.5, 1.0, 0.0,
]);
var vertexTexCoordBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexTexCoordBuffer);
gl.bufferData(gl.ARRAY_BUFFER, verticesTexCoords, gl.STATIC_DRAW);
var FSIZE = verticesTexCoords.BYTES_PER_ELEMENT;
var a_Position = gl.getAttribLocation(program, 'a_Position');
gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, FSIZE * 4, 0);
gl.enableVertexAttribArray(a_Position);
var a_TexCoord = gl.getAttribLocation(program, 'a_TexCoord');
gl.vertexAttribPointer(a_TexCoord, 2, gl.FLOAT, false, FSIZE * 4, FSIZE * 2);
gl.enableVertexAttribArray(a_TexCoord);
}
function initTextures(gl,program,url){
var texture = gl.createTexture();
var u_Sampler = gl.getUniformLocation(program, 'u_Sampler');
var image = new Image();
image.onload = function(){
loadTexture(gl,texture, u_Sampler, image);
};
image.src = url;
}
function loadTexture(gl, texture, u_Sampler, image){
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, image);
gl.uniform1i(u_Sampler, 0);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
}
function initShaderProgram(gl, vsSource, fsSource) {
let vertexShader = loadShader(gl, gl.VERTEX_SHADER, vsSource);
let fragmentShader = loadShader(gl, gl.FRAGMENT_SHADER, fsSource);
const shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert('Unable to initialize the shader program: ' + gl.getProgramInfoLog(shaderProgram));
return null;
}
return shaderProgram;
}
function loadShader(gl, type, source) {
const shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert('An error occurred compiling the shaders: ' + gl.getShaderInfoLog(shader));
gl.deleteShader(shader);
return null;
}
return shader;
}
<h3>WebGL Demo</h3>
<canvas id="glcanvas" width="640" height="480"></canvas>