Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL: layout qualifier?

Tags:

opengl

glsl

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?

like image 855
msmith1114 Avatar asked Nov 03 '15 07:11

msmith1114


People also ask

What is a layout in OpenGL?

A number of OpenGL Shading Language variables and definitions can have layout qualifiers associated with them. Layout qualifiers affect where the storage for a variable comes from, as well as other user-facing properties of a particular definition.

What is std140?

In short, it allows the user to share buffers between multiple programs. std140: This layout alleviates the need to query the offsets for definitions. The rules of std140 layout explicitly state the layout arrangement of any interface block declared with this layout.

What is gl_FragCoord?

Description. Available only in the fragment language, gl_FragCoord is an input variable that contains the window relative coordinate (x, y, z, 1/w) values for the fragment. If multi-sampling, this value can be for any location within the pixel, or one of the fragment samples.

What is gl_VertexID?

Description. gl_VertexID is a vertex language input variable that holds an integer index for the vertex.


1 Answers

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?

like image 177
Jerem Avatar answered Jan 03 '23 10:01

Jerem