Jump to: navigation, search

Howto:Shader programming in FlightGear

2,587 bytes added, 03:41, 13 October 2014
no edit summary
Note the name, "use_searchlight", which was originally defined in preferences.xml and then became an entry in parameters is now being passed to the program shader by the uniform. Below in the "Shader Program" section, we will show you how the shader receives the uniform's data.
=== Shader Program Programs ===
The shader programs used in this example are "tree-ALS.frag" and "secondary_lights.frag".
=== secondary_lights.frag ===
"secondary_lights.frag" consists of
*uniform inputs (data coming into the shader to be manipulated
*functions that manipulate the uniform data
Following it the actual GLSL code involvedin "secondary_lights.frag".
==== Uniform Input ====
==== Main Program =tree-ALS.frag ==="tree-ALS.frag" consists of*uniform inputs (data coming into the shader to be manipulated*functions that manipulate the uniform data
Following it the actual GLSL code in "tree-ALS.frag".
While there is significantly more code in tree-ALS.frag, only the code that was included for the ALS Lights is being shown and discussed here.
==== Uniform Input ====
Uniform data is brought into the shader in the following manner.
uniform float landing_light1_offset;
uniform float landing_light2_offset;
uniform int use_searchlight;
uniform int use_landing_light;
uniform int use_alt_landing_light;
uniform int quality_level;
uniform int tquality_level;
Note "use_searchlight" and how it is defined as incoming uniform data.
==== Variable Data ====
Variable data can be defined in the shader program. An example of variable data defined in the shader program that is needed for ALS Lights is
vec3 secondary_light = vec3 (0.0,0.0,0.0);
==== Functions ====
Function calls to the function defined in secondary_lights.frag are
vec3 searchlight();
vec3 landing_light(in float offset);
You don't have to use any path information or includes in the code because the GLSL compiler program takes care of linking all the shader programs as long as they are defined correctly in the "programs" section of the effect file.
Variable data can be passed to and returned from GLSL functions just like any other language.
==== Main Program ====
The "main" function is the heart of the shader program. This is where the shader program manipulates all the data made available to it.
void main()
vec3 secondary_light = vec3 (0.0,0.0,0.0);
if ((quality_level>5) && (tquality_level>5))
if (use_searchlight == 1)
secondary_light += searchlight();
if (use_landing_light == 1)
secondary_light += landing_light(landing_light1_offset);
if (use_alt_landing_light == 1)
secondary_light += landing_light(landing_light2_offset);
vec4 fragColor = vec4 (gl_Color.rgb +secondary_light * light_distance_fading(dist),1.0) * texel;
Note how "use_searchlight" is used in the main function to determine if the property defined in preferences.xml and manipulated in the property tree is set to true or 1.
Some of the variable data contained in the shader program is used for other purposes and is introduced into the shader program from other property, parameter and uniform definitions not pertaining to ALS Lights.
==== File List ====
Files that are directly touched by this effect include
* preferences.xml
texel color - what is the base color of the pixel fully lit and unfogged
lighting - how is this color changed by the light falling onto that pixel, usually the relation is something like fragColor = texel*times light
fogging - how much is the color hidden by haze, usually the relation is something like gl_FragColor = mix(fragColor, hazeColor, transmission_factor);
what is displayed on the screen in the end is whatever gl_FragColor is set to
So, the lighting equation in tree-haze.frag is indeed
vec4 fragColor = equals vec4 (,1.0) * times texel;
and your change to the light should happen just before that. But you can't do
gl_Color.rgb = equals gl_Color.rgb + plus my_light;
because gl_Color.rgb is a varying variable type, and you can't assign new values to them inside the shader, so you need to either make a new variable or just do
vec4 fragColor = equals vec4 ((gl_Color.rgb + plus my_light),1.0) * times texel;
(note that color.rgb is the same as, GLSL doesn't really care which convention you use, but I took a few months to learn that, so early code by myself often uses xyz indexing convention for color vectors as well).<ref>{{cite web |url=
|title=ALS landing lights

Navigation menu