GLSL global variables

12,459

Solution 1

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. If you want to see a list of all the ones that remain, you should look at the GLSL Reference Pages.

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.

Solution 2

You can use uniform buffers. Here is a tutorial: http://www.lighthouse3d.com/tutorials/glsl-tutorial/uniform-blocks/

Solution 3

Global shader variables in OpenGL are called "uniforms". They're read-only and can be bound with glUniformX calls. Roughly, it looks like this (this is more of a pseudocode):

// 1. activate the program
glUseProgram(program)

// 2. get uniform location
foo = glGetUniformLocation(program, "foo")

// 3. bind the value, e.g. a single float
glUniform1f(foo, 42.0)

Within the shaders, you need to qualify the variable with uniform storage qualifier, e.g.

uniform float foo;
Share:
12,459

Related videos on Youtube

Kevin Kostlan
Author by

Kevin Kostlan

Updated on June 14, 2022

Comments

  • Kevin Kostlan
    Kevin Kostlan almost 2 years

    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?