Search code examples
javaandroidartoolkitjpct

Rendering a model basic on JPCT-AE with ARToolkit in Android


i want to render model via JPCT-AE and use the ARToolkit to realizing AR Application.

so , i inject the code as below into the ARToolkit Project:

    Matrix projMatrix = new Matrix();
    projMatrix.setDump(ARNativeActivity.getProjectM());
    projMatrix.transformToGL();
    SimpleVector translation = projMatrix.getTranslation();
    SimpleVector dir = projMatrix.getZAxis();
    SimpleVector up = projMatrix.getYAxis();
    cameraController.setPosition(translation);
    cameraController.setOrientation(dir, up);

    Matrix transformM = new Matrix();
    transformM .setDump(ARNativeActivity.getTransformationM());
    transformM .transformToGL();

    model.clearTranslation();
    model.translate(transformM .getTranslation());

    dump.setRow(3,0.0f,0.0f,0.0f,1.0f);
    model.clearRotation();
    model.setRotationMatrix(transformM );  

And then , the model can be render on the screen but always lie on the mark in the screen, ever i using model.rotateX/Y/Z( (float)Math.PI/2 );

Actually, the matrix output from the ARToolkit::ARNativeActivity.getTransformationMatrix() is correct, and then i split this 4*4Matrix into translation Matrix and Rotation Matrix and set into the model like this:

model.translate(transformM .getTranslation());
model.setRotationMatrix(transformM ); 

But still no work.


Solution

  • I would suggest to organize better your code, and work with matrices to separate the transformations to make to your model and the transformations to place the model in the marker.

    What I suggest is:

    First, use an additional matrix. It may be called modelMatrix, as it will store the transformation done to the model (scale, rotation and translation).

    Then, declare all matrices outside this method (it is for performance reasons only, but is recommended), and on each frame simply setIdentity to them:

    projMatrix.setIdentity();
    transformM.setIdentity();
    modelM.setIdentity();
    

    later, make the model transformations on the modelM matrix. This transformations will apply to the model, after placed on the marker.

    modelM.rotateZ((float) Math.toRadians(-angle+180));
    modelM.translate(movementX, movementY, 0);
    

    then, multiply the modelM matrix by the trasnformM (this means you get all the transformations done, and move and rotate them as the transformM describes, which in our case means that all the transformations done to the model are moved on top of the marker).

    //now multiply trasnformationMat * modelMat
    modelM.matMul(trasnformM);
    

    And finally, apply the rotation and translation to your model:

    model.setRotationMatrix(modelM);
    model.setTranslationMatrix(modelM);
    

    so the whole code would look as:

    projMatrix.setIdentity();
    projMatrix.setDump(ARNativeActivity.getProjectM());
    projMatrix.transformToGL();
    SimpleVector translation = projMatrix.getTranslation();
    SimpleVector dir = projMatrix.getZAxis();
    SimpleVector up = projMatrix.getYAxis();
    cameraController.setPosition(translation);
    cameraController.setOrientation(dir, up);
    
    model.clearTranslation();
    model.clearRotation();
    
    transformM.setIdentity();
    transformM .setDump(ARNativeActivity.getTransformationM());
    transformM .transformToGL();
    
    modelM.setIdentity()
    //do whatever you want to your model
    modelM.rotateZ((float)Math.toRadians(180));
    
    modelM.matMul(transformM);
    
    model.setRotationMatrix(modelM );  
    model.setTranslationMatrix(modelM);
    

    I strongly reccomend to look at this tutorial about matrices and OpenGL, it is not about JPCT, but all concepts may apply also to there, and it is what I've used to place correctly models in markers with the ARSimple example as you may see in this blog entry I made.

    Hope this helps!