Pixel and Vertex Shaders

Programmable shaders add a new dimension to graphics rendering by allowing the transform, lighting, and rendering functionality to be modified at runtime on a vertex and pixel basis. A shader is a small program that runs for every vertex, or every pixel that is rendered on the screen. It can control the vertex position, color and normal, the texture coordinates, as well as the pixel color and brightness, dependent on the influence of light, textures or lite-C variables. This gives the user a new level of dynamic flexibility over the way that pixels are rendered. Vertex and pixel shaders can be used to create realistic water ripples, render cartoon style, cover models with fur, or control the lava flow of a volcano (screenshots by users on the Gamestudio Shader Forum).

     
     
Normalmapping shader
Fur shader

Shaders come in two flavors: vertex shaders manipulate mesh position vertices, pixel shaders manipulate texture pixels. The shader code is loaded into the graphics card memory and plugged directly into the graphics pipeline. Shader code is in assembly, however nowadays a higher level 'C' type language - HLSL - is used that is compiled down to the assembly and makes shaders much easier to program. HLSL uses the same syntax as lite-C. Pixel and vertex shaders can be defined as part of the material effect script, by using the VertexShader and PixelShader keywords. For learning shader programming, start with the Shader Workshop on the Gamestudio Download Page. Here's a quick overview only.

Example for a vertex shader:

float4x4 matWorldViewProj : WORLDVIEWPROJ;
float4 vecTime;


void vs_flicker_red(
   in float4 iPos : POSITION,
   in float2 iTex0 : TEXCOORD0,
   in float2 iTex1 : TEXCOORD1,
   out float4 oPos : POSITION,
   out float4 oDiffuse: COLOR0,
   out float2 oTex0 : TEXCOORD0,
   out float2 oTex1 : TEXCOORD1)
   {
     oPos = mul(iPos,matWorldViewProj);
     oTex0 = iTex0;
     oTex1 = iTex1;
     oDiffuse.r = fmod(vecTime.w * 0.1,1.0);
     oDiffuse.g = 0.0;
     oDiffuse.b = 0.0;
     oDiffuse.a = 1.0;
   }


technique flicker
{
   pass p0
   {
     VertexShader = compile vs_1_1 vs_flicker_red();

   }
}

Shader programming is non-trivial and requires some knowledge in vector and matrix algebra. However, once a shader effect is defined in a proper way, it can be used by everyone without knowledge of its internal works. A good introduction into writing shaders for Gamestudio can be found at the end of the lite-C Workshops. The full shader language reference is contained in Microsoft's DirectX documentation. There are several books about shader programming. For an example how to write reusable shaders, look into the Shader library.

Recommended tutorials and books:

Some additional information for Gamestudio shader programming:

Vertex format

That's what the engine passes to the vertex shader. The A7 vertex struct is defined in atypes.h. All geometry and model vertices use three UV coordinate sets for texture, lightmap/detail map, and shader tangents, in the following format:
typedef struct {
   float x,y,z;   // position
float nx,ny,nz; // normal float tu1,tv1; // coordinate set 1, for base texture float tu2,tv2; // coordinate set 2, for light map or detail map float tx3,ty3,tz3,tw3; // coordinate set 3, for tangent or other purposes (A7.06 and above only) } D3DVERTEX; #define D3DFVF_D3DVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX3|D3DFVF_TEXCOORDSIZE2(0)|D3DFVF_TEXCOORDSIZE2(1)|D3DFVF_TEXCOORDSIZE4(2))

 !!  Shader coordinates are in the DirectX coordinate format with horizontal z axis and vertical y axis. Engine and DirectX coordinates can be converted by swapping the x and y values.

Please note that the tw3 coordinate, which contains the orientation of the binormal, is only available in A7.06 and above. A6 up to A7.4 only had 3 values at the third UV coordiate set. For correct handedness, multiply the binormal by tangent.w.

The default.fx shader functions collection

The most common shader functions are contained in the code\default.fx file and can just be included in any shader that you use. This not only makes the shader code much shorter, it also avoids errors and allows adjustments that automatically affect all shaders. For details see Shader includes. Examples for the usage of default.fx can be found in the mtlFX.c shader library.

The mtlFX.c shader library

A library of standard surface and postprocessing shaders is defined in the code\mtlFX.c file and can be included in any project. For details see Shader library.

Edition

 C   P 

See also:

MATERIAL, Effect, shader library, shader variables


► latest version online