webgl基本原理流程
1.创建编译片元和顶点着色器
const program = gl.createProgram();
const vertexShader = gl.createShader(gl.VERTEX_SHADER)
gl.shaderSource(vertexShader , 顶点着色器)
gl.compileShader(vertexShader)
gl.attachShader(program , vertexShader)
const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER)
gl.shaderSource(fragmentShader , 片元着色器)
gl.compileShader(fragmentShader)
gl.attachShader(program , fragmentShader)
gl.linkProgram(program )
// 销毁
gl.detachShader(program , vertexShader)
gl.deleteShader(vertexShader)
gl.detachShader(program , fragmentShader)
gl.deleteShader(fragmentShader)
2. 获取到着色器中arrtibute和uniform的指向位置,就是确定标记,用于后续用从gl.ARRAY_BUFFER中数据给着色器中对应attribute赋值
// getAttribLocation方法返回了给定WebGLProgram对象中某属性的下标指向位置
const prog1Locs = {
position: gl.getAttribLocation(program , 'position'),
color: gl.getUniformLocation(program , 'color'),
};
3.创建VBO,并给对应创建的Buffer里面写数据,
1.每次需要先将gl.ARRAY_BUFFER指向对应的Buffer,再写数据,写完再复位指向Null
2.向对应着色器中的attribute传值
const position1Buffer = gl.createBuffer();
// gl.ARRAY_BUFFER指向上一步创建的缓冲区对象position1Buffer
gl.bindBuffer(gl.ARRAY_BUFFER, position1Buffer);
// 向缓冲区写入数据
gl.bufferData(gl.ARRAY_BUFFER, Float32Array类型的顶点数组, gl.STATIC_DRAW);
// gl.ARRAY_BUFFER指向复位
gl.bindBuffer(gl.ARRAY_BUFFER, null);
gl.bindBuffer(gl.ARRAY_BUFFER, position1Buffer);
// 不被激活的属性是不会被使用的,一旦激活,以下其他方法就可以获取到属性的值了:
// 包括
// vertexAttribPointer()
// vertexAttrib*()
// getVertexAttrib()
gl.enableVertexAttribArray(prog1Locs.position);
// 绑定当前缓冲区范围到gl.ARRAY_BUFFER,成为当前顶点缓冲区对象的通用顶点属性并指定它的布局(缓冲区对象中的偏移量)。
// 也就是这里从gl.ARRAY_BUFFER指向的buffer里面拿数据,然后指向顶点着色器里面对应的attribute
gl.vertexAttribPointer(
prog1Locs.position, // location
2, // size (components per iteration)
gl.FLOAT, // type of to get from buffer
false, // normalize
0, // stride (bytes to advance each iteration)
0, // offset (bytes from start of buffer)
);
// 销毁
gl.bindBuffer(gl.ARRAY_BUFFER, null);
4.绘制
// 上面数据写入和对应关系好了
gl.useProgram(program ); // 使用上一步的顶点、片元着色器
// orange
gl.uniform4fv(prog1Locs.color, [1, 0.7, 0.5, 1]);
// draw 12 points
gl.drawArrays(gl.POINTS, 0, 12)
5.完整画两个物体不同材质
const canvas = document.querySelector('canvas');
const gl = canvas.getContext('webgl');
const vsGLSL1 = `
attribute vec4 position;
void main() {
gl_Position = position;
gl_PointSize = 10.0;
}
`;
const fsGLSL1 = `
precision highp float;
uniform vec4 color;
void main() {
gl_FragColor = color;
}
`;
const vsGLSL2 = `
attribute vec4 position;
attribute vec4 color;
varying vec4 v_color;
void main() {
gl_Position = position;
gl_PointSize = 10.0;
v_color = color;
}
`;
const fsGLSL2 = `
precision highp float;
varying vec4 v_color;
void main() {
gl_FragColor = v_color;
}
`;
const createShader = function(gl, type, glsl) {
const shader = gl.createShader(type)
gl.shaderSource(shader, glsl)
gl.compileShader(shader)
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
throw new Error(gl.getShaderInfoLog(shader))
}
return shader
};
const compileShadersAndLinkProgram = function(gl, prg, vsGLSL, fsGLSL) {
const vertexShader = createShader(gl, gl.VERTEX_SHADER, vsGLSL)
const fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fsGLSL)
gl.attachShader(prg, vertexShader)
gl.attachShader(prg, fragmentShader)
gl.linkProgram(prg)
if (!gl.getProgramParameter(prg, gl.LINK_STATUS)) {
throw new Error(gl.getProgramInfoLog(prg))
}
// NOTE! These are only here to unclutter the diagram.
// It is safe to detach and delete shaders once
// a program is linked though it is arguably not common.
// and I usually don't do it.
gl.detachShader(prg, vertexShader)
gl.deleteShader(vertexShader)
gl.detachShader(prg, fragmentShader)
gl.deleteShader(fragmentShader)
return prg
};
// program 1 uses 1 attribute and 1 uniform
const prog1 = gl.createProgram();
compileShadersAndLinkProgram(gl, prog1, vsGLSL1, fsGLSL1);
const prog1Locs = {
position: gl.getAttribLocation(prog1, 'position'),
color: gl.getUniformLocation(prog1, 'color'),
};
// program 2 uses 2 attributes and no uniforms
const prog2 = gl.createProgram();
compileShadersAndLinkProgram(gl, prog2, vsGLSL2, fsGLSL2);
const prog2Locs = {
position: gl.getAttribLocation(prog2, 'position'),
color: gl.getAttribLocation(prog2, 'color'),
};
// vertex positions for a ⃟
const prog1VertexPositions = new Float32Array([
-0.2, 0,
-0.8, 0,
-0.3, -0.2,
-0.7, -0.2,
-0.3, 0.2,
-0.7, 0.2,
-0.4, 0.4,
-0.6, 0.4,
-0.4, -0.4,
-0.6, -0.4,
-0.5, 0.6,
-0.5, -0.6,
]);
// vertex positions for a +
const prog2VertexPositions = new Float32Array([
0.5, 0,
0.5, -0.2,
0.5, 0.2,
0.5, -0.4,
0.5, 0.4,
0.4, 0,
0.6, 0,
0.3, 0,
0.7, 0,
]);
const prog2VertexColors = new Float32Array([
1, 0, 0, // red
0, 0, 1, // blue
0, 0, 1,
0, 1, 0, // green
0, 1, 0,
0, 1, 1, // cyan
0, 1, 1,
1, 0, 1, // magenta
1, 0, 1,
]);
const position1Buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, position1Buffer);
gl.bufferData(gl.ARRAY_BUFFER, prog1VertexPositions, gl.STATIC_DRAW);
const position2Buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, position2Buffer);
gl.bufferData(gl.ARRAY_BUFFER, prog2VertexPositions, gl.STATIC_DRAW);
const color2Buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, color2Buffer);
gl.bufferData(gl.ARRAY_BUFFER, prog2VertexColors, gl.STATIC_DRAW);
// this is not needed. It's just here to unclutter the diagram
gl.bindBuffer(gl.ARRAY_BUFFER, null);
// above this line is initialization code
// --------------------------------------
// below is rendering code.
// --------------------------------------
// First draw a diamond of points with the first program
// set the attributes to what's needed for program 1
gl.bindBuffer(gl.ARRAY_BUFFER, position1Buffer);
gl.enableVertexAttribArray(prog1Locs.position);
gl.vertexAttribPointer(
prog1Locs.position, // location
2, // size (components per iteration)
gl.FLOAT, // type of to get from buffer
false, // normalize
0, // stride (bytes to advance each iteration)
0, // offset (bytes from start of buffer)
);
// this is not needed. It's just here to unclutter the diagram
gl.bindBuffer(gl.ARRAY_BUFFER, null);
gl.useProgram(prog1);
// orange
gl.uniform4fv(prog1Locs.color, [1, 0.7, 0.5, 1]);
// draw 12 points
gl.drawArrays(gl.POINTS, 0, 12);
// --------------------------------------
// Second, draw a plus of points with the second program
// set the attributes to what's needed for program 1
gl.bindBuffer(gl.ARRAY_BUFFER, position2Buffer);
gl.enableVertexAttribArray(prog2Locs.position);
gl.vertexAttribPointer(
prog2Locs.position, // location
2, // size (components per iteration)
gl.FLOAT, // type of to get from buffer
false, // normalize
0, // stride (bytes to advance each iteration)
0, // offset (bytes from start of buffer)
);
gl.bindBuffer(gl.ARRAY_BUFFER, color2Buffer);
gl.enableVertexAttribArray(prog2Locs.color);
gl.vertexAttribPointer(
prog2Locs.color, // location
3, // size (components per iteration)
gl.FLOAT, // type of to get from buffer
false, // normalize
0, // stride (bytes to advance each iteration)
0, // offset (bytes from start of buffer)
);
// this is not needed. It's just here to unclutter the diagram
gl.bindBuffer(gl.ARRAY_BUFFER, null);
gl.useProgram(prog2);
// draw 9 points
gl.drawArrays(gl.POINTS, 0, 9);
335




被折叠的 条评论
为什么被折叠?



