OpenGL tiled images with glScalef creating black lines

Hey guys,

I’ve got a bunch of tiled 512x512 textures that are drawn at the logical corresponding coordinates in ortho mode:

ex. (0,0) (0,512) (512,0) (512,512) would be an example of the 4 places I’m drawing them.

When everything is at a scale of 1, there is no problem and it all looks like one seamless background. However, when I allow the user to zoom out and do so via a

glScalef(scale, scale, 1.0f);

At seemingly random scaling values, OpenGL creates a black line in between the tiles, which seems like some sort of rounding problem. My coordinates are never actually changed - instead, the glScalef adjusts everything. Does anyone have any ideas as to why OpenGL is creating these lines, and how I can get around it?

Also I’m drawing each sprite via

glDrawArrays(GL_TRIANGLE_FAN)

Thanks!

Also, I’m using GL_LINEAR, and using GL_NEAREST fixes the problem, but obviously it looks more ugly with scaling. Any way I can have the best of both worlds?

try setting the texture wrap modes to GL_CLAMP, the default is GL_REPEAT, so you might end up grapping pixels from the ‘wrong side’ of the texture

if you have 1 texture with a lot of ‘images’ with black space between them, it gets pretty obvious why you see black lines. either ‘scale’ your texcoords half a texel, and reduce the dimension by one texel, or add a textureborder (just fill the edges of your images in the texture with the nearest pixel in the image. (N.B. not opengl texture borders!)

Are you 100% sure that you have a manifold mesh? It’s very easy to use optimisations such as pulling out a multiplication in a loop to instead use an accumulator and without realising it to get co-ords which are different by a few ulp.

Are you using depth testing when rendering the tiles? It might be z-fighting, but I kinda doubt it since the rendering works when using GL_NEAREST.

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

That solved it, thanks for pointing me in the right direction, Riven, and thanks for the other suggestions, guys.

Basically what was happening was that it was using the linear averaging combined with a wrap to be testing the other side of the texture on certain scales. So actually sometimes it would be black (for edges of a tile that had one side as the edge of the whole image) and sometimes it would be weirdly colored (for edges of a tile that had one side adjacent to another tile). But this solved the issue so happy day.