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?