libgdx - GroupRraw中的ShapeRenderer呈现错误的颜色 [英] libgdx - ShapeRenderer in Group.draw renders in wrong colour
问题描述
MyGroup ::
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();
}
}
主 p>
public class GameControl实现ApplicationListener {
private Stage stage;
private GShape gShape;
@Override
public void create(){
stage = new Stage(480,320,false);
纹理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
void pause(){
}
@Override
public void resume(){
}
}
形状颜色不是白色吗?为什么?
你可能得到不一致的结果,因为你混合 SpriteBatch
和 ShapeRenderer
上下文。这两个期望状态,它们在OpenGL中存储以保持在 begin()
和 end()
调用之间。
Actor
draw()
已经调用 SpriteBatch
begin()
的上下文,因此您需要在开始 ShapeRenderer
。 (您需要在返回之前重新启动 SpriteBatch
。
像这样:
@Override
public void draw(SpriteBatch batch,float parentAlpha){
super.draw(batch,parentAlpha);
batch.end(); // **结束批处理上下文
shape.begin(ShapeType.Line);
// ...绘制线条...
shape.end()
batch.begin(); // **重新启动调用者假设的SpriteBatch上下文
}
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();
}
}
Main:
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() {
}
}
Colour of shape is not white? Why?
You are probably getting inconsistent results because you're mixing SpriteBatch
and ShapeRenderer
contexts. Both of these expect state they "store" in OpenGL to be maintained between begin()
and end()
calls.
The Actor
draw()
method is called in a context where the SpriteBatch
begin()
has already been called, so you need to end it before beginning your ShapeRenderer
. (And you need to restart the SpriteBatch
before returning.
Like this:
@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
}
这篇关于libgdx - GroupRraw中的ShapeRenderer呈现错误的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!