简体   繁体   English

GLSL OpenGL 3.x如何指定通用顶点属性索引和语义之间的映射?

[英]GLSL OpenGL 3.x how to specify the mapping between generic vertex attribute indices and semantics?

I'm switching from HLSL to GLSL 我正在从HLSL切换到GLSL

When defining vertex attributes in of a vertexbuffer, one has to call 在顶点缓冲区中定义顶点属性时,必须调用

glVertexAttribPointer(  GLuint      index, 
    GLint   size, 
    GLenum      type, 
    GLboolean   normalized, 
    GLsizei     stride, 
    const GLvoid *      pointer);

and pass an index. 并传递一个索引。 But how do I specify which index maps to which semantic in the shader? 但是,如何在着色器中指定哪个索引映射到哪个语义呢?

for example gl_Normal . 例如gl_Normal How can I specify that when using gl_Normal in a vertex shader, I want this to be the generic vertex attribute with index 1? 如何在顶点着色器中使用gl_Normal时指定它为索引1的通用顶点属性?

There is no such thing as a "semantic" in GLSL. 在GLSL中没有所谓的“语义”。 There are just attribute indices and vertex shader inputs. 只有属性索引和顶点着色器输入。

There are two kinds of vertex shader inputs. 有两种顶点着色器输入。 The kind that were removed in 3.1 (the ones that start with "gl_") and the user-defined kind. 在3.1中删除的种类(以“ gl_”开头的种类)和用户定义的种类。 The removed kind cannot be set with glVertexAttribPointer ; 无法使用glVertexAttribPointer设置移除的种类; each of these variables had its own special function. 每个变量都有其自己的特殊功能。 gl_Normal had glNormalPointer , gl_Color had glColorPointer , etc. But those functions aren't around in core OpenGL anymore. gl_Normal具有glNormalPointergl_Color具有glColorPointer等。但是这些功能不再存在于核心OpenGL中。

User-defined vertex shader inputs are associated with an attribute index. 用户定义的顶点着色器输入与属性索引关联。 Each named input is assigned an index in one of the following ways, in order from most overriding to the default: 按以下方式之一为每个命名输入分配索引,从最重载到默认:

  1. Through the use of the GLSL 3.30 or ARB_explicit_attrib_location extension syntax layout(location = #) , where # is the attribute index. 通过使用GLSL 3.30或ARB_explicit_attrib_location扩展语法layout(location = #) ,其中#是属性索引。 So if I have an input called position , I would give it index 3 like this: 因此,如果我有一个名为position的输入,我会给它索引3:

     layout(location = 3) in vec4 position; 

    This is my preferred method of handling this. 这是我处理此问题的首选方法。 Explicit_attrib_location is available on pretty much any hardware that is still being supported (that isn't Intel). Explicit_attrib_location在几乎所有仍受支持的硬件上都可用(不是Intel)。

  2. Explicit association via glBindAttribLocation . 通过glBindAttribLocation显式关联。 You call this function before linking the program. 链接程序之前,请先调用此函数。 To do the above, we would do this: 为此,我们将执行以下操作:

     GLuint program = glCreateProgram(); glAttachShader(program, some_shader); glBindVertexAttrib(program, 3, "position"); glLinkProgram(program); 

    You can set multiple attributes. 您可以设置多个属性。 Indeed, you can set multiple attribute names to the same index. 实际上,您可以将多个属性名称设置为同一索引。 The idea with that is to be able to just set a bunch of mappings automatically and let OpenGL figure out which one works with the actual shader code. 这样做的想法是能够自动设置一堆映射,并让OpenGL找出哪个可以与实际的着色器代码一起使用。 So you could have "position" and "axis" map to index 3, and as long as you don't put a shader into this system that has both of those inputs, you'll be fine. 因此,您可以将“位置”和“轴”映射到索引3,并且只要您不将包含两个输入的着色器放到该系统中,就可以了。

  3. Let OpenGL assign it. 让OpenGL分配它。 If you don't assign an attribute index to an attribute in one of the other ways, the GLSL linker will assign it for you. 如果您没有通过其他方法之一将属性索引分配给属性,则GLSL链接器将为您分配该属性。 You can fetch the attribute post-linking with glGetAttribLocation . 您可以使用glGetAttribLocation获取链接后的属性。

    I really don't advise this, because OpenGL will assign the indices arbitrarily . 我真的不建议这样做,因为OpenGL会任意分配索引。 So every shader that uses an attribute named position may have the position in a different index. 因此,每个使用名为position的属性的着色器都可能在不同的索引中具有该位置。 I don't think it's a good idea. 我认为这不是一个好主意。 So if you can't explicitly set it in the shader, then at least explicitly set it in your OpenGL code before linking. 因此,如果您不能在着色器中显式设置它,那么至少在链接之前在OpenGL代码中显式设置它。 That way, you can have a convention about what attribute index 0 means, what index 1 means, etc. 这样,您就可以对索引0的含义,索引1的含义等等有一个约定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM