Is there a decent OpenGL text drawing library for the iPhone SDK?

前端 未结 8 759
无人共我
无人共我 2020-11-28 20:54

I\'m trying to figure out a simple to draw some text in OpenGL. My research has shown its a fairly complex task. It involves creating (or generating at runtime) a font atl

8条回答
  •  独厮守ぢ
    2020-11-28 21:34

    Yes, there are 2 that I know of. The trick is, you have to render to texture using Quartz (CGContext* functions), then render that texture for the user to see.

    If you're concerned about performance, you can also use Quartz to generate your texture atlas.

    It's fairly easy to whip up a texture generation tool (in xCode) once you know the basics of rendering a font.

    All you have to do is have a valid CGContext. You can find some really great sample code in this texture loader.

    Basically the code goes:

    // Create the cgcontext as shown by links above
    
    // Now to render text into the cg context,
    // the drop the raw data behind the cgcontext
    // to opengl.  2 ways to do this:
    // 1)  CG* way: (advantage: easy to use color)
    CGContextSelectFont(cgContext, "Arial", 24, kCGEncodingMacRoman);
    
    // set color to yellow text
    CGContextSetRGBFillColor(cgContext, 1, 1, 0, 1) ; // Be sure to use FILL not stroke!
    
    // draw it in there
    CGContextShowTextAtPoint(cgContext, 20, 20, "HI THERE", strlen("HI THERE") ) ;
    
    /*
    // WAY #2:  this way it's always black and white
    // DRAW SOME TEXT IN IT
    // that works ok but let's try cg to control color
    UIGraphicsPushContext(cgContext);
    UIFont *helv = [UIFont fontWithName:@"Helvetica" size:[UIFont systemFontSize]];
    NSString* msg = @"Hi hello" ;  
    [msg drawInRect:CGRectMake(0,0,pow2Width,pow2Height) withFont:helv] ;
        UIGraphicsPopContext();
    */
    
    // put imData into OpenGL's memory
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, pow2Width, pow2Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imData);
    CHECK_GL ;
    

    It comes out looking pretty good and antialiased,

    tex atlas

    A list of ios fonts is available here, and with prerenders here

提交回复
热议问题