2011-11-04 35 views
0

基本上我有一個應用程序Android 1.5與GLSurfaceView類,在屏幕上顯示一個簡單的正方形多邊形。我想學習添加新功能,用手指移動正方形的功能。我的意思是當用戶觸摸正方形並移動手指時,應該用手指移動正方形,直到手指釋放屏幕。我想使用gluUnProject獲取匹配手指的確切位置的OpenGL座標,然後,我將製作一個translatef到多邊形,並且我將得到多邊形移動到該位置(我希望它)使用gluUnProject的問題

問題是gluUnProject出了問題,它給我這個異常:java.lang.IllegalArgumentException: length - offset < n在gluUnProject調用。

首先,我傳遞0作爲Z贏得座標,因爲我不知道我必須通過什麼樣的z贏得座標,因爲win沒有Z座標,只有X和Y. 我測試了傳遞1在Z座標上,我得到了同樣的異常。

float [] outputCoords=getOpenGLCoords(event.getX(), event.getY(), 0); 
x=outputCoords[0]; 
y=outputCoords[1]; 
z=outputCoords[2]; 

。 。 。

public float[] getOpenGLCoords(float xWin,float yWin,float zWin) 
{ 
    int screenW=SectionManager.instance.getDisplayWidth(); 
    int screenH=SectionManager.instance.getDisplayHeight(); 
    //CODE FOR TRANSLATING FROM SCREEN COORDINATES TO OPENGL COORDINATES 
    mg.getCurrentProjection(MyGl); 
    mg.getCurrentModelView(MyGl); 
    float [] modelMatrix = new float[16]; 
    float [] projMatrix = new float[16]; 
    modelMatrix=mg.mModelView; 
    projMatrix=mg.mProjection;   
    int [] mView = new int[4]; 
    mView[0] = 0; 
    mView[1] = 0; 
    mView[2] = screenW; //width 
    mView[3] = screenH; //height 
    float [] outputCoords = new float[3]; 
    GLU.gluUnProject(xWin, yWin, zWin, modelMatrix, 0, projMatrix, 0, mView, 0, outputCoords, 0); 
    return outputCoords; 
} 

回答

2

我回答了同樣的問題here;基本上,gluUnproject函數需要你的outputCoords數組的大小爲4而不是3.請注意,這些是齊次座標,所以如果您正在進行透視投影,則仍然必須將第一個3除以第四個。

+0

你能解釋我爲什麼要除以第四個元素嗎? – NullPointerException

+0

還有,至於Z值,我必須傳遞給winZ值上的gluUnProject函數? – NullPointerException

+0

@ AndroidUser99因爲它們是齊次座標'(x,y,z,w)'而且你想要等價的座標'(x/w,y/w,z/w,1)',這又相當於一個在仿射三維空間中點'(x/w,y/w,z/w)'。 –