Hi,

I have an iphone 3G with 3.1.2 SDK (OpenGL 1.1). I am trying to load a 3D model from an OBJ file and texture from a jpg file. The model loads properly. But the texture leaves much to be desired. Look at the attachments - expected-color.jpg and actual-color.jpg.

Here is the code that I am using. Where am I going wrong? Appreciate any help.

John

----

NSString* basePath = [NSString stringWithUTF8String:name.c_str()];
NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
NSString* fullPath = [resourcePath stringByAppendingPathComponent:basePath];

cgImage = uiImage.CGImage;

CGImageRef cgimage;
m_resourceManager->GetCGIImage((void **)&cgimage);

int w = CGImageGetWidth((CGImageRef)cgimage);
int h = CGImageGetHeight((CGImageRef)cgimage);
width = w;
height = h;
pixels = malloc( height * width * 4 );
memset(pixels, 0, (height * width * 4));

unsigned int format;
bool hasAlpha = CGImageGetAlphaInfo(cgimage) != kCGImageAlphaNone;
//CGColorSpaceRef colorSpace = CGImageGetColorSpace(cgimage);

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate( pixels, width, height, 8, 4 * width, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big );
CGColorSpaceRelease( colorSpace );
CGContextClearRect( context, CGRectMake( 0, 0, width, height ) );
CGContextTranslateCTM( context, 0, h - h );
CGContextDrawImage( context, CGRectMake( 0, 0, width, height ), (CGImage *)cgimage );


glBindTexture(GL_TEXTURE_2D, tex);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
.......
.......
......
render
....
Name:  expected-color.jpg
Views: 254
Size:  2.6 KB

Name:  actual-color.jpg
Views: 317
Size:  2.2 KB