Shaders in Oolite: uniforms

From Elite Wiki
Revision as of 15:51, 7 June 2007 by Ahruman (talk | contribs) (Started article.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Uniforms in Oolite Shaders

The information presented on this page applies to Oolite test release 1.69 and later.

A uniform in a shader is a variable whose value is specified by the host application – in this case, Oolite. Oolite allows arbitrary uniforms to be specified for shaders applied to ships, and these may either take constant values specified in shipdata.plist, or be “bound” to an attribute of the ship.

Basic syntax

Uniforms are specified in the materials or shaders dictionary of a ship’s shipdata.plist entry:

"ahruman_shady_cobra_example" =
{
    like_ship = "cobra3-player";
    shaders =
    {
        "cobra3_redux.png" =
        {
            textures = ( "cobra3_redux.png" );
            fragment_shader = "ahruman_shady_cobra_example.fragment";
            uniforms =
            {
                shininess =
                {
                    type = float;
                    value = 42.0;
                }
                withinAegis =
                {
                    type = binding;
                    value = "within_station_aegis";
                }
            }
        }
    }
}

This contrived example declares two uniforms, shininess and withinAegis. shininess is a constant float – it is always 42 – while withinAegis is bound to the ship’s within_station_aegis property. (See below for information on available properties.) This is a boolean, that is, a number whose value is either 0 (meaning no) or 1 (meaning yes). To use the uniforms within a shader (either a vertex shader or a fragment shader), they are declared as follows:

uniform float shininess;
uniform int withinAegis;
...
void main(void)
{
    if (withinAegis)
    {
         // Do aegis-specific effect here
    }
    ...
}