Creating a pixel buffer Objective-C

Viewed 544

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?

2 Answers

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");
Related