tags:

views:

82

answers:

1

hi

i Want to know what is the reason image is not load but a white view is open.When i debug the texture vale and summary value is -1.

My Code is:

_textures[kTexture_Background] = [[Texture2D alloc] initWithImage: [UIImage imageNamed:@"backgroundimage.png"]];

Thanks

A: 

Hi in EAGLview.m My Code is:

(void)drawView { CGRect rect = [[UIScreen mainScreen] bounds]; glViewport(0, 0, backingWidth, backingHeight); glMatrixMode(GL_PROJECTION); //glLoadIdentity(); // enable GL states glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnable(GL_TEXTURE_2D); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

//Set up OpenGL projection matrix
glMatrixMode(GL_PROJECTION);
glOrthof(0, rect.size.width, 0, rect.size.height, -1, 1);
glMatrixMode(GL_MODELVIEW);

//Initialize OpenGL states
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);


_textures[kTexture_Background] = [[Texture2D alloc] initWithImage:[UIImage imageWithContentsOfFile :[[NSBundle mainBundle] pathForResource:@"background" ofType:@"png"]]];
glBindTexture(GL_TEXTURE_2D, [_textures[kTexture_Background] name]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

and i create Texture2D where I use This Function

  • (id) initWithImage:(UIImage )uiImage { NSUInteger width,height,i; CGContextRef context = nil; void data = nil;; CGColorSpaceRef colorSpace; void* tempData; unsigned int* inPixel32; unsigned short* outPixel16; BOOL hasAlpha; CGImageAlphaInfo info; CGAffineTransform transform; CGSize imageSize; Texture2DPixelFormat pixelFormat; CGImageRef image; UIImageOrientation orientation; BOOL sizeToFit = NO;

    image = [uiImage CGImage]; orientation = [uiImage imageOrientation]; if(image == NULL) { [self release]; NSLog(@"Image is Null"); return nil; } info = CGImageGetAlphaInfo(image); hasAlpha = ((info == kCGImageAlphaPremultipliedLast) || (info == kCGImageAlphaPremultipliedFirst) || (info == kCGImageAlphaLast) || (info == kCGImageAlphaFirst) ? YES : NO); if(CGImageGetColorSpace(image)) { if(hasAlpha) pixelFormat = kTexture2DPixelFormat_RGBA8888; else pixelFormat = kTexture2DPixelFormat_RGB565; } else //NOTE: No colorspace means a mask image pixelFormat = kTexture2DPixelFormat_A8; imageSize = CGSizeMake(CGImageGetWidth(image), CGImageGetHeight(image)); transform = CGAffineTransformIdentity; width = imageSize.width; if((width != 1) && (width & (width - 1))) { i = 1; while((sizeToFit ? 2 * i : i) < width) i *= 2; width = i; } height = imageSize.height; if((height != 1) && (height & (height - 1))) { i = 1; while((sizeToFit ? 2 * i : i) < height) i *= 2; height = i; } while((width > kMaxTextureSize) || (height > kMaxTextureSize)) { width /= 2; height /= 2; transform = CGAffineTransformScale(transform, 0.5, 0.5); imageSize.width *= 0.5; imageSize.height *= 0.5; } switch(pixelFormat) { case kTexture2DPixelFormat_RGBA8888: colorSpace = CGColorSpaceCreateDeviceRGB(); data = malloc(height * width * 4); context = CGBitmapContextCreate(data, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); CGColorSpaceRelease(colorSpace); break; case kTexture2DPixelFormat_RGB565: colorSpace = CGColorSpaceCreateDeviceRGB(); data = malloc(height * width * 4); context = CGBitmapContextCreate(data, width, height, 8, 4 * width, colorSpace, kCGImageAlphaNoneSkipLast | kCGBitmapByteOrder32Big); CGColorSpaceRelease(colorSpace); break;

    case kTexture2DPixelFormat_A8:
     data = malloc(height * width);
     context = CGBitmapContextCreate(data, width, height, 8, width, NULL, kCGImageAlphaOnly);
     break;    
    default:
     [NSException raise:NSInternalInconsistencyException format:@"Invalid pixel format"];
    

    } CGContextClearRect(context, CGRectMake(0, 0, width, height)); CGContextTranslateCTM(context, 0, height - imageSize.height);

    if(!CGAffineTransformIsIdentity(transform)) CGContextConcatCTM(context, transform); CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image)), image); //Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRGGGGGGBBBBB" if(pixelFormat == kTexture2DPixelFormat_RGB565) { tempData = malloc(height * width * 2); inPixel32 = (unsigned int*)data; outPixel16 = (unsigned short*)tempData; for(i = 0; i < width * height; ++i, ++inPixel32) outPixel16++ = ((((inPixel32 >> 0) & 0xFF) >> 3) << 11) | ((((inPixel32 >> 8) & 0xFF) >> 2) << 5) | ((((inPixel32 >> 16) & 0xFF) >> 3) << 0); free(data); data = tempData;

    } self = [self initWithData:data pixelFormat:pixelFormat pixelsWide:width pixelsHigh:height contentSize:imageSize];

    CGContextRelease(context); free(data);

    return self; }

@end

Thanks

Please format your code: four spaces to indent code on SO.
Nikolai Ruhe