Jump to content
  • Advertisement
Sign in to follow this  
Freeky

Drawing entities from back to front

This topic is 1803 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement

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.

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.

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.

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.

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!