libgdx - 在Group.draw中使用ShapeRenderer渲染的颜色不正确

4

MyGroup:

public class GShape extends Group{
private ShapeRenderer shape;

public GShape() {
    super();
    shape = new ShapeRenderer();
}

@Override
public void draw(SpriteBatch batch, float parentAlpha) {
    super.draw(batch, parentAlpha);
    shape.begin(ShapeType.Line);
    Gdx.gl10.glLineWidth(5);
    shape.setColor(1, 1f, 1f, 1f);
    shape.line(0, 0, 200, 100);
    shape.end();
}
}

主要内容:

public class GameControl implements ApplicationListener {
private Stage stage;
private GShape gShape;

@Override
public void create() {
    stage = new Stage(480,320,false);

    Texture t = new Texture(Gdx.files.internal("data/the200.png"));
    Image i = new Image(t);
    stage.addActor(i);
    gShape = new GShape();
    stage.addActor(gShape);
}

@Override
public void dispose() {
}

@Override
public void render() {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    stage.draw();
//  gShape.render();
}

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

@Override
public void pause() {
}

@Override
public void resume() {
}
} 

形状的颜色不是白色?为什么? http://nw7.upanh.com/b3.s38.d3/352dd792eb77ce6df204a7af47ae1ac6_55348087.cos.jpg?rand=0.19125773780979216
1个回答

13

你可能因为混合使用了SpriteBatchShapeRenderer上下文而导致结果不一致。它们都期望在begin()end()之间维护OpenGL中的“存储”状态。

Actordraw()方法被调用时,SpriteBatchbegin()已经被调用,所以你需要在开始使用ShapeRenderer之前结束它。在返回之前还需要重新启动SpriteBatch

像这样:

@Override
public void draw(SpriteBatch batch, float parentAlpha) {
  super.draw(batch, parentAlpha);
  batch.end();   //  ** End the batch context
  shape.begin(ShapeType.Line);
  // .. draw lines ...
  shape.end()
  batch.begin(); // ** Restart SpriteBatch context that caller assumes is active
}

2
只是一个旁注,如果您在每帧调用begin()和end()很多次(100次以上),您会发现在许多安卓设备上性能会受到巨大的影响。 - Jyro117

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接