在 Android 中使用 ARToolkit 渲染基于 JPCT-AE 的模型

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

我想通过JPCT-AE渲染模型并使用ARToolkit实现AR应用。

所以,我将如下代码注入到 ARToolkit 项目中:

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

然后,模型可以在屏幕上渲染,但始终位于屏幕上的标记上,无论我使用模型。rotateX/Y/Z( (float)Math.PI/2);

实际上,ARToolkit::ARNativeActivity.getTransformationMatrix() 输出的矩阵是正确的,然后我将这个 4*4Matrix 拆分为平移矩阵和旋转矩阵,并像这样设置到模型中:

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

但仍然没有工作。

我建议更好地组织您的代码,并使用矩阵来分离对模型进行的转换和将模型放置在标记中的转换。

我的建议是:

首先,使用一个额外的矩阵。它可能被称为 modelMatrix,因为它将存储对模型所做的转换(缩放、旋转和平移)。

然后,在此方法之外声明所有矩阵(这只是出于性能原因,但推荐),并在每个帧上简单地为它们设置身份:

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

稍后,对modelM矩阵进行模型变换。放置在标记上后,此转换将应用于模型。

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

然后,将 modelM 矩阵乘以 trasnformM(这意味着您完成了所有变换,并按照 transformM 描述的那样移动和旋转它们,在我们的例子中这意味着对模型完成的所有变换都被移动了标记的顶部)。

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

最后,将旋转和平移应用于您的模型:

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

所以整个代码看起来像:

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

我强烈推荐看看这个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

希望对您有所帮助!