21

Is there a way to implement antialiasing in LibGDX?

My lines are very pixilated.

Screenshot

ComanderKai77
  • 460
  • 6
  • 14

3 Answers3

32

To smooth any line or shape use multi sampling anti aliasing

replace:
Gdx.gl.glclear()

with:
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT | (Gdx.graphics.getBufferFormat().coverageSampling?GL20.GL_COVERAGE_BUFFER_BIT_NV:0))

and for Android, open AndroidLauncher.java file and put : config.numSamples = 2 2 is a good value

for desktopsconfig.samples=3 this will do your work...

Zumbarlal Saindane
  • 1,199
  • 11
  • 24
Ashwani
  • 1,294
  • 1
  • 11
  • 24
  • @SamanMiran nope, see Manuel's downvoted answer below for better interpolation of textures. This is only fixes the edges of textures (which i advise you to leave transparent whenever possible) – Riki137 Aug 19 '20 at 09:57
  • 1
    If you're using Lwjgl3ApplicationConfiguration, then you won't be able to access the samples field. Instead do "config.setBackBufferConfig(8,8,8,8,16,0,num_of_samples);". These values are the defaults - only set the last parameter which is the samples value. – Gad Wissberg Mar 30 '22 at 20:47
3

You can also put the filter in the texture itself

bg = new TextureRegion(new Texture(
            Gdx.files.internal("data/bg.png")));
//bg.getTexture().setFilter(GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR);
bg.getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear); 
Manuel Mejias
  • 326
  • 1
  • 8
2

Set the number of samples in your Lwjgl3ApplicationConfiguration. It defaults to 0, try something higher:

final Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
int samples = 2; // you can also play around with hiher values like 4
config.setBackBufferConfig(8, 8, 8, 8, 16, 0, samples); // 8, 8, 8, 8, 16, 0 are default values
Sebastian
  • 5,721
  • 3
  • 43
  • 69