2013-06-25 98 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); 
    } 
    

    更正窗口比例