Archived

This topic is now archived and is closed to further replies.

Optimizing GL for simple 2d

This topic is 5652 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

I''m using GL for a windows application that I''m working on, in which most of the drawing will be simple 2d animation. Lots of circles and squares dancing around the screen, etc. I will make occasional use of textures, but I''m really interested in animating simple 2d primitives. I''m able to do this just fine, but for this application it''s critical that I don''t waste too many CPU cycles on drawing. Does anyone have any hints to really take advantage of the fact that I''m not using 3d and squeeze the extra cycles out of GL? Specific questions : 1) I already disabled depth-testing and blending... are there any other major features I need to disable? 2) Does anyone know what options I should be passing in my PIXELFORMATDESCRIPTOR for simple 2d graphics? Again, I have depth-testing and z-buffering off, and I set my colors to 16-bit instead of 24, but is there anything else I should be aware of? 3) Does anyone have recommendations about the fastest way to draw smooth circles in 2d? Is gluDisk a silly way to do this? 4) Is there any reason that GL is actually just a really bad idea for this application? Does anyone know if DirectX would be a whole lot faster? I did some performance profiling, and it seems that a lot of my time is spent swapping the buffers, which is why I ask about the PIXELFORMATDESCRIPTOR... Thanks for any ideas, or any good examples of optimized 2d applications that make use of GL. Like everyone else I''m sure, I''ve learned GL from examples that are optimized for 3d, so I want to make sure I''m not totally missing the boat here. Thanks... -Dan

Share this post


Link to post
Share on other sites
quote:
Original post by bobboberts
1) I already disabled depth-testing and blending... are there any other major features I need to disable?

Use glOrtho (gluOrtho maybe? cant remember, look it up in the msdn It makes the view more suitable for 2d.

quote:
2) Does anyone know what options I should be passing in my PIXELFORMATDESCRIPTOR for simple 2d graphics? Again, I have depth-testing and z-buffering off, and I set my colors to 16-bit instead of 24, but is there anything else I should be aware of?

You specified 0 bits for depth buffer? I don't know if its legal(it probably is); it might speed things up.

quote:
3) Does anyone have recommendations about the fastest way to draw smooth circles in 2d? Is gluDisk a silly way to do this?
I would write my own function that used GL_TRIANGLEFAN.
It would probably be faster, as you could make a sine/cosine table of only the angles that you needed (ie if you wanted 36 sections, just precalculate the sines/cosines of every 10 degrees). If you don't know what a triangle fan is, look up glBegin in the msdn.
quote:

4) Is there any reason that GL is actually just a really bad idea for this application? Does anyone know if DirectX would be a whole lot faster?

Well, DirectDraw is a 2d api, so I'm guessing it might be faster, but I've never used it, and if you haven't used it either, you might want to use ogl so you don't have to learn a new api.

quote:
I did some performance profiling, and it seems that a lot of my time is spent swapping the buffers, which is why I ask about the PIXELFORMATDESCRIPTOR...

I don't think this can be avoided, SwapBuffers just takes a lot of CPU cycles (I think, I don't know for sure).

quote:
Thanks for any ideas, or any good examples of optimized 2d applications that make use of GL. Like everyone else I'm sure, I've learned GL from examples that are optimized for 3d, so I want to make sure I'm not totally missing the boat here.

What you could do to figure this out is go thru every line in a 3d optimized ogl setup code (for example, nehe's tutorial 1) and anything that looks 3d-optimized, make it the 2d equivalent or get rid of it. That would be easier than people here telling you all of the lines to delete/change/etc.

Good luck,
James

EDIT - problem with quote tag

[edited by - neosmyle on June 23, 2002 2:05:38 AM]

Share this post


Link to post
Share on other sites
Hi Dan,

A fast way to do smooth circles would be to draw a textured quad (the texture including an alpha-channel), using glAlphaTest to clip away the shape of the circle.

If you use a fairly hi-res texture (say 256x256 or more), this should look quite good, and will have much less vertex traffic than using triangle fans.

For maximum performance, I would suggest using vertex arrays (specifying that there are only 2 components per vertex of course). Have your primitive structures/classes hold indices into these arrays so that they can update the values of their vertices during animation.

If you draw all primitives as quads (one vertex array for squares/rectangles, and another for textured-quad circles), you can batch them all off in two glDrawArrays calls (one with texture and alpha-test enabled, and one without). To allow each primitive to be a different colour, enable and use color arrays (specifying the colour for each vertex in the vertex arrays).

Hope that was a bit helpful


[edit] - something to add... I believe there may be a small speedup available in some instances if the modelview matrix is set and left as identity (as no transform from object to world to eye space is done... if the driver is smart). Therefore, calculate all your vertex positions 'absolutely' if you can, rather than transforming each primitve to its position (this approach would also be incompatible with the vertex-array approach I outlined)

[edited by - Bad Monkey on June 23, 2002 9:04:31 PM]

Share this post


Link to post
Share on other sites