Not so long ago I had a problem with displaying a texture as I was using edges of texels as texture coordinates instead of centers of texels. This, in combination with linear blending gave a smooth result of colors instead of pure colors given by a texture.
Thanks to advices given me here, I thought my problem is solved as I started to use centers of texels as coordinates, but it only worked in one case and I have no idea why only in that one case.
Again I'll show some pictures.
This is a texture I'm using (32x32 pixels) with border of 2 pixels:
My whole object that is being drawn consists of 9 quads, but they use only one texture (the one I showed). One of them will serve as an explanation to my problem:
I think it is well visible that colors of that quad aren't properly set according to the texture.
It looks like a problem with blending, but for setting texture coordinates I have used texels' centers, like this:
glBegin(GL_QUADS);
// Bottom left
glTexCoord2f(0.0 + texelCentersOffset, maxTexCoordBorderY + texelCentersOffset);
glVertex2i(pos.x, pos.y + height - m_borderWidth);
// Top left
glTexCoord2f(0.0 + texelCentersOffset, (GLfloat)1.0 - maxTexCoordBorderY - texelCentersOffset);
glVertex2i(pos.x, pos.y + m_borderWidth);
// Top right
glTexCoord2f(maxTexCoordBorderX - texelCentersOffset, (GLfloat)1.0 - maxTexCoordBorderY - texelCentersOffset);
glVertex2i(pos.x + m_borderWidth, pos.y + m_borderWidth);
// Bottom right
glTexCoord2f(maxTexCoordBorderX - texelCentersOffset, maxTexCoordBorderY + texelCentersOffset);
glVertex2i(pos.x + m_borderWidth, pos.y + height - m_borderWidth);
glEnd();
For clarification:
m_borderWidth
= 2
maxTexCoordBorderX
= maxTexCoordBorderY
= 2/32 = 0.0625
texelCentersOffset
= 1/64 = 0.015625
Could anyone tell me what is wrong in what I've wrote?
EDIT1:
Here are settings for my texture:
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_NEAREST );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
GL_REPEAT );
gluBuild2DMipmaps( GL_TEXTURE_2D, 4, m_width, m_height,
GL_RGBA, GL_UNSIGNED_BYTE, pixmap );
When I change from GL_LINEAR to GL_NEAREST, it works properly so it's obviously a problem with blending, but still can't figure out why it is wrong.
EDIT2:
When I increase height of quad to be the same as or larger than texture's height, it works correctly without changing any texture coordinates.