2014-08-31 47 views

回答

0

編輯: 你是對的,默認的openGL座標系統的中心在屏幕中心,但你可以很容易地改變它。我不太喜歡android開發人員網站上顯示的方法。以下是設置場景以使座標從左下角開始的示例。

private final float[] mMVPMatrix = new float[16]; // this is the matrix you will send to your shader 
private final float[] mProjectionMatrix = new float[16]; 
private final float[] mViewMatrix = new float[16]; 

public void onSurfaceChanged(GL10 unused, int width, int height) { 
    GLES20.glViewport(0, 0, width, height); 
    Matrix.orthoM(mProjectionMatrix, 0, 0f, width, 0.0f, height, 0, 1); 
    // Set the camera position (View matrix) 
    Matrix.setLookAtM(mViewMatrix, 0, 0f, 0f, 1, 0f, 0f, 0f, 0f, 1.0f, 0.0f); 
    // Calculate the projection and view transformation 
    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0); 
} 

請注意,這個方法在您的屏幕空間設置你的數字座標比< -1,1>範圍在我看來,就方便多了。

現在你可以使用這個代碼讓你觸摸的座標:

public boolean onTouchEvent(MotionEvent event) { 
    float touch_x = event.getRawX(); 
    float touch_y = yourScreenHeight - event.getRawY(); 
} 
+0

openGL顯示座標的中心是屏幕的中心。 – 2014-08-31 12:33:37

+0

鏈接http://developer.android.com/guide/topics/graphics/opengl.html – 2014-08-31 12:39:05

0

把這個在onSurfaceChanged功能,這是將昌座標系統,如Android使用:

 float [] ortho= 
    { 
    2f/width, 0f,   0f, 0f, 
    0f,  -2f/height, 0f, 0f, 
    0f,   0f,   0f, 0f, 
    -1f,   1f,   0f, 1f 
    }; 
float ratio = width/height; 
//public static float[] m2dProjectionMatrix = new float[16]; 
System.arraycopy(ortho, 0, Render.m2dProjectionMatrix, 0,ortho.length); 

GLES20.glViewport(0, 0, width, height); 
Render.width=width; 
Render.height=height; 
//public static float[] mModelMatrix= new float[16]; 
Matrix.setIdentityM(Render.mModelMatrix, 0); 

您需要在您的頂點着色器m2dProjectionMatrix和mModelMatrix中使用此2浮點矩陣,並且您也需要此行

gl_Position = (m2dProjectionMatrix) *mModelMatrix * vec4(a_Position.xy, 0.0, 1.0); 

a_Position.xy是座標,如果你想移動,你可以做你ORIGO觸摸通過修改mModelMatrix這樣的:

//mMoveMatrix is an array like the other two 
    // positionX and positionY is the coordinates to move the origo 
    Matrix.setIdentityM(mMoveMatrix, 0); 
    Matrix.translateM(mMoveMatrix, 0, positionX, positionY, 0); 
    Matrix.multiplyMM(mMoveMatrix, 0, Render.mModelMatrix, 0, mMoveMatrix, 0); 
    // use mMoveMatrix insted of mModelMatrix 

我希望我沒有搞砸,它會工作