   iPhone: Texture Mapping pixelation error
  Ive been following some guides for loading PNG's with Object C fir OpenGL ES. Im getting a strange bug which seems like all the data is corrupt, heres the picture:http://i39.photobucket.com/albums/e183/kingwoods/iPhone_Err.jpgHeres the code im using:@interface BBImage : NSObject {@publicCGImageRef m_imageData;GLuint m_iImageID;size_t m_iImageWidth, m_iImageHeight;}@property (assign) CGImageRef m_imageData;@property (assign) GLuint m_iImageID;@property (assign) size_t m_iImageWidth;@property (assign) size_t m_iImageHeight;@endTHe above is used to store the image data and the OpenGL bound image id.Loading code:-(BBImage*) loadTexture: (NSString *) source {BBImage *image = [[BBImage alloc] init];// Load textureimage.m_imageData = [UIImage imageNamed:source].CGImage;if (image.m_imageData == nil) {NSLog(@"Failed to load texture image: ");NSLog(source);NSLog(@"Loading error texture in place. ");// Load error textureimage.m_imageData = [UIImage imageNamed:@"error.png"].CGImage;if (image.m_imageData == nil) {NSLog(@"Failed to load error texture image.");}}NSInteger texWidth = CGImageGetWidth(image.m_imageData);    NSInteger texHeight = CGImageGetHeight(image.m_imageData);image.m_iImageWidth = texWidth;image.m_iImageHeight = texHeight;GLubyte *textureData = (GLubyte *)malloc(texWidth * texHeight * 4);    CGContextRef textureContext = CGBitmapContextCreate(textureData,texWidth, texHeight,8, texWidth * 4,CGImageGetColorSpace(image.m_imageData),kCGImageAlphaPremultipliedLast);CGContextDrawImage(textureContext, CGRectMake(0.0, 0.0, (float)texWidth, (float)texHeight), image.m_imageData);CGContextRelease(textureContext);   // If anyone can think of a way to make this gen straight into the class without a warning please tell :)GLuint textures[1];glGenTextures(1, &textures[0]);image.m_iImageID = textures[0];// Free local image datafree(textureData);NSLog(@"Loaded image succesfully: ");NSLog(source);return image;}And finally rendering:- (void)doRender {const GLfloat squareVertices[] = {   -1.0, 1.0, 0.0,  // Top left   1.0, 1.0, 0.0,   // Top right   1.0, -1.0, 0.0,  // Bottom right   -1.0, -1.0, 0.0  // Bottom left    };    const GLshort squareTextureCoords[] = {   0, 1,  // top left   1, 1,  // top right   1, 0,  // bottom right   0, 0   // bottom left    };//[VIZ preRender];[EAGLContext setCurrentContext:pContext];   glBindFramebufferOES(GL_FRAMEBUFFER_OES, (GLint)*viewFramebuffer);    glViewport(0, 0, m_iScreenWidth, m_iScreenHeight);glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);   glMatrixMode(GL_PROJECTION);    glLoadIdentity();   glOrthof(-1.0f, 1.0f, -1.5f, 1.5f, -1.0f, 1.0f);glMatrixMode(GL_MODELVIEW);glEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D, image.m_iImageID);glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.m_iImageWidth, image.m_iImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, image.m_imageData);glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);glLoadIdentity();glScalef(0.5, 0.5, 0.5);    glColor4f(1.0, 1.0, 1.0, 1.0);glVertexPointer(3, GL_FLOAT, 0, squareVertices);    glEnableClientState(GL_VERTEX_ARRAY);   glTexCoordPointer(2, GL_SHORT, 0, squareTextureCoords);   glEnableClientState(GL_TEXTURE_COORD_ARRAY);    glDrawArrays(GL_TRIANGLE_FAN, 0, 4);    glDisableClientState(GL_TEXTURE_COORD_ARRAY);//[VIZ postRender];glBindRenderbufferOES(GL_RENDERBUFFER_OES, (GLint)*viewRenderbuffer);    [pContext presentRenderbuffer:GL_RENDERBUFFER_OES];}Any help would be great, this has been driving me insane :(Also im usually a C++ programmer so if theres anything im doing thats considered bad practice in Object C let me know.   
  I fixed this problem, it was just this line:// Free local image datafree(textureData);I was deleting the actual data in the creation function. Silly mistake :(   ;  Your binding and recreating the texture every time you want to render, that's an expensive process. Usually that block of code glEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D, image.m_iImageID);glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.m_iImageWidth, image.m_iImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, image.m_imageData);glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);is done once within ur texture loading code before :// Free local image datafree(textureData);is executed ( the reason u free the image after u bind it is because binding creates a openGL copy which then frees the need of keeping the original image )Good Luck!-ddn   