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

StackOverflow https://stackoverflow.com/questions/8368791

  •  27-10-2019
  •  | 
  •  

سؤال

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. 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(). In your case, you simply have to enable texturing via glEnable(GL10.GL_TEXTURE_2D) before you render your mesh.

When in doubt check out the SpriteBatch javadocs which tell you about the GL state changes it makes.

نصائح أخرى

I ran into the same problem and my solution was to set the active texture back to GL_TEXTURE0 before letting SpriteBatch (or in my case scene2d) do its stuff:

Gdx.gl20.glActiveTexture(GL20.GL_TEXTURE0);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top