I have this code in OpenGL:

Vuforia::Matrix44F modelViewProjection;

VuforiaApplicationUtils::translatePoseMatrix(0.0f, 0.0f, self.scale, &Vuforia::modelViewMatrix.data[0]);
VuforiaApplicationUtils::scalePoseMatrix(self.scale, self.scale, self.scale, &Vuforia::modelViewMatrix.data[0]);
VuforiaApplicationUtils::multiplyMatrix(&projectionMatrix.data[0], &Vuforia::modelViewMatrix.data[0], &modelViewProjection.data[0]);

And the modelViewProjection gets used like this:

glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE, (const GLfloat*)&modelViewProjection.data[0]);

With mvpMatrixHandle being a piece of code in the shader:

mvpMatrixHandle = glGetUniformLocation(shaderProgramID, "modelViewProjectionMatrix");

And in the shader itself:

gl_Position = modelViewProjectionMatrix * vertexPosition;

Now I try to convert this to SceneKit code (never used it before, so I might miss a big part of my SceneKit code)

// Setup
let plane = SCNNode(mdlObject: MDLAsset(url: Bundle.main.url(forResource: "Bubble", withExtension: "obj")!).object(at: 0))

let camera = SCNCamera()
plane.camera = camera

// Each frame
modelViewMatrix = SCNMatrix4Translate(modelViewMatrix, 0, 0, scale)
modelViewMatrix = SCNMatrix4Scale(modelViewMatrix, scale, scale, scale)
let modelViewProjection = SCNMatrix4Mult(arrayToMatrix(pose: projectionMatrix), modelViewMatrix)

//camera.projectionTransform = modelViewProjection
scene.rootNode.childNodes[0].transform = modelViewProjection

As you can see, I have also tried to set this as the projectionTransform of the camera, but that did not work either.

My result is the most annoying to debug: nothing gets rendered using the SceneKit code, while the OpenGL code works.

  • \$\begingroup\$ Are you intending to translate by the scale factor? That seems a bit odd. \$\endgroup\$ Mar 20 '17 at 21:58
  • \$\begingroup\$ @user1118321 well the original code did that as well, so I left it in. \$\endgroup\$ Mar 20 '17 at 22:01

I am not using Vuforia but ARToolkit and I had the same kind of issue. The main solution is to realize that SceneKit is a graph of nodes, therefore applying a modelViewProjection to a node transform may not may so much sense. The node with the camera is your view.

Instead, you should apply your projectionMatrix to the SCNCamera's projectionTransform property, and apply your modelView/view matrix to a different childNode that contains your "AR" scene.

That's for a context with an SCNCamera that you can control, not in an ARSCNView (ARKit) where the ARCamera updates as you move your device.

Hope this helps.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.