Reputation: 3229
So i've been learning some OpenGL, it's a lot to take in and im just a beginner, but I don't understand the "Layout Qualifier" in GLSL.
So something like this:
layout (location = 0) in vec3 position;
in a simple vertex shader such as this:
#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
}
I understand the out vec4 for instance (since thats going to the fragment shader). And the vertexColor makes sense for instance.
But maybe im not understanding the "position" and what exactly that means in this sense? Does anyone care to explain? The opengl wiki honestly didn't help me.
But maybe im misunderstanding what a vertex shader (im still of course a little unsure of the pipeline). but from my understanding the vertex specification is the first thing we do correct? (Vertices/Indices if needed) and storing these into a VAO.
So the vertex shader is interacting with each individual vertex point? (i hope) because thats how I understood it?
Upvotes: 34
Views: 31113
Reputation: 1862
You're right, the vertex shader is executed for each vertex.
A vertex is composed by of or several attributes (positions, normals, texture coordinates etc.).
On the CPU side when you create your VAO, you describe each attribute by saying "this data in this buffer will be attribute 0, the data next to it will be attribute 1 etc.". Note that the VAO only stores this information of where's who. The actual vertex data is stored in VBOs.
In the vertex shader, the line with layout and position is just saying "get the attribute 0 and put it in a variable called position" (the location represents the number of the attribute).
If the two steps are done correctly, you should end up with a position in the variable named position :) Is it clearer?
Upvotes: 44