I have a problem with rendering some objects with OpenGL (C++). I can’t understand the right order of rendering and right blending and depth options.
I have following objects:
- Layers - they are flat, horizontally aligned. They must be a little transparent
- Objects - they also must be a little transparent
The problem is that if I render objects after layers, they are not visible behind layers, and if I render them before layers, they are not transparent.
Could you explain me what blending options, depth function and rendering order should I use?
Now I use:
If you’re using transparency, objects must be rendered from back to front (or from front to back with an alpha channel in the framebuffer and a blending mode which uses destination alpha, but that’s usually needless complexity).
And if you’re rendering from back to front, you don’t need to use a depth buffer. The purpose of a depth buffer is so that when objects overlap, you always get the front-most object regardless of rendering order, rather than the last object rendered. If you’re rendering from back to front, the front-most object is the last object rendered.
If you have a mix of opaque and transparent objects, the usual approach is to render the opaque objects first, in any order, with depth tests and writes enabled, then the transparent objects afterwards, from back to front, with depth tests enabled (so that they will be occluded by any opaque objects which are in front of them) and depth writes disabled (because there’s no need for them).
The hard part is getting the order correct for arbitrary geometry. That’s why depth buffers are normally used for opaque objects rather than simply rendering from back to front.