Howto:Shader programming in FlightGear: Difference between revisions

m
→‎A Practical Application in Flightgear: +syntax highlighting and formatting
No edit summary
m (→‎A Practical Application in Flightgear: +syntax highlighting and formatting)
Line 381: Line 381:
Any combination of Preferences, [[Nasal|Nasal]] or [[Xml|XML]] manipulates data in the [[Property Tree]].  
Any combination of Preferences, [[Nasal|Nasal]] or [[Xml|XML]] manipulates data in the [[Property Tree]].  
In this case the switch to turn on the landing or spot light and a couple other needed data containers are defined in $FG_ROOT/preferences.xml with the following lines.
In this case the switch to turn on the landing or spot light and a couple other needed data containers are defined in $FG_ROOT/preferences.xml with the following lines.
<syntaxhighlight lang="xml">
  <als-secondary-lights>
  <als-secondary-lights>
   <use-searchlight type="bool">false</use-searchlight>
   <use-searchlight type="bool">false</use-searchlight>
Line 388: Line 389:
   <landing-light2-offset-deg type="float">0.0</landing-light2-offset-deg>
   <landing-light2-offset-deg type="float">0.0</landing-light2-offset-deg>
  </als-secondary-lights>
  </als-secondary-lights>
</syntaxhighlight>
They show up in the Property Tree under sim/rendering/als-secondary-lights and can be activated or manipulated by normal [[Nasal|Nasal]] calls or [[Xml|XML]].
They show up in the Property Tree under sim/rendering/als-secondary-lights and can be activated or manipulated by normal [[Nasal|Nasal]] calls or [[Xml|XML]].


Line 396: Line 398:
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.
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 shader program.
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>
<syntaxhighlight lang="xml">
  <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>
  <view_pitch_offset><use>/sim/current-view/pitch-offset-deg</use></view_pitch_offset>
  <view_pitch_offset><use>/sim/current-view/pitch-offset-deg</use></view_pitch_offset>
Line 409: Line 413:
  <tquality_level><use>/sim/rendering/shaders/transition</use></tquality_level>
  <tquality_level><use>/sim/rendering/shaders/transition</use></tquality_level>
</small>
</small>
</syntaxhighlight>
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.


Line 420: Line 426:
Next comes the entry to define what 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 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.
<syntaxhighlight lang="xml">
  <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>
</syntaxhighlight>
In the case of ALS Lights, so far we only have to deal with the fragment shader.
In the case of ALS Lights, so far we only have to deal with the fragment shader.


Line 432: Line 440:
==== Uniforms ====
==== Uniforms ====
The uniforms section is the mechanism that feeds the parameter data to the shader program.
The uniforms section is the mechanism that feeds the parameter data to the shader program.
  <small><uniform>
<syntaxhighlight lang="xml">
  <small>
  <uniform>
   <name>view_pitch_offset</name>
   <name>view_pitch_offset</name>
   <type>float</type>
   <type>float</type>
Line 481: Line 491:
   <type>int</type>
   <type>int</type>
   <value><use>display_ysize</use></value>
   <value><use>display_ysize</use></value>
  </uniform></small>
  </uniform>
</small>
</syntaxhighlight>
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.
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.


Line 504: Line 516:


==== Functions ====
==== Functions ====
<syntaxhighlight lang="glsl">
  float light_distance_fading(in float dist)
  float light_distance_fading(in float dist)
  {
  {
Line 553: Line 566:
   else return vec3 (0.0,0.0,0.0);
   else return vec3 (0.0,0.0,0.0);
  }
  }
 
</syntaxhighlight>
[[File:Model on Water and Trees on Land.jpg|803px|Model on Water and Trees on Land ALS Lights Effect]]
[[File:Model on Water and Trees on Land.jpg|803px|Model on Water and Trees on Land ALS Lights Effect]]


Line 582: Line 595:
==== Functions ====
==== Functions ====
Function calls to the function defined in secondary_lights.frag are
Function calls to the function defined in secondary_lights.frag are
<syntaxhighlight lang="glsl">
  vec3 searchlight();
  vec3 searchlight();
  vec3 landing_light(in float offset);
  vec3 landing_light(in float offset);
</syntaxhighlight>
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.
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.
Line 589: Line 605:
==== Main Program ====
==== 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.
The "main" function is the heart of the shader program. This is where the shader program manipulates all the data made available to it.
<syntaxhighlight lang="glsl">
  void main()
  void main()
  {
  {
Line 609: Line 626:
   vec4 fragColor = vec4 (gl_Color.rgb +secondary_light * light_distance_fading(dist),1.0) * texel;
   vec4 fragColor = vec4 (gl_Color.rgb +secondary_light * light_distance_fading(dist),1.0) * texel;
  }
  }
</syntaxhighlight>
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 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.
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.