Changes

Jump to: navigation, search

Howto:Shader programming in FlightGear

347 bytes added, 17:05, 13 October 2014
m
A Practical Application in Flightgear: +syntax highlighting and formatting
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.
<syntaxhighlight lang="xml">
<als-secondary-lights>
<use-searchlight type="bool">false</use-searchlight>
<landing-light2-offset-deg type="float">0.0</landing-light2-offset-deg>
</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]].
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.
<syntaxhighlight lang="xml"> <small> <display_xsize><use>/sim/startup/xsize</use></display_xsize>
<display_ysize><use>/sim/startup/ysize</use></display_ysize>
<view_pitch_offset><use>/sim/current-view/pitch-offset-deg</use></view_pitch_offset>
<tquality_level><use>/sim/rendering/shaders/transition</use></tquality_level>
</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.
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.
<syntaxhighlight lang="xml">
<program>
<fragment-shader>Shaders/tree-ALS.frag</fragment-shader>
<fragment-shader>Shaders/secondary_lights.frag</fragment-shader>
</program>
</syntaxhighlight>
In the case of ALS Lights, so far we only have to deal with the fragment shader.
==== Uniforms ====
The uniforms section is the mechanism that feeds the parameter data to the shader program.
<syntaxhighlight lang="xml"> <small> <uniform>
<name>view_pitch_offset</name>
<type>float</type>
<type>int</type>
<value><use>display_ysize</use></value>
</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.
==== Functions ====
<syntaxhighlight lang="glsl">
float light_distance_fading(in float dist)
{
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]]
==== Functions ====
Function calls to the function defined in secondary_lights.frag are
<syntaxhighlight lang="glsl">
vec3 searchlight();
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.
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.
<syntaxhighlight lang="glsl">
void main()
{
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.
 
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.
18,328
edits

Navigation menu