Question

I'm programming a small app with opengl for the iphone, and now I want to load textures in the PVRTC format. Is this complicated ?. Right now all my textures are in .png format, and I'm using this method for loading them:

- (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);   

}

Modifying this function to read PVRTC is complicated ?, how can I do it ?

Was it helpful?

Solution

Yep, you'll want to look at the PVRTextureLoader sample. It's a very simple file format. The key piece is the header structure:

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;

You can then slurp the entire file with dataWithContentsOfFile, and simply cast the result to the structure:

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;

To determine the texture format, you can look at the flags field in the struct. Don't forget to call glCompressedTexImage2D rather than glTexImage2D.

OTHER TIPS

Check out the PVRTextureLoader example in the developer documentation in XCode. It has all you need.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top