tags:

views:

522

answers:

3

the glVertexAttrib family of functions allows to add generic attributes to each vertex. You can set a index that the data will be associated with. However, you can't choose the index arbitrarily, since I discovered that using indices lower than 4 will break standard (Normal/TexCoord) attributes, plus 0 is the vertex position. How can I find out, which will be the first real free index to use for custom attributes?

+2  A: 

You shouldn't be choosing your own indices. GL can provide free ones for you through the glGetAttribLocation function by using the attribute name in code (if you do this after linking the program).

I never encountered this problem... but unfortunately I don't have my own shader wrapper code to hand (at work atm) to be able to explain why.

jheriko
+1  A: 

You can actually use vertex attribute arrays exclusively and thus skip glVertexPointer() and co entirely. This will avoid any double ups and is also nicely unified. You should still use glGetAttribLocation() though, because AFAIK you can't know ahead of time what index each attribute will be mapped to at runtime.

As always, the extension spec has all the gory details.

richard_nz
+2  A: 

I found a website which lists the predefined indices on nvidia hardware:

http://www.opengl.org/sdk/docs/tutorials/ClockworkCoders/attributes.php

Unfortunately there are certain limitations when using this on NVidia Hardware. According to NVidia: "GLSL attempts to eliminate aliasing of vertex attributes but this is integral to NVIDIA’s hardware approach and necessary for maintaining compatibility with existing OpenGL applications that NVIDIA customers rely on. NVIDIA’s GLSL implementation therefore does not allow built-in vertex attributes to collide with a generic vertex attributes that is assigned to a particular vertex attribute index with glBindAttribLocation. For example, you should not use gl_Normal (a built-in vertex attribute) and also use glBindAttribLocation to bind a generic vertex attribute named "whatever" to vertex attribute index 2 because gl_Normal aliases to index 2."

In other words, NVidia hardware indices are reserved for built-in attributes:

  • gl_Vertex 0
  • gl_Normal 2
  • gl_Color 3
  • gl_SecondaryColor 4
  • gl_FogCoord 5
  • gl_MultiTexCoord0 8
  • gl_MultiTexCoord1 9
  • gl_MultiTexCoord2 10
  • gl_MultiTexCoord3 11
  • gl_MultiTexCoord4 12
  • gl_MultiTexCoord5 13
  • gl_MultiTexCoord6 14
  • gl_MultiTexCoord7 15
heeen