[英]Loading image into OpenGL ES from c++ code in iPhone
我正在创建一个iPhone游戏,我需要将一个PNG文件中的图像加载到OpenGL中(并将其绑定为纹理)。 我正在使用glTexImage2D函数来实现此目标。
我知道如何使用UIImage将图像加载到OpenGL中(通过将其转换为CGImage并随后绘制到上下文中)。
如何在C ++代码(我在.mm文件中进行编码)中调用我的Objective-C代码?
这是我正在使用的代码,它将在Objective-C中运行:
NSString *path = [[NSBundle mainBundle] pathForResource:@"texture" 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 );
CGColorSpaceRelease( colorSpace );
CGContextClearRect( context, CGRectMake( 0, 0, width, height ) );
CGContextTranslateCTM( context, 0, 0 );
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);
[image release];
[texData release];
有没有办法从C ++代码调用那些UI和Core Graphics函数? 我必须包括哪些文件?
先感谢您。
解决了问题。 必须手动查找并添加Core Graphics框架。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.