我想将NSString呈现为单色OpenGL纹理。我在iOS上找到了很多关于如何实现这一点的例子,但我很难找到适用于OSX的代码片段。
我知道如何做最后一点,将纹理上传到GPU,但我如何获得原始(每像素8位)数据?
答案 0 :(得分:2)
在Apple的GLString.m中查看Cocoa OpenGL code sample。您可以轻松修改它以生成单色纹理。相关修改会产生如下代码:
NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL pixelsWide:frameSize.width pixelsHigh:frameSize.height
bitsPerSample:8 samplesPerPixel:1 hasAlpha:NO isPlanar:NO colorSpaceName:NSDeviceWhiteColorSpace bitmapFormat:0 bytesPerRow:frameSize.width bitsPerPixel:8];
NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithBitmapImageRep:bitmap];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:context];
…
[string drawAtPoint:NSMakePoint(marginSize.width, marginSize.height)]; // draw at offset position
…
[NSGraphicsContext restoreGraphicsState];
if (0 == texName)
glGenTextures (1, &texName);
glBindTexture (GL_TEXTURE_RECTANGLE_EXT, texName);
glTexSubImage2D(GL_TEXTURE_RECTANGLE_EXT, 0, 0, 0, texSize.width, texSize.height, GL_LUMINANCE, GL_UNSIGNED_BYTE, [bitmap bitmapData]);
关键部分是: