Search code examples
openglglslglobalshader

GLSL global variables


GLSL has many global variables that are predefined such as gl_LightSource. They are global because any shader can access them.

How do I define custom global variables in GLSL?


Solution

  • Global variables don't exist in GLSL. The gl_* variables are hardcoded variables (think of them as automatically added compile-time) to access non-programmable elements of the pipeline from a shader. In Core OpenGL 3.0 most of them were removed, including gl_LightSource. The user is now expected to handle his own matrices and lights and send them to the shaders as uniforms. For a complete list, see the Built-in Variable (GLSL) wiki.

    What you want are uniforms. If you want to synchronize uniforms between shaders, store the location of the uniform and iterate through all your programs to upload the uniform to each shader.