Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Optimal way to do z-sorting  (Read 4155 times)

0 Members and 1 Guest are viewing this topic.

bullno1

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Optimal way to do z-sorting
« on: December 10, 2008, 01:49:33 pm »
Currently, I'm storing my sprites in a std::list then I sort them every frame according to their "z" values through std::list::sort so that I can draw them in the correct order.
To speed it up, I have different lists of sprite called layers. Their their rendering flags decides whether they are sorted every frame(moving object) or sorted only once(background objects)
However, I still want to know if there is a better way of doing this? Does OpenGL have any function to exploit GPU power to do sorting?

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Optimal way to do z-sorting
« Reply #1 on: December 10, 2008, 02:39:25 pm »
Well, I would do that :
if sprite added or their z has changed, set 'sort' to true (a bool). Before drawing, test 'sort' to know if you need to sort it or not.
SFML / OS X developer

Imbue

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Re: Optimal way to do z-sorting
« Reply #2 on: December 10, 2008, 07:21:10 pm »
Quote from: "bullno1"
Does OpenGL have any function to exploit GPU power to do sorting?
Yes, it certainly does. All of your 2d sprites are actually drawn as 3d squares in 3d space. If SFML only passes a Z value (and sets some other states), then the graphics card will happily do Z ordering.

I do wish that SFML would add Z values as an option. Something like if two Z values are 0 or not set then it goes by drawing order, otherwise it passes the Z values to OpenGL for the graphics card to take care of.

Remerber Manually setting Z-order in feature requests from a while back?

Imbue

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Optimal way to do z-sorting
« Reply #3 on: December 10, 2008, 07:27:35 pm »
To answer you actual question better, here's an idea to consider.

Make a class that inherits from sf::Drawable and adds Z value functions (e.g. GetZ() and SetZ()). Every frame create an empty std::set. Each frame, when you want to render an object, just insert it's pointer into this set (you'll need to define a less than operator for the pointers based on the Z value). The set should fairly efficiently sort each object (log n time, I believe). At the end of the frame iterate through the set and render each object for real.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Optimal way to do z-sorting
« Reply #4 on: December 10, 2008, 11:38:04 pm »
SFML can't rely on Z-buffer being available because it might be unsupported by the graphics card, or even disabled explicitely by the user when creating the SFML window.
Laurent Gomila - SFML developer

 

anything