Howto:Shader programming in FlightGear: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
No edit summary
Line 348: Line 348:
[[File:ALS Secondary Light Proof of Concept.png|640px|ALS Secondary Light Proof of Concept]]
[[File:ALS Secondary Light Proof of Concept.png|640px|ALS Secondary Light Proof of Concept]]
== ALS Landing Lights - Spotlight ==
== 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.
The ALS Landing Lights-Spotlight (we'll call it ALS Lights from now on) is a good example for showing how to incorporate a 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.
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.
Line 368: Line 368:
**stream
**stream


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.  
Some of these items may be controlled or rendered by the same effect and 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.
The example highlighted in this article is what was added to tree.eff to shine the lights on trees.


=== Program Flow Simplified ===
=== Program Flow Simplified ===
Preferences/[[Nasal]]/XML >> [[Property Tree]] >> Effect File >> [[Howto:Shader Programming in FlightGear|shader]] >> Rendered to Screen
Preferences/[[Nasal]]/XML >> [[Property Tree]] >> Effect File >> Shader >> Rendered to Screen


=== Preferences/Nasal/XML ===  
=== Preferences/Nasal/XML ===  
Line 392: Line 392:


==== Parameters ====
==== 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.
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 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 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]].
In the case of ALS Lights, below is some of the data passed to, and used by, the shader program.
  <small><display_xsize><use>/sim/startup/xsize</use></display_xsize>
  <small><display_xsize><use>/sim/startup/xsize</use></display_xsize>
  <display_ysize><use>/sim/startup/ysize</use></display_ysize>
  <display_ysize><use>/sim/startup/ysize</use></display_ysize>
Line 409: Line 409:
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.
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.
Some of this data may play a duel role inside the 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.  
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 shader is handling.  


==== Technique ====
==== 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.
In general, the 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 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 shader set to either of the the highest two shader settings still produces ALS Lights when activated.


==== Shader Program ====
==== Shader Program ====
Next comes the entry to define what [[Howto:Shader Programming in FlightGear|shader]] program the parameters data is going to be passed to.
Next comes the entry to define what shader program the parameters data is going to be passed to.
This is where you specify what [[Howto:Shader Programming in FlightGear|shader]] program is to be used by the technique. ALS has the lowest techniques, with higher quality preceding lower quality.
This is where you specify what shader program is to be used by the technique. ALS has the lowest techniques, with higher quality preceding lower quality.
  <program>
  <program>
   <fragment-shader>Shaders/tree-ALS.frag</fragment-shader>
   <fragment-shader>Shaders/tree-ALS.frag</fragment-shader>
   <fragment-shader>Shaders/secondary_lights.frag</fragment-shader>
   <fragment-shader>Shaders/secondary_lights.frag</fragment-shader>
  </program>
  </program>
In the case of ALS Lights, so far we only have to deal with the fragment [[Howto:Shader Programming in FlightGear|shader]].
In the case of ALS Lights, so far we only have to deal with the fragment 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.
The program section of the effect file is a nifty method used to allow users to add 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 shader. It can work this way because 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.
We'll describe the contents of the 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 ====
==== Uniforms ====
The uniforms section is the mechanism that feeds the parameter data to the [[Howto:Shader Programming in FlightGear|shader]] program.
The uniforms section is the mechanism that feeds the parameter data to the shader program.
  <small><uniform>
  <small><uniform>
   <name>view_pitch_offset</name>
   <name>view_pitch_offset</name>
Line 480: Line 480:
   <value><use>display_ysize</use></value>
   <value><use>display_ysize</use></value>
  </uniform></small>
  </uniform></small>
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.
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.


[[File:Als Secondary Lights combined with Fog Effect.jpg|1024px|Weather settings to produce fog and ALS Landing Lights on a runway.]]
[[File:Als Secondary Lights combined with Fog Effect.jpg|1024px|Weather settings to produce fog and ALS Landing Lights on a runway.]]


=== Shader Programs ===
=== Shader Programs ===
The [[Howto:Shader Programming in FlightGear|shader]] programs used in this example are "tree-ALS.frag" and "secondary_lights.frag".
The shader programs used in this example are "tree-ALS.frag" and "secondary_lights.frag".


=== secondary_lights.frag ===
=== secondary_lights.frag ===
Line 563: Line 563:


==== Uniform Input ====
==== Uniform Input ====
Uniform data is brought into the [[Howto:Shader Programming in FlightGear|shader]] in the following manner.
Uniform data is brought into the shader in the following manner.


  uniform float landing_light1_offset;
  uniform float landing_light1_offset;
Line 575: Line 575:


==== Variable Data ====
==== 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
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);
  vec3 secondary_light = vec3 (0.0,0.0,0.0);


Line 582: Line 582:
  vec3 searchlight();
  vec3 searchlight();
  vec3 landing_light(in float offset);
  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.
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.
Variable data can be passed to and returned from GLSL functions just like any other language.


==== Main Program ====
==== 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.
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()
  void main()
  {
  {
Line 608: Line 608:
  }
  }
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.
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.
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:Model on Water.jpg|640px|ALS Lights Effect over model and water.]]
[[File:Model on Water.jpg|640px|ALS Lights Effect over model and water.]]
Line 767: Line 767:


{{WIP|more to follow}}
{{WIP|more to follow}}


[[Xml|xml]]
[[Xml|xml]]
330

edits

Navigation menu