Menu Home

iOS:使用GLES绘制文字

EAGLContext *contextGL;
contextGL = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
    if (contextGL == nil) {
        NSLog(@"wangsijie, failed to create EAGLContext");
    } else {
        NSLog(@"wangsijie, create EAGLContext OK");
    }
    
    CGRect rectGL = CGRectMake(100, 100, 150, 100);
    GLKView *viewGL = [[GLKView alloc] initWithFrame:rectGL];
    viewGL.context = contextGL;
    viewGL.enableSetNeedsDisplay = NO;
    [self.view addSubview:viewGL];
    [viewGL setDelegate:self];
    CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(render:)];
    [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    
    [viewGL setTag:9];
    [self.mediaControl2 addSubview:viewGL];

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
    UIGraphicsBeginImageContext(_mediaControl2.currentTimeLabel.bounds.size);
    [_mediaControl2.currentTimeLabel.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *layerImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    CIContext *ciContext = [CIContext contextWithEAGLContext:contextGL];
    CIImage *ciImage = [[CIImage alloc] initWithImage:layerImage];
    [ciContext drawImage:ciImage inRect:CGRectMake(0, 0, 150, 100) fromRect:[ciImage extent]];
    
    //glClearColor(1.0, 0.0, 0.0, 1.0);
    //glClear(GL_COLOR_BUFFER_BIT);
}
- (void)render:(CADisplayLink*)displayLink {
    GLKView *view = [self.mediaControl2 viewWithTag:9];
    [view display];
}

Categories: IOS objective-c

Tagged as:

lnmcc