Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-24-2010, 06:59 AM   #1 (permalink)
Divine avenger
 
Johanovski's Avatar
 
Join Date: Nov 2009
Location: Vic, Catalunya (Spain)
Posts: 320
Default Unable to load textures in OpenGL 3D!

Hi all!

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...

Anyone can help me with this?

Thanks in advance!
Johanovski is offline   Reply With Quote
Old 02-24-2010, 01:28 PM   #2 (permalink)
Registered Member
 
Join Date: Jan 2010
Location: West Yorkshire, UK
Posts: 63
Default

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).
JohnnyC is offline   Reply With Quote
Old 02-25-2010, 02:46 AM   #3 (permalink)
Divine avenger
 
Johanovski's Avatar
 
Join Date: Nov 2009
Location: Vic, Catalunya (Spain)
Posts: 320
Default

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?

Thanks!
Johanovski is offline   Reply With Quote
Old 02-25-2010, 03:04 AM   #4 (permalink)
Divine avenger
 
Johanovski's Avatar
 
Join Date: Nov 2009
Location: Vic, Catalunya (Spain)
Posts: 320
Default

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:

Code:
CGImageRef textureImage = [UIImage imageNamed:name].CGImage;
	
	(...)
	
	NSLog(@"COLOR:%@",CGImageGetColorSpace(textureImage));

	CGContextRef textureContext = CGBitmapContextCreate(
										textureData,
										texWidth,
										texHeight,
										8, texWidth * 4,
										CGImageGetColorSpace(textureImage),
										kCGImageAlphaPremultipliedLast);
And here's what my console traces:

Code:
...[749:207] COLOR:<NSCFType: 0x1c27ba0>
...[749:207] COLOR:<NSCFType: 0x1c27ba0>
[Switching to process 749]
...[749:207] COLOR:<NSCFType: 0x1c27ba0>
...[749:207] COLOR:<NSCFType: 0x1c27ba0>
...[749:207] COLOR:<NSCFType: 0x1c27ba0>
...[749:207] COLOR:<NSCFType: 0x1c27ba0>
...[749:207] COLOR:<NSCFType: 0x1c27ba0>
...[749:207] COLOR:<NSCFType: 0x383d5b0>
...[749] <Error>: CGBitmapContextCreate: unsupported colorspace.
...[749] <Error>: CGContextDrawImage: invalid context
...[749:207] COLOR:<NSCFType: 0x3b3cca0>
...[749] <Error>: CGBitmapContextCreate: unsupported colorspace.
...[749] <Error>: CGContextDrawImage: invalid context
...[749:207] COLOR:<NSCFType: 0x3b3ed60>
...[749] <Error>: CGBitmapContextCreate: unsupported colorspace.
...[749] <Error>: CGContextDrawImage: invalid context
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?

Thanks!
Johanovski is offline   Reply With Quote
Old 02-25-2010, 12:06 PM   #5 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

The "broken" images might be 8-bit pngs, or pngs with fewer colors. You could re-save them as 24-bit pngs, or you could always use
Code:
colorSpace = CGColorSpaceCreateDeviceRGB();
//create your context with colorSpace

CGColorSpaceRelease(colorSpace);
no matter what the color space the images have. That's what the texture2d class does.
__________________

Free Games!
smasher is offline   Reply With Quote
Reply

Bookmarks

Tags
glgentextures, opengl 3d, opengl es, textures

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Stats
Members: 158,884
Threads: 89,229
Posts: 380,763
Top Poster: BrianSlick (7,129)
Welcome to our newest member, karlam963
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:14 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0