/* eslint no-console:0 consistent-return:0 */
"use strict"; /* 严格模式 */
/*
*** 创建对应的类型的着色器
*** gl === 渲染上下文
*** type === 着色器类型
*** source === 数据源
*/
function createShader(gl, type, source) {
var shader = gl.createShader(type); // 创建着色器对象
gl.shaderSource(shader, source); // 提供数据源
gl.compileShader(shader); // 编译 -> 生成着色器
var success = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
if (success) {
return shader;
}
console.log(gl.getShaderInfoLog(shader));
gl.deleteShader(shader);
}
/*
*** 创建渲染程序
*** gl === 渲染上下文
*** type === 着色器类型
*** source === 数据源
*/
function createProgram(gl, vertexShader, fragmentShader) {
var program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
var success = gl.getProgramParameter(program, gl.LINK_STATUS);
if (success) {
return program;
}
console.log(gl.getProgramInfoLog(program));
gl.deleteProgram(program);
}
function main() {
// Get A WebGL context 获取对应的绘制上下文 canvas
var canvas = document.querySelector("#c");
var gl = canvas.getContext("webgl");
if (!gl) {
return;
}
// Get the strings for our GLSL shaders 获取对应的glsl程序的数据
var vertexShaderSource = document.querySelector("#vertex-shader-2d").text;
var fragmentShaderSource = document.querySelector("#fragment-shader-2d").text;
// create GLSL shaders, upload the GLSL source, compile the shaders 创建对应的着色器
var vertexShader = createShader(gl, gl.VERTEX_SHADER, vertexShaderSource);
var fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fragmentShaderSource);
// Link the two shaders into a program 建立渲染程序
var program = createProgram(gl, vertexShader, fragmentShader);
// look up where the vertex data needs to go 把对应的定点传入到对应的程序
var positionAttributeLocation = gl.getAttribLocation(program, "a_position");
// 如果从js的一遍遍的向程序添加数据的话;单线程会导致在大量数据的情况会比较卡和缓慢 所以在利用gpu的多线程并行渲染的能力就可以
// 而GPU的数据是从对应的内存缓存中去( butter )
// Create a buffer and put three 2d clip space points in it
var positionBuffer = gl.createBuffer();
// Bind it to ARRAY_BUFFER (think of it as ARRAY_BUFFER = positionBuffer) 绑定对应的类型
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
var positions = [
0, 0,
0, 0.5,
0.7, 0,
];
// 绑定对应的强类型的点到缓存的类型数据上去
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
// code above this line is initialization code.
// code below this line is rendering code.
webglUtils.resizeCanvasToDisplaySize(gl.canvas);
// Tell WebGL how to convert from clip space to pixels 设置对应的视窗效果
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
// Clear the canvas 清空画布
gl.clearColor(0, 0, 0, 0);
gl.clear(gl.COLOR_BUFFER_BIT);
// Tell it to use our program (pair of shaders) 使用渲染程序
gl.useProgram(program);
// Turn on the attribute 激活变量
gl.enableVertexAttribArray(positionAttributeLocation);
// Bind the position buffer. 又来一遍?
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
// Tell the attribute how to get data out of positionBuffer (ARRAY_BUFFER) 告诉属性怎么从positionBuffer中缓存中读取数据
var size = 2; // 2 components per iteration 每次迭代运行提取两个单位数据
var type = gl.FLOAT; // the data is 32bit floats 每个单位的数据类型是32位浮点型
var normalize = false; // don't normalize the data 不需要归一化数据
var stride = 0; // 0 = move forward size * sizeof(type) each iteration to get the next position 0 = 移动单位数量 * 每个单位占用内存(sizeof(type))
var offset = 0; // start at the beginning of the buffer
gl.vertexAttribPointer positionAttributeLocation, size, type, normalize, stride, offset);
// draw
var primitiveType = gl.TRIANGLES;
var offset = 0;
var count = 3;
gl.drawArrays(primitiveType, offset, count);
}
// 开始启动
main();
// Html
<canvas id="c"></canvas>
<script id="vertex-shader-2d" type="notjs">
// an attribute will receive data from a buffer
attribute vec4 a_position;
// all shaders have a main function
void main() {
// gl_Position is a special variable a vertex shader
// is responsible for setting
gl_Position = a_position;
}
</script>
<script id="fragment-shader-2d" type="notjs">
// fragment shaders don't have a default precision so we need
// to pick one. mediump is a good default
precision mediump float;
void main() {
// gl_FragColor is a special variable a fragment shader
// is responsible for setting
gl_FragColor = vec4(1, 0, 0.5, 1); // return redish-purple
}
</script>
<!--
for most samples webgl-utils only provides shader compiling/linking and
canvas resizing because why clutter the examples with code that's the same in every sample.
See https://webglfundamentals.org/webgl/lessons/webgl-boilerplate.html
and https://webglfundamentals.org/webgl/lessons/webgl-resizing-the-canvas.html
for webgl-utils, m3, m4, and webgl-lessons-ui.
-->
<script src="https://webglfundamentals.org/webgl/resources/webgl-utils.js"></script>