基本上我有一個應用程序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;
}
你能解釋我爲什麼要除以第四個元素嗎? – NullPointerException
還有,至於Z值,我必須傳遞給winZ值上的gluUnProject函數? – NullPointerException
@ AndroidUser99因爲它們是齊次座標'(x,y,z,w)'而且你想要等價的座標'(x/w,y/w,z/w,1)',這又相當於一個在仿射三維空間中點'(x/w,y/w,z/w)'。 –