Loading textures from bitmaps

Apprentice
Posts: 18
Joined: 2006.12
Post: #1
I started working with GLUT for making my game and have been following all the tutorials and everythings been working until now. I want to be able to load a texture from a bmp or any other image type so i can make a texture out of it to bind to my GLUT objects. I found this code:

Code:
static char *circles[] = {
  "....xxxx........",
  "..xxxxxxxx......",
  ".xxxxxxxxxx.....",
  ".xxx....xxx.....",
  "xxx......xxx....",
  "xxx......xxx....",
  "xxx......xxx....",
  "xxx......xxx....",
  ".xxx....xxx.....",
  ".xxxxxxxxxx.....",
  "..xxxxxxxx......",
  "....xxxx........",
  "................",
  "................",
  "................",
  "................",
};

static void
makeTexture(void)
{
  GLubyte floorTexture[16][16][3];
  GLubyte *loc;
  int s, t;

  /* Setup RGB image for the texture. */
  loc = (GLubyte*) floorTexture;
  for (t = 0; t < 16; t++) {
    for (s = 0; s < 16; s++) {
      if (circles[t][s] == 'x') {
    /* Nice green. */
        loc[0] = 0x1f;
        loc[1] = 0x8f;
        loc[2] = 0x1f;
      } else {
    /* Light gray. */
        loc[0] = 0xaa;
        loc[1] = 0xaa;
        loc[2] = 0xaa;
      }
      loc += 3;
    }
  }

  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
      GL_LINEAR_MIPMAP_LINEAR);
    gluBuild2DMipmaps(GL_TEXTURE_2D, 3, 16, 16,
      GL_RGB, GL_UNSIGNED_BYTE, floorTexture);
      
}

i makes an RGB image from the array given and makes the texture then i just used:
Code:
glEnable(GL_TEXTURE_2D);
gluQuadricTexture(qobj,true);
to wrap the texture around my objects. It worked fine

I want to be able to load from a image i edited now and not just make arrays. I tryed looking for code for loading images but all i could find was code that used AUX_RGBImageRec or something... It didnt work because the it wont recognize AUX_RGBImageRec... I'm kinda confused because im making a glut app but i dont know what im looking for when i search for code to do things like loading bitmaps and stuff. Do i look for carbon C++ stuff? what kind of code is it that loads bitmaps?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
If you search the boards, you'll find plenty of code using QuickTime (not recommended for new development), NSBitmapImageRep (never recommended), ImageIO or Quartz, libpng, or libjpeg, to suit your tastes.
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #3
You're going to have to use some sort of 3rd party library to do this. (I suppose you could do this yourself, but... don't) I suggest libpng.
Quote this message in a reply
Apprentice
Posts: 18
Joined: 2006.12
Post: #4
So is libpng is a standard c++ library I can include? Oh and can you make a png a texture once you load it?
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #5
Actually, libpng is a C library, but it can be used with C++ without any extra effort. If you search the forums, as well as Google if you don't find enough, you'll find everything you need.
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #6
Code:
    NSString * imagePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"testimage.jpg"]; // or png bmp tga tiff etc etc etc
    NSLog(imagePath);
    
    CGImageRef image;
    CGImageSourceRef image_source;
    int width, height;
    
    image_source = CGImageSourceCreateWithURL((CFURLRef)[NSURL fileURLWithPath:imagePath], NULL);
    if(!image_source)
        NSLog(@"Image does not exist");
    image = CGImageSourceCreateImageAtIndex(image_source, 0, NULL);
    CFRelease(image_source);
    
    width = CGImageGetWidth(image);
    height = CGImageGetHeight(image);
    
    NSLog(@"width, height = %d, %d", width, height);
    
    unsigned char * image_data = malloc(width*height*4);
    CGContextRef bitmapRenderContext = CGBitmapContextCreate(image_data, width, height, 8, width*4, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedLast);
    CGContextDrawImage(bitmapRenderContext, (CGRect){{0, 0}, {width, height}}, image);
    CGContextRelease(bitmapRenderContext);
    
    GLuint texture;
    glGenTextures(1, &texture);
    
    glBindTexture(GL_TEXTURE_2D, texture);
    // GL_RGB8, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    
    free(image_data);

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Loading and using textures with alpha in OpenGL with Cocoa corporatenewt 4 6,234 Dec 8, 2007 02:06 PM
Last Post: Malarkey
  Drawing bitmaps in OpenGL MacGoober 21 21,851 Sep 22, 2007 05:50 PM
Last Post: MikeC
  loading textures - cocoa openGL mDmarco 20 9,404 Aug 28, 2007 08:48 PM
Last Post: OneSadCookie
  Best way to deal with loading textures? bronxbomber92 14 5,441 May 4, 2007 05:31 PM
Last Post: OneSadCookie
  libpng loading junk at bottom of my OpenGL textures...? BinarySpike 6 6,056 Apr 19, 2007 12:20 PM
Last Post: BinarySpike