Sign in to follow this  
Followers 0
Freeky

Drawing entities from back to front

5 posts in this topic

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

0

Share this post


Link to post
Share on other sites

You could introduce a z-order component to each of your items. Then sort your list of items by z-order and draw them in order.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

I just found out I could just do this:

glAlphaFunc(GL_GREATER, 0.1f);
glEnable(GL_ALPHA_TEST);

This does exactly what I wanted.

1

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

0

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  
Followers 0