-1
\$\begingroup\$

I develop a game where I draw some textures on the screen and want to zoom in and out the scene. I am using OrthographicCamera and when I zoom in, the textures become very pixelated. Is it possible to enable anti-aliasing or is there another way to get zoomed textures without crisp edges?

enter image description here

My code is the following

public class DesktopLauncher {
    public static void main (String[] arg) {
        LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
        config.title = "Anti-aliasing";
        config.width = 600;
        config.height = 480;
        new LwjglApplication(new MyGdxGame(), config);
    }
}

class MyGdxGame extends ApplicationAdapter {
    SpriteBatch batch;
    Texture img;
    OrthographicCamera camera;
    
    @Override
    public void create () {
        batch = new SpriteBatch();
        img = new Texture("badlogic.jpg");
        camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        camera.setToOrtho(false);

        camera.zoom = 0.1f; // this makes it all to look pixelated
    }

    @Override
    public void render () {
        ScreenUtils.clear(0.1f, 0.2f, 0.3f, 0.1f);
        camera.update();
        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        batch.draw(img, 100, 100);
        batch.end();
    }
    
    @Override
    public void dispose () {
        batch.dispose();
        img.dispose();
    }
}
\$\endgroup\$
5
  • \$\begingroup\$ What would you like to see here instead? Smooth gradients like with bilinear filtering? Or sharp smooth lines like vector art? \$\endgroup\$
    – DMGregory
    Jun 4 '21 at 23:52
  • \$\begingroup\$ @DMGregory, I would like to see a blurred image like I get in any image viewer when scaling a photo. \$\endgroup\$
    – mishadr
    Jun 5 '21 at 1:10
  • \$\begingroup\$ Are you looking for How to smooth textures when they are resized in LibGDX? \$\endgroup\$
    – DMGregory
    Jun 5 '21 at 1:12
  • \$\begingroup\$ Thanks! That's what I need \$\endgroup\$
    – mishadr
    Jun 5 '21 at 10:26
  • 1
    \$\begingroup\$ If you've solved your problem, want to write up the steps of the solution as an Answer below? \$\endgroup\$
    – DMGregory
    Jun 5 '21 at 11:07
0
\$\begingroup\$

Thanks to @DMGregory, the solution was to add a texture filter after creation of the texture, e.g. Texture.TextureFilter.Linear which does bilinear interpolation.

I modified the code of create method as follows

public void create () {
    batch = new SpriteBatch();
    img = new Texture("badlogic.jpg");
    // adding bilinear filter
    img.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);

    camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    camera.setToOrtho(false);

    camera.zoom = 0.1f;
}

Now the image from the question looks like this

enter image description here

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .