3
\$\begingroup\$

I am rendering a world, object, and models from an older game format. The world is presented in a BSP tree so I can quickly iterate through. The opaque surfaces are rendered front to back. Then transparent surfaces are rendered back to front. For the world, that is all fine. I add the transparent pieces into a stack and pop them off one at a time and render.

Now I am realizing that characters that move in the world also support transparency and there is a possibility for error if I render them after the world and they happen to be overlapping with other semi transparent surfaces. Similarly, I have objects to handle.

The solution I have been kicking around in my head is to create a vector of containers that hold my current render structure and a distance from the camera. I would then sort these structures based on distance and then render them one by one.

As it stands now, my render call accepts a pretty generic object:

struct RenderCall
{
    GLuint textureID;
    int indexStart;
    int indexCount;
};

struct TransparentRenderCall
{
    float distanceToCamera;
    RenderCall call;
};

What do you think? Is there a more streamlined way to do this?

\$\endgroup\$
1
\$\begingroup\$

What you've described (Painter's Algorithm) is indeed the way it's usually done. There are however maybe a few things to consider in addition:

  • If you're sorting by object, then there are some limitations. These might not be super important in your case, but if you have a lot of complex objects with transparency, your sort might break in situations like this, where A is the camera and B and C your transparent objects:
    transparency sort fail case
    source

  • Many of the objects in your scene may be outside of the viewport/frustum so there's no point in sorting those. Usually sorting is done after frustum culling on every render call, rather than every time an object moves or so. In other words, on rendering, you get the visible transparent objects, then sort them, then render them. You kind of get part of that for free anyway by storing in a BSP tree.

  • There are alternative commutative blending methods that are rendering order independent. Those might be worth a look at. What you want to search is Order-independent Transparency.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.