Obtenir la texture Gluint à partir de NSImage
-
21-12-2019 - |
Question
je travaille sur un mac os
application utilisant Syphon-SDK
.
Syphon
est un service qui « publie » des données de rendu, les mettant à la disposition des outils de gestion vidéo pour les utiliser comme source d'entrée.
L'exemple de projet (disponible ici : http://syphon.v002.info/) obtient les données de rendu d'un Quartz Composer
fichier et le publie.
Sur mon projet, je dois rendre un statique NSImage
, et pour cela je dois utiliser OpenGL
, puis publiez la texture obtenue.
La solution
voici le truc :
- (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];
}
j'espère que ça aidera quelqu'un d'autre
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow