195k views
1 vote
g Suppose the vertex data is stored in an array named verts and each vertex uses a list to store the coordinates. Write the code that will allocate a buffer in the GPU and store the data into that buffer. Use gl as the WebGL graphics object.

1 Answer

6 votes

Answer:

function createAndFillBufferObject(gl, data) {

var buffer_id;

// Create a buffer object

buffer_id = gl.createBuffer();

if (!buffer_id) {

out.displayError('Failed to create the buffer object for ' + model_name);

return null;

}

// Make the buffer object the active buffer.

gl.bindBuffer(gl.ARRAY_BUFFER, buffer_id);

// Upload the data for this buffer object to the GPU.

gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW);

return buffer_id;

}

User HiteshP
by
6.3k points