コード例 #1
0
	CCamera::CCamera(const CPoint& eyePos, const CPoint& target, int screenWidth, int screenHeight, F fovy) :
		mScreenHeight(screenHeight), mScreenWidth(screenWidth), mEye(eyePos), mTarget(target), mFovy(fovy)
	{
		CVector3 d = mEye - mTarget;
		CVector3 up(CVector3::mUp);

		mU = d.CrossProduct(CVector3::mUp);
		mV = mU.CrossProduct(d);
		mW = d;

		mU.Normalize();
		mV.Normalize();
		mW.Normalize();

		mAspectRatio = (float)mScreenHeight / mScreenWidth;

		mNearPlaneWidth = 2.0 * tan(mFovy * 3.14 / 180); //distance to near plane equals 1.0
		mNearPlaneHeight = mNearPlaneWidth * mAspectRatio;

		mTransformCoeffX = mNearPlaneWidth / mScreenWidth;
		mTransformCoeffY = mNearPlaneHeight / mScreenHeight;
	}
コード例 #2
0
 void CQTOpenGLCamera::SSettings::RotateLeftRight2(const CRadians& c_angle) {
    /* This rotation is performed around the global Z axis.
       To this aim, along with the global Z axis we project the
       Forward and Left axes onto the XY plane and use these as
       additional axis to build up the rotation matrix.
       With the matrix, we rotate the projected Forward and Left vectors.
       We then transform the projected vectors into the final ones.
       Finally, we cross-product the two vectors to obtain the new Up vector.
    */
    if(Forward.GetX() != 0 || Forward.GetY() != 0) {
       /* Project Forward onto the XY axis */
       CVector3 cForwardXY(Forward.GetX(), Forward.GetY(), 0.0f);
       /* Save its length: it will be used to restore the Z coordinate correctly */
       Real cForwardXYLength = cForwardXY.Length();
       /* Normalize the projection */
       cForwardXY.Normalize();
       
       /* Project Left onto the XY axis and normalize the result */
       CVector3 cLeftXY = CVector3::Z;
       cLeftXY.CrossProduct(cForwardXY).Normalize();
       
       /* The rotation matrix corresponding to this rotation is:
          
          | Fcos(a)+Lsin(a)   -Fsin(a)+Lcos(a)   Z |
          
          where a is c_angle
          L is the Left vector projected on XY
          F is the Forward vector projected on XY
          Z is the global Z vector
       */
       
       /* Calculate the new cForwardXY vector, given by:
          Fcos(a)+Lsin(a)
          We keep the unrotated cForwardXY vector, because we will
          need it for rotating Left too.
       */
       /* Same, but faster than
          CVector3 cNewForwardXY = cForwardXY * Cos(c_angle) + cLeftXY * Sin(c_angle);
       */
       CVector3 cNewForwardXY(cForwardXY);
       cNewForwardXY *= Cos(c_angle);
       cNewForwardXY += cLeftXY * Sin(c_angle);
       cNewForwardXY.Normalize();
       /* Update Forward from cNewForwardXY: we want to keep the Z coordinate
          of Forward unchanged cause we rotated around the global Z, but we
          need to update the X and Y coordinates. Right now, cNewForwardXY has
          zero Z coordinate and length 1, which means that its X and Y are wrong.
          To get the right values, we need to scale it back to the length of the
          projection of Forward onto the XY plane. Once this is done, the X and Y
          coordinates are right.
       */
       /* Scale the vector back to the right length */
       cNewForwardXY *= cForwardXYLength;
       /* Finally, update Forward */
       Forward.SetX(cNewForwardXY.GetX());
       Forward.SetY(cNewForwardXY.GetY());
       Forward.Normalize();
       
       /* Calculate the new Left vector, given by:
          -Fsin(a)+Lcos(a)
       */
       /* Same, but faster than
          Left = cLeftXY * Cos(c_angle) - cForwardXY * Sin(c_angle);
       */
       Left = cLeftXY;
       Left *= Cos(c_angle);
       Left -= cForwardXY * Sin(c_angle);
       Left.Normalize();
       
       /* To calculate the new Up vector, a cross-product is enough */
       Up = Forward;
       Up.CrossProduct(Left).Normalize();
       
       /* Finally, update the Target vector */
       /* Same, but faster than
          Target = Position + Forward;
          Target = Position;
          Target += Forward;
       */
    }
 }