【问题标题】:Draw text in iPhone openGL ES using just objective-c仅使用objective-c在iPhone openGL ES中绘制文本
【发布时间】:2010-06-19 13:46:27
【问题描述】:

需要在 openGL 中绘制一些文本,然后对其进行旋转和平移。只需要使用objective-c。有什么帮助吗?

【问题讨论】:

    标签: iphone objective-c xcode opengl-es


    【解决方案1】:

    使用 Photoshop 或其他工具创建如下纹理文件。

    |ABCDEFGH|
    |IJKLMNOP|
    |QRSTU...|
    
    sample.png
    

    计算每个字符的UV位置。

    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;
    

    设置 texcoords 并绘制。

    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();
    

    【讨论】:

      【解决方案2】:

      AFAIK 基本上有三种方法可以做到这一点,您可以将文本渲染为纹理 (see),也可以将二维图形库 (Quartz) 与您的 opengl 混合使用,或者最后您可以使用例如UILabel 在您的 opengl 输出顶部显示文本。

      【讨论】:

      • 问题是我需要将文本绘制为对象,然后旋转并翻译它。 2D 解决方案就是解决方案。感谢任何方式:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多