简体   繁体   中英

Draw text in iPhone openGL ES using just objective-c

A need to draw some text in openGL and then make rotations and translations over it. Need to use just objective-c. Any help?

Use Photoshop or something to create a texture file like below.

|ABCDEFGH|
|IJKLMNOP|
|QRSTU...|

sample.png

Calculate UV position of each character.

float eachCharHeight = 0.125f;
float eachCharWidth = 0.125f;
char charToDraw = 'a';
int indexOfChar = charToDraw - 65; // 'a' = 65
float uValueForCharA = (float)(indexOfChar / 8) * eachCharHeight;
float vValueForCharA = (float)(indexOfChar % 8) * eachCharWidth;

Set texcoords and draw.

glPushMatrix();
glRotatef(...);  // or translate

glEnable(GL_TEXTURE_2D);
glBindTexture(texture);

float vertices[8] = {0.0f, 0.0f, 1.0f, 0.0f, ...};
float texcoords[8] = {uValueForCharA, vValueForCharA, 
                      uValueForCharA + eachCharWidth, ...};

...

glPopMatrix();

AFAIK基本上有三种方法可以将文本渲染为纹理( 请参阅参考资料 )或者您可以使用与您的opengl混合的2-D图形库( Quartz ),或者最后您可以使用例如UILabel来显示文本你的opengl输出的顶部。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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