Tiling a texture in LibGDX

Hi all, I’m attempting to tile a texture in LibGDX with GL_REPEAT but when I render this, it stretches the image instead of repeating:


import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.Texture.TextureWrap;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class MyApp extends Game implements ApplicationListener {
	public static final String LOG = "LOGIT";

	protected Texture background;
	protected SpriteBatch batch;

	@Override
	public void create() {
		batch = new SpriteBatch();
		background = ImageLoader.loadTexture("groundtile.png");
		background.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);

		setScreen(new IntroScreen());
	}

	@Override
	public void render() {
		float w = Gdx.graphics.getWidth();
		float h = Gdx.graphics.getHeight();
		batch.begin();
		batch.draw(background, 0, 0, w, h, 0, 0, w/128.0f, h/128.0f);
		batch.end();
		super.render();
	}
}


I believe I do have the UV coordinates right in batch.draw(…) so what could be wrong?

Thanks in advance

For now im just going to use this code, but if anyone knows how to do it with GL_REPEAT, please respond :slight_smile:


float w = Gdx.graphics.getWidth();
		float h = Gdx.graphics.getHeight();
		batch.begin();
		int ws = (int)(w/128.0+0.5);
		int hs = (int)(h/128.0+0.5);
		for (int i = 0; i < ws; i++) {
			for (int j = 0; j < hs; j++) {
				batch.draw(background, i*128, j*128);
			}
		}
		batch.end();

See my reply here:
http://badlogicgames.com/forum/viewtopic.php?p=38778#p38778

Usually your method of using a for loop is just as good, if not better, since it allows you to include the tile in the same draw call as the rest of your game (assuming you use sprite sheets).

Ahh I see.
Well i’ll probably just stick to my for loop then, thanks!