zoukankan      html  css  js  c++  java
  • webgl动画小测试

    // MultiPoint.js (c) 2012 matsuda
    // Vertex shader program
    var VSHADER_SOURCE =
      'attribute vec4 a_Position;
    ' +
      'void main() {
    ' +
      '  gl_Position = a_Position;
    ' +
      '  gl_PointSize = 10.0;
    ' +
      '}
    ';
    
    // Fragment shader program
    var FSHADER_SOURCE =
      'precision mediump float;
    ' +
      'void main() {
    ' +    // Center coordinate is (0.5, 0.5)
      '  float d = distance(gl_PointCoord, vec2(0.5, 0.5));
    ' +
      '  if(d < 0.6) {
    ' +  // Radius is 0.5
      '    gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
    ' +
      '  } else {discard;}
    ' +
      '}
    ';
    
    function main() {
      // Retrieve <canvas> element
      var canvas = document.getElementById('webgl');
    
      // Get the rendering context for WebGL
      var gl = getWebGLContext(canvas);
      if (!gl) {
        console.log('Failed to get the rendering context for WebGL');
        return;
      }
    
      // Initialize shaders
      if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
        console.log('Failed to intialize shaders.');
        return;
      }
    
      // Write the positions of vertices to a vertex shader
      var n = initVertexBuffers(gl);
      if (n < 0) {
        console.log('Failed to set the positions of the vertices');
        return;
      }
    
      // Specify the color for clearing <canvas>
      gl.clearColor(0, 0, 0, 1);
    
      // Clear <canvas>
      gl.clear(gl.COLOR_BUFFER_BIT);
    
      // Draw three points
    
    var g_init = Date.now();
    var speed = 20;
    function pointCount() {
        var now = Date.now();
        var count = (now - g_init) * (speed * 0.001 )
        if (count > n) {
          return n
        }
        else if (count<1){
          return 1;
        }
        else {
          return count;
        }
      }
    
    var tick = function() {
      gl.clear(gl.COLOR_BUFFER_BIT);
      gl.drawArrays(gl.POINTS, 0, pointCount());
      requestAnimationFrame(tick);
    };
    tick();
    }
    
    
    
    function initVertexBuffers(gl) {
      var vertices = new Float32Array([
        -0.9, 0.0, -0.8, 0.0, -0.7, 0.0,
        -0.6, 0.0, -0.5, 0.0, -0.4, 0.0,
        -0.3, 0.0, -0.2, 0.0, -0.1, 0.0,
        0.0, 0.0,
        0.0, -0.1, 0.0, -0.2, 0.0, -0.3,
        0.0, -0.4, 0.0, -0.5, 0.0, -0.6,
        0.0, -0.7, 0.0, -0.8, 0.0, -0.9
      ]);
      var n = 19; // The number of vertices
    
      // Create a buffer object
      var vertexBuffer = gl.createBuffer();
      if (!vertexBuffer) {
        console.log('Failed to create the buffer object');
        return -1;
      }
    
      // Bind the buffer object to target
      gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
      // Write date into the buffer object
      gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
    
      var a_Position = gl.getAttribLocation(gl.program, 'a_Position');
      if (a_Position < 0) {
        console.log('Failed to get the storage location of a_Position');
        return -1;
      }
      // Assign the buffer object to a_Position variable
      gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);
    
      // Enable the assignment to a_Position variable
      gl.enableVertexAttribArray(a_Position);
    
      return n;
    }
    

      

  • 相关阅读:
    shell脚本中执行python脚本并接收其返回值的例子
    linux查找所有文件中某个字符串
    Shell脚本中单引号(‘)和双引号(“)的使用区别
    第一个shell脚本
    shell 比较符号
    source ~/.bash_profile是什么意思
    bash shell:获取当前脚本的绝对路径(pwd/readlink)
    poj 3307 Smart Sister 打表解因子生成数问题
    Python将JSON格式数据转换为SQL语句以便导入MySQL数据库
    UISegmentedControl的具体使用
  • 原文地址:https://www.cnblogs.com/shenwenkai/p/6247604.html
Copyright © 2011-2022 走看看