Shaders in Oolite: uniforms
Contents
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. This allows shader authors to access numerous properties of a game entity without having to ask for them and wait for an updated version of Oolite. (Note: in the case of subentities, bound properties are fetched from the parent 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 } ... }
Uniform property types
Every bindable property has a type. The simplest types, int and float, correspond directly to the same types in GLSL. Both represent numbers, with the difference that a float can represent fractional values and very large values, but always has the same level of precision (about six digits), while ints can represent any integer (non-fractional value) in a limited range. A bool is an integer whose value can be only 0 (meaning no or false) or 1 (meaning yes or true). (Note: the GLSL specification is deliberately vague on the ranges of the number types. It also points out that most hardware will actually use floating-point calculations for int variables, which implies that integer calculations will incur rounding overhead.)
The vector type is translated to a GLSL vec4, whose x, y and z components correspond to those of the Oolite vector, and whose w component is always 1.0.
The quaternion type can be translated to a mat4x4 representing a rotation matrix, or to a vec4 whose x, y, z and w components correspond to those of the Oolite quaternion.
The matrix type is translated into a GLSL mat4x4.
The colour type is translated into a GLSL vec4 type, whose r, g, b and a components correspond to those of the Oolite colour object.
Conversions
Several types of binding can be modified by conversion options, specified in the uniforms dictionary, for example:
haveFuel = { type = binding; value = "fuel"; clamped = true; };
The clamped conversion option, when applied to a property of type float, causes the value to be clamped to the range [0, 1]. When applied to an int propertie, any value other than 0 (including negative values) is converted to 1. The default value is false.
The normalized option causes vector properties to be normalized, that is, scaled so they have the same direction but length 1. The default value is false.
The toMatrix option causes quaternion properties to be converted to rotation matrix, rather than having their components packed into a vec4. The default value is true.
Bindable property reference
The following sections list entity properties which can be used in a uniform binding, regardless of apparent usefullness. Note, however, that many of these names will change before the next stable release – in particular, it is intended that by Oolite 1.70 properties using underscores (a_property_name
) will be converted to CamelCase (aPropertyName
). This reflects ongoing clean-up activity within the Oolite code. Some bindable properties are not listed because they are of questionable usefulness and likely to be removed in future.
Entity
The following properties are available for all entities:
Name | Type | Description |
---|---|---|
position | vector | The location of the entity in system coordinates. |
orientation | quaternion | The orientation of the entity. |
transform | matrix | The geometric transformation matrix of the entity (combining position and orientation). |
relativePosition | vector | The location of the entity, relative to the player. |
viewpointOffset | vector | The location of the current camera, in entity coordinates. Although this is defined for all entities, it is only meaningful for the player ship. |
collisionRadius | float | Together with the position, specifies a sphere completely containing the collidable volume of the entity. |
mass | float | The mass of the entity. |
energy | float | The current energy level of the entity. |
maxEnergy | float | The current maximum energy level of the entity. |
universalTime | float | The game real time clock. This is bound to the uniform time by default, for backwards-compatibility reasons. The value is the same for all entities and updates once per frame. |
spawnTime | float | The game real time at which the entity came into existance. |
timeElapsedSinceSpawn | float | The number of seconds (in the game real time scale) since the entity came into existance – the difference between universalTime and spawnTime. |
universalID | int | A unique identifier for the entity. |
throwingSparks | bool | Whether the entity is generating sparks (for instance, a ship about to explode). |
Ship
Ships have all the properties of entities, and several others: FIXME: fill in
Player ship
The player ship has all the properties of entities and ships, and several others: FIXME: fill in