9 May 2012 by Ian Davis
Another area I worked on this week was vertex culling to improve performance. In an earlier post I wrote about optimising the rendering by batching up and caching the calls to OpenGL drawing primitives in preparation for moving to vertex buffers. However I was still drawing all the vertices within a certain radius of the player (the view radius).An obvious optimisation is not to draw vertices that are off the screen. To do this I had to calculate the edges of the screen and project them into the orientation of the game world then calculate the distance of each vertex from an edge, dropping any that are on the wrong side. I also dropped any vertices for faces that are out of sight behind blocks (I was already dropping ones that are out of sight because they are covered by blocks, e.g. underground). The results are pretty good. the following video shows some of the results. Look at the first line of the debug display at the bottom of the screen. FPS means frames per second, V shows the number of vertices being rendered with the number being dropped in brackets after.
A plain landscape at default zoom and window size now shows FPS of 40-45, compared with the 30 I was getting before. You can see I’m only drawing 13512 vertices and dropping 84440 that were off screen or rear facing. As i rotate the number of vertices on screen changes because of the trees. Then zoom out which brings even more vertices on screen with a corresponding drop in FPS. When I zoom right in, the number of drawn vertices drops under 2000 and the FPS shoots up to 60 (rate limited by the vsync on my GPU). Lastly I zoom right out and expand the view range to include more blocks onscreen. The FPS drops down to about 10, drawing about 140K vertices and dropping another 228K. Good results, and still room for improvement as I optimise the code even more.