Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does glDrawArrays know what to draw?

I am following some begginer OpenGL tutorials, and am a bit confused about this snippet of code:

glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject); //Bind GL_ARRAY_BUFFER to our handle glEnableVertexAttribArray(0); //? glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); //Information about the array, 3 points for each vertex, using the float type, don't normalize, no stepping, and an offset of 0. I don't know what the first parameter does however, and how does this function know which array to deal with (does it always assume we're talking about GL_ARRAY_BUFFER?  glDrawArrays(GL_POINTS, 0, 1); //Draw the vertices, once again how does this know which vertices to draw? (Does it always use the ones in GL_ARRAY_BUFFER)  glDisableVertexAttribArray(0); //? glBindBuffer(GL_ARRAY_BUFFER, 0); //Unbind 

I don't understand how glDrawArrays knows which vertices to draw, and what all the stuff to do with glEnableVertexAttribArray is. Could someone shed some light on the situation?

like image 656
w4etwetewtwet Avatar asked Sep 30 '13 19:09

w4etwetewtwet


People also ask

How does glDrawArrays work?

With glDrawArrays, OpenGL pulls data from the enabled arrays in order, vertex 0, then vertex 1, then vertex 2, and so on. With glDrawElements, you provide a list of vertex numbers. OpenGL will go through the list of vertex numbers, pulling data for the specified vertices from the arrays.

What is the difference between glDrawArrays () and glDrawElements ()?

What's the difference between glDrawArrays and glDrawElements ? Can anyone help me? glDrawArrays submits the vertices in linear order, as they are stored in the vertex arrays. With glDrawElements you have to supply an index buffer.

What is glDrawElements?

When glDrawElements is called, it uses count sequential elements from an enabled array, starting at indices to construct a sequence of geometric primitives. mode specifies what kind of primitives are constructed and how the array elements construct these primitives. If more than one array is enabled, each is used.

What is glEnableVertexAttribArray?

Description. glEnableVertexAttribArray enables the generic vertex attribute array specified by index . glDisableVertexAttribArray disables the generic vertex attribute array specified by index . By default, all client-side capabilities are disabled, including all generic vertex attribute arrays.


2 Answers

The call to glBindBuffer tells OpenGL to use vertexBufferObject whenever it needs the GL_ARRAY_BUFFER.

glEnableVertexAttribArray means that you want OpenGL to use vertex attribute arrays; without this call the data you supplied will be ignored.

glVertexAttribPointer, as you said, tells OpenGL what to do with the supplied array data, since OpenGL doesn't inherently know what format that data will be in.

glDrawArrays uses all of the above data to draw points.

Remember that OpenGL is a big state machine. Most calls to OpenGL functions modify a global state that you can't directly access. That's why the code ends with glDisableVertexAttribArray and glBindBuffer(..., 0): you have to put that global state back when you're done using it.

like image 80
Steve Howard Avatar answered Oct 02 '22 17:10

Steve Howard


DrawArrays takes data from ARRAY_BUFFER.

Data are 'mapped' according to your setup in glVertexAttribPointer which tells what is the definition of your vertex.

In your example you have one vertex attrib (glEnableVertexAttribArray) at position 0 (you can normally have 16 vertex attribs, each 4 floats). Then you tell that each attrib will be obtained by reading 3 GL_FLOATS from the buffer starting from position 0.

like image 42
fen Avatar answered Oct 02 '22 18:10

fen