libgdx texture filters and mipmap

China☆狼群 提交于 2020-01-11 04:57:07

问题


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.


回答1:


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




回答2:


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



回答3:


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.




回答4:


Try using the same minFilter and maxFilter. I had a similiar problem and if I put TextureFilter.Linear, TextureFilter.Linear or both MipMap the problem is solved. Hope this helps.



来源:https://stackoverflow.com/questions/15378791/libgdx-texture-filters-and-mipmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!