[英]Texture mapping in GLKit is not working only in devices
我使用此鏈接中的代碼來映射人臉的紋理。 此代碼使用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));
}
模擬器的圖像:
設備中的相同代碼提供以下輸出:
有什么東西我不見了?
使用Apple的內置1行方法加載 - 不要編寫自己的(損壞的)實現!
或者,如果你必須自己做,你有兩個可疑的部分:
2.1。 擺脫這條線:
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
2.2。 此行使用超出范圍的數字,這不是很好:
glGenTextures(1,&_texture);
如果由於某種原因你不能使用Apple的代碼(例如你想從內存中加載原始數據),這里是工作代碼的復制/粘貼:
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嗎?
即視網膜到非視網膜?
在我看來,你的圖像加載可能會忽略Retina比例,因此iPad上的紋理貼圖是“2x太大”。
使用Apple的方法(我的另一個答案)應該自動修復,但如果沒有:你可以嘗試制作兩個圖像副本,一個是當前大小(名為“image@2x.png”),另一個是一半大小(在photoshop中調整大小) / preview.app / etc)(名為“image.png”)
我終於屈服了並切換到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
項目更改它...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.