Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's 'glBufferData' for in OpenGL ES?

Tags:

opengl-es

I run across a sample OpenGL code that I ported to OpenGL ES 2.0 (there wasn't much to be done actually), but I cannot help wondering what the glBufferData function is for. The original source is like that:

glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 2 * 6, quad, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 2, (void *) 0);

But I can successfully simplify it as:

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 2, quad);

That is, I can omit the glBufferData function just by using a valid pointer to the quad array in glVertexAttribPointer.

So, could anyone explain what's the glBufferData function for? From what I'm doing it seems to be redundant but that must be because of my serious lack of knowledge of the API. As a matter of fact I tried reading the docs at khronos.org but this didn't help me understand its use.

like image 544
Albus Dumbledore Avatar asked Mar 23 '11 08:03

Albus Dumbledore


People also ask

Does glBufferData copy data?

glBufferSubData is like memcpy ; it copies data into existing storage. Just as you can't memcpy without allocating first, you can't call glBufferSubData without calling glBufferData first.

What are buffer objects?

Buffer Objects are OpenGL Objects that store an array of unformatted memory allocated by the OpenGL context (AKA the GPU). These can be used to store vertex data, pixel data retrieved from images or the framebuffer, and a variety of other things.


1 Answers

If you are reusing the same data in multiple frames, using glBufferData as part of your setup/initialization will transfer data from the CPU to the GPU only once. Whereas glVertexAttribPointer must be called every frame, so using it to transfer data results in using a lot more bus bandwidth.

If you're updating the attribute array every frame, there's not much advantage one way or the other.

like image 183
Ben Voigt Avatar answered Sep 18 '22 14:09

Ben Voigt