How to turn off OpenGL filtering when scaling up textures?

I already have these lines in here when I'm loading the texture:

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

But when I scale the image here during rendering:

glTexCoord2d(0.0,0.0);
glTexCoord2d(0.0454,0.0);
glTexCoord2d(0.0454,1.0);
glTexCoord2d(1.0,1.0);

I get really bad filtering on the texture (the texture is a spritesheet with multiple frames and I'd rather not make an individual file for each frame).

Answers


You're doing it right in terms of texture filtering. However, it's not enough to avoid all sampling artifacts, especially if you move your sprites. You must also make sure that:

  • Your sprites' size in pixels (on the screen) is an integral multiple of the their size in texels (in the texture)
  • You draw your sprites at integral coordinates in pixels (or integral-and-a-half, depending on how OpenGL positions texels in textures, I can't remember)

To illustrate the problem, say you have a 32x32 sprite (in the texture) and you map it to a 43x43-pixel quad on screen. Then the GPU only has 32 texels to fill a width of 43 pixels, so it needs to duplicate some of the texels. Exactly which texels are duplicated will depend on the coordinates of your quad on-screen (if you use non-integral coordinates). So moving sprites will appear to have weirdly flickering colors as the GPU decides to duplicate different texels to fill your quad.

To avoid this problem and achieve the best-looking sprites, you really want each texel to map to a single on-screen pixel. If you do this, then using GL_NEAREST or GL_LINEAR won't matter anymore since every pixel will only use the color from a single texel.


You're texture coordinates are pretty confusing to me because they don't form a square or rectangle (you have 3 different X values), nor are you showing the corresponding vertex coordinates (if you aren't interleaving those tex coords with vertex coords, you're just overwriting one texture coordinate over and over).

If you're trying to access a given (square) sub-region of the texture, then I would expect your texCoords to look something like this:

float x, float y; // position of the sprite in the texture
float height, float width; // size of the sprite in the texture
glBegin(GL_TRIANGLE_STRIP)
// lower left
glVertex3D(...)
glTexCoord2d(x, y);
// lower right
glVertex3D(...)
glTexCoord2d(x, y + width);
// upper left
glVertex3D(...)
glTexCoord2d(x + height, y);
// upper right
glVertex3D(...)
glTexCoord2d(x + height, y + width);
glEnd()

Need Your Help

Default constructors and inheritance in Java

java inheritance initialization default-constructor

I have a question about default constructors and inheritance in Java.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.