开发者

Draw text in iPhone openGL ES using just objective-c

开发者 https://www.devze.com 2023-01-03 20:40 出处:网络
A need to draw some开发者_StackOverflow社区 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

A need to draw some开发者_StackOverflow社区 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 there are basically three ways to do it either you can render the text as a texture (see) or you can use the 2-d graphics library (Quartz) mixed with your opengl or finally you can use e.g. UILabel to display the text on top of your opengl output.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号