akira108 - 8 months ago 130

iOS Question

I'm trying to achieve Augmented Reality with

`SceneKit`

I got a intrinsic camera matrix and a extrinsic matrix by estimating pose of a marker, using ARuco (OpenCV augmented reality library).

And I set up the

`SCNCamera`

`projectionTransform`

Normally in

`OpenGL`

So I calculated inverse matrix of the extrinsic matrix to get

And I think I've got correct camera's position by the inverse matrix which contains rotation and translate matrix.

However I cannot get camera's

Do you have any ideas?

Answer

SceneKit has the same view matrixes that you've come across in OpenGL, they're just a little hidden until you start toying with shaders. A little too hidden IMO.

You seem to have most of this figured out. The projection matrix comes from your camera `projectionTransform`

, and the view matrix comes from the inverse of your camera matrix `SCNMatrix4Invert(cameraNode.transform)`

. In my case everything was in world coordinates making my model matrix a simple identity matrix.

The code I ended up using to get the classic model-view-projection matrix was something like...

```
let projection = camera.projectionTransform()
let view = SCNMatrix4Invert(cameraNode.transform)
let model = SCNMatrix4Identity
let viewProjection = SCNMatrix4Mult(view, projection)
let modelViewProjection = SCNMatrix4Mult(model, viewProjection)
```

For some reason I found `SCNMatrix4Mult(...)`

took arguments in a different order than I was expecting (eg; opposite to `GLKMatrix4Multiply(...)`

).

I'm still not 100% on this, so would welcome edits/tips. Using this method I was unable to get the SceneKit MVP matrix (as passed to shader) to match up with that calculated by the code above... but it was close enough for what I needed.

Source (Stackoverflow)