1

I am trying to combine Vuforia SDK and jMonkeyEngine. The cube is placed on the target (ImageTarget) so far. But when I move the camera the cube moves a little bit too. I want that the cube remains at the center of the target (like the teapot in VuforiaSamples ImageTarget). Do you have an idea how I can solve the problem?

I think this is the relevant code:

public void initForegroundCamera()
{
    foregroundCamera = new Camera(settings.getWidth(), settings.getHeight());

    foregroundCamera.setLocation(new Vector3f(0.0f, 0.0f, 0.0f));

    // Get perspective transformation
    CameraCalibration cameraCalibration = CameraDevice.getInstance().getCameraCalibration(); 

    VideoBackgroundConfig config = Renderer.getInstance().getVideoBackgroundConfig();

    float viewportWidth = config.getSize().getData()[0];
    float viewportHeight = config.getSize().getData()[1];

    float cameraWidth = cameraCalibration.getSize().getData()[0];
    float cameraHeight = cameraCalibration.getSize().getData()[1];

    float screenWidth = settings.getWidth();
    float screenHeight = settings.getHeight();

    Vec2F size = new Vec2F(cameraWidth, cameraHeight);
    Vec2F focalLength = cameraCalibration.getFocalLength();

    float fovRadians = 2 * (float) Math.atan(0.5f * (size.getData()[1] / focalLength.getData()[1]));
    float fovDegrees = fovRadians * 180.0f / (float) Math.PI;
    float aspectRatio = (size.getData()[0] / size.getData()[1]);

    // Adjust for screen / camera size distortion
    float viewportDistort = 1.0f;

    if (viewportWidth != screenWidth)
    {
        viewportDistort = viewportWidth / screenWidth;
        fovDegrees = fovDegrees * viewportDistort;
        aspectRatio = aspectRatio / viewportDistort;
        Log.v(TAG, "viewportDistort: " + viewportDistort + " fovDegreed: " + fovDegrees + " aspectRatio: " + aspectRatio); 
    }

    if (viewportHeight != screenHeight)
    {
        viewportDistort = viewportHeight / screenHeight;
        fovDegrees = fovDegrees / viewportDistort;
        aspectRatio = aspectRatio * viewportDistort;
        Log.v(TAG, "viewportDistort: " + viewportDistort + " fovDegreed: " + fovDegrees + " aspectRatio: " + aspectRatio); 
    }

    setCameraPerspectiveFromVuforia(fovDegrees, aspectRatio);

    setCameraViewportFromVuforia(viewportWidth, viewportHeight, cameraWidth, cameraHeight);

    ViewPort foregroundViewPort = renderManager.createMainView("ForegroundView", foregroundCamera);
    foregroundViewPort.attachScene(rootNode);
    foregroundViewPort.setClearFlags(false, true, false);
    foregroundViewPort.setBackgroundColor(ColorRGBA.Blue);

    sceneInitialized = true;
}

private void ProcessTrackable(TrackableResult result, int i)
{
    // Show the 3D object corresponding on the found trackable
    Spatial model = rootNode.getChild(0);
    model.setCullHint(CullHint.Dynamic);

    Matrix44F modelViewMatrix_Vuforia = Tool.convertPose2GLMatrix(result.getPose());
    Matrix44F inverseMatrix_Vuforia = MathHelpers.Matrix44FInverse(modelViewMatrix_Vuforia);
    Matrix44F inverseTransposedMatrix_Vuforia = MathHelpers.Matrix44FTranspose(inverseMatrix_Vuforia);

    float[] modelViewMatrix = inverseTransposedMatrix_Vuforia.getData();

    // Get camera position
    float cam_x = modelViewMatrix[12];
    float cam_y = modelViewMatrix[13];
    float cam_z = modelViewMatrix[14];

    // Get camera rotation
    float cam_right_x = modelViewMatrix[0];
    float cam_right_y = modelViewMatrix[1];
    float cam_right_z = modelViewMatrix[2];
    float cam_up_x =    modelViewMatrix[4];
    float cam_up_y =    modelViewMatrix[5];
    float cam_up_z =    modelViewMatrix[6];
    float cam_dir_x =   modelViewMatrix[8];
    float cam_dir_y =   modelViewMatrix[9];
    float cam_dir_z =   modelViewMatrix[10];


    setCameraPoseFromVuforia(cam_x, cam_y, cam_z);
    setCameraOrientationFromVuforia(cam_right_x, cam_right_y, cam_right_z, cam_up_x, cam_up_y, cam_up_z, cam_dir_x, cam_dir_y, cam_dir_z);

}

//we modify the left axis of the JME camera to match the coodindate system used by Vuforia
private void setCameraPerspectiveFromVuforia(float fovY, float aspectRatio) 
{

    foregroundCamera.setFrustumPerspective(fovY, aspectRatio, 1.0f, 1000.0f); 
    foregroundCamera.update();
}

private void setCameraPoseFromVuforia(float camX, float camY, float camZ)
{
    foregroundCamera.setLocation(new Vector3f(camX, camY, camZ));
    foregroundCamera.update();
}

private void setCameraOrientationFromVuforia(float camRightX, float camRightY, float camRightZ, float camUpX, float camUpY, float camUpZ, float camDirX, float camDirY, float camDirZ)
{
    foregroundCamera.setAxes(new Vector3f(-camRightX, -camRightY, -camRightZ), new Vector3f(-camUpX, -camUpY, -camUpZ), new Vector3f( camDirX, camDirY, camDirZ));
    foregroundCamera.update();
}
Aleana
  • 11
  • 1

1 Answers1

0

I have also implemented Vuforia with JMonkey Engine. I must admit that the shaky movement of 3D models is noticeable even when I hold my phone still, whereas using the OpenGl rendering engine with Vuforia alone doesn't produce such results.

Reason might be that what is being rendered on screen is Quad with the texture being output from the camera, and this also takes a while to change every frame. Moreover while running my app the phone gets very hot so I guess it is a heavy load for processor as well.

Memmo Fiero
  • 109
  • 7