I'm working in an OpenGL 3D project and I've started working with textures. I've taken an example project to learn how OpenGL 3D works. This project has 6 textures and a texture loader method, and I want to add more textures so I've searched over the net and I've found some interesting textures. However, when I try to load them, console gives me the following error:
Code:
Wed Feb 24 12:49:36 Joan.local provaObjectes3D[7977] <Error>: CGBitmapContextCreate: unsupported colorspace.
Wed Feb 24 12:49:36 Joan.local provaObjectes3D[7977] <Error>: CGContextDrawImage: invalid context
And my own-textured objects are shown with the last "default" texture in the project (this is the last texture loaded).
Here is how the code goes:
Code:
GLuint textures[10]; // <-- of course this is defined in the header file
// this goes to the .m init method and the first 6 textures are loaded well
// (in fact example project comes with those 6 textures)
(...)
glGenTextures(10, &textures[0]);
[self loadTexture:@"bamboo.png" intoLocation:textures[0]];
[self loadTexture:@"flowers.png" intoLocation:textures[1]];
[self loadTexture:@"grass.png" intoLocation:textures[2]];
[self loadTexture:@"lino.png" intoLocation:textures[3]];
[self loadTexture:@"metal.png" intoLocation:textures[4]];
[self loadTexture:@"schematic.png" intoLocation:textures[5]];
[self loadTexture:@"blanc.png" intoLocation:textures[6]];
[self loadTexture:@"floor1.png" intoLocation:textures[7]];
[self loadTexture:@"bricks1.png" intoLocation:textures[8]];
[self loadTexture:@"bricks2.png" intoLocation:textures[9]];
(...)
// Here's the loading method...
- (void)loadTexture:(NSString *)name intoLocation:(GLuint)Location {
CGImageRef textureImage = [UIImage imageNamed:name].CGImage;
if (textureImage == nil) {
NSLog(@"Failed to load texture image: %@",name);
return;
}
NSInteger texWidth = CGImageGetWidth(textureImage);
NSInteger texHeight = CGImageGetHeight(textureImage);
GLubyte *textureData = (GLubyte *)malloc(texWidth * texHeight * 4);
CGContextRef textureContext = CGBitmapContextCreate(
textureData,
texWidth,
texHeight,
8, texWidth * 4,
CGImageGetColorSpace(textureImage),
kCGImageAlphaPremultipliedLast);
CGContextDrawImage(textureContext,
CGRectMake(0.0, 0.0, (float)texWidth, (float)texHeight),
textureImage);
CGContextRelease(textureContext);
//Aquí es guarda la textura
glBindTexture(GL_TEXTURE_2D, Location);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);
free(textureData);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glEnable(GL_TEXTURE_2D);
}
// And how I print my own object trying to use my textures...
glPushMatrix();
{
//[vista apply];
//glTranslatef([c getX], [c getY], [c getZ]);
//glRotatef([c getRota], 1.0, 1.0, 1.0);
glVertexPointer(3, GL_FLOAT, 0, roomVertices);
glEnableClientState(GL_VERTEX_ARRAY);
// Draw the front face
glBindTexture(GL_TEXTURE_2D, textures[7]);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
// Draw the top face
glBindTexture(GL_TEXTURE_2D, textures[8]);
glDrawArrays(GL_TRIANGLE_FAN, 4, 4);
(...)
}
glPopMatrix();
I don't know what can be happening with textures, I've downloaded from the net (some of them in .PNG and some of them in .JPG) and I've converted through Photoshop to .PNG files (because I've tried to use them directly how I've found them but it didn't worked, so I've thought the problem can be solved by converting to .PNG) but it's still not working...
I'm pretty sure you can get the 'unsupported colorspace' error when using textures where the width/height is not a power of 2 (width/height needs to be 32/64/128/256 etc).
Thanks for your reply, but unfortunately have to say that textures are 256 x 256 pixels, so I think this is fine... These errors occurs when trying to load images to textures because I don't even try to use them as a textures, but I don't know why... Maybe there's something in my function that isn't fully right?
I've traced my images' colorSpaces and I've found that images that are correctly loaded have a colorSpace that, when converting to a NSString (I know they aren't, but don't know how to show it's name properly) results in "<NSCFType: 0x1c27ba0>", and images that are not correctly loaded results in "<NSCFType: 0x383d5b0>"... Don't know what it exactly means, except that seems that "bad" images have something strange with it's color information. Here's the part of the code where color is processed:
So the three last textures (which are the ones that I'm trying to add to my project) seem to have a different colorSpace (which I don't know exactly what it means because I don't even know which "space" have the right ones and which have the bad ones... Anything about this?