title: OpenGL: Defining variables in shaders
body: <p>My OpenGL program, using GLSL for shaders, has a simple vertex and fragment shader (given by a tutorial).</p>
  
  <p>The vertex shader is:</p>
  
  <pre><code>#version 330
  
  layout (location = 0) in vec3 Position;
  
  void main()
  {
      gl_Position = vec4(0.5 * Position.x, 0.5 * Position.y, Position.z, 1.0);
  }
  </code></pre>
  
  <p>And the fragment shader is:</p>
  
  <pre><code>#version 330
  
  out vec4 FragColor;
  
  void main()
  {
      FragColor = vec4(1.0, 0.0, 0.0, 1.0);
  }
  </code></pre>
  
  <p>What is happening here is that the vertex shader divides the vertex coordinates by 2, and the fragment shader then colors it in red.</p>
  
  <p>Now from my understanding, <code>gl_Position</code> tells the fragment shader the pixel coordinates of this vector. And <code>gl_Position</code> is an existing variable that both shaders know about, so the fragment shader will always look for <code>gl_Position</code> when deciding where to draw that vertex.</p>
  
  <p>However, what about <code>FragColor</code>? In this example, it has been manually defined in the fragment shader. So how does OpenGL then know that <code>FragColor</code> is the variable we are using to set the vertex's color? <code>FragColor</code> can have been defined with a different name and the program still runs in the same way.</p>
  
  <p>So I am wondering why <code>gl_Position</code> is a variable that has already been defined by OpenGL, whereas <code>FragColor</code> is manually defined, and how OpenGL knows how to interpret <code>FragColor</code>?</p>