简体   繁体   English

OpenglES 2.0 顶点着色器属性

[英]OpenglES 2.0 Vertex Shader Attributes

I've read in tutorials that the bare minimum a vertex shader needs is define below:我在教程中读到顶点着色器需要的最低限度定义如下:

attribute vec3 v3_variable;
attribute vec3 v3Pos;

void main()
{
    gl_Position = v3Pos;
}

OpenglES passes to the shader vertex data to v3Pos but the name of this variable can actually be anything, other tutorials name it a_Position , a_Pos , whatever~. OpenglES 将着色器顶点数据传递给v3Pos但这个变量的名称实际上可以是任何东西,其他教程将其命名为a_Positiona_Pos ,随便~。

So my question is, how does OpenglES know that when I call glVertexAttribPointer() it knows that it should put that data into v3Pos instead of v3_variable ?所以我的问题是,当我调用glVertexAttribPointer()时,OpenglES 如何知道它应该将该数据放入v3Pos而不是v3_variable

Okay, so on the app side you will have a call that looks like either好的,所以在应用程序方面,您将收到一个看起来像

glBindAttribLocation(MyShader, 0, "v3Pos"); 

(before you link the shader) or (在链接着色器之前)或

vertexLoc = glGetAttribLocation(MyShader, "v3Pos");

After you link the program.链接程序后。

Then the first element of the glVertexAttribPointer call will either be 0 or vertexLoc.然后 glVertexAttribPointer 调用的第一个元素将是 0 或 vertexLoc。

glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, GL_FALSE, sizeof(MyVertex), BUFFER_OFFSET(0));

In short, the gl linker is going to either assign v3Pos it's own location in memory, or go off of what you tell it.简而言之,gl 链接器要么将 v3Pos 分配给它自己在内存中的位置,要么脱离你告诉它的内容。 Either way you are going to have to perform some interaction with the shader in order to interact with the attribute.无论哪种方式,您都必须与着色器进行一些交互才能与属性进行交互。

Hope that helps希望有帮助

There are two methods of identifying which attributes in your shaders get 'paired' with the data you provide to glVertexAttribPointer .有两种方法可以识别着色器中的哪些属性与您提供给glVertexAttribPointer的数据“配对”。 The only identifier you get with glVertexAttribPointer is the index (the first parameter), so it must be done by index somehow.使用glVertexAttribPointer获得的唯一标识符是index (第一个参数),因此必须以某种方式通过索引完成。

In the first method, you obtain the index of the attribute from your shader after it is compiled and linked using glGetAttribLocation , and use that as the first parameter, eg.在第一种方法中,在使用glGetAttribLocation编译和链接后,您从着色器获取属性的索引,并将其用作第一个参数,例如。

GLint program = ...;
GLint v3PosAttributeIndex = glGetAttribLocation(program, "v3Pos");
GLint v3varAttributeIndex = glGetAttribLocation(program, "v3_variable");
glVertexAttribPointer(v3PosAttributeIndex, ...);
glVertexAttribPointer(v3varAttributeIndex, ...);

You can also explicitly set the layout of the attributes when authoring the shader (as @jp's comment suggests), so you would modify your shader like so:您还可以在创作着色器时显式设置属性的布局(如@jp 的评论所示),因此您可以像这样修改着色器:

layout(location = 1) in vec3 v3_variable;
layout(location = 0) in vec3 v3Pos;

And then in code you would set them explicitly:然后在代码中,您将显式设置它们:

glVertexAttribPointer(0, ...); // v3Pos data
glVertexAttribPointer(1, ...); // v3_variable data

This second method only works in GLES 3.0+ (and OpenGL 3.0+), so it may not be applicable to your case if you are only using GLES 2.0.第二种方法仅适用于 GLES 3.0+(和 OpenGL 3.0+),因此如果您仅使用 GLES 2.0,它可能不适用于您的情况。 Also, note that you can use both of these methods together.另请注意,您可以同时使用这两种方法。 Eg.例如。 even if you explicitly define the layouts, it doesn't restrict you from querying them later.即使您明确定义了布局,也不会限制您以后查询它们。

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

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