Simple Fixed Function Tutorial

From GameStudio Wiki

Jump to: navigation, search

SimpleFixedFunctionTutorial

The 3DGS-Project can downloaded here http://people.freenet.de/a6world/pst1.zip
(Just replace the PixelShaderCode with the FixedFunctionCode.)

If you extend this tutorial please extend the table at the bottom. Please with screenshot. If another screenshot/project is needed i will upload it. Contact "Rigoletto" at the 3DGS-Forum. Following examples use the project in the pst1.zip

block.jpg
shadow.jpg
alpha.jpg
grass.jpg
Here are our four textures, stone, shadow, alpha and grass. The following code parts are the FixedFunctionCode that will produce the effects shown in the pictures beside. Remember we will use the textures in following texture-channels:

stone in Texture 0, which is provided in entSkin1 (the applied level texture in WED)
shadow in Texture 1, which is provided in entSkin2 (the rendered shadow by the WED-Compiler)
alpha in Texture 2, which must be loaded with bmap
grass in Texture 3, which must be loaded with bmap

block.jpg This and the next code is stupidly, because the engine do this just without function.
 ColorOp[1] = Disable;		// Just switch texture in 1 off (shadow)
 
block+shadow.jpg multiply the stone with the shadow texture.

stone * shadow

 ColorArg1[0] = Texture;	// Get texture in 0
 ColorOp[0] = SelectArg1;	// and make it current
 ColorArg1[1] = Current;	// Get current texture (0)
 ColorOp[1] = Modulate;		// and multiply
 ColorArg2[1] = Texture;	// with texture in 1 = (0*1)
 
add.jpg adding the alpha to the stone texture.

stone + alpha

 // For this we must change the textures
 Texture[0] = <entSkin1>;	// block
 Texture[1] = <mtlSkin1>;	// alpha

 TexCoordIndex[0] = 1;		//
 TexCoordIndex[1] = 1;		//

 ColorArg1[0] = Texture;        // Get texture in 0
 ColorOp[0] = SelectArg1;       // and make it current

 ColorArg1[1] = Current;        // Get current texture (0)
 ColorOp[1] = Add;		// and add
 ColorArg2[1] = Texture;        // with texture in 1 = (0+1)
 
sub.jpg subtract the alpha to the stone texture.

stone - alpha

 // For this we must change the textures
 Texture[0] = <entSkin1>;	// block
 Texture[1] = <mtlSkin1>;	// alpha

 TexCoordIndex[0] = 1;		//
 TexCoordIndex[1] = 1;		//

 ColorArg1[0] = Texture;        // Get texture in 0
 ColorOp[0] = SelectArg1;       // and make it current

 ColorArg1[1] = Current;        // Get current texture (0)
 ColorOp[1] = Subtract;		// and subtract
 ColorArg2[1] = Texture;        // with texture in 1 = (0-1)
 
mad.jpg] multiply stone with shadow and adding alpha.

(stone * shadow) + alpha

 ColorArg1[0] = Texture;	// Get texture in 0
 ColorOp[0] = SelectArg1;	// and make it current

 ColorArg1[1] = Current;	// Get current texture (0)
 ColorOp[1] = Modulate;		// and multiply
 ColorArg2[1] = Texture;	// with texture in 1

 ColorArg1[2] = Current;	// Get current texture (0*1)
 ColorOp[2] = Add;		// and add
 ColorArg2[2] = Texture;	// with texture in 2 = (0*1+2)
 

This do the same:

 ColorArg1[0] = Texture;        // Get texture in 0
 ResultArg[0] = Temp;	        // and save it in temp

 ColorArg1[1] = Texture;        // Get texture in 1
 ColorOp[1] = SelectArg1;	// and make it current

 ColorArg0[2] = Texture;	// Get texture in 2 (alpha)
 ColorArg1[2] = Current;	// Get current texture (shadow)
 ColorArg2[2] = Temp;		// Get texture in temp (stone)
 ColorOp[2] = MultiplyAdd;	// ColorArg1*ColorArg2+ColorArg0
 
block+shadow+alpha+grass.jpg (stone * shadow) + (alpha * grass)

You can see the shadow is not effecting the grass, because it is not multiplied with it.

 ColorArg1[0] = Texture;	// Get texture in 0
 ColorOp[0] = SelectArg1;	// and make it current

 ColorArg1[1] = Current;	// Get current texture (0)
 ColorOp[1] = Modulate;		// and multiply
 ColorArg2[1] = Texture;	// with texture in 1
 ResultArg[1] = Temp;		// and save it in temp (0*1)

 ColorArg1[2] = Texture;	// Get texture in 2
 ColorOp[2] = SelectArg1;	// and make it current

 ColorArg1[3] = Current;	// Get current texture (2)
 ColorOp[3] = Modulate;		// and multiply
 ColorArg2[3] = Texture;	// with texture in 3

 ColorArg1[4] = Current;	// Get current texture (2*3)
 ColorOp[4] = Add;		// and add
 ColorArg2[4] = Temp;		// with texture in temp
 
lrp.jpg Linear interpolation between the stone and the grass texture. Looks similarly to the code above but the blending is better. The colors are not changed by the alpha texture.
 // For this we must reorder the textures
 Texture[0] = <entSkin1>;	// block
 Texture[1] = <mtlSkin2>;	// grass
 Texture[2] = <mtlSkin1>;	// alpha
 Texture[3] = <entSkin2>;	// shadow

 TexCoordIndex[0] = 1;		//
 TexCoordIndex[1] = 1;		//
 TexCoordIndex[2] = 1;		//
 TexCoordIndex[3] = 0;		// Must 0 for shadow-map

 ColorArg1[0] = Texture;	// Get texture in 0
 ResultArg[0] = Temp;		// and save it in temp

 ColorArg1[1] = Texture;	// Get texture in 1
 ColorOp[1] = SelectArg1;	// and make it current

 ColorArg0[2] = Texture;	// Get texture in 2 (alpha)
 ColorArg1[2] = Current;	// and the current (grass)
 ColorOp[2] = Lerp;		// interpolate
 ColorArg2[2] = Temp;		// and from temp (stone)

 ColorArg1[3] = Current;	// Get current texture (from interpolate)
 ColorOp[3] = Modulate;		// and multiply
 ColorArg2[3] = Texture;	// with texture in 3 (shadow)
 
lrp2.jpg Complement after Texture.
 // For this we must reorder the textures
 Texture[0] = <mtlSkin1>;       // alpha
 Texture[1] = <mtlSkin2>;       // grass
 Texture[2] = <entSkin1>;       // block
 Texture[3] = <entSkin2>;       // shadow

 TexCoordIndex[0] = 1;          //
 TexCoordIndex[1] = 1;          //
 TexCoordIndex[2] = 1;          //
 TexCoordIndex[3] = 0;          // Must 0 for shadow-map

 ColorArg1[0] = Texture|Complement;// Get texture in 0 and invert
 ResultArg[0] = Temp;           // and save it in temp

 ColorArg1[1] = Texture;        // Get texture in 1
 ColorOp[1] = SelectArg1;       // and make it current

 ColorArg0[2] = Temp;           // Get texture in temp (alpha)
 ColorArg1[2] = Current;        // and the current (grass)
 ColorOp[2] = Lerp;             // interpolate
 ColorArg2[2] = Texture;        // and texture in 2 (stone)

 ColorArg1[3] = Current;        // Get current texture (from interpolate)
 ColorOp[3] = Modulate;         // and multiply
 ColorArg2[3] = Texture;        // with texture in 3 (shadow)
 
red.jpg Coloring with FF
 // For this we must reorder the textures
 Texture[0] = <mtlSkin1>;       // alpha
 Texture[1] = <mtlSkin2>;       // grass
 Texture[2] = <entSkin1>;       // block
 Texture[3] = <entSkin2>;       // shadow

 TexCoordIndex[0] = 1;          //
 TexCoordIndex[1] = 1;          //
 TexCoordIndex[2] = 1;          //
 TexCoordIndex[3] = 0;          // Must 0 for shadow-map

 ColorArg1[0] = Texture;        // Get texture in 0 and invert
 ResultArg[0] = Temp;           // and save it in temp

 ColorArg1[1] = Texture;        // Get texture in 1
 ColorOp[1] = SelectArg1;       // and make it current

 ColorArg0[2] = Temp;           // Get texture in temp (alpha)
 ColorArg1[2] = Current;        // and the current (grass)
 ColorOp[2] = Lerp;             // interpolate
 ColorArg2[2] = Texture;        // and texture in 2 (stone)

 ColorArg1[3] = Current;        // Get current texture (from interpolate)
 ColorOp[3] = Modulate;         // and multiply
 ColorArg2[3] = Texture;        // with texture in 3 (shadow)

 TextureFactor = 0xFF0000;	// Set color
 ColorArg1[4] = TFactor;        // choose color
 ColorOp[4] = Modulate2x;       // and multiply with
 ColorArg2[4] = Current;        // the current texture
 
ff_shift.jpg Shifting alpha with FF
 // For this we must reorder the textures
 Texture[0] = <entSkin1>;       // block
 Texture[1] = <mtlSkin2>;       // grass
 Texture[2] = <mtlSkin1>;       // alpha
 Texture[3] = <entSkin2>;       // shadow

 TexCoordIndex[0] = 1;          //
 TexCoordIndex[1] = 1;          //
 TexCoordIndex[2] = 1;          //
 TexCoordIndex[3] = 0;          // Must 0 for shadow-map

 ColorArg1[0] = Texture;        // Get texture in 0
 ResultArg[0] = Temp;           // and save it in temp

 ColorArg1[1] = Texture;        // Get texture in 1
 ColorOp[1] = SelectArg1;       // and make it current

 TextureTransformFlags[2] = Count2; // Select shifting and set matrix values
 TextureTransform[2] = 	{
			1.0, 0.0, 0.0, 0.0, // xscale, rotate, ------, ------
			0.0, 1.0, 0.0, 0.0, // ------, yscale, ------, ------
			0.0, 0.5, 0.0, 0.0, // xshift, yshift, ------, ------
			0.0, 0.0, 0.0, 0.0  // ------, ------, ------, ------
			};

 ColorArg0[2] = Texture;        // Get texture in 2 (alpha)
 ColorArg1[2] = Current;        // and the current (grass)
 ColorOp[2] = Lerp;             // interpolate
 ColorArg2[2] = Temp;           // and from temp (stone)

 ColorArg1[3] = Current;        // Get current texture (from interpolate)
 ColorOp[3] = Modulate;         // and multiply
 ColorArg2[3] = Texture;        // with texture in 3 (shadow)
 
Personal tools