Hey,
This may be more like a general OpenGL question, but I’m really struggling with this one…
I am trying to get per-pixel dynamic lights working in my JOGL app (an extremely young game engine), using multiple passes to draw each light and blend them together. From what I know, the basic idea is to render ambient light in an initial pass, and then add the diffuse lighting components in the next passes, one for each light.
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.
I initialize OpenGL like this:
gl.glEnable(GL.GL_BLEND);
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glDepthFunc(GL.GL_LEQUAL);
And 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();
With this code, I get strange artifacts that I thought at first were due to z-fighting, because if I set glDepthMask(true) for the diffuse passes, the artifacts disappear. But polygons aren’t appearing over others, sections of my rendered model are just disappearing. Right now I’m testing with only one light.
Each pass is working fine on its own, I’ve tried commenting out one or the other and looking at the results.
Is there anything wrong with my code? I don’t think the problem in is the shaders, but I can post their code too if it would be helpful. I can also try to upload some screenshots of the artifacts.
System specs:
Windows Vista Business 64-bit
nVidida GeForce 8800 GTS 640MB with 175.19 drivers
JDK/JRE 1.6.0 update 4
JOGL 1.1.1
Thanks!