如何使用 SCNRenderer 将 SceneKit 与现有的 OpenGL 结合起来?

How can I combine SceneKit with existing OpenGL using SCNRenderer?

我想使用 SceneKit 的 SCNRenderer 将一些模型绘制到我现有的 GLKit OpenGL ES 3.0 应用程序中并匹配我现有的 modelViewProjection 转换。当我加载一个 SceneKit 场景并使用 SCNRenderer 渲染它时,SceneKit 相机转换似乎被忽略了,我只得到一个默认的边界框视图。更一般地说,当然我更愿意提供我自己的矩阵,但我不确定该怎么做。

//
// Called from my glkView(view: GLKView, drawInRect rect: CGRect)
//

// Load the scene
let scene = SCNScene(named: "art.scnassets/model.scn")!

// Create and add regular SceneKit camera to the scene
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.position = SCNVector3(x: 0, y: 0, z: 10)
scene.rootNode.addChildNode(cameraNode)

// Render into the current OpenGL context
let renderer = SCNRenderer( context: EAGLContext.currentContext(), options: nil )
renderer.scene = scene
renderer.renderAtTime(0)

我看到 SCN 相机上暴露了一个 projectionTransform,我也尝试对其进行操作,但没有结果。我还猜测这只是字面上的投影变换,而不是期待完整的 modelViewProjection 变换。

// Probably WRONG
cameraNode.camera!.projectionTransform = SCNMatrix4FromGLKMatrix4( modelViewProjectionTransform )

有没有人有用这种方式将SceneKit混合到OpenGL绘图中的例子?谢谢

编辑:

Bobelyuk 对示例的引用很到位,到目前为止已经帮助我解决了一半的问题。事实证明,虽然我添加了相机节点,但我未能将相机节点设置为 'pointOfView':

scene.pointOfView = cameraNode

该示例似乎展示了如何获取 opengl 矩阵并将其反转以将其设置在相机上,但是到目前为止我还无法将其用于我的代码。稍后我将再次更新代码示例。

http://qiita.com/akira108/items/a743138fca532ee193fe 在这里你可以找到如何结合 SCNRendererOpenGLContext

最后发现很简单。除了将 pointOfView 设置为相机节点外,我只需要将相机变换和相机投影变换正确设置为我自己的矩阵。

这是更新后的示例:

//
// Called from my glkView(view: GLKView, drawInRect rect: CGRect)
//

// Load the scene
let scene = SCNScene(named: "art.scnassets/model.scn")!
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
scene.rootNode.addChildNode(cameraNode)

// Set my own matrices
cameraNode.transform = SCNMatrix4Invert(SCNMatrix4FromGLKMatrix4(myModelViewMatrix))
cameraNode.camera!.projectionTransform = SCNMatrix4FromGLKMatrix4(myProjectionMatrix)

// Render into the current OpenGL context
let renderer = SCNRenderer( context: EAGLContext.currentContext(), options: nil )
renderer.scene = scene
renderer.pointOfView = cameraNode  // set the point of view for the scene
renderer.renderAtTime(0)

对于那些新手:模型视图矩阵是定位场景的矩阵,例如编写 GLKMatrix4Scale 和 GLKMatrix4Rotate 调用。投影矩阵是设置您的视锥和视点的矩阵,例如通过编写对 GLKMatrix4MakePerspective 和 GLKMatrix4MakeLookAt 调用的调用。