I fear this will take some advanced explaining…
Ok, I had my simple engine up-and-rendering away happily, opaque & transparent meshes using GLSL and stuff. So far so good. However, the rendering used multiple lights in one pass, all calculated & accumulated in the GLSL code. This turned out to be a bad design decision, mostly because it just requires too many input attributes. I hit a limit (OpenGL ES).
So I decided to turn it into multi-pass rendering, with each light’s contribution blended additively to the rendered mesh.
Having read a bit about how go go about it, I came up with this schematic approach:
Pass 1:
[ul][li]Ambient lighting (+ one light, optionally)[]Blending disabled[]Z-test enabled, GL_LEQUAL (or GL_LESS)[]Z-write enabled[/ul][/li]
Pass 2-n: (depending on number of affecting lights)
[ul][li]Additive blending (i.e. GL_FUNC_ADD, GL_ONE, GL_ONE)[]Z-test enabled, GL_EQUAL[*]Z-write disabled[/ul][/li]
First off, does this look like a working assumption?
However, as always, things just doesn’t work as expected, do they?
My test app renders a few opaque primitives with a single shader and three lights (different colors, to be identifiable). It has mouse control to orbit the camera around the scene and also zooming. Nothing is animated.
So, when using the optional light in the 1st pass it will render 3 passes.
Now to my problem: When the app starts, without moving the mouse, I can see all everything rendered correctly. But as soon as I rotate the mouse the rendered meshes are kind of “peeled” away and disappears. A tad difficult to describe this phenomenon. This “peeling” is probably a side-effect of tessellation.
If I zoom inwards they become visible again. But if I zoom out, or rotate, they disappear. Also, to see them again I must zoom in further than I did the first time, and so on. After a while of this zoom in+out further and further, I can’t see the rendered objects at all any more.
These symptoms leads my fumbling mind to the conclusion that this is related the Z-buffer somehow.
If I disable Z-testing for all passes I can see everything all the time, but then of course, the rendering will not be correct.
And yes, I am clearing the Z-buffer using glClear().
And also, this behaviour is identical on multiple platforms & drivers, so I’m quite sure the culprit is somewhere in my application.
I can’t attach images at this time, but will attempt to do so later if my explanation is insufficient (probably).
Any tips on things to test & verify are gladly appreciated!
Or even flames for my stupidity