Ejemplo n.º 1
0
        public void MakeFrustum()
        {
            WV = Matrix3d.MultiplyMatrix(World, View);

            Matrix3d viewProjection = Matrix3d.MultiplyMatrix(WV, Projection);

            WVP = viewProjection.Clone();

            Matrix3d inverseWorld = World.Clone();

            inverseWorld.Invert();

            // Left plane
            frustum[0].A = viewProjection.M14 + viewProjection.M11;
            frustum[0].B = viewProjection.M24 + viewProjection.M21;
            frustum[0].C = viewProjection.M34 + viewProjection.M31;
            frustum[0].D = viewProjection.M44 + viewProjection.M41;

            // Right plane
            frustum[1].A = viewProjection.M14 - viewProjection.M11;
            frustum[1].B = viewProjection.M24 - viewProjection.M21;
            frustum[1].C = viewProjection.M34 - viewProjection.M31;
            frustum[1].D = viewProjection.M44 - viewProjection.M41;

            // Top plane
            frustum[2].A = viewProjection.M14 - viewProjection.M12;
            frustum[2].B = viewProjection.M24 - viewProjection.M22;
            frustum[2].C = viewProjection.M34 - viewProjection.M32;
            frustum[2].D = viewProjection.M44 - viewProjection.M42;

            // Bottom plane
            frustum[3].A = viewProjection.M14 + viewProjection.M12;
            frustum[3].B = viewProjection.M24 + viewProjection.M22;
            frustum[3].C = viewProjection.M34 + viewProjection.M32;
            frustum[3].D = viewProjection.M44 + viewProjection.M42;

            // Near plane
            frustum[4].A = viewProjection.M13;
            frustum[4].B = viewProjection.M23;
            frustum[4].C = viewProjection.M33;
            frustum[4].D = viewProjection.M43;

            // Far plane
            frustum[5].A = viewProjection.M14 - viewProjection.M13;
            frustum[5].B = viewProjection.M24 - viewProjection.M23;
            frustum[5].C = viewProjection.M34 - viewProjection.M33;
            frustum[5].D = viewProjection.M44 - viewProjection.M43;

            // Normalize planes
            for (int i = 0; i < 6; i++)
            {
                frustum[i].Normalize();
            }
            frustumDirty = false;

            WVP.Scale(Vector3d.Create(Width / 2, -Height / 2, 1));
            WVP.Translate(Vector3d.Create(Width / 2, Height / 2, 0));
            SetMatrixes();
        }
Ejemplo n.º 2
0
        public static Matrix4d PutTheMatrix4dtogether(this Matrix4d mat4d, Vector3d T, Matrix3d Rotation)
        {
            //put the 4d matrix together
            Matrix3d r3D      = Rotation.Clone();
            Matrix4d myMatrix = new Matrix4d(r3D);

            myMatrix[0, 3] = T.X;
            myMatrix[1, 3] = T.Y;
            myMatrix[2, 3] = T.Z;
            myMatrix[3, 3] = 1f;

            return(myMatrix);
        }
Ejemplo n.º 3
0
        public void AddGlyphPoints(List <PositionTexture> pointList, Vector2d size, Rectangle position, Rectangle uv)
        {
            PositionTexture[] points = new PositionTexture[6];

            for (int i = 0; i < 6; i++)
            {
                points[i] = new PositionTexture();
            }



            Vector3d left  = Vector3d.Cross(center, up);
            Vector3d right = Vector3d.Cross(up, center);

            left.Normalize();
            right.Normalize();
            up.Normalize();

            Vector3d upTan = Vector3d.Cross(center, right);

            upTan.Normalize();

            if (alignment == Alignment.Center)
            {
                left.Multiply(width - position.Left * 2);
                right.Multiply(width - ((width * 2) - position.Right * 2));
            }
            else if (alignment == Alignment.Left)
            {
                left.Multiply(-position.Left * 2);
                right.Multiply(position.Right * 2);
            }

            Vector3d top    = upTan.Copy();
            Vector3d bottom = Vector3d.SubtractVectors(Vector3d.Empty, upTan);

            top.Multiply(height - position.Top * 2);
            bottom.Multiply(height - ((height * 2) - position.Bottom * 2));
            Vector3d ul = center.Copy();

            ul.Add(top);
            if (sky)
            {
                ul.Add(left);
            }
            else
            {
                ul.Subtract(left);
            }
            Vector3d ur = center.Copy();

            ur.Add(top);
            if (sky)
            {
                ur.Add(right);
            }
            else
            {
                ur.Subtract(right);
            }
            Vector3d ll = center.Copy();

            if (sky)
            {
                ll.Add(left);
            }
            else
            {
                ll.Subtract(left);
            }

            ll.Add(bottom);

            Vector3d lr = center.Copy();

            if (sky)
            {
                lr.Add(right);
            }
            else
            {
                lr.Subtract(right);
            }
            lr.Add(bottom);

            points[0].Position = ul.Copy();
            points[0].Tu       = uv.Left;
            points[0].Tv       = uv.Top;
            //     points[0].Color = Color;

            points[2].Tu       = uv.Left;
            points[2].Tv       = uv.Bottom;
            points[2].Position = ll.Copy();
            //      points[2].Color = Color;

            points[1].Tu       = uv.Right;
            points[1].Tv       = uv.Top;
            points[1].Position = ur.Copy();
            //      points[1].Color = Color;

            points[3].Tu       = uv.Right;
            points[3].Tv       = uv.Bottom;
            points[3].Position = lr.Copy();
            //      points[3].Color = Color;

            points[5].Tu       = uv.Right;
            points[5].Tv       = uv.Top;
            points[5].Position = ur.Copy();
            //     points[5].Color = Color;

            points[4].Tu       = uv.Left;
            points[4].Tv       = uv.Bottom;
            points[4].Position = ll.Copy();
            //     points[4].Color = Color;

            if (Rotation != 0 || Tilt != 0 || Bank != 0)
            {
                if (!matInit)
                {
                    Matrix3d lookAt    = Matrix3d.LookAtLH(center, new Vector3d(), up);
                    Matrix3d lookAtInv = lookAt.Clone();
                    lookAtInv.Invert();

                    rtbMat = Matrix3d.MultiplyMatrix(Matrix3d.MultiplyMatrix(Matrix3d.MultiplyMatrix(Matrix3d.MultiplyMatrix(lookAt, Matrix3d.RotationZ(-Rotation / 180 * Math.PI)), Matrix3d.RotationX(-Tilt / 180 * Math.PI)), Matrix3d.RotationY(-Bank / 180 * Math.PI)), lookAtInv);
                    //todo make this true after debug
                    matInit = true;
                }
                for (int i = 0; i < 6; i++)
                {
                    points[i].Position = Vector3d.TransformCoordinate(points[i].Position, rtbMat);
                }
            }

            foreach (PositionTexture pnt in points)
            {
                pointList.Add(pnt);
            }
        }
Ejemplo n.º 4
0
        public void SetupMatricesSolarSystem(bool forStars)
        {
            Lighting = Settings.Active.SolarSystemLighting;

            Space = false;
            if (SolarSystemTrack != SolarSystemObjects.Custom && SolarSystemTrack != SolarSystemObjects.Undefined)
            {
                ViewCamera.ViewTarget = Planets.GetPlanetTargetPoint(SolarSystemTrack, ViewCamera.Lat, ViewCamera.Lng, 0);
            }
            RenderTriangle.CullInside = false;


            double cameraDistance = SolarSystemCameraDistance;

            Matrix3d trackingMatrix = Matrix3d.Identity;

            cameraDistance -= 0.000001;

            bool activeTrackingFrame = false;
            //if (SolarSystemTrack == SolarSystemObjects.Custom && !string.IsNullOrEmpty(TrackingFrame))
            //{
            //    activeTrackingFrame = true;
            //    viewCamera.ViewTarget = LayerManager.GetFrameTarget(RenderContext, TrackingFrame, out trackingMatrix);
            //}
            //else if (!string.IsNullOrEmpty(TrackingFrame))
            {
                TrackingFrame = "";
            }


            Vector3d center = ViewCamera.ViewTarget;
            //Vector3d lightPosition = -center;

            double   localZoom = ViewCamera.Zoom * 20;
            Vector3d lookAt    = new Vector3d();

            Matrix3d viewAdjust = Matrix3d.Identity;

            viewAdjust.Multiply(Matrix3d.RotationX(((-ViewCamera.Lat) / 180f * Math.PI)));
            viewAdjust.Multiply(Matrix3d.RotationY(((-ViewCamera.Lng) / 180f * Math.PI)));

            Matrix3d lookAtAdjust = Matrix3d.Identity;


            bool dome = false;

            Vector3d lookUp;


            if (useSolarSystemTilt && !SandboxMode)
            {
                double angle = ViewCamera.Angle;
                if (cameraDistance > 0.0008)
                {
                    angle = 0;
                }
                else if (cameraDistance > 0.00001)
                {
                    double val = Math.Min(1.903089987, Util.Log10(cameraDistance) + 5) / 1.903089987;

                    angle = angle * Math.Max(0, 1 - val);
                }



                CameraPosition = Vector3d.Create(
                    (Math.Sin(-ViewCamera.Rotation) * Math.Sin(angle) * cameraDistance),
                    (Math.Cos(-ViewCamera.Rotation) * Math.Sin(angle) * cameraDistance),
                    ((Math.Cos(angle) * cameraDistance)));
                lookUp = Vector3d.Create(Math.Sin(-ViewCamera.Rotation), Math.Cos(-ViewCamera.Rotation), 0.00001f);
            }
            else
            {
                CameraPosition = Vector3d.Create(0, 0, ((cameraDistance)));

                lookUp = Vector3d.Create(Math.Sin(-ViewCamera.Rotation), Math.Cos(-ViewCamera.Rotation), 0.0001f);
            }


            CameraPosition = viewAdjust.Transform(CameraPosition);

            cameraOffset = CameraPosition.Copy();

            Matrix3d tmp = trackingMatrix.Clone();

            tmp.Invert();
            cameraOffset = Vector3d.TransformCoordinate(cameraOffset, tmp);



            lookUp = viewAdjust.Transform(lookUp);


            // WorldMatrix = Matrix3d.Identity;
            World     = Matrix3d.Identity;
            WorldBase = World.Clone();


            View = Matrix3d.MultiplyMatrix(Matrix3d.MultiplyMatrix(trackingMatrix, Matrix3d.LookAtLH(CameraPosition, lookAt, lookUp)), lookAtAdjust);


            ViewBase = View.Clone();


            Vector3d temp = Vector3d.SubtractVectors(lookAt, CameraPosition);

            temp.Normalize();
            temp = Vector3d.TransformCoordinate(temp, trackingMatrix);
            temp.Normalize();
            viewPoint = temp;



            //if (activeTrackingFrame)
            //{
            //    Vector3d atfCamPos = RenderContext11.CameraPosition;
            //    Vector3d atfLookAt = lookAt;
            //    Vector3d atfLookUp = lookUp;
            //    Matrix3d mat = trackingMatrix;
            //    mat.Invert();

            //    atfCamPos.TransformCoordinate(mat);
            //    atfLookAt.TransformCoordinate(mat);
            //    atfLookUp.TransformCoordinate(mat);
            //    atfLookAt.Normalize();
            //    atfLookUp.Normalize();

            //    CustomTrackingParams.Angle = 0;
            //    CustomTrackingParams.Rotation = 0;
            //    CustomTrackingParams.DomeAlt = viewCamera.DomeAlt;
            //    CustomTrackingParams.DomeAz = viewCamera.DomeAz;
            //    CustomTrackingParams.TargetReferenceFrame = "";
            //    CustomTrackingParams.ViewTarget = viewCamera.ViewTarget;
            //    CustomTrackingParams.Zoom = viewCamera.Zoom;
            //    CustomTrackingParams.Target = SolarSystemObjects.Custom;


            //    Vector3d atfLook = atfCamPos - atfLookAt;
            //    atfLook.Normalize();



            //    Coordinates latlng = Coordinates.CartesianToSpherical2(atfLook);
            //    CustomTrackingParams.Lat = latlng.Lat;
            //    CustomTrackingParams.Lng = latlng.Lng - 90;

            //    Vector3d up = Coordinates.GeoTo3dDouble(latlng.Lat + 90, latlng.Lng - 90);
            //    Vector3d left = Vector3d.Cross(atfLook, up);

            //    double dotU = Math.Acos(Vector3d.Dot(atfLookUp, up));
            //    double dotL = Math.Acos(Vector3d.Dot(atfLookUp, left));

            //    CustomTrackingParams.Rotation = dotU;// -Math.PI / 2;
            //}


            double radius = Planets.GetAdjustedPlanetRadius((int)SolarSystemTrack);


            if (cameraDistance < radius * 2.0 && !forStars)
            {
                nearPlane = cameraDistance * 0.03;

                //m_nearPlane = Math.Max(m_nearPlane, .000000000030);
                nearPlane = Math.Max(nearPlane, .00000000001);
                back      = 1900;
            }
            else
            {
                if (forStars)
                {
                    back      = 900056;
                    back      = cameraDistance > 900056 ? cameraDistance * 3 : 900056;
                    nearPlane = .00003f;

                    // m_nearPlane = cameraDistance * 0.03;

                    // back = 9937812653;
                    //  back = 21421655730;
                }
                else
                {
                    back = cameraDistance > 1900 ? cameraDistance + 200 : 1900;

                    // m_nearPlane = .0001f;
                    if (Settings.Active.SolarSystemScale < 13)
                    {
                        nearPlane = (float)Math.Min(cameraDistance * 0.03, 0.01);
                    }
                    else
                    {
                        nearPlane = .001f;
                    }
                }
            }


            Projection     = Matrix3d.PerspectiveFovLH((fovLocal), (double)Width / (double)Height, nearPlane, back);
            PerspectiveFov = fovLocal;
            fovAngle       = ((this.ViewCamera.Zoom) / FOVMULT) / Math.PI * 180;
            fovScale       = (fovAngle / Height) * 3600;

            SetMatrixes();
            MakeFrustum();
        }
Ejemplo n.º 5
0
        public void SetupMatricesSpace3d(double canvasWidth, double canvasHeight)
        {
            Lighting = false;
            if (!firstTimeInit)
            {
                galacticMatrix = Matrix3d.Identity;
                // -28.9361739586894, 17.7603329867975
                galacticMatrix.Multiply(Matrix3d.RotationY(-(270 - (17.7603329867975 * 15)) / 180.0 * Math.PI));
                galacticMatrix.Multiply(Matrix3d.RotationX(-((-28.9361739586894)) / 180.0 * Math.PI));
                galacticMatrix.Multiply(Matrix3d.RotationZ(((31.422052860102041270114993238783) - 90) / 180.0 * Math.PI));
                //galacticMatrix.Transpose();
                //galacticMatrix.Invert();
                firstTimeInit = true;
            }


            Space = true;
            RenderTriangle.CullInside = true;

            Matrix3d WorldMatrix = Matrix3d.Identity;

            if (Settings.Active.GalacticMode)
            {
                WorldMatrix.Multiply(galacticMatrix);
                WorldMatrix.Multiply(Matrix3d.RotationY(((az)) / 180.0 * Math.PI));
                WorldMatrix.Multiply(Matrix3d.RotationX(-((alt)) / 180.0 * Math.PI));
                double[] gPoint = Coordinates.GalactictoJ2000(az, alt);

                viewPoint        = Coordinates.RADecTo3dAu(gPoint[0] / 15, gPoint[1], 1.0);
                TargetCamera.Lng = this.RAtoViewLng(gPoint[0] / 15);
                TargetCamera.Lat = gPoint[1];
                ViewCamera.Lat   = TargetCamera.Lat;
                ViewCamera.Lng   = TargetCamera.Lng;
            }
            else
            {
                WorldMatrix.Multiply(Matrix3d.RotationY(-((ViewCamera.Lng - 90)) / 180.0 * Math.PI));
                WorldMatrix.Multiply(Matrix3d.RotationX(-((ViewCamera.Lat)) / 180.0 * Math.PI));
                viewPoint = Coordinates.RADecTo3dAu(RA, Dec, 1.0);
            }



            double camLocal = ((ViewCamera.Rotation /*+ 90*/));

            fovAngle = ((this.ViewCamera.Zoom) / FOVMULT) / Math.PI * 180;
            fovScale = (fovAngle / canvasHeight) * 3600;

            //Settings.Global.LocalHorizonMode = true;

            // altaz
            if (Settings.Active.LocalHorizonMode && backgroundImageset.DataSetType == ImageSetType.Sky)
            {
                Coordinates zenithAltAz = new Coordinates(0, 0);

                zenithAltAz.Az = 0;

                zenithAltAz.Alt = 0;

                Coordinates zenith = Coordinates.HorizonToEquitorial(zenithAltAz, SpaceTimeController.Location, SpaceTimeController.Now);
                //Coordinates zenith2 = Coordinates.HorizonToEquitorial(zenithAltAz, Coordinates.FromLatLng(1, 1), SpaceTimeController.Now);
                //Coordinates zenith3 = Coordinates.HorizonToEquitorial(zenithAltAz, Coordinates.FromLatLng(-1, 1), SpaceTimeController.Now);

                double raPart  = -((zenith.RA - 6) / 24.0 * (Math.PI * 2));
                double decPart = -(((zenith.Dec)) / 360.0 * (Math.PI * 2));
                string raText  = Coordinates.FormatDMS(zenith.RA);
                WorldMatrix = Matrix3d.RotationY(-raPart - Math.PI);
                WorldMatrix.Multiply(Matrix3d.RotationX(decPart));

                if (SpaceTimeController.Location.Lat < 0)
                {
                    WorldMatrix.Multiply(Matrix3d.RotationY(((az) / 180.0 * Math.PI)));

                    WorldMatrix.Multiply(Matrix3d.RotationX(((alt) / 180.0 * Math.PI)));
                    camLocal += Math.PI;
                }
                else
                {
                    WorldMatrix.Multiply(Matrix3d.RotationY(((-az) / 180.0 * Math.PI)));

                    WorldMatrix.Multiply(Matrix3d.RotationX(((-alt) / 180.0 * Math.PI)));
                }

                Coordinates currentRaDec = Coordinates.HorizonToEquitorial(Coordinates.FromLatLng(alt, az), SpaceTimeController.Location, SpaceTimeController.Now);

                ViewCamera.Lat = TargetCamera.Lat = currentRaDec.Dec;
                ViewCamera.Lng = TargetCamera.Lng = RAtoViewLng(currentRaDec.RA);
            }
            World     = WorldMatrix;
            WorldBase = WorldMatrix.Clone();
            // altaz



            double localZoomFactor = ViewCamera.Zoom;

            double FovAngle = ((localZoomFactor) / FOVMULT) / Math.PI * 180;

            CameraPosition = Vector3d.Create(0.0, 0.0, 0.0);
            // This is for distance Calculation. For space everything is the same distance, so camera target is key.

            View     = Matrix3d.LookAtLH(CameraPosition, Vector3d.Create(0.0, 0.0, -1.0), Vector3d.Create(Math.Sin(camLocal), Math.Cos(camLocal), 0.0));
            ViewBase = View.Clone();

            double m_nearPlane = .1;

            nearPlane  = .1f;
            Projection = Matrix3d.PerspectiveFovLH((localZoomFactor) / FOVMULT, (double)canvasWidth / (double)canvasHeight, .1, -2.0);


            SetMatrixes();

            MakeFrustum();
        }
Ejemplo n.º 6
0
        internal void SetupMatricesLand3d()
        {
            Lighting = false;
            Space    = false;
            RenderTriangle.CullInside = false;

            // For our world matrix, we will just rotate the Earth and Clouds about the y-axis.
            Matrix3d WorldMatrix = Matrix3d.RotationY(((ViewCamera.Lng - 90f) / 180f * Math.PI));

            WorldMatrix.Multiply(Matrix3d.RotationX(((-ViewCamera.Lat) / 180f * Math.PI)));
            World     = WorldMatrix;
            WorldBase = WorldMatrix.Clone();

            viewPoint = Coordinates.GeoTo3d(ViewCamera.Lat, ViewCamera.Lng);

            double distance = 0;

            if (backgroundImageset.IsMandelbrot)
            {
                distance = (4.0 * (ViewCamera.Zoom / 180)) + 0.00000000000000000000000000000000000000001;
            }
            else
            {
                distance = (4.0 * (ViewCamera.Zoom / 180)) + 0.000001;
            }
            fovAngle = ((this.ViewCamera.Zoom) / FOVMULT) / Math.PI * 180;
            fovScale = (fovAngle / Height) * 3600;

            if (gl != null)
            {
                targetAltitude = GetScaledAltitudeForLatLong(ViewCamera.Lat, ViewCamera.Lng);
                double heightNow = 1 + targetAltitude;
                targetAltitude *= NominalRadius;
                //if ((double.IsNaN(heightNow)))
                //{
                //    heightNow = 0;
                //}

                if (targetHeight < heightNow)
                {
                    targetHeight = (((targetHeight * 2) + heightNow) / 3);
                }
                else
                {
                    targetHeight = (((targetHeight * 9) + heightNow) / 10);
                }
                //if (double.IsNaN(targetHeight))
                //{
                //    targetHeight = 0;
                //}
            }
            else
            {
                targetAltitude = 0;
                targetHeight   = 1;
            }

            double rotLocal = ViewCamera.Rotation;

            CameraPosition = Vector3d.Create(
                (Math.Sin(rotLocal) * Math.Sin(ViewCamera.Angle) * distance),
                (Math.Cos(rotLocal) * Math.Sin(ViewCamera.Angle) * distance),
                (-targetHeight - (Math.Cos(ViewCamera.Angle) * distance)));
            Vector3d cameraTarget = Vector3d.Create(0.0f, 0.0f, -targetHeight);


            double camHeight = CameraPosition.Length();


            Vector3d lookUp = Vector3d.Create(Math.Sin(rotLocal) * Math.Cos(ViewCamera.Angle), Math.Cos(rotLocal) * Math.Cos(ViewCamera.Angle), Math.Sin(ViewCamera.Angle));

            View = Matrix3d.LookAtLH(
                (CameraPosition),
                (cameraTarget),
                lookUp);
            // * Matrix3d.RotationX(((-config.DomeTilt) / 180 * Math.PI));

            ViewBase = View;

            double back = Math.Sqrt((distance + 1f) * (distance + 1f) - 1);

            back = Math.Max(.5, back);
            // back = (float)camDist * 40f;
            double m_nearPlane = distance * .05f;

            m_nearPlane = distance * .05f;
            Projection  = Matrix3d.PerspectiveFovLH((Math.PI / 4.0), (double)Width / (double)Height, m_nearPlane, back);

            SetMatrixes();

            MakeFrustum();
        }