webgl绘制矩形

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>WebGL</title>

    <!-- 顶点着色器 -->
    <script id="shader-vs" type="x-shader/x-vertex">
        attribute vec3 aVertexPosition;

        void main() {
            // 直接将外部传入的顶点坐标作为最终坐标
            gl_Position = vec4(aVertexPosition, 1.0);
        }
    </script>

    <!-- 片段着色器 -->
    <script id="shader-fs" type="x-shader/x-fragment">
        // 设定 float 的精度, 这里我们使用中等精度
        precision mediump float;

        void main() {
            // 输出为纯白色
            gl_FragColor = vec4(0, 1.0, 1.0, 1.0);
        }
    </script>

    <script>
        var gl;
        var canvas;
        var shaderProgram;
        var vertexBuffer;

        function createGLContext(canvas) {
            var names = ["webgl", "experimental-webgl"];
            var context = null;
            for (var i = 0; i < names.length; i++) {
                try {
                    context = canvas.getContext(names[i]);
                } catch (e) { }
                if (context) {
                    break;
                }
            }
            if (context) {
                // 添加动态属性记录画布的大小
                context.viewportWidth = canvas.width;
                context.viewportHeight = canvas.height;
            } else {
                alert("Failed to create WebGL context!");
            }
            return context;
        }

        function setupShaders() {
            // 从 DOM 上创建对应的着色器
            vertexShader = loadShaderFromDOM("shader-vs");
            fragmentShader = loadShaderFromDOM("shader-fs");

            // 创建程序并连接着色器
            shaderProgram = gl.createProgram();
            gl.attachShader(shaderProgram, vertexShader);
            gl.attachShader(shaderProgram, fragmentShader);
            gl.linkProgram(shaderProgram);

            // 连接失败的检测
            if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
                alert("Failed to setup shaders");
            }

            // 使用着色器
            gl.useProgram(shaderProgram);

            // 创建动态属性获取着色器中 aVertexPosition 属性的位置
            shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
        }

        function loadShaderFromDOM(id) {
            // 获取 DOM
            var shaderScript = document.getElementById(id);

            if (!shaderScript) {
                return null;
            }

            // 获取着色器代码
            var shaderSource = "";
            var currentChild = shaderScript.firstChild;
            while (currentChild) {
                if (currentChild.nodeType == 3) { // 3 corresponds to TEXT_NODE
                    shaderSource += currentChild.textContent;
                }
                currentChild = currentChild.nextSibling;
            }

            // 创建着色器
            var shader;
            if (shaderScript.type == "x-shader/x-fragment") {
                shader = gl.createShader(gl.FRAGMENT_SHADER);
            } else if (shaderScript.type == "x-shader/x-vertex") {
                shader = gl.createShader(gl.VERTEX_SHADER);
            } else {
                return null;
            }

            // 编译着色器
            gl.shaderSource(shader, shaderSource);
            gl.compileShader(shader);

            // 判断编译是否成功
            if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
                alert(gl.getShaderInfoLog(shader));
                return null;
            }
            return shader;
        }

        function setupBuffers() {
            // 创建顶点缓冲
            vertexBuffer = gl.createBuffer();
            // 绑定顶点缓冲
            gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
            // 定义顶点数据
            var triangleVertices = [
                -0.5, 0.5, 0.0,0.0,0.0,0.0,
                0.5, 0.5, 0.0,0.0,0.0,0.0,
                0.5, -0.5, 0.0,0.0,0.0,0.0,

               -0.5, 0.5, 0.0,0.0,0.0,0.0,
                0.5, -0.5, 0.0,0.0,0.0,0.0,
                -0.5, -0.5, 0.0,0.0,0.0,0.0,             
            ];
            // 提交顶点数据
            gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(triangleVertices), gl.STATIC_DRAW);                     
        }

        function draw() {
            // 设置视口大小, 使用像素, 调整该大小不影响显示内容,只影响图像在 Canvas 上显示的位置和尺寸
            gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
            // 清除颜色缓冲
            gl.clear(gl.COLOR_BUFFER_BIT);
            // 将提交的顶点数据绑定到着色器的 aVertexPosition 属性
            gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, 3, gl.FLOAT, false, 4*6, 0);
            // 开启属性 aVertexPosition 的使用
            gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
            // 绘制图像
            gl.drawArrays(gl.TRIANGLES, 0, 6);
        }

        function startup() {
            canvas = document.getElementById("myGLCanvas");
            gl = createGLContext(canvas);
            setupShaders();
            setupBuffers();
            // 定义清除颜色缓冲之后的填充色为黑色
            gl.clearColor(0.8, 0.4, 0.0, 0.5);
            draw();
        }
    </script>
</head>
<body onload="startup();">
    <canvas id="myGLCanvas" width="500" height="500"></canvas>
</body>
</html>

webgl绘制矩形_第1张图片

你可能感兴趣的:(WebGl)