无法使用 SceneKit 从 OpenGL 获取 CVPixelBufferRef 数据
Posted
技术标签:
【中文标题】无法使用 SceneKit 从 OpenGL 获取 CVPixelBufferRef 数据【英文标题】:Cannot get CVPixelBufferRef data from OpenGL with SceneKit 【发布时间】:2018-11-24 11:08:51 【问题描述】:我需要使用 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
【问题讨论】:
【参考方案1】:正确设置像素和深度缓冲区:
glEnable(GL_DEPTH_TEST);
glEnable(GL_STENCIL_TEST);
glGenFramebuffers(1, &_frameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, _frameBuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, CVOpenGLESTextureGetName(_texture), 0);
glGenRenderbuffers(1, &_depthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, _depthBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8_OES, _renderSize.width, _renderSize.height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, _depthBuffer);
【讨论】:
以上是关于无法使用 SceneKit 从 OpenGL 获取 CVPixelBufferRef 数据的主要内容,如果未能解决你的问题,请参考以下文章
用于 SceneKit 内容的正确法线贴图格式 - DirectX 还是 OpenGL?
SceneKit - 从 SCNView 获取渲染场景作为 MTLTexture 而不使用单独的 SCNRenderer