OpenGL:布局限定符?

OpenGL: layout qualifier?

所以我一直在学习一些 OpenGL,要吸收的东西很多而且我只是一个初学者,但我不了解 GLSL 中的 "Layout Qualifier"。

所以像这样:

layout (location = 0) in vec3 position;

在像这样的简单顶点着色器中:

#version 330 core
layout (location = 0) in vec3 position; // The position variable has attribute position 0

out vec4 vertexColor; // Specify a color output to the fragment shader

void main()
{
    gl_Position = vec4(position, 1.0); // See how we directly give a vec3 to vec4's constructor
    vertexColor = vec4(0.5f, 0.0f, 0.0f, 1.0f); // Set the output variable to a dark-red color
}

例如,我了解 out vec4(因为那是片段着色器)。例如,vertexColor 是有意义的。

但也许我不理解 "position" 以及从这个意义上说这到底意味着什么?有人愿意解释吗?老实说,opengl wiki 对我没有帮助。

但也许我误解了什么是顶点着色器(当然我仍然对管道有点不确定)。但根据我的理解,顶点规范是我们做正确的第一件事? (Vertices/Indices 如果需要)并将它们存储到 VAO 中。

所以顶点着色器正在与每个单独的顶点交互? (我希望)因为我就是这么理解的?

没错,顶点着色器是为每个顶点执行的。

一个顶点由一个或多个属性(位置、法线、纹理坐标等)组成。

在 CPU 一侧,当您创建 VAO 时,您通过说 "this data in this buffer will be attribute 0, the data next to it will be attribute 1 etc." 来描述每个属性。请注意,VAO 仅存储位置信息。实际的顶点数据存储在 VBO 中。

在vertex shader中,layout和position那一行就是"get the attribute 0 and put it in a variable called position"(location表示属性的编号)。

如果这两个步骤都做对了,你应该在名为position的变量中得到一个位置:)是不是更清楚了?