Sign in to follow this  

OpenGL 2D Tilemap with OGL

Recommended Posts

It might be that this is already covered somewhere in this forum, but my luck with forum search engine wasnt high enough to get results I needed. I want to draw simple 2D Tilemap (top down, non-isometric) with 3D API. For this purpose I use OpenGL and draw tiles as textured quads (GL_QUADS). My problem is that tiled map has glitches while scrolling. It may "jump" if GL_NEAREST mode is used or "blink borders" if GL_LINEAR applied. I have experimented with GL_CLAMP/GL_REPEAT, with setting or not setting the border in glTexImage2D, with different approaches, but still no luck. How do you deal with this problem when you want smooth, glitchless scrolling of your tilemap?

Share this post

Link to post
Share on other sites
Thank you, I tried GL_CLAMP_TO_EDGE, but it wouldnt solve the glitches completely. I am still stuck with the problem, though its better with GL_CLAMP_TO_EDGE.

I realized I could produce tiles with repeating pattern (repeating rows and columns of pixels at the edges), but it would work only for areas covered with one type of tile only. Which is most unlikely in final game.

Please, add your comment here if you have successfully dealt with this issue.

Share this post

Link to post
Share on other sites
How are you organizing your tiles? One per texture, or in tile sets?
Are you drawing your tiles at 1:1 (i e a 64x64 tile becomes 64x64 on the screen) or with scaling? If scaling, does it become smaller or larger?

Assuming you're drawing your tiles one per texture, then CLAMP_TO_EDGE really should do it, even if you're scaling.

If you use a tile set, then you should get 1:1 pixels to texels; scaling will cause bleeding (and you'll have to pad the tileset itself with additional pixels around the edges). Generate the texture coordinates for a tile as such:

Tile Xt,Yt (with 0 == left and 0 == bottom, for a TGA loaded into GL); total of Wt tiles across and Ht tiles up/down (typically, 8/8 or 16/16).

uleft = Xt/Wt
vbottom = Yt/Ht
uright = (Xt+1)/Wt
vtop = (Yt+1)/Ht

Use glOrtho2D() to set the screen into 1:1 match between coordinates and pixels, with 0,0 in the lower-left corner. Then draw a tile at pos Xp,Yp as such:

glTexCoord2f( uleft, vbottom ); glVertex2f( Xp*Wt, Yp*Ht );
glTexCoord2f( uright, vbottom ); glVertex2f( (Xp+1)*Wt, Yp*Ht );
glTexCoord2f( uright, vtop ); glVertex2f( (Xp+1)*Wt, (Yp+1)*Ht );
glTexCoord2f( uleft, vtop ); glVertex2f( Xp*Wt, (Yp+1)*Ht );

That should generate a totally solid set of tiles and pixels. With these settings, filtering or NEAREST shouldn't matter, as you have 1:1 between pixels and texels. You can scroll the entire board by using glTranslatef() on whole pixel coordinates; intermediate texture coordinates will cause filtering, which will require border padding around each tile in the tile set.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this