简体   繁体   English

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

[英]How can I combine SceneKit with existing OpenGL using SCNRenderer?

I would like to use SceneKit's SCNRenderer to draw a few models into my existing GLKit OpenGL ES 3.0 application and match my existing modelViewProjection transform. 我想使用SceneKit的SCNRenderer将一些模型绘制到我现有的GLKit OpenGL ES 3.0应用程序中,并匹配我现有的modelViewProjection变换。 When I load a SceneKit scene and render it using SCNRenderer the SceneKit camera transformations seem to be ignored and I just get a default bounding box view. 当我加载SceneKit场景并使用SCNRenderer渲染它时,SceneKit摄像机转换似乎被忽略,我只是得到一个默认的边界框视图。 More generally of course I would prefer to supply my own matrix and I am not sure how to do that. 更一般地说,我更愿意提供自己的矩阵,我不知道该怎么做。

//
// 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)

I see that there is a projectionTransform exposed on the SCN Camera and I have tried manipulating that as well, but with no results. 我看到在SCN相机上暴露了一个projectionTransform,我也尝试过操作它,但没有结果。 I'm also guessing that that is just literally a projection transform and not expecting the full modelViewProjection transform. 我也猜测这只是一个投影变换而不是期望完整的modelViewProjection变换。

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

Does anyone have an example of mixing SceneKit into OpenGL drawing in this way? 有没有人有这样的方式将SceneKit混合到OpenGL绘图中? Thanks. 谢谢。

EDIT: 编辑:

Bobelyuk's reference to the example is on point and has helped me solve half of my problem so far. Bobelyuk对这个例子的参考是关键的,并帮助我解决了目前为止我的一半问题。 It turns out that although I was adding the camera node I had failed to set the camera node as the 'pointOfView': 事实证明,虽然我添加了摄像头节点,但我未能将摄像头节点设置为'pointOfView':

scene.pointOfView = cameraNode

The example appears to show how to take an opengl matrix and invert it to set it on the camera, however as of yet I have not been able to get this to work with my code. 该示例似乎显示如何取一个opengl矩阵并将其反转以在相机上设置它,但是到目前为止我还没有能够使用我的代码。 I will update again with a code example shortly. 我将很快再次使用代码示例进行更新。

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

Ultimately it turned out to be very simple. 最终它变得非常简单。 In addition to setting the pointOfView to the camera node I just needed to properly set the camera transform and camera projection transform to my own matrices. 除了将pointOfView设置到摄像机节点之外,我只需要将摄像机变换和摄像机投影变换正确地设置为我自己的矩阵。

Here is the updated example: 这是更新的示例:

//
// 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)

For thos new to this: The model view matrix is the matrix that orients your scene, eg composing GLKMatrix4Scale and GLKMatrix4Rotate calls. 对于新手:模型视图矩阵是定向场景的矩阵,例如组成GLKMatrix4Scale和GLKMatrix4Rotate调用。 The projection matrix is the one that sets your view frustrum and point of view, eg by composing calls to GLKMatrix4MakePerspective and GLKMatrix4MakeLookAt calls. 投影矩阵是设置视图截头和视点的矩阵,例如通过组合调用GLKMatrix4MakePerspective和GLKMatrix4MakeLookAt调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM