四元数摄像机

无万向锁,旋转绝对自由。就是不好控制

BTW:这是渲染引擎部件之一。CCamera可以用于设置RenderTarget,而CViewerCamera则是给用户3D漫游用的

None.gif public   class  CCamera
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        Matrix4x4 mViewMatrix, mProjMatrix;
InBlock.gif
InBlock.gif        
protected Vector mPosition;
InBlock.gif        
protected Quaternion mOri;
InBlock.gif
InBlock.gif        
protected Vector mFront;
InBlock.gif        
protected Vector mTop;
InBlock.gif        
protected Vector mRight;
InBlock.gif
InBlock.gif        
double mFovy;
InBlock.gif        
double mNear;
InBlock.gif        
double mFar;
InBlock.gif        
double mAspect;
InBlock.gif
InBlock.gif        CFrustum mFrustum 
= new CFrustum();
InBlock.gif 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 摄像机Z
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif
InBlock.gif        
public Vector Front
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn mFront; }
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 摄像机Y
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public Vector Top
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn mTop; }
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 摄像机X
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public Vector Right
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn mRight; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public Vector Position
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn mPosition; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ mPosition = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public Quaternion Orientation
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn mOri; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ mOri = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public double Fovy
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn mFovy; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ mFovy = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public double Near
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn mNear; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ mNear = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public double Far
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn mFar; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ mFar = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public double Aspect
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn mAspect; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ mAspect = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public CCamera(double aspect)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            mFar 
= 2500;
InBlock.gif            mFovy 
= 45;
InBlock.gif            mNear 
= 1;
InBlock.gif            mAspect 
= aspect;
InBlock.gif            mOri 
= Quaternion.Inentity;
InBlock.gif            Update();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void UpdateProjection()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            mProjMatrix 
= Matrix4x4.GetProjectionMatrix(mFovy, mAspect, mNear, mFar);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Update()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            mViewMatrix 
= Matrix4x4.GetTranslateMatrix(-mPosition) * mOri.ToMatrix4(new Vector());
InBlock.gif            mProjMatrix 
= Matrix4x4.GetProjectionMatrix(mFovy, mAspect, mNear, mFar);
InBlock.gif            mFrustum.Update(mViewMatrix, mProjMatrix);
InBlock.gif
InBlock.gif            Matrix3x3 rot 
= mOri.ToMatrix3();
InBlock.gif            mFront 
= rot * new Vector(001);
InBlock.gif            mTop 
= rot * new Vector(010);
InBlock.gif            mRight 
= rot * new Vector(100);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public Matrix4x4 ProjMatrix
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn mProjMatrix; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ mProjMatrix = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public Matrix4x4 ViewMatrix
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn mViewMatrix; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ mViewMatrix = value; }
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif}

None.gif
None.gif
public   class  CViewerCamera : CCamera
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public CViewerCamera(double aspect)
InBlock.gif            : 
base(aspect)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{ }
InBlock.gif
InBlock.gif        
protected double mMoveSpeed = 2.5;
InBlock.gif        
protected double mTurnSpeed = Math.PI / 45;
InBlock.gif
InBlock.gif        
public double MoveSpeed
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn mMoveSpeed; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ mMoveSpeed = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public double TurnSpeed
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn mTurnSpeed; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ mTurnSpeed = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void MoveAbs(Vector mov)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            mPosition 
+= mOri.ToMatrix3() * mov;
ExpandedSubBlockEnd.gif        }

InBlock.gif 
InBlock.gif        
public void MoveFront()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{ mPosition += mFront * mMoveSpeed; }
InBlock.gif
InBlock.gif        
public void MoveBack()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{ mPosition -= mFront * mMoveSpeed; }
InBlock.gif
InBlock.gif        
public void MoveLeft()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{ mPosition -= mRight * mMoveSpeed; }
InBlock.gif
InBlock.gif        
public void MoveRight()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{ mPosition += mRight * mMoveSpeed; }
InBlock.gif
InBlock.gif        
public void MoveUp()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            mPosition 
+= mTop * mMoveSpeed;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void MoveDown()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            mPosition 
-= mTop * mMoveSpeed;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void TurnLeft()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            mOri 
*= Quaternion.FromAngleAxis(mTurnSpeed, new Vector(0-10));
InBlock.gif            mOri.Normalize();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void TurnRight()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            mOri 
*= Quaternion.FromAngleAxis(mTurnSpeed, new Vector(010));
InBlock.gif            mOri.Normalize();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void TurnUp()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            mOri 
*= Quaternion.FromAngleAxis(mTurnSpeed, new Vector(-100));
InBlock.gif            mOri.Normalize();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void TurnDown()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            mOri 
*= Quaternion.FromAngleAxis(mTurnSpeed, new Vector(100));
InBlock.gif            mOri.Normalize();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void RollLeft()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            mOri 
*= Quaternion.FromAngleAxis(mTurnSpeed, new Vector(001));
InBlock.gif            mOri.Normalize();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void RollRight()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            mOri 
*= Quaternion.FromAngleAxis(mTurnSpeed, new Vector(00-1));
InBlock.gif            mOri.Normalize();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif    }


 

转载于:https://www.cnblogs.com/Yuri/archive/2007/07/01/802376.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值