gpt4 book ai didi

objective-c - 从 NSImage 获取 GLuint 纹理

转载 作者:行者123 更新时间:2023-12-02 21:25:04 24 4
gpt4 key购买 nike

我正在使用 Syphon-SDK 开发 mac os 应用程序。Syphon 是一项“发布”渲染数据的服务,使其可供视频处理工具用作输入源。

示例项目(可在此处获取: http://syphon.v002.info/ )从 Quartz Composer 文件获取渲染数据并发布它。

在我的项目中,我必须渲染静态 NSImage,为此我必须使用 OpenGL,然后发布生成的纹理。

最佳答案

事情是这样的:

- (void)textureFromImage:(NSImage*)theImg textureName:(GLuint*)texName
{
NSBitmapImageRep* bitmap = [NSBitmapImageRep alloc];
int samplesPerPixel = 0;
NSSize imgSize = [theImg size];

[theImg lockFocus];
[bitmap initWithFocusedViewRect:
NSMakeRect(0.0, 0.0, imgSize.width, imgSize.height)];
[theImg 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 a new texture name if one was not provided.
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];

// Nonplanar, 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
{
// Handle other bitmap formats.
}

// Clean up.
[bitmap release];
}

希望对其他人有帮助

关于objective-c - 从 NSImage 获取 GLuint 纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25019684/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com