2016-03-17 16 views
1

尋找谷歌地圖cardboard example我想知道,在哪裏發生了頭部運動轉換,以便通過場景或視圖來反映頭部運動。CardboardView如何照顧頭部運動?

有趣的方法應該是MainActivity類中的public void onNewFrame(HeadTransform headTransform)public void onDrawEye(Eye eye)。 這裏是一個片段:

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

}

我的第一個假設是,根據從headTransform數據模型(或攝像機)被修改在onNewFrame()。但這似乎並非如此,因爲只有兩次訪問。一個用於識別我們正在尋找哪個立方體(headTransform.getHeadView(headView, 0);),另一個用於音頻引擎。

所以我的下一個假設,只有我看到的可能性是,它被處理的eye傳遞給onDrawEye()。但另一方面,在disassembly內部進行簡短的檢查之後,我無法找到headTransformeye之間的關係(這並不意味着沒有關係,因爲我沒有投入太多時間)。

所以我的問題:

是我的假設嗎?渲染是否通過將camera乘以eyeView來考慮頭部運動?

回答

1

那麼,我花了一些時間瀏覽反彙編,似乎我的假設是正確的。 內CardboardView私有類RendererHelper實現以下的方法(這是相當大的,所以我刪除了什麼似乎不是對我很重要):

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之間的關係。