Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android & libgdx - disable blurry rendering

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);
  }
like image 852
android developer Avatar asked Mar 23 '12 23:03

android developer


2 Answers

Try this:

TextureRegion.getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
like image 115
monster Avatar answered Sep 30 '22 00:09

monster


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.

like image 21
Brad Avatar answered Sep 30 '22 01:09

Brad