《WebGL权势巨子编程指南》书籍例题及学习笔记(1~6章)

打印 上一主题 下一主题

主题 1637|帖子 1637|积分 4911

 

   

  • 书名:WebGL Programming Guide
  • 语言:English
  • 作者:Kouichi Matsuda、Rodger Lea
  • 版本:1th Edition
   

  • OS: Mac Catalina 10.15.4 
  • Hardware: Intel Core i9/16G 2667MHz DDR4
  • 编译器版本:vscode
  • 浏览器:Chrome
  
Chapter 1 WebGL简述

相识WebGL起源,如下图:

相识WebGL架构方式,如下图:
  
Chapter 2 WebGL的第一步

Example 1 使用<canvas>构建图形

DrawRectangle.html 如下:
  1. <!DOCTYPE html>
  2. <html lang="eng">
  3.     <head>
  4.         <meta charset="utf-8"/>
  5.         <title>
  6.             Draw a blue rectangle (canvas version)
  7.         </title>
  8.     </head>
  9.     <body onload="main()">
  10.         <canvas id = "example" width="400" height="400">
  11.             Please use a browser that supports "canvas"
  12.         </canvas>
  13.         <script src="DrawRectangle.js">
  14.         </script>
  15.     </body>
  16. </html>
复制代码
DrawRectangle.js 如下:
  1. // DrawRectangle.js
  2. function main() {
  3.     // Rectriee <canvas> element
  4.     var canvas = document.getElementById("example");
  5.     if (!canvas) {
  6.         console.log("Failed to retrieve the <canvas> element");
  7.         return;
  8.     }
  9.     // Get the rendering context for 2DCG
  10.     var ctx = canvas.getContext("2d");
  11.     // Draw a blue rectangle
  12.     ctx.fillStyle = 'rgba(0, 0, 255, 1.0)'; // Set a blue color
  13.     ctx.fillRect(120, 10, 150, 150); // Fill a rectangle with the color
  14. }
复制代码
运行结果如下图:

    注意:
  

  • 这是一个使用<canvas>的例子
  • 坐标左上角为(0,0),x轴往右为正,y轴往下为正,如下图:

  Example 2 WebGL Program: Clear Drawing Area

HelloCanvas.html 如下:
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.     <head>
  4.         <meta charset="utf-8"/>
  5.         <title> Clear canvas </title>
  6.     </head>
  7.     <body onload="main()">
  8.         <canvas id = "webgl" width="400" height="400">
  9.             Please use the browser supporting "canvas"
  10.         </canvas>
  11.         
  12.         <script src="../../lib/webgl-utils.js"></script>
  13.         <script src="../../lib/webgl-debug.js"></script>
  14.         <script src="../../lib/cuon-utils.js"></script>
  15.         <script src="HelloCanvas.js"></script>
  16.     </body>
  17. </html>
复制代码
HelloCanvas.js 如下:
  1. // HelloCanvas.js
  2. function main() {
  3.     // Retrieve <canvas> element
  4.     var canvas = document.getElementById("webgl");
  5.     // Get the rendering context for WebGL
  6.     var gl = getWebGLContext(canvas);
  7.     if(!gl) {
  8.         console.log('Failed to get the rendering context for WebGL');
  9.         return;
  10.     }
  11.     // Spectify the color for clearing <canvas>
  12.     gl.clearColor(0.0, 0.0, 0.0, 1.0);
  13.     // Clear <canvas>
  14.     gl.clear(gl.COLOR_BUFFER_BIT);
  15. }
复制代码
步伐输出结果:

    注意:
  

  • 步伐调用了webgl库文件
  • main()处置处罚流程如下图:
  

  Example 3 绘制点(版本1)

HelloPoint1.html 如下:
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.     <head>
  4.         <meta charset="utf-8"/>
  5.         <title>Draw a point (1)</title>
  6.     </head>
  7.     <body onload="main()">
  8.         <canvas id = "webgl" width="400" height="400">
  9.             Please use the browser supporting "canvas".
  10.         </canvas>
  11.         <script src="../../lib/webgl-utils.js"></script>
  12.         <script src="../../lib/webgl-debug.js"></script>
  13.         <script src="../../lib/cuon-utils.js"></script>
  14.         <script src="HelloPoint1.js"></script>
  15.     </body>
  16. </html>
复制代码
HelloPoint1.js 如下:
  1. // HelloPoint1.js
  2. // Vertex shader program
  3. var VSHADER_SOURCE =
  4.     ' void main() {\n' +
  5.     ' gl_Position = vec4(0.0, 0.0, 0.0, 1.0); \n' + // Coordinates
  6.     ' gl_PointSize = 10.0;\n' +                     // Set the point size
  7.     ' }\n';
  8. // Frame shader program
  9. var FSHADER_SOURCE =
  10.     'void main() {\n' +
  11.     '   gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n' + // Set the color
  12.     '}\n';
  13. function main() {
  14.     // Retrieve <canvas> element
  15.     var canvas = document.getElementById("webgl");
  16.     // Get the rendering context for WebGL
  17.     var gl = getWebGLContext(canvas, true);
  18.     if(!gl) {
  19.         console.log('Failed to get the rendering context for WebGL');
  20.         return;
  21.     }
  22.     // Initialize shaders
  23.     if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
  24.         console.log('Failed to initialize shaders.');
  25.         return;
  26.     }
  27.     // Spectify the color for clearing <canvas>
  28.     gl.clearColor(0.0, 0.0, 0.0, 1.0);
  29.     // Clear <canvas>
  30.     gl.clear(gl.COLOR_BUFFER_BIT);
  31.     // Draw a point
  32.     gl.drawArrays(gl.POINTS, 0, 1);
  33. }
复制代码
步伐运行结果:

 在Safari浏览器会报INVALID_OPERATION错误,但是在Chrome浏览器运行正常,原因暂时不明,如下图所示:有知道原因的大侠烦请留言告知,谢谢。

JavaScript步伐显示处置处罚流程


WebGL步伐处置处罚流程


 着色器的一样寻常性处置处罚流程


 WebGL坐标体系


右手坐标系 ,手指所指的方向为正向

 <canvas>绘制区域与WebGL坐标体系,注意:<canvas>左上角为原点,使用int标示坐标位置。而WebGL默认原点在中心,且使用float范例标示坐标位置。

Example 4 绘制点(版本2)

两种差别的顶点变量范例: attribute variable和uniform variable


attirbute variable 定义的格式:


属性变量赋值过程:


 示例代码:
  1. // HelloPoint2.js
  2. // Vertex shader program
  3. var VSHADER_SOURCE =
  4.     'attribute vec4 a_Position;\n' +
  5.     'void main() {\n' +
  6.     '  gl_Position = a_Position;\n' +
  7.     '  gl_PointSize = 10.0;\n' +
  8.     '}\n';
  9. // Frame shader program
  10. var FSHADER_SOURCE =
  11.     'void main() {\n' +
  12.     '   gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n' + // Set the color
  13.     '}\n';
  14. function main() {
  15.     // Retrieve <canvas> element
  16.     var canvas = document.getElementById("webgl");
  17.     // Get the rendering context for WebGL
  18.     var gl = getWebGLContext(canvas, true);
  19.     if (!gl) {
  20.         console.log('Failed to get the rendering context for WebGL');
  21.         return;
  22.     }
  23.     // Initialize shaders
  24.     if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
  25.         console.log('Failed to initialize shaders.');
  26.         return;
  27.     }
  28.     // Get the storage location of attribute variable
  29.     var a_Position = gl.getAttribLocation(gl.program, 'a_Position');
  30.     if (a_Position < 0) {
  31.         console.log('Failed to get the storage location of a_Position');
  32.         return;
  33.     }
  34.     // Pass vertext position to attribute variable
  35.     gl.vertexAttrib3f(a_Position, 0.0, 0.0, 0.0);
  36.     // Spectify the color for clearing <canvas>
  37.     gl.clearColor(0.0, 0.0, 0.0, 1.0);
  38.     // Clear <canvas>
  39.     gl.clear(gl.COLOR_BUFFER_BIT);
  40.     // Draw a point
  41.     gl.drawArrays(gl.POINTS, 0, 1);
  42. }
复制代码
html文件与HelloPoint1格式基本相同,除了javascript的文件名不一致。如下:
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.     <head>
  4.         <meta charset="utf-8"/>
  5.         <title>Draw a point (1)</title>
  6.     </head>
  7.     <body onload="main()">
  8.         <canvas id = "webgl" width="400" height="400">
  9.             Please use the browser supporting "canvas".
  10.         </canvas>
  11.         <script src="../../lib/webgl-utils.js"></script>
  12.         <script src="../../lib/webgl-debug.js"></script>
  13.         <script src="../../lib/cuon-utils.js"></script>
  14.         <script src="HelloPoint2.js"></script>
  15.     </body>
  16. </html>
复制代码
步伐运行效果:

 gl.vertexAttribute3f()映射齐次坐标

 gl.vertexAttribute3f()映射齐次坐标时第4个参数默认为1.0,如下图:

 WebGL函数命名规则


Example 5 使用鼠标点击来绘制点

HTML代码如下:
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.     <head>
  4.         <meta charset="utf-8"/>
  5.         <title>Draw a point (1)</title>
  6.     </head>
  7.     <body onload="main()">
  8.         <canvas id = "webgl" width="400" height="400">
  9.             Please use the browser supporting "canvas".
  10.         </canvas>
  11.         <script src="../../lib/webgl-utils.js"></script>
  12.         <script src="../../lib/webgl-debug.js"></script>
  13.         <script src="../../lib/cuon-utils.js"></script>
  14.         <script src="ClickedPoints.js"></script>
  15.     </body>
  16. </html>
复制代码
JAVA Script代码如下:
  1. // ClickedPoints.js
  2. // Vertex shader program
  3. var VSHADER_SOURCE =
  4.     'attribute vec4 a_Position;\n' +
  5.     'attribute float a_PointSize;\n' +
  6.     'void main() {\n' +
  7.     '  gl_Position = a_Position;\n' +
  8.     '  gl_PointSize = a_PointSize;\n' +
  9.     '}\n';
  10. // Frame shader program
  11. var FSHADER_SOURCE =
  12.     'void main() {\n' +
  13.     '   gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n' + // Set the color
  14.     '}\n';
  15. function main() {
  16.     // Retrieve <canvas> element
  17.     var canvas = document.getElementById("webgl");
  18.     // Get the rendering context for WebGL
  19.     var gl = getWebGLContext(canvas, true);
  20.     if (!gl) {
  21.         console.log('Failed to get the rendering context for WebGL');
  22.         return;
  23.     }
  24.     // Initialize shaders
  25.     if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
  26.         console.log('Failed to initialize shaders.');
  27.         return;
  28.     }
  29.     // Get the storage location of attribute variable
  30.     var a_Position = gl.getAttribLocation(gl.program, 'a_Position');
  31.     if (a_Position < 0) {
  32.         console.log('Failed to get the storage location of a_Position');
  33.         return;
  34.     }
  35.     var a_PointSize = gl.getAttribLocation(gl.program, 'a_PointSize');
  36.     // Pass vertext position to attribute variable
  37.     gl.vertexAttrib3f(a_Position, 0.0, 0.0, 0.0);
  38.     gl.vertexAttrib1f(a_PointSize, 10.0);
  39.     // Register function (event handler) to be called on a mouse press
  40.     canvas.onmousedown = function(ev) {
  41.         click(ev, gl, canvas, a_Position);
  42.     };
  43.     // Spectify the color for clearing <canvas>
  44.     gl.clearColor(0.0, 0.0, 0.0, 1.0);
  45.     // Clear <canvas>
  46.     gl.clear(gl.COLOR_BUFFER_BIT);
  47. }
  48. var g_points = []; // The array for a mouse press
  49. function click(ev, gl, canvas, a_Position) {
  50.     var x = ev.clientX; // x coordinate of a mouse pointer
  51.     var y = ev.clientY; // y coordinate of a mouse pointer
  52.     var rect = ev.target.getBoundingClientRect();
  53.     x = ((x - rect.left) - canvas.height / 2) / (canvas.height / 2);
  54.     y = (canvas.width / 2 - (y - rect.top)) / (canvas.width / 2);
  55.     // Store the coordinates to g_points array
  56.     g_points.push(x);
  57.     g_points.push(y);
  58.     // Clear <canvas>
  59.     gl.clear(gl.COLOR_BUFFER_BIT);
  60.     var len = g_points.length;
  61.     for(var i = 0; i < len; i += 2) {
  62.         // Pass the position of a point to a_Position variable
  63.         gl.vertexAttrib3f(a_Position, g_points[i], g_points[i+1], 0.0);
  64.         // Draw a point
  65.         gl.drawArrays(gl.POINTS, 0, 1);
  66.     }
  67. }
复制代码
步伐执行如下图:

 注册变乱处置处罚

<canvas>鼠标点击变乱(包括左键、右键、中键)
canvas.onmousedown = function(ev) { click(ev, gl, canvas, a_Position); };
上例同样也是一个匿名函数,如许的写法可以使变乱调用main()里声明的变量,并通过参数转达的方式转达给click函数
浏览器点击变乱的坐标体系


 WebGL坐标体系与<canvas>坐标体系的区别


坐标转换关系见下图:


 Example 6 改变点击点的颜色

在4个象限中定义差别的颜色,第二和第四象限使用白色,第一象限使用红色,第三象限使用绿色。
使用了片段着色器同一变量(uniform variable)传送点颜色值,当鼠标点击差别象限范围,将对应的颜色指发送给绑定的片段着色器变量。
  1. // ColoredPoints.js
  2. // Vertex shader program
  3. var VSHADER_SOURCE =
  4.     'attribute vec4 a_Position;\n' +
  5.     'void main() {\n' +
  6.     '   gl_Position = a_Position;\n' + // Coordinates
  7.     '   gl_PointSize = 10.0;\n' +       // Set the point size
  8.     '}\n';
  9. // Fragment shader program
  10. var FSHADER_SOURCE =
  11.     'precision mediump float;\n' +
  12.     'uniform vec4 u_FragColor;\n' + // uniform variable
  13.     'void main() {\n' +
  14.     '   gl_FragColor = u_FragColor;\n' +   // Set the color
  15.     '}\n';
  16. function main() {
  17.     // Retrieve <canvas> element
  18.     var canvas = document.getElementById('webgl');
  19.     // Get the rendering context for WebGL
  20.     var gl = getWebGLContext(canvas);
  21.     if (!gl) {
  22.         console.log('Failed to get the rendering context for WebGL');
  23.         return;
  24.     }
  25.     // Initialize shaders
  26.     if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
  27.         console.log('Failed to initialize shaders.');
  28.         return;
  29.     }
  30.     // Get the storage location of attribute variable
  31.     var a_Position = gl.getAttribLocation(gl.program, 'a_Position');
  32.     if (a_Position < 0) {
  33.         console.log('Failed to get the storage location of a_Position');
  34.         return;
  35.     }
  36.    
  37.     // Get the storage location of u_FragColor variable
  38.     var u_FragColor = gl.getUniformLocation(gl.program, 'u_FragColor');
  39.     if (!u_FragColor) {
  40.         console.log('Failed to get the storage location of u_FragColor');
  41.         return;
  42.     }
  43.     // Register function (event handler) to be called on mouse press
  44.     canvas.onmousedown = function(ev) {
  45.         click(ev, gl, canvas, a_Position, u_FragColor);
  46.     }
  47.     // Specify the color for clearing <canvas>
  48.     gl.clearColor(0.0, 0.0, 0.0, 1.0);
  49.    
  50.     // Clear <canvas>
  51.     gl.clear(gl.COLOR_BUFFER_BIT);
  52. }
  53. var g_points = []; // The array of a mouse press
  54. var g_colors = []; // The array to store the color of a point
  55. function click(ev, gl, canvas, a_Position, u_FragColor) {
  56.     var x = ev.clientX; // x coordinate of a mouse pointer
  57.     var y = ev.clientY;  // y coordinate of a mouse pointer
  58.     var rect = ev.target.getBoundingClientRect();
  59.     x = ((x - rect.left) - canvas.height / 2) / (canvas.height / 2);
  60.     y = (canvas.width / 2 - (y - rect.top)) / (canvas.width / 2);
  61.     // Store the coordinates to g_Points array
  62.     g_points.push([x, y]);
  63.     // Store the color to g_colors array
  64.     if (x >= 0.0 && y >= 0.0) {     // First quadrant
  65.         g_colors.push([1.0, 0.0, 0.0, 1.0]);    // Red
  66.     } else if (x < 0.0 && y < 0.0) {  // Third quadrant
  67.         g_colors.push([0.0, 1.0, 0.0, 1.0]);    // Green
  68.     } else {                        // Others
  69.         g_colors.push([1.0, 1.0, 1.0, 1.0]);    // White
  70.     }
  71.     // Clear <canvas>
  72.     gl.clear(gl.COLOR_BUFFER_BIT);
  73.     var len = g_points.length;
  74.     for(var i = 0; i < len; i++) {
  75.         var xy = g_points[i];
  76.         var rgba = g_colors[i];
  77.         // Pass the position of a point to a_Position variable
  78.         gl.vertexAttrib3f(a_Position, xy[0], xy[1], 0.0);
  79.         // Pass the color of a point to u_FragColor variable
  80.         gl.uniform4f(u_FragColor, rgba[0], rgba[1], rgba[2], rgba[3]);
  81.         // Draw a point
  82.         gl.drawArrays(gl.POINTS, 0, 1);
  83.     }
  84. }
复制代码
 运行画面截图:

 两种方法转达到片段着色器

Chapter 3 绘制和变换三角形

Example 1 绘制多个点

使用缓冲区对象


  1.     var vertices = new Float32Array([
  2.         0.0, 0.5,   -0.5, -0.5,   0.5, -0.5
  3.     ]);
  4.     var n = 3; // The number of vertices
  5.     // Create a buffer object
  6.     var vertexBuffer = gl.createBuffer();
  7.     if (!vertexBuffer) {
  8.         console.log('Failed to create the buffer object');
  9.         return -1;
  10.     }
  11.     // Bind the buffer object to traget
  12.     gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
  13.     // Write data into the buffer object
  14.     gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
复制代码
使用缓冲区对象的5个步调:

  • 创建缓冲区对象 gl.createBuffer()
  • 绑定缓冲区对象到目的
  • 写入数据到缓冲区对象
  • 赋值缓冲区对象到属性变量
  • 开启匹配
创建缓冲区对象

  1.     // Create a buffer object
  2.     var vertexBuffer = gl.createBuffer();
复制代码

绑定缓冲区对象目的

  1.     // Bind the buffer object to traget
  2.     gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
复制代码

写入数据到缓冲区对象

  1.     // Write data into the buffer object
  2.     gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
复制代码

赋值缓冲区对象到属性变量

  1.     // Assign the buffer object to a_Position variable
  2.     gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);
复制代码

开启匹配

  1.     // Enable the assignment to a_Position variable
  2.     gl.enableVertexAttribArray(a_Position);
复制代码

步伐范例:
  1. // MulitPoint.js// Vertex shader programvar VSHADER_SOURCE =    'attribute vec4 a_Position;\n' +    'void main() {\n' +    '   gl_Position = a_Position;\n' + // Coordinates    '   gl_PointSize = 10.0;\n' +       // Set the point size    '}\n';// Fragment shader programvar FSHADER_SOURCE =    'void main() {\n' +    '   gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n' +   // Set the color    '}\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 initialize shaders.');        return;    }    // Set the positions of vertices    var n = initVertexBuffers(gl);    if (n < 0) {        console.log('Failed to set the positions of the vertices');        return;    }    // Set the color for clearing <canvas>    gl.clearColor(0.0, 0.0, 0.0, 1.0);        // Clear <canvas>    gl.clear(gl.COLOR_BUFFER_BIT);    // Draw a point    gl.drawArrays(gl.POINTS, 0, n);}function initVertexBuffers(gl) {    var vertices = new Float32Array([
  2.         0.0, 0.5,   -0.5, -0.5,   0.5, -0.5
  3.     ]);
  4.     var n = 3; // The number of vertices
  5.     // Create a buffer object
  6.     var vertexBuffer = gl.createBuffer();
  7.     if (!vertexBuffer) {
  8.         console.log('Failed to create the buffer object');
  9.         return -1;
  10.     }
  11.     // Bind the buffer object to traget
  12.     gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
  13.     // Write data into the buffer object
  14.     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;    }    // Assign the buffer object to a_Position variable
  15.     gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);    // Enable the assignment to a_Position variable
  16.     gl.enableVertexAttribArray(a_Position);    return n;}
复制代码
运行结果:

Example 2 绘制三角形

基本图元


步伐示例:
  1. // HelloTriangle.js// Vertex shader programvar VSHADER_SOURCE =    'attribute vec4 a_Position;\n' +    'void main() {\n' +    '   gl_Position = a_Position;\n' + // Coordinates    '}\n';// Fragment shader programvar FSHADER_SOURCE =    'void main() {\n' +    '   gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n' +   // Set the color    '}\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 initialize shaders.');        return;    }    // Set the positions of vertices    var n = initVertexBuffers(gl);    if (n < 0) {        console.log('Failed to set the positions of the vertices');        return;    }    // Set the color for clearing <canvas>    gl.clearColor(0.0, 0.0, 0.0, 1.0);        // Clear <canvas>    gl.clear(gl.COLOR_BUFFER_BIT);    // Draw a point    gl.drawArrays(gl.TRIANGLES, 0, n);}function initVertexBuffers(gl) {    var vertices = new Float32Array([
  2.         0.0, 0.5,   -0.5, -0.5,   0.5, -0.5
  3.     ]);
  4.     var n = 3; // The number of vertices
  5.     // Create a buffer object
  6.     var vertexBuffer = gl.createBuffer();
  7.     if (!vertexBuffer) {
  8.         console.log('Failed to create the buffer object');
  9.         return -1;
  10.     }
  11.     // Bind the buffer object to traget
  12.     gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
  13.     // Write data into the buffer object
  14.     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;    }    // Assign the buffer object to a_Position variable
  15.     gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);    // Enable the assignment to a_Position variable
  16.     gl.enableVertexAttribArray(a_Position);    return n;}
复制代码
运行结果

LINES、LINE_STRIP、LINE_LOOP


Example 3 绘制矩形

使用TRIANGLE_STRIP

步伐示例
  1. // HelloQuand.js// Vertex shader programvar VSHADER_SOURCE =    'attribute vec4 a_Position;\n' +    'void main() {\n' +    '   gl_Position = a_Position;\n' + // Coordinates    '}\n';// Fragment shader programvar FSHADER_SOURCE =    'void main() {\n' +    '   gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n' +   // Set the color    '}\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 initialize shaders.');        return;    }    // Set the positions of vertices    var n = initVertexBuffers(gl);    if (n < 0) {        console.log('Failed to set the positions of the vertices');        return;    }    // Set the color for clearing <canvas>    gl.clearColor(0.0, 0.0, 0.0, 1.0);        // Clear <canvas>    gl.clear(gl.COLOR_BUFFER_BIT);    // Draw a point    gl.drawArrays(gl.TRIANGLE_STRIP, 0, n);}function initVertexBuffers(gl) {    var vertices = new Float32Array([        -0.5, 0.5,    -0.5, -0.5,    0.5, 0.5,    0.5, -0.5    ]);    var n = 4; // The number of vertices    // Create a buffer object
  2.     var vertexBuffer = gl.createBuffer();    if (!vertexBuffer) {        console.log('Failed to create the buffer object');        return -1;    }    // Bind the buffer object to traget
  3.     gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);    // Write data into the buffer object
  4.     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;    }    // Assign the buffer object to a_Position variable
  5.     gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);    // Enable the assignment to a_Position variable
  6.     gl.enableVertexAttribArray(a_Position);    return n;}
复制代码
运行结果

使用TRIANGLE_FAN

  1. // HelloQuandFan.js// Vertex shader programvar VSHADER_SOURCE =    'attribute vec4 a_Position;\n' +    'void main() {\n' +    '   gl_Position = a_Position;\n' + // Coordinates    '}\n';// Fragment shader programvar FSHADER_SOURCE =    'void main() {\n' +    '   gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n' +   // Set the color    '}\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 initialize shaders.');        return;    }    // Set the positions of vertices    var n = initVertexBuffers(gl);    if (n < 0) {        console.log('Failed to set the positions of the vertices');        return;    }    // Set the color for clearing <canvas>    gl.clearColor(0.0, 0.0, 0.0, 1.0);        // Clear <canvas>    gl.clear(gl.COLOR_BUFFER_BIT);    // Draw a point    gl.drawArrays(gl.TRIANGLE_FAN, 0, n);}function initVertexBuffers(gl) {    var vertices = new Float32Array([        -0.5, 0.5,    -0.5, -0.5,    0.5, 0.5,    0.5, -0.5    ]);    var n = 4; // The number of vertices    // Create a buffer object
  2.     var vertexBuffer = gl.createBuffer();    if (!vertexBuffer) {        console.log('Failed to create the buffer object');        return -1;    }    // Bind the buffer object to traget
  3.     gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);    // Write data into the buffer object
  4.     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;    }    // Assign the buffer object to a_Position variable
  5.     gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);    // Enable the assignment to a_Position variable
  6.     gl.enableVertexAttribArray(a_Position);    return n;}
复制代码
 运行结果
 

第2个图形覆盖了第一个图形

Example 4 移动、旋转、缩放

移动

步伐示例:
  1. // TranslatedTriangle.js// Vertex shader programvar VSHADER_SOURCE =    'attribute vec4 a_Position;\n' +    'uniform vec4 u_Translation;\n' +    'void main() {\n' +    '   gl_Position = a_Position + u_Translation;\n' + // Coordinates    '}\n';// Fragment shader programvar FSHADER_SOURCE =    'void main() {\n' +    '   gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n' +   // Set the color    '}\n';// The translation distance for x, y, and z directionvar Tx = 0.5, Ty = 0.5, Tz = 0.0;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 initialize shaders.');        return;    }    // Set the positions of vertices    var n = initVertexBuffers(gl);    if (n < 0) {        console.log('Failed to set the positions of the vertices');        return;    }    // Pass the translation distance to the vertex shader    var u_Translation = gl.getUniformLocation(gl.program, 'u_Translation');    if (u_Translation == null) {        console.log('Failed to get the storage location of u_Translation');        return;       }    gl.uniform4f(u_Translation, Tx, Ty, Tz, 0.0);    // Set the color for clearing <canvas>    gl.clearColor(0.0, 0.0, 0.0, 1.0);        // Clear <canvas>    gl.clear(gl.COLOR_BUFFER_BIT);    // Draw a point    gl.drawArrays(gl.TRIANGLES, 0, n);}function initVertexBuffers(gl) {    var vertices = new Float32Array([
  2.         0.0, 0.5,   -0.5, -0.5,   0.5, -0.5
  3.     ]);
  4.     var n = 3; // The number of vertices
  5.     // Create a buffer object
  6.     var vertexBuffer = gl.createBuffer();
  7.     if (!vertexBuffer) {
  8.         console.log('Failed to create the buffer object');
  9.         return -1;
  10.     }
  11.     // Bind the buffer object to traget
  12.     gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
  13.     // Write data into the buffer object
  14.     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;    }    // Assign the buffer object to a_Position variable
  15.     gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);    // Enable the assignment to a_Position variable
  16.     gl.enableVertexAttribArray(a_Position);    return n;}
复制代码
运行结果:

旋转

绕z轴逆时针旋转β角度


vec4范例成原访问方法:

步伐示例
  1. // RotatedTriangle.js// Vertex shader programvar VSHADER_SOURCE =    // x' = x cos b - y sin b    // y' = x sin b + y cos b    // z' = z    'attribute vec4 a_Position;\n' +    'uniform float u_CosB, u_SinB;\n' +    'void main() {\n' +    '   gl_Position.x = a_Position.x * u_CosB - a_Position.y * u_SinB;\n' +    '   gl_Position.y = a_Position.x * u_SinB + a_Position.y * u_CosB;\n' +    '   gl_Position.z = a_Position.z;\n' +    '   gl_Position.w = 1.0;\n' +    '}\n';// Fragment shader programvar FSHADER_SOURCE =    'void main() {\n' +    '   gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n' +   // Set the color    '}\n';// Rotation anglevar ANGLE = 90.0;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 initialize shaders.');        return; // β α     }    // Set the positions of vertices    var n = initVertexBuffers(gl);    if (n < 0) {        console.log('Failed to set the positions of the vertices');        return;    }    // Pass the data required to rotate the shape to the vertex shader    var radian = Math.PI * ANGLE / 180.0; // Convert to radians    var cosB = Math.cos(radian);    var sinB = Math.sin(radian);    var u_CosB = gl.getUniformLocation(gl.program, 'u_CosB');    var u_SinB = gl.getUniformLocation(gl.program, 'u_SinB');    if(u_CosB == null || u_SinB == null) {        console.log('Failed to get uniform value u_CosB or u_SinB');        return;    }    gl.uniform1f(u_CosB, cosB);    gl.uniform1f(u_SinB, sinB);    // Set the color for clearing <canvas>    gl.clearColor(0.0, 0.0, 0.0, 1.0);        // Clear <canvas>    gl.clear(gl.COLOR_BUFFER_BIT);    // Draw a point    gl.drawArrays(gl.TRIANGLES, 0, n);}function initVertexBuffers(gl) {    var vertices = new Float32Array([
  2.         0.0, 0.5,   -0.5, -0.5,   0.5, -0.5
  3.     ]);
  4.     var n = 3; // The number of vertices
  5.     // Create a buffer object
  6.     var vertexBuffer = gl.createBuffer();
  7.     if (!vertexBuffer) {
  8.         console.log('Failed to create the buffer object');
  9.         return -1;
  10.     }
  11.     // Bind the buffer object to traget
  12.     gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
  13.     // Write data into the buffer object
  14.     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;    }    // Assign the buffer object to a_Position variable
  15.     gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);    // Enable the assignment to a_Position variable
  16.     gl.enableVertexAttribArray(a_Position);    return n;}
复制代码
运行结果:

变换矩阵:旋转

旋转矩阵:

变换矩阵:移动

平移矩阵:

WebGL 使用的是列主矩阵,所以在构建矩阵是,要转置一下,下面为旋转矩阵转置后的代码:
  1.     // Note: WebGL is column major order
  2.     var xformMatrix = new Float32Array([
  3.         cosB, sinB, 0.0, 0.0,
  4.         -sinB, cosB, 0.0, 0.0,
  5.         0.0, 0.0, 1.0, 0.0,
  6.         0.0, 0.0, 0.0, 1.0
  7.     ]);
复制代码
步伐示例:
  1. // RotatedTriangleMatrix.js// Vertex shader programvar VSHADER_SOURCE =    'attribute vec4 a_Position;\n' +    'uniform mat4 u_xformMatrix;\n' +    'void main() {\n' +    '   gl_Position = u_xformMatrix * a_Position;\n' +    '}\n';// Fragment shader programvar FSHADER_SOURCE =    'void main() {\n' +    '   gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n' +   // Set the color    '}\n';// Rotation anglevar ANGLE = 90.0;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 initialize shaders.');        return; //  α     }    // Set the positions of vertices    var n = initVertexBuffers(gl);    if (n < 0) {        console.log('Failed to set the positions of the vertices');        return;    }    // Pass the data required to rotate the shape to the vertex shader    var radian = Math.PI * ANGLE / 180.0; // Convert to radians    var cosB = Math.cos(radian);    var sinB = Math.sin(radian);    // Note: WebGL is column major order
  2.     var xformMatrix = new Float32Array([
  3.         cosB, sinB, 0.0, 0.0,
  4.         -sinB, cosB, 0.0, 0.0,
  5.         0.0, 0.0, 1.0, 0.0,
  6.         0.0, 0.0, 0.0, 1.0
  7.     ]);    // Pass the rotation matrix to the vertex shader    var u_xformMatrix = gl.getUniformLocation(gl.program, 'u_xformMatrix');    if(u_xformMatrix == null) {        console.log('Failed to get uniform value u_CosB or u_SinB');        return;    }    gl.uniformMatrix4fv(u_xformMatrix, false, xformMatrix);    // Set the color for clearing <canvas>    gl.clearColor(0.0, 0.0, 0.0, 1.0);        // Clear <canvas>    gl.clear(gl.COLOR_BUFFER_BIT);    // Draw a point    gl.drawArrays(gl.TRIANGLES, 0, n);}function initVertexBuffers(gl) {    var vertices = new Float32Array([
  8.         0.0, 0.5,   -0.5, -0.5,   0.5, -0.5
  9.     ]);
  10.     var n = 3; // The number of vertices
  11.     // Create a buffer object
  12.     var vertexBuffer = gl.createBuffer();
  13.     if (!vertexBuffer) {
  14.         console.log('Failed to create the buffer object');
  15.         return -1;
  16.     }
  17.     // Bind the buffer object to traget
  18.     gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
  19.     // Write data into the buffer object
  20.     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;    }    // Assign the buffer object to a_Position variable
  21.     gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);    // Enable the assignment to a_Position variable
  22.     gl.enableVertexAttribArray(a_Position);    return n;}
复制代码
运行结果:

变换矩阵:缩放


步伐示例:
  1. // ScalingTriangleMatrix.js// Vertex shader programvar VSHADER_SOURCE =    'attribute vec4 a_Position;\n' +    'uniform mat4 u_xformMatrix;\n' +    'void main() {\n' +    '   gl_Position = u_xformMatrix * a_Position;\n' +    '}\n';// Fragment shader programvar FSHADER_SOURCE =    'void main() {\n' +    '   gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n' +   // Set the color    '}\n';var Sx = 1.0, Sy = 1.5, Sz = 1.0;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 initialize shaders.');        return; //  α     }    // Set the positions of vertices    var n = initVertexBuffers(gl);    if (n < 0) {        console.log('Failed to set the positions of the vertices');        return;    }    // Note: WebGL is column major order    var xformMatrix = new Float32Array([        Sx, 0.0, 0.0, 0.0,        0.0, Sy, 0.0, 0.0,        0.0, 0.0, Sz, 0.0,        0.0, 0.0, 0.0, 1.0    ]);    // Pass the rotation matrix to the vertex shader    var u_xformMatrix = gl.getUniformLocation(gl.program, 'u_xformMatrix');    if(u_xformMatrix == null) {        console.log('Failed to get uniform value u_CosB or u_SinB');        return;    }    gl.uniformMatrix4fv(u_xformMatrix, false, xformMatrix);    // Set the color for clearing <canvas>    gl.clearColor(0.0, 0.0, 0.0, 1.0);        // Clear <canvas>    gl.clear(gl.COLOR_BUFFER_BIT);    // Draw a point    gl.drawArrays(gl.TRIANGLES, 0, n);}function initVertexBuffers(gl) {    var vertices = new Float32Array([
  2.         0.0, 0.5,   -0.5, -0.5,   0.5, -0.5
  3.     ]);
  4.     var n = 3; // The number of vertices
  5.     // Create a buffer object
  6.     var vertexBuffer = gl.createBuffer();
  7.     if (!vertexBuffer) {
  8.         console.log('Failed to create the buffer object');
  9.         return -1;
  10.     }
  11.     // Bind the buffer object to traget
  12.     gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
  13.     // Write data into the buffer object
  14.     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;    }    // Assign the buffer object to a_Position variable
  15.     gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);    // Enable the assignment to a_Position variable
  16.     gl.enableVertexAttribArray(a_Position);    return n;}
复制代码
运行结果:

Chapter 4 更多幻化和基本动画

移动并旋转

组合多个变换:
矩阵变革为逆向进行,如下图:


步伐示例:
  1. // RotatedTranslatedTriangle.js// Vertex shader programvar VSHADER_SOURCE =    'attribute vec4 a_Position;\n' +    'uniform mat4 u_ModelMatrix;\n' +    'void main() {\n' +    '   gl_Position = u_ModelMatrix * a_Position;\n' +    '}\n';// Fragment shader programvar FSHADER_SOURCE =    'void main() {\n' +    '   gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n' +   // Set the color    '}\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 initialize shaders.');        return; //  α     }    // Set the positions of vertices    var n = initVertexBuffers(gl);    if (n < 0) {        console.log('Failed to set the positions of the vertices');        return;    }    // Create Matrix4 object for model transformation    var modelMatrix = new Matrix4();    // Calculate a model matrix    var ANGLE = 60.0; // Rotation angle;    var Tx = 0.5; // Translation distance    //modelMatrix.setTranslate(Tx, 0, 0); // Mulitpy modelMatrix by the calculated translation matrix    //modelMatrix.rotate(ANGLE, 0, 0, 1); // Set rotation matrix    modelMatrix.setRotate(ANGLE, 0, 0, 1); // Set rotation matrix    modelMatrix.translate(Tx, 0, 0); // Mulitpy modelMatrix by the calculated translation matrix                // Pass the rotation matrix to the vertex shader    var u_ModelMatrix = gl.getUniformLocation(gl.program, 'u_ModelMatrix');    if(u_ModelMatrix == null) {        console.log('Failed to get uniform value u_ModelMatrix');        return;    }    gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix.elements);    // Set the color for clearing <canvas>    gl.clearColor(0.0, 0.0, 0.0, 1.0);        // Clear <canvas>    gl.clear(gl.COLOR_BUFFER_BIT);    // Draw a point    gl.drawArrays(gl.TRIANGLES, 0, n);}function initVertexBuffers(gl) {    var vertices = new Float32Array([        0.0, 0.3,   -0.3, -0.3,   0.3, -0.3    ]);    var n = 3; // The number of vertices    // Create a buffer object
  2.     var vertexBuffer = gl.createBuffer();    if (!vertexBuffer) {        console.log('Failed to create the buffer object');        return -1;    }    // Bind the buffer object to traget
  3.     gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);    // Write data into the buffer object
  4.     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;    }    // Assign the buffer object to a_Position variable
  5.     gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);    // Enable the assignment to a_Position variable
  6.     gl.enableVertexAttribArray(a_Position);    return n;}
复制代码
运行结果:

两种矩阵变换次序的比较如下图:

动画

请求浏览器调用tick()
  1.      // Current rotation angle of a triangle
  2.     var currentAngle = 0.0;
  3.     // Create Matrix4 object for model transformation
  4.     var modelMatrix = new Matrix4();
  5.     // Start to draw a triangle
  6.     var tick = function() {
  7.         currentAngle = animate(currentAngle); // Update the rotation angle
  8.         draw(gl, n, currentAngle, modelMatrix, u_ModelMatrix);
  9.         requestAnimationFrame(tick); // Request that the browser calls tick
  10.     };
  11.     tick();
复制代码

步伐示例:
  1. // RotatingTriangle.js// Vertex shader programvar VSHADER_SOURCE =    'attribute vec4 a_Position;\n' +    'uniform mat4 u_ModelMatrix;\n' +    'void main() {\n' +    '   gl_Position = u_ModelMatrix * a_Position;\n' +    '}\n';// Fragment shader programvar FSHADER_SOURCE =    'void main() {\n' +    '   gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n' +   // Set the color    '}\n';// Rotation angle (degrees/second)var ANGLE_STEP = 45.0;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 initialize shaders.');        return; //  α     }    // Set the positions of vertices    var n = initVertexBuffers(gl);    if (n < 0) {        console.log('Failed to set the positions of the vertices');        return;    }    // Set the color for clearing <canvas>    gl.clearColor(0.0, 0.0, 0.0, 1.0);    // Pass the rotation matrix to the vertex shader    var u_ModelMatrix = gl.getUniformLocation(gl.program, 'u_ModelMatrix');    if(u_ModelMatrix == null) {        console.log('Failed to get uniform value u_ModelMatrix');        return;    }    // Current rotation angle of a triangle    var currentAngle = 0.0;    // Create Matrix4 object for model transformation    var modelMatrix = new Matrix4();    // Start to draw a triangle    var tick = function() {        currentAngle = animate(currentAngle); // Update the rotation angle        draw(gl, n, currentAngle, modelMatrix, u_ModelMatrix);        requestAnimationFrame(tick); // Request that the browser calls tick    };    tick();}function initVertexBuffers(gl) {    var vertices = new Float32Array([
  2.         0.0, 0.5,   -0.5, -0.5,   0.5, -0.5
  3.     ]);
  4.     var n = 3; // The number of vertices
  5.     // Create a buffer object
  6.     var vertexBuffer = gl.createBuffer();
  7.     if (!vertexBuffer) {
  8.         console.log('Failed to create the buffer object');
  9.         return -1;
  10.     }
  11.     // Bind the buffer object to traget
  12.     gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
  13.     // Write data into the buffer object
  14.     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;    }    // Assign the buffer object to a_Position variable
  15.     gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);    // Enable the assignment to a_Position variable
  16.     gl.enableVertexAttribArray(a_Position);    return n;}function draw(gl, n, currentAngle, modelMatrix, u_ModelMatrix) {    // Set up rotation matrix    modelMatrix.setRotate(currentAngle, 0, 0, 1);    // Pass the rotation matrix to the vertex shader    gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix.elements);        // Clear <canvas>    gl.clear(gl.COLOR_BUFFER_BIT);    // Draw a point    gl.drawArrays(gl.TRIANGLES, 0, n);}// Last time when this function was calledvar g_last = Date.now();function animate(angle) {    // Calculate the elapsed time    var now = Date.now();    var elapsed = now - g_last; // milliseconds    g_last = now;    // Update the current rotation angle (adjusted by the elapsed time)    var newAngle = angle + (ANGLE_STEP * elapsed) / 1000.0;    return newAngle %= 360;}
复制代码
运行结果:

带按钮的旋转动画

步伐示例:
  1. // RotatingTriangleWithButtons.js// Vertex shader programvar VSHADER_SOURCE =    'attribute vec4 a_Position;\n' +    'uniform mat4 u_ModelMatrix;\n' +    'void main() {\n' +    '   gl_Position = u_ModelMatrix * a_Position;\n' +    '}\n';// Fragment shader programvar FSHADER_SOURCE =    'void main() {\n' +    '   gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n' +   // Set the color    '}\n';// Rotation angle (degrees/second)var ANGLE_STEP = 45.0;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 initialize shaders.');        return; //  α     }    // Set the positions of vertices    var n = initVertexBuffers(gl);    if (n < 0) {        console.log('Failed to set the positions of the vertices');        return;    }    // Set the color for clearing <canvas>    gl.clearColor(0.0, 0.0, 0.0, 1.0);    // Pass the rotation matrix to the vertex shader    var u_ModelMatrix = gl.getUniformLocation(gl.program, 'u_ModelMatrix');    if(u_ModelMatrix == null) {        console.log('Failed to get uniform value u_ModelMatrix');        return;    }    //注册按钮变乱    //旋转速率+    document.getElementById("btnUp").onclick = function(e) {        ANGLE_STEP += 5.0;    };    //旋转速率-    document.getElementById("btnDown").onclick = function(e) {        ANGLE_STEP -= 5.0;    };    // Current rotation angle of a triangle    var currentAngle = 0.0;    // Create Matrix4 object for model transformation    var modelMatrix = new Matrix4();    // Start to draw a triangle    var tick = function() {        currentAngle = animate(currentAngle); // Update the rotation angle        draw(gl, n, currentAngle, modelMatrix, u_ModelMatrix);        requestAnimationFrame(tick); // Request that the browser calls tick    };    tick();}function initVertexBuffers(gl) {    var vertices = new Float32Array([
  2.         0.0, 0.5,   -0.5, -0.5,   0.5, -0.5
  3.     ]);
  4.     var n = 3; // The number of vertices
  5.     // Create a buffer object
  6.     var vertexBuffer = gl.createBuffer();
  7.     if (!vertexBuffer) {
  8.         console.log('Failed to create the buffer object');
  9.         return -1;
  10.     }
  11.     // Bind the buffer object to traget
  12.     gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
  13.     // Write data into the buffer object
  14.     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;    }    // Assign the buffer object to a_Position variable
  15.     gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);    // Enable the assignment to a_Position variable
  16.     gl.enableVertexAttribArray(a_Position);    return n;}function draw(gl, n, currentAngle, modelMatrix, u_ModelMatrix) {    // Set up rotation matrix    modelMatrix.setRotate(currentAngle, 0, 0, 1);    // Pass the rotation matrix to the vertex shader    gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix.elements);        // Clear <canvas>    gl.clear(gl.COLOR_BUFFER_BIT);    // Draw a point    gl.drawArrays(gl.TRIANGLES, 0, n);}// Last time when this function was calledvar g_last = Date.now();function animate(angle) {    // Calculate the elapsed time    var now = Date.now();    var elapsed = now - g_last; // milliseconds    g_last = now;    // Update the current rotation angle (adjusted by the elapsed time)    var newAngle = angle + (ANGLE_STEP * elapsed) / 1000.0;    return newAngle %= 360;}
复制代码
步伐截图:

Chapter 5 使用颜色和纹理图像

转达其它信息到顶点着色器

将顶点坐标缓冲区和点的巨细缓冲区同时赋值给顶点着色器表现图:

示例步伐:
  1. // MulitAttributeSize.js// Vertex shader programvar VSHADER_SOURCE =    'attribute vec4 a_Position;\n' +    'attribute float a_PointSize;\n' +    'void main() {\n' +    '   gl_Position = a_Position;\n' + // Coordinates    '   gl_PointSize = a_PointSize;\n' +       // Set the point size    '}\n';// Fragment shader programvar FSHADER_SOURCE =    'void main() {\n' +    '   gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n' +   // Set the color    '}\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 initialize shaders.');        return;    }    // Set the positions of vertices    var n = initVertexBuffers(gl);    if (n < 0) {        console.log('Failed to set the positions of the vertices');        return;    }    // Set the color for clearing <canvas>    gl.clearColor(0.0, 0.0, 0.0, 1.0);        // Clear <canvas>    gl.clear(gl.COLOR_BUFFER_BIT);    // Draw a point    gl.drawArrays(gl.POINTS, 0, n);}function initVertexBuffers(gl) {    var vertices = new Float32Array([        0.0, 0.5,   -0.5, -0.5,   0.5, -0.5    ]);    var n = 3; // The number of vertices    var sizes = new Float32Array([        10.0, 20.0, 30.0 // Point sizes    ]);    // Create a buffer object
  2.     var vertexBuffer = gl.createBuffer();    var sizeBuffer = gl.createBuffer();    if (!vertexBuffer || !sizeBuffer) {        console.log('Failed to create the buffer object');        return -1;    }    // Bind the buffer object to traget
  3.     gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);    // Write data into the buffer object
  4.     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;    }    // Assign the buffer object to a_Position variable
  5.     gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);    // Enable the assignment to a_Position variable
  6.     gl.enableVertexAttribArray(a_Position);    // Write point sizes to the buffer object and enable it    gl.bindBuffer(gl.ARRAY_BUFFER, sizeBuffer);    gl.bufferData(gl.ARRAY_BUFFER, sizes, gl.STATIC_DRAW);    var a_PointSize = gl.getAttribLocation(gl.program, 'a_PointSize');    if (a_PointSize < 0) {        console.log('Failed to get the storage location of a_PointSize');        return;    }       gl.vertexAttribPointer(a_PointSize, 1, gl.FLOAT, false, 0, 0);    gl.enableVertexAttribArray(a_PointSize);    return n;}
复制代码
运行截图:

使用步长和偏移量将多范例数组发往顶点着色器

将点的巨细混入到点的坐标数组中,代码见下
  1.     var verticesSizes = new Float32Array([
  2.         // Vertex coordinates and size of a point
  3.          0.0,  0.5, 10.0, // The 1st vertex
  4.         -0.5, -0.5, 20.0, // The 2nd vertex
  5.          0.5, -0.5, 30.0  // The 3rd vertex
  6.     ]);
复制代码
其中前两个是点的x、y坐标,第3个是点的巨细
起首创建缓冲区对象,然后将缓冲区对象绑定到gl.ARRAY_BUFFER,末了将数组绑定到缓冲区对象:
  1. var vertexSizeBuffer = gl.createBuffer();
  2. gl.bindBuffer(gl.ARRAY_BUFFER, vertexSizeBuffer);
  3. gl.bufferData(gl.ARRAY_BUFFER, verticesSizes, gl.STATIC_DRAW);
复制代码
取得每个元素的巨细:
  1. var FSIZE = verticesSizes.BYTES_PER_ELEMENT;
复制代码
关联上顶点着色器中的变量:
  1. var a_Position = gl.getAttribLocation(gl.program, 'a_Position');
  2. var a_PointSize = gl.getAttribLocation(gl.program, 'a_PointSize');
复制代码
顶点位置使用3个成员一组步长的方式,偏移量为0(第5和第6个参数):
  1. gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, FSIZE * 3, 0);
复制代码
点的巨细,使用偏移量为2(第5和第6个参数):
  1. gl.vertexAttribPointer(a_PointSize, 1, gl.FLOAT, false, FSIZE * 3, FSIZE * 2);
复制代码
末了分别开启两个缓冲区,使其见效:
  1. gl.enableVertexAttribArray(a_Position);
  2. gl.enableVertexAttribArray(a_PointSize);
复制代码
表现图如下:

webgl内部在使用偏移量的情形下的表现图:

使用可变变量转达到片段着色器

同一变量(uniform)不能转达可变的变量.
定一个含有顶点坐标+颜色值的数组:
  1.     var verticesSizes = new Float32Array([
  2.         // Vertex coordinates and color
  3.          0.0,  0.5, 1.0, 0.0, 0.0, // The 1st vertex
  4.         -0.5, -0.5, 0.0, 1.0, 0.0, // The 2nd vertex
  5.          0.5, -0.5, 0.0, 0.0, 1.0  // The 3rd vertex
  6.     ]);
复制代码
其中,每行的3,4,5项分别表现颜色的R,G,B的值,第1,2项为坐标值。
使用偏移量来区分写入缓冲区的位置:
  1. gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, FSIZE * 5, 0);
  2. gl.vertexAttribPointer(a_Color, 3, gl.FLOAT, false, FSIZE * 5, FSIZE * 2);
复制代码
第1举动位置缓冲区,占用2个元素,gl.FLOAT范例,不进行归一化,5个一组,偏移量0
第2举动位置缓冲区,占用3个元素,gl.FLOAT范例,不进行归一化,5个一组,偏移量2
在顶点着色器中,设置可变变量v_Color:
  1. // Vertex shader program
  2. var VSHADER_SOURCE =
  3.     'attribute vec4 a_Position;\n' +
  4.     'attribute vec4 a_Color;\n' +
  5.     'varying vec4 v_Color;\n' + // varying variable
  6.     'void main() {\n' +
  7.     '   gl_Position = a_Position;\n' + // Coordinates
  8.     '   gl_PointSize = 10.0;\n' +       // Set the point size
  9.     '   v_Color = a_Color;\n' + // Pass the data to the fragment shader
  10.     '}\n';
复制代码
 在片段着色器中,引入可变变量v_Color,并复制给保留变量gl_FragColor:
  1. // Fragment shader program
  2. var FSHADER_SOURCE =
  3.     '#ifdef GL_ES\n'+
  4.     '   precision mediump float;\n' +
  5.     '#endif\n'+
  6.     'varying vec4 v_Color;\n' +
  7.     'void main() {\n' +
  8.     '   gl_FragColor = v_Color;\n' +   // Receive the data from the vertex shader
  9.     '}\n';
复制代码
其余和上一个列题一致,不再重复。
从顶点着色器转达数据到片段着色器的方法:



  • 可变变量(varying variable)只支持浮点型
  • 当顶点着色器上的可变变量名称及范例与片段着色器上的可变变量名称及范例相同时,自动会完成数据转达的动作。
示例步伐:
  1. // MulitAttributeColor.js// Vertex shader program
  2. var VSHADER_SOURCE =
  3.     'attribute vec4 a_Position;\n' +
  4.     'attribute vec4 a_Color;\n' +
  5.     'varying vec4 v_Color;\n' + // varying variable
  6.     'void main() {\n' +
  7.     '   gl_Position = a_Position;\n' + // Coordinates
  8.     '   gl_PointSize = 10.0;\n' +       // Set the point size
  9.     '   v_Color = a_Color;\n' + // Pass the data to the fragment shader
  10.     '}\n';// Fragment shader program
  11. var FSHADER_SOURCE =
  12.     '#ifdef GL_ES\n'+
  13.     '   precision mediump float;\n' +
  14.     '#endif\n'+
  15.     'varying vec4 v_Color;\n' +
  16.     'void main() {\n' +
  17.     '   gl_FragColor = v_Color;\n' +   // Receive the data from the vertex shader
  18.     '}\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 initialize shaders.');        return;    }    // Set the positions of vertices    var n = initVertexBuffers(gl);    if (n < 0) {        console.log('Failed to set the positions of the vertices');        return;    }    // Set the color for clearing <canvas>    gl.clearColor(0.0, 0.0, 0.0, 1.0);        // Clear <canvas>    gl.clear(gl.COLOR_BUFFER_BIT);    // Draw a point    gl.drawArrays(gl.POINTS, 0, n);}function initVertexBuffers(gl) {    var verticesSizes = new Float32Array([
  19.         // Vertex coordinates and color
  20.          0.0,  0.5, 1.0, 0.0, 0.0, // The 1st vertex
  21.         -0.5, -0.5, 0.0, 1.0, 0.0, // The 2nd vertex
  22.          0.5, -0.5, 0.0, 0.0, 1.0  // The 3rd vertex
  23.     ]);    var n = 3; // The number of vertices    // Create a buffer object    var vertexSizeBuffer = gl.createBuffer();     if (!vertexSizeBuffer) {        console.log('Failed to create the buffer object');        return -1;    }    // Write point sizes to the buffer object and enable it    gl.bindBuffer(gl.ARRAY_BUFFER, vertexSizeBuffer);    gl.bufferData(gl.ARRAY_BUFFER, verticesSizes, gl.STATIC_DRAW);    var FSIZE = verticesSizes.BYTES_PER_ELEMENT;    // Get the storage location of a_Position, allocate buffer, & enable    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;    }    gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, FSIZE * 5, 0);    gl.enableVertexAttribArray(a_Position);  // Enable allocation    // Get the storage location of a_PointSize, allocate buffer, & enable    var a_Color = gl.getAttribLocation(gl.program, 'a_Color');    if (a_Color < 0) {        console.log('Failed to get the storage location of a_Color');        return;    }       gl.vertexAttribPointer(a_Color, 3, gl.FLOAT, false, FSIZE * 5, FSIZE * 2);    gl.enableVertexAttribArray(a_Color);    return n;}
复制代码
运行截图:

带有颜色差值的三角形

顶点着色器和片段着色器之间的组装和光栅化

片段着色器的生成

修改每个片段的颜色值
步伐示例:
  1. // HelloTriangleFragCoord.js// Vertex shader programvar VSHADER_SOURCE =    'attribute vec4 a_Position;\n' +    'void main() {\n' +    '   gl_Position = a_Position;\n' + // Coordinates    '}\n';// Fragment shader programvar FSHADER_SOURCE =    'precision mediump float;\n' +    'uniform float u_Width;\n' +    'uniform float u_Height;\n' +    'void main() {\n' +    '   gl_FragColor = vec4(gl_FragCoord.x/u_Width, 0.0, gl_FragCoord.y/u_Height, 1.0);\n' +   // Set the color    '}\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 initialize shaders.');        return;    }    // Set the positions of vertices    var n = initVertexBuffers(gl);    if (n < 0) {        console.log('Failed to set the positions of the vertices');        return;    }    // Set the color for clearing <canvas>    gl.clearColor(0.0, 0.0, 0.0, 1.0);        // Clear <canvas>    gl.clear(gl.COLOR_BUFFER_BIT);    // Draw a point    gl.drawArrays(gl.TRIANGLES, 0, n);}function initVertexBuffers(gl) {    var vertices = new Float32Array([
  2.         0.0, 0.5,   -0.5, -0.5,   0.5, -0.5
  3.     ]);
  4.     var n = 3; // The number of vertices
  5.     // Create a buffer object
  6.     var vertexBuffer = gl.createBuffer();
  7.     if (!vertexBuffer) {
  8.         console.log('Failed to create the buffer object');
  9.         return -1;
  10.     }
  11.     // Bind the buffer object to traget
  12.     gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
  13.     // Write data into the buffer object
  14.     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;    }    // Assign the buffer object to a_Position variable
  15.     gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);    var u_Width = gl.getUniformLocation(gl.program, 'u_Width');    if (!u_Width) {      console.log('Failed to get the storage location of u_Width');      return;    }      var u_Height = gl.getUniformLocation(gl.program, 'u_Height');    if (!u_Height) {      console.log('Failed to get the storage location of u_Height');      return;    }      // Pass the width and hight of the <canvas>    gl.uniform1f(u_Width, gl.drawingBufferWidth);    gl.uniform1f(u_Height, gl.drawingBufferHeight);    // Enable the assignment to a_Position variable
  16.     gl.enableVertexAttribArray(a_Position);    return n;}
复制代码
运行截图:

变量的函数性和差值过程

可变变量的差值化:

颜色的差值化范例,红色-蓝色:

步伐示例:
  1. // ColoredTriangle.js// Vertex shader program
  2. var VSHADER_SOURCE =
  3.     'attribute vec4 a_Position;\n' +
  4.     'attribute vec4 a_Color;\n' +
  5.     'varying vec4 v_Color;\n' + // varying variable
  6.     'void main() {\n' +
  7.     '   gl_Position = a_Position;\n' + // Coordinates
  8.     '   gl_PointSize = 10.0;\n' +       // Set the point size
  9.     '   v_Color = a_Color;\n' + // Pass the data to the fragment shader
  10.     '}\n';// Fragment shader program
  11. var FSHADER_SOURCE =
  12.     '#ifdef GL_ES\n'+
  13.     '   precision mediump float;\n' +
  14.     '#endif\n'+
  15.     'varying vec4 v_Color;\n' +
  16.     'void main() {\n' +
  17.     '   gl_FragColor = v_Color;\n' +   // Receive the data from the vertex shader
  18.     '}\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 initialize shaders.');        return;    }    // Set the positions of vertices    var n = initVertexBuffers(gl);    if (n < 0) {        console.log('Failed to set the positions of the vertices');        return;    }    // Set the color for clearing <canvas>    gl.clearColor(0.0, 0.0, 0.0, 1.0);        // Clear <canvas>    gl.clear(gl.COLOR_BUFFER_BIT);    // Draw a point    gl.drawArrays(gl.TRIANGLES, 0, n);}function initVertexBuffers(gl) {    var verticesSizes = new Float32Array([
  19.         // Vertex coordinates and color
  20.          0.0,  0.5, 1.0, 0.0, 0.0, // The 1st vertex
  21.         -0.5, -0.5, 0.0, 1.0, 0.0, // The 2nd vertex
  22.          0.5, -0.5, 0.0, 0.0, 1.0  // The 3rd vertex
  23.     ]);    var n = 3; // The number of vertices    // Create a buffer object    var vertexSizeBuffer = gl.createBuffer();     if (!vertexSizeBuffer) {        console.log('Failed to create the buffer object');        return -1;    }    // Write point sizes to the buffer object and enable it    gl.bindBuffer(gl.ARRAY_BUFFER, vertexSizeBuffer);    gl.bufferData(gl.ARRAY_BUFFER, verticesSizes, gl.STATIC_DRAW);    var FSIZE = verticesSizes.BYTES_PER_ELEMENT;    // Get the storage location of a_Position, allocate buffer, & enable    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;    }    gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, FSIZE * 5, 0);    gl.enableVertexAttribArray(a_Position);  // Enable allocation    // Get the storage location of a_PointSize, allocate buffer, & enable    var a_Color = gl.getAttribLocation(gl.program, 'a_Color');    if (a_Color < 0) {        console.log('Failed to get the storage location of a_Color');        return;    }       gl.vertexAttribPointer(a_Color, 3, gl.FLOAT, false, FSIZE * 5, FSIZE * 2);    gl.enableVertexAttribArray(a_Color);    return n;}
复制代码
运行截图:

将图像贴到矩形上

构建纹理映射的步调

  • 预备要映射到多少图形上的图像
  • 指定多少形状的图像映射方法
  • 装载图像并对其配置使其可以大概在webgl中使用
  • 在片段着色器中从图像中提取纹理像素,并相应地设置对应的片段。
纹理坐标系


纹理坐标映射到顶点坐标

  1.     var verticesTexCoords = new Float32Array([
  2.         // Vertices coordinates, textures coordinates
  3.         -0.5,  0.5,  0.0,  1.0,
  4.         -0.5, -0.5,  0.0,  0.0,
  5.         0.5,   0.5,  1.0,  1.0,
  6.         0.5,  -0.5,  1.0,  0.0,
  7.     ]);
复制代码

创建纹理对象,这里请求服务器家在一个图片(image.src = '.../resources/sky.jpg'),而这是异步执行的。当图片被夹在完成后,会触发image.onload变乱,而这个变乱也是异步执行的。
  1.     var texture = gl.createTexture(); // Create a texture object
  2.     // Get the storage location of the u_Sampler
  3.     var u_Sampler = gl.getUniformLocation(gl.program, 'u_Sampler');
  4.     var image = new Image(); // Create an image object
  5.     // Register the event handler to be called on loading an image
  6.     image.onload = function(){ loadTexture(gl, n, texture, u_Sampler, image); };
  7.     // Tell the browser to load an image
  8.     image.src = '../resources/sky.jpg';
复制代码
创建纹理对象的内部情形表现图: 

翻转图像的Y轴

纹理对象装载后,必要反转坐标。由于图像(jpg、png、bmp等)的坐标与webgl不一致,得反转一下坐标(注意,不是反转图片)
  1. gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1);  // Flip the image's y axis
复制代码

激活纹理单位

纹理单位的数量因体系和硬件差别有所差异,但最少支持8个纹理单位,分别为gl.TEXTURE0~gl.TEXTURE7。另外在使用纹理单位前,必须先激活一下纹理单位。

  1. // Enable the texture unit 0
  2. gl.activeTexture(gl.TEXTURE0);
复制代码
将纹理对象绑定到目的

纹理范例包括二维纹理及立方体纹理贴图,本书只涉及二维纹理。
注意:绑定操作现实上完成了两个任务:一个是启动纹理对象并绑定它到目的;另一个是绑定它到纹理单位。
  1. // Bind the texture object to the target
  2. gl.bindTexture(gl.TEXTURE_2D, texture);
复制代码
执行后WEBGL内部变革表现图:

设置纹理对象的纹理参数

指定纹理图像映射到形状时如那边置处罚纹理图像。
  1. // Set the texture parameters
  2. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
复制代码
4种映射方式如下图所示:

映射完毕后,webgl内部的表现图如下:

将纹理指定给纹理对象

  1. // Set the texture image
  2. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, image);
复制代码
指定图像后,webgl内部的表现图如下:

将纹理单位转达给片段着色器

起首从片段着色器中关联变量
  1. var u_Sampler = gl.getUniformLocation(gl.program, 'u_Sampler');
复制代码
 然后通过image对象的onload函数转达给loadTexture
  1. var image = new Image(); // Create an image object
  2. image.onload = function(){ loadTexture(gl, n, texture, u_Sampler, image); };
复制代码
 末了将纹理单位转达个u_Sampler,其中第2个参数表现纹理管理器对象第0个纹理对象。
  1. // Set the texture unit 0 to the sampler
  2. gl.uniform1i(u_Sampler, 0);
复制代码
而在片段着色器中定义了u_Sampler的同一变量
  1. // Fragment shader program                  <- (Part 2)
  2. var FSHADER_SOURCE =
  3.     '#ifdef GL_ES\n' +
  4.     'precision mediump float;\n' +
  5.     '#endif\n' +
  6.     'uniform sampler2D u_Sampler;\n' +
  7.     'varying vec2 v_TexCoord;\n' +
  8.     'void main() {\n' +
  9.     '   gl_FragColor = texture2D(u_Sampler, v_TexCoord);\n' +
  10.     '}\n';
复制代码
使用着色器语言,将 u_Sampler, v_TextCoor发送给函数texture2D,并将结果返回给保留变量gl_FragColor。其中v_TextCoor为纹理映射坐标,来自顶点着色器。
执行后,webgl内部表现图如下:

将纹理坐标从顶点着色器转达到片段着色器

顶点着色器中将纹理坐标a_TexCoord通过可变变量v_TexCoord转达给片段着色器。
  1. // Vertex shader program                    <- (Part 1)
  2. var VSHADER_SOURCE =
  3.     'attribute vec4 a_Position;\n' +
  4.     'attribute vec2 a_TexCoord;\n' +
  5.     'varying vec2 v_TexCoord;\n' +
  6.     'void main() {\n' +
  7.     '  gl_Position = a_Position;\n' +
  8.     '  v_TexCoord = a_TexCoord;\n' +
  9.     '}\n';
复制代码
而纹理坐标在initVertexBuffer被关联:
  1. // Create the buffer object
  2. var vertexTexCoordBuffer = gl.createBuffer();
  3. // Write the vertex coords and textures coords to the object buffer
  4. gl.bindBuffer(gl.ARRAY_BUFFER, vertexTexCoordBuffer);
  5. gl.bufferData(gl.ARRAY_BUFFER, verticesTexCoords, gl.STATIC_DRAW);
  6. var FSIZE = verticesTexCoords.BYTES_PER_ELEMENT;
  7. var a_TexCoord = gl.getAttribLocation(gl.program, 'a_TexCoord');
  8. gl.vertexAttribPointer(a_TexCoord, 2, gl.FLOAT, false, FSIZE * 4, FSIZE * 2);
  9. gl.enableVertexAttribArray(a_TexCoord); // Enable buffer allocation
复制代码
纹理坐标在数组中被定义(第3,第4项):
  1. var verticesTexCoords = new Float32Array([
  2.         // Vertices coordinates, textures coordinates
  3.         -0.5,  0.5,  0.0,  1.0,
  4.         -0.5, -0.5,  0.0,  0.0,
  5.         0.5,   0.5,  1.0,  1.0,
  6.         0.5,  -0.5,  1.0,  0.0,
  7.     ]);
复制代码
在判定着色器中检索纹理颜色

在片段着色器中,使用texture2D将纹理和坐标映射到绘制的形状上,本例是个矩形。webgl使用差值颜色提取的方式,将颜色从纹理中提取出来赋值给gl_FragColor内置保留变量。
  1.    'void main() {\n' +
  2.     '   gl_FragColor = texture2D(u_Sampler, v_TexCoord);\n' +
  3.     '}\n';
复制代码
在本例中使用了RGBA的返回格式,因为图片是jpg/png形式。
步伐示例

  1. // TexturedQuad.js// Vertex shader program                    <- (Part 1)
  2. var VSHADER_SOURCE =
  3.     'attribute vec4 a_Position;\n' +
  4.     'attribute vec2 a_TexCoord;\n' +
  5.     'varying vec2 v_TexCoord;\n' +
  6.     'void main() {\n' +
  7.     '  gl_Position = a_Position;\n' +
  8.     '  v_TexCoord = a_TexCoord;\n' +
  9.     '}\n';// Fragment shader program                  <- (Part 2)
  10. var FSHADER_SOURCE =
  11.     '#ifdef GL_ES\n' +
  12.     'precision mediump float;\n' +
  13.     '#endif\n' +
  14.     'uniform sampler2D u_Sampler;\n' +
  15.     'varying vec2 v_TexCoord;\n' +
  16.     'void main() {\n' +
  17.     '   gl_FragColor = texture2D(u_Sampler, v_TexCoord);\n' +
  18.     '}\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 initialize shaders.');        return;    }    // Set the positions of vertices        <- (part 3)    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.0, 0.0, 1.0);    // Setting the textures    if (!initTextures(gl, n)) {        console.log('Failed to intialize the texture.');        return;    }}function initVertexBuffers(gl) {    var verticesTexCoords = new Float32Array([
  19.         // Vertices coordinates, textures coordinates
  20.         -0.5,  0.5,  0.0,  1.0,
  21.         -0.5, -0.5,  0.0,  0.0,
  22.         0.5,   0.5,  1.0,  1.0,
  23.         0.5,  -0.5,  1.0,  0.0,
  24.     ]);    var n = 4; // The number of vertices    // Create the buffer object    var vertexTexCoordBuffer = gl.createBuffer();    if (!vertexTexCoordBuffer) {        console.log('Failed to create the buffer object');        return -1;    }    // Write the vertex coords and textures coords to the object buffer    gl.bindBuffer(gl.ARRAY_BUFFER, vertexTexCoordBuffer);    gl.bufferData(gl.ARRAY_BUFFER, verticesTexCoords, gl.STATIC_DRAW);    var FSIZE = verticesTexCoords.BYTES_PER_ELEMENT;    // Get the storage location of a_Position, allocate buffer, & enable    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;    }    gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, FSIZE * 4, 0);    gl.enableVertexAttribArray(a_Position);  // Enable buffer allocation    // Allocate the texture coordinates to a_TexCoord, and enable it.    var a_TexCoord = gl.getAttribLocation(gl.program, 'a_TexCoord');    if (a_TexCoord < 0) {        console.log('Failed to get the storage location of a_TexCoord');        return;    }    gl.vertexAttribPointer(a_TexCoord, 2, gl.FLOAT, false, FSIZE * 4, FSIZE * 2);    gl.enableVertexAttribArray(a_TexCoord); // Enable buffer allocation    return n;}function initTextures(gl, n) {          // <- (Part 4)    var texture = gl.createTexture(); // Create a texture object    if (!texture) {        console.log('Failed to create the texture object');        return false;    }    // Get the storage location of the u_Sampler    var u_Sampler = gl.getUniformLocation(gl.program, 'u_Sampler');    if (!u_Sampler) {        console.log('Failed to get the storage location of u_Sampler');        return false;    }    var image = new Image(); // Create an image object    if (!image) {        console.log('Failed to create the image object');        return false;    }    // Register the event handler to be called on loading an image    image.onload = function(){ loadTexture(gl, n, texture, u_Sampler, image); };    // Tell the browser to load an image    image.src = '../resources/sky.jpg';    return true;}function loadTexture(gl, n, texture, u_Sampler, image) {    // <- (Part 5)    gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1);  // Flip the image's y axis    // Enable the texture unit 0    gl.activeTexture(gl.TEXTURE0);    // Bind the texture object to the target    gl.bindTexture(gl.TEXTURE_2D, texture);    // Set the texture parameters    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);    // Set the texture image    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, image);    // Set the texture unit 0 to the sampler    gl.uniform1i(u_Sampler, 0);    gl.clear(gl.COLOR_BUFFER_BIT);   // Clear <canvas>    gl.drawArrays(gl.TRIANGLE_STRIP, 0, n); // Draw a rectangle}
复制代码
运行结果:

将纹理循环映射

修改纹理映射关系,如下:
  1.    var verticesTexCoords = new Float32Array([
  2.         // Vertices coordinates, textures coordinates
  3.         -0.5,  0.5,  -0.3,  1.7,
  4.         -0.5, -0.5,  -0.3, -0.2,
  5.         0.5,   0.5,   1.7,  1.7,
  6.         0.5,  -0.5,   1.7,  -0.2
  7.     ]);
复制代码
映射关系表现图(见右)其余内容保持不变,执行步伐后,运行结果(见左)如下:

边缘填充与镜像填充
gl.TEXTURE_WRAP_S: 水平方向的填充, gl.TEXTURE_WRAP_T:垂直方向的填充。
填充方法:gl.CLAMP_TO_EDGE:使用边缘颜色;gl.MIRRORED_REPEAT:使用镜像重复填充。
  1.     // Set the texture parameters
  2.     gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
  3.     gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  4.     gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.MIRRORED_REPEAT);
复制代码
上面代码水平使用了边缘颜色填充,垂直使用了纹理镜像填充。运行结果如下图:

 复合多个纹理

与前文TexturedQuad.js的7处差别

1. 片段着色器使用了两个纹理变量,并使用它们各自的纹理映射颜色的乘积作为终极颜色赋值给gl_FragColor。
  1. var FSHADER_SOURCE =
  2.     '#ifdef GL_ES\n' +
  3.     'precision mediump float;\n' +
  4.     '#endif\n' +
  5.     'uniform sampler2D u_Sampler0;\n' +
  6.     'uniform sampler2D u_Sampler1;\n' +
  7.     'varying vec2 v_TexCoord;\n' +
  8.     'void main() {\n' +
  9.     '   vec4 color0 = texture2D(u_Sampler0, v_TexCoord);\n' +
  10.     '   vec4 color1 = texture2D(u_Sampler1, v_TexCoord);\n' +
  11.     '   gl_FragColor = color0 * color1;\n' +
  12.     '}\n';
复制代码
2. 初始化纹理函数initTextures中,声明了两个纹理对象,然后两个纹理文件,关联两个同一变量。
  1.     var texture0 = gl.createTexture(); // Create a texture objec
  2.     var texture1 = gl.createTexture(); // create another texture object
  3.     // Get the storage location of the u_Sampler
  4.     var u_Sampler0 = gl.getUniformLocation(gl.program, 'u_Sampler0');
  5.     var u_Sampler1 = gl.getUniformLocation(gl.program, 'u_Sampler1');
  6.     var image0 = new Image(); // Create an image object
  7.     var image1 = new Image(); // Create another image object
  8.     // Register the event handler to be called on loading an image
  9.     image0.onload = function(){ loadTexture(gl, n, texture0, u_Sampler0, image0,
  10.         0); };
  11.     image1.onload = function(){ loadTexture(gl, n, texture1, u_Sampler1, image1,
  12.         1); };
  13.     // Tell the browser to load an image
  14.     image0.src = '../resources/sky.jpg';
  15.     image1.src = '../resources/circle.gif';
复制代码
3. 定义两个全局变量,用来记录两个纹理是否各自预备就绪已经做好了映射预备。这是因为图像时异步加载的,并不清晰哪个先加载,所以放入两个变量,确保都为true才认为两个图像都映射完毕。
  1. // Specify whether the texture unit is ready to use
  2. var g_texUnit0 = false, g_texUnit1 = false;
复制代码
4. 修改loadTexture参数,增加纹理单位编号参数:
  1. function loadTexture(gl, n, texture, u_Sampler, image, texUnit) {
  2. ...
  3. }
复制代码
5. 每个纹理参数调用loadTexture时,各自执行自己的纹理激活状态
  1.     // make the texture unit active
  2.     if (texUnit == 0) {
  3.         gl.activeTexture(gl.TEXTURE0);
  4.         g_texUnit0  = true;
  5.     } else {
  6.         gl.activeTexture(gl.TEXTURE1);
  7.         g_texUnit1 = true;
  8.     }
复制代码
6. 修改各自关联的对象
  1.     // Set the texture unit number to the sampler
  2.     gl.uniform1i(u_Sampler, texUnit);
复制代码
7. 在两个纹理全部映射完毕的前提下才开启绘制图像。确保两个图像都映射完毕(为true),因为图像加载是异步的。
  1.     if (g_texUnit0 && g_texUnit1) {
  2.         gl.drawArrays(gl.TRIANGLE_STRIP, 0, n); // Draw a rectangle
  3.     }
复制代码
两个图像映射完毕后的webgl内部表现图:

步伐示例

  1. // TexturedQuad.js// Vertex shader program                    <- (Part 1)
  2. var VSHADER_SOURCE =
  3.     'attribute vec4 a_Position;\n' +
  4.     'attribute vec2 a_TexCoord;\n' +
  5.     'varying vec2 v_TexCoord;\n' +
  6.     'void main() {\n' +
  7.     '  gl_Position = a_Position;\n' +
  8.     '  v_TexCoord = a_TexCoord;\n' +
  9.     '}\n';// Fragment shader program                  <- (Part 2)var FSHADER_SOURCE =
  10.     '#ifdef GL_ES\n' +
  11.     'precision mediump float;\n' +
  12.     '#endif\n' +
  13.     'uniform sampler2D u_Sampler0;\n' +
  14.     'uniform sampler2D u_Sampler1;\n' +
  15.     'varying vec2 v_TexCoord;\n' +
  16.     'void main() {\n' +
  17.     '   vec4 color0 = texture2D(u_Sampler0, v_TexCoord);\n' +
  18.     '   vec4 color1 = texture2D(u_Sampler1, v_TexCoord);\n' +
  19.     '   gl_FragColor = color0 * color1;\n' +
  20.     '}\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 initialize shaders.');        return;    }    // Set the positions of vertices        <- (part 3)    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.0, 0.0, 1.0);    // Setting the textures    if (!initTextures(gl, n)) {        console.log('Failed to intialize the texture.');        return;    }}function initVertexBuffers(gl) {    var verticesTexCoords = new Float32Array([        // Vertices coordinates, textures coordinates        -0.5,  0.5,   0.0,  1.0,        -0.5, -0.5,   0.0,  0.0,        0.5,   0.5,   1.0,  1.0,        0.5,  -0.5,   1.0,  0.0    ]);    var n = 4; // The number of vertices    // Create the buffer object    var vertexTexCoordBuffer = gl.createBuffer();    if (!vertexTexCoordBuffer) {        console.log('Failed to create the buffer object');        return -1;    }    // Write the vertex coords and textures coords to the object buffer    gl.bindBuffer(gl.ARRAY_BUFFER, vertexTexCoordBuffer);    gl.bufferData(gl.ARRAY_BUFFER, verticesTexCoords, gl.STATIC_DRAW);    var FSIZE = verticesTexCoords.BYTES_PER_ELEMENT;    // Get the storage location of a_Position, allocate buffer, & enable    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;    }    gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, FSIZE * 4, 0);    gl.enableVertexAttribArray(a_Position);  // Enable buffer allocation    // Allocate the texture coordinates to a_TexCoord, and enable it.    var a_TexCoord = gl.getAttribLocation(gl.program, 'a_TexCoord');    if (a_TexCoord < 0) {        console.log('Failed to get the storage location of a_TexCoord');        return;    }    gl.vertexAttribPointer(a_TexCoord, 2, gl.FLOAT, false, FSIZE * 4, FSIZE * 2);    gl.enableVertexAttribArray(a_TexCoord); // Enable buffer allocation    return n;}function initTextures(gl, n) {          // <- (Part 4)    var texture0 = gl.createTexture(); // Create a texture object    if (!texture0) {        console.log('Failed to create the texture0 object');        return false;    }    var texture1 = gl.createTexture(); // create another texture object    if (!texture1) {        console.log('Failed to create the texture1 object');        return false;    }    // Get the storage location of the u_Sampler    var u_Sampler0 = gl.getUniformLocation(gl.program, 'u_Sampler0');    if (!u_Sampler0) {        console.log('Failed to get the storage location of u_Sampler0');        return false;    }    var u_Sampler1 = gl.getUniformLocation(gl.program, 'u_Sampler1');    if (!u_Sampler1) {        console.log('Failed to get the storage location of u_Sampler1');        return false;    }    var image0 = new Image(); // Create an image object    if (!image0) {        console.log('Failed to create the image0 object');        return false;    }    var image1 = new Image(); // Create another image object    if (!image1) {        console.log('Failed to create the image1 object');        return false;    }    // Register the event handler to be called on loading an image    image0.onload = function(){ loadTexture(gl, n, texture0, u_Sampler0, image0,        0); };    image1.onload = function(){ loadTexture(gl, n, texture1, u_Sampler1, image1,        1); };    // Tell the browser to load an image    image0.src = '../resources/sky.jpg';    image1.src = '../resources/circle.gif';    return true;}// Specify whether the texture unit is ready to use
  21. var g_texUnit0 = false, g_texUnit1 = false;function loadTexture(gl, n, texture, u_Sampler, image, texUnit) {    // <- (Part 5)    gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1);  // Flip the image's y axis    // make the texture unit active
  22.     if (texUnit == 0) {
  23.         gl.activeTexture(gl.TEXTURE0);
  24.         g_texUnit0  = true;
  25.     } else {
  26.         gl.activeTexture(gl.TEXTURE1);
  27.         g_texUnit1 = true;
  28.     }    // Bind the texture object to the target    gl.bindTexture(gl.TEXTURE_2D, texture);    // Set the texture parameters    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);    // Set the texture image    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, image);    // Set the texture unit number to the sampler
  29.     gl.uniform1i(u_Sampler, texUnit);    gl.clear(gl.COLOR_BUFFER_BIT);   // Clear <canvas>    if (g_texUnit0 && g_texUnit1) {
  30.         gl.drawArrays(gl.TRIANGLE_STRIP, 0, n); // Draw a rectangle
  31.     }}
复制代码
运行结果

Chapter 6 OpenGL ES着色语言

初识GLSL ES 着色器

根本


  • GLSL ES类C语言,巨细写敏感
  • 每行末端使用分号(;)结束
执行次序

始终从main()开始,且main()不能有任何参数,返回值为void
解释


  • 单行解释://
  • 多行解释:/*......*/
数据范例


  • 数值范例:包括integer、float。不带小数点将被视作整数。
  • 布尔范例:true、false
  • GLSL ES不支持string字符串范例
变量


  • a~z,A~Z,_,0-9
  • 数字不能放在第一位
  • 不能使用关键字和保留字
  • 不能使用gl_、webgl_、_webgl_开头的保留字作为变量名称
GLSL ES是一种强范例语言

定义变量的结构

<data type> <variable name>
例如:vec4 a_Position
基本范例



  • float
  • int 
  • bool
范例转换

int(float), int (bool), float(int), float(bool), bool(int), bool(float)
向量范例和矩阵范例


支持的向量范例



  • vec2, vec3, vec4
  • ivec2, ivec3, ivec4
  • bvec2, bvec3, bvec4
支持的矩阵范例



  • mat2, mat3, mat4
赋值与构造

向量的赋值与构造
  1. vec3 v3 = vec3(1.0, 0.0, 0.5); // sets v3 to(1.0, 0.0, 0.5)
  2. vec2 v2 = vec2(v3); // sets v2 to (1.0, 0.0) using the 1st and 2nd elements of v3
  3. vec4 v4 = vec4(1.0); // sets v4 to (1.0, 1.0, 1.0, 1.0)
  4. vec4 v4b = vec4(v2, v4); // sets (1.0, 0.0, 1.0, 1.0) to v4b
复制代码
矩阵的赋值与构造
注意:矩阵是列主形式的。

  1. // two vec2 are used to construct a mat2
  2. vec2 v2_1 = vec2(1.0, 3.0);
  3. vec2 v2_2 = vec2(2.0, 4.0);
  4. mat2 m2_1 = mat2(v2_1, v2_2); // 1.0 2.0
  5.                               // 3.0 4.0
  6. // vec4 is used to construct mat2
  7. vec4 v4 = vec4(1.0, 3.0, 2.0, 4.0);
  8. mat2 m2_2 = mat2(v4); // 1.0 2.0
  9.                       // 3.4 4.0
  10. // Two floating point numbers and vec2 are used to construct a mat2
  11. mat2 m2 = mat2(1.0, 3.0, v2_2); // 1.0 2.0
  12.                                 // 3.0 4.0
  13. //If a single value is specified to a matrix constructor, a matrix is constructed using the value as its diagonal elements:
  14. mat4 m4 = mat4(1.0); // 1.0 0.0 0.0 0.0
  15.                      // 0.0 1.0 0.0 0.0
  16.                      // 0.0 0.0 1.0 0.0
  17.                      // 0.0 0.0 0.0 1.0
  18. mat4 m4 = mat4(1.0, 2.0, 3.0); // Error. mat4 requires 16 elements.
复制代码
访问成员

使用点(.)操作符
  1. vec3 v3 = vec3(1.0, 2.0, 3.0); // sets v3 to(1.0, 2.0, 3.0)
  2. float f;
  3. f = v3.x; // sets f to 1.0
  4. f = v3.y; // sets f to 2.0
  5. f = v3.z; // sets f to 3.0
  6. f = v3.r; // sets f to 1.0
  7. f = v3.s; // sets f to 1.0
  8. f = v3.w; // w requires access to the fourth element, which doesn't exist.
  9. vec2 v2;
  10. v2 = v3.xy; // sets v2 to (1.0, 2.0)
  11. v2 = v3.yz; // sets v2 to (2.0, 3.0). Any component can be omitted
  12. v2 = v3.xz; // sets v2 to (1.0, 3.0). You can skip any component.
  13. v2 = v3.yx; // sets v2 to (2.0, 1.0). You can reverse the order.
  14. v2 = v3.xx; // sets v2 to (1.0, 1.0). You can repeat any component.
  15. vec3 v3a;
  16. v3a = v3.zyx; // sets v3a to (3.0, 2.0, 1.0). You can use all names.
  17. vec4 position = vec4(1.0, 2.0, 3.0, 4.0);
  18. position.xw = vec2(5.0, 6.0); // position = (5.0, 2.0, 3.0, 6.0)
复制代码
使用[]操作符
  1. mat4 m4 = mat4 ( 1.0, 2.0, 3.0, 4.0,
  2. 5.0, 6.0, 7.0, 8.0,
  3. 9.0, 10.0, 11.0, 12.0,
  4. 13.0, 14.0, 15.0, 16.0);
  5. vec4 v4 = m4[0]; // Retrieve the 1st column from m4: (1.0, 2.0, 3.0, 4.0)
  6. float m23 = m4[1][2]; // sets m23 to the third component of the second
  7.                       // column of m4 (7.0).
  8. float m32 = m4[2].y; // sets m32 to the second component of the third
  9.                      // column of m4 (10.0).
  10. const int index = 0; // "const" keyword specifies the variable is a
  11.                      // read-only variable.
  12. vec4 v4a = m4[index]; // is the same as m4[0]
  13. vec4 v4b = m4[index + 1]; // is the same as m4[1]
  14. int index2 = 0;
  15. vec4 v4c = m4[index2]; // Error: because index2 is not a constant index.
复制代码
向量与矩阵的操作

向量与标量操作
  1. // The following example uses the + operator, but the -, *, and /
  2. // operators also have the same effect.
  3. v3b = v3a + f; // v3b.x = v3a.x + f;
  4.                // v3b.y = v3a.y + f;
  5. // v3b.z = v3a.z + f;
复制代码
向量与向量操作
  1. // The following example uses the + operator, but the -, *, and /
  2. // operators also have the same effect.
  3. v3c = v3a + v3b; // v3a.x + v3b.x;
  4.                  // v3a.y + v3b.y;
  5.                  // v3a.z + v3b.z;
复制代码
矩阵与标量操作
  1. // The following example uses the + operator, but the -, *, and /
  2. // operators also have the same effect.
  3. m3b = m3a * f; // m3b[0].x = m3a[0].x * f; m3b[0].y = m3a[0].y * f;
  4.                // m3b[0].z = m3a[0].z * f;
  5.                // m3b[1].x = m3a[1].x * f; m3b[1].y = m3a[1].y * f;
  6.                // m3b[1].z = m3a[1].z * f;
  7.                // m3b[2].x = m3a[2].x * f; m3b[2].y = m3a[2].y * f;
  8.                // m3b[2].z = m3a[2].z * f;
复制代码
矩阵与向量相乘
  1. v3b = m3a * v3a; // v3b.x = m3a[0].x * v3a.x + m3a[1].x * v3a.y
  2.                  //         + m3a[2].x * v3a.z;
  3.                  // v3b.y = m3a[0].y * v3a.x + m3a[1].y * v3a.y
  4.                  //         + m3a[2].y * v3a.z;
  5.                  // v3b.z = m3a[0].z * v3a.x + m3a[1].z * v3a.y
  6.                  //         + m3a[2].z * v3a.z;
复制代码
向量与矩阵相乘
  1. v3b = v3a * m3a; // v3b.x = v3a.x * m3a[0].x + v3a.y * m3a[0].y
  2.                  //         + v3a.z * m3a[0].z;
  3.                  // v3b.y = v3a.x * m3a[1].x + v3a.y * m3a[1].y
  4.                  //         + v3a.z * m3a[1].z;
  5.                  // v3b.z = v3a.x * m3a[2].x + v3a.y * m3a[2].y
  6.                  //         + v3a.z * m3a[2].z;
复制代码
矩阵与矩阵相乘
  1. m3c = m3a * m3b; // m3c[0].x = m3a[0].x * m3b[0].x + m3a[1].x * m3b[0].y
  2.                  //            + m3a[2].x * m3b[0].z;
  3.                  // m3c[1].x = m3a[0].x * m3b[1].x + m3a[1].x * m3b[1].y
  4.                  //            + m3a[2].x * m3b[1].z;
  5.                  // m3c[2].x = m3a[0].x * m3b[2].x + m3a[1].x * m3b[2].y
  6.                  //            + m3a[2].x * m3b[2].z;
  7.                  // m3c[0].y = m3a[0].y * m3b[0].x + m3a[1].y * m3b[0].y
  8.                  //            + m3a[2].y * m3b[0].z;
  9.                  // m3c[1].y = m3a[0].y * m3b[1].x + m3a[1].y * m3b[1].y
  10.                  //            + m3a[2].y * m3b[1].z;
  11.                  // m3c[2].y = m3a[0].y * m3b[2].x + m3a[1].y * m3b[2].y
  12.                  //            + m3a[2].y * m3b[2].z;
  13.                  // m3c[0].z = m3a[0].z * m3b[0].x + m3a[1].z * m3b[0].y
  14.                  //            + m3a[2].z * m3b[0].z;
  15.                  // m3c[1].z = m3a[0].z * m3b[1].x + m3a[1].z * m3b[1].y
  16.                  //            + m3a[2].z * m3b[1].z;
  17.                  // m3c[2].z = m3a[0].z * m3b[2].x + m3a[1].z * m3b[2].y
  18.                  //            + m3a[2].z * m3b[2].z;
复制代码
结构体

赋值与构造

结构体的构造
  1. struct light { // defines the structure "light"
  2. vec4 color;
  3. vec3 position;
  4. }
  5. light l1, l2; // declares variable "l1" and "l2" of the type "light"
复制代码
或简化为
  1. struct light { // declares structure and its variable all together
  2. vec4 color; // color of a light
  3. vec3 position; // position of a light
  4. } l1; // variable "l1" of the structure
复制代码
结构体的构造
按结构体成员定义的次序构造结构体。

访问结构体成员

使用点(.)操作符
  1. vec4 color = l1.color;
  2. vec3 position = l1.position;
复制代码
结构体的操作

操作符:=、==、!=。
注意:结构体内含有数组大概采样器时不支持此类操作
数组

类C数组,不支持push(), pop(),定义数组是要指定数组巨细,并指定数组范例如下:
  1. float floatArray[4]; // declares an array consisting of four floats
  2. vec4 vec4Array[2]; // declares an array consisting of two vec4s
复制代码
数组巨细必须是常量,下面代码是错误的
  1. int size = 4;
  2. vec4 vec4Array[size]; // Error. If you declare "const int size = 4;"
  3.                       // it will not result in an error
复制代码
访问数组的成员可以使用[]操作符,如下示例:
  1. float f = floatArray[2];
复制代码
当给向量数构成员赋值时可以通过向量赋值如下:
  1. vec4Array[0] = vec4(4.0, 3.0, 6.0, 1.0);
  2. vec4Array[1] = vec4(3.0, 2.0, 0.0, 1.0);
复制代码
标量与数构成员相乘、向量与向量成员相乘示例:
  1. // multiplies the second element of floatArray by 3.14
  2. float f = floatArray[1] * 3.14;
  3. // multiplies the first element of vec4Array by vec4(1.0, 2.0, 3.0, 4.0);
  4. vec4 v4 = vec4Array[0] * vec4(1.0, 2.0, 3.0, 4.0);
复制代码
采样器

webgl支持两种范例的采样器,分别为sampler2D和samplerCube用来存储纹理对象。使用纹理对象应该定义为uniform范例,如下:
  1. uniform sampler2D u_Sampler;
复制代码
在转达纹理到纹理单位时,使用如下代码,第2个参数为纹理单位序号,序号从0到7。
  1. gl.uniform1i(u_Sampler, 0);
复制代码
操作符优先级


条件控制流与迭代器

if语句用法同C,其规范如下:
  1. if (conditional-expression1) {
  2.     commands here are executed if conditional-expression1 is true.
  3. } else if (conditional-expression2) {
  4.     commands here are executed if conditional-expression1 is false but            
  5.     conditionalexpression2 is true.
  6. } else {
  7.     commands here are executed if conditional-expression1 is false and     
  8.     conditionalexpression2 is false.
  9. }
复制代码
for语句用法同C,其规范如下:
  1. for (for-init-statement; conditional-expression; loop-index-expression) {
  2.     the commands which you want to execute repeatedly.
  3. }
复制代码
 同样也支持continue和break语句。不支持while和do...while语句。也不支持case语句。
函数

函数的一样寻常定义如下:
  1. returnType functionName(type0 arg0, type1 arg1, ..., typen argn) {
  2.     do some computation
  3.     return returnValue;
  4. }
复制代码
当函数无返回值时,定义返回范例void。当函数无参数时,参数列表定义为void。 
原型声明

同C语言一样,被调函数原型定义在主调函数前面时,才能在主调函数背面出现被调函数的定义。如下代码:
  1. float luma(vec4); // a prototype declaration
  2. main() {
  3.     ...
  4.     float brightness = luma(color); // luma() is called before it is defined.
  5.     ...
  6. }
  7. float luma (vec4 color) {
  8.     return 0.2126 * color.r + 0.7162 * color.g + 0.0722 * color.b;
  9. }
复制代码
参数限定符

参数限定符包括:


  • in 转达进一个值到函数,可修改,但不影响主调函数
  • const in 转达进一个值到函数,不可修改
  • out 从函数转达出一个值到被调函数,类似引用转达
  • inout 既能转达进函数也能转达出函数,类似引用转达
代码示例:主调函数
  1. luma2(color, brightness); // the result is stored into "brightness"
  2.                           // same as brightness = luma(color)
复制代码
被调函数:
  1. void luma2 (in vec3 color, out float brightness) {
  2.     brightness = 0.2126 * color.r + 0.7162 * color.g + 0.0722 * color.b;
  3. }
复制代码
这里从主调函数输入vec3范例的变量color,在函数内部盘算后通过out限定符定义的brightness转达出函数,转达给主调函数的brightness。 
内置函数


全局变量与局部变量

同C语言,函数内部是局部变量,函数外部是全局变量。
存储限定符

属性变量,同一变量,可变变量三个存储限定符在webgl中的表现图

const常量

类似C语言,常量存储限定符,示例代码如下,常量初始化,初始化后不可变更:
  1. const int lightspeed = 299792458; // light speed (m/s)
  2. const vec4 red = vec4(1.0, 0.0, 0.0, 1.0); // red
  3. const mat4 identity = mat4(1.0); // identity matrix
复制代码
attribute变量

属性变量必须定义为全局变量,通过它可以将顶点坐标转达给顶点着色器。同样,属性变量支持向量和矩阵范例,如下:
  1. attribute vec4 a_Color;
  2. attribute float a_PointSize;
复制代码
本质上,属性变量、同一变量、可变变量的数量不能无穷制使用,但有一个最小支持量,如下:
 uniform变量


同一变量是只读的,不可变更转达进来的值。它既可以放在顶点着色器中也可以放在片段着色器中。另外,同一变量可定义为局部变量也可以定义为全局变量。
如果在片段着色器中使用了和顶点着色中相同名称的同一变量,其存储数据将被两个着色器共享。
下面为定义同一变量示例:
  1. uniform mat4 u_ViewMatrix;
  2. uniform vec3 u_LightPosition;
复制代码
varying变量

可变变量必须定义为全局变量。在变量名称相同的情况下,可以使用它将数据从顶点着色器转达到片段着色器中。示例如下:
  1. varying vec2 v_TexCoord;
  2. varying vec4 v_Color;
复制代码
精度限定符

为了提高服从及节流存储成本、电池耗电量等原因,可限定属范例的精度。存储精度分为三级:highp、mediump、lowp。定义的方法如下:
  1. #ifdef GL_ES
  2. precision mediump float;
  3. #endif
复制代码
三种精度限定符所表现的范围如下:


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

民工心事

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表