简体   繁体   English

如何避免libgdx SpriteBatch / BitmapFont破坏Texture.bind?

[英]How can I avoid libgdx SpriteBatch/BitmapFont breaking Texture.bind?

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? 知道为什么会这样吗? What should I do to keep both materials in drawing ('texture' for mesh and its sprite texture, that should be different)? 我应该怎么做才能将两种材质保持在绘图中(网格及其精灵纹理的'纹理',应该是不同的)?

SpriteBatch#end() has to disable blending and texturing as it assumes a clean GL state context on a call to SpriteBatch#begin() and restores that clean state context on SpriteBatch#end() . SpriteBatch#end()有禁止混合和纹理,因为它假定在呼叫干净GL状态上下文SpriteBatch#begin()和还原的清洁状态上下文SpriteBatch#end() In your case, you simply have to enable texturing via glEnable(GL10.GL_TEXTURE_2D) before you render your mesh. 在您的情况下,您只需在渲染网格之前通过glEnable(GL10.GL_TEXTURE_2D)启用纹理。

When in doubt check out the SpriteBatch javadocs which tell you about the GL state changes it makes. 如有疑问,请查看SpriteBatch javadocs,它会告诉您它所做的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