我是openGL中的菜鸟并尽可能多地学习。我正在使用此方法加载我的openGL纹理,将每个.png加载为RGBA4444。我做错了什么?
- (void)loadTexture:(NSString*)nombre {
CGImageRef textureImage =[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:nombre ofType:nil]].CGImage;
if (textureImage == nil) {
NSLog(@"Failed to load texture image");
return;
}
textureWidth = NextPowerOfTwo(CGImageGetWidth(textureImage));
textureHeight = NextPowerOfTwo(CGImageGetHeight(textureImage));
imageSizeX= CGImageGetWidth(textureImage);
imageSizeY= CGImageGetHeight(textureImage);
GLubyte *textureData = (GLubyte *)calloc(1,textureWidth * textureHeight * 4); // Por 4 pues cada pixel necesita 4 bytes, RGBA
CGContextRef textureContext = CGBitmapContextCreate(textureData, textureWidth,textureHeight,8, textureWidth * 4,CGImageGetColorSpace(textureImage),kCGImageAlphaPremultipliedLast );
CGContextDrawImage(textureContext, CGRectMake(0.0, 0.0, (float)textureWidth, (float)textureHeight), textureImage);
//Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRGGGGBBBBAAAA"
void *tempData = malloc(textureWidth * textureHeight * 2);
unsigned int* inPixel32 = (unsigned int*)textureData;
unsigned short* outPixel16 = (unsigned short*)tempData;
for(int i = 0; i < textureWidth * textureHeight ; ++i, ++inPixel32)
*outPixel16++ =
((((*inPixel32 >> 0) & 0xFF) >> 4) << 12) | // R
((((*inPixel32 >> 8) & 0xFF) >> 4) << 8) | // G
((((*inPixel32 >> 16) & 0xFF) >> 4) << 4) | // B
((((*inPixel32 >> 24) & 0xFF) >> 4) << 0); // A
free(textureData);
textureData = tempData;
CGContextRelease(textureContext);
glGenTextures(1, &textures[0]);
glBindTexture(GL_TEXTURE_2D, textures[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4 , textureData);
free(textureData);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
这是我的dealloc方法:
- (void)dealloc {
glDeleteTextures(1,textures);
[super dealloc];
}
答案 0 :(得分:0)
我不太确定API相关的问题,但至少看起来你在重新打包时会改变颜色组件的排序(RGBA-> ABGR),并且可以更好地表达按位表达式:
for(int i = 0; i < textureWidth * textureHeight; i++)
outPixel16[i] = (inPixel32[i] >> 4 & 0xF) |
(inPixel32[i] >> 12 & 0xF) |
(inPixel32[i] >> 20 & 0xF) |
(inPixel32[i] >> 28 & 0xF);
顺便说一下,textures
来自哪里?它是全球性的吗?
答案 1 :(得分:0)
你为什么这么问?它在屏幕上看起来不正确吗?
除了*outPixel16++ = blah;
我的第一反应中的endian ickiness
令人惊讶的是iPhone支持GL_UNSIGNED_SHORT_4_4_4_4
。
每当使用GL时,请确保您的上下文处于活动状态,使用
[EAGLContext setCurrentContext:context];