Slightly More General Shader Program

We will see a few features here that we have not yet studied (per-vertex and per-primitive attributes, "out" variables, etc.), but let's not worry too much about what these mean. We will understand them all much better soon. Let's focus for now on GPU storage and related structures, extending our basic "Hello, OpenGL" diagram a little bit.

colorMode == 0
Red vertex - Green vertex - Blue vertex
colorMode == 1
Leftmost vertices about -32° – Rightmost vertex about 120°

Vertex Shader

#version 410 core

// Per-vertex attributes are available as read-only variables ONLY in
// the Vertex Shader. They are tagged with the "in" storage qualifier:

in vec2 mcPosition; // in some range
in vec3 color; // (r,g,b)
in float temperature; // degrees

out vec3 colorToFS;
out float temperatureToFS;

// Per-primitive attributes are available as read-only variables in ALL
// shader programs. They are tagged with the "uniform" storage qualifier:
uniform vec4 scaleTrans;

void main()
{
    // pass color and temperature to fragment shader, interpolating
    // their values across the current primitive:
    colorToFS = color;
    temperatureToFS = temperature;
    // coordinates in an arbitrary range can be mapped to the -1..+1
    // range of LDS with a simple scale/translate operation:
    float xLDS = scaleTrans[0]*mcPosition.x + scaleTrans[1];
    float yLDS = scaleTrans[2]*mcPosition.y + scaleTrans[3];
    gl_Position = vec4(xLDS, yLDS, 0, 1);
}

Fragment Shader

#version 410 core

// "in" variables in all shader programs OTHER THAN vertex shaders are
// variables passed in from a previous shader stage in the pipeline:

in vec3 colorToFS;
in float temperatureToFS; // COLDEST=0 <= temperatureToFS <= 1=HOTTEST

// Per-primitive attributes are available as read-only variables in ALL
// shader programs. They are tagged with the "uniform" storage qualifier:
uniform int colorMode;

out vec4 fragColor;

void main()
{
    if (colorMode == 0)
        fragColor = vec4(colorToFS, 1);
    else
        fragColor = vec4(temperatureToFS, 0, 1-temperatureToFS, 1);
}

GLSL Object Storage on GPU

See this diagram – a generalization of the one we saw for "Hello, OpenGL".


James R. Miller (jrmiller@ku.edu) – Page last modified: 03/15/2025 00:37:35