My journey into learning WebGL has led me to initialize a vertex buffer with data that is designated for gl.STATIC_DRAW
. According to the documentation on MDN, gl.STATIC_DRAW
is typically used when the vertex data remains constant throughout the application, as explained here:
The contents are intended to be specified once by the application, and used many times as the source for WebGL drawing and image specification commands.
The code snippet I currently have for initializing the vertex buffer is as follows:
const vertices = new Float32Array([
-1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0, -1.0
]);
const n = 4;
const vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
const aPosition = gl.getAttribLocation(program, 'aPosition');
gl.vertexAttribPointer(aPosition, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(aPosition);
However, I now have a need to modify the contents of this buffer by adding new vertex coordinates without the need to reinitialize the entire buffer. This led me to consider using gl.DYNAMIC_DRAW
for more flexibility, as described in the MDN documentation:
The contents are intended to be respecified repeatedly by the application, and used many times as the source for WebGL drawing and image specification commands.
My dilemma now is how to incorporate this change in usage into my current workflow, specifically in providing new vertices to the existing buffer and triggering a redraw. I haven't come across any examples demonstrating this process.
Your insights and guidance on this matter would be greatly appreciated. Thank you.