Loading textures from bitmaps
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:
i makes an RGB image from the array given and makes the texture then i just used:
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?
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);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?
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.
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.
So is libpng is a standard c++ library I can include? Oh and can you make a png a texture once you load it?
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.
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!
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Loading and using textures with alpha in OpenGL with Cocoa | corporatenewt | 4 | 5,081 |
Dec 8, 2007 02:06 PM Last Post: Malarkey |
|
| Drawing bitmaps in OpenGL | MacGoober | 21 | 19,065 |
Sep 22, 2007 05:50 PM Last Post: MikeC |
|
| loading textures - cocoa openGL | mDmarco | 20 | 8,333 |
Aug 28, 2007 08:48 PM Last Post: OneSadCookie |
|
| Best way to deal with loading textures? | bronxbomber92 | 14 | 4,807 |
May 4, 2007 05:31 PM Last Post: OneSadCookie |
|
| libpng loading junk at bottom of my OpenGL textures...? | BinarySpike | 6 | 5,048 |
Apr 19, 2007 12:20 PM Last Post: BinarySpike |
|

