android & libgdx - disable blurry rendering
Asked Answered
P

3

6

i'm trying out libgdx as an opengl wrapper , and i have some issues with its graphical rendering : for some reason , all images (textures) on android device look a little blurred using libgdx . this also includes text (font) .

for text , i thought that it's because i use bitmap-fonts , but i can't find an alternative- i've found out that there is a library called "gdx-stb-truetype" , but i can't find how to download it and use it .

for normal images , even when i show the entire image without any scaling , i expect it to look as sharp as i see it on a computer's screen , especially if i have such a good screen on the device (it's galaxy nexus) . i've tried to set the anti-aliasing off , by using the next code :

final AndroidApplicationConfiguration androidApplicationConfiguration=new AndroidApplicationConfiguration();
androidApplicationConfiguration.numSamples=0; //tried the value of 1 too.
...

i've also tried to set the scaling method to various methods , but with no luck. example:

texture.setFilter(TextureFilter.Nearest,TextureFilter.Nearest);

as a test , i've found a sharp image that is exactly the same as the seen resolution on the device (720x1184 for galaxy nexus , because of the buttons bar) , and i've put it to be on the background of the libgdx app . of course , i had to add extra blank space in order for the texute to be loaded , so the final size of the image (which will include content and empty space) is still a power of 2 for both width and height (1024x2048 in this case) . on the desktop app , it look ok . on the device , it looked blurred.

a weird thing that i've noticed is that when i change the device's orientation (horizontal <=> vertical) , for the very short time before the rotating animation starts , i see both the image and the text very well .

surely libgdx can handle this , since the opengl part of the api-tests project of android shows images just fine.

can anyone please help me?

@user1130529 : i do use spritebatch . also , here's what i do for setting the viewport . it occurs whether i choose to keep the aspect ratio or not.

public static final int          VIRTUAL_WIDTH         =720;
public static final int          VIRTUAL_HEIGHT        =1280-96;
private static final float       ASPECT_RATIO          =(float)VIRTUAL_WIDTH/(float)VIRTUAL_HEIGHT;
...
@Override
public void resize(final int width,final int height)
  {
  // calculate new viewport
  if(!KEEP_ASPECT_RATIO)
    {
    _viewport=new Rectangle(0,0,Gdx.app.getGraphics().getWidth(),Gdx.app.getGraphics().getHeight());
    Gdx.app.log("DEBUG","size:"+_viewport);
    return;
    }
  final float currentAspectRatio=(float)width/(float)height;
  float scale=1f;
  final Vector2 crop=new Vector2(0f,0f);
  if(currentAspectRatio>ASPECT_RATIO)
    {
    scale=(float)height/(float)VIRTUAL_HEIGHT;
    crop.x=(width-VIRTUAL_WIDTH*scale)/2f;
    }
  else if(currentAspectRatio<ASPECT_RATIO)
    {
    scale=(float)width/(float)VIRTUAL_WIDTH;
    crop.y=(height-VIRTUAL_HEIGHT*scale)/2f;
    }
  else scale=(float)width/(float)VIRTUAL_WIDTH;
  final float w=VIRTUAL_WIDTH*scale;
  final float h=VIRTUAL_HEIGHT*scale;
  _viewport=new Rectangle(crop.x,crop.y,w,h);
  Gdx.app.log("DEBUG","viewport:"+_viewport+" originalSize:"+VIRTUAL_WIDTH+","+VIRTUAL_HEIGHT+" aspectRatio:"+ASPECT_RATIO+" currentAspectRatio:"+currentAspectRatio);
  }
Pruter answered 23/3, 2012 at 23:26 Comment(5)
Are you sure you need to be subtracting the 96 pixels? ie, does the reported width/height by the GDX class return 1280x720 or does it return 1184x720 and 1280x624 respectively?Douglassdougy
@tencent : i did it since the real resolution on galaxy nexus is not 1280 - the system bar takes 96 pixels and cannot be hidden while the user touches it.Pruter
:nod: I assumed that is why you did it. I just find it strange that it would report pixels you can't use. If they ever change the size of the bar it could totally botch your manual 96 entry unless in 3.0 they added a way to get the parameter? I would be extremely reluctant to hard code a value like that.Douglassdougy
this doesn't matter now . the issue is different - the images&fonts become blurry no matter what target resolution i use and no matter what size of images i use (even images that are the same size of the target resolution).Pruter
I believe this outlines your same issue for a different problem. #9199432Douglassdougy
H
2

Try this:

TextureRegion.getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
Hoebart answered 30/4, 2012 at 5:40 Comment(2)
wish that would work , but i've tried it too.it makes the images blurry as well. you can try it out - find a clear image on the internet that its content size is exactly the one of your device resolution . show the image on the app and capture the image.compare the original image and the one on the device.they look different . i've also tried TextureFilter.Nearest ,but that looks pixeletted .Pruter
Doesn't work. While it doesn't hurt anything, it also does nothing.Marrin
T
1

Try the following:

texture.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);

There are several types of TextureFilters. I assume that the Linear one (is that default?) is blurring.

Trine answered 15/12, 2013 at 21:5 Comment(0)
S
0

If you have the Chainfire 3D application or another which reduce textures or change it to 16bit, turn it off; that works for me, and I had the same problem.

Sky answered 3/6, 2013 at 19:32 Comment(1)
this post is quite old, and i don't think i've used what you are talking about.Pruter

© 2022 - 2024 — McMap. All rights reserved.