简体   繁体   English

将文本添加到2D OpenGL ES 2.0对象-GLKit-iOS

[英]Adding Text to a 2D OpenGL ES 2.0 Object - GLKit - iOS

I have some basic code to draw various polygons (in this case a triangle) : 我有一些基本的代码可以绘制各种多边形(在本例中为三角形):

#import "Triangle.h"

@interface Triangle() {
    GLKBaseEffect * effect;
    NSMutableData *vertexData;

    int numVertices;
}

@property (nonatomic, strong) GLKBaseEffect * effect;
@property (nonatomic, assign) int numVertices;
@property (nonatomic, assign) GLKVector2 *vertices;

@end

@implementation Triangle
@synthesize effect;
@synthesize numVertices;
@synthesize vertices;

- (id)initWithEffect : (GLKBaseEffect *)effectPassed {
    self.effect = effectPassed;
    self.numVertices = 3;

    self.vertices[0] = GLKVector2Make(0,0);
    self.vertices[1] = GLKVector2Make(500, 0);
    self.vertices[2] = GLKVector2Make(500, 500);

    return self;
}

- (GLKVector2 *)vertices {
    if (vertexData == nil)
        vertexData = [NSMutableData dataWithLength:sizeof(GLKVector2)*self.numVertices];
    return [vertexData mutableBytes];
}

-(void)render {

    effect.useConstantColor = YES;
    effect.constantColor = GLKVector4Make(1.0, 0.0, 0.1, 1.0);

    [self.effect prepareToDraw];

    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, self.vertices);
    glDrawArrays(GL_TRIANGLE_FAN, 0, self.numVertices);
    glDisableVertexAttribArray(GLKVertexAttribPosition);
}

@end

What I want to do is somehow put some text inside the shape(s). 我想做的是以某种方式在形状中放入一些文本。 I am thinking along the lines of creating a texture of the same shape (perhaps using Quartz ?) and then using this texture on the shape. 我正在考虑创建相同形状的纹理(也许使用Quartz?),然后在形状上使用此纹理。

Would anyone be able to advise if this a good way of achieving this, or any other advise ? 任何人都可以建议这是否是实现此目标的好方法,或者有其他建议? Is it possible to use Quartz 2D to create an offscreen shape to then save as a UIImage ? 是否可以使用Quartz 2D创建屏幕外的形状然后保存为UIImage?

If you want this in a pure OpenGL ES way, you can use freetype library for that purpose. 如果您希望以纯OpenGL ES方式实现此目的,则可以为此使用freetype库。 It might be a bit difficult to build and grasp at the beginning. 一开始可能很难构建和掌握。 But once you are familiar to it, you can use its pure C API to draw scalable and anti-alised text to OpenGL surface on any device. 但是一旦您熟悉了它,就可以使用其纯C API在任何设备上的OpenGL表面绘制可扩展且抗锯齿的文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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