31

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?

1 Answer 1

39

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?

6
  • 3
    So why would you use anything BUT zero?
    – msmith1114
    Nov 3, 2015 at 16:44
  • 3
    Well if you have positions and normals for example, you can position in attribute 0 and normals in attribute 1. You can't put both in attribute 0.
    – Jerem
    Nov 3, 2015 at 17:24
  • Ah that makes sense, or something like position in attribute 0, and colors in 1 or something like that.
    – msmith1114
    Nov 3, 2015 at 17:27
  • Do you have to set these locations in your opengl and if so, how? Sep 9, 2017 at 17:45
  • In which OpenGL version was this layout (location = 0) introduced? I saw code using attribute keyword which I guess is replaced by layout (location = 0), isn't?
    – KcFnMi
    Dec 5, 2022 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.