Pixel Perfect in OpenGL

Started by
5 comments, last by Yitzak 18 years, 5 months ago
I'm writting a 2D game that involves lots of rotating things, so I'm trying to use OpenGL. My problem is that the tiles on the tile map sometimes put their rightmost column of pixels on the left side and vice versa (I haven't looked for top/bottom wrapping). The window is scaled so that each pixel is one unit. Each tile is a separate quad with a texture on it. The tiles do not overlap at all. Any ideas on how to fix this? The best suggestion I could find on the internet was to add a small translation to the modelview matrix. Is there a better way of making a tile map in OpenGL?
Advertisement
Sounds like your texture filtering is going past the edge. Try changing the filtering mode to GL_CLAMP_TO_EDGE:

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 was probably what was happening as your suggestion fixed my problem. Is there a way to get rid of the lines that now appear around the edges of tiles? (I used GL_CLAMP, as GL_CLAMP_TO_EDGE didn't exist). I'm also calling:
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

Would it have something to do with: glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, ????); If so, how do I set the RGBA of the last parameter so that it disappears? (MSDN says it defaults 0,0,0,0).
Make sure you're setting Ortho projection something like this:

  GLint viewport[4];  float width;  float height;  glGetIntegerv(GL_VIEWPORT, viewport);  width =  float((viewport[2]-viewport[0]))-0.375f;  height = float((viewport[3]-viewport[1]))-0.375f;  glPushAttrib(GL_ALL_ATTRIB_BITS);  glDisable(GL_TEXTURE_2D);  glMatrixMode(GL_PROJECTION);  glPushMatrix();  glLoadIdentity();  glOrtho(-0.375f,width,height,-0.375f, 0.0f, 1.0f); // This is the important line  glMatrixMode(GL_MODELVIEW);  glPushMatrix();    glLoadIdentity();


I dont remember exactly on which page, but on the red book its documented that you have to offset your screen dimensions by -0.375f in order to get pixel perfect coordinates (IE: glVertex2d(100,100) would draw a point at exactly 100,100 in the screen).

Hope that helps.
Quote:Original post by Yitzak
Is there a way to get rid of the lines that now appear around the edges of tiles? (I used GL_CLAMP, as GL_CLAMP_TO_EDGE didn't exist). I'm also calling:

CLAMP clamps the colour to the teture's border colour (which is a single colour for the whole image, set at texture creation). CLAMP_TO_EDGE is different, and clamps the texture coordinates to the edge of the texture data. Switch to CLAMP_TO_EDGE and you won't get the borders.

However CLAMP_TO_EDGE isn't in GL 1.1, (IIRC it's in 1.2). Have a look in the forum faq for using OpenGL past 1.1 on windows (in this case I think you just need an additional header, as you're not using any extra function calls).

Ideally you should change the filtering to NEAREST, but in practice this isn't actually needed I find.

Edit: Ooops, not this forums faq, but the OpenGL forum's faq
OrangyTang: Are those your fluffy slippers? :)

I tried to find the link to the actual fact (spent about 60 seconds) but I couldn't locate it. Sorry.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

You are most truly correct. Problem fixed. Many thanks.

This topic is closed to new replies.

Advertisement