Ejemplo n.º 1
0
        private void ComputeDisplayParametersForThumbnail()
        {
            if (currentConstellation == "Error")
            {
                return;
            }
            IPlace  target    = Constellations.ConstellationCentroids[currentConstellation];
            Lineset boundries = Constellations.boundries[currentConstellation];

            Vector3[] points    = new Vector3[boundries.Points.Count];
            Vector3[] pointsOut = new Vector3[boundries.Points.Count];
            for (int i = 0; i < points.Length; i++)
            {
                points[i] = Coordinates.GeoTo3d(boundries.Points[i].Dec, boundries.Points[i].RA);
            }

            Matrix mat = Matrix.RotationY((float)(-(24 - (target.RA + 6)) / 12 * Math.PI));

            mat = Matrix.Multiply(mat, Matrix.RotationX((float)(target.Lat / 180f * Math.PI)));
            mat = Matrix.Multiply(mat, Matrix.Scaling(50, -50, 50));
            mat = Matrix.Multiply(mat, Matrix.Translation(48, 22, 0));
            // mat.Translate(58, 33,0);

            Vector3.TransformCoordinate(points, ref mat, pointsOut);

            lines = new PointF[points.Length + 1];

            int index = 0;

            foreach (Vector3 point in pointsOut)
            {
                lines[index++] = new PointF((float)point.X, (float)point.Y);
            }
            lines[index] = lines[0];
        }
Ejemplo n.º 2
0
        public void Execute()
        {
            var context = Services.ECS.GetContext <MainContext>();

            var mainCameraEntity = new Entity();

            if (!context.TryGetEntity <MainCameraComponent>(ref mainCameraEntity))
            {
                return;
            }

            var cameraComponent = new CameraComponent();

            if (!mainCameraEntity.TryGetComponent(ref cameraComponent))
            {
                return;
            }

            var deviceEntity = new Entity();

            if (!context.TryGetEntity <DeviceComponent>(ref deviceEntity))
            {
                return;
            }

            var deviceComponent = deviceEntity.GetComponent <DeviceComponent>();

            deviceComponent.Bmp.Lock();
            Clear(ref deviceComponent);
            var viewMatrix       = Matrix.LookAtLH(ToDxVector3(cameraComponent.Position), ToDxVector3(cameraComponent.Target), SharpDX.Vector3.Up);
            var projectionMatrix = Matrix.PerspectiveFovRH(0.78f, (float)deviceComponent.Bmp.PixelWidth / deviceComponent.Bmp.PixelHeight,
                                                           0.01f, 1.0f);

            var emptyTransformComponent = new TransformComponent();

            foreach (var entityId in context.GetAllEntities <MeshRendererComponent>())
            {
                var entity = context.GetEntity(entityId);

                var transform = emptyTransformComponent;
                if (!entity.TryGetComponent(ref transform))
                {
                    continue;
                }

                var mesh = entity.GetComponent <MeshRendererComponent>().Mesh;

                // Beware to apply rotation before translation
                var worldMatrix = Matrix.Scaling(transform.Scale.X, transform.Scale.Y, transform.Scale.Z) * Matrix.RotationYawPitchRoll(transform.Rotation.Y,
                                                                                                                                        transform.Rotation.X, transform.Rotation.Z) *
                                  Matrix.Translation(ToDxVector3(transform.Position));

                var transformMatrix = worldMatrix * viewMatrix * projectionMatrix;
                //Console.WriteLine(_farZ + " " + _nearZ);

                /*
                 * Parallel.ForEach(mesh.Triangles, (triangle) =>
                 * {
                 *  var vertexA = ToDxVector3(mesh.Vertices[triangle.A]);
                 *  var vertexB = ToDxVector3(mesh.Vertices[triangle.B]);
                 *  var vertexC = ToDxVector3(mesh.Vertices[triangle.C]);
                 *
                 *  var pixelA = Project(ref deviceComponent, vertexA, transformMatrix);
                 *  var pixelB = Project(ref deviceComponent, vertexB, transformMatrix);
                 *  var pixelC = Project(ref deviceComponent, vertexC, transformMatrix);
                 *
                 *  //var colorValue = (byte)((0.25f + (index++ % mesh.Triangles.Length) * 0.75f / mesh.Triangles.Length) * 255);
                 *  //var color = new Color { R = colorValue, G = colorValue, B = colorValue, A = 255 };
                 *
                 *
                 *
                 *  if (!(pixelA.Y == pixelB.Y && pixelA.Y == pixelC.Y))
                 *  {
                 *      if ((triangle.A == 0 && triangle.B == 1 && triangle.C == 2) || (triangle.A == 4 && triangle.B == 6 && triangle.C == 7))
                 *      {
                 *          DrawTriangle(ref deviceComponent, pixelA, pixelB, pixelC, Colors.Gray);
                 *      }
                 *
                 *
                 *  }
                 *  else
                 *  {
                 *      //DrawBLine(ref deviceComponent, new ScreenPoint(pixelA, vertexA.Z), new ScreenPoint(pixelB, vertexB.Z));
                 *      //DrawBLine(ref deviceComponent, new ScreenPoint(pixelB, vertexB.Z), new ScreenPoint(pixelC, vertexC.Z));
                 *      //DrawBLine(ref deviceComponent, new ScreenPoint(pixelC, vertexC.Z), new ScreenPoint(pixelA, vertexA.Z));
                 *  }
                 *
                 *  //continue;
                 *  //_currentColor = Colors.Red;
                 *  DrawBLine(ref deviceComponent, pixelA, pixelB, Colors.Red);
                 *  DrawBLine(ref deviceComponent, pixelB, pixelC, Colors.Red);
                 *  DrawBLine(ref deviceComponent, pixelC, pixelA, Colors.Red);
                 * });
                 */
                for (var i = 0; i < mesh.Triangles.Length; i++)
                {
                    var triangle = mesh.Triangles[i];
                    var vertexA  = ToDxVector3(mesh.Vertices[triangle.A]);
                    var vertexB  = ToDxVector3(mesh.Vertices[triangle.B]);
                    var vertexC  = ToDxVector3(mesh.Vertices[triangle.C]);

                    var pixelA = Project(ref deviceComponent, vertexA, transformMatrix);
                    var pixelB = Project(ref deviceComponent, vertexB, transformMatrix);
                    var pixelC = Project(ref deviceComponent, vertexC, transformMatrix);

                    //var colorValue = (byte)((0.25f + (index++ % mesh.Triangles.Length) * 0.75f / mesh.Triangles.Length) * 255);
                    //var color = new Color { R = colorValue, G = colorValue, B = colorValue, A = 255 };


                    if (!(pixelA.Y == pixelB.Y && pixelA.Y == pixelC.Y))
                    {
                        //if (i == 8 || i == 9)
                        //if ((triangle.A == 0 && triangle.B == 1 && triangle.C == 2) || (triangle.A == 4 && triangle.B == 6 && triangle.C == 7))
                        {
                            DrawTriangle(ref deviceComponent, pixelA, pixelB, pixelC, Colors.Gray);
                        }
                    }
                    else
                    {
                        //DrawBLine(ref deviceComponent, new ScreenPoint(pixelA, vertexA.Z), new ScreenPoint(pixelB, vertexB.Z));
                        //DrawBLine(ref deviceComponent, new ScreenPoint(pixelB, vertexB.Z), new ScreenPoint(pixelC, vertexC.Z));
                        //DrawBLine(ref deviceComponent, new ScreenPoint(pixelC, vertexC.Z), new ScreenPoint(pixelA, vertexA.Z));
                    }

                    //continue;
                    //_currentColor = Colors.Red;
                    DrawBLine(ref deviceComponent, pixelA, pixelB, Colors.Red);
                    DrawBLine(ref deviceComponent, pixelB, pixelC, Colors.Red);
                    DrawBLine(ref deviceComponent, pixelC, pixelA, Colors.Red);
                }
            }


            Present(ref deviceComponent);

            deviceComponent.Bmp.AddDirtyRect(new Int32Rect(0, 0,
                                                           deviceComponent.Bmp.PixelWidth, deviceComponent.Bmp.PixelHeight));
            deviceComponent.Bmp.Unlock();
        }
Ejemplo n.º 3
0
        public void SetViewRect(Coordinates[] corners)
        {
            if (corners.Length != 4)
            {
                return;
            }

            if (string.IsNullOrEmpty(currentConstellation))
            {
                return;
            }

            bool change = false;

            if (this.corners != null)
            {
                for (int i = 0; i < 4; i++)
                {
                    if (this.corners[i] != corners[i])
                    {
                        change = true;
                        break;
                    }
                }
            }
            else
            {
                change = true;
            }

            if (!change)
            {
                return;
            }

            this.corners = corners;

            IPlace target = Constellations.ConstellationCentroids[currentConstellation];

            Vector3[] points    = new Vector3[4];
            Vector3[] pointsOut = new Vector3[4];
            for (int i = 0; i < 4; i++)
            {
                points[i] = Coordinates.GeoTo3d(this.corners[i].Dec, ((this.corners[i].RA / 24.0 * 360) - 180));
            }


            Matrix mat = Matrix.RotationY((float)(-(24 - (target.RA + 6)) / 12 * Math.PI));

            mat = Matrix.Multiply(mat, Matrix.RotationX((float)(target.Lat / 180f * Math.PI)));
            mat = Matrix.Multiply(mat, Matrix.Scaling(60, -60, 60));
            mat = Matrix.Multiply(mat, Matrix.Translation(58, 33, 0));

            Vector3.TransformCoordinate(points, ref mat, pointsOut);
            fov = new PointF[5];

            int index = 0;

            foreach (Vector3 point in pointsOut)
            {
                fov[index++] = new PointF((float)point.X, (float)point.Y);
            }
            fov[4] = fov[0];

            //Calculate view length
            double a = fov[0].X - fov[1].X;
            double b = fov[0].Y - fov[1].Y;
            double c = Math.Sqrt(a * a + b * b);

            RefreshHint();
        }