ios - 如何在 opengl es 中使用由 UIGraphicsBeginImageContextWithOptions() 制作的图像

标签 ios opengl-es

我需要使用在运行时以编程方式制作的图像作为纹理,但不确定如何从一点转到另一点。制作的图像是使用 UIGraphicsBeginImageContextwithOptions() 制作的,下面是我当前从项目中的图像加载纹理的代码:

-(void)loadTextures:(NSString*) filename textureIdentifier:(int) textureNumber {

    //enable textures.
    glEnable(GL_TEXTURE_2D);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_FASTEST);
    glEnable(GL_BLEND); 
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    // Bind the number of textures we need.
    glGenTextures(1, &texture[textureNumber]);
    glBindTexture(GL_TEXTURE_2D, texture[textureNumber]); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D,GL_GENERATE_MIPMAP,GL_TRUE);    
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);


    NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"png"];
    NSData *texData = [[NSData alloc] initWithContentsOfFile:path];
    UIImage *image = [[UIImage alloc] initWithData:texData];

    if (image == nil)
        NSLog(@"Do real error checking here");

    GLuint width = CGImageGetWidth(image.CGImage);
    GLuint height = CGImageGetHeight(image.CGImage);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    void *imageData = malloc( height * width * 4 );
    CGContextRef context = CGBitmapContextCreate( imageData, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big );

    // Flip the Y-axis
    CGContextTranslateCTM (context, 0, height);
    CGContextScaleCTM (context, 1.0, -1.0);

    CGColorSpaceRelease( colorSpace );
    CGContextClearRect( context, CGRectMake( 0, 0, width, height ) );
    CGContextDrawImage( context, CGRectMake( 0, 0, width, height ), image.CGImage );

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);

    CGContextRelease(context);

    free(imageData);
}

最佳答案

简单。从纹理加载器代码中删除这些行,并将它们替换为您的核心图形绘制代码:

NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"png"];
NSData *texData = [[NSData alloc] initWithContentsOfFile:path];

现在替换这一行:

UIImage *image = [[UIImage alloc] initWithData:texData];

与:

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

关于ios - 如何在 opengl es 中使用由 UIGraphicsBeginImageContextWithOptions() 制作的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8859411/

相关文章:

ios - 带有 sendAsynchronousRequest Objective-C 的 NSURLConnection 进度条

iphone - 如何从 SQL 数据库获取数据到 Core-Data?

ios - 如何在与 OpenGL 混合的 GLKView 中绘制字符串

html - 使用预乘 alpha 时,WebGL 中的 Alpha 渐变不平滑

android - 在 ImageView 上使用图像变换矩阵渲染位图的性能

iphone - 从touchesBegan获取触摸位置? (以及其他游戏问题)

ios - 绘图类绘制直线而不是曲线

mysql - 如何在 iOS 中连接亚马逊 RDS

ios - 使用RestKit,使用 block 加载对象,何时以及如何取消请求?

iphone - OpenGL ES - glImageProcessing - 移除纹理