Beispiel #1
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 / mvMath.PiOver180 ), (float)vAxis.x, (float)vAxis.y, (float)vAxis.z );
        }
        public EntityCreationProperties(int screenx, int screeny)
        {
            LogFile.WriteLine("create entity screen pos : " + screenx + " " + screeny);

            IGraphicsHelper graphics = GraphicsHelperFactory.GetInstance();

            Camera camera = Camera.GetInstance();
            Vector3 mousevector = graphics.GetMouseVector(
                camera.CameraPos, camera.CameraRot, screenx, screeny);
            pos = camera.CameraPos + 3.0 * mousevector.Normalize();
            LogFile.WriteLine("mousevector: " + mousevector);

            rot = new Rot();
            scale = new Vector3(1, 1, 1);
        }
Beispiel #3
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;
            }
        }
 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);
 }
        // not to self: this is ugly, maybe simplify a little/lot?
        public Vector3 GetScreenPos(Vector3 ObserverPos, Rot ObserverRot, Vector3 TargetPos3D)
        {
            float[] feedbackbuffer = new float[1 * 3];

            Gl.glFeedbackBuffer(3, Gl.GL_2D, feedbackbuffer);

            int[] viewport = new int[4];
            // This Sets The Array <viewport> To The Size And Location Of The Screen Relative To The Window
            Gl.glGetIntegerv(Gl.GL_VIEWPORT, viewport);

            Gl.glPushMatrix();

            Gl.glRenderMode(Gl.GL_FEEDBACK);
            Gl.glLoadIdentity();

            // rotate so z axis is up, and x axis is forward
            Gl.glRotatef(90f, 0.0f, 0.0f, 1.0f);
            Gl.glRotatef(90f, 0.0f, 1.0f, 0.0f);

            Rotate(ObserverRot.Inverse());
            Translate(-ObserverPos);

            Gl.glBegin(Gl.GL_POINTS);
            Gl.glVertex3f((float)TargetPos3D.x, (float)TargetPos3D.y, (float)TargetPos3D.z);
            Gl.glEnd();

            Gl.glRenderMode(Gl.GL_RENDER);

            Gl.glPopMatrix();

            //DEBUG(  "Screencoords of input vertex: " << FeedbackBuffer.Vertices[0].x << " " << FeedbackBuffer.Vertices[0].y ); // DEBUG
            Vector2 screenpoint = GetFeedbackPointBufferItem(feedbackbuffer, 0);
            Vector3 ScreenPos = new Vector3(
                0,
                RendererFactory.GetInstance().WindowWidth - screenpoint.x,
                screenpoint.y
            );

            //DEBUG(  "screenpos: " << ScreenPos ); // DEBUG

            return ScreenPos;
        }
        //! 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);
            Vector3 MouseVectorObserverAxes = new Vector3(
                renderer.ScreenDistanceScreenCoords,
                renderer.WindowWidth / 2 - mousex,
                renderer.WindowHeight / 2 - mousey);
            //LogFile.WriteLine("MouseVectorObserverAxes: " + MouseVectorObserverAxes);
            MouseVectorObserverAxes.Normalize();
            //LogFile.WriteLine("MouseVectorObserverAxes (normalized): " + MouseVectorObserverAxes);
            Vector3 MouseVectorWorldAxes = MouseVectorObserverAxes * OurRot.Inverse();
            //LogFile.WriteLine("MouseVectorWorldAxes: " + MouseVectorWorldAxes.ToString());
            MouseVectorWorldAxes.Normalize();
            return MouseVectorWorldAxes;
        }
Beispiel #7
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;
 }
Beispiel #8
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;
        }
Beispiel #9
0
 public static 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 );
 }
Beispiel #10
0
        void SetupFrustrum()
        {
            //LogFile.WriteLine("setup frustrum");

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

            viewray = mvMath.XAxis * inversecamerarot;
            viewray.Normalize();
            right = - mvMath.YAxis * inversecamerarot;
            up = mvMath.ZAxis * inversecamerarot;
            right.Normalize();
            up.Normalize();

            nearclip = RendererFactory.GetInstance().NearClip;
            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().OuterWindowWidth / RendererFactory.GetInstance().OuterWindowHeight;
            HFar = HNear * farclip / nearclip;

            //Console.WriteLine( "clips: " + nearclip + " " + farclip + " " + VNear + " " + VFar + " " + HNear + " " + HFar );

            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);
        }
Beispiel #11
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, 3.1415926535 );
            }
            else
            {
                double DotProduct = Vector3.DotProduct( VectorNorm1, VectorNorm2 );
                Test.Debug( "DotProduct: " + DotProduct.ToString() ); // Test.Debug
                double Vangle = Math.Acos( DotProduct );
                Test.Debug( "math: " + Vangle.ToString() ); // Test.Debug
                rResult = AxisAngle2Rot( RotationAxis, Vangle );
            }
            return rResult;
        }
Beispiel #12
0
        //public void MouseDown(string command, bool down)
        //{
        //Test.Debug("Playermovement MouseDown " + e.ToString() );
        //LogFile.WriteLine("PlayerMovement.MouseDown " + down + " " + bcapturing);
        //  if (ViewerState.GetInstance().CurrentViewerState == ViewerState.ViewerStateEnum.MouseLook)
        //            {
        //              InMouseMoveDrag = down;
        //        }
        //      else
        //    {
        //      InMouseMoveDrag = false;
        //}
        //}
        //public void MouseMove( object source, MouseEventArgs e )
        //{
        //Test.Debug("Playermovement MouseMove " + e.ToString() );
        //  if( _bcapturing )
        //{
        //  avatarzrot = startavatarzrot - (double)( e.X - istartmousex ) * fAvatarTurnSpeed;
        //avataryrot = Math.Min( Math.Max( startavataryrot + (double)( e.Y - istartmousey ) * fAvatarTurnSpeed, - 90 ), 90 );
        //UpdateAvatarObjectRotAndPos();
        //}
        //}
        //public void MouseUp( object source, MouseEventArgs e )
        //{
        //Test.Debug("Playermovement MouseUp " + e.ToString() );
        //bcapturing = false;
        //  _bcapturing = false;
        //}
        public void UpdateAvatarObjectRotAndPos()
        {
            avatarrot = mvMath.AxisAngle2Rot( mvMath.ZAxis, ( avatarzrot * Math.PI / 180 ) );
            avatarrot *= mvMath.AxisAngle2Rot( mvMath.YAxis, avataryrot * Math.PI / 180 );

            Entity avatar = MetaverseClient.GetInstance().myavatar;

            if( avatar != null )
            {
                avatar.pos = avatarpos;
                avatar.rot = avatarrot;
            }
        }
Beispiel #13
0
        void UpdateCamera()
        {
            PlayerMovement playermovement = PlayerMovement.GetInstance();

            if (bRoamingCameraEnabled)
            {
                camerapos = RoamingCameraPos;
                camerarot = RoamingCameraRot;
            }
            else if (viewpoint == Viewpoint.MouseLook)
            {
                camerapos = playermovement.avatarpos;
                camerarot =
                    mvMath.AxisAngle2Rot(mvMath.ZAxis, playermovement.avatarzrot * Math.PI / 180) *
                    mvMath.AxisAngle2Rot(mvMath.YAxis, playermovement.avataryrot * Math.PI / 180)
                    ;
                //cameramatrix.applyRotate(-playermovement.avataryrot, 0f, 1f, 0f);
                //cameramatrix.applyRotate(-playermovement.avatarzrot, 0f, 0f, 1f);
                //cameramatrix.applyTranslate(-playermovement.avatarpos.x, -playermovement.avatarpos.y, -playermovement.avatarpos.z);
            }
                /*
            else if (viewpoint == Viewpoint.BehindPlayer)
            {
                cameramatrix.applyRotate(-18f, 0f, 1f, 0f);

                // Vector3 V = new Vector3( 0, playermovement.avataryrot * mvMath.PiOver180, playermovement.avatarzrot * mvMath.PiOver180 );

                cameramatrix.applyTranslate(3.0f, 0.0f, -1.0f);

                cameramatrix.applyRotate(-(float)playermovement.avataryrot, 0f, 1f, 0f);
                cameramatrix.applyRotate(-(float)playermovement.avatarzrot, 0f, 0f, 1f);

                cameramatrix.applyTranslate(-playermovement.avatarpos.x, -playermovement.avatarpos.y, -playermovement.avatarpos.z);
            }
            else if (viewpoint == Viewpoint.ThirdParty)
            {
                cameramatrix.applyRotate(-18f, 0f, 1f, 0f);
                cameramatrix.applyRotate(-90f, 0f, 0f, 1f);

                cameramatrix.applyTranslate(0.0, -fThirdPartyViewZoom, fThirdPartyViewZoom / 3.0);
                cameramatrix.applyRotate(-fThirdPartyViewRotate, 0f, 0f, 1f);
                cameramatrix.applyTranslate(-playermovement.avatarpos.x, -playermovement.avatarpos.y, -playermovement.avatarpos.z);
            }
            */
        }
Beispiel #14
0
        public void GetCurrentCameraFromAltZoomCamera()
        {
            Rot RotationAboutZAxis =  mvMath.AxisAngle2Rot( mvMath.ZAxis, mvMath.Pi - fAltZoomRotateZAxis );
            Rot RotationAboutYAxis = mvMath.AxisAngle2Rot( mvMath.YAxis, fAltZoomRotateYAxis );

            Rot CombinedZYRotation = RotationAboutZAxis * RotationAboutYAxis;

            double DeltaZ = fAltZoomZoom * Math.Sin( fAltZoomRotateYAxis );
            RoamingCameraPos.z = AltZoomCentrePos.z + DeltaZ;

            double DistanceInXYPlane = fAltZoomZoom * Math.Cos( fAltZoomRotateYAxis );
            RoamingCameraPos.x = AltZoomCentrePos.x + DistanceInXYPlane * Math.Cos( fAltZoomRotateZAxis );
            RoamingCameraPos.y = AltZoomCentrePos.y - DistanceInXYPlane * Math.Sin( fAltZoomRotateZAxis );

            RoamingCameraRot = CombinedZYRotation;
        }