// 現在の OpenGL コンテキスト内の 'theView' から 'texName' テクスチャを生成
-(void)textureFromView:(NSView*)theView textureName:(GLuint*)texName
{
// ソースビューからビットマップを生成
NSBitmapImageRep * bitmap = [NSBitmapImageRep alloc];
int samplesPerPixel = 0;
[theView lockFocus];
[bitmap initWithFocusedViewRect:[theView bounds]];
[theView unlockFocus];
// 読み取るビットマップの適切な行の長さを設定
glPixelStorei(GL_UNPACK_ROW_LENGTH, [bitmap pixelsWide]);
// 読み取る配列のバイト数を設定 (ビットマップのピクセルあたり 3 バイト必要)
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
// テクスチャが渡されない場合は、新しいテクスチャオブジェクトを生成
if (*texName == 0)
glGenTextures (1, texName);
glBindTexture (GL_TEXTURE_RECTANGLE_EXT, *texName);
// ミップマップなしのフィルタ処理 (texture_rectangle には冗長)
glTexParameteri(GL_TEXTURE_RECTANGLE_EXT,
GL_TEXTURE_MIN_FILTER, GL_LINEAR);
samplesPerPixel = [bitmap samplesPerPixel];
// 非 Planer、RGB 24 ビットビットマップ、または RGBA 32 ビットビットマップ
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 {
/*
エラー状態...
上のコードでは 2 つのケース (24 ビット RGB、および 32 ビット RGBA) が処理されており、
必要であれば、ほかのビットマップ形式のサポートも可能。
いくつかの有用な情報をログに書き出す。
*/
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]);
}
// クリーンアップ処理
[bitmap release];
}
|