// Generate texture 'texName' from 'theView' in current OpenGL context
-(void)textureFromView:(NSView*)theView textureName:(GLuint*)texName
{
// Bitmap generation from source view
NSBitmapImageRep * bitmap = [NSBitmapImageRep alloc];
int samplesPerPixel = 0;
[theView lockFocus];
[bitmap initWithFocusedViewRect:[theView bounds]];
[theView unlockFocus];
// Set proper unpacking row length for bitmap
glPixelStorei(GL_UNPACK_ROW_LENGTH, [bitmap pixelsWide]);
// Set byte aligned unpacking (needed for 3 byte per pixel bitmaps)
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
// Generate new texture object if none passed in
if (*texName == 0)
glGenTextures (1, texName);
glBindTexture (GL_TEXTURE_RECTANGLE_EXT, *texName);
// Non-mipmap filtering (redundant for texture_rectangle)
glTexParameteri(GL_TEXTURE_RECTANGLE_EXT,
GL_TEXTURE_MIN_FILTER, GL_LINEAR);
samplesPerPixel = [bitmap samplesPerPixel];
// Non-planar, RGB 24 bit bitmap, or RGBA 32 bit bitmap
if(![bitmap isPlanar] &&
(samplesPerPixel == 3 || samplesPerPixel == 4)) {
glTexImage2D(GL_TEXTURE_RECTANGLE_EXT,
0,
samplesPerPixel == 4 ? GL_RGBA8 : GL_RGB8,
[bitmap pixelsWide],
[bitmap pixelsHigh],
0,
samplesPerPixel == 4 ? GL_RGBA : GL_RGB,
GL_UNSIGNED_BYTE,
[bitmap bitmapData]);
} else {
/*
Error condition...
The above code handles 2 cases (24 bit RGB and 32 bit RGBA),
it is possible to support other bitmap formats if desired.
So we'll log out some useful information.
*/
NSLog (@"-textureFromView: Unsupported bitmap data
format: isPlanar:%d, samplesPerPixel:%d, bitsPerPixel:%d,
bytesPerRow:%d, bytesPerPlane:%d",
[bitmap isPlanar],
[bitmap samplesPerPixel],
[bitmap bitsPerPixel],
[bitmap bytesPerRow],
[bitmap bytesPerPlane]);
}
// Clean up
[bitmap release];
}
|