2013-06-25 157 views
0

我最近開始使用openGL,因爲普通視圖中的畫布已經不能滿足我的需求了。但我似乎無法理解opengl中的座標系。Android opengl座標

當我製作一個底部座標爲y = 0的矩形時,我在屏幕的上半部分得到一個矩形的結果。這是可以預料的。

但是,當我設置y = 0.25f,那麼我會認爲該矩形將是屏幕的四分之一頂部。但實際上它更少,例如1/6或1/7。

或者當我嘗試y = 0.375f(應該是屏幕的1/8)時,結果就像1/11或1/12。有些人可以解釋爲什麼會發生?我錯過了什麼。那麼我怎麼能做出一個矩形填充屏幕的前1/8?

(我知道,在屏幕和底部的頂部有 - + 0.5F座標的變焦IM)

回答

3

這可能是你的相機從你的視矩陣的位置造成的。 回想一下座標系的方位在OpenGL:

  • + X點右邊
  • + Y點向上
  • + Z點在屏幕
  • (0,0,0)的是在在屏幕的中央

要設置攝像機:

Matrix.setLookAtM(mVMatrix, 0, 0, 0, 2, 0f, 0f, 0f, 0f, 1.0f, 0f); 

// My camera is located at (0,0,2), 
// it's looking toward (0,0,0) 
// its top is pointing along (0,1,0) aka. Y-axis. 

如果我的矩形位於XY平面的某個位置,即Z座標爲0,那麼我的相機在正Z軸上距離它2個單位。從這個位置查看矩形使其看起來比預期的要小。要使矩形看起來更大,請嘗試通過更改其在視圖矩陣中的位置將攝像機移近它。我改變Z座標從2到1的下方:

Matrix.setLookAtM(mVMatrix, 0, 0, 0, 1, 0f, 0f, 0f, 0f, 1.0f, 0f); 

// My camera is located at (0,0,1), 
// it's looking toward (0,0,0) 
// its top is pointing along (0,1,0) aka. Y-axis. 
0

從該示例中的代碼示例:

Android Training > Applying Projection and Camera Views

  1. 定義的相機視圖

    // Set the camera position (View matrix) 
    Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f); 
    
    // Calculate the projection and view transformation 
    Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0); 
    

    在我的情況下產生一個帶有y軸的座標系範圍從-1到+1。

  2. 定義投影

    @Override 
    public void onSurfaceChanged(GL10 unused, int width, int height) { 
        GLES20.glViewport(0, 0, width, height); 
    
        float ratio = (float) width/height; 
    
        // this projection matrix is applied to object coordinates 
        // in the onDrawFrame() method 
        Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7); 
    } 
    

    更正窗口比例