I want to display text using the mentioned method in this article. Meanwhile I care about code to be generic.
In the article it mentions manually creating a buffer info which I call first method:
// Maunally create a bufferInfo
var textBufferInfo = {
attribs: {
a_position: { buffer: gl.createBuffer(), numComponents: 2, },
a_texcoord: { buffer: gl.createBuffer(), numComponents: 2, },
},
numElements: 0,
};
var textVAO = twgl.createVAOFromBufferInfo(
gl, textProgramInfo, textBufferInfo);
and setup render using:
// update the buffers
textBufferInfo.attribs.a_position.numComponents = 2;
gl.bindBuffer(gl.ARRAY_BUFFER, textBufferInfo.attribs.a_position.buffer);
gl.bufferData(gl.ARRAY_BUFFER, vertices.arrays.position, gl.DYNAMIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, textBufferInfo.attribs.a_texcoord.buffer);
gl.bufferData(gl.ARRAY_BUFFER, vertices.arrays.texcoord, gl.DYNAMIC_DRAW);
opposed to this second method:
// Create data for 'F'
var fBufferInfo = twgl.primitives.create3DFBufferInfo(gl);
var fVAO = twgl.createVAOFromBufferInfo(
gl, fProgramInfo, fBufferInfo);
and setup render:
// setup the attributes and buffers for the F
gl.bindVertexArray(fVAO);
So I thought this means, on initialization time, I can setup a VAO like this:
const makeVao = (bufferInfos) => {
let vao = gl.createVertexArray();
gl.bindVertexArray(vao);
bufferInfos.forEach(({
array,
size,
index
}) => {
let buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(array), gl.STATIC_DRAW);
gl.enableVertexAttribArray(index);
gl.vertexAttribPointer(index,
size,
gl.FLOAT,
false,
0,
0);
});
gl.bindVertexArray(null);
return vao;
};
With bufferInfos
usage:
let bufferInfos = [{
array: [vertices],
size: 2,
index: gl.getAttribLocation(program, name)
}];
This will setup attributes and give me a VAO that I can use at render time like:
gl.bindVertexArray(vao);
then I am done.
Except, I want the first method, where I can set vertices attributes on each render. So how do I set up the generic code to be able to set shader attributes on render time?