PDA

View Full Version : textures in an array


Iceman
2002.08.20, 05:08 PM
Hi,
How can I make an array out of my textures? Here's the code that I tried to use, but it obviously doesn't work:

- (void)loadGLTextures {
NSString *path[2];
NSImage *some_image[2];
NSBundle *bundle[2];
NSBitmapImageRep *some_bitmap[2];

int loop;
GLuint texture[2];

path[0] = [bundle[0] pathForResource:@"grass"ofType:@"tif"];

path[1] = [bundle[1] pathForResource:@"wall"ofType:@"tif"];

for(loop = 0; loop < 2; loop++) {

some_image[loop] = [[NSImage alloc]initWithContentsOfFile: path[loop] ];
bundle[loop] = [NSBundle bundleForClass:[self class]];

some_bitmap[loop] = [[NSBitmapImageRep alloc]initWithData:
[some_image[loop] TIFFRepresentation]];

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(2, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[loop]);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, [some_bitmap[loop] size].width,
[some_bitmap[loop] size].height, 0, GL_RGB, GL_UNSIGNED_BYTE, [some_bitmap[loop] bitmapData]);

[some_bitmap[loop] release];
}
}


Is this the best way to load them or is there a better way?

Iceman

Ian Kerr
2002.08.20, 06:27 PM
How about this? I moved glPixelStorei and glGenTextures out of the loop. I haven't done any objective C so I can't comment on the rest. What happened when you ran your original code (how do you know it didn't work)?

- (void)loadGLTextures {
NSString *path[2];
NSImage *some_image[2];
NSBundle *bundle[2];
NSBitmapImageRep *some_bitmap[2];

int loop;
GLuint texture[2];

path[0] = [bundle[0] pathForResource:@"grass"ofType:@"tif"];

path[1] = [bundle[1] pathForResource:@"wall"ofType:@"tif"];

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(2, &texture[0]);

for(loop = 0; loop < 2; loop++) {

some_image[loop] = [[NSImage alloc]initWithContentsOfFile: path[loop] ];
bundle[loop] = [NSBundle bundleForClass:[self class]];

some_bitmap[loop] = [[NSBitmapImageRep alloc]initWithData:
[some_image[loop] TIFFRepresentation]];

glBindTexture(GL_TEXTURE_2D, texture[loop]);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, [some_bitmap[loop] size].width,
[some_bitmap[loop] size].height, 0, GL_RGB, GL_UNSIGNED_BYTE, [some_bitmap[loop] bitmapData]);

[some_bitmap[loop] release];
}
}

Ian Kerr
2002.08.20, 06:30 PM
Also not that you're probably going to want to keep track of the values in

GLuint texture[2];

rather than declaring them in the function. Unless you just put them there as an example...

Iceman
2002.08.21, 08:00 PM
Thanks so much I finally got it working with your code here's what I did:

- (void)loadGLTextures {
NSString *path[2];
NSImage *some_image[2];
NSBundle *bundle[2];
NSBitmapImageRep *some_bitmap[2];

for( loop = 0; loop < 2; loop++ ) {
bundle[loop] = [NSBundle bundleForClass:[self class]];

path[0] = [bundle[0] pathForResource:@"grass"ofType:@"tif"];
path[1] = [bundle[1] pathForResource:@"wall"ofType:@"tif"];

some_image[loop] = [[NSImage alloc]initWithContentsOfFile: path[loop]];

some_bitmap[loop] = [[NSBitmapImageRep alloc]initWithData:
[some_image[loop] TIFFRepresentation]];
}

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(2, &texture[0]);

for(loop = 0; loop < 2; loop++) {
glBindTexture(GL_TEXTURE_2D, texture[loop]);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, [some_bitmap[loop] size].width,
[some_bitmap[loop] size].height, 0, GL_RGB, GL_UNSIGNED_BYTE, [some_bitmap[loop] bitmapData]);

[some_bitmap[loop] release];
}
}


Yes Ian I just put the GLuint texture[2]; there so everything was together. Also is there any way I can get it to detect if the image is transparent and change the GL_RGB to GL_RGBA?

Thank you again,
Iceman

Iceman
2002.08.22, 07:00 PM
Ok I think I got it figured out thanks to one of those great NeHe tutorials. My problem is I keep getting these errors:

2002-08-22 18:39:00.766 Project UFO 0.4.4[510] *** -[MyCustomGLView pathForResource: ofType:]: selector not recognized
2002-08-22 18:39:00.768 Project UFO 0.4.4[510] An uncaught exception was raised
2002-08-22 18:39:00.768 Project UFO 0.4.4[510] *** -[MyCustomGLView pathForResource: ofType:]: selector not recognized
2002-08-22 18:39:00.768 Project UFO 0.4.4[510] *** Uncaught exception: <NSInvalidArgumentException> *** -[MyCustomGLView pathForResource: ofType:]: selector not recognized

Project UFO 0.4.4.app has exited with status 255.


This is the code I'm using to load the GL_RGBA and GL_RGB textures:

- (void)loadGLTextures {
NSString *path[3];
NSImage *some_image[3];
NSBundle *bundle[3];
NSBitmapImageRep *some_bitmap[3];

GLenum texFormat[3];

for( loop = 0; loop < 3; loop++ ) {
bundle[loop] = [NSBundle bundleForClass:[self class]];

path[0] = [bundle[0] pathForResource:@"grass"ofType:@"tif"];
path[1] = [bundle[1] pathForResource:@"wall"ofType:@"tif"];
path[2] = [bundle[2] pathForResource:@"black_hole"ofType:@"tif"];

texFormat[0] = GL_RGB;
texFormat[1] = GL_RGB;
texFormat[2] = GL_RGBA;

some_image[loop] = [[NSImage alloc]initWithContentsOfFile: path[loop]];

some_bitmap[loop] = [[NSBitmapImageRep alloc]initWithData:
[some_image[loop] TIFFRepresentation]];
}

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(3, &texture[0]);

for(loop = 0; loop < 3; loop++) {
glBindTexture(GL_TEXTURE_2D, texture[loop]);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, [some_bitmap[loop] size].width,
[some_bitmap[loop] size].height, 0, texFormat[loop], GL_UNSIGNED_BYTE, [some_bitmap[loop] bitmapData]);

[some_bitmap[loop] release];
}
}


Please help me,
Iceman

Jeff Binder
2002.08.22, 07:19 PM
There are some odd things going on with the loops. In the first loop, you set the value of bundle[0], then use all three values in the array. Then you set bundle[1] and again use all three. Thus you're sending messages to uninitialized pointers three times. Try this (untested!):

- (void)loadGLTextures {
NSString *paths[3];
GLenum texFormats[3];
GLint loop;

NSBundle *bundle = [NSBundle mainBundle];

path[0] = [bundle pathForResource:@"grass"ofType:@"tif"];
path[1] = [bundle pathForResource:@"wall"ofType:@"tif"];
path[2] = [bundle pathForResource:@"black_hole"ofType:@"tif"];

texFormat[0] = GL_RGB;
texFormat[1] = GL_RGB;
texFormat[2] = GL_RGBA;

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(3, &texture[0]);

for (loop = 0; loop < 3; loop++) {
NSImage *image = [[NSImage alloc] initWithContentsOfFile: path[loop]];
NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithData:
[image TIFFRepresentation]];
NSSize size = [imageRep size];

glBindTexture(GL_TEXTURE_2D, texture[loop]);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.width, size.height, 0,
texFormat[loop], GL_UNSIGNED_BYTE, [imageRep bitmapData]);

[imageRep release];
[image release];
}
}

Iceman
2002.08.23, 06:18 PM
Cool!:cool: I put the alloc in the other loop because I thought the allocating was the problem, but I should have just taken the bundle out of the loop like you did. Thanks so much it works great.

Thanks again,
Iceman