Jump to content
  • Advertisement
Sign in to follow this  
rzilibowitz

GL_NEAREST seams

This topic is 5081 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, I am creating a 2D tile map based game. I have a single texture loaded with all my tiles. I use GL_QUADS. Position texture coords for the correct tile. use glVertex2d for the screen coords. GL_LINEAR causes a problem: neighbouring tiles get factored into each other - effectively the tile map gets seams around the tiles. I thought GL_NEAREST would work perfectly. But - to my surprise this also creates seems. The behaviour is strange - I use glOrtho to scroll the tile map - when at 0,0 it looks nice - but seams appear and dissapear as it scrolls. Anyone understand why this is occuring? And, any suggestions? Thanks, Ruben

Share this post


Link to post
Share on other sites
Advertisement
Try GL_CLAMP or GL_CLAMP_TO_EDGE (latter is more likely to work). The problem has to do with both Nearest and Linear occasionally choosing the pixel from the other side of the texture map to sample from (sampling wraps around textures). Clamping will prevent that.

Share this post


Link to post
Share on other sites
No it won't, because he has many tiles in one texture. Just keep your map at integer coordinates, and if you have your viewport set up right then it will always sample from texel centers and these problems will go away.

Share this post


Link to post
Share on other sites
>>I thought GL_NEAREST would work perfectly. But - to my surprise this also creates seems.<<

youre using the incorrect texture coordinate then

Share this post


Link to post
Share on other sites
No I'm not using the wrong texture coordinates. That's the whole point of this thread. I don't know why this problem is happening.

I'll try experimenting with keeping the map fixed at integer coordinates and this might fix it... Otherwise I may have to split the tiles into seperate textures which seems terribly unnecessary and will be slower.

Share this post


Link to post
Share on other sites
Here is an excerpt from "The Red Book" Ch. 9 I had a look at:

"If you choose GL_NEAREST, the texel with coordinates nearest the center of the pixel is used for both magnification and minification. This can result in aliasing artifacts (sometimes severe)."

I'm not sure if these "aliasing artifacts" are the problem I'm having? I'm still quite sure why my problem is occuring though...

Here is the relevant snippet of code...


...

glBindTexture(GL_TEXTURE_2D, tex_id);

glBegin(GL_QUADS);
for (map_y = start_tile_y; map_y < end_tile_y; map_y++)
{
loc_y = map_y*tileHeight + bounds.bottom;

for (map_x = start_tile_x; map_x < end_tile_x; map_x++)
{
loc_x = map_x*tileWidth + bounds.left;

if(map_y >= 0 && map_y < mapHeight && map_x >= 0 && map_x < mapWidth)
tile = map[map_y*mapWidth + map_x];
else
tile = -1;

if (tile >= 0)
drawTile(loc_x, loc_y, tile);
}
}
glEnd();
}

//
// drawTile - Draws a single tile at a given location
//
void ScTilemap::drawTile(ScFloat x, ScFloat y, int tile)
{
static ScFloat tx1;
static ScFloat ty1;
static ScFloat tx2;
static ScFloat ty2;
static ScFloat x1;
static ScFloat y1;
static ScFloat x2;
static ScFloat y2;

tx1 = (tile % paletteWidth) * invPaletteWidth;
ty1 = (tile / paletteWidth) * invPaletteHeight;
tx2 = tx1 + (tileWidth * invTextureWidth);
ty2 = ty1 + (tileHeight * invTextureHeight);
x1 = x;
y1 = y;
x2 = x + tileWidth;
y2 = y + tileHeight;

glTexCoord2d(tx1, ty1); glVertex2d(x1, y1);
glTexCoord2d(tx2, ty1); glVertex2d(x2, y1);
glTexCoord2d(tx2, ty2); glVertex2d(x2, y2);
glTexCoord2d(tx1, ty2); glVertex2d(x1, y2);
}




Thanks,

Ruben

[Edited by - rzilibowitz on December 16, 2004 2:58:30 AM]

Share this post


Link to post
Share on other sites
no thats something else (u see it when u zoom in out of an image) its the reason for mipmapping,
try converting everything to floats
eg tile % paletteWidth now these to numbers are integers so cast the result to a float perhaps

Share this post


Link to post
Share on other sites
Well, I have made a big improvement by using glVertex2i instead of glVertex2d for drawing my tiles (see code snippet in above post). But it is still far from perfect. The seams have mostly dissapeared, although now the screen flickers (mainly top to bottom - in the order I draw my tiles) whilst scrolling. When not moving the picture looks pretty much perfect.

But, if I zoom in or out, faint seams start occuring again.

Sigh.

(edit)

As a additional point: if I use a 1 pixel border around each tile and use GL_LINEAR the problems pretty much go away. But the tradeoff is I lose 1 pixel around all my tiles. There is some screen flicker here too - why is this? But it is hardly noticeable as compared with GL_NEAREST.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!