CardboardView如何处理头部运动?

时间:2016-03-17 13:32:04

标签: java android google-cardboard

浏览谷歌cardboard example我想知道,头部运动转换发生在哪里,以便采用场景或视图来反映头部运动。

有趣的方法应该是public void onNewFrame(HeadTransform headTransform)类中的public void onDrawEye(Eye eye)MainActivity。 这是一个片段:

@Override
public void onNewFrame(HeadTransform headTransform) {
   // Build the Model part of the ModelView matrix.
   Matrix.rotateM(modelCube, 0, TIME_DELTA, 0.5f, 0.5f, 1.0f);

   // Build the camera matrix and apply it to the ModelView.
   Matrix.setLookAtM(camera, 0, 0.0f, 0.0f, CAMERA_Z, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

   headTransform.getHeadView(headView, 0);

   // Update the 3d audio engine with the most recent head rotation.
   headTransform.getQuaternion(headRotation, 0);
   cardboardAudioEngine.setHeadRotation(
    headRotation[0], headRotation[1], headRotation[2], headRotation[3]);

   checkGLError("onReadyToDraw");
}

@Override
public void onDrawEye(Eye eye) {
   GLES20.glEnable(GLES20.GL_DEPTH_TEST);
   GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

   checkGLError("colorParam");

   // Apply the eye transformation to the camera.
   Matrix.multiplyMM(view, 0, eye.getEyeView(), 0, camera, 0);

   // Set the position of the light
   Matrix.multiplyMV(lightPosInEyeSpace, 0, view, 0, LIGHT_POS_IN_WORLD_SPACE, 0);

   // Build the ModelView and ModelViewProjection matrices
   // for calculating cube position and light.
   float[] perspective = eye.getPerspective(Z_NEAR, Z_FAR);
   Matrix.multiplyMM(modelView, 0, view, 0, modelCube, 0);
   Matrix.multiplyMM(modelViewProjection, 0, perspective, 0, modelView, 0);
   drawCube();

   // Set modelView for the floor, so we draw floor in the correct location
   Matrix.multiplyMM(modelView, 0, view, 0, modelFloor, 0);
   Matrix.multiplyMM(modelViewProjection, 0, perspective, 0, modelView, 0);
   drawFloor();

}

我的第一个假设是模型(或相机)在onNewFrame()中被修改,具体取决于来自headTransform的数据。但似乎并非如此,因为只有两次访问。一个用于识别我们正在查找的多维数据集(headTransform.getHeadView(headView, 0);)和另一个用于音频引擎的多维数据集。

所以我的下一个假设,也就是我看到的唯一可能性是,它由传递给eye的{​​{1}}处理。但另一方面,在disassembly内部进行了简短的审视之后,我找不到onDrawEye()headTransform之间的关系(这并不意味着没有关系,因为我没有' t投入了很多时间。)

所以我的问题:

我的假设是对的吗?渲染是通过将eye乘以eyeView来计算头部运动吗?

1 个答案:

答案 0 :(得分:1)

好吧,我花了一些时间浏览反汇编,似乎我的假设是正确的。 RendererHelper中的私有类CardboardView实现了以下方法(它非常大,因此我删除了对我来说似乎不重要的内容):

public void onDrawFrame(GL10 gl)
{
    // ...

    if (mVRMode)
    {
        Matrix.setIdentityM(mLeftEyeTranslate, 0);
        Matrix.setIdentityM(mRightEyeTranslate, 0);

        Matrix.translateM(mLeftEyeTranslate, 0, halfInterpupillaryDistance, 0.0F, 0.0F);

        Matrix.translateM(mRightEyeTranslate, 0, -halfInterpupillaryDistance, 0.0F, 0.0F);

        Matrix.multiplyMM(mLeftEye.getTransform().getEyeView(), 0, mLeftEyeTranslate, 0, mHeadTransform.getHeadView(), 0);

        Matrix.multiplyMM(mRightEye.getTransform().getEyeView(), 0, mRightEyeTranslate, 0, mHeadTransform.getHeadView(), 0);
    }

    // ...
}

最后两个矩阵乘法似乎是headTransformeye之间的关系。

相关问题