1. /* eslint no-console:0 consistent-return:0 */
    2. "use strict"; /* 严格模式 */
    3. /*
    4. *** 创建对应的类型的着色器
    5. *** gl === 渲染上下文
    6. *** type === 着色器类型
    7. *** source === 数据源
    8. */
    9. function createShader(gl, type, source) {
    10. var shader = gl.createShader(type); // 创建着色器对象
    11. gl.shaderSource(shader, source); // 提供数据源
    12. gl.compileShader(shader); // 编译 -> 生成着色器
    13. var success = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
    14. if (success) {
    15. return shader;
    16. }
    17. console.log(gl.getShaderInfoLog(shader));
    18. gl.deleteShader(shader);
    19. }
    20. /*
    21. *** 创建渲染程序
    22. *** gl === 渲染上下文
    23. *** type === 着色器类型
    24. *** source === 数据源
    25. */
    26. function createProgram(gl, vertexShader, fragmentShader) {
    27. var program = gl.createProgram();
    28. gl.attachShader(program, vertexShader);
    29. gl.attachShader(program, fragmentShader);
    30. gl.linkProgram(program);
    31. var success = gl.getProgramParameter(program, gl.LINK_STATUS);
    32. if (success) {
    33. return program;
    34. }
    35. console.log(gl.getProgramInfoLog(program));
    36. gl.deleteProgram(program);
    37. }
    38. function main() {
    39. // Get A WebGL context 获取对应的绘制上下文 canvas
    40. var canvas = document.querySelector("#c");
    41. var gl = canvas.getContext("webgl");
    42. if (!gl) {
    43. return;
    44. }
    45. // Get the strings for our GLSL shaders 获取对应的glsl程序的数据
    46. var vertexShaderSource = document.querySelector("#vertex-shader-2d").text;
    47. var fragmentShaderSource = document.querySelector("#fragment-shader-2d").text;
    48. // create GLSL shaders, upload the GLSL source, compile the shaders 创建对应的着色器
    49. var vertexShader = createShader(gl, gl.VERTEX_SHADER, vertexShaderSource);
    50. var fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fragmentShaderSource);
    51. // Link the two shaders into a program 建立渲染程序
    52. var program = createProgram(gl, vertexShader, fragmentShader);
    53. // look up where the vertex data needs to go 把对应的定点传入到对应的程序
    54. var positionAttributeLocation = gl.getAttribLocation(program, "a_position");
    55. // 如果从js的一遍遍的向程序添加数据的话;单线程会导致在大量数据的情况会比较卡和缓慢 所以在利用gpu的多线程并行渲染的能力就可以
    56. // 而GPU的数据是从对应的内存缓存中去( butter )
    57. // Create a buffer and put three 2d clip space points in it
    58. var positionBuffer = gl.createBuffer();
    59. // Bind it to ARRAY_BUFFER (think of it as ARRAY_BUFFER = positionBuffer) 绑定对应的类型
    60. gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
    61. var positions = [
    62. 0, 0,
    63. 0, 0.5,
    64. 0.7, 0,
    65. ];
    66. // 绑定对应的强类型的点到缓存的类型数据上去
    67. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
    68. // code above this line is initialization code.
    69. // code below this line is rendering code.
    70. webglUtils.resizeCanvasToDisplaySize(gl.canvas);
    71. // Tell WebGL how to convert from clip space to pixels 设置对应的视窗效果
    72. gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
    73. // Clear the canvas 清空画布
    74. gl.clearColor(0, 0, 0, 0);
    75. gl.clear(gl.COLOR_BUFFER_BIT);
    76. // Tell it to use our program (pair of shaders) 使用渲染程序
    77. gl.useProgram(program);
    78. // Turn on the attribute 激活变量
    79. gl.enableVertexAttribArray(positionAttributeLocation);
    80. // Bind the position buffer. 又来一遍?
    81. gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
    82. // Tell the attribute how to get data out of positionBuffer (ARRAY_BUFFER) 告诉属性怎么从positionBuffer中缓存中读取数据
    83. var size = 2; // 2 components per iteration 每次迭代运行提取两个单位数据
    84. var type = gl.FLOAT; // the data is 32bit floats 每个单位的数据类型是32位浮点型
    85. var normalize = false; // don't normalize the data 不需要归一化数据
    86. var stride = 0; // 0 = move forward size * sizeof(type) each iteration to get the next position 0 = 移动单位数量 * 每个单位占用内存(sizeof(type))
    87. var offset = 0; // start at the beginning of the buffer
    88. gl.vertexAttribPointer positionAttributeLocation, size, type, normalize, stride, offset);
    89. // draw
    90. var primitiveType = gl.TRIANGLES;
    91. var offset = 0;
    92. var count = 3;
    93. gl.drawArrays(primitiveType, offset, count);
    94. }
    95. // 开始启动
    96. main();
    97. // Html
    98. <canvas id="c"></canvas>
    99. <script id="vertex-shader-2d" type="notjs">
    100. // an attribute will receive data from a buffer
    101. attribute vec4 a_position;
    102. // all shaders have a main function
    103. void main() {
    104. // gl_Position is a special variable a vertex shader
    105. // is responsible for setting
    106. gl_Position = a_position;
    107. }
    108. </script>
    109. <script id="fragment-shader-2d" type="notjs">
    110. // fragment shaders don't have a default precision so we need
    111. // to pick one. mediump is a good default
    112. precision mediump float;
    113. void main() {
    114. // gl_FragColor is a special variable a fragment shader
    115. // is responsible for setting
    116. gl_FragColor = vec4(1, 0, 0.5, 1); // return redish-purple
    117. }
    118. </script>
    119. <!--
    120. for most samples webgl-utils only provides shader compiling/linking and
    121. canvas resizing because why clutter the examples with code that's the same in every sample.
    122. See https://webglfundamentals.org/webgl/lessons/webgl-boilerplate.html
    123. and https://webglfundamentals.org/webgl/lessons/webgl-resizing-the-canvas.html
    124. for webgl-utils, m3, m4, and webgl-lessons-ui.
    125. -->
    126. <script src="https://webglfundamentals.org/webgl/resources/webgl-utils.js"></script>