Game Development Community

dev|Pro Game Development Curriculum

TGE Shader Pack

by Koushik · 11/20/2008 (1:38 pm) · 20 comments

Download Code File

This Shader pack contains my previous Normal mapping resource bundled with 3 other shaders on DTS objects. The uploaded file contains 4 source files which you need to add to your existing TGE installation (start from stock TGE + the MK and replace the 4 files with the files in the archive)

The materials.cs file shows a sample of how to setup materials with this new system

Shaders included: Skin Shader, Fur shader, Normal map shader and the toon shader

Usage Instructions
The default MK material system has been somewhat revamped to accommodate for the special setup needed to render these special effects. I will briefly try to explain them. If it does not make sense, or if you need some extra info, please feel free to post a comment and I'll answer them.

1. Parameters:
Parameters (called uniforms in GLSL) are just that - parameters. The best way to understand this is to consider a simple example. Let's say you want to use a particlar shader to render something in some variation of red, depending on, say, the lighting position. If you want to write a shader that worked in a similar manner, only this time rendered in green, you'll need to rewrite the entire shader again and load it into memory. This is a serious waste of time (when you'd be rather writing better gameplay stuff)

So what do you do? You parameterize the color (or whatever you think is going to vary) and set it from script. That way, you define one shader block and use it with different parameters to achieve the desired effect. The modifications I've included support only 3 types of parameters - vectors, floats, and
integers

2. Blend modes:
Blend modes in OpenGL are what control transparency. There are different types of blend modes that you can set depending on your need. In this code, only 3 blend modes are included - (SourceAlpha, 1 - SourceAlpha), (SourceAlpha, One) and off. For more information on Blend modes, you might want to try
this

3. Multipass rendering:
Multipass rendering is quite straight forward. You basically draw the geometry over and over again, using a different parameterized shader each time. The shader bound is the same, only a certain parameter (in most cases the pass number) is altered. Based on the pass number, the geometry is altered in the vertex shader or the fragment shader and a plethora of effects is possible.

In the example set above, the fur shader uses the multi-pass effect. When you flag a material as being multi-pass in script (a method I shall describe below), you can define a uniform variable in the shader, called passNumber (case sensitive) which gets updated automatically by the engine, based on the current render pass.

4. Material Scripts:

Once you get the basics out of the way, setting up the materials is quite easy. Look at materials.cs for for more information on how to do this. You can basically set blend modes as:

blendModes[passNumber] = (either -1, 0 or 1)

-1 disables blending, 0 sets it to (sourceAlpha, 1 - SourceAlpha) and 1 sets it to (sourceAlpha, One), which is used by the fur shader. So, for instance, you have a 2-pass material, and wanted to enable blend on the second pass only, with a (SourceAlpha, 1-sourceAlpha) setup, you'd do:

blendModes[0] = -1;
blendModes[1] = 0;

So much for blending. Simple, isn't it?

To parameterize the shaders, you'd define the shaders as usual. The parameter would then be defined in the shader.vert/shader.frag file as: (assuming you have a float parameter and a vector/color parameter)

uniform float myVar;
uniform vec4 myColor;

For the example above, we have 1 float parameter, and 1 vector parameter. So, in our material script, we write:

numVecParams = 1;
numFloatParams = 1;
floatUniforms[0] = "myVar";
floatvalues[0] = 1.5;
vecUniforms[0] = "myColor";
vecValues[0] = "0.3 0.3 0.3 1.0";

This indicates that you are passing the values stored in the vecValues/floatValues array to the corresponding variable names held in floatUniforms/vecUniforms. This makes the entire thing a little more flexible and intuitive.

You can see the inherent versatility in this method, if you look into the provided material scripts - there are two materials (toonShader11 and toonShader) which use the same shader. They pass different values to the color parameters.

And finally, to create a material with multipass rendering, and say, 3 render passes. You'd add

multiPass = true;
numPasses = 3;

to your material definition block. Then you can define:

uniform float passIndex;

inside your shader (look at furV.glsl for a sample). This variable is automatically updated by the engine everytime it goes to a new pass. So, if you want to do someting special on the third render pass in your vertex shader, you'd do

if(passIndex == 3)
(do something special)
inside your vertex shader.

I've tried to be as brief as possible. If you want more information, please feel free to ask! I'll be around more often from now on (hurray!), so I'll get back to you.

Cheers and best of luck with your projects!

Koushik

P.S: If you want to see some screenies, you can take a look at my last blog post :)

#1
11/21/2008 (12:26 am)
Thank you very much Koushik.

I will try to use it today for tests.

Nicola Buquet
www.buquet-net.com/cv/
#2
11/24/2008 (6:49 am)
Hi Koushik,

I have problem with the DTSBump shader :

When I define a material that uses the DTSBump shader, the material disapears on the DTS.

See screenshot with shader applied : www.buquet-net.com/pub/tge/20081124/tge_001.jpgSee screenshot w/o shader applied : www.buquet-net.com/pub/tge/20081124/tge_002.jpg
On the shader one, the black top appears and disappears realtive to my controller's position.

I'm on MacBook Pro, with a 8600M GT graphic card, 128 MB, Mac OSX.5.4

Do you have any idea on the source of this problem ?

Toon shader is working at the same time on the same scene, on Kork.

Nicolas Buquet
www.buquet-net.com/cv/
#3
11/25/2008 (12:23 am)
Are you defining the blend mode correctly in the material script?

Are you using the normal map shader that comes with this pack or the older one I had submitted? If you're using the older one, you'll have to reset the blend mode in the script.

Try setting the blend mode to off in the script and try again, I suppose the transparency issues might be taken care of.
#4
11/25/2008 (4:43 am)
Very nice resource, ty :)

I got a little problem with water reflection when I apply a shader on the player's model:

gallery.filefront.com/huehn//1121815/

I would love to switch to MK but I still have the issue, that underwater not looks that well. I'am still using CG water resource but MK have a much better design.
#5
11/25/2008 (7:28 am)
Can someone post a image of the fur rendering? I'm interested in seeing that.
#6
11/25/2008 (9:38 am)
@Thomas:
Is that with the default MK? Or is it with the MK + my additions above?

@Tek0:
You can see some shots in my .plan Here. Those shots were a little older. If you need some more shots, in more ambient light, I could upload some for you in a couple of days when I get access to my main system
#7
11/25/2008 (10:14 am)
Kaushik, it's only with applied material shader on the player. If i remove the "Material" definition the reflection is ok and looks like in the stock MK.
#8
11/25/2008 (12:17 pm)
Hi Koushik,

and first, thanks to answer.

I use your last modification, from this actual resource.

I try to set blendModes[0..2] = -1/0 in the Material definition.

But no success. The cube is still near invisible.

When I try to add DTSbump on other models, I get a sort of explosion of black polygons through my view.

Here is a zip of my files if you wish to have a look : www.buquet-net.com/pub/tge/20081125/barebones_dts_shader.zip

Nicolas Buquet
www.buquet-net.com/cv/
#9
11/28/2008 (9:40 pm)
@Thomas:
Thanks for that. I'll take a look at it and get back to you in a bit

@Nicolas:
Just downloaded your file. I'll have to get back to my main rig, which I'll be doing in a couple of days I guess. So I'll take a look at it and let you know.
#10
11/29/2008 (1:26 pm)
Excellent :) When I get back to my project, this will be the first thing to go in.
#11
12/01/2008 (12:58 am)
Koushik,

thank you very much for any help you can bring.

Nicolas
#12
12/08/2008 (4:03 pm)
I had a system crash a couple of days ago. So I guess it might take a few days more for me to check out the problem Nicolas has posted.
#13
12/09/2008 (8:28 am)
Take your time.

I'm quite busy on my side too.

Nicolas Buquet
#14
01/01/2009 (4:34 am)
Not sure if I found a bug or if it's a greater problem, this was crashing me because I think the vars were not getting reset each time in the loop?

I had to move this line in tsMesh.cc
S32 numParamsI = 0, numParamsF = 0, numParamsC = 0;

down inside the loop:

for (S32 i=0; i<primitives.size(); i++)
   {
        S32 numParamsI = 0, numParamsF = 0, numParamsC = 0;

Koushik, I'm not sure I understand how the textures need to be made for the fur effect, can you elaborate on that or supply the fur.png/rainbow.bmp that are in the sample definition?
#15
01/04/2009 (7:12 pm)
It stopped crashing after you made that change? Strange. The material is set only once per object, not once per primitive, so the variables aren't going to be altered anywhere inside for sure.

I'll check into it anyway.

About the fur - here's the fur.png file Download

The rainbow.bmp file is just a texture. You can put whatever you want to here. It is basically the UV-mapped texture, which is applied in accordance with the texture co-ordinates of the model that you're applying the shader on.

The fur.png file is basically a random noise file. It contains a grey-scale image (in RGB of course) with some random values for the 4 channels (RGBA) This is then used to create the fur 'strands'. This is what gives it the appearance of 'hairiness' if I might call it. You can also alter the fur density by altering the relative black isto white in the fur.png image.

I hope that explains it a bit.
#16
01/30/2009 (6:42 am)
Nice resources Koushik! You're taking a bunch over on behalf of Alex. That is cool!

This probably isn't the right post for this stuff, but...

I have 4 questions if you have the time:

1. You wouldn't happen to beable to point me in the right direction on a GLSL grass shader would you? I'm just a newbie to the shaders, and I expect the grass shader would be similar to the fur shader you have here... but I've found a couple grass shaders, a bunch of tutorials (that really helped from the get-go, but haven't helped me to advance)...can't get them into the engine, nor do I know enough to convert HLSL to GLSL; I want to make my own anyways and not just copy someone else. Any ideas of resources/webpages to learn something like this? He he! Should I say: quickly learn something like this?

2. You have shader support for DTS and interiors...what about your textures on the terrain? Maybe a resource I haven't come across yet.
That is also part of wanting a grass shader :)

3. I have a cave area and it is lighting really wierd. Once it will be black, then it will be really light. I have my settings set fairly dark, (can't remember off the top of my head...at work)

bias limit? (0.15)
bias (-0.04, 0.0)
scale is (1,2)
BloomOffset (0.3)
BloomQuality (1)

scene is lit at:
ambient: 0.057 0.057 0.057 1
sun: 0.047 0.045 0.047 1

I deleted all the lighting files and just full relight so I know I have 1 lighting file and it is the one I want. Any ideas/solutions on why it is doing this?

4. A major preformance hit...
I was running my forest at about 57 to 80 fps...with MK (at the settings above) it is cut in half...27 to 45 fps

I was running my caves at 75 to 110 fps and with MK it is 52 to 84 fps
BIG preformance hit... So is there a setting I need to adjust more? or a little code change...shader change I can do to make it run better? I really like they way it looks, just destroyed my fps.

Any advice/solutions about this stuff would be GREATLY APPRECIATED!!!

GOD BLESS!
J
Emal: jeremy@act7gaming.com

EDIT: forgot html for second there
#17
02/23/2009 (2:07 pm)
I have the same issues as you do on the lighting issue number 3. Im still trying to find out whats causing it though. If I find a fix I'll post it here. I have noticed so far it changes based on the camera position and it only affects the side that the sun is hitting I think since it only affects one side.

Found a temporary fix for the drastic performance issues Im having it went from 90-100 fps with no dts shader down to 24-26 with dts shaders, with this change in the bumpP.glsl file It went back u pto 30-35 fps, change all instances of gl_LightSource[i] to gl_LightSource[0], the one side effect I have found so far is I've had to manually set the ambient light to a very very dark color in order to make it look decent.
#18
06/02/2009 (4:22 am)
Any chance of getting this ported to TGEA ? :P
#19
01/03/2011 (8:41 pm)
Is there a way to set up the shader to not use mapTo? If I set mapTo = base.body, all my characters with base.body all have the same texture.
#20
11/05/2012 (12:43 am)
I got a message error at loading in console:

GLSL vertex shader failed to compile!
GLSL vertex shader info log ERROR: 0:19: '-' :  wrong operand types no operation '-' exists that takes a left-hand operand of type 'uniform float' and a right operand of type 'const int' (or there is no acceptable conversion)
ERROR: 0:26: '-' :  wrong operand types no operation '-' exists that takes a left-hand operand of type 'uniform float' and a right operand of type 'const int' (or there is no acceptable conversion)