我需要使用 SceneKit 从 OpenGL 上下文中获取 CVPixelBufferRef。
创建渲染器
_context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
_renderer = [SCNRenderer rendererWithContext:_context options:nil];
_renderer.delegate = self;
_renderer.scene = scene;
然后从OpenGL获取缓冲区
CVPixelBufferRef _targetBuffer;
GLuint frameBuffer = 0;
glGenFramebuffers(1, &frameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
GLuint colorBuffer = 0;
glGenRenderbuffers(1, &colorBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, colorBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, _renderSize.width, _renderSize.height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorBuffer);
GLuint depthBuffer = 0;
glGenRenderbuffers(1, &depthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);
glClearColor(0.0, 0.0, 0.0, 0.0);
glViewport(0, 0, _renderSize.width, _renderSize.height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
[_renderer renderAtTime:0];
glReadPixels(0, 0, _renderSize.width, _renderSize.height, GL_RGBA, GL_UNSIGNED_BYTE, _targetBuffer);
但是在渲染时我收到一个错误:
[SceneKit] 错误:渲染 SceneKit 内容之前的 glError:506