I would like to implement a renderer with vertex buffers that will be updated on the application side in each frame. In addition, the number of vertices (i.e., the number of triangles) will also change each frame.
My approach was to pre-allocate the maximum needed once as a Float32Array, and then update only the values ββthat change, and update the buffer data using bufferSubData. Then draw the ones I want by sending the range from the index buffer.
As a minimal example, suppose I select vertex positions for two separate triangles in a Float32Array, and for this frame I just want to move and draw a second triangle. I would think what I would do:
arrPos[9] += 1.0; // move the X coordinates in the Float32Array arrPos[12] += 1.0; arrPos[15] += 1.0; gl.bindBuffer(gl.ARRAY_BUFFER, bufPos); // tell GL which buffer to use gl.bufferSubData( gl.ARRAY_BUFFER, 3 * 3 * 4, arrPos ); // update the vertices - ERROR // then just draw the 2nd traingle by sending its indices only gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, bufId); // tell GL which buffer to use gl.drawElements(gl.TRIANGLES, 3, gl.UNSIGNED_SHORT, 3 * 2); // draw just this range
The problem is that bufferSubData throws an error: "GL ERROR: GL_INVALID_VALUE: glBufferSubData: out of range"
I tried this using offsets 3, 9, 12 for bufferSubData just for that, but they all give me the same error.
In another note: if I can ever get this work, it seems to me that if I want to draw a variable number of triangles for each frame when reusing this previously allocated Float32Array, I will need to update the values ββin the END of the array, and not at the beginning, since I can only specify the offset, not the starting index, in bufferSubData. I hope someone tells me whether this approach can work or not, or if I am completely disconnected and should stop wasting my time.