Jump to: navigation, search

Howto:Shader programming in FlightGear

1,274 bytes added, 07:10, 13 October 2014
no edit summary
[[File:ALS Secondary Light Proof of Concept.png|640px|ALS Secondary Light Proof of Concept]]
== ALS Landing Lights - Spotlight ==
The ALS Landing Lights-Spotlight (we'll call it ALS Lights from now on) is a good example for showing how to incorporate a [[Howto:Shader Programming in FlightGear|shader ]] effect into [[Flightgear]] as it touches many parts of the visuals we see and many parts of the coding pipeline.
In the case of ALS Lights, you have to add the effect to every visual item rendered on the screen that you want to see a light shining on. If you want it to be capable of shining on everything, you have to account for each separate item and how that item is rendered. That is a lot of code to touch.
Some of these items may be controlled or rendered by the same effect and [[Howto:Shader Programming in FlightGear|shader ]] file. They might have to be accounted for individually. They may have special lighting influences that have to be accounted for. You have to take each one separately and account for all its needs.
The example highlighted in this article is what was added to tree.eff to shine the lights on trees.
=== Program Flow Simplified ===
Preferences/[[Nasal]]/XML >> [[Property Tree]] >> Effect File >> [[Howto:Shader Programming in FlightGear|shader]] >> Rendered to Screen
=== Preferences/Nasal/XML ===
==== Parameters ====
Parameter entries defined in the Effect file correspond to a property tree data container (static or variable). They will contain the data needed by the [[Howto:Shader Programming in FlightGear|shader ]] program to perform its magic. The type of information contained in the property tree might be program control data or variable/static data that the [[Howto:Shader Programming in FlightGear|shader ]] program can manipulate prior to sending on to render.In the case of ALS Lights, below is some of the data passed to, and used by, the [[Howto:Shader Programming in FlightGear|shader]].
Note the "use-searchlight" entry, it is pointing to the use-searchlight entry in the property tree under "sim/rendering/als-secondary-lights" that was defined in preferences.xml.
Some of this data may play a duel role inside the [[Howto:Shader Programming in FlightGear|shader ]] program. In other words it might be used to control other functions in addition to ALS Lights.There will also be other parameter entries that have nothing to do with ALS Lights. They might be used for other actions or effects the [[Howto:Shader Programming in FlightGear|shader ]] is handling.
==== Technique ====
In general, the [[Howto:Shader Programming in FlightGear|shader ]] program and the uniforms are defined in between the technique tags. The technique is assigned an index to distinguish one technique from another (technique n="1"). As is the case with tree.eff, sometimes the [[Howto:Shader Programming in FlightGear|shader ]] program and its uniforms are defined and needed in more than one technique. In the case of tree.eff it is used in technique 4 and 5. Which means in [[Flightgear]], the tree [[Howto:Shader Programming in FlightGear|shader ]] set to either of the the highest two [[Howto:Shader Programming in FlightGear|shader ]] settings still produces ALS Lights when activated.
==== Shader Program ====
Next comes the entry to define what [[Howto:Shader Program Programming in FlightGear|shader]] program the parameters data is going to be passed to.This is where you specify what [[Howto:Shader Programming in FlightGear|shader code ]] program is to be used by the technique. ALS has the lowest techniques, with higher quality preceding lower quality.
In the case of ALS Lights, so far we only have to deal with the fragment [[Howto:Shader Programming in FlightGear|shader]].
The program section of the effect file is a nifty method used to allow users to add [[Howto:Shader Programming in FlightGear|shaders ]] to [[Flightgear]] without having to add code at C level language base. The C level base is programed to recognize the XML tag pair of <program></program> and thus incorporate the GLSL program files pointed to between the tags. Otherwise you would have to add the GLSL program calls in the base C requiring a completely different set of programming skills and also the necessity of compiling [[Flightgear]] everytime you want to add new [[Howto:Shader Programming in FlightGear|shaders]]. It can work this way because [[Howto:Shader Programming in FlightGear|shader ]] programs are compiled at run-time.
We'll describe the contents of the [[Howto:Shader Programming in FlightGear|shader ]] programs below. For now, suffice it to say tree-ALS.frag contains the main program and secondary_lights.frag has functions that are passed uniform data that is manipulated and returned to main for processing.
==== Uniforms ====
The uniforms section is the mechanism that feeds the parameter data to the [[Howto:Shader Programming in FlightGear|shader ]] program.
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 [[Howto:Shader Programming in FlightGear|shader ]] by the uniform. Below in the "Shader Program" section, we will show you how the [[Howto:Shader Programming in FlightGear|shader ]] receives the uniform's data.
[[File:Als Secondary Lights combined with Fog Effect.jpg|1024px|Weather settings to produce fog and ALS Landing Lights on a runway.]]
=== Shader Programs ===
The [[Howto:Shader Programming in FlightGear|shader ]] programs used in this example are "tree-ALS.frag" and "secondary_lights.frag".
=== secondary_lights.frag ===
==== Uniform Input ====
Uniform data is brought into the [[Howto:Shader Programming in FlightGear|shader ]] in the following manner.
uniform float landing_light1_offset;
==== Variable Data ====
Variable data can be defined in the [[Howto:Shader Programming in FlightGear|shader ]] program. An example of variable data defined in the [[Howto:Shader Programming in FlightGear|shader ]] program that is needed for ALS Lights is
vec3 secondary_light = vec3 (0.0,0.0,0.0);
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 [[Howto:Shader Programming in FlightGear|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 [[Howto:Shader Programming in FlightGear|shader ]] program. This is where the [[Howto:Shader Programming in FlightGear|shader ]] program manipulates all the data made available to it.
void main()
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 [[Howto:Shader Programming in FlightGear|shader ]] program is used for other purposes and is introduced into the [[Howto:Shader Programming in FlightGear|shader ]] program from other property, parameter and uniform definitions not pertaining to ALS Lights.
[[File:Model on Water.jpg|640px|ALS Lights Effect over model and water.]]
{{WIP|more to follow}}
[[Shader Coding - Best Practices|shader]]

Navigation menu