iDevGames Forums
2D Drawing Problem... - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: 2D Drawing Problem... (/thread-6576.html)



2D Drawing Problem... - Blake - Jan 4, 2004 04:15 AM

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?


2D Drawing Problem... - AnotherJake - Jan 4, 2004 04:26 AM

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.


2D Drawing Problem... - OneSadCookie - Jan 4, 2004 04:35 AM

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.


2D Drawing Problem... - AnotherJake - Jan 4, 2004 05:11 AM

I feel oddly insulted...


2D Drawing Problem... - JeroMiya - Jan 4, 2004 01:29 PM

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.


2D Drawing Problem... - Justin Brimm - Jan 4, 2004 06:25 PM

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.


2D Drawing Problem... - codemattic - Jan 4, 2004 08:52 PM

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).


2D Drawing Problem... - Entropy - Jan 5, 2004 01:15 AM

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


2D Drawing Problem... - Blake - Jan 5, 2004 02:08 AM

thanks for all the help guys but after rethinking the animation process ive decided to go 3d Wink