美文网首页WebGL
示例14:利用键盘改变视点

示例14:利用键盘改变视点

作者: WebGiser | 来源:发表于2021-07-04 10:11 被阅读0次

效果

按下键盘左右方向键改变视点


image.png

代码

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <script src="./cuon-matrix.js"></script>
    </head>
    <body>
        <canvas id="canvas" width="600" height="600" style="border: 1px solid #ff0000;"></canvas>

        <script>
            let canvas = document.getElementById('canvas');
            let gl = canvas.getContext('webgl');

            // 顶点着色器
            let VSHADER_SOURCE =
                "attribute vec4 a_Position;\n" +
                "attribute vec4 a_Color;\n" +
                "uniform mat4 u_ViewMatrix;\n" +
                "varying vec4 v_Color;\n" +
                "void main(){\n" +
                "gl_Position = u_ViewMatrix * a_Position;\n" +
                "v_Color = a_Color;\n" +
                "}\n";

            // 片元着色器
            let FSHADER_SOURCE =
                "#ifdef GL_ES\n" +
                "precision mediump float;\n" +
                "#endif\n" +
                "varying vec4 v_Color;\n" +
                "void main(){\n" +
                " gl_FragColor = v_Color;\n" +
                "}\n";


            // 初始化着色器
            function initShaders(gl, vSource, fSource) {
                let vShader = gl.createShader(gl.VERTEX_SHADER);
                gl.shaderSource(vShader, vSource);
                gl.compileShader(vShader);

                let fShader = gl.createShader(gl.FRAGMENT_SHADER);
                gl.shaderSource(fShader, fSource);
                gl.compileShader(fShader);

                let program = gl.createProgram();
                gl.attachShader(program, vShader);
                gl.attachShader(program, fShader);
                gl.linkProgram(program);
                gl.useProgram(program);
                gl.program = program;
            }

            // 设置定点位置-缓冲区对象
            function initVertexBuffers(gl) {
                let vertices = new Float32Array([
                    0.0, 0.5, -0.4, 0.4, 1.0, 0.4, // The back green one
                    -0.5, -0.5, -0.4, 0.4, 1.0, 0.4,
                    0.5, -0.5, -0.4, 1.0, 0.4, 0.4,

                    0.5, 0.4, -0.2, 1.0, 0.4, 0.4, // The middle yellow one
                    -0.5, 0.4, -0.2, 1.0, 1.0, 0.4,
                    0.0, -0.6, -0.2, 1.0, 1.0, 0.4,

                    0.0, 0.5, 0.0, 0.4, 0.4, 1.0, // The front blue one 
                    -0.5, -0.5, 0.0, 0.4, 0.4, 1.0,
                    0.5, -0.5, 0.0, 1.0, 0.4, 0.4,
                ]);
                let n = 9;

                // 创建顶点位置大小颜色缓冲区对象
                let vertexBuffer = gl.createBuffer();
                gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
                gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
                let fsize = vertices.BYTES_PER_ELEMENT;

                let a_Position = gl.getAttribLocation(gl.program, 'a_Position');
                gl.vertexAttribPointer(a_Position, 3, gl.FLOAT, false, fsize * 6, fsize * 0);
                gl.enableVertexAttribArray(a_Position);

                let a_Color = gl.getAttribLocation(gl.program, 'a_Color');
                gl.vertexAttribPointer(a_Color, 3, gl.FLOAT, false, fsize * 6, fsize * 3);
                gl.enableVertexAttribArray(a_Color);

                return n;
            }
            
            function keydown(ev, gl, n, u_ViewMatrix, viewMatrix){
                if(ev.keyCode == 39){
                    g_eyeX += 0.01;
                }else if(ev.keyCode == 37){
                    g_eyeX -= 0.01;
                }else{
                    return;
                }
                draw(gl, n, u_ViewMatrix, viewMatrix);
            }
            
            function draw(gl, n, u_ViewMatrix, viewMatrix){
                viewMatrix.setLookAt(g_eyeX, g_eyeY, g_eyeZ, 0, 0, 0, 0, 1, 0);
                gl.uniformMatrix4fv(u_ViewMatrix, false, matrix.elements);
                gl.clear(gl.COLOR_BUFFER_BIT);
                gl.drawArrays(gl.TRIANGLES, 0, 9);
            }

            initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE);

            initVertexBuffers(gl);
            
            gl.clearColor(0, 0, 0, 1);
            
            let g_eyeX=0.2, g_eyeY=0.25, g_eyeZ=0.25;
            let matrix = new Matrix4();
            matrix.lookAt(0.2, 0.25, 0.25, 0, 0, 0, 0, 1, 0);
            let u_ViewMatrix = gl.getUniformLocation(gl.program, 'u_ViewMatrix');
            document.onkeydown = function(ev){
                keydown(ev, gl, 9, u_ViewMatrix, matrix);
            }
            
            gl.uniformMatrix4fv(u_ViewMatrix, false, matrix.elements);
            gl.clear(gl.COLOR_BUFFER_BIT);
            gl.drawArrays(gl.TRIANGLES, 0, 9);
        </script>
    </body>
</html>

相关文章

网友评论

    本文标题:示例14:利用键盘改变视点

    本文链接:https://www.haomeiwen.com/subject/bjbxultx.html