I do not know if this will help. As some people have said, GL / WebGL has a bunch of internal state . All functions that you call set the state. When all settings you call drawArraysor drawElements, and all this state is used to draw things
This is explained elsewhere on SO, but the buffer binding just sets 1 out of 2 global variables inside WebGL. After that, you refer to the buffer by its anchor point.
You can think of it this way:
gl = function() {
let lastError;
let arrayBuffer = null;
let vertexArray = {
elementArrayBuffer: null,
attributes: [
{ enabled: false, type: gl.FLOAT, size: 3, normalized: false,
stride: 0, offset: 0, value: [0, 0, 0, 1], buffer: null },
{ enabled: false, type: gl.FLOAT, size: 3, normalized: false,
stride: 0, offset: 0, value: [0, 0, 0, 1], buffer: null },
{ enabled: false, type: gl.FLOAT, size: 3, normalized: false,
stride: 0, offset: 0, value: [0, 0, 0, 1], buffer: null },
{ enabled: false, type: gl.FLOAT, size: 3, normalized: false,
stride: 0, offset: 0, value: [0, 0, 0, 1], buffer: null },
{ enabled: false, type: gl.FLOAT, size: 3, normalized: false,
stride: 0, offset: 0, value: [0, 0, 0, 1], buffer: null },
...
],
}
...
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;
}
};
...
}();
After that, other WebGL functions reference them. For example, it gl.bufferDatacan do something like
this.bufferData = function(bindPoint, data, usage) {
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;
}
buffer.copyData(data);
buffer.setUsage(usage);
};
. . , . gl.getAttribLocation(someProgram, "nameOfAttribute") , .
, 4 , , . gl.enableVertexAttribArray, gl.disableVertexAttribArray, gl.vertexAttribPointer gl.vertexAttrib??.
-
this.enableVertexAttribArray = function(location) {
const attribute = vertexArray.attributes[location];
attribute.enabled = true;
};
this.disableVertexAttribArray = function(location) {
const attribute = vertexArray.attributes[location];
attribute.enabled = false;
};
this.vertexAttribPointer = function(location, size, type, normalized, stride, offset) {
const attribute = vertexArray.attributes[location];
attribute.size = size;
attribute.type = type;
attribute.normalized = normalized;
attribute.stride = stride;
attribute.offset = offset;
attribute.buffer = arrayBuffer;
};
this.vertexAttrib4f = function(location, x, y, z, w) {
const attribute = vertexArray.attributes[location];
attribute.value[0] = x;
attribute.value[1] = y;
attribute.value[2] = z;
attribute.value[3] = w;
};
, gl.drawArrays gl.drawElements, , , , . . , .
, , , drawElements drawArrays, , . # 1 # 2 , 3 , 6 gl.drawArrays, . , , gl.ELEMENT_ARRAY_BUFFER, , > 2, index out of range. 3 , 0, 1 2.
, , - , , . ? , , , , , , . , . , , , . , , , . , , , .
, , , . , 10 . , , , , , .
, [ OES_vertex_array_object], WebGL 2.0. Vertex Array - , vertexArray, elementArrayBuffer .
gl.createVertexArray . gl.bindVertexArray attributes , .
gl.bindVertexArray
this.bindVertexArray = function(vao) {
vertexArray = vao ? vao : defaultVertexArray;
}
, init, 1 WebGL .