<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>WebGL Image Display</title>
<style>
canvas {
width: 500px;
height: 500px;
display: block;
margin: 0 auto;
}
</style>
</head>
<body>
<canvas id="glcanvas"></canvas>
<script>
// Vertex shader source code
const vsSource = `
attribute vec4 aVertexPosition;
attribute vec2 aTextureCoord;
varying highp vec2 vTextureCoord;
void main(void) {
gl_Position = aVertexPosition;
vTextureCoord = aTextureCoord;
}
`;
// Fragment shader source code
const fsSource = `
varying highp vec2 vTextureCoord;
uniform sampler2D uSampler;
void main(void) {
gl_FragColor = texture2D(uSampler, vTextureCoord);
}
`;
// Initialize WebGL context
const canvas = document.getElementById('glcanvas');
const gl = canvas.getContext('webgl');
// Initialize shaders
function initShaderProgram(gl, vsSource, fsSource) {
const vertexShader = loadShader(gl, gl.VERTEX_SHADER, vsSource);
const 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;
}
const shaderProgram = initShaderProgram(gl, vsSource, fsSource);
const programInfo = {
program: shaderProgram,
attribLocations: {
vertexPosition: gl.getAttribLocation(shaderProgram, 'aVertexPosition'),
textureCoord: gl.getAttribLocation(shaderProgram, 'aTextureCoord'),
},
uniformLocations: {
uSampler: gl.getUniformLocation(shaderProgram, 'uSampler'),
},
};
// Initialize buffers
const positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
const positions = [
-1.0, 1.0,
1.0, 1.0,
-1.0, -1.0,
1.0, -1.0,
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
const textureCoordBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, textureCoordBuffer);
const textureCoordinates = [
0.0, 0.0,
1.0, 0.0,
0.0, 1.0,
1.0, 1.0,
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoordinates), gl.STATIC_DRAW);
// Load texture
function loadTexture(gl, cb) {
const texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
const level = 0;
const internalFormat = gl.RGBA;
const width = 1;
const height = 1;
const border = 0;
const srcFormat = gl.RGBA;
const srcType = gl.UNSIGNED_BYTE;
const pixel = new Uint8Array([0, 0, 255, 255]); // Blue default color
gl.texImage2D(gl.TEXTURE_2D, level, internalFormat, width, height, border, srcFormat, srcType, pixel);
const image = new Image();
image.onload = function () {
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
gl.texImage2D(gl.TEXTURE_2D, level, internalFormat, srcFormat, srcType, image);
if (isPowerOf2(image.width) && isPowerOf2(image.height)) {
gl.generateMipmap(gl.TEXTURE_2D);
} else {
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
}
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEX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SmFRzRTGc5xuljbGc+POcZ22/nxrrj/5x/jjnF5K3avXattQqiK6cnTGVmpk0chkcuiuMd5/uRXigVILUDHnfVsyZS0WCCfKY2rVpVLVR0u7WX421wYMELlHH4my7sQRwp7xyK7bK0zylVlEgIqNUl+hxxxx/pFpxxxw0aclquuxHYGmI1MIdT1lKwOOT4kkYlTZnGoeSJOiyNZWrTqRQVIjkcKyOLlEK5u60I62IxYsGeNcyjYu3WSPBwBQVEvHMVhCrVaxTsBDattcJtVz9psphgN8L1z+jleVS2eNkEEmsC69gGYzV7Dqj1Wq7CTYRJElwT1YojWaiJZx+wESmMXJDMTIGYTPUiiZXCdj+0kYh0VgcZ7EWeBj0JktYyyJ7DJE6ayOOlKV0O/0fHB5unjE2Yw2q3Mokxaua9QkOkChMhkkhksbjQ2QHChN3JlV93uwVQk4UUBA6ZOv4L2ssjuGOcH68bJCidqWnXgytZOLLQmMk4xXqddJhRSL4VBgkPDimRBdyz3y4jPx483xc45DfiaFjzS2uMlYemy9kSUNa+vZw9xLibE+TyDZ29gmyyCgznEY4TIgqIEJSspeT1gHz1BJVxCYGVwg6uRpSrx8dJXFXMZVABIzCwyV3xwJWGyWZYb1CuyIEBPgLOvuk7dtZdQ2rSsjdxDoN0ypeUM4rb0JMQKSPY5YHX2jqhsQCZaBzbpyO1Yy/UA7eJotpQBkgNR4Gd4TDGnwWDVn/AOdLCf8APn+M5z/PnPn+f+efdxzMqjVRYK0pIA8qiKJMEYEpq1n27KEz1iOYW+/bZEzyXLyiZkYGBxOvWnoiq1xmgbb70LIpkYtWa9Oq90dpmYJiKFRZRHA8IGYiCkpJxxxyXqJpxxxw0accccNGnHHHDRpxxxw0accccNGv/9k=';
return texture;
}
function isPowerOf2(value) {
return (value & (value - 1)) == 0;
}
const texture = loadTexture(gl, () => {
render();
});
// Draw the scene
function drawScene(gl, programInfo, buffers, texture) {
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.useProgram(programInfo.program);
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
gl.vertexAttribPointer(programInfo.attribLocations.vertexPosition, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(programInfo.attribLocations.vertexPosition);
gl.bindBuffer(gl.ARRAY_BUFFER, textureCoordBuffer);
gl.vertexAttribPointer(programInfo.attribLocations.textureCoord, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(programInfo.attribLocations.textureCoord);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.uniform1i(programInfo.uniformLocations.uSampler, 0);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
}
function render() {
drawScene(gl, programInfo, { position: positionBuffer, textureCoord: textureCoordBuffer }, texture);
}
render();
</script>
</body>
</html>
03-10
392
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
06-30
514
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)