使用PolygonSpriteBatch在Libgdx中使用重复纹理渲染多边形

时间:2013-09-26 08:03:35

标签: opengl textures libgdx

我想绘制一个具有重复纹理的多边形(例如砖块)。这是我的代码:

textureBrick = new Texture(Gdx.files.internal("data/brick.png"));
textureBrick.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);


TextureRegion texreg = new TextureRegion(textureBrick,0,0,1f,1f);
texreg.setTexture(textureBrick);
PolygonRegion po = new PolygonRegion(texreg, floatvertices);

然后我画画(渲染):

public void render(SpriteBatch spriteBatch, PolygonSpriteBatch polygonBatch) {
    Gdx.gl.glEnable(GL10.GL_TEXTURE_2D);
    polygonBatch.draw(po, 0,0, 512f, 256f);
}

不幸的是,我总是得到用白色填充的多边形。为什么呢?

1 个答案:

答案 0 :(得分:3)

您可能按此顺序调用代码

spriteBatch.begin();
spriteBatch.draw(textureRegion, 0, 0, 480, 480);
polygonBatch.begin();
polygonBatch.draw(polygonRegion, 0,0, 400f, 400f);  
polygonBatch.end();
spriteBatch.end();

一起使用spriteBatch,polygonBatch,shapeRenders等可能导致这类问题,你应该单独使用它们:

spriteBatch.begin();
spriteBatch.draw(textureRegion, 0, 0, 480, 480);
spriteBatch.end();
polygonBatch.begin();
polygonBatch.draw(polygonRegion, 0,0, 400f, 400f);  
polygonBatch.end();

在使用任何其他批次的开始之前,您应该结束上一批。