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

时间:2022-04-12 18:54:13

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来创建一个offscreen形状,然后保存为UIImage ?

1 个解决方案

#1


0  

If you want this in a pure OpenGL ES way, you can use freetype library for that purpose. 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.

如果你想用纯OpenGL ES的方式,你可以使用freetype库来实现这个目的。在开始的时候建立和掌握可能有点困难。但是一旦您熟悉了它,您就可以使用它的纯C API来在任何设备上绘制可伸缩和反文本的OpenGL表面。

#1


0  

If you want this in a pure OpenGL ES way, you can use freetype library for that purpose. 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.

如果你想用纯OpenGL ES的方式,你可以使用freetype库来实现这个目的。在开始的时候建立和掌握可能有点困难。但是一旦您熟悉了它,您就可以使用它的纯C API来在任何设备上绘制可伸缩和反文本的OpenGL表面。