OpenGL ES中glBindAttribLocation函数的使用

发布于 2024-11-27 19:19:57 字数 247 浏览 0 评论 0原文

我无法在 OpenGL ES 2.0 中使用 glBindAttribLocation 函数

有人能给我完整的上下文吗? 类似吗

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

与 DirectX 10 ?在各个网站上在线阅读,但无法使用此功能。需要帮助..

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 online in various sites, but couldn't get the use of this function. Help needed..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

分开我的手 2024-12-04 19:19:57

通过此函数,您可以为用户定义的着色器属性(而不是统一的,如代码示例所示)指定属性索引,稍后在引用该属性时使用该属性索引。

假设您有一个顶点着色器:

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

然后您可以将索引绑定到这些变量,

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

您可以使用您喜欢的任何非负数(在小范围内)。然后,当引用这些属性时,您可以使用它们的索引,例如在 glVertexAttribPointerglEnableVertexAttribArray 函数中。

但请记住,在链接程序之前必须调用 glBindAttribLocation。您也可以省略它。然后,OpenGL 在链接期间自动将索引绑定到所有使用的属性,稍后可以通过 glGetAttribLocation 查询这些属性。但通过glBindAttribLocation,您可以建立自己的属性索引语义并保持一致。

较新的 GLSL 版本甚至允许您在着色器内指定属性索引(使用 layout 语法),从而不再需要 glBindAttribLocationglGetAttribLocation >,但我不确定ES是否支持这一点。

但我的回答并没有告诉你更多你看过的“各种”网站或任何优秀的 OpenGL/GLSL 书籍,所以如果你仍然不明白,请更深入地研究 GLSL 的基础知识。

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.

拥抱影子 2024-12-04 19:19:57

我从未使用过 Direct X,但 glBindAttribLocation 用于定义着色器中属性的位置,假设您的着色器中有这一行:

attribute vec3 g_LightDir;

然后您可以使用 glBindAttribLocation 来设置该 vec3 的位置,您必须在创建之后执行此操作将顶点着色器附加到着色器程序,但在链接它之前(或者您必须“重新链接”它)。如果您不指定位置,编译器将为您指定位置,您可以使用 glGetAttribLocation 查询位置。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文