首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WebGL画点程序v3

WebGL画点程序v3

作者头像
步行者08
发布2018-10-09 18:03:29
7390
发布2018-10-09 18:03:29
举报

本文程序实现画一个点的任务,如下图。其中,点的颜色由Javascript传到片元着色器程序中。

Hello_Point
Hello_Point

整个程序包含两个文件,分别是:

1. HelloPoint3.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>画一个点</title>
    </head>
    <body onload="startup()">
        <canvas id="myGLCanvas" width="640" height="480">
        </canvas>
    </body>
    <script type="text/javascript" src="HelloPoint3.js">
    </script>
</html>

2. HelloPoint3.js

var gl;
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]); //获取webgl context绘图上下文
    } 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 loadShader(type, shaderSource) {
  var shader = gl.createShader(type);
  gl.shaderSource(shader, shaderSource);
  gl.compileShader(shader);

  if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
      alert("Error compiling shader" + gl.getShaderInfoLog(shader));
      gl.deleteShader(shader);   
      return null;
  }
  return shader;  
}

function setupShaders() {
    //顶点着色器程序
    var vertexShaderSource = 
      'attribute vec4 a_Position;\n' + // attribute variable
      'void main() {\n' +
      '  gl_Position = a_Position;\n' +
      '  gl_PointSize = 10.0;\n' +
      '}\n'; 

    //片元着色器程序
    var fragmentShaderSource = 
      'precision mediump float;\n' +
      'uniform vec4 u_FragColor;\n' +  // uniform変量
      'void main() {\n' +
      '  gl_FragColor = u_FragColor;\n' +
      '}\n';                                        

  var vertexShader = loadShader(gl.VERTEX_SHADER, vertexShaderSource);
  var fragmentShader = loadShader(gl.FRAGMENT_SHADER, fragmentShaderSource);

  var 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);
  gl.program= shaderProgram;
}

function startup(){
    var canvas = document.getElementById('myGLCanvas');//获取<canvas>元素
    gl = createGLContext(canvas);
    setupShaders(); 

    // Get the storage location of a_Position
    var a_PosLocation = gl.getAttribLocation(gl.program, 'a_Position');
    if (a_PosLocation < 0) {
      console.log('Failed to get the storage location of a_Position');
      return;
    }

    // Get the storage location of u_FragColor
    var u_FragColorLocation = gl.getUniformLocation(gl.program, 'u_FragColor');
    if (!u_FragColorLocation) {
      console.log('Failed to get the storage location of u_FragColor');
      return;
    }

    // Pass vertex position to attribute variable
    gl.vertexAttrib3f(a_PosLocation, 0.0, 0.0, 0.0);
    // Pass the color of a point to u_FragColor variable
    gl.uniform4f(u_FragColorLocation, 1.0, 1.0, 0.0, 1.0);

    gl.clearColor(0.0, 0.0, 0.0, 1.0);//指定清空<canvas>的颜色    
    gl.clear(gl.COLOR_BUFFER_BIT);//清空<canvas>
    gl.drawArrays(gl.POINTS, 0, 1);//从第0个元素开始,在指定位置(gl_Position)画1个点
 }

参考代码

  1. Hello Point——WebGL, http://www.cnblogs.com/idealer3d/p/3513838.html
  2. Professional WebGL Programming: Developing 3D Graphics for the Web,Listing 2-1,http://media.wiley.com/product_ancillary/60/11199688/DOWNLOAD/Listing-2-1.html
  3. WebGL Programming Guide, https://sites.google.com/site/webglbook/

转载请注明出处:http://www.cnblogs.com/opengl/p/7269946.html

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年09月18日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. HelloPoint3.html
  • 2. HelloPoint3.js
  • 参考代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档