62

I was reading: Dissecting Display, Chapter 1. Hello, Triangle!.

What exactly does glEnableVertexAttribArray do? I think it enables the use of a given VBO, but I am not sure.

I thought that is what glEnableClientState(GL_VERTEX_ARRAY) did.

LunchMarble
  • 5,079
  • 9
  • 64
  • 94

3 Answers3

46

I have been reading: Dissecting Display, Chapter 1. Hello, Triangle!.

Then please read the next page; I explain exactly what it does there ;)

If I may quote myself:

We assigned the attribute index of the position attribute to 0 in the vertex shader, so the call to glEnableVertexAttribArray(0) enables the attribute index for the position attribute. [...] If the attribute is not enabled, it will not be used during rendering.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • 2
    That was epic fail on my part. Just to clarify though: calling glEnableVertexAttribArray(0) enables the use of the vertex attribute at index 0 in the vertex shader. Whether or not it is a position attribute is irrelevant right? – LunchMarble Aug 09 '11 at 17:37
  • @Storm: Yes, that's the idea. – Nicol Bolas Aug 09 '11 at 17:42
  • Thank you so much for your help. I went and re-read that whole section. :D – LunchMarble Aug 09 '11 at 17:45
  • @NicolBolas do you have source code for the tutorials on your site? Much of the book mentions the example projects, but the book has no links to the full code, just the inline snippets. – Alex Kuhl Feb 12 '13 at 14:45
  • 2
    @NicolBolas Thanks! I would suggest adding a more explicit source code link to the main page (convention seems to be making source links very obvious); I skipped that introductory section thinking it'd be compiler tips and environment setup, which I did not need help with. – Alex Kuhl Feb 12 '13 at 19:13
  • 61
    'glEnableVertexAttribArray(0) enables the attribute index for the position attribute' - doesn't explain anything. Why we need to enable it and when? We call glVertexAttribPointer - to indicate where ogl should take vertices or other data for shader input, why then we need to call this additional function? Should i call this method every time active shader program changed, if vertex shader? – Zakus Oct 20 '13 at 19:57
  • 1
    Maybe I'm slow, but what exactly does "enables the attribute index" mean? Does it simply mean that the 'position' attribute will be 'feed' the data from the VBO? Is this state set for the shder, VBO, VBA or OpenGL context as a whole? (Otherwise your book is excellent BTW) – Ken Apr 09 '14 at 13:51
  • @NicolBolas http://arcsynthesis.org domain has expired yesterday. Will you renew it? – nikagra Mar 19 '15 at 07:35
  • @NicolBolas your domain arcsynthesis.org seems to have expired two days ago. I was hoping to read your tutorials, are you going to renew it? – peedee Mar 19 '15 at 09:58
  • 9
    This isn't much of an explanation. What does "it will not be used during rendering" mean? The code in the shader accessing it doesn't magically go away. – Glenn Maynard Jan 12 '16 at 21:20
  • @NicolBolas I don't know where else to post this remark... So I must say that to me it is truly the best OpenGL tutorial I have seen so far (paid or free). Even if some will claim it leaves a thing or two open to interpretation or rather... experimentation. – omahena Feb 08 '19 at 09:06
  • I've seen many examples that claim you have to enable it to begin rendering and disable it after ending rendering. Someone told me on a forum that this is saved locally to the object, so you can enable it once unless you actually need it disabled. How valid is this, as this is what I do and it works every time. – user2262111 Jun 05 '19 at 02:31
5

There is another way to specify data for a vertex attribute, the non-array way:

khronos: glVertexAttrib

In this way, we specify data for only one vertex, this data will be replicated for all vertices.

To use non-array data, call glDisableVertexAttribArray.

To use array data, call glEnableVertexAttribArray.

Nghia Bui
  • 3,694
  • 14
  • 21
5

It's similar to a vertex array but stores additional extra values for each point rather than it's coordinates. You might want to store a temperature, density, signal strength at each point in a modelling app for example, or perhaps a damage value for a game

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263