Howto:Shader programming in FlightGear: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
No edit summary
Line 481: Line 481:
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.


=== Shader Program ===
=== Shader Programs ===
The 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" consists of
"secondary_lights.frag" consists of
*uniform inputs (data coming into the shader to be manipulated
*uniform inputs (data coming into the shader to be manipulated
*functions that manipulate the uniform data
*functions that manipulate the uniform data


Following it the actual GLSL code involved.
Following it the actual GLSL code in "secondary_lights.frag".


==== Uniform Input ====
==== Uniform Input ====
Line 547: Line 548:
   else return vec3 (0.0,0.0,0.0);
   else return vec3 (0.0,0.0,0.0);
  }
  }
=== 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 ====
==== 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  
Files that are directly touched by this effect include  
* preferences.xml
* preferences.xml
Line 609: Line 664:


texel color - what is the base color of the pixel fully lit and unfogged
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*light
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);
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
what is displayed on the screen in the end is whatever gl_FragColor is set to
Line 615: Line 670:


So, the lighting equation in tree-haze.frag is indeed
So, the lighting equation in tree-haze.frag is indeed
vec4 fragColor = vec4 (gl_Color.xyz,1.0) * texel;
vec4 fragColor equals vec4 (gl_Color.xyz,1.0) times texel;
and your change to the light should happen just before that. But you can't do
and your change to the light should happen just before that. But you can't do
gl_Color.rgb = gl_Color.rgb + my_light;
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
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 = vec4 ((gl_Color.rgb + my_light),1.0) * texel;
vec4 fragColor equals vec4 ((gl_Color.rgb plus my_light),1.0) times texel;
(note that color.rgb is the same as color.xyz, 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=http://forum.flightgear.org/viewtopic.php?f=47&t=24226&start=15#p220075  
(note that color.rgb is the same as color.xyz, 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=http://forum.flightgear.org/viewtopic.php?f=47&t=24226&start=15#p220075  
|title=ALS landing lights
|title=ALS landing lights
330

edits

Navigation menu