简体   繁体   English

为什么libgdx SpriteBatch / BitmapFont会破坏Texture.bind?

[英]Why is libgdx SpriteBatch/BitmapFont breaking Texture.bind?

I'm writing a game for Android using libgdx. 我正在使用libgdx为Android编写游戏。 Here is some code that draws a textured torus: 这是一些绘制纹理环面的代码:

Gdx.gl10.glPushMatrix();
Gdx.gl10.glTranslatef(center.x, center.y, 0);
Gdx.gl10.glRotatef(0, 0, 1, angle * 360f / (2f * (float)Math.PI));
texture.bind();
mesh.render(GL10.GL_TRIANGLE_STRIP);
Gdx.gl10.glPopMatrix();

...and here is some code that draws a bit of text: ...这里有一些代码可以绘制一些文字:

spriteBatch.begin();
spriteBatch.setColor(1, 1, 1, 1);
float fps = 1f / Gdx.graphics.getDeltaTime();
font.draw(spriteBatch, "fps: " + fps, 0, 50);
spriteBatch.end();

The first bit of code works, frame after frame, until the second bit of code runs. 第一位代码一帧一帧地工作,直到第二位代码运行。 After that, the first bit's triangle strip is rendered using only the latest glMaterial. 之后,第一位的三角形条带仅使用最新的glMaterial渲染。 Any idea why this is happening? 知道为什么会这样吗?

Update: Solved! 更新:解决了! It turns out SpriteBatch.end() calls glDisable(GL_TEXTURE_2D). 事实证明SpriteBatch.end()调用glDisable(GL_TEXTURE_2D)。 Just had to read the source... 只是阅读源...

It actually says so in the Javadocs as well. 它实际上也是在Javadocs中也是如此。 Rule of thumb: all libgdx classes connected to OpenGL ES will change the minimum amount of states and most likely won't reset things as querying GL for state is costly. 经验法则:连接到OpenGL ES的所有libgdx类都将更改最小状态量,并且很可能不会重置事物,因为查询状态的GL代价很高。

我遇到了同样的问题,我的解决方案是将活动纹理设置回GL_TEXTURE0,然后让SpriteBatch(或者在我的情况下为scene2d)做它的东西:

Gdx.gl20.glActiveTexture(GL20.GL_TEXTURE0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM