• webgl 深度缓冲


    传统的画2d画布就是后画的会盖在先画的上面,但是在画一些三维图形时,这很难控制

    深度缓冲区的作用就是区分颜色所在的层次,防止把被遮挡住的颜色显示出来。

    深度缓冲很强大,用起来很简单

    开启深度缓冲(测试),注意是测试
    gl.enable(gl.DEPTH_TEST);
     
    清除深度缓存
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
     
     
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8"/>
        <title>Hidden Surface Removal & Depth Test</title>
    </head>
    <body>
    
    <canvas id="canvas" width="400" height="400">
        Please use a browser that supports "canvas"
    </canvas>
    
    <script id="vertex-shader" type="glsl">
        attribute vec4 a_Position;
        attribute vec4 a_Color;
        uniform mat4 u_ProjMatrix;
        uniform mat4 u_ViewMatrix;
        varying vec4 v_Color;
    void main() { gl_Position = u_ProjMatrix * u_ViewMatrix * 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 vertices = new Float32Array([ // Three triangles on the right side 0.75, 1.0, 0.0, 0.4, 0.4, 1.0, // 前面的蓝色三角形 0.25, -1.0, 0.0, 0.4, 0.4, 1.0, 1.25, -1.0, 0.0, 1.0, 0.4, 0.4, 0.75, 1.0, -2.0, 1.0, 1.0, 0.4, // 中间的黄色三角形 0.25, -1.0, -2.0, 1.0, 1.0, 0.4, 1.25, -1.0, -2.0, 1.0, 0.4, 0.4, 0.75, 1.0, -4.0, 0.4, 1.0, 0.4, // 后面的绿色三角形 0.25, -1.0, -4.0, 0.4, 1.0, 0.4, 1.25, -1.0, -4.0, 1.0, 0.4, 0.4, // Three triangles on the left side -0.75, 1.0, 0.0, 0.4, 0.4, 1.0, // 前面的蓝色三角形 -1.25, -1.0, 0.0, 0.4, 0.4, 1.0, -0.25, -1.0, 0.0, 1.0, 0.4, 0.4, -0.75, 1.0, -2.0, 1.0, 1.0, 0.4, // 中间的黄色三角形 -1.25, -1.0, -2.0, 1.0, 1.0, 0.4, -0.25, -1.0, -2.0, 1.0, 0.4, 0.4, -0.75, 1.0, -4.0, 0.4, 1.0, 0.4, // 后面的绿色三角形 -1.25, -1.0, -4.0, 0.4, 1.0, 0.4, -0.25, -1.0, -4.0, 1.0, 0.4, 0.4 ]); initVertexBuffers(gl, vertices); var u_ViewMatrix = gl.getUniformLocation(gl.program, 'u_ViewMatrix'); var u_ProjMatrix = gl.getUniformLocation(gl.program, 'u_ProjMatrix'); var viewMatrix = new Matrix4(); viewMatrix.setLookAt(0, 0, 5, 0, 0, -100, 0, 1, 0); var projMatrix = new Matrix4(); projMatrix.setPerspective(30, canvas.width / canvas.height, 1, 100); gl.uniformMatrix4fv(u_ViewMatrix, false, viewMatrix.elements); gl.uniformMatrix4fv(u_ProjMatrix, false, projMatrix.elements); //开启后,则按照z值排序 // 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.drawArrays(gl.TRIANGLES, 0, 18); function initVertexBuffers(gl, vertices) { var vertexBuffer = gl.createBuffer(); if (!vertexBuffer) { console.log('Failed to create buffer object'); return -1; } gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer); gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); var FSIZE = vertices.BYTES_PER_ELEMENT; var a_Position = gl.getAttribLocation(gl.program, 'a_Position'); gl.vertexAttribPointer(a_Position, 3, gl.FLOAT, false, 6 * FSIZE, 0); gl.enableVertexAttribArray(a_Position); var a_Color = gl.getAttribLocation(gl.program, 'a_Color'); gl.vertexAttribPointer(a_Color, 3, gl.FLOAT, false, 6 * FSIZE, 3 * FSIZE); gl.enableVertexAttribArray(a_Color); } </script> </body> </html>

      

     
  • 相关阅读:
    Android Studio遇到了“No USB devices or running emulators detected”
    (转)Android Studio启动AVD遇到的问题 ( HAXM安装失败)
    (转)秒懂,Java 注解 (Annotation)你可以这样学 ---- 重要 注解定义与反射解析
    DSL简介(转)
    有什么软件可以让手机使用卫星通信吗?
    Openfire调整成自己的IM部署到LInux系统上
    cpu和gpu的区别
    (转)OpenFire源码学习之二十七:Smack源码解析
    (转)OpenFire源码学习之十八:IOS离线推送
    (转)openfire插件开发(三)通过http方式向openfire客户端发信息
  • 原文地址:https://www.cnblogs.com/honghong87/p/9760174.html
Copyright © 2020-2023  润新知