예제 #1
0
 public static Rot RotBetween( Vector3 v1, Vector3 v2 )
 {
     Rot rResult = new Rot();
     
     Vector3 VectorNorm1 = new Vector3( v1 ).Normalize();
     Vector3 VectorNorm2 = new Vector3( v2 ).Normalize();
 
     Vector3 RotationAxis = Vector3.CrossProduct( VectorNorm1, VectorNorm2 );
     //Test.Debug(  "math: " << RotationAxis ); // Test.Debug
 
     //Test.Debug(  Math.Abs( RotationAxis.x ) << " " << Math.Abs( RotationAxis.y ) << " " << Math.Abs( RotationAxis.z ) ); // Test.Debug
     if( Math.Abs( RotationAxis.x ) < 0.0005 && Math.Abs( RotationAxis.y ) < 0.0005 && Math.Abs( RotationAxis.z ) < 0.0005 )
     {
         Vector3 RandomVector = new Vector3( VectorNorm1 );
         RandomVector.x += 0.5;
         RandomVector.Normalize();
         RotationAxis = Vector3.CrossProduct( VectorNorm1, RandomVector );
 
         rResult = mvMath.AxisAngle2Rot( RotationAxis, Math.PI );
     }
     else
     {
         double DotProduct = Vector3.DotProduct( VectorNorm1, VectorNorm2 );
         Diag.Debug( "DotProduct: " + DotProduct.ToString() ); // Test.Debug
         double Vangle = Math.Acos( DotProduct );
         Diag.Debug( "math: " + Vangle.ToString() ); // Test.Debug
         rResult = AxisAngle2Rot( RotationAxis, Vangle );
     }
     return rResult;
 }
예제 #2
0
 static public Vector3 operator*( Vector3 V, Rot rot )
 {
     Rot InverseInRot = rot.Inverse();
     Rot VectorRot = new Rot( V.x, V.y, V.z, 0 );
     Rot IntRot = VectorRot * rot;
     Rot ResultRot = InverseInRot * IntRot;            
     return new Vector3( ResultRot.x, ResultRot.y, ResultRot.z );  
 }
        void SetupFrustrum()
        {
            //Console.WriteLine("setup frustrum");

            camerapos = Camera.GetInstance().RoamingCameraPos;
            camerarot = Camera.GetInstance().RoamingCameraRot;
            Rot inversecamerarot = camerarot.Inverse();
            viewray = -mvMath.YAxis * inversecamerarot;
            viewray.Normalize();
            right = mvMath.XAxis * inversecamerarot;
            up = mvMath.ZAxis * inversecamerarot;
            right.Normalize();
            up.Normalize();

            double nearclip = RendererFactory.GetInstance().NearClip;
            double farclip = RendererFactory.GetInstance().FarClip;
            VNear = 2 * Math.Tan(RendererFactory.GetInstance().FieldOfView / 2 * Math.PI / 180) * nearclip;
            VFar = VNear * farclip / nearclip;
            HNear = VNear * (double)RendererFactory.GetInstance().WindowWidth / RendererFactory.GetInstance().WindowHeight;
            HFar = HNear * farclip / nearclip;

            fc = camerapos + viewray * farclip;
            ftl = fc + (up * VFar / 2) - (right * HFar / 2);
            ftr = fc + (up * VFar / 2) + (right * HFar / 2);
            fbl = fc - (up * VFar / 2) - (right * HFar / 2);
            fbr = fc - (up * VFar / 2) + (right * HFar / 2);

            nc = camerapos + viewray * nearclip;

            ntl = nc + (up * VNear / 2) - (right * HNear / 2);
            ntr = nc + (up * VNear / 2) + (right * HNear / 2);
            nbl = nc - (up * VNear / 2) - (right * HNear / 2);
            nbr = nc - (up * VNear / 2) + (right * HNear / 2);

            // note: all normals point outwards
            planes[0] = new Plane(-viewray, nc);
            planes[1] = new Plane(viewray, fc);

            Vector3 vectoralongplane;
            Vector3 normal;

            vectoralongplane = (ntr - camerapos).Normalize();
            normal = (up * vectoralongplane).Normalize();
            planes[2] = new Plane(normal, camerapos);

            vectoralongplane = (nbr - camerapos).Normalize();
            normal = (right * vectoralongplane).Normalize();
            planes[3] = new Plane(normal, camerapos);

            vectoralongplane = (nbl - camerapos).Normalize();
            normal = -(up * vectoralongplane).Normalize();
            planes[4] = new Plane(normal, camerapos);

            vectoralongplane = (ntl - camerapos).Normalize();
            normal = -(right * vectoralongplane).Normalize();
            planes[5] = new Plane(normal, camerapos);
        }
예제 #4
0
 public static Rot operator*( Rot R1, Rot R2 )
 {
     Rot result = new Rot();
     result.s = R1.s * R2.s - R1.x * R2.x - R1.y * R2.y - R1.z * R2.z;
 
     result.x = R1.s * R2.x + R1.x * R2.s + R1.y * R2.z -R1.z * R2.y;
     result.y = R1.s * R2.y + R1.y * R2.s + R1.z * R2.x - R1.x * R2.z;
     result.z = R1.s * R2.z + R1.z * R2.s + R1.x * R2.y - R1.y * R2.x;
 
     //Test.Debug(  "RotMULITPLY in=" << Q1 << " " << Q2 << " out=" << Qr <<endl;
     return result;
 }
        //! Feedback line buffer for OpenGL feedback, used by mvgraphics.cpp
        /*
        class FeedbackLineBufferItem
        {
            public double type;
            public Vector2[] vertices = new Vector2[2];
        }
        */

        public Vector3 GetMouseVector(Vector3 OurPos, Rot OurRot, int mousex, int mousey)
        {
            IRenderer renderer = RendererFactory.GetInstance();

            Vector3 MouseVectorObserverAxes = new Vector3( - renderer.WindowWidth / 2 + mousex, - renderer.ScreenDistanceScreenCoords, renderer.WindowHeight / 2 - mousey);
            //Console.WriteLine("MouseVectorObserverAxes: " + MouseVectorObserverAxes);
            MouseVectorObserverAxes.Normalize();
            //Console.WriteLine("MouseVectorObserverAxes (normalized): " + MouseVectorObserverAxes);
            Vector3 MouseVectorWorldAxes = MouseVectorObserverAxes * OurRot.Inverse();
            //Console.WriteLine("MouseVectorWorldAxes: " + MouseVectorWorldAxes.ToString());
            MouseVectorWorldAxes.Normalize();
            return MouseVectorWorldAxes;
        }
예제 #6
0
 public static void Rot2AxisAngle( ref Vector3 Vr, ref double Thetar, Rot R )
 {
     //QuaternionNormalize( |X,Y,Z,W| );
     
     Vr = new Vector3();
 
     double cos_a = R.s;
     Thetar = Math.Acos( cos_a ) * 2;
     double sin_a = Math.Sqrt( 1.0 - cos_a * cos_a );
 
     if ( Math.Abs( sin_a )> 0.0005 )
     {
         Vr.x = R.x / sin_a;
         Vr.y = R.y / sin_a;
         Vr.z = R.z / sin_a;
     }
     else
     {
         Vr.x = 1;
         Vr.y = 0;
         Vr.z = 0;
     }
 }
예제 #7
0
 void UpdateRoamingCameraRotAndPosFromOverheadOTA()
 {
     RoamingCameraPos = new Vector3(
         overheadotastate_current.x,
         overheadotastate_current.y + overheadotastate_current.distance * Math.Cos(overheadotastate_current.anglefromhorizontaldegrees * Math.PI / 180),
         overheadotastate_current.distance * Math.Sin(overheadotastate_current.anglefromhorizontaldegrees * Math.PI / 180));
     //RoamingCameraRot = mvMath.AxisAngle2Rot( mvMath.ZAxis, Math.PI ) *  mvMath.AxisAngle2Rot(mvMath.XAxis, + overheadotastate_current.anglefromhorizontaldegrees * Math.PI / 180);
     RoamingCameraRot = mvMath.AxisAngle2Rot(mvMath.XAxis, + overheadotastate_current.anglefromhorizontaldegrees * Math.PI / 180);
 }
 public void Rotate( Rot rot )
 {
     double fRotAngle = 0;
     Vector3 vAxis = new Vector3();
     mvMath.Rot2AxisAngle( ref vAxis, ref fRotAngle, rot );
     Gl.glRotatef( (float)( fRotAngle / Math.PI * 180 ), (float)vAxis.x, (float)vAxis.y, (float)vAxis.z );
 }
예제 #9
0
 // This obviously gives an ambiguous result, but its ok for things like editing handles that 
 // are symmetric when rotated by 90,180, or 270 degrees around the target axis
 public Rot ToRot()
 {
     Rot result = null;
     if( axisindex == 0 && bPositiveAxis )
     {
         result = new Rot();
     }
     else if( axisindex == 1 && bPositiveAxis )
     {
         result = mvMath.AxisAngle2Rot( Axis.PosZ.ToVector(), Math.PI / 2 );
     }
     else if( axisindex == 2 && bPositiveAxis )
     {
         result = mvMath.AxisAngle2Rot(Axis.PosY.ToVector(), - Math.PI / 2 );
     }
     else if( axisindex == 0  )
     {
         result = mvMath.AxisAngle2Rot(Axis.PosZ.ToVector(), Math.PI );
     }
     else if( axisindex == 1  )
     {
         result = mvMath.AxisAngle2Rot(Axis.PosZ.ToVector(), - Math.PI / 2 );
     }
     else if( axisindex == 2  )
     {
         result = mvMath.AxisAngle2Rot(Axis.PosY.ToVector(), Math.PI / 2 );
     }
     return result;
 }
예제 #10
0
 public static void ApplyRotToGLMatrix4d( ref GLMatrix4d matrix, Rot rot )
 {
     double fRotAngle = 0;
     Vector3 vAxis = new Vector3();
     mvMath.Rot2AxisAngle( ref vAxis, ref fRotAngle, rot );
     
     matrix.applyRotate( (float)( fRotAngle / Math.PI * 180 ), (float)vAxis.x, (float)vAxis.y, (float)vAxis.z );
 }