笔记一下:
背景:手机全景播放app,放向根据陀螺仪调整。
需求:增加一个功能,手指滑动屏幕,可以调整坐标系方向。或者关闭陀螺仪仅仅用手指操作方向。
方法:
1. 根据触摸的api得到精度和纬度方向旋转角度,thOffset,laOffset。(实现略)
2. 根据thOffset,laOffset计算旋转后在球面上的矢量,(lookX, lookY, lookZ)。因为在球坐标上根据经纬度很好计算
// x = sin(theta)cos(lamda)
// y = cos(theta)
// z = sin(theta)sin (lamda)
public int modifyViewVectorSphere (float thOffset, float laOffset)
{
float rotateAngle;
float angleTh, angleLa;
int ret = 0;
if (theta + thOffset >= 180) {
theta = (float) 179;
ret = 1;
//Log.v(TAG, "chao,?? " + theta);
}
else if (theta + thOffset <= 0) {
theta = 1;
ret = 1;
//Log.v(TAG, "chao,!! " + theta);
}
else {
theta += thOffset;
}
if (lamda + laOffset > 360) {
lamda = lamda + laOffset - 360;
}
else if (lamda + laOffset < 0) {
lamda = lamda + laOffset + 360;
}
else {
lamda = lamda + laOffset;
}
angleTh = (float) (( theta / 180) * PI_T);
angleLa = (float) (( lamda / 180) * PI_T);
lookX = (float) ((float)Math.sin(angleTh) * Math.cos(angleLa));
lookY = (float) ((float)Math.cos(angleTh) );
lookZ = (float) ((float)Math.sin(angleTh) * Math.sin(angleLa));
return ret;
}
modifyViewVectorSphere(thAngle, laAngle);
3. 通过Matrix.setLookAtM的方法,以旋转后的矢量得到旋转矩阵。
Matrix.setLookAtM(mVMatrix, 0,
0f, 0f, 0f,
lookX, lookY, lookZ,
0f, 1f, 0.0f);
4. 处理MVP矩阵中的V(实际乘的顺序是PVM),对于旋转,先做(lookX, lookY, lookZ)的旋转,再做陀螺仪得到矩阵fm的旋转,所以View矩阵为
Matrix.multiplyMM(mVMatrix, 0, fm, 0, mVMatrix, 0);
MatrixState.setViewMatrix(mVMatrix);
5. 后面按PVM乘起来,draw就可以了。