Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libgdx texture filters and mipmap

When I try to use mipmap filtering in LibGDX, none of the images appear.

I'm new to LibGDX, and I have a simple 2d scene with three rotating, scaled circles. In order to anti-alias them, I wanted to use linear filtering. For advice, I looked to this article,which said that, for heavily scaled images, a mipmap can be used to improve speed or quality.

The first unexpected appearance was that, despite the fact that all of my images were scaled down, I would only see a linear filter if the magFilter was linear. In other words:

This code will show a linear filter for minified images:

parentTexture.setFilter(TextureFilter.Nearest, TextureFilter.Linear);

whlie this code will not:

parentTexture.setFilter(TextureFilter.Linear, TextureFilter.Nearest);

which seems opposite to the libGDX function:

void com.badlogic.gdx.graphics.Texture.setFilter(TextureFilter minFilter, TextureFilter magFilter)

This would not bother me, except that it indicates that either libgdx is wrong (unlikely), the article is wrong (unlikely), or I don't understand texture filters. The latter seems especially likely when I try mipmap filters.

This code causes nothing to display

parentTexture.setFilter(TextureFilter.MipMapLinearLinear, TextureFilter.Linear);

This code displays, but with nearest filtering

parentTexture.setFilter(TextureFilter.Linear, TextureFilter.MipMapLinearLinear);

Any explanation of where I'm wrong would be greatly appreciated. I have searched elsewhere, but texture filters in libGDX is pretty specific, so aside from the article, I haven't found much to help.

like image 726
Kenkron Avatar asked Mar 13 '13 06:03

Kenkron


3 Answers

I had this same problem, and the fix turned out to be insanely simple. When you create a Texture, you need to specify that it uses mipmaps.

All you have to do is pass a second parameter to the Texture constructor like this:

Texture myTexture = new Texture(Gdx.files.internal("myImage.png"), true);

You can view all the Texture class constructors in the API docs here: http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/Texture.html

like image 106
twiz Avatar answered Nov 16 '22 05:11

twiz


Just like Mitesh said in his answer mipmaps filters doesn't work because you are not telling Libgdx to generate mipmaps.

if you are using assets manager the code will be something like this

 TextureParameter param = new TextureParameter();
 param.genMipMaps = true; // enabling mipmaps

 manager.load("path/to/texfile.png", Texture.class, param);

 Texture tex = manager.get("path/to/texfile.png", Texture.class);
 tex.setFilter(TextureFilter.MipMap, TextureFilter.Nearest);
like image 32
Ashraf Saleh Avatar answered Nov 16 '22 06:11

Ashraf Saleh


There can be multiple issues with your image:

  1. It should be power of 2, if you are using an image with size like 354X420, it won't work. In this case you need to take an image of 512X512 or any other power of 2.

  2. When you want to enable Mipmap filtering, then you need to enable it using boolean genMipMaps which tells libgdx whether to generate mapmaps.

like image 1
Mitesh Sharma Avatar answered Nov 16 '22 04:11

Mitesh Sharma