Built-in Variables


introduction

To work with the OpenGL pipeline the shaders you develop needs a way to recieve from and pass values to the pipeline. To do this GLSL has some predefined variables that you can work with. These variables have gone through many variations with the different version so of GLSL. We will focus on the state of these variables with OpenGL ES 2.0s


vertex shader

The built-in variables specific to the vertx shader are listed in the table below. They are listed as input and out variables. The input variables are given values by the OpenGL pipeline before your vertex shader. The output variables should be given values by the shader you write.

outputqualifierfunction
vec4 gl_Positionvertex shader writes to this variable to pass on the new value for the current vertex
float gl_PointSizevertex shader writes to this variable to pass on the new value for the current point size in pixels

For example refer to the first vertex shader we wrote.

void main() {
    gl_Position = ftransform();
}
        

The above code takes the current vertex position and transforms it before placing it in gl_Position. ftransform(). Simulates what the fixed OpenGL pipeline does to the vertex position.

gl_Position is a variable that should be written to, i.e. initialized, during your vertex shader to be a valid shader. Once initialized it is then valid for your shader to read this variable. Into this variable you will place the vertex calculated by your vertex shader. Hense your vertex shader has control over the current vertex used in the OpenGL pipeline. This is one of the primary functions of the vertex shader. gl_Vertex is the vertex specified by your OpenGL code. However the value you place into gl_Position is in window coordinates with the origin in the middle and 1,1 being the top right corner of your display. gl_Position is gl_Vertex after it has been transformed by the modelview matrix and the projection matrix. So the following will generally not produce the result you wish.

  void main() {
    gl_Position = gl_Vertex;
}
        

However the following will simulate the function ftransform().

  void main() {
      gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
        

In this code gl_ModelViewProjectionMatrix is a matrix which is the modelview matrix multiplied by the projection matrix.

So if you wrote the following vertex shader each vertex would appear in the top left corner of your window. The z value is now a value from 0 to 1. So gl_Position is in window coordinates with the origin in the middle.

void main() {
    gl_Position = vec4(-0.95,0.95,0.5,1);
}
        

If the variables with qualifiers varying are utilised by the following pipeline or by the fragment shader then they must be assigned a value, otherwise they will be undefined.


fragment shader

Like the vertex shader the fragment shader can recieve values already in the OpenGL pipeline and write values back to the pipeline. The variables with qualifiers varying are derived from the varying variables of the vertex shader.

inputqualifierfunction
vec4 gl_FragCoordread-only current fragment window relative coordinate. z is 0-1 and assigned to gl_FragDepth if no other value is assigned.
bool gl_FrontFacingtrue if the fragment belongs to a front facing geometric primitive.
int gl_PointCoordfragment position within a point (point rasterization only) in the range 0.0 to 1.0
outputqualifierfunction
vec4 gl_FragColorthe colour of the fragment
vec4 gl_FragDatadata associated with the fragment

gl_FragColor is the principal variable that your fragment shader is designed to change. If your code does not assign a value to it then it is left undefined for the rest of the pipeline.

gl_FragData is an array of data that can be used by the rest of the pipeline. If you assign a value to gl_FragData then you most not assign a value to gl_FragColor and visa versa.

Below is an example of a fragment shader we used before. It set the fragment colour to a fixed value.

        void main() {
        // Set the fragment color for example to gray, alpha 1.0
        gl_FragColor = vec4(0.5, 0.5, 0.5, 1.0);
    }
        

built-in constant variables

constant variabledescriptionminimum number
int gl_MaxVertexAttribsmaxiumum number of vertex attributes8
int gl_MaxVertexUniformVectorsmaxiumum number of vertex shader uniform vectors128
int gl_MaxVaryingVectorsmaxiumum number of varying vectors8
int gl_MaxVertexTextureImageUnitsmaxiumum number of vertex shader texture image units0
int gl_MaxCombinedTextureImageUnitsmaxiumum number of combined texture image units8
int gl_MaxFragmentUniformVectorsmaxiumum number of fragment shader uniform vectors16
int gl_MaxDrawBuffersmaxiumun number of draw buffers1