What is glVertexAttrib (versus glVertexAttribPointer) used for?

╄→尐↘猪︶ㄣ 提交于 2019-12-04 15:28:59

问题


I understand that glVertexAttribPointer will set the values for a vertex attribute based on the pointed-to array. What is glVertexAttrib for, though? It looks like it just sets a single (possibly vector) value for the vertex attribute, so what happens when you have multiple vertices? Do all of the vertices end up seeing the same value for the attribute?


回答1:


This was mainly used with the old immediate mode (glBegin/glEnd), where you don't use vertex arrays, which is deprecated (and removed in OpenGL ES 2.0 and desktop OpenGL 3+ core).

But this function still has its use with arrays (that's why it's still there in the modern versions). You are right in your assumption that all vertices following this call have the same value for this attribute (only if you don't enable this attribute's array, of course). Or more exactly every used shader attribute that doesn't have its corresponding array enabled sources its value from a single state value and this value can be changed with glVertexAttrib.

This is usefull if you have a general shader with e.g. a color attribute and a position attribute and you have an object with a constant color. So by using glVertexAttrib you neither have to submit a color for each vertex, nor do you have to use a special shader with the color changed to a uniform.



来源:https://stackoverflow.com/questions/7718976/what-is-glvertexattrib-versus-glvertexattribpointer-used-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!