我会持续更新关于wegl的编程指南中的代码。
当前的代码不会使用书中的缩写,每一步都是会展开写。希望能给后来学习的一些帮助
接着 上一节中 接着做平移的转化。在本次的案例案例中主要是xy的坐标变量相加,同时传递个给相关变量
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- head>
-
- <body>
- <h3>可以按下按键ADSWh3>
- <p>旋转的角度:<span id="num">span>p>
- <canvas id='canvas'>canvas>
- <script>
- let canvas = document.getElementById('canvas');
- let gl = canvas.getContext('webgl');
-
- let vertexShaderSource = `
- attribute vec4 a_Position;
- uniform mat4 u_mat4;
- void main(){
- gl_Position=u_mat4*a_Position;
- }
- `
- let fragmentShaderSouce = `
- precision mediump float;
- void main(){
- gl_FragColor = vec4(1.0,0.0,0.0,1.0);
- }
- `
- // 创建顶点着色器
- let vertexShader = gl.createShader(gl.VERTEX_SHADER);
- // 设置着色器源代码
- gl.shaderSource(vertexShader, vertexShaderSource);
- // 编译着色器
- gl.compileShader(vertexShader);
-
- // 创建片元着色器
- let fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
- // 设置着色器源代码
- gl.shaderSource(fragmentShader, fragmentShaderSouce);
- // 编译着色器
- gl.compileShader(fragmentShader)
- //创建渲染程序
- let program = gl.createProgram();
- // 附着顶点着色器和片元着色器到渲染程序
- gl.attachShader(program, vertexShader);
- gl.attachShader(program, fragmentShader);
- gl.linkProgram(program);
- //使用当前渲染程序
- gl.useProgram(program)
-
-
- // 检测着色器链接是否正确
- if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
- throw gl.getProgramInfoLog(program);
- }
- // 旋转角度
- let rotateX = 0.0;
- let rotateY = 0;
- // 三角形的顶点
- let positonArray = new Float32Array([0.0, 0.5, -0.5, -0.5, 0.5, -0.5])
- // 获取顶点着色器中变量a_Position的存储位置
- let a_Position = gl.getAttribLocation(program, 'a_Position');
- // 激活顶点着色器中变量u_mat4的存储位置
- let u_mat4 = gl.getUniformLocation(program, 'u_mat4');
-
-
-
- // 创建buffer对象
- let buffer = gl.createBuffer();
- // 绑定buffer对象
- gl.bindBuffer(gl.ARRAY_BUFFER, buffer)
- // 向buffer中写入数据
- gl.bufferData(gl.ARRAY_BUFFER, positonArray, gl.STATIC_DRAW)
- // 设置attribute变量a_Position的值
- gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0)
- // 启用attribute变量a_Position
- gl.enableVertexAttribArray(a_Position);
-
-
-
- //绘制
- gl.clearColor(0.0, 0.0, 0.0, 1.0);
-
-
-
- const render = function () {
- // 计算角度
- let cos = Math.cos(Math.PI*rotateX/180.0);
- let sin = Math.sin(Math.PI*rotateX/180.0);
- // 计算旋转矩阵
- let mate4=new Float32Array([
- cos,sin,0.0,0.0,
- -sin,cos,0.0,0.0,
- 0.0,0.0,1.0,0.0,
- 0.0,0.0,0.0,1.0,
- ])
- // 设置旋转矩阵
- gl.uniformMatrix4fv(u_mat4, false, mate4)
-
- // 清除画布
- gl.clear(gl.COLOR_BUFFER_BIT);
- // 绘制图形
- gl.drawArrays(gl.TRIANGLES, 0, 3)
- // 更新角度
- setNumer()
- }
- render()
-
- document.onkeydown = function (e) { //对整个页面监听
- var keyNum = window.event ? e.keyCode : e.which; //获取被按下的键值
- //判断如果用户按下了回车键(keycody=13)
- if (keyNum == 65) {
- console.log('A');
- rotateX -= 1;
- rotateY=0
- render()
- }
-
- //判断如果用户按下了空格键(keycode=32),
- if (keyNum == 68) {
- console.log('D');
- rotateX +=1;
- rotateY=0
- render()
- }
- //判断如果用户按下了空格键(keycode=32),
- if (keyNum == 83) {
- console.log('S');
- rotateY -= 1;
- rotateX=0
- render()
- }
-
-
- //判断如果用户按下了空格键(keycode=32),
- if (keyNum == 87) {
- console.log('W');
- rotateY += 1;
- rotateX=0
- render();
-
- }
-
- }
- function setNumer (){
- let text = document.getElementById('num');
- text.innerText = `rotateX:${ rotateX}`
- }
-
-
-
- script>
- body>
-
- html>