mk1*_*k12 7 opengl macos textures image objective-c
当我正常加载图像中的纹理时,由于OpenGL的坐标系统,它们是颠倒的.翻转它们的最佳方法是什么?
这是我在Utilities.m文件(Objective-C)中用来加载png纹理的方法:
+ (TextureImageRef)loadPngTexture:(NSString *)name {
CFURLRef textureURL = CFBundleCopyResourceURL(
CFBundleGetMainBundle(),
(CFStringRef)name,
CFSTR("png"),
CFSTR("Textures"));
NSAssert(textureURL, @"Texture name invalid");
CGImageSourceRef imageSource = CGImageSourceCreateWithURL(textureURL, NULL);
NSAssert(imageSource, @"Invalid Image Path.");
NSAssert((CGImageSourceGetCount(imageSource) > 0), @"No Image in Image Source.");
CFRelease(textureURL);
CGImageRef image = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
NSAssert(image, @"Image not created.");
CFRelease(imageSource);
GLuint width = CGImageGetWidth(image);
GLuint height = CGImageGetHeight(image);
void *data = malloc(width * height * 4);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSAssert(colorSpace, @"Colorspace not created.");
CGContextRef context = CGBitmapContextCreate(
data,
width,
height,
8,
width * 4,
colorSpace,
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host);
NSAssert(context, @"Context not created.");
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
CGImageRelease(image);
CGContextRelease(context);
return TextureImageCreate(width, height, data);
}
Run Code Online (Sandbox Code Playgroud)
其中TextureImage是具有height,width和void*数据的结构.
现在我只是玩OpenGL,但后来我想尝试制作一个简单的2D游戏.我使用Cocoa进行所有窗口,使用Objective-C作为语言.
另外,我想知道另一件事:如果我做了一个简单的游戏,将像素映射到单位,是否可以设置它以使原点位于左上角(个人喜好),或者我会运行处理其他事情的问题(例如文本呈现)?
谢谢.
Jordan Lewis 指出CGContextDrawImage 在传递 UIImage.CGImage 时会上下颠倒地绘制图像。在那里我找到了一个快速简单的解决方案:在调用 CGContextDrawImage 之前,
CGContextTranslateCTM(context, 0, height);
CGContextScaleCTM(context, 1.0f, -1.0f);
Run Code Online (Sandbox Code Playgroud)
工作做得非常好。
| 归档时间: |
|
| 查看次数: |
5076 次 |
| 最近记录: |