[英]Opengl ES Android - Rotate camera around itself
我正在使用Opengl ES在android上開發地形生成應用程序,但在圍繞自身旋轉相機時遇到了問題(FPS效果很清楚)。 我正在做的是使用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);
其中xrot
, yrot
, mAngleX+mAngleY
是從觸摸屏輸入的。 該代碼僅在原點起作用,但是在移動時它繞世界的y軸旋轉而不是攝像機的y軸旋轉。 我想我做的不對,但是我沒有找到一種可以在任何地方工作的方法。
我找到了一種方法:
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);
其中mTemporaryMatrix, mCurrentRotation,mAccumulatedRotation,
是分別計算總旋轉,保持幀旋轉和存儲攝像機整體旋轉的矩陣。 此解決方案可旋轉,但不會使相機朝其指向的方向移動。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.