textures in an array

Member
Posts: 196
Joined: 2002.04
Post: #1
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:
Code:
- (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
Quote this message in a reply
Member
Posts: 49
Joined: 2002.05
Post: #2
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];
}
}
Quote this message in a reply
Member
Posts: 49
Joined: 2002.05
Post: #3
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...
Quote this message in a reply
Member
Posts: 196
Joined: 2002.04
Post: #4
Thanks so much I finally got it working with your code here's what I did:
Code:
- (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
Quote this message in a reply
Member
Posts: 196
Joined: 2002.04
Post: #5
Ok I think I got it figured out thanks to one of those great NeHe tutorials. My problem is I keep getting these errors:
Code:
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:
Code:
- (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
Quote this message in a reply
Jeff Binder
Unregistered
 
Post: #6
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!):

Code:
- (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];
    }
}
Quote this message in a reply
Member
Posts: 196
Joined: 2002.04
Post: #7
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
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Vertex Array &amp; Interleaved Array troubles TomorrowPlusX 5 5,444 Nov 17, 2007 09:59 AM
Last Post: TomorrowPlusX