开发者

Use of glBindAttribLocation function in OpenGL ES

开发者 https://www.devze.com 2023-03-24 01:47 出处:网络
I dont get the use of glBindAttribLocation function in OpenGL ES 2.0 Can some one give me the full context ? Is it something like

I dont get the use of glBindAttribLocation function in OpenGL ES 2.0

Can some one give me the full context ? Is it something like

g_pLightDir = g_pEffect10->GetVariableByName( "g_LightDir" )->AsVector();

in DirectX 10 ? Read onli开发者_如何学运维ne in various sites, but couldn't get the use of this function. Help needed..


This function let's you specify an attribute index for a user defined shader attribute (not for a uniform, as your code sample suggests), which you later use when refering to the attribute.

Say you have a vertex shader:

...
attribute vec4 vertex;     //or 'in vec4 vertex' in modern syntax
attribute vec3 normal;
...

You then can bind indices to those variables, with

glBindAttribLocation(program, 0, "vertex");
glBindAttribLocation(program, 1, "normal");

You can use whatever non-negative numbers you like (in a small range). Then when refering to these attributes you use their indices, e.g. in glVertexAttribPointer or glEnableVertexAttribArray functions.

But keep in mind that glBindAttribLocation has to be called before linking the program. You can also omit it. Then OpenGL automatically binds indices to all used attributes during linking, which can later be queried by glGetAttribLocation. But with glBindAttribLocation you can establish your own attribute index semantics and keep them consistent.

The newer GLSL versions even allow you to specify the attribute indices within the shader (using the layout syntax), removing the need for either glBindAttribLocation or glGetAttribLocation, but I'm not sure if this is supported in ES.

But my answer doesn't tell you more than those "various" sites you looked at or any good OpenGL/GLSL book, so if you still don't get it, delve a little deeper into the basics of GLSL.


I never used Direct X, but glBindAttribLocation is used to define the location for a Attribute in your shader, say you have this row in your shader:

attribute vec3 g_LightDir;

Then you can use glBindAttribLocation to set a location for that vec3, you must do this after you have attached your vertex-shader to your shader-program but before you link it (or you have to "relink" it). If you dont specify a location the compiler will do it for you and you can query the location with glGetAttribLocation.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号