Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a pixel buffer Objective-C

I want to create an NSOpenGLPixelBuffer for offscreen rendering. For now, I'm even unable to init it. Here's the code:

NSOpenGLPixelFormatAttribute attribs[] = {
    NSOpenGLPFAPixelBuffer,
    NSOpenGLPFANoRecovery,
    NSOpenGLPFAAccelerated,
    NSOpenGLPFADepthSize, 24,
    (NSOpenGLPixelFormatAttribute) 0
};

NSOpenGLPixelFormat* format = [[[NSOpenGLPixelFormat alloc] initWithAttributes:attribs] autorelease];
NSOpenGLPixelBuffer* pixBuf = [[NSOpenGLPixelBuffer alloc]
                               initWithTextureTarget: GL_TEXTURE_2D
                               textureInternalFormat: GL_RGBA
                               textureMaxMipMapLevel: 0
                               pixelsWide: 32
                               pixelsHigh: 32];

NSLog(@"pbuf address: %p", pixBuf);

I've tried to fiddle with pixel format and width, height but nothing seems to work. I repeatedly get nil for the pixBuf. I'm using Xcode 9 on MacOS 10.13. It says the NSOpenGLPixelBuffer is deprecated but it should still work, right?

like image 308
alDiablo Avatar asked Nov 05 '17 05:11

alDiablo


2 Answers

NSOpenGLPixelBuffer has been deprecated since OS X Lion, which itself dates back to 2011. No wonder you`re getting nil.

Here, Apple propose a new approach to drawing into textures:

Finally note that the entirety of the NSOpenGLPixelBuffer class should be considered deprecated. Use IOSurface in conjunction with GL framebuffer objects as a replacement.

See this example to get a grasp of how it needs to be.

like image 77
hidefromkgb Avatar answered Oct 23 '22 21:10

hidefromkgb


Might be help following code. you replace GL_TEXTURE_2D to this GL_TEXTURE_RECTANGLE_EXT.

// Create an OpenGL pixel buffer
pixBuf = [[NSOpenGLPixelBuffer alloc] initWithTextureTarget:GL_TEXTURE_RECTANGLE_EXT textureInternalFormat:GL_RGBA textureMaxMipMapLevel:0 pixelsWide:32 pixelsHigh:32];
NULL_ERROR_EXIT(pixBuf, "Unable to create NSOpenGLPixelBuffer");
like image 29
BuLB JoBs Avatar answered Oct 23 '22 23:10

BuLB JoBs