Page 1 of 1
2d Map Culling OpenGL/SDL
Posted: Tue Aug 31, 2010 12:17 am
by jjackdev
Hello,
I am working on a game with a 2d scrolling tilemap. I am currently finding ways to optimize it. I found a couple but one that I can never get to work in 2d map culling. Is there a way someone can point me or show me how I could implement this with SDL and OpenGL.
Thanks
Re: 2d Map Culling OpenGL/SDL
Posted: Tue Aug 31, 2010 12:52 am
by Ginto8
"Premature optimization is the root of all evil." For modern graphics hardware, drawing a 2D tilemap is about as labor-intensive as it is for you to lift a 3-pound weight. Not only is there no need to worry about culling backfaces, there are no optimizations that you need. If you really need to do some optimization, look into vertex arrays. Other than that, any optimizations you do will have minimal effect (maybe a millisecond or two less draw time).
Re: 2d Map Culling OpenGL/SDL
Posted: Tue Aug 31, 2010 2:20 am
by WSPSNIPER
agreed. but what you could do is in the map rendering function check to see if the tile is on the screen before you render it.
Re: 2d Map Culling OpenGL/SDL
Posted: Tue Aug 31, 2010 6:28 am
by GroundUpEngine
Ginto8 wrote:If you really need to do some optimization, look into vertex arrays. Other than that, any optimizations you do will have minimal effect (maybe a millisecond or two less draw time).
Very good advice.
Re: 2d Map Culling OpenGL/SDL
Posted: Tue Aug 31, 2010 8:23 am
by Lord Pingas
Re: 2d Map Culling OpenGL/SDL
Posted: Thu Sep 02, 2010 9:17 am
by dandymcgee
WSPSNIPER wrote:agreed. but what you could do is in the map rendering function check to see if the tile is on the screen before you render it.
This is what culling means, and contrary to Ginto's advice I think that culling off-screen tiles is a logical and completely necessary optimization. If your world is 4000x3000 tiles big and you can only ever see 20x15 on the screen at once, that's a shit ton of render time being wasted on drawing.. well nothing.
To expand slightly on what WSPSNIPER said, you need to check the coords of a tile against the camera's location and if tile overlaps the camera rectangle render it, otherwise don't. Depending on how you represent the map, you can probably hack together an algorithm to only call the render function of the visible tiles rather than calling render on every one and then checking whether or not it's visible.
Check the link Lord Pingas posted.
Re: 2d Map Culling OpenGL/SDL
Posted: Fri Sep 03, 2010 5:55 am
by MrDeathNote
dandymcgee wrote:WSPSNIPER wrote:agreed. but what you could do is in the map rendering function check to see if the tile is on the screen before you render it.
This is what culling means, and contrary to Ginto's advice I think that culling off-screen tiles is a logical and completely necessary optimization. If your world is 4000x3000 tiles big and you can only ever see 20x15 on the screen at once, that's a shit ton of render time being wasted on drawing.. well nothing.
To expand slightly on what WSPSNIPER said, you need to check the coords of a tile against the camera's location and if tile overlaps the camera rectangle render it, otherwise don't. Depending on how you represent the map, you can probably hack together an algorithm to only call the render function of the visible tiles rather than calling render on every one and then checking whether or not it's visible.
Check the link Lord Pingas posted.
I would have to agree with you man, good advice. You can just alter the limits of your rendering loop to draw only on screen tiles.