摄像机旋转
三維場景中的旋轉,是攝像機本身在世界坐標系中繞Y軸進行旋轉,從而改變位置,而其他的姿態不變,也就是攝像機的Position向量繞著世界坐標系的Y軸進行旋轉。
三種方式:
1,采用角度(不通用),此方法適合目標點是世界坐標系原點。
?
private float angleFirst = 0; private void RotateYFirst(float paAngle) { //通過角度 angleFirst += paAngle; if (angle > 6.28f) { angle -= 6.28f; } camPosition = new Vector3(4 * (float)Math.Cos(angleFirst), 0, 4 * (float)Math.Sin(angleFirst)); Matrix view = Matrix.LookAtLH(camPosition, camTarget, camUp); device.SetTransform(TransformType.View, view); }2,采用Matrix.RotationY方法對Position向量進行旋轉(較為通用)
//通過世界矩陣的Transform private void RotateYSecond(float paAngle) { //攝像機繞世界坐標系的Y軸進行旋轉,攝像機本身的姿態不進行任何改變 Vector4 tempRotate = Vector3.Transform( camPosition, Matrix.RotationY(paAngle)); camPosition = new Vector3(tempRotate.X, tempRotate.Y, tempRotate.Z); ? Matrix view = Matrix.LookAtLH( camPosition, camTarget, camUp); device.SetTransform(TransformType.View, view); }3,原理和2中一樣,不過在旋轉計算時,采用四元素數進行處理
//通過四元素數進行旋轉變換(同樣繞著世界坐標系的Y軸) private void RotateYThree(float paAngle) { Vector4 tempRotate = Vector3.Transform( camPosition, Matrix.RotationQuaternion(Quaternion.RotationAxis(new Vector3(0, 1, 0), paAngle))); camPosition = new Vector3(tempRotate.X, tempRotate.Y, tempRotate.Z); ? Matrix view = Matrix.LookAtLH(camPosition, camTarget, camUp); device.SetTransform(TransformType.View, view); }轉載于:https://www.cnblogs.com/sharpfeng/archive/2011/03/30/1999593.html
總結
- 上一篇: 版本模型
- 下一篇: Xtreme ToolkitPro 编译