Box2D b2Draw Renderer OpenGL 3 single draw call (FAST!)

Post your tutorials and samples here.
Predator106
Posts: 3
Joined: Mon Apr 08, 2013 11:38 pm

Box2D b2Draw Renderer OpenGL 3 single draw call (FAST!)

Postby Predator106 » Mon Apr 08, 2013 11:56 pm

Well I'm posting this hoping that other people can benefit from my laboring. The game I'm working on is open source and is written in OpenGL 3.3 programmable pipeline (no deprecated calls what-so-ever).

What makes my solution very special is that it uses *only* 1 draw call for each primitive type drawn. So if you're drawing only Solid Polygons, then regardless of how many shapes, it is only 1 draw call.

Basically my grievance with this previously was that all the code I could find everywhere, used O(n) draw calls. So for 2,000 polygons you've got 2,000 draw calls *just for solid polygons*. That isn't even including AABB drawing, or translation handles being drawn. As you can imagine, it absolutely murders the FPS by doing this.

Sure, it's not too big of a deal since it's used for debugging only..but since I'm going to be spending most of my time debugging and need to use this to see what my game is doing, it absolutely *needed* to be optimized. Especially since it's for a tile based physics game, so there will be loads of static bodies. And a decent amount non-static ones.

As of typing only Solid Polygons are fixed to do 1 draw call. Everything else is commented out (but previously used n-draw calls). So I'm changing that as I speak.

Here's the code below, hoping people can benefit from it. Should be trivial to convert to OpenGL ES as well (and if you do, I'd be interested in those patches. Or well, any patches in general, to improve this).

It's licensed GPL2+, though if that's a problem you could contact me.

The classes like Shader and Camera are just simple wrappers around glsl shaders and glm matrices, respectively. So it's not too difficult to separate that out. (and in fact, you could easily just take those as well, the code is there and not dependent on much else for those classes, either.)

https://github.com/sreich/ore-infinium/ ... nderer.cpp

Return to “Tutorials and Samples”



Who is online

Users browsing this forum: No registered users and 1 guest