2
我正在創建一個iPhone遊戲,我需要將圖像從PNG文件加載到OpenGL中(並將其作爲紋理進行綁定)。我使用函數glTexImage2D來實現這個目標。從iPhone中的C++代碼將圖像載入到OpenGL ES中
我知道如何使用UIImage將圖像加載到OpenGL中(通過將圖像轉換爲CGImage並隨後繪製到上下文中)。
如何從C++代碼中調用我的Objective-C代碼(我正在編寫.mm文件)?
下面是我使用的代碼,這將在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和核芯顯卡的功能呢?我必須包含哪些文件?
預先感謝您。
或者,右鍵單擊Frameworks組並選擇「添加現有框架」,然後從列表中選取Core Graphics。 – 2010-01-01 14:21:39