0
\$\begingroup\$

I have a question about how to proper render 3d objects in a good order. I use blend states to render some 3d models with a transparent texture. This works but if there are multiple alpha objects in the scene it gets messed up because of the seemingly random render order.

Now I ask myself how should I do it?

The first theory I have in mind would be: When I render something I actually don't render it, I just save it in some sort of queue and sort it later at the final end call (expensive because I have to allocate / deallocate memory a lot and sort).

But even with this theory I still dont know how I should sort them. I cant just look how they are positioned because at some times they are kind of in each other so looking for these values would produce in wrong results.

I already guessed that i have to order in the way the camera is positioned or some way.

Hope you can help me

\$\endgroup\$
  • \$\begingroup\$ As an aside, why would a sorted collection of translucent objects to render require repeated allocation & deallocation? Since you're using this list every frame, you can simply keep it persistent, with enough capacity to handle your most demanding case. This also saves much of the sorting cost, since barring a camera teleport, the order this frame should be mostly similar to the order from last frame, generally requiring less fixup than a worst-case n•log(n) sort. Algorithms like insertion sort can handle these almost-sorted lists quite efficiently. \$\endgroup\$ – DMGregory Sep 1 '17 at 16:20
1
\$\begingroup\$

Rendering transparency is a tricky subject and hard to completely overcome. Many rendering engines are just using hacks to overcome complexity, achieve good performance for the price of reduced visual quality.

The easiest way is just to sort your objects from back to front and render with alpha blending. Obviously, this approach is very limited and can easily produce incorrect results. with overlapping objects and even just overlapping geometry within a single object.

You could use a blend state which doesn't require correct ordering, like additive blending. This is probably not what you want, but maybe you can get away with it for some objects and art which takes it into account. I've seen a game for example with transparent umbrella being rendered this way. Users probably won't notice if you don't overuse it.

Then there are the advanced ways of order independent transparency (OIT). For example depth peeling renders the geometry several times in multiple layers from back to front to overcome the sorting issues with overlapping geometry. This is probably very costly. The more recent graphics APIs and hardware lets you use advanced data structures in your rendering, like you could create per pixel linked lists and sort per pixel. Some games are already utilizing techniques like this.

As you can see it is an advanced topic, but you have some options already and probably plenty opportunity for research as well, good luck!

\$\endgroup\$
  • \$\begingroup\$ @Bálint: without sorting, there's no guarantee you'll get the right order. Even with a sorted list of objects there are challenges with the order of faces within a single object, or with intersecting/bracketing geometry that's neither fully in front nor behind. \$\endgroup\$ – DMGregory Sep 1 '17 at 16:08
  • 1
    \$\begingroup\$ thanks <3 additive blending worked like charm and is exactly what i serached bec i only use it for some small lasers \$\endgroup\$ – L. Terrat Sep 11 '17 at 20:49

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.