2010-01-26 6 views
0

저는 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 방법이다 : 나는 API 관련 문제에 대한 아주 잘 모르겠지만, 적어도 그것을 재 포장 할 때 색상 구성 요소의 순서를 변경하고 것으로 보인다

- (void)dealloc { 
    glDeleteTextures(1,textures); 
    [super dealloc]; 
} 

답변

0

(

BTW
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에서 온 않습니다 RGBA-> ABGR) 및 비트 표현이 더 잘 표현 될 수 있을까? 글로벌인가?

+0

사용하여, 사용자의 컨텍스트가 활성화되어 있는지 확인합니다. GLuint 텍스처 [1]; –

+0

로컬 범위에서 선언을 볼 수 없습니다. 아마도 뭔가를 놓친 것 같습니다. – fortran

+0

죄송합니다. Texture 클래스의 멤버입니다. 로드 메소드는 클래스의 객체를 만들 때 텍스처를로드하는 데 사용됩니다. –

0

왜 묻는가? 화면에서 제대로 보이지 않습니까? *outPixel16++ = blah; 내 첫 반응
에서 별도로 엔디안 ickiness에서

는 아이폰이 GL_UNSIGNED_SHORT_4_4_4_4을 지원하는 것을 놀람이었다. GL을 사용 할 때마다

은, 그것은, 그냥 내가 텍스처 ID를 저장 변수하지 Global의 것
[EAGLContext setCurrentContext:context];

관련 문제