【问题标题】:How can I combine SceneKit with existing OpenGL using SCNRenderer?如何使用 SCNRenderer 将 SceneKit 与现有 OpenGL 结合?
【发布时间】:2017-05-14 01:26:04
【问题描述】:

我想使用 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 矩阵并将其反转以将其设置在相机上,但是到目前为止,我还不能让它与我的代码一起使用。稍后我将再次更新代码示例。

【问题讨论】:

    标签: ios opengl-es scenekit glkit


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      最终证明它非常简单。除了将 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 调用。

      【讨论】:

        猜你喜欢
        • 2019-02-01
        • 2010-11-01
        • 2013-09-09
        • 1970-01-01
        • 1970-01-01
        • 2021-05-17
        • 2011-04-29
        • 2016-09-17
        • 1970-01-01
        相关资源
        最近更新 更多