GLKit中的纹理映射不适用于设备
我用这个链接的代码来映射人脸的纹理。 该代码使用GLKIT来渲染图像。 代码在模拟器中工作正常,但是如果我在设备上运行,代码不起作用。 下面是在设备中工作的图像的屏幕截图,而不是在我的ipad中。
我用来加载纹理的代码:
- (void)loadTexture:(UIImage *)textureImage { glGenTextures(1, &_texture); glBindTexture(GL_TEXTURE_2D, _texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); CGImageRef cgImage = textureImage.CGImage; float imageWidth = CGImageGetWidth(cgImage); float imageHeight = CGImageGetHeight(cgImage); CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider(cgImage)); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageWidth, imageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, CFDataGetBytePtr(data)); }
模拟器的图像:
设备中的相同代码给出以下输出:
有我失踪的东西吗?
我终于放下并切换到GLKTextureLoader。 正如亚当所说,这是非常坚固的。
这是一个可能适合你的实现:
- (void)loadTexture:(NSString *)fileName { NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES], GLKTextureLoaderOriginBottomLeft, nil]; NSError* error; NSString* path = [[NSBundle mainBundle] pathForResource:fileName ofType:nil]; GLKTextureInfo* texture = [GLKTextureLoader textureWithContentsOfFile:path options:options error:&error]; if(texture == nil) { NSLog(@"Error loading file: %@", [error localizedDescription]); } glBindTexture(GL_TEXTURE_2D, texture.name); }
我将尽快更新它在GitHub上的mtl2opengl
项目…
-
使用苹果公司内置的1行方法加载 – 不要编写自己的(破坏)实现!
-
或者,如果你必须自己做,你有两个可疑的部分:
2.1。 摆脱这一行:
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
2.2。 这一行使用一个超出范围的数字,这不是很好:
glGenTextures(1,&_texture);
-
如果由于某种原因,你不能使用苹果的代码(例如,你想从内存中的图像加载原始数据),这里是工作代码的复制/粘贴:
NSData* imageData = ... // fill this yourself CGSize* imageSize = ... // fill this yourself GLuint integerForOpenGLToFill; glGenTextures(1, &integerForOpenGLToFill); glBindTexture( GL_TEXTURE_2D, integerForOpenGLToFill); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); unsigned char* pixelData = (unsigned char*) malloc( [imageData length] * sizeof(unsigned char) ); [imageData getBytes:pixelData]; glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageSize.width, imageSize.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixelData);
看截图…我不知道问题是,你要从iPhone到iPad?
即视网膜到非视网膜?
看起来像你的图像加载可能会忽略视网膜的规模,所以纹理地图是在iPad上“2倍太大”。
使用苹果的方法(我的其他答案)应该自动修复,但如果不是这样:你可以尝试制作两个副本的图像,一个在当前的大小(名为“image@2x.png”)和一个一半的大小(在Photoshop中resize/ preview.app / etc)(名为“image.png”)