Blinn-Phong shader acting strange(?)

When trying to implement per pixel lighting in GLSL, I noticed that my specular light didn't seem to show up quite right. So I stripped it from the shader, leaving only diffuse lighting in, or so I thought. Here are my shaders:

private static final String vShader =
"   varying vec4 diffuse,ambient;"+
"   varying vec3 normal,lightDir,halfVector;"+
"   void main()"+
"   {   "+
"      normal = normalize(gl_NormalMatrix * gl_Normal);"+
"      lightDir = gl_LightSource[0].position;"+
"      diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;"+
"      ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;"+
"      ambient += gl_LightModel.ambient * gl_FrontMaterial.ambient;"+
"      gl_Position = ftransform();"+
"   }"+
        "";
       
private static final String fShader =
"   varying vec4 diffuse,ambient;"+
"   varying vec3 normal,lightDir;" +
"   void main()"+
"   {"+
"      vec3 n;" +
"      float NdotL;" +
"      vec4 color = ambient;"+
"      n = normalize(normal);"+
"      NdotL = max(dot(n,lightDir),0.0);"+
"      if (NdotL > 0.0) {"+
"         color += diffuse * NdotL;"+
"      }"+
"      gl_FragColor = color;"+
"        }"+
        "";



And here's how I use them:

    protected void simpleInitGame() {
        lightState.detachAll();
        DirectionalLight l = new DirectionalLight();
        l.setDirection(new Vector3f(1, 0, 0));
        lightState.attach(l);
       
        FluidSimHeightMap heightMap = new FluidSimHeightMap(129, 120);
        heightMap.setHeightScale(0.001f);
        Vector3f terrainScale = new Vector3f(10, 1, 10);
        TerrainPage terrain = new TerrainPage("Terrain", 33, heightMap
                        .getSize(), terrainScale, heightMap.getHeightMap(), false);

        rootNode.attachChild(terrain);
       
        GLSLShaderObjectsState so = display.getRenderer().createGLSLShaderObjectsState();
        so.load(vShader, fShader);
        rootNode.setRenderState(so);
       
        lightState.setEnabled(false);
       
        cam.setLocation(new Vector3f(0, 128, 0));
        rootNode.updateRenderState();
    }



I really can't see what would be wrong with the shaders, but when viewing the sene in jME the lighting appears to depend not only on the light vector, but on the camera orientation as well - that can't be right! There must be a problem with the shaders, but I cannot find it!
I took the GLSL code from http://www.lighthouse3d.com/opengl/glsl/index.php?dirlightpix, and to me it seems to be correct...
Could somebody please enlighten me?

You are not performing any calculations between the light direction and the surface normal in order to obtain an intensity for your diffuse colour.

Try the code from this page of that website:



http://www.lighthouse3d.com/opengl/glsl/index.php?pointlight

adamgp said:

You are not performing any calculations between the light direction and the surface normal in order to obtain an intensity for your diffuse colour.

Then what does this do?
hevee said:


"      n = normalize(normal);"+
"      NdotL = max(dot(n,lightDir),0.0);"+
"      if (NdotL > 0.0) {"+
"         color += diffuse * NdotL;"+
"      }"+
"      gl_FragColor = color;"+



As for the pointlight code, I know that, but right now I am after a directional light effect. How would I use the pointlight code for that?

Sorry, I totally overlooked that. I saw the varying half vector which wasn't used in vertex shader and jumped to conclusions.



I can't say whats wrong without trying it out - which I can't do at work.

Sorry, but still… bump



It would really help me if somebody could try my code, you won't need more than a SimpleGame subclass to paste it to and a minute to run it. Then, tell me what you see when moving around or rotating the camera. Does the lighting change? (It shouldn't, but for me it does.)

Thank you!

The lighting changes. Especially when I turn the camera upwards some areas gets much darker.



I'm also working on per pixel lighting with the same shaders from the url you posted. I didn't notice any problems with directional lighting (but then I don't move the camera) but I'm having some problems with point lights and the position of the specular highlight. Perhaps they are related…

Mine didnt change one bit. But then I had to made a slight alteration to your vertex shader to allow it to compile.

I had to swizzle your gl_LightSource[0].position by adding .xyz to the end to provide compatible types.



lightDir = gl_LightSource[0].position.xyz;

adamgp said:

Mine didnt change one bit. But then I had to made a slight alteration to your vertex shader to allow it to compile.

Weird.

Even more so since I think I may have gotten closer to the reason for my problem: turns out that if I do

normal = normalize(gl_Normal);


instead of

normal = normalize(gl_NormalMatrix * gl_Normal);


everything is fine. But wouldn' that break if I ever have a modified modelview matrix in effect?



adamgp said:

I had to swizzle your gl_LightSource[0].position by adding .xyz to the end to provide compatible types.

Sorry for that, my nvidia driver is so forgiving with that that I tend to forget about avoiding implicit conversions :(

Well i'm glad my copy doesn't exhibit the strange behaviour, but sorry I can't help any further. :expressionless:

Just out of interest, what graphics card/drivers do you have?

nvidia geforce go 7400, on winXP32. The driver is quite dated (version 165.01), as it is the notebook manufacturer's responsibility to publish drivers for the 7x series.

Of course hp (just as almost any other manufacturer) never cared to update the graphics drivers since I bought my notebook 3 years ago. Now that I think about it, I'll try the shader in linux later today and see what that will bring…

I have exactly the same problem with my Toshiba GeForce 7600 Go. Im using the manufacturer drivers. There's a website called www.laptopvideo2go.com which you might be interested in, I'll say no more - but all the variations confuse the hell out of me.

Hehe, looks like I actually have a driver from  laptopvideo2go on windows… I thought I had removed it in favor of the original driver, but apparently I forgot to.



Anyway, I have the same problem under linux (ubuntu 7.10), with nvidia's linux drivers which are linked from ubuntu's package repository AFAIK. So I guess it's not the driver's fault after all.

And I found that not multiplying with gl_NormalMatrix doesn't really help, it just made the problem more visible in one specific case.





So, to summarize:

For my nvidia gf go 7400 normals as reported by glsl's gl_Normal are changing based on the camera view angle. This problem exists independent of driver version or even OS.