Pergunta

Eu estou programando um pequeno aplicativo com OpenGL para o iPhone, e agora eu quero carregar texturas no formato PVRTC. É este complicado?. Agora todas as minhas texturas são em formato .png, e eu estou usando esse método para carregá-los:

- (void)loadTexture:(NSString*)nombre {

    CGImageRef textureImage = [UIImage imageNamed:nombre].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); 

    CGContextRef textureContext = CGBitmapContextCreate(textureData, textureWidth,textureHeight,8, textureWidth * 4,CGImageGetColorSpace(textureImage),kCGImageAlphaPremultipliedLast );
    CGContextDrawImage(textureContext, CGRectMake(0.0, 0.0, (float)textureWidth, (float)textureHeight), textureImage);

    CGContextRelease(textureContext);
    a
    glGenTextures(1, &textures[0]);

    glBindTexture(GL_TEXTURE_2D, textures[0]);


    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);

    free(textureData);


    glEnable(GL_BLEND);


    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);   

}

Modificar esta função para ler PVRTC é complicado?, Como posso fazê-lo?

Foi útil?

Solução

Sim, você vai querer olhar para a amostra PVRTextureLoader. É um formato de arquivo muito simples. A peça-chave é a estrutura de cabeçalho:

typedef struct _PVRTexHeader
{
    uint32_t headerLength;
    uint32_t height;
    uint32_t width;
    uint32_t numMipmaps;
    uint32_t flags;
    uint32_t dataLength;
    uint32_t bpp;
    uint32_t bitmaskRed;
    uint32_t bitmaskGreen;
    uint32_t bitmaskBlue;
    uint32_t bitmaskAlpha;
    uint32_t pvrTag;
    uint32_t numSurfs;
} PVRTexHeader;

Você pode então ingerir o arquivo inteiro com dataWithContentsOfFile , e simplesmente converter o resultado para a estrutura:

NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
NSString* fullPath = [resourcePath stringByAppendingPathComponent:nombre];
NSData* fileData = [NSData dataWithContentsOfFile:fullPath];
PVRTexHeader* header = (PVRTexHeader*) [fileData bytes];
char* imageData = (char*) [fileData bytes] + header->headerLength;

Para determinar o formato de textura, você pode olhar para as bandeiras campo no struct. Não se esqueça de chamada glCompressedTexImage2D em vez de glTexImage2D .

Outras dicas

Confira o exemplo PVRTextureLoader na documentação do desenvolvedor no XCode. Ele tem tudo que você precisa.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top