Sign in to follow this  
Freeky

Drawing entities from back to front

Recommended Posts

Freeky    195

My last problem has barely been solved and I have already encountered the next one.

 

I am making a simple 3D first-person game. I made trees by rendering 2D sprites in my 3D world wich are always turned toward the player.

 

The tree sprites have some transparent pixels in them. When a few of them overlap, sometimes, a part of the fruthest tree is not rendered behind the transparent pixels of the one in front of it. It's hard to explain so here is a screenshot:

2uh5kys.png

 

Anyway, I did some googling around and found out that the best solution would be to make sure everything is rendered from back to front.

 

Everything visible in my game is an entity and I just loop through them all every frame to render them, but now I'm going to have to make sure they are all rendered from far away from the player -> close to the player. That order could theoretically change every frame.

 

I wonder what would be an efficient way to make sure all entities are rendered ordered by their distance to the player? The height coordinate (Y-axis) does not matter in this case, because this is 0 for every entity.

 

Many thanks in advance,

Freek

Share this post


Link to post
Share on other sites
Freeky    195

I just did something like this. I just sorted all entities by the distance to the player, but when I turn the camera slightly, sometimes the transparent pixels still overlap.

 

By the way: The sprites only have fully transparent and fully visible pixels, so nothing in between.

Share this post


Link to post
Share on other sites
NightCreature83    5002

It is better to not have the entities draw themselves and have them generate the data needed for rendering and passing this to the renderer. The renderer can then decide how this should be ordered and rendered, this allows you to not have to care about the update order in the entities. And you can do more sorting in the renderer other then back to front ordering.

Share this post


Link to post
Share on other sites
SyncViews    734

Even with sorting in the renderer, doing sorting for say alpha blending has a lot of difficult edge cases, especially if 2 things you want to alpha blend are allowed to intersect, since then there is no correct order unless you split the objects along the intersection. e.g had this playing around with some simple water and small piece of floating semi transparent ice, both using alpha. You need to draw the ice block below the water line first, then the water, then the part of the ice block above the water line. Didn't really solve it, although in that case make the ice not transparent at all was not a major loss.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this