Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

glEnableClientState with modern OpenGL (glVertexAttribPointer etc)

Tags:

opengl

I'd like to lay out some things I think I've learned, but am unsure about:

  • VBOs are the way to go. They're created with glGenBuffers and glBufferData.
  • For maximum flexibility, it's best to pass generic vertex attributes to shaders with glVertexAttribPointer, rather than glVertex, glNormal, etc..
  • glDrawElements can be used with vertex buffers and an index buffer to efficiently render geometry with lots of shared vertices, such as a landscape mesh.

Assuming all of that is correct so far, here's my question. All of the tutorials I've read about modern OpenGL completely omit glEnableClientState. But the OpenGL man pages say that without glEnableClientState, glDrawElements will do nothing:

http://www.opengl.org/sdk/docs/man/xhtml/glDrawElements.xml

The key passage is: "If GL_VERTEX_ARRAY is not enabled, no geometric primitives are constructed."

This leads me to the following questions:

  • None of the tutorials use glEnableClientState before calling glDrawElements. Does this mean the man page is wrong or outdated?
  • GL_VERTEX_ARRAY would seem to be the thing you enable if you're going to use glVertexPointer, and likewise you'd use GL_NORMAL_ARRAY with glNormalPointer, and so on. But if I'm not using those functions, and am instead using generic vertex attributes with glVertexAttribPointer, then why would it be necessary to enable GL_VERTEX_ARRAY?
like image 755
rlkw1024 Avatar asked Dec 18 '11 06:12

rlkw1024


2 Answers

If GL_VERTEX_ARRAY is not enabled, no geometric primitives are constructed.

That's because the man page is wrong. The man page covers GL 2.1 (and it's still wrong for that), and for whatever reason, the people updating the man page refuse to update the older GL versions for bug fixes.

In GL 2.1, you must use either generic attribute index 0 or GL_VERTEX_ARRAY. In GL 3.1+, you don't need to use any specific attribute indices.

This is because, in GL versions before 3.1, all array rendering functions were defined in terms of calls to glArrayElement, which used immediate mode-based rendering. That means that you need something to provoke the vertex. Recall that, in immediate mode, calling glVertex*() not only sets the vertex position, it also causes the vertex to be sent with the other attributes. Calling glVertexAttrib*(0, ...) does the same thing. That's why older versions require you to use either attribute 0 or GL_VERTEX_ARRAY.

In GL 3.1+, once they took out immediate mode, they had to specify array rendering differently. And because of that, they didn't have to limit themselves to using attribute 0.

If you want API docs for core GL 3.3 works, I suggest you look at the actual API docs for core GL 3.3. Though to be honest, I'd just look at the spec if you want accurate information. Those docs have a lot of misinformation in them. And since they're not actually wiki-pages, that information never gets corrected.

like image 62
Nicol Bolas Avatar answered Nov 08 '22 01:11

Nicol Bolas


Your first 3 points are correct. And to answer your last half of your question, do not use glEnableClientState for modern OpenGL. Start coding!

like image 32
cmannett85 Avatar answered Nov 08 '22 02:11

cmannett85