iDevGames Forums
texture from NSBitmapRep - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: texture from NSBitmapRep (/thread-7754.html)



texture from NSBitmapRep - type_r503 - May 20, 2002 11:24 AM

Hi all,

I seem to think there was a previous thread explaining this but I can't find it. I was wondering what the code was for creating a texture from an NSBitmapimageRep. What I have is

Code:



    image = [NSBitmapImageRep imageRepWithContentsOfFile: [motionArray objectAtIndex:z]];
                 //bind data to texture

               if(!image)
                   exit(20);
                              
              
                //create the texture
                glGenTextures(1, &spriteData[x-1][y-1][z-1]);
            
                //genreate texture with bitmap data
               glBindTexture(GL_TEXTURE_2D, temp);
                  
                //generate the texture
               glTexImage2D(GL_TEXTURE_2D, 0, 4,
                    [image pixelsWide], [image pixelsHigh],0,
                  GL_RGBA, [image bitsPerSample], [image bitmapData]);  



thanks dudes.


texture from NSBitmapRep - Jeff Binder - May 20, 2002 01:38 PM

You've got some of the arguments to glTexImage2D() mixed up. Here's what I use, adapted a bit for clarity:

Code:
GLuint LoadTexture(NSString *imagePath) {
  NSSize size;
  NSData *data;
  GLuint i;

  if ((data = [NSData dataWithContentsOfFile: imagePath]))
  {
    NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:data];
    
    GLenum mode = (([imageRep samplesPerPixel] == 4) ? GL_RGBA :
                  (([imageRep samplesPerPixel] == 3) ? GL_RGB :
                  (([imageRep samplesPerPixel] == 2) ? GL_LUMINANCE_ALPHA :
                  GL_LUMINANCE)));
                  
    
    GLubyte *image = [imageRep bitmapData];
    GLubyte *data;
    GLuint samples = [imageRep samplesPerPixel];

    size = [imageRep size];
    data = malloc(sizeof(GLubyte) * (GLuint)(size.width * size.height * samples));
    
    //Flip the image to convert to the GL coordinate system.
    for (i = 0; i < size.height; i++) {
      memcpy(data + (GLuint)((size.height - 1 - i) * size.width * samples), image + (UInt32)(i * size.width * samples), (UInt32)(size.width * samples));
    }
    
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)(size.width), (GLsizei)(size.height), 0, mode, GL_UNSIGNED_BYTE, data);
    
    free(data);
  } else {
    NSLog(@"Failed to load image %@", imagePath);
    
    return 0;
  }

  return 1;
}

This compiles, but I didn't try this version of it. HTH.