Absolute beginners guide to shaders

[Update July 2015: added extra commands to help w debugging in notes section]

[BIG FAT DISCLAIMER: I’m a beginner to this, so I *barely* know what I’m doing. I’ve checked with Spike and GB (thanks HEAPS guys!!), so if the following is wrong, it’s not insanely wrong. I’ll be constantly editing this page as new info comes in, or if I think of a clearer way to describe something, so anyone reading this please check back, and read the comments/corrections/clarifications (below) from those who know more than I]

INTRODUCTION
Hi all. Inspired by some [edit: the following link is now dead] tantalising screenshots of GBs upcoming Scout’s Journey, I’ve delved into the world of shaders. All the stuff I rtfmed however was to do with the writing of shaders, but not the implementation, no ‘step by step’ guide to actually putting one in a map [EDIT: specifically a q1 map using a q1 map editor]], and now that I’ve (finally) got one working(!), here’s the guide I wish I had, the guide for absolute beginners.

BACKGROUND INFO: WHAT’S A SHADER?
A shader is a “short text script that define the properties of a surface as it appears and functions in a game world (or compatible editing tool)”. The definition comes from here, where you’ll also find a terrific instruction manual, and here’s more info on how to write your own. These links deal with the ‘q3 shader language’, which is what you’ll be using in your Quake engine based game. Also useful to get your head around the concept (and possibilities) of shaders is this video of the amazing Andrew Price explaining Blenders compositor, which is kind of like a ‘shader maker’ with a GUI interface. The jargon is different, but concept/principle IIUC is essentially the same, and the ‘nodes’ concept let you see and control the (equivalent of) changing different ‘lines of shader code’, in real time. [UPDATE: Also, here’s a FANTASTIC Introduction to OpenGL Shaders; though the actual coding examples (in the second half) are NOT compatible with the q3 shader language that FTE and DP use]

THE QUAKE SITUATION
Regarding Quake, shaders need engines which can read shader scripts, and run them on the surfaces to which they are assigned. The only engines that support this AFAIK are FTEQW and DP. Their shader code/implementation is not 100% interchangable/compatible, so I’ll deal with FTE, which is the one I use. Re: map editors, some (like NetRadiant) allow you to assign shaders to brushes, but others (eg TrenchBroom) don’t. So how do you implement shaders in a q1 map editor? Where do we begin? Here’s what do to:

  1. Make a texture which will be the default ‘dummy’ texture, lets call it “foo.tga”. Like all Quake textures, this needs to have dimensions which are powers of 2, eg 16 x 16, 128 x 256, etc. The purpose of this texture is to identify the brush surface you want the shader to ‘do its magic’ on.
  2. Add “foo.tga” to the wad you’re using for your map.
  3. In the map editor, assign “foo.tga” to the surface you want to ‘run’ the shader on (not sure if that’s the correct way to put it). Let’s call this surface the “TargetSurface”.
  4. Compile the map
  5. Make a text file called ‘foo.shader’
    Inside this file put the following code:
  6. [code]foo {
    // this is where you put the foo shader code
    // any surface assigned the texture "foo" will receive the output of this code
    } [/code]
  7. Save ‘foo.shader’ in Quake\FTE\scripts

Now when the map runs, FTE will load all the textures (including ‘foo.tga’), and then check the Quake\FTE\scripts folder, looking through all the *.shader files for any shader called ‘foo’, and (if found) will ‘run the foo code’ on the TargetSurface.

Clients which are NOT ‘shader compatible’ will display the actual “foo.tga” on the TargetSurface.

Notes:
1. Some editors (NetRadiant is one) can assign [EDIT: run?] shaders on textures in the actual map editor, so you don’t need the extra step of making the ‘dummy texture’.

2. In the example above, the foo shader doesn’t [i]have [/i]to exist in it’s own individual file (“foo.shader”). It can exist in any *.shader file in the Quake\FTE\scripts folder.

3. Some shaders modify existing textures rather than replace them, so foo.tga (in the example above) might need to be a good ‘basis’ on which to perform shader operations on. Otoh, shaders can contain links to the images they use as ‘inputs’, so foo.tga might just need to be something that looks ‘acceptable’ in the map editor, or to any player who isn’t using a shader compatible client.

your corrections/comments welcome.

UPDATE: The FAQ [ July 2015]
Q: How can I tell if the current map is trying to run my shader? How can I get a list of all the textures being used?
A: Type ‘mod_texturelist’. You should have an entry there which matches the name of your shader. [credit: Spike]

Q: I’ve written different versions of the foo shader, how do I know which one is being run?
A: Type ‘r_showshader foo’ at the client console, and it will show you the ‘currently running’ foo code [credit: Spike]

Q: I’m making multiple changes to shader code, how can I quickly see the effect of different values?
A: Typing ‘vid_restart’ reloads all textures and shaders

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>