WebGL繪製正方體

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Title</title>
    <style>
        body {
            margin: 0;
            text-align: center;
        }
 
        #canvas {
            margin: 0;
        }
    </style>
</head>
<body οnlοad="main()">
<canvas id="canvas" height="800" width="1200"></canvas>
</body>
<script src="lib/webgl-utils.js"></script>
<script src="lib/webgl-debug.js"></script>
<script src="lib/cuon-utils.js"></script>
<script src="lib/cuon-matrix.js"></script>
<script>
    //頂點着色器
    var VSHADER_SOURCE = "" +
        "attribute vec4 a_Position;\n" +
        "attribute vec4 a_Color;\n" +
        "uniform mat4 u_ModelViewMatrix;\n" +
        "varying vec4 v_Color;\n" +
        "void main(){" +
        "   gl_Position = u_ModelViewMatrix * a_Position;\n" +
        "   v_Color = a_Color;\n" +
        "}\n";
 
    //片元着色器
    var FSHADER_SOURCE = "" +
        "#ifdef GL_ES\n" +
        "precision mediump float;\n" +
        "#endif\n" +
        "varying vec4 v_Color;\n" +
        "void main(){" +
        "   gl_FragColor = v_Color;\n" +
        "}\n";
 
    //聲明js須要的相關變量
    var canvas = document.getElementById("canvas");
    var gl = getWebGLContext(canvas);
 
    function main() {
        if (!gl) {
            console.log("你的瀏覽器不支持WebGL");
            return;
        }
 
        //初始化着色器
        if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
            console.log("沒法初始化着色器");
            return;
        }
 
        var n = initVertexBuffers(gl);
        if (n < 0) {
            console.log("沒法建立緩衝區");
            return;
        }
 
        //設置視角矩陣的相關信息
        var u_ModelViewMatrix = gl.getUniformLocation(gl.program, "u_ModelViewMatrix");
        if (u_ModelViewMatrix < 0) {
            console.log("沒法獲取矩陣變量的存儲位置");
            return;
        }
 
        //設置底色
        gl.clearColor(0.0, 0.0, 0.0, 1.0);
 
        //進入場景初始化
        draw(gl, n, u_ModelViewMatrix);
    }
 
    function draw(gl, n, u_ModelViewMatrix) {
        //設置視角矩陣的相關信息(視點,視線,上方向)
        var viewMatrix = new Matrix4();
        viewMatrix.setLookAt(3,3,7,0,0,0,0,1,0);
 
        //設置模型矩陣的相關信息
        var modelMatrix = new Matrix4();
        modelMatrix.setRotate(0, 0, 0, 1);
 
        //設置透視投影矩陣
        var projMatrix = new Matrix4();
        projMatrix.setPerspective(30,canvas.width/canvas.height,1,100);
 
        //計算出模型視圖矩陣 viewMatrix.multiply(modelMatrix)至關於在着色器裏面u_ViewMatrix * u_ModelMatrix
        var modeViewMatrix = projMatrix.multiply(viewMatrix.multiply(modelMatrix));
 
        //將試圖矩陣傳給u_ViewMatrix變量
        gl.uniformMatrix4fv(u_ModelViewMatrix, false, modeViewMatrix.elements);
 
        //開啓隱藏面清除
        gl.enable(gl.DEPTH_TEST);
 
        //清空顏色和深度緩衝區
        gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
 
        //繪製圖形
        gl.drawElements(gl.TRIANGLES,n,gl.UNSIGNED_BYTE,0);
 
    }
 
    function initVertexBuffers(gl) {
        // 建立一個立方體
        //    v6----- v5
        //   /|      /|
        //  v1------v0|
        //  | |     | |
        //  | |v7---|-|v4
        //  |/      |/
        //  v2------v3
        var verticesColors = new Float32Array([
            // 設置頂點和顏色(偷的頂點代碼位置)
            1.0,  1.0,  1.0,     1.0,  1.0,  1.0,  // v0 White
            -1.0,  1.0,  1.0,     1.0,  0.0,  1.0,  // v1 Magenta
            -1.0, -1.0,  1.0,     1.0,  0.0,  0.0,  // v2 Red
            1.0, -1.0,  1.0,     1.0,  1.0,  0.0,  // v3 Yellow
            1.0, -1.0, -1.0,     0.0,  1.0,  0.0,  // v4 Green
            1.0,  1.0, -1.0,     0.0,  1.0,  1.0,  // v5 Cyan
            -1.0,  1.0, -1.0,     0.0,  0.0,  1.0,  // v6 Blue
            -1.0, -1.0, -1.0,     0.0,  0.0,  0.0   // v7 Black
        ]);
 
        //頂點索引
        var indices = new Uint8Array([
            0, 1, 2,   0, 2, 3,    // 前
            0, 3, 4,   0, 4, 5,    // 右
            0, 5, 6,   0, 6, 1,    // 上
            1, 6, 7,   1, 7, 2,    // 左
            7, 4, 3,   7, 3, 2,    // 下
            4, 7, 6,   4, 6, 5     // 後
        ]);
 
        //建立緩衝區對象
        var vertexColorBuffer = gl.createBuffer();
        var indexBuffer = gl.createBuffer();
        if (!vertexColorBuffer || !indexBuffer) {
            console.log("沒法建立緩衝區對象");
            return -1;
        }
 
        //綁定緩衝區對象並寫入數據
        gl.bindBuffer(gl.ARRAY_BUFFER, vertexColorBuffer);
        gl.bufferData(gl.ARRAY_BUFFER, verticesColors, gl.STATIC_DRAW);
 
        //獲取數組中一個元素所佔的字節數
        var fsize = verticesColors.BYTES_PER_ELEMENT;
 
        //獲取頂點位置變量位置
        var a_Position = gl.getAttribLocation(gl.program, "a_Position");
        if (a_Position < 0) {
            console.log("沒法獲取頂點位置的存儲變量");
            return -1;
        }
 
        //對位置的頂點數據進行分配,並開啓
        gl.vertexAttribPointer(a_Position, 3, gl.FLOAT, false, fsize * 6, 0);
        gl.enableVertexAttribArray(a_Position);
 
        //獲取頂點顏色的變量
        var a_Color = gl.getAttribLocation(gl.program, "a_Color");
        if (a_Color < 0) {
            console.log("沒法獲取頂點位置的存儲變量");
            return -1;
        }
 
        //對位置的頂點數據進行分配,並開啓
        gl.vertexAttribPointer(a_Color, 3, gl.FLOAT, false, fsize * 6, fsize * 3);
        gl.enableVertexAttribArray(a_Color);
 
        //將頂點索引數據寫入緩衝區對象
        gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER,indexBuffer);
        gl.bufferData(gl.ELEMENT_ARRAY_BUFFER,indices,gl.STATIC_DRAW);
 
        return indices.length;
    }
</script>
</html>
相關文章
相關標籤/搜索