我不知道这是否有帮助。正如有些人所说,GL/WebGL 有一堆内部的state。您调用的所有函数都会设置状态。当一切都设置完毕后,你可以打电话drawArrays
or drawElements
所有这些状态都用来绘制东西
这已在 SO 的其他地方进行了解释,但绑定缓冲区只是在 WebGL 内设置 2 个全局变量中的 1 个。之后,您可以通过其绑定点来引用缓冲区。
你可以这样想
gl = function() {
// internal WebGL state
let lastError;
let arrayBuffer = null;
let vertexArray = {
elementArrayBuffer: null,
attributes: [
{ enabled: false, type: gl.FLOAT, size: 3, normalized: false,
stride: 0, offset: 0, buffer: null },
{ enabled: false, type: gl.FLOAT, size: 3, normalized: false,
stride: 0, offset: 0, buffer: null },
{ enabled: false, type: gl.FLOAT, size: 3, normalized: false,
stride: 0, offset: 0, buffer: null },
{ enabled: false, type: gl.FLOAT, size: 3, normalized: false,
stride: 0, offset: 0, buffer: null },
{ enabled: false, type: gl.FLOAT, size: 3, normalized: false,
stride: 0, offset: 0, buffer: null },
...
],
}
// these values are used when a vertex attrib is disabled
let attribValues = [
[0, 0, 0, 1],
[0, 0, 0, 1],
[0, 0, 0, 1],
[0, 0, 0, 1],
[0, 0, 0, 1],
...
];
...
// Implementation of gl.bindBuffer.
// note this function is doing nothing but setting 2 internal variables.
this.bindBuffer = function(bindPoint, buffer) {
switch(bindPoint) {
case gl.ARRAY_BUFFER;
arrayBuffer = buffer;
break;
case gl.ELEMENT_ARRAY_BUFFER;
vertexArray.elementArrayBuffer = buffer;
break;
default:
lastError = gl.INVALID_ENUM;
break;
}
};
...
}();
之后其他 WebGL 函数引用这些函数。例如gl.bufferData
可能会做类似的事情
// implementation of gl.bufferData
// Notice you don't pass in a buffer. You pass in a bindPoint.
// The function gets the buffer one of its internal variable you set by
// previously calling gl.bindBuffer
this.bufferData = function(bindPoint, data, usage) {
// lookup the buffer from the bindPoint
var buffer;
switch (bindPoint) {
case gl.ARRAY_BUFFER;
buffer = arrayBuffer;
break;
case gl.ELEMENT_ARRAY_BUFFER;
buffer = vertexArray.elemenArrayBuffer;
break;
default:
lastError = gl.INVALID_ENUM;
break;
}
// copy data into buffer
buffer.copyData(data); // just making this up
buffer.setUsage(usage); // just making this up
};
与这些绑定点分开的是许多属性。默认情况下,属性也是全局状态。它们定义如何从缓冲区中提取数据以提供给顶点着色器。呼唤gl.getAttribLocation(someProgram, "nameOfAttribute")
告诉您顶点着色器将查看哪个属性以从缓冲区中获取数据。
因此,您可以使用 4 个函数来配置属性如何从缓冲区获取数据。gl.enableVertexAttribArray
, gl.disableVertexAttribArray
, gl.vertexAttribPointer
, and gl.vertexAttrib??
.
他们有效地实施了这样的事情
this.enableVertexAttribArray = function(location) {
const attribute = vertexArray.attributes[location];
attribute.enabled = true; // true means get data from attribute.buffer
};
this.disableVertexAttribArray = function(location) {
const attribute = vertexArray.attributes[location];
attribute.enabled = false; // false means get data from attribValues[location]
};
this.vertexAttribPointer = function(location, size, type, normalized, stride, offset) {
const attribute = vertexArray.attributes[location];
attribute.size = size; // num values to pull from buffer per vertex shader iteration
attribute.type = type; // type of values to pull from buffer
attribute.normalized = normalized; // whether or not to normalize
attribute.stride = stride; // number of bytes to advance for each iteration of the vertex shader. 0 = compute from type, size
attribute.offset = offset; // where to start in buffer.
// IMPORTANT!!! Associates whatever buffer is currently *bound* to
// "arrayBuffer" to this attribute
attribute.buffer = arrayBuffer;
};
this.vertexAttrib4f = function(location, x, y, z, w) {
const attrivValue = attribValues[location];
attribValue[0] = x;
attribValue[1] = y;
attribValue[2] = z;
attribValue[3] = w;
};
现在,当你打电话时gl.drawArrays
or gl.drawElements
系统知道您希望如何从您为提供顶点着色器而创建的缓冲区中提取数据。请参阅此处了解其工作原理 http://webglfundamentals.org/webgl/lessons/webgl-how-it-works.html.
由于属性是全局状态这意味着每次你打电话drawElements
or drawArrays
属性的设置方式决定了它们的使用方式。如果您将属性 #1 和 #2 设置为每个都有 3 个顶点的缓冲区,但您要求绘制 6 个顶点gl.drawArrays
你会得到一个错误。同样,如果您创建一个索引缓冲区并将其绑定到gl.ELEMENT_ARRAY_BUFFER
绑定点并且该缓冲区的索引 > 2 你会得到index out of range
错误。如果你的缓冲区只有 3 个顶点,那么唯一有效的索引是0
, 1
, and 2
.
通常,每次绘制不同的东西时,您都会重新绑定绘制该东西所需的所有属性。绘制一个具有位置和法线的立方体?将缓冲区与位置数据绑定,设置用于位置的属性,将缓冲区与法线数据绑定,设置用于法线的属性,现在绘制。接下来,您绘制一个具有位置、顶点颜色和纹理坐标的球体。绑定包含位置数据的缓冲区,设置用于位置的属性。绑定包含顶点颜色数据的缓冲区,设置用于顶点颜色的属性。绑定包含纹理坐标的缓冲区,设置用于纹理坐标的属性。
唯一不重新绑定缓冲区的情况是多次绘制同一事物时。例如绘制 10 个立方体。您需要重新绑定缓冲区,然后为一个立方体设置制服,绘制它,为下一个立方体设置制服,绘制它,重复。
我还应该补充一点,有一个扩展名 [OES_vertex_array_object
] 这也是WebGL 2.0的一个特性。顶点数组对象是上面称为的全局状态vertexArray
其中包括elementArrayBuffer
和所有属性。
Calling gl.createVertexArray
制作新的其中之一。呼唤gl.bindVertexArray
设置全局attributes
指向绑定的 vertexArray 中的那个。
Calling gl.bindVertexArray
那么就会是
this.bindVertexArray = function(vao) {
vertexArray = vao ? vao : defaultVertexArray;
}
这样做的优点是可以让您在初始化时设置所有属性和缓冲区,然后在绘制时只需 1 个 WebGL 调用即可设置所有缓冲区和属性。
这里有一个webgl状态图 https://webglfundamentals.org/webgl/lessons/resources/webgl-state-diagram.html这可能有助于更好地形象化这一点。