2D Drawing Problem...

Blake
Unregistered
 
Post: #1
hey all, I am currently working on a 2 RTS and have come across a small problem, when i run the game it is drawing the objects in the order they were created, and this is all fine till they start overlapping and the objects that are higher (drawn higher up on the screeen ) are being drawn over the objects below them ( which isnt very pretty ), now is there any way in OpenGL to fix this or am i going to have to sort the objects according to their y positions and draw it like that?
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #2
Not sure I totally understand the question, but you can turn on depth testing and assign a depth value (z coordinate) to your objects. It's usually (almost always) faster to sort them yourself though.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #3
AnotherJake Wrote:It's usually (almost always) faster to sort them yourself though.

Eh? What planet are you on?

OpenGL's depth testing is very fast on any card newer than the Rage 128. You should sort front-to-back and enable depth testing, because it will be orders of magnitude faster than sorting back-to-front and drawing without depth test.
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #4
I feel oddly insulted...
Quote this message in a reply
Member
Posts: 45
Joined: 2006.11
Post: #5
Well, sorting front to back takes just as long as sorting back to front. However, I believe what his real problem is that he's using blending to mask 2D objects. In this case, he MUST sort from back to front to draw things correctly. Unless you have like a million little 2D objects on your screen or huge 512x512 sprites moving around, don't worry about drawing speed with a 2D engine using OpenGL until you actually notice a speed issue.
Quote this message in a reply
Member
Posts: 64
Joined: 2005.06
Post: #6
Quote:don't worry about drawing speed with a 2D engine using OpenGL until you actually notice a speed issue.

That's a bad way of going about things. If you can foresee a potential problem, it is far better to tackle it before it becomes a real problem. Putting off fixing it, only makes it harder to fix.
Quote this message in a reply
Member
Posts: 304
Joined: 2002.04
Post: #7
Sorry Justin, but I agree with JeroMiya. Thats a *good* way of doing things. Get it working first. Profile second. Optimize third.

Maybe you could optimize a section of code - but it turns out its fast enough unoptimized. Or after profiling you realize the cpu spends even *more* time in a different section of code - and its best to spend your energies optimizing that first. And finally - I feel often programmers get bogged down in optimizing some algorithm - but run out of gas before they finish their project (I know Ive done that many times).
Quote this message in a reply
Entropy
Unregistered
 
Post: #8
Im working on a qbert clone, and encountered almost exactly the same problem. What I did was add a z-order data member to my sprite class. When the sprite is created, or I need to, I modified the sprite z-order value. Then, in order to organize drawing of sprites, I add them to a std::vector in my sprite engine (which renders them in order). After my sprites are added to the engine, I use std::stable_sort() on my vector of sprites, using a custom function that returns the lesser z-value of two sprites. In this way, sprites are drawn as intended; lower z-order first.

Code:
class spriteZOrderCmp_lt: public std::binary_function<CM_Sprite&, CM_Sprite*&, bool>
{
public:
    spriteZOrderCmp_lt() {}
    bool operator()(CM_Sprite* const s1, CM_Sprite* const s2) const
    {
        return ((int)s1->getZ_order() < (int)s2->getZ_order());
    }
};
and:
Code:
void orderSpritesByZ(void)
{
    std::stable_sort(m_spriteVector.begin(), m_spriteVector.end(), spriteZOrderCmp_lt());
}

I don't know if this is the way of doing things, but it works for me.
HTH
Quote this message in a reply
Blake
Unregistered
 
Post: #9
thanks for all the help guys but after rethinking the animation process ive decided to go 3d Wink
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  "stretched" drawing problem joellevin 1 2,505 Dec 21, 2006 09:19 PM
Last Post: OneSadCookie