Libgdx - Speed up render of many polygon regions

Hi guys, I have a question:

how can I speed up render of 1800 polygon regions, containing 6000 vertices? Currently I’m using frustum culling, and polygon regions are stored in quad tree, so I’m querying polygons from quad tree in viewbox.

I’m not fully into OpenGL, I heard something about VBO and caching objects in GPU memory, but don’t know where to start and how to use this for my purposes. Can anyone teach me?

Render loop for Textured Polygons (it’s my wrapper for PolygonRegion with AABB) looks like that:


getTexturedPolygons().execute(qTreeBox, new QuadTree.Executor<TexturedPolygon>() {
    @Override
	
	// for each TexturedPolygon in viewbox
    public void execute(double x, double y, TexturedPolygon texturedPolygon) {
        if (camera.frustum.boundsInFrustum(texturedPolygon.getAABB())) {
            polygonSpriteBatch.draw(texturedPolygon.getPolygonRegion(), 0, 0);
        }
    }
});

The result is textured terrain. Here are bounds of each triangle:

SpriteCache uses a VBO and could serve as an example. SpriteCache caches geometry in a VBO (via Mesh) for any number of textures, keeps track of the offsets and allows you to reference that geometry by an ID. When you draw, you give the ID and the geometry is drawn from the VBO, without needing to be sent to the GPU again. Similar to SpriteBatch, SpriteCache caches quads. There is no PolygonSpriteCache, but you could write one. Getting the array indexes, etc exactly right was a bit tricky. :slight_smile: PolygonSpriteCache would make a nice contribution to libgdx, but it may be easier for your scenario to use Mesh directly.

Beyond using the VBO, you’ll need to determine what geometry to cache. Usually you’d choose the geometry on screen, plus some buffer around that. Only when the camera moves far enough would you need to re-cache your geometry. You want to cache enough that re-caches don’t happen too often, but not so much that a re-cache takes too long. OrthoCachedTiledMapRenderer could serve as an example, it does this for a tile map using SpriteCache.

Thanks Nate, I just finished creating meshes and I’m using it to render my terrain. FPS didn’t changed on my PC, but on my Android phone it changed from unstable 43-52 to stable 60.
Also, I didn’t implemented any culling etc, mesh containing whole scene (which is ~5 times larger than visible area) is rendered every frame.

Thanks for your response, now I think that I understand everything.

P.S. should I divide scene into e.g. 10 meshes and do culling? Can you predict FPS gain after implementing it? I’m asking because I cut my scene to 1/5 of it’s size and FPS didn’t changed.

Your fps is capped at 60 on Android so maybe you just can’t see improvements. No idea what they might be though. OpenGL will do culling and you aren’t sending the geometry every frame, so you make OpenGL’s culling faster by caching less. There may be a bigger difference if you draw from many textures.