R4 Shader Tutorial|
Shaders in R4 are text strings that tell R4 Modules how to draw onto the screen. Most of the shader commands are shorthand for OpenGL functions that will be called. Every command in a shader is represented by a single character, followed by more characters which represent parameters, and separated by a ';'. Every shader must end with a ';' too.
A simple example of a shader is:
In this, T0 uses Texture number 0 (the first one passed to a scene), and B11 tells OpenGL to Draw triangles at the colour settings specified in the module (the first '1') and the second one says that the background is kept at its original colour too - so the module is added onto the background.
Now you have the idea, i'll go cover some of the commands in shaders, and some commonly used shaders.
OpenGL stores depth information about what is rendered on the screen, and if told to do so will not draw any pixels that are behind pixels currently on the screen - this is very useful for Solid Objects. There are two shader commands for this - D and d.
D Enables depthtesting and clears the depth buffer. This is the most useful
d Enables depthtesting, but doesn't clear the depth buffer. This means what is drawn with this command on will be drawn as if it is in the same scene as what was drawn before. Its good for things like tunnels, where there are two layers. The outer might be drawn solid with D and the inner, transparent, with d.
Blend Function (B)
Blend functions are maybe the most useful shader command. They define how things are combined with what is currently on the scene. The command consists of "Bxy;" where x and y are two characters. It works out kindof like this: (source * x) + (destination * y) => destination. Destination is the screen, and source is what you're drawing.
x and y are defined by two letters, and can be lots of different things:
x = Source Colour
y = Destination Colour
Excuse the brackets and quotes and stuff, its copied out of my code. Obviously GL_ZERO is 0, and GL_ONE is 1. The GL_DST... are destination, and GL_SRC... are source. ALPHA is the transparency value of the texture. You need a special texture that has transparency in it or the value of any alpha is 1. This may take some thinking about, but here are some common commands and what they do:
B10 - just draw the source colour
BAa - draw the source colour with transparency
B11 - just Add in the source
BA1 - Add in the source, but take account of source transparency
Bcc - If the source is a black+white texture, this INVERTS the background colour if the texture is white, and if its black it leaves it alone.
Texture Number (T)
The Command is of the form "Tx;" where x is a number. At the moment this number is virtually always 0 because most scenes only take one texture as input. Hopefully this will change later. It uses the texture numbered to draw the current module. If no T is specified, the default colour is used instead. It'll probably be white.
Texture Generation (G)
This is an interesting one. The command is of the form "Gxy;" where x specifies the texture coordinate (S,T,R or Q) and y is the method of generation. When you turn generation on, it throws away the texture coordinates generated by the module, and makes its own using the rules specified in y.
S and T are the most used texture coordinates. These are like x and y in a texture. R and Q can be used for other stuff, but not here really. As for the method of generation, O is object linear - the coordinate is set according to the distance from the centre of the 3D object along a pre-defined vector. W is the same but using world coordinates, but S is the cool one - Sphere Mapping - this sets up the vector as if a light ray bounced off the object and hit a sphere containing your texture that was around the object. This means you get a nice reflection effect, and its used loads in R4.
So Usually, you should see GSS;GTS; around a lot - this means treat the texture like its a reflection.
Another kindof useful one is GSO;GTO;PSO0.0005,0,0;PTO0,0.0005,0; - This is used for putting a texture on a biotunnel. It sets the texture coordinates as if the tunnel was cut out of a bit of candy rock (that cylinder of sugar with writing all the way through it). I hope you know what i mean - maybe its an english thing :p The PS... and PT... set those vectors i was babbling about earlier. If you make the numbers smaller, the texture gets bigger and vice-versa.
This is of the form "Cr,g,b,a;" where r,g,b and a are numbers specifying red, green, blue and alpha (transparency) respectively. They are values between 0 and 1 - occasionally a module may override this if it has to put different colours on an object.
Cull Face (F)
This is either "FF;" or "FB;" - FF means only the bits of object that are facing the viewer are drawn, and FB is only those that are facing away. It sometimes makes stuff faster, and can sometimes create some fun effects.