dim3 Forum

Full Version: Shaders and Dim3
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5
What operation is this? I would like to know... Ebentaully maybe I'll be a GLSL expert, but not for a while. :P
I'm just typing this in (not testing so it might be bad), but a quick version would be something like:

Code:
uniform sampler2D tex;
uniform vec3 dim3ClosestLightNormal;
varying vec3 normal;

void main()
{
float intensity;
vec4 color,pix;

intensity = dot(dim3ClosestLightNormal,normal);
pix = texture2D(tex,gl_TexCoord[0].st);

if (intensity > 0.95)
  color = pix;
else if (intensity > 0.5)
  color = pix * 0.6;
else if (intensity > 0.25)
  color = pix * 0.4;
else
  color = pix * 0.2;

gl_FragColor = color;
}

Instead of using the flat model color (which is what you want in a real cartoon effect) it uses the texture pixel.

Note that these are just changes to the original shader; that shader has a bunch of problems, one being it only uses one light and can get jumpy going in and out of light. Those circumstances will have to be accounted for, I was just getting the example going. I might play with this a bit more later on and make sure whatever files I end up with get up on the dim3 files site.

[>] Brian
Oh I understand! GLSL doesn't look too difficult, maybe I'll spend a little time on it next weekend.
Hey, Brian. Thanks for checking out the shaders.

I just pulled that one off the net somewhere. I'm in search of a bloom/blur/hdr shader. I'm looking to achieve the same effect used in games such as Shadow Of The Colossus and Doom 3 [I think doom 3 has it..] and Oblivion and games like that.

Is there a fast and reliable way to do this?

I pulled the shader from GameDev Forums: http://www.gamedev.net/community/forums/..._id=395186

I have no experience with GLSL and only very little of just GL.
I just pulled this shader off the net, it doesn't work without modification, but it seems a little closer to right than the previous one I posted.

Code:
Vertex program

uniform vec3 LightDir;
uniform vec4 vViewPosition;
uniform mat4 matViewProjection;

varying vec2 texCoord;
varying vec3 normal;
varying vec3 lightDirInTangent;
varying vec3 viewDirInTangent;

attribute vec3 rm_Tangent;
attribute vec3 rm_Binormal;

void main(void)
{
   texCoord = gl_MultiTexCoord0.xy;
  
   mat3 tangentMat = mat3(rm_Tangent,
                          rm_Binormal,
                          gl_Normal);
   lightDirInTangent = normalize(LightDir) * tangentMat;
   viewDirInTangent  = normalize(vViewPosition-gl_Position).xyz * tangentMat;
  
   gl_Position = ftransform();
}

Fragment program

uniform sampler2D BumpMap;
uniform sampler2D ObjectMap;
uniform sampler2D SpecMap;

uniform float Shininess;
uniform float SpecularIntensity;

varying vec2 texCoord;
varying vec3 lightDirInTangent;
varying vec3 viewDirInTangent;

void main(void)
{
   vec3  n_lightDirInTangent = -normalize(lightDirInTangent);
   vec3  n_viewDirInTangent = normalize(viewDirInTangent);
   vec3  bump     = normalize(texture2D(BumpMap,texCoord).xyz * 2.0 - 1.0);
   float lighting = dot(bump,n_lightDirInTangent);
   float blighting= n_lightDirInTangent.z;
   float specular = dot(-reflect(n_lightDirInTangent,bump),n_viewDirInTangent);
  
   vec4 texColor = texture2D(ObjectMap,texCoord);
   vec4 specColor = texture2D(SpecMap, texCoord);
   vec4 color = texColor * lighting
              + float(lighting>0.0)*float(blighting>0.0)
                * SpecularIntensity*pow(specular,Shininess)*specColor;
   gl_FragColor = vec4(color.xyz*0.5,0.414);
}
I'll look at this when I get a chance but half the variables at the top are passed in by core and without telling exactly what they are meant to do, it's really hard to figure out what they are trying to do.

As I've said before, these shaders are designed to work with a certain core and would need a lot of modification.

I've got the earlier shader working, it just doesn't seem to do much.

[>] Brian
brian, what do the toonshaders look like on your computer?
mafoo Wrote:brian, what do the toonshaders look like on your computer?

There's a picture on page 2 (an earlier version that didn't use the alternate color you can have in the new beta.)

Just for laughs, here's the new versions (b14).

This is the vertex shader, same for both:

Code:
varying vec3 normal;
varying vec4 uv;

void main()
{
    normal=gl_Normal;
    uv=gl_MultiTexCoord0;
    
    gl_Position=ftransform();
}

Use this fragment program for texture alternate color toon shading (like the picture.)

Code:
uniform bool dim3HasClosestLight;
uniform vec3 dim3ClosestLightNormal;
uniform vec4 dim3TexColor;
varying vec3 normal;

void main()
{
    float    intensity;
    
    if (!dim3HasClosestLight) {
        gl_FragColor = vec4(0.0);
    }
    else {
        intensity = dot(dim3ClosestLightNormal,normal);
        
        if (intensity > 0.95)
            gl_FragColor = dim3TexColor;
        else if (intensity > 0.5)
            gl_FragColor = dim3TexColor * 0.6;
        else if (intensity > 0.25)
            gl_FragColor = dim3TexColor * 0.4;
        else
            gl_FragColor = dim3TexColor * 0.2;
    }
}

Use this to toon shade the texture mapped object.  This looks good on high colored, less busy textures, and not so good on regular busy textures.

Code:
uniform sampler2D dim3Tex;
uniform bool dim3HasClosestLight;
uniform vec3 dim3ClosestLightNormal;
varying vec3 normal;
varying vec4 uv;

void main()
{
    float    intensity;
    vec4    pix;
    
    if (!dim3HasClosestLight) {
        gl_FragColor=vec4(0.0);
    }
    else {
        pix = texture2D(dim3Tex,uv.st);
        intensity = dot(dim3ClosestLightNormal,normal);

        if (intensity > 0.95)
            gl_FragColor = pix;
        else if (intensity > 0.5)
            gl_FragColor = pix * 0.6;
        else if (intensity > 0.25)
            gl_FragColor = pix * 0.4;
        else
            gl_FragColor = pix * 0.2;
    }
}

Note that shaders on the cards require 10.4.8, and you can hit F1 and check the console to see if GLSL is supported on your card/machine.

[>] Brian
I'm not at home right now, so I cannot test shaders in dim3, but I found these for Bloom and Blur:

Blur [Horizontal and Vertical]:
Code:
uniform sampler2D texture0_2D;
uniform float ddx;
uniform float ddy;

void main(void)
{
  //Initialisierungen
  vec4 outp = vec4(0.0, 0.0, 0.0, 0.0);

  // Texturen auslesen
  // und vertikal bluren (gauss)
  outp += 0.015625 * texture2D(texture0_2D, gl_TexCoord[0].xy + vec2(ddx*-3.0, 0.0) );
  outp += 0.09375 * texture2D(texture0_2D, gl_TexCoord[0].xy + vec2(ddx*-2.0, 0.0) );
  outp += 0.234375 * texture2D(texture0_2D, gl_TexCoord[0].xy + vec2(ddx*-1.0, 0.0) );
  outp += 0.3125 * texture2D(texture0_2D, gl_TexCoord[0].xy + vec2(0.0, 0.0) );
  outp += 0.234375 * texture2D(texture0_2D, gl_TexCoord[0].xy + vec2(ddx*1.0, 0.0) );
  outp += 0.09375 * texture2D(texture0_2D, gl_TexCoord[0].xy + vec2(ddx*2.0, 0.0) );
  outp += 0.015625 * texture2D(texture0_2D, gl_TexCoord[0].xy + vec2(ddx*3.0, 0.0) );
// Texturen auslesen
  // und horizontal bluren (gauss)
  outp += 0.015625 * texture2D(texture0_2D, gl_TexCoord[0].xy + vec2(0.0, ddy*-3.0) );
  outp += 0.09375 * texture2D(texture0_2D, gl_TexCoord[0].xy + vec2(0.0, ddy*-2.0) );
  outp += 0.234375 * texture2D(texture0_2D, gl_TexCoord[0].xy + vec2(0.0, ddy*-1.0) );
  outp += 0.3125 * texture2D(texture0_2D, gl_TexCoord[0].xy + vec2(0.0, 0.0) );
  outp += 0.234375 * texture2D(texture0_2D, gl_TexCoord[0].xy + vec2(0.0, ddy*1.0) );
  outp += 0.09375 * texture2D(texture0_2D, gl_TexCoord[0].xy + vec2(0.0, ddy*2.0) );
  outp += 0.015625 * texture2D(texture0_2D, gl_TexCoord[0].xy + vec2(0.0, ddy*3.0) );


  gl_FragColor =  outp;
}
Bloom:
Code:
uniform sampler2D texture0_2D;
uniform vec4 Contrast;
uniform vec4 Brightness;

vec4 saturate(vec4 inp)
{
  return clamp(inp, 0.0, 1.0);
}

void main(void)
{
  //Initialisierungen
  vec4 outp = vec4(0.0, 0.0, 0.0, 0.0);

  // Texturen auslesen
  outp = texture2D(texture0_2D, gl_TexCoord[0].xy );

  //Kontrast
  vec4 c = vec4(2.0,2.0,2.0,1.0) * (vec4(1.0,1.0,1.0,1.0) - Contrast);
  outp = saturate((outp - vec4(0.5,0.5,0.5,0.5)) * c + vec4(0.5,0.5,0.5,0.5));

  //Farben pushen
  outp *= Brightness;

  //Helligkeit
  vec4 b = (vec4(1.0,1.0,1.0,1.0) - Contrast * vec4(2.0,2.0,2.0,1.0));
  outp = saturate(outp + b);

  gl_FragColor =  outp;
}

I guess you could combine these because dim3 will not let you use two at a time, but I could be wrong. Can anybody confirm whether these work or not?
Anonymous Helper Wrote:I'm not at home right now, so I cannot test shaders in dim3, but I found these for Bloom and Blur:

I'll take a look at these when I get the chance ... to make it do both these things, you'll want to combine them into one shader and make sure the output of one goes into the input of the other and then finally sets the pixel (i.e., gl_FragColor).

Note that ANYTHING with a "uniform" before it is passed in through core.  Both of these have "texture0_2D", which is obviously the texture in unit 0, which you substitute for the dim3 version (which is currently dim3Tex in the version on my machine, soon to be on yours Smile ).

What ddx, ddy, Contrast and Brightness are supposed to be I don't know.  That's the problem with some of these shaders, unless you are tailoring it for your project they are full of variables that have some expected value from core.

There's a good chance that these are some kind of constants, so you can always just hard set them to stuff and see what happens. For instance, instead of:

Code:
uniform float ddx;

You could have:

Code:
float ddx=1.0;

(Obviously the 1.0 is just a random number, I have no idea what they want here without trying to take apart the math.)

[>] Brian
Pages: 1 2 3 4 5
Reference URL's