美文网首页WebGL
示例15:模型视图投影矩阵

示例15:模型视图投影矩阵

作者: WebGiser | 来源:发表于2021-07-06 08:13 被阅读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_MvpMatrix;\n" +
                "varying vec4 v_Color;\n" +
                "void main(){\n" +
                "gl_Position = u_MvpMatrix * 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) {
                // webgl在默认情况下会按照缓冲区中的顺序绘制图形,而且后绘制的图形会覆盖先绘制的图形
                let vertices = new Float32Array([
                    0.0, 1.0, 1.0, 0.4, 1.0, 0.4, // The back green one
                    -0.5, -1.0, 1.0, 0.4, 1.0, 0.4,
                    0.5, -1.0, 1.0, 1.0, 0.4, 0.4,

                    0.0, 1.0, -2.0, 1.0, 1.0, 0.4, // The middle yellow one
                    -0.5, -1.0, -2.0, 1.0, 1.0, 0.4,
                    0.5, -1.0, -2.0, 1.0, 0.4, 0.4,

                    0.0, 1.0, 0.0, 0.4, 0.4, 1.0, // The front blue one 
                    -0.5, -1.0, 0.0, 0.4, 0.4, 1.0,
                    0.5, -1.0, 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;
            }

            initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE);

            initVertexBuffers(gl);

            gl.clearColor(0, 0, 0, 1);
            // 开启隐藏面消除(深度检测)
            gl.enable(gl.DEPTH_TEST);
            gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

            // 设置mvp矩阵:模型矩阵、视图矩阵、投影矩阵
            let modelMatrix = new Matrix4();
            modelMatrix.setTranslate(0.75, 0, 0);
            let viewMatrix = new Matrix4();
            viewMatrix.setLookAt(0, 0, 5, 0, 0, -100, 0, 1, 0);
            let projectMatrix = new Matrix4();
            projectMatrix.setPerspective(30, canvas.width/canvas.height, 1, 100);
            let mvpMatrix = new Matrix4();
            mvpMatrix.set(projectMatrix).multiply(viewMatrix).multiply(modelMatrix);
            
            // 绘制右侧3个三角形
            let u_MvpMatrix = gl.getUniformLocation(gl.program, 'u_MvpMatrix');
            gl.uniformMatrix4fv(u_MvpMatrix, false, mvpMatrix.elements);
            gl.drawArrays(gl.TRIANGLES, 0, 9);
            
            // 绘制左侧3个三角形
            modelMatrix.setTranslate(-0.75, 0, 0);
            mvpMatrix.set(projectMatrix).multiply(viewMatrix).multiply(modelMatrix);
            gl.uniformMatrix4fv(u_MvpMatrix, false, mvpMatrix.elements);
            gl.drawArrays(gl.TRIANGLES, 0, 9);
            
        </script>
    </body>
</html>

相关文章

网友评论

    本文标题:示例15:模型视图投影矩阵

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