How to turn off OpenGL filtering when scaling up textures?

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


But when I scale the image here during rendering:


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).


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
// lower left
glTexCoord2d(x, y);
// lower right
glTexCoord2d(x, y + width);
// upper left
glTexCoord2d(x + height, y);
// upper right
glTexCoord2d(x + height, y + width);

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.