我想在世界空间中翻译相机。它没有像我预期的那样移动。我的算法是
首先我在视图空间中翻译相机。相机始终位于 (0,0,0) 中。在我计算出视图空间中的新相机位置点之后。然后我将这些点与逆视图矩阵相乘,所以我想我会得到世界空间中的相机位置。
其次,我计算新的视图矩阵然后我使用 OpenGL 的 setLookAtM 函数计算视图矩阵。并设置新的视图矩阵。
但我的问题是它的新位置无法计算。它的值为 NAN
这是我的代码。
private void pivotRotation(double angle,int[] pivot, double distance){
float[] temp = new float[4];
float[] temp2 = new float[4];
float[] extObj = new float[16];
float[] startPointsOnViewSpace = new float[4];
float[] newEyeOrigin = new float[4];
float[] newLookOrigin = new float[4];
int[] viewport = new int[16];
viewport[0] = 0;
viewport[1] = 0;
viewport[2] = (int)myRenderer.screenWidth;
viewport[3] = (int)myRenderer.screenHeight;
int[] newEye = new int[2];
newEye[0] = (int)((pivot[0] + ((viewport[2]/2) - pivot[0]) * Math.cos(angle) - ((viewport[3]/2) - pivot[1]) * Math.sin(angle)));
newEye[1] = (int)((pivot[1] + ((viewport[2]/2) - pivot[0]) * Math.sin(angle) + ((viewport[3]/2) - pivot[1]) * Math.cos(angle)));
GLU.gluUnProject((float)newEye[0], (float)(viewport[3] - newEye[1]), 0.0f, myRenderer.modelViewMatrix, 0, myRenderer.projectionMatrix, 0, viewport, 0, extObj, 0);
Matrix.multiplyMV(startPointsOnViewSpace, 0, myRenderer.modelViewMatrix, 0, extObj, 0);
// new camera location in viewspace
startPointsOnViewSpace[0] /= startPointsOnViewSpace[3];
startPointsOnViewSpace[1] /= startPointsOnViewSpace[3];
startPointsOnViewSpace[2] /= startPointsOnViewSpace[3];
newEyeOrigin[0] = startPointsOnViewSpace[0];
newEyeOrigin[1] = startPointsOnViewSpace[1];
newEyeOrigin[2] += distance / pinchParameter;
newEyeOrigin[3] = 1.0f;
newLookOrigin[0] = newEyeOrigin[0];
newLookOrigin[1] = newEyeOrigin[1];
newLookOrigin[2] = newEyeOrigin[2] - 1.0f;
newLookOrigin[3] = 1.0f;
// temp[0], temp[1], temp[2] are new camera location in the world space. i think
Matrix.multiplyMV(temp, 0, myRenderer.inverseViewMatrix, 0, newEyeOrigin, 0);
Matrix.multiplyMV(temp2, 0, myRenderer.inverseViewMatrix, 0, newLookOrigin, 0);
for(int i = 0; i < 3; i++){
myRenderer.eyeLocation[i] = temp[i];
myRenderer.lookPosition[i] = temp2[i];
}
myRenderer.eyeSettings();