Quick texture loading in Cocoa?
Quote:Originally posted by Mark Levin
I've actually never had rowbytes cause a problem (then again, I'm rarely loading images with unusual dimensions). You can fix it with glPixelStore() anyway.
You can't guarantee to be able to fix it with glPixelStore, though in practice it's probably always possible (glPixelStore only lets you specify rowbytes in pixels, so if the rowbytes is not a multiple of the width of the pixel for some reason, you can't fix it). You still have to add the code to do it, which none of the above samples have...
Quote:And I never did figure out how to load ARGB images directly into GL... What's the accepted method for that?
GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, as the QuickTime code does.
Wow, a lot of feedback! Very, very appreciated. 
I'm in a hurry right now, so I don't have the time to check them out right now, but rest assured that you definitely made me more calm.

I'm in a hurry right now, so I don't have the time to check them out right now, but rest assured that you definitely made me more calm.
oh yeah, and my example code is wrong, you need to draw into a CachedImageRep, not a BitmapImageRep.
Sorry for the confusion.
p.s. OSC, don't you need the extra copy to stretch the image or make it powerof2 anyways? (excluding newer cards. not everybody has newer cards
Sorry for the confusion.
p.s. OSC, don't you need the extra copy to stretch the image or make it powerof2 anyways? (excluding newer cards. not everybody has newer cards
---Kelvin--
15.4" MacBook Pro revA
1.83GHz/2GB/250GB
Hi all, this is the code I use, and it works! It handles most things no problem, it does however make several assumptions, but for me it works for all the jpegs, pngs and gifs I used.
[SOURCECODE]GLuint GetOpenGLTexture(NSImage *image)
{
NSArray *reps;
NSBitmapImageRep *bitmapRep;
int i, samples, bits, width, height;
BOOL alpha;
GLuint texName;
GLenum type, format;
reps = [image representations];
for(i=0;i<[reps count];i++)
{
if([[reps objectAtIndex:i] isKindOfClass:[NSBitmapImageRep class]])
{
bitmapRep = [reps objectAtIndex:i];
if([bitmapRep isPlanar])
{
NSLog(@"GetOpenGLTexture: Can't handle planar images.");
return -1;
}
// get image data
samples = [bitmapRep samplesPerPixel];
bits = [bitmapRep bitsPerPixel];
alpha = [bitmapRep hasAlpha];
width = [bitmapRep pixelsWide];
height = [bitmapRep pixelsHigh];
}
}
glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_2D, texName);
if(alpha)
bits *= 0.75;
switch(bits)
{
case 24:
type = GL_UNSIGNED_BYTE;
break;
case 48:
type = GL_UNSIGNED_SHORT;
break;
case 96:
type = GL_UNSIGNED_INT;
break;
default:
NSLog(@"GetOpenGLTexture: Can't handle bits per pixel of %d.", bits);
return -1;
}
if(alpha)
format = GL_RGBA;
else
format = GL_RGB;
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, [bitmapRep bitmapData]);
return texName;
}
[/SOURCECODE]
[SOURCECODE]GLuint GetOpenGLTexture(NSImage *image)
{
NSArray *reps;
NSBitmapImageRep *bitmapRep;
int i, samples, bits, width, height;
BOOL alpha;
GLuint texName;
GLenum type, format;
reps = [image representations];
for(i=0;i<[reps count];i++)
{
if([[reps objectAtIndex:i] isKindOfClass:[NSBitmapImageRep class]])
{
bitmapRep = [reps objectAtIndex:i];
if([bitmapRep isPlanar])
{
NSLog(@"GetOpenGLTexture: Can't handle planar images.");
return -1;
}
// get image data
samples = [bitmapRep samplesPerPixel];
bits = [bitmapRep bitsPerPixel];
alpha = [bitmapRep hasAlpha];
width = [bitmapRep pixelsWide];
height = [bitmapRep pixelsHigh];
}
}
glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_2D, texName);
if(alpha)
bits *= 0.75;
switch(bits)
{
case 24:
type = GL_UNSIGNED_BYTE;
break;
case 48:
type = GL_UNSIGNED_SHORT;
break;
case 96:
type = GL_UNSIGNED_INT;
break;
default:
NSLog(@"GetOpenGLTexture: Can't handle bits per pixel of %d.", bits);
return -1;
}
if(alpha)
format = GL_RGBA;
else
format = GL_RGB;
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, [bitmapRep bitmapData]);
return texName;
}
[/SOURCECODE]
Quote:Originally posted by kelvin
p.s. OSC, don't you need the extra copy to stretch the image or make it powerof2 anyways? (excluding newer cards. not everybody has newer cards![]()
gluBuild2DMipmaps takes care of rescaling NPOT images, and since I'm using that it's up to it to do the copy.
Most of the time, though, you're just going to check the image dimensions and reject any NPOT images anyway, so I don't really think you have an argument here...
Heh, I started to plug in all your code submissions one by one, but all of them ended up drawing a black quad. After 1.5 hours of looking through _everything_ I noticed that I was drawing with black color and color blended the textures with black. 
Thanks for all your help, guys!

Thanks for all your help, guys!
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| iPhone texture loading issue | yotryu | 0 | 3,145 |
Mar 11, 2009 03:40 AM Last Post: yotryu |
|
| Texture Mapping: Loading a texture from a .bmp file? | ishrock | 5 | 5,083 |
Dec 13, 2008 09:27 AM Last Post: ThemsAllTook |
|
| OpenGL Texture Loading & Sprites | corporatenewt | 2 | 10,640 |
Jan 30, 2008 12:39 PM Last Post: ynda20 |
|
| Texture Loading in Cocoa... | dave05 | 3 | 7,694 |
Dec 11, 2007 03:12 AM Last Post: DoG |
|
| Loading and using textures with alpha in OpenGL with Cocoa | corporatenewt | 4 | 5,113 |
Dec 8, 2007 02:06 PM Last Post: Malarkey |
|

