SCNRenderer

时间:2016-03-22 15:20:52

标签: ios opengl-es-2.0 scenekit glkit

我遇到了非常奇怪的情况,这让我整整困惑。基本上我想做的是在屏幕外渲染SCNScene(不使用[SCNView快照])。我遇到了几个stackoverflow问题而没有任何答案,如this

在玩了几个小时之后,我发现这是EAGLContext初始化的问题。这是我从Apple复制的内容:

_context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
int width = 640;
int height = 480;

[EAGLContext setCurrentContext: _context];

glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);

glGenRenderbuffers(1, &colorRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer);

glGenRenderbuffers(1, &depthRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderbuffer);

GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER) ;
if(status != GL_FRAMEBUFFER_COMPLETE) {
    NSLog(@"failed to make complete framebuffer object %x", status);
}

渲染它

SCNScene * scnScene = [SCNScene scene];

glViewport(0, 0, width, height);
[scnScene.rootNode addChildNode: _node];
SCNRenderer *scnRender = [SCNRenderer rendererWithContext:_context options:nil];
scnRender.scene = scnScene;
SCNNode *cameraNode = [SCNNode node];
SCNCamera *mycamera = [SCNCamera camera];
cameraNode.camera = mycamera;
cameraNode.position = SCNVector3Make(0, 0, 0);

GLKMatrix4 mat = GLKMatrix4MakeOrtho(-1, 1,
                                     -5, 5,
                                     0.01, 100); // z range arbitrary
[cameraNode.camera setProjectionTransform:SCNMatrix4FromGLKMatrix4(mat)];
scnRender.pointOfView = cameraNode;
scnRender.delegate = self;
[scnRender render];

_node包含应该呈现的模型。

使用这些代码,它会呈现空场景(透明的UIImage)。

但是,如果我以这样的hacky方式初始化EAGLContext:

_context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
_glkView = [[GLKView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
_glkView.context = _context;
[_glkView display];

_glkView甚至不需要添加到ViewController的视图或绘制任何东西,并且[_glkView display]是需要的,因为当GLKView实际初始化其EAGLContext的frameBuffer和renderBuffer时。

使用GLKView初始化的这个新_context,SCNRenderer按预期工作。

我真的很困惑为什么手动初始化的renderBuffer,frameBuffer不能与SCNRenderer一起使用,以及为什么它在用GLKView初始化时有效。

Gist这里有完整的功能。

0 个答案:

没有答案