美文网首页
webgl 21.画带颜色的正方体

webgl 21.画带颜色的正方体

作者: lesliefang | 来源:发表于2017-09-07 17:09 被阅读99次
    colored cube.png

    上一节中我们每个顶点给了一个颜色,颜色在各个顶点之间进行了插值。现在我们要每一个面都有一个不同的颜色怎么办呢?

    每个面一个颜色要求每个面上的 4 个顶点的颜色相同,但每个顶点都是被 3 个面所共用,所以 8 个顶点已经不行了,现在每个面需要 4 个顶点,总共需要 24 个顶点。

    每个面用 4 个顶点

    //    v6----- v5
    //   /|      /|
    //  v1------v0|
    //  | |     | |
    //  | |v7---|-|v4
    //  |/      |/
    //  v2------v3
    
    var vertices = new Float32Array([
       1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0,  // v0-v1-v2-v3 front
       1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0,  // v0-v3-v4-v5 right
       1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0,  // v0-v5-v6-v1 up
       -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0,  // v1-v6-v7-v2 left
       -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 1.0,  // v7-v4-v3-v2 down
       1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0   // v4-v7-v6-v5 back
    ]);
    

    每个面 4个顶点指定一个颜色

    var colors = new Float32Array([     // Colors
       0.4, 0.4, 1.0, 0.4, 0.4, 1.0, 0.4, 0.4, 1.0, 0.4, 0.4, 1.0,  // v0-v1-v2-v3 front(blue)
       0.4, 1.0, 0.4, 0.4, 1.0, 0.4, 0.4, 1.0, 0.4, 0.4, 1.0, 0.4,  // v0-v3-v4-v5 right(green)
       1.0, 0.4, 0.4, 1.0, 0.4, 0.4, 1.0, 0.4, 0.4, 1.0, 0.4, 0.4,  // v0-v5-v6-v1 up(red)
       1.0, 1.0, 0.4, 1.0, 1.0, 0.4, 1.0, 1.0, 0.4, 1.0, 1.0, 0.4,  // v1-v6-v7-v2 left
       1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,  // v7-v4-v3-v2 down
       0.4, 1.0, 1.0, 0.4, 1.0, 1.0, 0.4, 1.0, 1.0, 0.4, 1.0, 1.0   // v4-v7-v6-v5 back
    ]);
    

    索引值需要相应的改变

    var indices = new Uint8Array([   
        0, 1, 2, 0, 2, 3,    // front
        4, 5, 6, 4, 6, 7,    // right
        8, 9, 10, 8, 10, 11,    // up
        12, 13, 14, 12, 14, 15,    // left
        16, 17, 18, 16, 18, 19,    // down
        20, 21, 22, 20, 22, 23     // back
    ]);
    

    完整代码

    <script id="vertex-shader" type="glsl">
        attribute vec4 a_Position;
        attribute vec4 a_Color;
        uniform mat4 u_mvpMatrix;
        varying vec4 v_Color;
    
        void main() {
            gl_Position = u_mvpMatrix * a_Position;
            v_Color = a_Color;
        }
    </script>
    
    <script id="fragment-shader" type="glsl">
        precision mediump float;
        varying vec4 v_Color;
        void main() {
           gl_FragColor = v_Color;
        }
    </script>
    
    <script src="lib/cuon-matrix.js"></script>
    <script src="lib/myutils.js"></script>
    
    <script>
        var VERTEX_SHADER_SOURCE = document.getElementById('vertex-shader').text;
        var FRAGMENT_SHADER_SOURCE = document.getElementById('fragment-shader').text;
    
        var canvas = document.getElementById("canvas");
        var gl = canvas.getContext('webgl');
    
        if (!initShaders(gl, VERTEX_SHADER_SOURCE, FRAGMENT_SHADER_SOURCE)) {
            alert('Failed to init shaders');
        }
    
        var n = initVertexBuffers(gl);
    
        var u_mvpMatrix = gl.getUniformLocation(gl.program, 'u_mvpMatrix');
        // <projection matrix> * <view matrix>
        var mvpMatrix = new Matrix4();
        mvpMatrix.setPerspective(30, 1, 1, 100);
        mvpMatrix.lookAt(3, 3, 7, 0, 0, 0, 0, 1, 0);
    
        gl.uniformMatrix4fv(u_mvpMatrix, false, mvpMatrix.elements);
    
        gl.enable(gl.DEPTH_TEST);
        gl.clearColor(0.0, 0.0, 0.0, 1.0);
        gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
        gl.drawElements(gl.TRIANGLES, n, gl.UNSIGNED_BYTE, 0);
    
        function initVertexBuffers(gl) {
            // Create a cube
            //    v6----- v5
            //   /|      /|
            //  v1------v0|
            //  | |     | |
            //  | |v7---|-|v4
            //  |/      |/
            //  v2------v3
    
            var vertices = new Float32Array([
                1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0,  // v0-v1-v2-v3 front
                1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0,  // v0-v3-v4-v5 right
                1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0,  // v0-v5-v6-v1 up
                -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0,  // v1-v6-v7-v2 left
                -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 1.0,  // v7-v4-v3-v2 down
                1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0   // v4-v7-v6-v5 back
            ]);
    
            var colors = new Float32Array([     // Colors
                0.4, 0.4, 1.0, 0.4, 0.4, 1.0, 0.4, 0.4, 1.0, 0.4, 0.4, 1.0,  // v0-v1-v2-v3 front(blue)
                0.4, 1.0, 0.4, 0.4, 1.0, 0.4, 0.4, 1.0, 0.4, 0.4, 1.0, 0.4,  // v0-v3-v4-v5 right(green)
                1.0, 0.4, 0.4, 1.0, 0.4, 0.4, 1.0, 0.4, 0.4, 1.0, 0.4, 0.4,  // v0-v5-v6-v1 up(red)
                1.0, 1.0, 0.4, 1.0, 1.0, 0.4, 1.0, 1.0, 0.4, 1.0, 1.0, 0.4,  // v1-v6-v7-v2 left
                1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,  // v7-v4-v3-v2 down
                0.4, 1.0, 1.0, 0.4, 1.0, 1.0, 0.4, 1.0, 1.0, 0.4, 1.0, 1.0   // v4-v7-v6-v5 back
            ]);
    
            var indices = new Uint8Array([       // Indices of the vertices
                0, 1, 2, 0, 2, 3,    // front
                4, 5, 6, 4, 6, 7,    // right
                8, 9, 10, 8, 10, 11,    // up
                12, 13, 14, 12, 14, 15,    // left
                16, 17, 18, 16, 18, 19,    // down
                20, 21, 22, 20, 22, 23     // back
            ]);
    
            if (!initArrayBuffer(gl, vertices, 3, gl.FLOAT, 'a_Position')) {
                return -1;
            }
            if (!initArrayBuffer(gl, colors, 3, gl.FLOAT, 'a_Color')) {
                return -1;
            }
    
            var indexBuffer = gl.createBuffer();
            if (!indexBuffer) {
                console.log('Failed to create index buffer');
                return -1;
            }
            gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
            gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
    
            return indices.length;
        }
    
        function initArrayBuffer(gl, data, numOfComponents, dataType, attributeName) {
            var buffer = gl.createBuffer();
            if (!buffer) {
                console.log('Failed to create buffer object');
                return false;
            }
    
            gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
            gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW);
    
            var attribute = gl.getAttribLocation(gl.program, attributeName);
            gl.vertexAttribPointer(attribute, numOfComponents, dataType, false, 0, 0);
            gl.enableVertexAttribArray(attribute);
    
            return true;
        }
    </script>
    

    查看源码

    相关文章

      网友评论

          本文标题:webgl 21.画带颜色的正方体

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