Hi!
I want to render some meshes with translucency and multiple lighting passes. I compute the contribution of each light with a relatively big GLSL shader, and blend it with what already exists in the framebuffer. For fully opaque meshes, this requires rendering a black pass first, then blending with GL_ONE,GL_ONE. It works, and quite well.
But when rendering translucent meshes, there is a problem. For each such mesh, I first render it with color writes disabled and alpha writes enabled, to get the desired alpha value into the framebuffer. Then I enable blending with GL_DST_ALPHA,GL_ONE_MINUS_DST_ALPHA, enable color writes, disable alpha writes, and render the mesh N more times for N lights.
Let C_n be the color computed by lighting pass N, let C_b be the color in the framebuffer before blending, and let A be the destination alpha value. For one light, the final color is
C_1A+C_b(1-A)
This is correct. But for two lights, it is
C_2A+(C_1A+C_b*(1-A))*(1-A)
which is not correct, because this blends C_1 with the factor (1-A)*A, not A, and it blends C_b with (1-A)^2, not (1-A). It should be
(C_1+C_2)A+C_b(1-A)
instead. The two lighting passes must be summed up BEFORE blending them with the framebuffer. I also looked at other blend modes, but so far I couldn’t find any combination that fits.
And here lies the problem: I can’t sum them up in the framebuffer like I do with opaque meshes, because colors in the framebuffer need to be preserved for blending. I see that current games have translucent polygons and they are correctly lighted (and shadowed), so there must be a way to solve this. But how?
If anybody knows how to do this, and would care to enlighten me, I would be really, really thankful!