簡體   English   中英

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

然后,可以使用model.rotateX / Y / Z((float)Math.PI / 2);將該模型呈現在屏幕上,但始終位於屏幕上的標記上。

實際上,ARToolkit :: ARNativeActivity.getTransformationMatrix()的矩陣輸出是正確的,然后我將此4 * 4Matrix拆分為平移矩陣和旋轉矩陣,並設置為如下模型:

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

但是仍然沒有工作。

我建議組織更好的代碼,並與矩陣一起使用以分離對模型進行的轉換和將模型放置在標記中的轉換。

我的建議是:

首先,使用附加矩陣。 它可以稱為modelMatrix,因為它將存儲對模型進行的轉換(縮放,旋轉和平移)。

然后,在此方法之外聲明所有矩陣(僅出於性能原因,但建議這樣做),並在每個幀上簡單地將setIdentity設置為它們:

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

我強烈建議您看一下有關矩陣和OpenGL的教程 ,它不是關於JPCT的,但是所有概念都可能適用於此,這就是我用來將模型正確放置在帶有ARSimple示例的標記中的方法在這個博客條目中,我做了

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM