Android opengl-es fast way to render text
I want to show some text in opengl ES. I have a 512*512 font texture (texture atlas), all letter is 32*32 pixel here.
My text length is about 400 char.
My algorithm
opengl.setClearTransparentBGEnabled();
float y2=0;
float j =0;
for (int i=0; i<text.length(); i++) {
int ch =(int)text.charAt(i);
float x2=((float)j*16*scale/50);
j++;
if ((text.charAt(i)+"").equals("\n")) {
y2+=(16*scale*2)/50;
j=0;
x2=0;
}
opengl.saveMatrix();
Sprites.selectVertex("font"+name)
.setSprite(ch)
.translate(x-x2, y+y2, -9)
.scale(scale, scale, scale)
.rotate(90, 0, 0, 1)
.draw(true开发者_如何学C);
opengl.loadMatrix();
}
opengl.setClearTransparentBGDisabled();
My only probleme, this method is very slow: after this i get 15-20 FPS.
What is the best way, to render texts in opengl-es dynamically?
That's far too much work to be doing per-frame.
I'd use the 2D APIs to Canvas.drawText()
(or drawBitmap
, if you're not using a real font) the 400 chars to a private Bitmap
, and use that as my texture.
You are repeating a lot of work every frame and for every character in the text. You should calculate all of the vertex and triangle data for a given string, and then submit it to opengl in one batch. Reuse the data for as long as the string stays the same.
精彩评论