Hey!
I've just implemented shaders in GLSL for per-pixel lighting, and am trying to get a multi-pass system working for rendering multiple dynamic lights. From what I've read in books and on these forums, the basic idea is to render ambient light in an initial pass, and then add the diffuse lighting components in the next passes.
It also seems like a good idea to write the z-buffer in only the first pass, to save time in the later ones. However, this doesn't seem to be working in my code. (The gl. prefix on all my calls is because I'm using Java and JOGL).
In my initialization code, I do this:
gl.glEnable(GL.GL_BLEND);
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glDepthFunc(GL.GL_LEQUAL);
My rendering code looks like this:
gl.glClear(GL.GL_COLOR_BUFFER_BIT|GL.GL_DEPTH_BUFFER_BIT);
// Ambient and depth buffer pass.
gl.glBlendFunc(GL.GL_ONE,GL.GL_ZERO);
// Enable depth buffer writes.
gl.glDepthMask(true);
ambientShader.enable();
renderScene();
ambientShader.disable();
// Lighting passes.
// Add new fragment colors to colors from previous pass.
gl.glBlendFunc(GL.GL_ONE,GL.GL_ONE);
// Disable depth buffer writes.
gl.glDepthMask(false);
diffuseShader.enable();
for(int i=0;i<lightManager.getNumLights();i++)
{
diffuseShader.sendUniform("lightIndex", i);
renderScene();
}
diffuseShader.disable();
The result of the above code is what appears to be intense z-fighting. If I set DepthMask to true for the diffuse passes, it looks fine. I have also tried changing the depth function for the diffuse passes to GL_EQUAL, but it doesn't change anything.
I would really like to take advantage of the speed of writing the depth buffer only once. Can anyone see a problem with my code? Or offer other things to try?
Thanks!
[Edited by - Aikavanak on July 10, 2008 11:36:57 PM]