webGL入门(七)通过键盘控制物体运动

效果:

代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebGL Example</title>
    <script>
        var webgl;
        var countX = 0;
        var countY = 0;

        var vsString = `
            attribute vec3 a_position;
            uniform float angleX;
            uniform float angleY;
            void main(){
                gl_Position = vec4(a_position.x + angleX, a_position.y + angleY, 0.0, 1.0);
            }
        `;

        var fsString = `
            void main(){
                gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
            }
        `;

        function init() {
            webgl = initWebgl();
            if (!webgl) {
                return;
            }
            initShader();
            initBuffer();
            initEvent();
            animationLoop();
        }

        function initWebgl() {
            let webglDiv = document.getElementById('webgl_canvas');
            webgl = webglDiv.getContext("webgl") || webglDiv.getContext("experimental-webgl");
            if (webgl) {
                webgl.viewport(0, 0, webglDiv.clientWidth, webglDiv.clientHeight);
                return webgl;
            } else {
                alert("Failed to get WebGL context. Your browser or machine may not support it.");
                return null;
            }
        }

        function initShader() {
            let shaderVS = webgl.createShader(webgl.VERTEX_SHADER);
            let shaderFS = webgl.createShader(webgl.FRAGMENT_SHADER);

            webgl.shaderSource(shaderVS, vsString);
            webgl.shaderSource(shaderFS, fsString);

            webgl.compileShader(shaderVS);
            webgl.compileShader(shaderFS);

            let program = webgl.createProgram();
            webgl.attachShader(program, shaderVS);
            webgl.attachShader(program, shaderFS);

            webgl.linkProgram(program);
            webgl.useProgram(program);
            webgl.program = program;
        }

        function initBuffer() {
            let arr = [
                0.1, 0.4, 0.0,
                0.1, 0.5, 0.0,
                0.2, 0.4, 0.0,
            ];
            let floatArr = new Float32Array(arr);
            let buffer = webgl.createBuffer();
            webgl.bindBuffer(webgl.ARRAY_BUFFER, buffer);
            webgl.bufferData(webgl.ARRAY_BUFFER, floatArr, webgl.STATIC_DRAW);

            let aPosition = webgl.getAttribLocation(webgl.program, "a_position");
            webgl.vertexAttribPointer(aPosition, 3, webgl.FLOAT, false, 0, 0);
            webgl.enableVertexAttribArray(aPosition);

            let xAngle = webgl.getUniformLocation(webgl.program, "angleX");
            let yAngle = webgl.getUniformLocation(webgl.program, "angleY");
            webgl.uniform1f(xAngle, countX);
            webgl.uniform1f(yAngle, countY);
        }

        function initEvent() {
            document.onkeydown = handleKeyDown;
        }

        function handleKeyDown(event) {
            event.preventDefault(); // Prevent scrolling/other default behavior
            if (event.keyCode == 87) { // W
                countY += 0.1;
            } else if (event.keyCode == 83) { // S
                countY -= 0.1;
            } else if (event.keyCode == 65) { // A
                countX -= 0.1;
            } else if (event.keyCode == 68) { // D
                countX += 0.1;
            }
            draw();
        }

        function draw() {
            webgl.clearColor(0.0, 0.0, 1.0, 1.0); // Clear to blue
            webgl.clear(webgl.COLOR_BUFFER_BIT);

            let xAngle = webgl.getUniformLocation(webgl.program, "angleX");
            let yAngle = webgl.getUniformLocation(webgl.program, "angleY");
            webgl.uniform1f(xAngle, countX);
            webgl.uniform1f(yAngle, countY);

            webgl.drawArrays(webgl.TRIANGLES, 0, 3);
        }

        function animationLoop() {
            requestAnimationFrame(animationLoop);
            draw();
        }
    </script>
</head>
<body onload="init()">
    <canvas id="webgl_canvas" width="500" height="500"></canvas>
</body>
</html>

复盘:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值