I am trying to draw a polygon with a repeating texture in 2d. The texture is 128x128. The following code, when executed, displays the polygon correctly, but the texture is “stretched.” I have seen other questions like this on this site and stackoverflow, but the answers are always inconclusive.
I am almost of the mind that this was possible when libgdx supported GL10, but now that it is GL20 only, you must use a mesh, set up the UV coords separately, create and apply a shader, etc, etc. That is a lot of work for what would seem to be a common operation needed in a 2d game. I’d like to get a couple other opinions before I head down that frustrating path.
Here is the code:
public class PolygonTest {
float[] vertices = {200, 100, 200, 150, 250, 150, 250, 100, 240, 100, 240, 140, 210,
140, 210, 100};
Texture bricks;
TextureRegion textureRegion;
PolygonRegion polygonRegion;
PolygonSpriteBatch polygonSpriteBatch;
PolygonSprite polygonSprite;
public PolygonTest() {
bricks = new Texture(Gdx.files.internal("bricks.png"));
bricks.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
textureRegion = new TextureRegion(bricks);
EarClippingTriangulator a = new EarClippingTriangulator();
ShortArray sar = a.computeTriangles(vertices);
polygonRegion = new PolygonRegion(textureRegion, vertices, sar.toArray());
polygonSprite = new PolygonSprite(polygonRegion);
polygonSpriteBatch = new PolygonSpriteBatch();
}
public void render(OrthographicCamera camera) {
polygonSpriteBatch.setProjectionMatrix(camera.combined);
polygonSpriteBatch.begin();
polygonSprite.draw(polygonSpriteBatch);
polygonSpriteBatch.end();
}
}
Please note that even if I put something like:
textureRegion = new TextureRegion(bricks,0,0,512,512);
or:
textureRegion.setU(0)
textureRegion.setU2(100);
textureRegion.setV(0);
textureRegion.setV2(100);
…it makes no difference. Nor does changing any of the numbers in the above examples.