var VSHADER_SOURCE =
'attribute vec4 a_Position;\n' +
'attribute vec4 a_Color;\n' +
'uniform mat4 u_MvpMatrix;\n' +
'varying vec4 v_Color;\n' +
'void main() {\n' +
' gl_Position = u_MvpMatrix * a_Position;\n' +
' v_Color = a_Color;\n' +
'}\n'; // Fragment shader program
var FSHADER_SOURCE =
'#ifdef GL_ES\n' +
'precision mediump float;\n' +
'#endif\n' +
'varying vec4 v_Color;\n' +
'void main() {\n' +
' gl_FragColor = v_Color;\n' +
'}\n'; 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;
} // Set the vertex coordinates and color (the blue triangle is in the front)
var n = initVertexBuffers(gl);
if (n < ) {
console.log('Failed to set the vertex information');
return;
} // Specify the color for clearing <canvas>
gl.clearColor(, , , );
  
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
//开启深度检测(基于Z轴,前面的自动遮住后面的,这样不用在意图形先后绘制决定层级的问题)
gl.enable(gl.DEPTH_TEST);
//开启多边形偏移解决不同物体Z轴值一样的问题(基于所在面与视区的角度设置z轴偏移量)
gl.enable(gl.POLYGON_OFFSET_FILL); // Get the storage location of u_MvpMatrix
var u_MvpMatrix = gl.getUniformLocation(gl.program, 'u_MvpMatrix');
if (!u_MvpMatrix) {
console.log('Failed to get the storage location of u_MvpMatrix');
return;
} var modelMatrix = new Matrix4(); // Model matrix
var viewMatrix = new Matrix4(); // View matrix
var projMatrix = new Matrix4(); // Projection matrix
var mvpMatrix = new Matrix4(); // Model view projection matrix   
//模型矩阵,用于计算模型的运动(平移、旋转、缩放)
modelMatrix.setTranslate(0.75, , );
//视图矩阵(人看物体的位置)
viewMatrix.setLookAt(, , , , , -, , , );
//透视投影矩阵(相对应的还有正视投影,但是3D下一般用透视投影,看起来更符合人类视角)
projMatrix.setPerspective(, canvas.width/canvas.height, , );
//上述三种矩阵相乘
mvpMatrix.set(projMatrix).multiply(viewMatrix).multiply(modelMatrix);
gl.uniformMatrix4fv(u_MvpMatrix, false, mvpMatrix.elements);
gl.clear(gl.COLOR_BUFFER_BIT); // Clear <canvas>
gl.drawArrays(gl.TRIANGLES, , n); // Draw the triangles   
//绘制之前设置多边形偏移参数值
gl.polygonOffset(1.0, 1.0); //几何体位置移动再画一次(放到右边)
modelMatrix.setTranslate(-0.75, , );
mvpMatrix.set(projMatrix).multiply(viewMatrix).multiply(modelMatrix);
gl.uniformMatrix4fv(u_MvpMatrix, false, mvpMatrix.elements);
gl.drawArrays(gl.TRIANGLES, , n); // Draw the triangles
} function initVertexBuffers(gl) {
  
var verticesColors = new Float32Array([
// Vertex coordinates and color
0.0, 1.0, -4.0, 0.4, 1.0, 0.4, // The back green one
-0.5, -1.0, -4.0, 0.4, 1.0, 0.4,
0.5, -1.0, -4.0, 1.0, 0.4, 0.4, 0.0, 1.0, -2.0, 1.0, 1.0, 0.4, // The middle yellow one
-0.5, -1.0, -2.0, 1.0, 1.0, 0.4,
0.5, -1.0, -2.0, 1.0, 0.4, 0.4, 0.0, 1.0, 0.0, 0.4, 0.4, 1.0, // The front blue one
-0.5, -1.0, 0.0, 0.4, 0.4, 1.0,
0.5, -1.0, 0.0, 1.0, 0.4, 0.4,
]);
var n = ; // Create a buffer object
var vertexColorBuffer = gl.createBuffer();
if (!vertexColorBuffer) {
console.log('Failed to create the buffer object');
return -;
} // Write the vertex information and enable it
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 < ) {
console.log('Failed to get the storage location of a_Position');
return -;
}
gl.vertexAttribPointer(a_Position, , gl.FLOAT, false, FSIZE * , );
gl.enableVertexAttribArray(a_Position); var a_Color = gl.getAttribLocation(gl.program, 'a_Color');
if(a_Color < ) {
console.log('Failed to get the storage location of a_Color');
return -;
}
gl.vertexAttribPointer(a_Color, , gl.FLOAT, false, FSIZE * , FSIZE * );
gl.enableVertexAttribArray(a_Color); return n;
}

注意点:

①通过矩阵处理position

  模型矩阵:物体的运动(平移、旋转、缩放);

  视图矩阵:设置人的视角,也就是第一人称视角;

  透视投影矩阵:处理物体的远近,远的物体小,近的物体大;

②开启隐藏面消除

  因为webgl渲染的图形的时候,同一个像素点上,后一个绘制的会把前一个绘制的遮盖;

  开启隐藏面消除之后,不用担心这个顺序问题,将由Z轴值决定谁应该被隐藏(遮住);

③开启多边形偏移

  当不同物体处于同一平面Z轴相同时,就会出现深度冲突,使得物体表面看上去斑斑驳驳的;

  开启多边形偏移之后,会根据物体所在的面和视角所形成的夹角生成一个偏移量附加在物体Z轴参数上,从而解决深度冲突;

  绘制图形前要设置多边形偏移参数值。

④图形变形问题

  在正视投影的视角下,可视区如果比图形高度小的话,图片就会被压缩

WebGL编程指南案例解析之3D视图视区问题的更多相关文章

  1. WebGL编程指南案例解析之绘制一个点

    <!DOCTYPE html> <html> <head> <title>webgl</title> <style type=&quo ...

  2. WebGL编程指南案例解析之平移和旋转的矩阵实现

    手写各种矩阵: //矩阵 var vShader = ` attribute vec4 a_Position; uniform mat4 u_xformMatrix; void main(){ gl_ ...

  3. WebGL编程指南案例解析之绘制三个点

    //案例2.绘制3个点,将顶点数据存到缓冲区对象(gl.ARRAY_BUFFER)中,然后顶点着色器从里面读数据(3个顶点) //着色器将对这些顶点进行逐个解析, //第一个顶点给到顶点着色器,赋值给 ...

  4. WebGL编程指南案例解析之绘制四边形

    //案例4,绘制矩形,和三角形类似,但是注意因为一个矩形有4个顶点,按照两个三角形绘制矩形的话,顶点顺序要注意 var vShader = ` attribute vec4 a_Position; v ...

  5. WebGL编程指南案例解析之绘制三角形

    //案例3.绘制三角形,将顶点数据存到缓冲区对象(gl.ARRAY_BUFFER)中,然后顶点着色器从里面读数据(3个顶点) //顶点着色器中去掉gl_PointSize = 10.0,绘制三角不能设 ...

  6. WebGL编程指南案例解析之纹理叠加

    var vShader = ` attribute vec4 a_Position; attribute vec2 a_TexCoord; varying vec2 v_TexCoord; void ...

  7. WebGL编程指南案例解析之加载纹理(贴图)

    var vShader = ` attribute vec4 a_Position; attribute vec2 a_TexCoord; varying vec2 v_TexCoord; void ...

  8. WebGL编程指南案例解析之多数据存储于一个缓冲区以及着色器通信

    //顶点着色器往片元着色器传值 //多个参数值存于一个缓冲对象中 var vShader = ` attribute vec4 a_Position; attribute float a_PointS ...

  9. WebGL编程指南案例解析之平移和旋转的math库实现

    这里说的math库实现,指的是,通过一般的加减乘除(角度计算)来更新坐标值. 因为涉及到坐标的变化,所以这里都是基于对顶点着色器的修改 平移: var vShader = ` attribute ve ...

随机推荐

  1. MVC ---- EF批处理

    #region 批处理 ///<summary> ///两增一删一改 ///</summary> public void Save(){ //新增参一 Parameter pa ...

  2. Ubuntu下配置JDK

    1. 首先你需要到oracle官网下载最新版本的JDK.跑到oracle官网,自己到Download下面找找吧 2.转到下载路径,对下载后的文件解压缩,比如我下载的文件名为jdk-7u7-linux- ...

  3. ubuntu 14.04 (desktop amd 64) 安装和配置ROS Indigo

    安装ROS 配置Ubuntu的软件源 配置Ubuntu要求允许接受restricted.universe和multiverse的软件源,可以根据下面的链接配置: https://help.ubuntu ...

  4. python 本地化 local

    locale 模块提供了 C 本地化( localization )函数的接口, 如 Example 8-1 所示. 同时提供相关函数, 实现基于当前 locale 设置的数字, 字符串转换. (而 ...

  5. Mutex, semaphore, spinlock

    Mutex是一把钥匙,一个人拿了就可进入一个房间,出来的时候把钥匙交给队列的第一个.一般的用法是用于串行化对critical section代码的访问,保证这段代码不会被并行的运行. Semaphor ...

  6. 《剑指offer》第十九题(正则表达式匹配)

    // 面试题19:正则表达式匹配 // 题目:请实现一个函数用来匹配包含'.'和'*'的正则表达式.模式中的字符'.' // 表示任意一个字符,而'*'表示它前面的字符可以出现任意次(含0次).在本题 ...

  7. jq 插件写法

    1.一次声明一个函数 $.fn.函数名 = function([options]){} $.fn.red=function(options){ var defaults = { 'color': 'r ...

  8. Grasshopper操作shp

    1 shp文件组件 提示ACE.OLEDB  未注册. 需要安装acess控件,https://www.microsoft.com/zh-CN/download/details.aspx?id=132 ...

  9. cookie session localstorage sessionStorage区别

    cookie:http://www.cnblogs.com/Darren_code/archive/2011/11/24/Cookie.html 重要特点: 1.cookie 有大小设置,有过期时间设 ...

  10. 解决dos窗口乱码问题

    大家有没有遇到这样的情况,看着就糟心 打开dos窗口, 输入命令 chcp 936 (936表示中文编码GBK, 也可以设置其他编码), 回车一下执行.  鼠标右键 -> 属性  (关键一步): ...