OpenGL着色器的显式属性和自动属性位置绑定 [英] Explicit vs Automatic attribute location binding for OpenGL shaders

查看:224
本文介绍了OpenGL着色器的显式属性和自动属性位置绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在为OpenGL着色器程序设置属性位置时,您面临两个选择:

When setting up attribute locations for an OpenGL shader program, you are faced with two options:

glBindAttribLocation(),然后进行链接以明确定义属性位置.

glBindAttribLocation() before linking to explicitly define an attribute location.

glGetAttribLocation().

将一个工具与另一个工具一起使用的实用工具是什么?

What is the utility for using one over the other?

在实践中最好选择哪一个?

And which one, if any, is preferred in practice?

推荐答案

我知道有一个很好的理由偏爱显式位置定义.

I know one good reason to prefer explicit location definition.

请考虑将几何数据保存在 顶点阵列对象中. 对于给定的对象,您可以使用索引对应的方式创建VAO,例如:

Consider that you hold your geometry data in Vertex Array Objects. For a given object, you create a VAO in such way that the indices correspond to, for example:

  • 索引0 :排名
  • 索引1 :法线,
  • 索引2 :texcoords
  • index 0: positions,
  • index 1: normals,
  • index 2: texcoords

现在考虑您要使用两个不同的着色器绘制一个对象.一个着色器需要位置和法线数据作为输入,另一个着色器需要位置和纹理坐标.

Now consider that you want to draw one object with two different shaders. One shader requires position and normal data as input, the other - positions and texture coords.

如果编译这些着色器,您会注意到第一个着色器将期望属性索引为0的位置且法线为1.另一个着色器将期望位置为0但纹理坐标为1.

If you compile those shaders, you will notice that the first shader will expect the positions at attribute index 0 and normals at 1. The other would expect positions at 0 but texture coords at 1.

引用 https://www.opengl.org/wiki/Vertex_Shader :

自动分配

如果前面两种方法均未将输入分配给属性索引,则在链接程序时,OpenGL将自动分配该索引.分配的索引是完全任意的,即使所链接的程序使用完全相同的顶点着色器代码,它们也可能会有所不同.

If neither of the prior two methods assign an input to an attribute index, then the index is automatically assigned by OpenGL when the program is linked. The index assigned is completely arbitrary and may be different for different programs that are linked, even if they use the exact same vertex shader code.

这意味着您将无法同时在两个着色器中使用VAO.在每个对象(例如,对象)中没有一个VAO的情况下,在最坏的情况下,您需要一个单独的VAO 每个对象的每个对象的VAO .

This means that you wouldn't be able to use your VAO with both shaders. Instead of having one VAO per, say, object, you'd need - in the worst case - a separate VAO per object per shader.

通过glBindAttribLocation强制着色器使用您自己的属性编号约定可以轻松解决此问题-您需要做的就是在属性及其已确定的ID之间保持一致的关系,并在需要时强制着色器使用该约定.链接.

Forcing the shaders to use your own attribute numbering convention via glBindAttribLocation can solve this problem easily - all you need to do is to keep a consistent relation between attributes and their estabilished IDs, and force the shaders to use that convention when linking.

(如果您不使用单独的VAO,这并不是一个大问题,但仍然可以使您的代码更清晰.)

(That's not really a big issue if you don't use separate VAOs, but still might make your code clearer.)

顺便说一句:

为OpenGL着色器程序设置属性位置时,您面临两个选择

When setting up attribute locations for an OpenGL shader program, you are faced with two options

OpenGL/GLSL 3.3中还有第三个选项:直接在着色器代码中指定位置.看起来像这样:

There's a third option in OpenGL/GLSL 3.3: Specify the location directly in shader code. It looks like this:

layout(location=0) in vec4 position;

但这在GLSL ES着色器语言中不存在.

But this is not present in GLSL ES shader language.

这篇关于OpenGL着色器的显式属性和自动属性位置绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆