使用libGDX的earclippingtriangulator和PolygonSpriteBatch绘制填充多边形

时间:2015-06-24 15:22:24

标签: java android libgdx polygon triangulation

我正在尝试使用libGDX绘制填充多边形,现在我正在尝试使用PolygonSpriteBatch执行此操作,您可以在下面看到我的代码:

public class MyGdxGame extends ApplicationAdapter implements GestureDetector.GestureListener {

Texture textureSolid;
PolygonSprite polySprite;
PolygonSpriteBatch polyBatch;
OrthographicCamera camera;
Vector2 touch;

@Override
public void create() {
    super.create();

    camera = new OrthographicCamera(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
    camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    touch = new Vector2();
    Gdx.input.setInputProcessor(new GestureDetector(this));

    polyBatch = new PolygonSpriteBatch(); // To assign at the beginning

    polyBatch.setProjectionMatrix(camera.combined);

    // Creating the color filling (but textures would work the same way)
    Pixmap pix = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
    pix.setColor(0xFF33691E); // DE is red, AD is green and BE is blue.
    pix.fill();
    textureSolid = new Texture(pix);
    TextureRegion textureRegion = new TextureRegion(textureSolid);

    float[] vertices = new float[] {10, 10, 100, 10, 200, 200, 10, 100};

    EarClippingTriangulator triangulator = new EarClippingTriangulator();
    ShortArray triangleIndices = triangulator.computeTriangles(vertices);

    PolygonRegion polyReg = new PolygonRegion(textureRegion, vertices, triangleIndices.toArray());

    polySprite = new PolygonSprite(polyReg);
}

@Override
public void resize(int width, int height) {
    super.resize(width, height);
}

@Override
public void render() {

    polyBatch.begin();
    polySprite.draw(polyBatch);
    polyBatch.end();

}

@Override
public void pause() {
}

@Override
public void resume() {
}

@Override
public void dispose() {
    polyBatch.dispose();
}

@Override
public boolean touchDown(float x, float y, int pointer, int button) {
    return false;
}

@Override
public boolean tap(float x, float y, int count, int button) {
    return false;
}

@Override
public boolean longPress(float x, float y) {
    return false;
}

@Override
public boolean fling(float velocityX, float velocityY, int button) {
    return false;
}

@Override
public boolean pan(float x, float y, float deltaX, float deltaY) {
    camera.translate(-deltaX, deltaY);
    camera.update();
    return true;
}

@Override
public boolean panStop(float x, float y, int pointer, int button) {
    return false;
}

@Override
public boolean zoom(float initialDistance, float distance) {
    return false;
}

@Override
public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {
    return false;
    }

}

当我运行代码时,我最终得到了多个多边形以及一些奇怪的东西 “背景”

enter image description here

我做错了什么?

1 个答案:

答案 0 :(得分:3)

您需要在每个帧的开头清除屏幕。重构你的渲染方法是这样的......

@Override
public void render() {

    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    polyBatch.begin();
    polySprite.draw(polyBatch);
    polyBatch.end();

}
相关问题