<!DOCTYPE HTML>
<head>
</head>
<html>
<body onload="main()">
<canvas id="webgl" width="500" height="500" style="border: 1px solid #ff0000;">
</canvas>
<script type="text/javascript">
//顶点着色器程序
var VSHADER_SOURCE =
'attribute vec4 a_Position;\n'+
'attribute vec2 a_TexCoord;\n'+
'varying vec2 v_TexCoord;\n'+
'void main(){\n'+
'gl_Position = a_Position;\n'+
'v_TexCoord = a_TexCoord;\n'+
'}\n';
//片元着色器程序
var FSHADER_SOURCE =
'uniform sampler2D u_Sampler;\n'+
'precision mediump float;\n'+
'varying vec2 v_TexCoord;\n'+
'void main(){\n'+
'gl_FragColor = texture2D(u_Sampler, v_TexCoord);\n'+
'}\n';
function main(){
//获取<canvas>元素
var canvas = document.getElementById("webgl");
if(!canvas){
console.log('error');
return;
}
//获取绘制三维图形的绘图上下文
var gl = canvas.getContext('webgl');
// 创建着色器对象
const fShader = gl.createShader(gl.FRAGMENT_SHADER);
const vShader = gl.createShader(gl.VERTEX_SHADER);
// 将着色器源码写入对象
gl.shaderSource(vShader, VSHADER_SOURCE);
gl.shaderSource(fShader, FSHADER_SOURCE);
// 编译着色器
gl.compileShader(vShader);
gl.compileShader(fShader);
// 创建程序
const program = gl.createProgram();
// 程序绑定着色器
gl.attachShader(program, vShader);
gl.attachShader(program, fShader);
// 链接程序
gl.linkProgram(program);
// 使用程序
gl.useProgram(program);
var n = initVertexBuffers(gl);
if(n < 0){
console.log('Failed to set positions of vertices');
return;
}
if(!initTextures(gl, n)){
return;
}
function initVertexBuffers(gl){
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 n = 4; //坐标点个数
//创建缓冲区对象
var vertexBuffer = gl.createBuffer();
if(!vertexBuffer){
console.log('Failed to create the buffer object');
return -1;
}
//将缓冲区对象绑定到目标
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
//向缓冲区对象中写入数据
gl.bufferData(gl.ARRAY_BUFFER, verticesTexCoords, gl.STATIC_DRAW);
var FSIZE = verticesTexCoords.BYTES_PER_ELEMENT;
var a_Position = gl.getAttribLocation(program, 'a_Position');
if(a_Position < 0){
console.log('Failed to get the storage location of a_Position');
return;
}
//将缓冲区对象分配给a_Position变量
gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, FSIZE*4, 0);
//连接a_Position变量与分配给它的缓冲区对象
gl.enableVertexAttribArray(a_Position);
var a_TexCoord = gl.getAttribLocation(program, 'a_TexCoord');
if(a_TexCoord < 0){
console.log('Failed to get the storage location of a_TexCoord');
return;
}
//将缓冲区对象分配给a_Position变量
gl.vertexAttribPointer(a_TexCoord, 2, gl.FLOAT, false, FSIZE*4, FSIZE*2);
//连接a_Position变量与分配给它的缓冲区对象
gl.enableVertexAttribArray(a_TexCoord);
return n;
}
function initTextures(gl, n){
//创建纹理对象
var texture = gl.createTexture();
//获取u_Sampler的存储位置
var u_Sampler = gl.getUniformLocation(program, 'u_Sampler');
var image = new Image();
image.onload = function(){
loadTexture(gl, n, texture, u_Sampler, image);
};
image.src = 'http://localhost:8080/webgl/1.jpg';
return true;
}
function loadTexture(gl, n, 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_MAG_FILTER, gl.LINEAR);
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.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, image);
gl.uniform1i(u_Sampler, 0);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, n);
}
}
</script>
</body>
</html>
WebGL 3D(纹理)
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1、谷歌浏览器(1)错误信息:Only secure origins are allowed (see: http...
- 也似李白說的“使我不得開心顏”.還有誰說的不再是純粹的自然之色.看周作人寫的詩《小孩》.很有感覺.但又覺自己想...