Howto:Shader programming in FlightGear: Difference between revisions

m
no edit summary
m (Adding more info about GLSL)
mNo edit summary
Line 21: Line 21:


To make it simple, a shader is a program that is loaded on the GPU and called for every vertex or pixel: this gives programmers the possibility to implement techniques and visual effects and execute them faster. In modern games or simulators lots of shaders are used: lights, water, skinning, reflections and much more.  
To make it simple, a shader is a program that is loaded on the GPU and called for every vertex or pixel: this gives programmers the possibility to implement techniques and visual effects and execute them faster. In modern games or simulators lots of shaders are used: lights, water, skinning, reflections and much more.  
We can create as many shader programs as needed. Once rendering, we can switch from program to program, and even go back to fixed functionality during a single frame.


To really understand shaders, you should have a knowledge about the rendering pipeline; this helps to understand where and when the shaders act in the rendering process. In general, you must know that vertex are collected, processed by vertex shaders, primitives are built, then are applied colors, textures and are also called fragment shaders; finally it comes to the rasterization and the frame is put on the buffer.  
To really understand shaders, you should have a knowledge about the rendering pipeline; this helps to understand where and when the shaders act in the rendering process. In general, you must know that vertex are collected, processed by vertex shaders, primitives are built, then are applied colors, textures and are also called fragment shaders; finally it comes to the rasterization and the frame is put on the buffer.  
Line 87: Line 89:


= Data Types in GLSL =
= Data Types in GLSL =
Note that there is implicit type conversion in GLSL, all conversions have to be done using explicit constructor calls!
Note that there is no implicit type conversion in GLSL, all conversions and initializations have to be done using explicit constructor calls!


== Scalars ==
== Scalars ==
Line 98: Line 100:
* ivec2, ivec3, ivec4 2D, 3D and 4D integer vector
* ivec2, ivec3, ivec4 2D, 3D and 4D integer vector
* bvec2, bvec3, bvec4 2D, 3D and 4D boolean vectors
* bvec2, bvec3, bvec4 2D, 3D and 4D boolean vectors
Accessing a vector can be done using letters as well as standard C selectors.
TODO: explain swizzling
One can use the letters x,y,z,w to access vectors components; r,g,b,a for color components; and
s,t,p,q for texture coordinates.


== Matrices ==
== Matrices ==
Line 105: Line 114:


== Samplers ==
== Samplers ==
In GLSL, textures are represented using so called "samplers", which are used for sampling textures and which have to be uniform. The following samplers are available:
In GLSL, textures are represented and accessed using so called "samplers", which are used for sampling textures and which have to be uniform. The following samplers are available:


* sampler1D, sampler2D, sampler3D 1D, 2D and 3D texture
* sampler1D, sampler2D, sampler3D 1D, 2D and 3D texture
Line 113: Line 122:
== Arrays ==
== Arrays ==
GLSL supports the same syntax for creating arrays that is already known from C or C++, e.g.:
GLSL supports the same syntax for creating arrays that is already known from C or C++, e.g.:
  vec2 foo[10];
  vec2 foo[10];
So, arrays can be declared using the same syntax as in C, but can't be initialized when declared. Accessing array's elements is done as in C.


== Structures ==
== Structures ==
Line 136: Line 148:
* No support for direct or indirect recursion
* No support for direct or indirect recursion
* Call by value-return calling convention
* Call by value-return calling convention
As in C, a shader is structured in functions. At least each type of shader must have a main function declared with the following syntax: void main()
User defined functions may be defined. As in C a function may have a return value, and use the return statement to pass out its result. A function can be void. The return type can have any type, except array.


== Parameter Qualifiers ==
== Parameter Qualifiers ==
The parameters of a function may have the following qualifiers:
* in - copy in, but don't copy back out (still writable within function)
* in - copy in, but don't copy back out (still writable within function)
* out - only copy out; undefined at function entry point
* out - only copy out; undefined at function entry point
* inout - copy in and copy out
* inout - copy in and copy out
If no qualifier is specified, by default it is considered to be in.
= Built-ins ==
== Vertex Shader ==
Vertex shader
* vec4 gl_Position;  must be written
* vec4 gl_ClipPosition; may be written
* float gl_PointSize;  may be written
== Fragment Shader ==
* float gl_FragColor; may be written
* float gl_FragDepth;  may be read/written
* vec4 gl_FragCoord;  may be read
* bool gl_FrontFacing; may be read
== Vertex Attributes ==
* attribute vec4 gl_Vertex;
* attribute vec3 gl_Normal;
* attribute vec4 gl_Color;
* attribute vec4 gl_SecondaryColor;
* attribute vec4 gl_MultiTexCoordn;
* attribute float gl_FogCoord;
== Uniforms ==
(TODO)
== Varyings ==
== Functions ==


[[Category: Howto]]
[[Category: Howto]]
2,561

edits