[英]Opengl ES Android - Rotate camera around itself
I am developing a terrain generation app on android with Opengl ES, and I am having problems rotating the camera around itself ( the FPS effect to make it clear). 我正在使用Opengl ES在android上开发地形生成应用程序,但在围绕自身旋转相机时遇到了问题(FPS效果很清楚)。 what I am doing is to move the view with Matrix.setLookAtM
and then rotating the view after translating it to the origin.This is a snippet of the code: 我正在做的是使用Matrix.setLookAtM
移动视图,然后将其转换为Matrix.setLookAtM
后旋转视图。这是代码片段:
Matrix.setLookAtM(mViewMatrix, 0, xrot, eyeY, yrot, xrot, lookY, yrot,0.0f, 1.0f, 0.0f);
Matrix.translateM(mViewMatrix,0,-xrot,0f,-yrot);
Matrix.rotateM(mViewMatrix, 0, mAngleX+mAngleY, 0.0f, 1.0f,0.0f);
Matrix.translateM(mViewMatrix,0,xrot,0f,yrot);
where xrot
, yrot
, mAngleX+mAngleY
are input from the touchscreen. 其中xrot
, yrot
, mAngleX+mAngleY
是从触摸屏输入的。 This code works only in the origin, but when you move it rotates around the y axis of the world and not the camera one. 该代码仅在原点起作用,但是在移动时它绕世界的y轴旋转而不是摄像机的y轴旋转。 I guess I am not doing it right but I didn't find a way to do it that works anywhere. 我想我做的不对,但是我没有找到一种可以在任何地方工作的方法。
I found a way to do it: 我找到了一种方法:
Matrix.setIdentityM(mCurrentRotation, 0);
Matrix.rotateM(mCurrentRotation, 0, mAngleY+mAngleX, 0.0f,1.0f, 0.0f);
Matrix.rotateM(mCurrentRotation, 0, mAngleX, 0.0f,0.0f, 1.0f);
mAngleX = 0.0f;
mAngleY = 0.0f;
Matrix.multiplyMM(mTemporaryMatrix, 0, mCurrentRotation, 0, mAccumulatedRotation, 0);
System.arraycopy(mTemporaryMatrix, 0, mAccumulatedRotation, 0, 16);
Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
// multiply the rotation just for model*view
Matrix.multiplyMM(mMVPMatrix, 0, mTemporaryMatrix, 0, mMVPMatrix, 0);
//than add projection as usual
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
Where mTemporaryMatrix, mCurrentRotation,mAccumulatedRotation,
are matrices to calculate total rotation, keep frame rotation and store the whole rotation of the camera, respectively. 其中mTemporaryMatrix, mCurrentRotation,mAccumulatedRotation,
是分别计算总旋转,保持帧旋转和存储摄像机整体旋转的矩阵。 This solution works for rotation but it doesn't make the camera move in the direction it is pointing. 此解决方案可旋转,但不会使相机朝其指向的方向移动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.