Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Drawing entities from back to front


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Freeky   Members   -  Reputation: 187

Like
0Likes
Like

Posted 11 August 2013 - 04:47 PM

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



Sponsor:

#2 David.M   Members   -  Reputation: 731

Like
0Likes
Like

Posted 11 August 2013 - 05:00 PM

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.



#3 Freeky   Members   -  Reputation: 187

Like
0Likes
Like

Posted 11 August 2013 - 05:59 PM

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.



#4 Freeky   Members   -  Reputation: 187

Like
1Likes
Like

Posted 11 August 2013 - 06:30 PM

I just found out I could just do this:

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

This does exactly what I wanted.



#5 NightCreature83   Crossbones+   -  Reputation: 2930

Like
0Likes
Like

Posted 12 August 2013 - 02:18 AM

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.


Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#6 SyncViews   Members   -  Reputation: 465

Like
0Likes
Like

Posted 12 August 2013 - 03:25 AM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS