I’m reading here.
The initBuffers function is used to store the vertices of the object we need to draw into the Buffer.
var triangleVertexPositionBuffer;
var triangleVertexColorBuffer;
function initBuffers() {
triangleVertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
var vertices = [
0.0, 1.0, 0.0,
-1.0, -1.0, 0.0,
1.0, -1.0, 0.0
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
triangleVertexPositionBuffer.itemSize = 3;
triangleVertexPositionBuffer.numItems = 3;
triangleVertexColorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexColorBuffer);
var colors = [
1.0, 0.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 0.0, 1.0, 1.0,
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
triangleVertexColorBuffer.itemSize = 4;
triangleVertexColorBuffer.numItems = 3;
}
Now this is some kind of a “shape” defined for a triangle. Now, if I want shapes of all types and sizes I would have to keep a buffer like triangleVertexPositionBuffer for each shape ,right? And it has to be initialized right?
Is new buffer object for every new shape is a must?
I plan to create a system, where all kinds of shapes can be created during runtime say by using a button.
So is there a solution to this issue?
How do I create new Buffers during runtime? Is there a common already existing solution?
Will it hinder performance if I create an array of buffer objects?
WebGl doesn’t care if you call initBuffers every frame. Well, the performance might.
As javascript is interpreted language, the concept of runtime vs. compile time doesn’t really exist. One advanced thing however is that the shaders required by webgl can be generated from “literal” strings, generated at runtime as well as from external files and html elements.
There are webgl-infrastructures available e.g. three.js, but I think Javascript itself is the answer.
EDIT: About your additional question in the comment. OpenGL and WebGL works that essentially how you described, but with matrices, which are more flexible than just giving an offset to the object. Also there are ‘index-buffers’, that help sharing vertices between triangles.
With these arrays one typically renders a hierarchical scene, where vertices and objects are reused. On can place the cube not only in two different positions, but to different scale and different orientation. Then once the scene is modeled in “World coordinates”, one places the camera to some place and some orientation and applies also a perspective matrix to transform the coordinates to “clip space” that models screen coordinates x,y and depth z.
The beauty of these matrices is that one can premultiply each of these into a single matrixthat performs everything mentioned. Or one can split this chain of operation into the best possible amount of operations, that are either calculated on the fly in shaders, or are precalculated in the vertex buffers. One should think carefully an optimal balance of reusing objects, as the bottleneck in rendering is typically the amount of drawElements/drawArray -calls and state changes (gl.useProgram) instead of the number of vertices per object.