سؤال

أقوم ببرمجة تطبيق صغير باستخدام opengl لجهاز iPhone، والآن أريد تحميل مواد بتنسيق PVRTC.هل هذا معقد؟.في الوقت الحالي، جميع الزخارف الخاصة بي موجودة بتنسيق ‎.png، وأنا أستخدم هذه الطريقة لتحميلها:

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

}

تعديل هذه الوظيفة لقراءة PVRTC أمر معقد؟ كيف يمكنني القيام بذلك؟

هل كانت مفيدة؟

المحلول

نعم، ستحتاج إلى إلقاء نظرة على نموذج PVRTextureLoader.إنه تنسيق ملف بسيط جدًا.القطعة الأساسية هي هيكل الرأس:

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;

يمكنك بعد ذلك تناول الملف بأكمله باستخدام dataWithContentsOfFile, ، وقم ببساطة بإلقاء النتيجة على الهيكل:

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;

لتحديد تنسيق الملمس، يمكنك إلقاء نظرة على أعلام المجال في الهيكل.لا تنسى الاتصال glCompressedTexImage2D بدلا من glTexImage2D.

نصائح أخرى

وراجع المثال PVRTextureLoader في وثائق المطور في كسكودي. لديها كل ما تحتاجه.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top