texture from NSBitmapRep
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
thanks dudes.
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.
You've got some of the arguments to glTexImage2D() mixed up. Here's what I use, adapted a bit for clarity:
This compiles, but I didn't try this version of it. HTH.
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.
