Casper
Casper

Reputation: 35

What does in vec and out vec means?

In GLSL I didnt understood what is "in" and "out" variables, what does it mean? Here is a sample of my code that I copied from a tutorial.

// Shader sources
const GLchar* vertexSource =
    "#version 150 core\n"
    "in vec2 position;"
    "in vec3 color;"
    "out vec3 Color;"
    "void main() {"
    "   Color = color;"
    "   gl_Position = vec4(position, 0.0, 1.0);"
    "}";
const GLchar* fragmentSource =
    "#version 150 core\n"
    "in vec3 Color;"
    "out vec4 outColor;"
    "void main() {"
    "   outColor = vec4(Color, 1.0);"
    "}";

Upvotes: 0

Views: 1321

Answers (1)

Andon M. Coleman
Andon M. Coleman

Reputation: 43319

Variables declared in and out at "file" scope like that refer to stage input/output.

  1. In a vertex shader, a variable declared in is a vertex attribute and is matched by an integer location to a vertex attribute pointer in OpenGL.

  2. In a fragment shader, a variable declared in should match, by name, an output from the vertex shader (same name, but out).

  3. In a fragment shader, a variable declared out is a color output and has a corresponding color attachment in the framebuffer you are drawing to.

In your vertex shader, you have two vertex attributes (position and color) used to compute the interpolated input in the fragment shader (Color). The fragment shader writes the interpolated color to the color buffer attachment identified by outColor.

It is impossible to tell what vertex attributes position and color and what color buffer outColor are associated with from your shader code. Those must be set in GL code through calls like glBindAttribLocation (...) and glBindFragDataLocation (...) prior to linking.

Upvotes: 2

Related Questions